Jump to content
  • Advertisement
Sign in to follow this  
Manolis

OpenGL Small LINE CRISIS!!!

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

Hello I realy need help with something: Im using a resolution for my open gl window of 1400 x 1050 when I print the mouse position on screen it indeed goes from 0 to 1399 and 0 to 1049. What i desperetly need is some way to draw a line so that its vertexes are defined as specific pixels on the screen and not for example using glBegin(GL_LINES); glVertex2f(-5.0f,3.0f); glVertex2f(5.0f,1.0f); glEnd(); because here the numbers are float and dont realy coincide with the actual pixels its just the OpenGL coordinate system. I hope this makes sence... Thanx for your help :)

Share this post


Link to post
Share on other sites
Advertisement
You need to set your projection matrix to an orthogonal projection that maps coordinates to pixels. Try:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glIdentity();
glOrtho(-0.5, WIDTH+0.0, -0.5, HEIGHT+0.5);

//...draw stuff...

glPopMatrix();


I'm not 100% certain about the adjustments on WIDTH and HEIGHT - I know some adjustment is necessary due to pixel coordinates mapping to the center of the pixel rather than the top left corner, or vice versa, but I'm somewhat rusty on OpenGL at the moment. :)

Share this post


Link to post
Share on other sites
You just have to setup a proper ortho projection:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1400,1050,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Share this post


Link to post
Share on other sites
Thanx guys for your help. I tryied both the things you wrote me but they dont seem to work (snif snif) my int DrawGLScene(GLvoid) looks like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1400,1050,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_LINES);
glVertex2d(0,0);
glVertex2d(20,20);
glEnd();

It does not display anything?

Share this post


Link to post
Share on other sites
You were forgetting to store the state with a glPushMatrix()

glMatrixMode(GL_PROJECTION); // switch to projection matrix
glPushMatrix(); // save the state
glLoadIdentity(); // reset the matrix

glOrtho(0, 1400, 1050, 0, 0, 1); // set orthographic mode (left, right, bottom, top, near, far)

glMatrixMode(GL_MODELVIEW); // switch to modelview matrix
glLoadIdentity(); // reset the matrix


/*DRAW A DIAGONAL LINE (top left to bottom right)*/
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(1400.0f, 1050.0f);
glEnd();

glMatrixMode( GL_PROJECTION ); // return to projection matrix
glPopMatrix(); // return to old state

glMatrixMode( GL_MODELVIEW ); // return to modelview matrix





The process works as follows...

1. Switch to your projection matrix
2. Save the state with a glPushMatrix()
3. Reset that matrix
4. Call glOrtho to set your new MatrixMode
5. Select the modelview matrix and reset that too
6. Draw your stuff using 2d coordinates
7. When done, select the projection matrix again.
8. Call Popmatrix to put it back how it was
9. return to modelview matrix if you want to draw more 3d stuff

Share this post


Link to post
Share on other sites
You could also use gluUnproject() on your mouse coordinates to get a world coordinate that would yield that screen location. That way you don't have to mess with the matrix stacks.

-Auron

Share this post


Link to post
Share on other sites
Quote:
Original post by Manolis
Thanx guys for your help. I tryied both the things you wrote me but they dont seem to work (snif snif) my int DrawGLScene(GLvoid) looks like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1400,1050,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_LINES);
glVertex2d(0,0);
glVertex2d(20,20);
glEnd();

It does not display anything?


I just tried the exact same code and it draws a yellow line starting from the upper left corner.You must be doing something else wrong.Try disabling texturing and lighting,if those are on.

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!