Jump to content
  • Advertisement
Sign in to follow this  
veeru

Opengl select mode to select an object.

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 Assignment
glPopName();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!