Picking: not working properly

This is a discussion on Picking: not working properly within the Game Programming forums, part of the General Programming Boards category; Ugh. I can't seem to get anything to work correctly lately. Iv'e attempted to add mouse object picking, aided by ...

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070

    Question Picking: not working properly

    Ugh. I can't seem to get anything to work correctly lately.

    Iv'e attempted to add mouse object picking, aided by an example from the OGL superbible (found by searching these forums). However, no matter where I click, it reads that I clicked the highest name ID (ex, if I have 3 objects, it always says I clicked object 3).

    on rendering:
    Code:
    void prefab::renderprefabs()
    {
    	for(int i=0; i<num_prefabs; i++)
    	{
    		//if(!_ceFrustum.CubeInFrustum(prefabP[i].pos.x, prefabP[i].pos.y, prefabP[i].pos.z, 1.0f))
    		//{
    		//i++; }
    		
    		if(prefabP[i].type==0)
    		{
    			glPushMatrix();
    			glLoadName(i+1);
    			glMultMatrixf(&prefabP[i].matrix[0][0]);
    			drawsolidcube(&prefabP[i],1,1);
    			glPopMatrix();
    		}
    	}
    }
    picking code:
    Code:
    void processclicks(GLuint id)
    {
    	if(id==0)
    		printf("miss\n");
    	else if(id>0)
    		printf("hit %d\n", id);
    }
    
    #define BUFFER_LENGTH 64
    void ProcessSelection(int xPos, int yPos)
    	{
    	// Space for selection buffer
    	GLuint selectBuff[BUFFER_LENGTH];
    
    	// Hit counter and viewport storeage
    	GLint hits, viewport[4];
    
    	// Setup selection buffer
    	glSelectBuffer(BUFFER_LENGTH, selectBuff);
    	
    	// Get the viewport
    	glGetIntegerv(GL_VIEWPORT, viewport);
    
    	// Switch to projection and save the matrix
    	glMatrixMode(GL_PROJECTION);
    	glPushMatrix();
    
    	// Change render mode
    	glRenderMode(GL_SELECT);
    
    	// Establish new clipping volume to be unit cube around
    	// mouse cursor point (xPos, yPos) and extending two pixels
    	// in the vertical and horzontal direction
    	glLoadIdentity();
    	gluPickMatrix(xPos, viewport[3] - yPos, 2,2, viewport);
    
    	// Apply perspective matrix 
    	gluPerspective(50.0f, fAspect, 0.01, -1.0);
    
    	// Draw the scene
    	display();
    
    	// Collect the hits
    	hits = glRenderMode(GL_RENDER);
    
    	// If a single hit occured, display the info.
    	if(hits == 1)
    		processclicks(selectBuff[3]);
    
    	// Restore the projection matrix
    	glMatrixMode(GL_PROJECTION);
    	glPopMatrix();
    
    	// Go back to modelview for normal rendering
    	glMatrixMode(GL_MODELVIEW);
    }
    
    void mouse(int button, int state, int x, int y)
    {
    	if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    		ProcessSelection(x, y);
    }
    and before drawing I have
    Code:
    glInitNames();
    glPushName(0);
    The only lead I have to this, is that i'm probably just not doing it right ;p.

    thanx
    -psychopath
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    void processclicks(GLuint id)
    {
    	if(id==0)
    		printf("miss\n");
    	else if(id>0)
    		printf("hit %d\n", id);
    }
    .
    .
    .
    	if(hits == 1)
    		processclicks(selectBuff[3]);
    1) I'm not sure what glSelectBuffer does, but it seems to me you're only testing the last object
    2)processclicks should be doing more than checking the value if id. I'm not familiar with the glu libs, but I'm guessing what you want to do is multiply your mouse coords by the matrix you get from that ever so sexy looking gluPickMatrix function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  2. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  3. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  4. MergeSort function not working with randomn number
    By Madshan in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2005, 12:15 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21