Pickdepth Question

Hi,

I’m trying to develop an app that uses picking. More specifically, what I’m trying to do is change the colour of a rectangle when the user clicks on it.

I’ve had a look at the three examples in chapter 13 of the Red Book. The code I’m using is based on pickdepth.c.

When I run the app and click on the rectangle it detects the hit correctly but it doesn’t change colour.

What am I missing here?

The code I’m using is below:

#include <GL/openglut.h>
#include <stdlib.h>
#include <stdio.h>

#define BUFSIZE 512

int gX;
int gY;

//*********************************************************************\\

void init(void)
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_FLAT);
   glDepthRange(0.0, 1.0);  /* The default z mapping */
}

//*********************************************************************\\

void drawRects(GLenum mode)
{
   if(mode == GL_SELECT)					// Selection mode.
   {
		glLoadName(1);
		glBegin(GL_QUADS);
		glColor3f(1.0, 0.0, 0.0);    // Change colour here?
		glVertex3i(2, 2, 0);
		glVertex3i(2, 6, 0);
		glVertex3i(6, 6, 0);
		glVertex3i(6, 2, 0);
		glEnd();
   }
   else
   {	
		glBegin(GL_QUADS);					// Normal mode.
		glColor3f(1.0, 1.0, 0.0);
		glVertex3i(2, 2, 0);
		glVertex3i(2, 6, 0);
		glVertex3i(6, 6, 0);
		glVertex3i(6, 2, 0);
		glEnd();
   }
}

//*********************************************************************\\

void processHits(GLint hits, GLuint buffer[])
{
   int i;
   unsigned j;
   GLuint names, *ptr;

   printf("hits = %d

", hits);
   ptr = (GLuint *) buffer;
   for(i = 0; i < hits; i++)				/* for each hit  */
   {  
      names = *ptr;
      printf("Number of names for hit = %d
", names); 
	  ptr++;
      printf("z1 is %g
", (float) *ptr/0x7fffffff); 
	  ptr++;
      printf("z2 is %g
", (float) *ptr/0x7fffffff); 
	  ptr++;
      printf("The name is ");
      for (j = 0; j < names; j++)			/* for each name */
	  {  
         printf("%d ", *ptr); 
		 ptr++;		
      }
      printf("

");
   }
}

//*********************************************************************\\

void pickRects(int button, int state, int x, int y)
{
   GLuint selectBuf[BUFSIZE];
   GLint hits;
   GLint viewport[4];

   gX = x;
   gY = y;

   printf("gX is %d
", gX); 
   printf("gY is %d

", gY);

   if(button != GLUT_LEFT_BUTTON &#0124;&#0124; state != GLUT_DOWN)
      return;

   glGetIntegerv(GL_VIEWPORT, viewport);

   glSelectBuffer(BUFSIZE, selectBuf);
   (void) glRenderMode(GL_SELECT);

   glInitNames();
   glPushName(0);

   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();
	
   gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport);		 /*  create 5x5 pixel picking region near cursor location */
   glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
   drawRects(GL_SELECT);
   glPopMatrix();
   glFlush();

   hits = glRenderMode(GL_RENDER);
   processHits(hits, selectBuf);
   glutPostRedisplay();
}

//*********************************************************************\\

void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   drawRects(GL_RENDER);
   glFlush();
   glutSwapBuffers();
}

//*********************************************************************\\

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

//*********************************************************************\\

void keyboard(unsigned char key, int x, int y)
{
   switch(key)
   {
      case 27:
         exit(0);
         break;
   }
}

//*********************************************************************\\

int main(int argc, char **argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (200, 200);
   glutInitWindowPosition (100, 100);
   glutCreateWindow(argv[0]);
   init();
   glutReshapeFunc(reshape);
   glutDisplayFunc(display);
   glutMouseFunc(pickRects);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0; 
}
 

in the drawRects function, glColor3f(1.0, 1.0, 0.0) is always called (in render mode).

the comment “change colour here?” can be answered with “no”, because changing the colour in selection mode will not have the desired effect.

Thanks,

I’ve got it working now.

Cheers,

Chris