How to snap a 2D Quad to the mouse cursor using OpenGL 3.0?
- by NoobScratcher
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.