   Help Required - Pick ,Drag and Draw
  My task is to pick a point from a set of point, change color of the picked point and then chage its position thorough mouse drag such that positions of rest of the points do not change. Basically its a point selection problem I am tackling with.My previous post on it had my code.This time I am using a snippet from the Red Book and modifying a bit.Now I am trying to get printed the results of hits and hit-records when mouse is dragged. Given below is the code I am using ( You can run it and verify results).// BookEx.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<stdio.h>#include<glut.h>#define BUFFER 512int flag = 0;int iClick;float X = 0.0f , Y = 0.0f , Z = 0.0f;int Index[3];void processHits(GLint  , GLuint* ); void drawRects(GLenum); void  Drag(int x , int y){  GLuint selectBuf[BUFFER];GLint hits ; GLint viewport[4];X = x ;Y = y;  if(flag)  {    glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(BUFFER, selectBuf); (void )glRenderMode(GL_SELECT); glInitNames(); glPushName(0) ; glMatrixMode(GL_PROJECTION);glPushMatrix(); glLoadIdentity(); gluPickMatrix( (GLdouble)x, (GLdouble)(viewport[3] - y)  , 50.0 , 50.0 , viewport ); 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 init(void){glClearColor(0.0, 0.0 , 0.0 , 0.0 );glEnable(GL_DEPTH_TEST);glShadeModel(GL_FLAT);glDepthRange(0.0 , 1.0);}void processHits(GLint hits , GLuint buffer[]){ unsigned int i , j ;   GLuint names , *ptr;printf("hits = %d\n", hits); ptr = (GLuint *)buffer; for ( i = 0 ; i < (unsigned)hits ; i++)  {names = *ptr ;   printf(" number of names for hit = %d\n " , names);  ptr++;   printf("z1 is %g ", (float) *ptr / 0x7fffffff);  ptr++;  printf("z2 is %g ", (float) *ptr / 0x7fffffff);  ptr++;  printf("Name is \n");  for ( j = 0 ; j < names ; j++)  {  printf("%d\t", ptr); ptr++;  }  }}void drawRects(GLenum mode){    glPointSize(7);//while(iClick != 0)   { //if(mode == GL_RENDER)   {   //1st pt   glColor3f(1.0 , 1.0 , 0.0);   if(mode == GL_SELECT)   {   glColor3f(1.0 , 1.0 , 1.0);   glPushName(Index[0]);   glLoadName(Index[0]); }   glBegin(GL_POINTS); if(mode == GL_SELECT) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ;   glEnable(GL_COLOR_MATERIAL) ; glEnable(GL_DEPTH_TEST); glVertex3f( (float)(2  + X/(float)5000) , (float)(1  + Y/(float)5000),0);glPopName();// glPopName();  } else  { glVertex3i(2, 1 ,0);  Index[0] = 1; }   glEnd(); ///Second pt   glColor3f(0.0 , 1.0 , 1.0); if(mode == GL_SELECT)   {   glColor3f(1.0 , 1.0 , 1.0);   glPushName(Index[1]);   glLoadName(Index[1]);   }   glBegin(GL_POINTS); if(mode == GL_SELECT)  { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ;    glEnable(GL_COLOR_MATERIAL) ;  glEnable(GL_DEPTH_TEST);  glVertex3f( (float)(2  + X/(float)5000) , (float)(6  + Y/(float)5000)  ,0); glPopName();// glPopName();  }  else  {  glVertex3i(2, 6 ,0); Index[1] = 2;  }glEnd();    //3rd pt   glColor3f(1.0 , 0.0 , 1.0);   if(mode == GL_SELECT)   {   glColor3f(1.0 , 1.0 , 1.0);    glPushName(Index[2]);   glLoadName(Index[2]);   }   glBegin(GL_POINTS);if(mode == GL_SELECT) {glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ;   glEnable(GL_COLOR_MATERIAL) ; glEnable(GL_DEPTH_TEST); glVertex3f( (float)(6  + X/(float)5000), (float)(6  + Y/(float)5000) ,0);glPopName();    // glPopName(); } else  {  glVertex3i(6, 6 ,0);  Index[2] = 3; }glEnd(); //4th pt   glColor3f(1.0 , 0.0 , 0.0);   if(mode == GL_SELECT)   {   glColor3f(1.0 , 1.0 , 1.0);    glPushName(Index[3]);   glLoadName(Index[3]);   }   glBegin(GL_POINTS);if(mode == GL_SELECT) {glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ;   glEnable(GL_COLOR_MATERIAL) ; glEnable(GL_DEPTH_TEST); glVertex3f( (float)(6  + X/(float)5000), (float)(8  + Y/(float)5000) , 0);glPopName();    // glPopName(); } else  {  glVertex3i(6, 4 ,0);  Index[3] = 4; }glEnd();   }   } }void pickRects(int button , int state , int x , int y ){  if(button != GLUT_MIDDLE_BUTTON && state != GLUT_DOWN)return ; flag = 1; }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();}  int main(int argc, char* argv[]){glutInit(&argc , argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(500 , 500);glutInitWindowPosition(600,400); glutCreateWindow("Book Example");init();glutMouseFunc(pickRects);glutMotionFunc(Drag);  glutReshapeFunc(reshape);glutDisplayFunc(display);glutMainLoop();return 0;}As an addition , I am aiming to get point's index value on picking.As a first step, I indexed all the points but still picking is giving menumbers other than index value.To be precise, I want to pick a point using MIDDLE button of mouse andthen desire to change its position and color on drag.Now in the code given as above, I am able to change point'scolor using a boolean flag.i.e when middle button is pressed, the flag isset and so the color changes, but I am getting hits as 0.Problem is I am not able to pick point( because number of hits is 0) andchange its position on drag.I am using Orthographic projection through out my code.Help appreciated.[Edited by - Cirus on June 3, 2006 4:37:49 PM]
  This is the output I get on execution of posted code.//When Blue Point is picked.........................hits = 1 number of names for hit = 3 z1 is 0.333333 z2 is 0.333333 Name is 124227212422761242280hits = 1 number of names for hit = 3 z1 is 0.333333 z2 is 0.333333 Name is 124227212422761242280hits = 1 number of names for hit = 3 z1 is 0.333333 z2 is 0.333333........................................................... //When yellow point is picked.................................Name is 124227212422761242280hits = 1 number of names for hit = 4 z1 is 0.333333 z2 is 0.333333 Name is 1242272124227612422801242284hits = 1 number of names for hit = 4 z1 is 0.333333 z2 is 0.333333 Name is 1242272124227612422801242284hits = 1 number of names for hit = 4 z1 is 0.333333 z2 is 0.333333 Name is 1242272124227612422801242284hits = 1 number of names for hit = 4 z1 is 0.333333 z2 is 0.333333 Name is 1242272124227612422801242284........................................................//When red point is pickedhits = 0hits = 0hits = 0hits = 1.....................................................//When Yellow Point is picked......................... number of names for hit = 2 z1 is 0.333333 z2 is 0.333333 Name is 12422721242276hits = 1 number of names for hit = 2 z1 is 0.333333 z2 is 0.333333 Name is 12422721242276hits = 1 number of names for hit = 2 z1 is 0.333333 z2 is 0.333333 Name is 12422721242276hits = 1 number of names for hit = 2 z1 is 0.333333 z2 is 0.333333 Name is 12422721242276   ;  If the flag is not temporary, you may want to rethink its workings.I would recommend you querry glGetError() regularly to help you notice and track down errors as they unnavoidably arise. Myself, I call this in every displayCallback run. (Could be streamlined a little)void checkErrors() {  GLenum err = glGetError();  if( err != GL_NO_ERROR )    switch(err) {    case(GL_STACK_OVERFLOW): cout << "GL_STACK_OVERFLOW error\n"; break;    case(GL_STACK_UNDERFLOW): cout << "GL_STACK_UNDERFLOW error\n"; break;    case(GL_INVALID_OPERATION): cout << "GL_INVALID_OPERATION error\n"; break;    case(GL_INVALID_ENUM): cout << "GL_INVALID_ENUM error\n"; break;    case(GL_INVALID_VALUE): cout << "GL_INVALID_VALUE error\n"; break;    case(GL_OUT_OF_MEMORY): cout << "GL_OUT_OF_MEMORY error\n"; break;    default: cout << "Unrecognized error " << err << "\n"; break;    }}glPushName(Index[0]);glLoadName(Index[0]);The load after pushing the same thing is redundant but not damaging....//if(mode == GL_RENDER){  //1st pt  glColor3f(1.0 , 1.0 , 0.0);  if(mode == GL_SELECT)  {    ...If you're in render mode you arn't in selection mode...You may want to look over your program's logic.Also, instead of repeatedly clearing the depth and color buffers when in selection mode... don't, as in selection mode no changes are made to frame buffers.   