Question

I'm trying to draw a shape in 2D, and I'm trying to use gluProject to convert between 3D and 2D coordinates. Basically my goal is to draw a point on (200,200) and the point will be drawn appropriately (given the starting position is 0,0,0).

I've initialized gluProject like this -

GLdouble modelView[16];
GLdouble projection[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX,modelView);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glGetIntegerv(GL_VIEWPORT,viewport);

and I'm calling it in my animate methods like this

double tx, ty, tz;
gluProject(0.0,0.0,0.0,modelView,projection,viewport,&tx,&ty,&tz);
cout<<"tx,ty,tz: "<<tx<<","<<ty<<","<<tz<<endl;

The values I get are weird (-9e61,-9e61,-9e61). I'm guessing it's because I'm multiplying by a different matrix than I should be. Any advice?

Was it helpful?

Solution

I've initialized gluProject like this -

There's your error right there. OpenGL is a state machine. There's no one-time-initialization. You need to fetch the matrices and viewport at the very time the OpenGL state machine is in the state you want to unproject to. If you fetch them e.g. right after context creation, you'll just get identity matrices and a viewport that may not resemble what you later got.

If your desire is to just draw a point at a specific location, look at radical7's answer.

OTHER TIPS

Your code as shown is correct. Make sure that you have a context bound (by calling either wglMakeCurrent, glxMakeCurrent, or equivalent in the windowing toolkit you're using), otherwise the values (including those from glGetError) won't be defined.

That said, I think you're not using OpenGL as well as you could. For example, if you do:

// Set up the projection transformation to match window coordinates
glMatrixMode( GL_PROJECTION );
glOrtho2D( 0, windowWidth, 0, windowHeight );

// No modeling transformation required for this rendering
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glColor3f( 1.0, 1.0, 1.0 );  // set the current color to white
glBegin( GL_POINTS );
glVertex2i( 200, 200 );
glEnd();

you should get a white point at pixel (200,200) in your window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top