Hello I’m trying really hard to do the maths here but I have no clue what to do at all when it comes to converting coordinates?
Here whats happening :
1.) I’m trying to select a area.
2.) This area that is selected is created by left clicking on the window.
3.) OpenGL puts a quad and then I put a texture with a white coloring in the center in perspective projection onto the window and my shaders colorkey out the center.
4.) Whatever is under that quad is then selected.
But the problem :
I want too snap the quad to the cursor at all times when I left click I have no idea how to do that and this is my attempt to do just that using gluUnProject and glTranslatef();
oh before I show the code I’m using
WIN32 API
OpenGL 3.0
GLSL
Heres the code :
This function is what attempts to snap the quad to the mouse but fails dramatically.
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_MODELVIEW_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);
//FOR SPAMMING 1/0
// cerr << DrawArea << endl;
}
This function is being called everytime in the main loop here :
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
display();
}
return Msg.wParam;
}