glOrtho use?

Hi. I’m having problems implementing the glOrtho projection and i’d like some help.

I have a scene with the drawing loop drawing a textured sphere.
It uses lighting and object lists.
I just want to add on top of this scene a 2d polygon.
(for menu usage)

the draw routine is like this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glPushMatrix();
glTranslatef(0, 0, cameraDistance);
glRotatef(cameraAngleX, 1, 0, 0);   // pitch
glRotatef(cameraAngleY, 0, 1, 0);   // heading
glPushMatrix(); // draw sphere
    if(animateFlag) angle += 0.5f;
    glRotatef(angle, 0.39875f, 0.91706f, 0.0f);
    glCallList(listId);     // render with display list
glPopMatrix();
glPopMatrix();

i just add this code

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glPopMatrix();

but it just results in a black screen…
any idea what i’m doing wrong.
Thanks

I think you made your viewport too small, try this and specify your vertices in screen space coordinates:

void SwitchToOrthographic(){
GLint viewport[4];
glGetInteger( GL_VIEWPORT );
glMatrixMode( GL_PROJECTION, &viewport );
glLoadIdentity();
glOrtho( 0, viewport[2], viewport[3], 0, 0, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glDisable( GL_DEPTH_TEST );
}

Oops, typo. I mean’t

void SwitchToOrthographic(){
GLint viewport[4];
glGetInteger( GL_VIEWPORT, &viewport );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, viewport[2], viewport[3], 0, 0, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glDisable( GL_DEPTH_TEST );
}

I’ve used your code but it can’t find
the glGetInteger() function.
i’m using VC++ 2008 with the opengl libraries provided with it.
I’m not using glut if that’s where it’s defined.
Do you know any other alternative?
Thanks, and sorry if i’m being a pain

try glGetIntegerv()

In Java I have 2 functions:

private void viewOrtho(GL10 gl, int x, int y){							// Set Up An Ortho View	
	gl.glMatrixMode(gl.GL_PROJECTION);					// Select Projection
	gl.glPushMatrix();							// Push The Matrix
	gl.glLoadIdentity();						// Reset The Matrix
	gl.glOrthof( 0, x , 0 , y, -1, 1 );				// Select Ortho Mode
	gl.glMatrixMode(gl.GL_MODELVIEW);					// Select Modelview Matrix
	gl.glPushMatrix();							// Push The Matrix
	gl.glLoadIdentity();						// Reset The Matrix
}

private void viewPerspective(GL10 gl)							// Set Up A Perspective View
{
	gl.glMatrixMode( gl.GL_PROJECTION );					// Select Projection
	gl.glPopMatrix();							// Pop The Matrix
	gl.glMatrixMode( gl.GL_MODELVIEW );					// Select Modelview
	gl.glPopMatrix();							// Pop The Matrix
}

And I use them in main rendering loop so:

viewOrtho(gl, width,height); // Where width and height are the screens dimensions
gl.glTranslate(0.375f,0.375f,0.0f); //moves the square from center of the screen towards top right corner
square.draw(gl); //draws a 2D square to the screen
viewPerspective(gl); //switching back to 3D drawing

draw 3D stuff

OK it worked fine now. thanks a lot!
It was a coordinates problem as it seems.