Selection and glutPostRedisplay

Hi …
I am trying to learn Opengl, especially the selection and picking concepts… Somethings are not clear to me and I am getting confused thoroughly…

After selection and picking is the entire Scene rerendered ??? (as glutpostRedisplay is called always at the end).
To make my question clear, let us consider an example, say I want to change the color of a particular object when I click on it…In Select mode, I change the color of that object to something say, glColor3f(1.0, 0.0, 0.0);
and now get back to the render mode by calling glRenderMode (GL_RENDER);
After processing hits , glutpostRedisplay is called …so, display callback is called , which renders the entire scene in GL_RENDER mode…I dont understand the necessity to rerender the entire Scene again in render mode…If I do something in Select mode, I want those changes to remain, and not to be overwritten…
For example , consider my following code:

Here , I am drawing 2 small spheres and connecting them using a narrow cylinder.
When I click on a second sphere , I want to change it to red color. but when I click on it, all the objects change color, as all the objects are rerendered in the GL_RENDER mode with the current color which is glColor3f(1.0, 0.0, 0.0) i.e red.

#include <glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifndef CALLBACK
#define CALLBACK
#endif

float spin_x = 0;
float spin_y = 0;

GLuint startList;

typedef struct {
float x, y, z;
int id;
} Point;

Point p1 = { 1.0, 1.0, 0.5, 1 };
Point p2 = { 2.0, 2.0, 1.5, 2 };
Point p3 = { 3.4, 1.2, 2.2, 3 };
Point p4 = { 0.3, 1.4, 1.9, 4 };

void drawScene(Point p1, Point p2, int bondid, GLenum mode);

void CALLBACK errorCallback(GLenum errorCode)
{
const GLubyte *estring;

estring = gluErrorString(errorCode);
fprintf(stderr, "Quadric Error: %s
", estring);
exit(0);
}

void init(void)
{

GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 }; // Enabling Vertex Arrays = intializing arrays
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat model_ambient[] = { 0.5, 0.5, 0.5, 1.0 };

glClearColor(0.0, 0.0, 0.0, 0.0);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

}

void display(void)
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   drawScene(p1, p2, 11, GL_RENDER);  

}

void drawScene(Point p1, Point p2, int bondid, GLenum mode)
{

GLUquadricObj *qobj;
FILE *fp;

float h, num, den;

float theta , phi;

fp = fopen ("/Users/mrudula/qtfiles/quadricselection5/outfile",“a”);
startList = glGenLists(3);
qobj = gluNewQuadric();

h = sqrt(pow((p1.x-p2.x),2) + pow((p1.y-p2.y),2) + pow((p1.z-p2.z),2));
theta = (180/3.14156) * atan((p2.x-p1.x)/(p2.z-p1.z));
phi = (180/3.14156) * atan((p2.y-p1.y)/sqrt(pow((p2.z-p1.z),2)+pow((p2.x-p1.x),2)));

fprintf(fp,"in drawScene
");

gluQuadricCallback(qobj, GLU_ERROR,
errorCallback);

                   glMatrixMode (GL_MODELVIEW);

gluLookAt (0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

gluQuadricDrawStyle(qobj, GLU_FILL); // smooth shaded
gluQuadricNormals(qobj, GLU_SMOOTH);
glNewList(startList, GL_COMPILE);
gluSphere(qobj, 0.05, 20, 20);
glEndList();

gluQuadricDrawStyle(qobj, GLU_FILL); // flat shaded
gluQuadricNormals(qobj, GLU_FLAT);
glNewList(startList+1, GL_COMPILE);
gluCylinder(qobj, 0.01, 0.01, h, 15, 5);
glEndList();

gluQuadricDrawStyle(qobj, GLU_FILL); // smooth shaded
gluQuadricNormals(qobj, GLU_SMOOTH);
glNewList(startList+2, GL_COMPILE);
gluSphere(qobj, 0.05, 20, 20);
glEndList();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(spin_y, 1, 0, 0);
glRotatef(spin_x, 0, 1, 0);

glPushMatrix();

glDisable(GL_LIGHTING); // 1st sphere
glPushMatrix();
glTranslatef(p1.x, p1.y, p1.z);
if(mode == GL_SELECT)
glLoadName(p1.id);
glCallList(startList);
//glPopMatrix();

glPushMatrix();

glRotatef(theta, 0.0, 1.0, 0.0);
glRotatef(360-phi, 1.0, 0.0, 0.0);
if(mode == GL_SELECT)
glLoadName(bondid);
glCallList(startList+1);
glPopMatrix();

glDisable(GL_LIGHTING); // 2nd sphere
glPushMatrix();
glTranslatef(p2.x, p2.y, p2.z);
if(mode == GL_SELECT){
glLoadName(p2.id);
glColor3f(1.0, 0.0, 0.0);
}
glCallList(startList+2);

glPopMatrix();
glFlush();

fclose(fp);

}

void proces****s (GLint hits, GLuint buffer1[])
{
unsigned int i, j;
GLuint names, *ptr;
FILE *fp;

fp = fopen ("/Users/mrudula/qtfiles/quadricselection5/outfile",“a”);
fprintf (fp,"hits = %d
“, hits);
ptr = (GLuint ) buffer1;
for (i = 0; i < hits; i++) { /
for each hit */
names = *ptr;
fprintf (fp,” number of names for this hit = %d
“, names);
ptr++;
fprintf(fp, " z1 is %g;”, (float) *ptr/0x7fffffff);
ptr++;
fprintf(fp, " z2 is %g
“, (float) ptr/0x7fffffff);
ptr++;
fprintf (fp, " names are ");
for (j = 0; j < names; j++) { /
for each name */
fprintf (fp,”%d
", *ptr);
*ptr++;
}
}

fclose(fp);
}

void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
// glOrtho(0, 5, 0, 5, -10.0, 10.0);
else
glOrtho(-2.5*(GLfloat)w/(GLfloat)h,
2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

#define BUFSIZE 1024

int old_x, old_y;

void
mouse(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits, zero;
GLint viewport[4];
FILE *fp;

fp = fopen ("/Users/mrudula/qtfiles/quadricselection5/outfile",“a”);

if (button != GLUT_LEFT_BUTTON | | state != GLUT_DOWN)
return;

glGetIntegerv (GL_VIEWPORT, viewport);

glSelectBuffer (BUFSIZE, selectBuf);
glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();

gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
2.0, 2.0, viewport);

glOrtho(-2.5, 2.5, -2.5x, 2.5y, -10.0, 10.0);
fprintf(fp, "in Mouse
");
fflush(fp);
renderfromMouse();

glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();

hits = glRenderMode (GL_RENDER);

if(hits!=0)
proces****s (hits, selectBuf);

glutPostRedisplay();

fclose(fp);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 200);
glutCreateWindow(“Molecules”);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

thanx…