Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been having issues trying to snap a 2D Quad to the mouse cursor position I'm able :

1.) To get values into posX, posY, posZ

2.) Translate with the values from those 3 variables

But the quad positioning I'm not able to do correctly in such a way that the 2D Quad is near the mouse cursor using those values from those 3 variables eg."posX, posY, posZ"

I need the mouse cursor in the center of the 2D Quad.

I'm hoping someone can help me achieve this.

I've tried searching around with no avail.

Heres the function that is ment to do the snapping but instead creates weird flicker or shows nothing at all only the 3d models show up :

void display()

  for(std::vector<GLuint>::iterator I = cube.begin(); I != cube.end(); ++I) 


     if(DrawArea == true)

    glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

cerr << winZ << endl;

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

glGetDoublev(GL_PROJECTION_MATRIX, projection);

glGetIntegerv(GL_VIEWPORT, viewport);

gluUnProject(winX, winY, winZ , modelview, projection, viewport, &posX, &posY, & posZ);

    glBindTexture(GL_TEXTURE_2D, DrawAreaTexture);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);



    glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, DrawAreaSurface->w, DrawAreaSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, DrawAreaSurface->pixels);


    glBindTexture(GL_TEXTURE_2D, DrawAreaTexture);

glTranslatef(posX , posY, posZ);


     glTexCoord2f (0.0, 0.0);
     glVertex3f(0.5, 0.5, 0);

     glTexCoord2f (1.0, 0.0);
     glVertex3f(0, 0.5, 0);

     glTexCoord2f (1.0, 1.0);
     glVertex3f(0, 0, 0);

     glTexCoord2f (0.0, 1.0);
     glVertex3f(0.5, 0, 0);




I'm using :

OpenGL 3.0 WIN32 API C++ GLSL

if you really want the full source here it is -> , Its pretty messy.

share|improve this question

It might be easier to just use an Orthogonal projection matrix instead of unprojecting.

glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

Then take the mouse position on the screen and map it to the range 0 to 1


float mousePosX = mouseWindowPosX/windowWidth;
float mousePosY = mouseWindowPosY/windowHeight;

Then translate

glTranslatef(mousePosX , mousePosY, 0.0f);

Remember to switch back to your previous matrix afterwards.

share|improve this answer

If you want it centered you should draw the quad with the origin in the center. You will probably want it to face the screen as well, which involves some math with the rotation matrix (search for "camera facing billboard").

(Nitpick: you don't want to upload new texture data each frame do you?)

share|improve this answer
That's a nitpick...? – stephelton Apr 25 '13 at 23:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.