OpenGL Selection Hates My Code!

Started by
0 comments, last by Leroy1891 20 years ago
I have been able to use selection while drawing basic primitives but when I try it with my class that loads an MD2 file no matter how big or where on the screen the model is drawn it always says the mouse cursor is on top. I project the 3D Model so it can be drawn inside a 2D box and whenever I use this method is when stuff doesn''t work. Here''s the code that calculates the projection. void LoadMD2::Draw2D(int X,int Y,int Width,int Height,float D) { float XDist,YDist,XRatio,YRatio; Vectors3f Top,Bottom,Left,Right,Pos; glPushMatrix(); glLoadIdentity(); //================= // D is Depth // MouseToOpenGL converts 2D coords to 3D Top=MS.MouseToOpenGL(X+(Width/2),Y+Height,D); Bottom=MS.MouseToOpenGL(X+(Width/2),Y,D); Left=MS.MouseToOpenGL(X,Y+(Height/2),D); Right=MS.MouseToOpenGL(X+Width,Y+(Height/2),D); Pos=MS.MouseToOpenGL(X+(Width/2),Y+(Height/2),D); XDist=Left.Distance(Right); // Width of bounding area in 3D space YDist=Top.Distance(Bottom); // Height of bounding area XRatio=XDist/Model.ModelDimensions.X; // calculate how much YRatio=YDist/Model.ModelDimensions.Y; // to scale model to fit // choose lowest scale val so we keep scale proportional if(YRatio < XRatio) XRatio=YRatio; //===================== glTranslatef(Pos.X,Pos.Y,Pos.Z); glScalef(XRatio,XRatio,XRatio); Draw(); // draw Model glPopMatrix(); }
Advertisement
It seems like whenever I use my MouseToOpenGL function while drawing something is when it doesn''t work.

I drew a line,triangle,quad,pentagon and sphere all using this
function and it did the same thing but when I typed in the vertices for the triangle by hand selection would work right with the triangle.

The function looks like this:

Vectors3f MouseShit::MouseToOpenGL(int X,int Y,float Z)
{
// DO NOT call this between glBegin & glEnd
GLdouble TempX;
GLdouble TempY;
GLdouble TempZ;
GLint Viewport[4];
GLdouble ModelView[16];
GLdouble Projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX,ModelView);
glGetDoublev(GL_PROJECTION_MATRIX,Projection);
glGetIntegerv(GL_VIEWPORT,Viewport);
gluUnProject((GLdouble)X,(GLdouble)Y,(GLdouble)Z, ModelView,Projection,Viewport,&TempX,&TempY,&TempZ);
Vectors3f TempV((float)TempX,(float)TempY,(float)TempZ);
return(TempV);
}

This topic is closed to new replies.

Advertisement