"Slight" gluUnproject problem

Started by
-1 comments, last by Xiachunyi 20 years, 9 months ago
Hi, I''ve been trying to change my window coordinates into my opengl app''s coordinates for a long time and finally came up with this code.

void mouse_convert(int x, int y)
{
    int viewportCoords[4];
    float winx, winy, winz;
    double modelview_matrix[16], projection_matrix[16];
    glGetIntegerv(GL_VIEWPORT, viewportCoords);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);
    winy = float(viewportCoords[3])-float(y);
    winx=float(x);
    glReadPixels(x, int(winy), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winz);
    gluUnProject(winx,winy,winz,modelview_matrix,projection_matrix,viewportCoords,&gl_x,&gl_y,&gl_z);
}
As you can already guess, I am feeding the function via "mouse_convert(LOWORD(lParam), HIWORD(lParam));" under the WM_LBUTTONDOWN message, and transfering it over to "glTranslated()". The problem is that whenever I select an area of my opengl application, the point where my texture is generated at is skilled a little ways from my mouse pointer. The difference between between the actual cursor position and the generated texture "object" is not very far but certainly enough to cause a serious problem. The distance between the actual position and the generated object is random in two quadrants assuming my actual cursor is at (0,0). Now, when I am on the right side of my map, the quadrants that the "object" is generated relative to my cursor is in the 1st and 4th quadrants, while in the other half it is vice versa. Here is how I draw my object:

             glEnable(GL_BLEND);
             glBindTexture(GL_TEXTURE_2D, texture[1]);
	     glPushMatrix();
	     glColor4f(1.0,1.0,1.0,1.0);
	     glTranslated(gl_x,gl_y,-0.1);
	     draw_particle(0.08);
	     glPopMatrix();
	     glDisable(GL_BLEND);
 
Contents of draw_particle (It''s a multipurpose function for my particle generator too)

void draw_particle(float size)
{
    glBegin(GL_TRIANGLE_FAN);
        glTexCoord2f(0.0,0.0); glVertex3f(0.0,0.0,0.0);
        glTexCoord2f(1.0,0.0); glVertex3f(size,0.0,0.0);
        glTexCoord2f(1.0,1.0); glVertex3f(size,size,0.0);
        glTexCoord2f(0.0,1.0); glVertex3f(0.0,size,0.0);
    glEnd();
}
 
My coordinates in my opengl map that I am working with are: [0.54,0.0;0.54,0.35]<-Yea it is small Thanks for reading.

This topic is closed to new replies.

Advertisement