Results 1 to 9 of 9

Thread: Can anyone see what Im doing wrong in this picking code?

  1. #1
    Member Contributor
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    93

    Can anyone see what Im doing wrong in this picking code?

    Code :
    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).]

  2. #2
    Member Contributor
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    93

    Re: Can anyone see what Im doing wrong in this picking code?

    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.

  3. #3
    Member Contributor
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    93

    Re: Can anyone see what Im doing wrong in this picking code?

    Any one?
    I still cant see where Im going wrong in the code.

  4. #4
    Member Contributor
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    93

    Re: Can anyone see what Im doing wrong in this picking code?

    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.

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Can anyone see what Im doing wrong in this picking code?

    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..

    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).]
    Deiussum
    Software Engineer and OpenGL enthusiast

  6. #6
    Member Contributor
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    93

    Re: Can anyone see what Im doing wrong in this picking code?

    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

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Can anyone see what Im doing wrong in this picking code?

    Yup. gluLookAt should be performed on the modelview. Just making sure.

    Sorry I can't be much more help, I haven't really done much with picking myself.
    Deiussum
    Software Engineer and OpenGL enthusiast

  8. #8
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,595

    Re: Can anyone see what Im doing wrong in this picking code?

    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

  9. #9
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Location
    Kingston, Jamaica, W.I.
    Posts
    282

    Re: Can anyone see what Im doing wrong in this picking code?

    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.

Similar Threads

  1. [HELP] something wrong with the code?
    By noobprogrammer89 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-13-2011, 11:59 AM
  2. picking works wrong
    By lenlod in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 07-11-2010, 05:00 AM
  3. Whats wrong with the picking?
    By Bilyboy in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 04-08-2004, 05:25 AM
  4. What's wrong with this code?
    By Seamoon in forum OpenGL: Advanced Coding
    Replies: 8
    Last Post: 02-26-2003, 09:07 AM
  5. What is wrong with this code?
    By John Jenkins in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 11-01-2002, 06:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean