Skip to main content
GameDev.net gamedev.net
🔒 Locked

Point Selection Problem - Revisit

Started by Cirus Jun 2, 2006 at 10:44 PM 2 replies 700+ views
Original Post
Cirus
Cirus
Hi all, I am struggling to develop an application in openGL that allows me to pick a particular point at a time , change its color and drag it to a new position with respect to other points. With my understanding and hard efforts I have been able to draw all points but not getting the desired results. Following is the relevant portion of my code concerning the above described function:

//This function is responsible for dragging.
void Drag(int iPos_X , int iPos_Y)
{

 int i = iEle;
 int iIndex;

 //Var declarations for picking and selection.
 GLuint selectBuf[BUF_SIZE];
 GLint ViewPort[FOUR] ;
 GLint iHits = 0;

 memset(selectBuf,0,BUF_SIZE); //clear
 
    if(bMiddleButton) //If middle mouse button is pressed.
	{   
	 MarkPointOnScreen(iPos_X,iPos_Y, WINHEIGHT , WINWIDTH, NULL, pfTemp);
         
    // Commented for temporarily.Using selection and picking for  selection..This works only for the last point.
	 //pfArray[i - THREE] = pfTemp[0];
         //pfArray[i - TWO] = pfTemp[1];
         //pfArray[i - ONE] = pfTemp[2];
		 

	 //Modification required.Indexing of points neccessary.
	 glGetIntegerv(GL_VIEWPORT,ViewPort);
	 glSelectBuffer(BUF_SIZE,selectBuf);
		 
	 //Entering SELECT mode.
	 (void)glRenderMode(GL_SELECT);

	 //Initialising name stack.
	 glInitNames();
   	 glPushName(0);

	 glMatrixMode(GL_PROJECTION);
	 glPushMatrix();
	 glLoadIdentity();

	 //createpickmatrix.that is draw a viewing mode of size 1 * 1 pixel.
	gluPickMatrix((GLdouble)iPos_X , (GLdouble)(ViewPort[3] - iPos_Y) , 1.0 , 1.0 , ViewPort);
			   
	glOrtho(LEFT , RIGHT , BOTTOM, TOP ,MIN_LIMIT, MAX_LIMIT);
    			
	//Pick a point and turn its color as blue.
	drawPoint(GL_SELECT);//When mode is GL_SELECT then works as a Point picker.Aim is to return the index of the point picked.
          
        glMatrixMode(GL_PROJECTION); 
	glPopMatrix(); 
	glFlush();
         
    //Exiting from SELECT mode and entering NORMAL mode.
	 iHits = glRenderMode(GL_RENDER);
         processHits(iHits, selectBuf);
	}

glutPostRedisplay();


}//End of function
And the code underneath is for drawPoint() that actually draws points and displays them.Moreover, when draggin is performed, th aim of the function is to continuously display the changing positions of the point being dragged.

void drawPoint(GLenum mode)
{ 
    int i =0, j = 0, k = 0 ;
    static iVar = 0;
    int iIndex = 0;	

   

   //if( mode != GL_SELECT)
   
	   
	    SHIFT_COORD_SIGN = SIGN(5.0 , 0.0);

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LINE_STIPPLE);	
	   
	   // Set the material color to follow the current color
		glMatrixMode(GL_MODELVIEW);   
		glLoadIdentity();  // ...Deleting it gives weired effects.
   
   
	    glPushMatrix();
	    glRotatef(fAngle_Y , 0.0 , 1.0 , 0.0); //Move left 
	    glRotatef(fAngle_X , 1.0 , 0.0 , 0.0);  //Move above 
	    glRotatef(fAngle_Z , 0.0 , 0.0 , 1.0);  //Move above 
   
		glPointSize(POINT_SIZE);       
		
		  while( j < iNumOfClicks)	  
		  {  
			  
			  //SHIIFT_COOR_SIGN is used on assumption that camera moves along Z axis.
			 
			  //glVertex3f(SHIFT_COORD_SIGN * pfArray, pfArray[i + 1], pfArray[i + 2] );		  
			  if(bMiddleButton) // && mode == GL_SELECT) 
			  {
					
					 glColor3f(0.0 ,1.0 ,0.0);
				  
					 //Section added w.ref to GameDev forum post.
					 if (mode == GL_SELECT)
					 	glLoadName(j); 		//j is the point index here.			
					 						  
					 
					 glBegin(GL_POINTS); 
		        		glVertex3f(SHIFT_COORD_SIGN * pfArray, pfArray[i + 1], pfArray[i + 2] );		  
           			 glEnd();

				//	 if(mode == GL_SELECT)
					//	 glPopName();        
              
			  }
			  else
			  {
				      glColor3f(1.0, 0.0, 1.0);
				      glBegin(GL_POINTS); 
				  glVertex3f(SHIFT_COORD_SIGN * pfArray * fZoom_X, pfArray[i + 1] * fZoom_Y, pfArray[i + 2] *fZoom_Z); 
					  glEnd();
			  }
			  
			  bDefault = TRUE;  //Reset Flag. 
		
			  i += 3; 
			  ++j;
			  
		  }//End of While Loop
      
		
		glPopMatrix();
		i = 0; //Reset counter.
		j = 0; //Reset counter.
      
		

  glutSwapBuffers();
 
}//End of function drawPoint.

Problem is: When I press middle button of mouse, the color of each vertex turns green from original color purple.But when I drag a point, , I observe that number of Hits remains 0. With more efforts , I was able to drag the last drawn point only by supplying to current mouse cursor position to glVertex3f function. I need a sceneario where , if I draw 4 points and I pick the 2nd point or 3rd and drag it, the point must move with respect to other points. ( i.e position of other points must not change and after dragging operation, all points should be displayed. Help is appreciated.
Bob III
Bob III
Do you have mismatching push/pops of names? I see you initialize using pushname(0). But when you draw a point, you use loadname(name), followed by popname(). Load name doesn't push a name, it writes over the current top of the name stack. A glGetError call would probably show you that you had (name)stack underflow.
"That''s to do with the day I finally realized the world had gone totally mad and built the Asylum to put it in, poor thing, and hoped it would get better."-Wonko the SaneSo Long, and Thanks for All the Fish
Cirus
Cirus
Quote:
Original post by Bob III
Do you have mismatching push/pops of names? I see you initialize using pushname(0). But when you draw a point, you use loadname(name), followed by popname(). Load name doesn't push a name, it writes over the current top of the name stack. A glGetError call would probably show you that you had (name)stack underflow.


Thanks Bob for your response. I believe in you. I was testing with snippet given in the redbook and did some modification.

Can you point out what is wrong with the code given at the link :

http://www.gamedev.net/community/forums/topic.asp?topic_id=396357

( Above link is a link to my another post on this topic) . Thanks.
I am checking about Push and Pop operations as pointed by you for the code in this post.

Help Appreciated.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.