Two problems with glOrtho2D()

Started by
1 comment, last by MadsGustaf 16 years, 3 months ago
I am trying to get OpenGL to use pixels instead of units, and change its coordinate system to (0,0) = top right of screen and (WIDTH,HEIGHT) = bottom right of the screen. It seems like this is working, but i can't seem to get OpenGL to use pixels for length units. code for, when the window is rezised/created
[source langg = "cpp"]
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
{
	if (height==0)										// Prevent A Divide By Zero By
	{
		height=1;										// Making Height Equal One
	}

	glViewport(0,0,width,height);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	gluOrtho2D(0,WIDTH,HEIGHT,0);
	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}

WIDTH = 800 HEIGHT = 600 The following code is supposed to draw my texture(top left corner at (0,0), this it is doing correctly so no problem there), but instead of producing a square, it produces a rectangle, width a greater height than width. It also seem to get the length units all wrong. ( i know i am translating into the screen, but if i don't, the image isn't being drawn, for some reason..)
[source langg = "cpp"]
int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();	
	glTranslatef(0.0f,0.0f,-2.0f);	
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);					
		glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 500,  0);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 500, 500,  0);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 500,  0,  0);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(0,  0,  0);	  //top left of the texture and quad
    glEnd();	
	return TRUE;										
}


Thanks in advance! -Mads
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
Advertisement
void SetOrtho(long Sizex, long SizeY)
{
glViewport(0,0,SizeX,SizeY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SizeX, SizeY, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

This works for me

--- Edit ---
Just wanted to mention, your gluPerspective call kills your gluOrtho2D call, since it sets 3d mode back again, would probably work with that removed. I have 2 functions, one for perspective and one for ortho, I first render my 3d scene, then switch to ortho and render my GUI overtop. I also work with a predefined width/height for my GUI, I always assume 1000x800 for the GUI (just random values i liked), no matter what the screen resolution is, so everything is the exact same size no matter the resolution, but depending on what you are doing you may or may not want that :)
Thanks :)! i Simply removed the gluPerspective line (didn't knew what it was doing anyway..) and its working wonderful now!
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!

This topic is closed to new replies.

Advertisement