Jump to content
  • Advertisement
Sign in to follow this  
MadsGustaf

OpenGL Two problems with glOrtho2D()

This topic is 3919 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!