Selection not working
Hi. I am trying to incorporate picking into a program of mine for the first time. I have stayed pretty close to NeHes stuff, but it''s still not working. I am storing the resulting id in a variable "Selection" and outputting that to the screen to see whether its working. The only reponse I get is 0, or no response at all.
The relevant code is as follows... Please let me know where I am going wrong - I have tried a variety of things from various places but they dont seem to work.
GLuint buffer[512];
GLint intersections=0;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(512, buffer);
(void) glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) (WinWidth-mpos.x), (GLdouble) (WinHeight-mpos.y), 1.0f, 1.0f, viewport);
glOrtho(0.0f,WinWidth,WinHeight,0.0f,-1.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
{
LoadIdentity();
for (int j = 0; j < Props.PropNumber+1; j++)
{
PushMatrix();
LoadName(j);
glTranslatef (Props.current[j].Position[0]+(float)x_move,Props.current[j].Position[1]+(float)y_move,0);
glRotatef((float)Props.current[j].Rotation, 0.0f, 0.0f, 1);
glBegin (GL_POLYGON);
for (int i = 0; i < Props.current[j].FrameCoords; i++)
{
glVertex2f(Props.current[j].PlaceHolder[0]-(float)Props.current[j].HDis,//+x_move,
Props.current[j].PlaceHolder[1]-(float)Props.current[j].VDis);//+y_move);
}
glEnd();
PopMatrix();
}
}
intersections=glRenderMode(GL_RENDER);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
if (intersections > 0)
{
int choose = buffer[3];
int depth = buffer[1];
for (int loop = 1; loop < intersections; loop++)
{
if (buffer[loop*4+1] < GLuint(depth))
{
choose = buffer[loop*4+3];
depth = buffer[loop*4+1];
}
}
Selection = choose;
}
Also, the glOrtho line… I assume it should be the same as how it is defined in the ReshapeGL proc? I believe I have it wrong in both places (it should be "glOrtho(0.0f,WinWidth,0.0f,WinHeight,-1.0f,1.0f);" yes?), I have tried correcting it but the only difference that makes is that everything goes upside down.
Any help would be greatly appreciated.
Thanks, Dy </i>
I''d suggest that you go
I''m not sure why, but thats how I am doing it:
" Do we need us? "
glMatrixMode(GL_PROJECTION);glPopMatrix();glMatrixMode(GL_MODELVIEW);intersections=glRenderMode(GL_RENDER);
I''m not sure why, but thats how I am doing it:
GLuint buffer[512]; // Set Up A Selection Buffer GLint hits; // The Number Of Objects That We Selected // The Size Of The Viewport. [0] Is <x>, [1] Is <y>, [2] Is <length>, [3] Is <width> GLint viewport[4]; // This Sets The Array <viewport> To The Size And Location Of The Screen Relative To The Window glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(512, buffer); // Tell OpenGL To Use Our Array For Selection (void) glRenderMode(GL_SELECT); glInitNames(); // Initializes The Name Stack glPushName(0); // Push 0 (At Least One Entry) Onto The Stack // Puts OpenGL In Selection Mode. Nothing Will Be Drawn. Object ID''s and Extents Are Stored In The Buffer. glMatrixMode(GL_PROJECTION); // Selects The Projection Matrix glPushMatrix(); // Push The Projection Matrix glLoadIdentity(); // Resets The Matrix // This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is. gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3]-y), 1.0f, 1.0f, viewport); // Apply The Perspective Matrix gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix // Draw one anchor for (int i = 0; i<lib.GetSize(); i++) { if(lib.Get(i) !=NULL) lib.Get(i)->Draw(); } //patch1.Draw(); glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPopMatrix(); // Pop The Projection Matrix glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix hits=glRenderMode(GL_RENDER); // Switch To Render Mode, Find Out How Many if (hits>0) // If There Were More Than 0 Hits { SelectedObj = buffer[3]; for (int i=0; i<lib.GetSize(); i++) { if( lib.Get(i)->GetID() == SelectedObj) { lib.Get(i)->Picked = true; if(strcmp(lib.Get(i)->GetName(),"BPatch") == 0) { EditMaterial.ShowWindow(SW_SHOW); EditMaterial.SetMaterial( &((CBezierPatch *)lib.Get(i))->m_material); EditMaterial.CheckRadioButton(ID_MAT_AMBIENT, ID_MAT_SPECULAR, ID_MAT_AMBIENT); } ObjProp.SetObject(GetObjSel(&lib)); } } }
" Do we need us? "
" Do we need us? "Ionware Productions - Games and Game Tools Development
Thanks for the response!
I tried that and it didnt seem to make too much difference. However, while testing it, I noticed that the screen seemed reversed so I changed the
glOrtho(0.0f,WinWidth,WinHeight,0.0f,-1.0f,1.0f);
line to
glOrtho(WinWidth,0.0f,WinHeight,0.0f,-1.0f,1.0f);
and now it seems to be working ok. Not entirely sure why, but there you go...
Dy
I tried that and it didnt seem to make too much difference. However, while testing it, I noticed that the screen seemed reversed so I changed the
glOrtho(0.0f,WinWidth,WinHeight,0.0f,-1.0f,1.0f);
line to
glOrtho(WinWidth,0.0f,WinHeight,0.0f,-1.0f,1.0f);
and now it seems to be working ok. Not entirely sure why, but there you go...
Dy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement