Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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()
{
  glClearColor(0.0,0.0,0.0,1.0);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);   

  for(std::vector<GLuint>::iterator I = cube.begin(); I != cube.end(); ++I) 
  {
      glCallList(*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);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);   

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

    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, DrawAreaTexture);

glTranslatef(posX , posY, posZ);

     glBegin(GL_QUADS);

     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);

     glEnd();

        }    

      SwapBuffers(hDC);            
}

I'm using :

OpenGL 3.0 WIN32 API C++ GLSL

if you really want the full source here it is -> http://pastebin.com/1Ncm9HNf , Its pretty messy.

share|improve this question
add comment

2 Answers

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

eg

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
add comment

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
add comment

Your Answer

 
discard

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.