ARGH!! glOrtho being weird!? :[

Started by
4 comments, last by haegarr 18 years ago
Okay, so I'm simply trying to set up an orthographic projection matrix. No big deal, right? I wish >:( The problem is, when I set glOrtho/gluOrtho2D nothing renders. Which is weird. Because I'm attempting to render a triangle primitive with vertices (0,0),(100,0),(0,100). First thing I did was to make sure that everything else is loaded right - removing the call to glOrtho (ie, leaving the matrix as the identity) produces triangles on the screen, so I know the render code isn't so wrong as to be the primary problem. So... this is where I ask for help. I've got a window set up with size 640x480. The viewport, by default, is set to the size of the window which is what I want. Calling glViewport(..) has no effect on the problem. When the application is loaded, it sets both matrices to the identity (which, again done for you automagically according to the Red Book) - glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); So we've got that. Now comes the call to glOrtho(..) which is being done every pass (because I'll need to be switching between rendering stuff in perspective mode and rendering the GUI in ortho mode) to simulate actual usage. The main loop looks like this -
while ( true ) { 
	gfx.clear();

	gluOrtho2D( 0, gfx.screenW(), 0, gfx.screenH() );

	glColor3f( 1, 1, 1 );
	
	glBegin( GL_TRIANGLE_STRIP );
	glVertex2i( 0, 1 );
	glVertex2i( 1, 0 );
	glVertex2i( 0, 0 );
	glEnd();

	gfx.flip();
}





Unit testing confirms that all of gfx's functions are working properly, therefore the problem must be the usage of gluOrtho2D. I want it to generate a matrix such that, well, *opens up MS Paint*... Where the points shown correlate to the vertex positions. So yeah... help? :( [Edited by - Mushu on April 13, 2006 1:42:16 AM]
Advertisement
Remember, opengl has (0,0) at the bottom left corner.
Try gluOrtho2D(0, width, height, 0) instead.
Also, the object you're rendering is only one unit across; using Ortho mode(with screen dimensions), this translates to ~1 pixel.
EDIT: Just to clarify that last statement: Since you specify pixel dimensions in gluOrtho2D, all your vertex coordinates should be pixel coordinates
Some very good points. Still, nothing is being rendered though. Grr.

The main loop has been changed to -

while ( true ) { 	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	gluOrtho2D( 0, 640, 480, 0 );	glColor3f( 1, 1, 1 );		glBegin( GL_TRIANGLE_STRIP );	glVertex2i( 0, 0 );	glVertex2i( 0, 100 );	glVertex2i( 100, 0 );	glVertex2i( 100, 100 );	glEnd();	SDL_GL_SwapBuffers();}


I swapped the arguments in gluOrtho2D as suggested, though I'd assume that would just flip the coordinates around - so that wouldn't be too much of a problem. I also made the triangle bigger, and a square. I've also removed the OO calls, which were for the most part a wrapper around the OGL calls. And still nothing works.

The only code left is the init code, but as something is rendered when the projection matrix is set to the identity, I'm assuming at this point that the init code kind of works. Its pretty short (yay SDL) so I might as well post it here too -

bool OGLGfxAdapter::_createScreenSurface( int screenW, int screenH, bool fullscreen ) {	if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {		return false;	}	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );			unsigned int flags = SDL_DOUBLEBUF | SDL_OPENGL | SDL_HWSURFACE;	if ( fullscreen ) flags |= SDL_FULLSCREEN;	_screen = SDL_SetVideoMode( screenW, screenH, 16, flags );	if ( !_screen )		return false;	glMatrixMode( GL_PROJECTION );	glLoadIdentity();	glMatrixMode( GL_MODELVIEW );	glLoadIdentity();	return true;}


This code is called from the constructor of OGLGfxAdapter with the parameters (640, 480, false).

And I'm going to hit the sack - I remember having a problem with glOrtho a while back, but I distinctly remember solving it. Arrgh! It seems I grow stupider with each passing day.

Anyone know what I'm doing wrong?
AHA.

gluOrtho2D(..) - the bastard - doesn't call glLoadIdentity before operating on the projection matrix. So calling it every pass screws up the matrix. The solution was to surround the call with nice little functions -

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 640, 480, 0 );
glMatrixMode( GL_MODELVIEW );

BAH. At least it works now :)

Thanks for the help, Melekor. I really appreciate it [smile]
Quote:Original post by Mushu
gluOrtho2D(..) - the bastard - doesn't call glLoadIdentity before operating on the projection matrix.

If it did, you wouldn't have the option NOT to reset it before applying the orthographic matrix.
To complete the list: All of glOrtho, glFrustum, gluOrtho2D, and gluPerspective multiply with the current matrix. (Interestingly is it mentioned explicitely in all of my man pages (SuSE linux) except in those of ... gluOrtho2D).

This topic is closed to new replies.

Advertisement