PDA

View Full Version : Can anyone see what Im doing wrong in this picking code?



blood.angel
05-07-2002, 12:54 PM
void draw_selection_triangles(void)
{
int face, square, vertex, triangle_count = 1;
GLint viewport[4];

glColor4f(0.0, 0.0, 0.0, 0.0);

glInitNames();
glPushName(0);

for(face = 0; face < 6; face++)
{
for(square = 0; square < 12; square++)
{
glPushMatrix();
glLoadName(triangle_count);
glBegin(GL_TRIANGLES);
for(vertex = 0; vertex < 3; vertex++)
{
glVertex3f(selection_triangles[face][square][vertex][0],
selection_triangles[face][square][vertex][1],
selection_triangles[face][square][vertex][2]);
}
glEnd();


glPopMatrix();
triangle_count++;
}
}

//now add the 2 scramble bitmap selection triangles.
//first change the viewport/projection so the triangles are drawn properly.


glLoadIdentity();

gluLookAt(camera.get_xposition(), camera.get_yposition(), camera.get_zposition(),
0.0, 0.0, 0.0,
camera.get_xupvector(), camera.get_yupvector(), camera.get_zupvector());

glGetIntegerv(GL_VIEWPORT, viewport);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, (GLfloat) viewport[2], 0.0, (GLfloat) viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//glLoadIdentity();
glLoadName(triangle_count);
glBegin(GL_TRIANGLES);
glVertex2f(10.0, 10.0);
glVertex2f(74.0, 10.0);
glVertex2f(74.0, 42.0);
glEnd();
glEnd();
glPopMatrix();

triangle_count++;

glPushMatrix();
//glColor3f(0.0, 1.0, 0.0);
glLoadName(triangle_count);
glBegin(GL_TRIANGLES);
glVertex2f(10.0, 10.0);
glVertex2f(74.0, 42.0);
glVertex2f(10.0, 42.0);
glEnd();
glEnd();
//glMatrixMode(GL_PROJECTION);
glPopMatrix();
// Restore the matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


The problem Im having is when Im drawing the last two selection triangles (which represent a 2D bitmap on the screen which is clickable by the user) the code always picks the last one. Its as if the triangle is drawn over the whole screen. But when I actually draw the selection triangles to the screen they are drawn in the right positions.


[This message has been edited by blood.angel (edited 05-07-2002).]

[This message has been edited by blood.angel (edited 05-09-2002).]

blood.angel
05-07-2002, 03:09 PM
Oh yeah, I forgot to mention it works perfectly when I remove the last two triangles. The first 72 have no trouble at all.
Another thing too, even when I click outside when no selection triangle occurs, it still picks the last triangle.

blood.angel
05-09-2002, 08:10 AM
Any one?
I still cant see where Im going wrong in the code.

blood.angel
05-09-2002, 12:55 PM
I am really near suicide over this.
Staring at the same section of code for the since I posted.
ANYONE?

I know it has something to do with the matrices.

Deiussum
05-10-2002, 04:37 AM
Sorry... I haven't done much with picking myself. I do know you need to use the same matrices for the picking as you used for the drawing, though. Just looking through the code I did notice a couple little oddities in the code..




glLoadIdentity();
gluLookAt(camera.get_xposition(),
camera.get_yposition(),
camera.get_zposition(),
0.0, 0.0, 0.0,
camera.get_xupvector(),
camera.get_yupvector(),
camera.get_zupvector());
glGetIntegerv(GL_VIEWPORT, viewport);
glMatrixMode(GL_MODELVIEW);


You switch to the MODELVIEW matrix after doing a gluLookAt? Does this mean you were previously using a PROJECTION matrix? If so, you usually should reserve the projection matrix for glFrustum/gluPerspective, glOrtho/gluOrtho2d. Doing a gluLookAt in the projection matrix rather than the modelview matrix can mess up things like fogging calculations. (As well as other things, I believe.)

Another thing that shouldn't really affect anything but... you have a couple places where you use the push/pop matrix, but nowhere between them do you make any changes to the matrix. This shouldn't cause a problem, but in these cases the push/pop is unnecessary. (Possibly you plan to add things later, or had things there you cut out?)

[This message has been edited by Deiussum (edited 05-10-2002).]

blood.angel
05-10-2002, 04:56 AM
Thanks for your reply.
No gluLookAt() is performed in the GL_MODELVIEW mode. Isnt that right?

Basically for the two last triangles I copied over the projection code I used for drawing the actual bitmap the two triangles represent. The code works for the bitmap ie it draws it to the correct 2D place I want it on the screen. But somehow for the two triangles it appears to draw them right but no matter where I click in the window the pick code always returns the triangle 73. THis is the first of the two triangles.

And for the point of Push/pops, it makes the code easier to understand for me. Though I dont understand whats going wrong http://www.opengl.org/discussion_boards/ubb/smile.gif

Deiussum
05-10-2002, 05:06 AM
Yup. gluLookAt should be performed on the modelview. Just making sure. http://www.opengl.org/discussion_boards/ubb/smile.gif

Sorry I can't be much more help, I haven't really done much with picking myself.

mikael_aronsson
05-10-2002, 11:47 AM
Hi !

Not sure if I get this right here, but you are modifing the matrices in the middle of the code, do you setup a selection first and then call this function, in that case and if you are using gluPickMatrix before the call, then you are messing up the projection matrix created by the gluPickMatrix....

Just an idea if that could be the problem....

Mikael

Furrage
05-13-2002, 05:10 AM
I agree with mikael. If you do anything to adjust your projection matrix you should complete picking first for that section, then set up the new matrix along with a new picking matrix, and pick again. The other problem I have is the call to glMatrixMode(GL_MODELVIEW) after you get your view port. It suggests that you were using the projection matrix which might cause problems.