Archived

This topic is now archived and is closed to further replies.

Xiachunyi

Selecting objects problem

Recommended Posts

Hello, the following code samples will be refered to: RESOURCES
      
...
//~Object Selection IDs

//The element specification screen

#define SELECT_FIRE                     106

#define SELECT_WATER                    107

#define SELECT_EARTH                    108

#define SELECT_AIR                      109

//Sphere view


#define SELECT_SPHERE                   110

...
  
DRAWING SECTION
        
int DrawGLScene(GLvoid)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);
    glInitNames();
    glPushName(0); 
...
    if(view == 0) //Intro View

    {   
	    glLoadName(SELECT_WATER);
 	    glBegin(GL_LINES);
	    glEnd();
	    glPushMatrix();
        RenderSphere(2, 0.0, 0.0, 0.0, 0.7, 35,-1.5, 1.0, -5);
	    glPopMatrix();
	    glEnd();
	    glLoadName(SELECT_FIRE);
 	    glBegin(GL_LINES);							
	    glEnd();
	    glPushMatrix();	    
        RenderSphere(5, 0.0, 0.0, 0.0, 0.7, 35,-1.5, -1.0, -5);
	    glPopMatrix();
	    glEnd();	    
	    glLoadName(SELECT_EARTH);
 	    glBegin(GL_LINES);							
	    glEnd();
	    glPushMatrix(); 
        RenderSphere(4, 0.0, 0.0, 0.0, 0.7, 35,1.5, -1.0, -5);
	    glPopMatrix();
	    glEnd();
	    glLoadName(SELECT_AIR);
 	    glBegin(GL_LINES);							
	    glEnd();
	    glPushMatrix();            
        RenderSphere(3, 0.0, 0.0, 0.0, 0.7, 35,1.5, 1.0, -5);
	    glPopMatrix();
	    glEnd();
	    glPopName();             
        glEnable(GL_BLEND);
        glBindTexture(GL_TEXTURE_2D, texture[1]);
        particle_generate(-1.9, 1.4, -6, 1.0, 0.0, 0.0);
        particle_generate(-1.9, -1.4, -6, 0.5, 0.25, 0.0);
        particle_generate(1.9, 1.4, -6, 0.0, 0.0, 1.0);
        particle_generate(1.9, -1.4, -6, 1.0, 1.0, 1.0);
        glDisable(GL_BLEND);
    }
    if(view == 1) //Sphere View

    {
	    glLoadName(SELECT_SPHERE);
 	    glBegin(GL_LINES);
	    glEnd();
	    glPushMatrix();
        RenderSphere(0, 0.0, 0.0, 0.0, 1.5, 35,0.0, 0.0, -5);
	    glPopMatrix();
	    glEnd();
	    glPopName();                       
    }
  
SELECTION EVALUATION SECTION
        
int select(int x, int y)
{
	int objectsFound = 0;
	int	viewportCoords[8] = {0};
	unsigned int selectBuffer[64] = {0};
	glSelectBuffer(64, selectBuffer);
	glGetIntegerv(GL_VIEWPORT, viewportCoords);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glRenderMode(GL_SELECT);
	glLoadIdentity();
  	gluPickMatrix(x, viewportCoords[8] - y, 2, 2, viewportCoords);
  	gluPerspective(45.0f,(float)g_rRect.right/(float)g_rRect.bottom,0.1f,150.0f);
  	glMatrixMode(GL_MODELVIEW);
  	DrawGLScene();
  	objectsFound = glRenderMode(GL_RENDER);
  	glMatrixMode(GL_PROJECTION);
  	glPopMatrix();
  	glMatrixMode(GL_MODELVIEW);
	if (objectsFound > 0)
	{
		unsigned int lowestDepth = selectBuffer[1];
		int selectedObject = selectBuffer[4];
		for(int i = 1; i < objectsFound; i++)
		{
			if(selectBuffer[(i * 4) + 1] < lowestDepth)
			{
				lowestDepth = selectBuffer[(i * 4) + 1];
				selectedObject = selectBuffer[(i * 4) + 3];
			}
		}             	
        return objectsFound;
    }
	return 0;      	
}
  
LSRESULT CALLBACK CONTENTS FOR CLICKING
        
...
        case WM_LBUTTONDOWN:
        {
            char what[15]={0};
	        mouse_x = LOWORD(lParam);
			mouse_y = HIWORD(lParam);
			objectID = select(mouse_x, mouse_y);
			_itoa(objectID, numbuff, 10);
            strcat(what, numbuff);
            MessageBox(NULL, what, "Click", MB_OK);
			switch(objectID)
		    {
		       case SELECT_FIRE:
		       {
			      MessageBox(NULL, "FIRE", "Click", MB_OK);
		          break;
               }
		       case SELECT_WATER:
		       {
		          MessageBox(NULL, "WATER", "Click", MB_OK);
		          break;
               }
               case SELECT_EARTH:
		       {
		          MessageBox(NULL, "EARTH", "Click", MB_OK);
		          break;
               }
               case SELECT_AIR:
		       {
		          MessageBox(NULL, "AIR", "Click", MB_OK);
		          break;
               }
               case SELECT_SPHERE:
               {
		          MessageBox(NULL, "The Sphere", "Click", MB_OK);
		          break;               
               }
            }
            break;				    
        }
...
  
Problem: After compiling the above syntax, whenever I click anywhere on the screen, a zero ,contents of "objectID", is reported to me. Since that works, I decide to click on a sphere in order to get the following: fire, water, air, or earth message box, but a zero is still reported to me. The program doesn't seem to recoginize that an ID is assigned to whatever I drew, I don't know why. If I go to the second view, and click on another sphere, what supposly was to report the message box, "The Sphere", reported a 1 that indicates it is assigned to the ID of fire, but it doesn't activate the "fire" case. I tried to find out why this does not work for about 3 days now, but to no avail. I tried changing the IDs to 1,2,3,4, but they did not work, can anyone tell me why? Thanks a lot! [edited by - Xiachunyi on May 3, 2003 3:51:43 PM] [edited by - Xiachunyi on May 3, 2003 3:53:01 PM]

Share this post


Link to post
Share on other sites
Two things to look at. First make sure that your gluPerspective call is identical to the one used to render your scene. I''ve found this to be my most common problem. Second, in gluPixkMatrix you use viewportCoords[8] - y. I use viewportCoords[3] - y. I would actually expect [8] to potentially crash, since your array is only 8 long!

Share this post


Link to post
Share on other sites
Thanks everyone for your replies, I''ve been searching around and still haven''t found what the problem was with mine. I''ve implemented the changes, thanks to Variant, it no longer puts out zeros for the objectIDs, but only two digits, 1 and 2. 1 if there is no object there, and 2 if there is an object there, but 1 is suppose to be an object, 2 of course if an object, but what about 3 and 4? This is starting to irritate me, but I will try to push on.

Share this post


Link to post
Share on other sites
It looks like your select function is returning the number of objects hit. If you lined all your spheres up in a row, each hiding behind the next, and selected it, you should get 3 or 4 or 5. If you actually want the object Id, select needs to return selectedObject.

Share this post


Link to post
Share on other sites
Thanks again for your help! Finally, some changes that were made correctly. Right now... another problem, for some reason, when I click on the texture shape assigned as fire, it comes up with the ID of water. The rest are mixed up to, this is indeed strange, but I''ll try to figure this one out. Maybe when I was trying stuff out, I''ve accidently taken out or implemented a function that is messing things up.

Share this post


Link to post
Share on other sites