Opengl select mode to select an object.

Started by
8 comments, last by veeru 18 years, 8 months ago
Hi, i am not able to get the object select at every point of the object. Following is the code for my select funtion. I am drawing a bunch of lines in my draw function. I get the object ID at some point of the line only. I would appreicate if any one can help me and let me know what am i doing wrong? int selection(int x, int y) { GLuint selectBuf[512]; GLint hits; GLint viewport[4]; wglMakeCurrent (hDC, hgr); glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (512, selectBuf); glRenderMode (GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); // Selects The Projection Matrix glPushMatrix(); // Push The Projection Matrix glLoadIdentity(); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix((GLdouble) x,(GLdouble) (viewport[3]-y), 10.0f, 10.0f, viewport); gluPerspective(45.0f, (GLfloat) (1.33), 1.0f, 100.0f); glMatrixMode(GL_MODELVIEW); DrawGL(GL_SELECT); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf, x, y); return 1; }
Advertisement
I have not got response from anyone. Does any one know what am i doing wrong? What about using glReadPixel() function. Does anyone have example code that i can follow?

veeru
I did not understand your statement, " i am not able to get the object select at every point of the object. Following is the code for my select function. I am drawing a bunch of lines in my draw function. I get the object ID at some point of the line only."

We will need to see how you are rendering to determine what it is you are doing wrong.

For now, all I can offer it that you peruse this and see if you have anything that is different in your selection code and implementation.

Maybe "gluPerspective(45.0f, (GLfloat) (1.33), 1.0f, 100.0f);" needs to be replaced with "gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);"
I started with gluperspective(45.0f, (GLfloat)(viewport[2]-viewport[0]/viewport[3]-viewport[1], 0.1f, 100.0f) following the lesson 32 from NEHE website. I was getting same result. Basically what i am doing is draw bunch of lines. I am drawing line for point A to point B. If i click any point on the line, it should recognize that object correct? However, it does not. It only recognize the oject line at few point on the line. Following is my DrawGL() function.


int DrawGL(GLenum mode)
{
int i, j;
glColor3f(0.981500, 0.0, 0.0);

if(mode==GL_SELECT)
glLoadName(5);
glLineWidth(5.0);
for(i=0;i<(length-2); i++)
{
glBegin(GL_LINES);
glVertex3f(GLpoints.x, GLpoints.y, GLpoints.z);
glVertex3f(GLpoints[i+1].x, GLpoints[i+1].y, GLpoints[i+1].z);
glEnd();
}
}

So, if my mouse click happens on any of the line, it should tell me the object ID, right? It does not. It recognize the object ID at some certain point only. What is see is if x is between 250 to 260 and y is between 520 to 510.

One more thing if anyone has any example code using glcolor3ub() and reading the pixel back with glreadpixel().

Thank you very much for helping me out.
Before you start loading names, you will need to push a name onto the stack.

Example:
glInitNames();glPushName(0);glLoadName(LINE_NAME); //Start of ID Assignment   glBegin(...)       ...   glEnd();glEnd();               //End of ID AssignmentglPopName();
I am already doing that in my select(). Do i need to do that again?
Can you display for me your "select()" function and where you are implementing it?
Here is my select().


int selection(int x, int y)
{
GLuint selectBuf[512];
GLint hits;
GLint viewport[4];

wglMakeCurrent (hDC, hgr);
glGetIntegerv (GL_VIEWPORT, viewport);

glSelectBuffer (512, selectBuf);
glRenderMode (GL_SELECT);

glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION); // Selects The Projection Matrix
glPushMatrix(); // Push The Projection Matrix
glLoadIdentity();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix((GLdouble) x,(GLdouble) (viewport[3]-y), 10.0f, 10.0f, viewport);
gluPerspective(45.0f, (GLfloat) (1.33), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
DrawGL(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

hits = glRenderMode (GL_RENDER);

processHits (hits, selectBuf, x, y);

return 1;
}


Following my main loop function.
loop()
{
Init();
select();
DrawGL(GL_RENDER);
}

I want to get the object select on mouse move so. Loop function is getting call at mouse move.

Thank you very much for helping me out here.
You need to implement the initiating, pushing, and ending of the selection stack in your rendering code.

Your selection code is correct, but your rendering code will need to have those added to work.
I will try that and see what i get. Thank you very much fro your help.

This topic is closed to new replies.

Advertisement