Results 1 to 3 of 3

Thread: Pickdepth Question

  1. #1
    Member Newbie
    Join Date
    Jan 2006
    Posts
    30

    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:

    Code :
    #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 proces****s(GLint hits, GLuint buffer[])
    {
       int i;
       unsigned j;
       GLuint names, *ptr;
     
       printf("hits = %d\n\n", hits);
       ptr = (GLuint *) buffer;
       for(i = 0; i < hits; i++)				/* for each hit  */
       {  
          names = *ptr;
          printf("Number of names for hit = %d\n", names); 
    	  ptr++;
          printf("z1 is %g\n", (float) *ptr/0x7fffffff); 
    	  ptr++;
          printf("z2 is %g\n", (float) *ptr/0x7fffffff); 
    	  ptr++;
          printf("The name is ");
          for (j = 0; j < names; j++)			/* for each name */
    	  {  
             printf("%d ", *ptr); 
    		 ptr++;		
          }
          printf("\n\n");
       }
    }
     
    //*********************************************************************\\
     
    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\n", gX); 
       printf("gY is %d\n\n", gY);
     
       if(button != GLUT_LEFT_BUTTON &amp;#0124;&amp;#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);
       proces****s(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(&amp;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; 
    }

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    659

    Re: Pickdepth Question

    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.

  3. #3
    Member Newbie
    Join Date
    Jan 2006
    Posts
    30

    Re: Pickdepth Question

    Thanks,

    I've got it working now.

    Cheers,

    Chris

Similar Threads

  1. Replies: 5
    Last Post: 04-23-2018, 12:03 AM
  2. Texture question and sphere question...
    By detzel in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-19-2001, 12:02 PM
  3. Matrix question (Not really an OGL Question)
    By KurtCob in forum OpenGL: Basic Coding
    Replies: 22
    Last Post: 04-04-2001, 07:55 AM
  4. Quake3 bsp question (not really openGL question)
    By ribblem in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 01-15-2001, 10:13 AM
  5. Replies: 2
    Last Post: 01-08-2001, 03:33 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