PDA

View Full Version : Keeping a 2D overlay in a fix size while zooming



Rakefet
11-04-2011, 10:22 AM
All,

I draw an overlay of a triangle shape over an image. The triangle tip points to a known location (x,y,z) on the image. I want the triangle to stay the same size even if I zoom in.
Here is my code:
glDisable(GL_DEPTH_TEST);
glPushMatrix()
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glTranslatef(x, y, z);
glBegin(GL_TRIANGLES);
glColor4f(1,0,0,0.75);
glVertex3f(0,0,0);
glVertex3f(0,0-0.5f,0);
glVertex3f(0+0.5f,0,0);
glEnd();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glPopMatrix();

When I zoom in, the triangle is still points to the same point of the image but its size get bigger.

Also, is there any redundancy in the call to those functions?

Thank you for your help.

Rakefet
11-17-2011, 11:02 AM
I figured out the solution and I want to share it with you all.
//Step 1: Convert from world coordinates to display window coordinates:
double modelMatrix[16];
double projMatrix[16];
int viewPort[16];
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewPort);

valid = gluProject(x, y , z, modelMatrix, projMatrix, viewPort, &winx, &winy, &winz);

//Step 2: Set up 2D ortho projection
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, cam.winWid, 0, cam.winLen);

glTranslatef(winx, winy, 0.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef (0.375, 0.375, 0.); // for exact pixelization
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);

glBegin()
//your drawing
glEnd


//clean up
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();