Sign in to follow this  

OpenGL coordinate question

This topic is 3725 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

Hi All, I have a question about the coordinate system in OpenGL. I want to be able to control the screen coordinates so that they behave like so: -1,1================1,1 -1,-1===============1,-1 I.e when I translate a quad to say 0.0, 0.0 it should draw in the center of the screen. At present I am trying to make a game that draws in 2d using Opengl/SDL. I have been basing a lot of my code from the OpenGL examples in NeHe's tutorials. I have also been reading the red book (totally excellent) but I am still having trouble understanding how to define the screen coordinate system (is that even the correct term to use for what I want?). Anyway, at present to get my quads to draw in the top corner I have to translate them to something like (-3,2) which is totally not what I want. The following function is what I use to initialize my opengl settings in my Graphics class: void Graphics::InitGL(int widthScreen, int heightScreen) { glViewport(0, 0, widthScreen, heightScreen); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer glDepthFunc(GL_LESS); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)widthScreen/(GLfloat)heightScreen,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); } This is a function I use to draw a circle: void Graphics::DrawCircle(float x, float y, float radius) { float DEG2RAD = 3.14159/180; glPushAttrib(GL_ALL_ATTRIB_BITS); glLoadIdentity(); glPushMatrix(); glTranslatef(x, y,-6.0f); glBegin(GL_TRIANGLE_FAN); glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green for (int i=0; i < 360; i++) { float degInRad = i*DEG2RAD; glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius); } glEnd(); glPopMatrix(); glPopAttrib(); } Any comments or critique about the way I am going about this would be most appreciated. Also, any links to any tutorials or references to parts of the red book that explain this would be most welcome (I am rereading chapter 3 at present). Thanks in advance for all the help.

Share this post


Link to post
Share on other sites
It sounds like you want an orthographic projection.

Take a look at glOrtho(). To get the effect you describe in your post, you would use it as follows:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
It sounds like you want an orthographic projection.

Take a look at glOrtho(). To get the effect you describe in your post, you would use it as follows:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);


Thanks Jyk, I saw that in an allegro/OpenGL tutorial. When I change my Opengl init code to do that projection I still get weird results. Like translatf(0.5,0.5,1); makes my circle disappear from the screen completely (I set my screen res to 640x480 with sdl and disabled depth check). I will play around with it some more maybe I am just doing something stupid. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
It sounds like you want an orthographic projection.

Take a look at glOrtho(). To get the effect you describe in your post, you would use it as follows:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);


Gah, I left in this line:

//gluPerspective(45.0f,(GLfloat)widthScreen/(GLfloat)heightScreen,0.1f,100.0f);

That was what screwed the whole thing up! Ah well, it made me read chapter 3 of the redbook again and I learned more about different projections. Thanks for the help.

Share this post


Link to post
Share on other sites

This topic is 3725 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this