Small LINE CRISIS!!!

Started by
6 comments, last by Manolis 19 years, 8 months ago
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 :)
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. :)
You just have to setup a proper ortho projection:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1400,1050,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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?

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
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
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.
PPL Thanx soooo mutch my program is looking sweeeeeet!!! THanx again, this forum ROCKS

This topic is closed to new replies.

Advertisement