PDA

View Full Version : strange color



beet6
01-20-2002, 08:00 PM
I wrote some code to draw a virtual hand in space; but after I used a light, the hand always shown as red, even after I deleted and disabled lighting. Some part of code pasted below. Thanks million.


void init(void)
{
R[0]=40;
R[1]=0;
R[2]=0;
R[3]=0;
R[4]=0;
R[5]=0;
R[6]=0;
R[7]=0;
R[8]=0;
R[9]=0;

m_bGetdata=true;
gData.init();
gData.upData();
for(i=0;i<10;i++)
Roffset[i]=gData.fJoint[i];

GLfloat ambientLight[]={1.0,1.0,1.0,1.0};
glEnable(GL_LIGHTING);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight );

GLfloat gray[]={1.0,1.0,1.0,1.0};
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFF USE,gray);

GLfloat Light_ambient[]={0.3,0.3,0.3,1.0};
GLfloat Light_diffuse[]={0.7,0.7,0.7,1.0};
glLightfv(GL_LIGHT0,GL_AMBIENT,Light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,Light_diffuse);
GLfloat position[]={5.0,0.0,0.0,1.0};
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHT0);

glEnable(GL_DEPTH_TEST);

glClearColor(0.0,1.0,1.0,1.0);
glShadeModel(GL_SMOOTH);

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// draw the palm

glPushMatrix();

glEnable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
glTranslatef(0.0,0.0,farView);
glTranslatef(0.0,-1.5,0.0);
glRotatef(Rx0,1.0,0.0,0.0);
glTranslatef(0.0,1.5,0.0);
glPushMatrix();
glScalef(3.0,3.0,0.5);
glutSolidCube(1.0);
glPopMatrix();

//draw the Thumb finger
glPushMatrix();
glTranslatef(-1.2,-1.0,0.0);
glRotatef(R[0],0.0,0.0,1.0);
glTranslatef(0.0,0.55,0.0);
glPushMatrix();
glScalef(0.8,1.1,0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef(0.0,0.55,0.0);
glRotatef(R[5],0.0,0.0,1.0);
glTranslatef(0.0,0.50,0.0); //offset for compact joint 0.55
glScalef(0.8,1.1,0.5);
glutSolidCube(1.0);
glPopMatrix();

//draw the index finger

glPushMatrix();
glTranslatef(-1.15,1.5,0.0);
glRotatef(R[1],1.0,0.0,0.0);
glTranslatef(0.0,0.6,0.0);
glPushMatrix();
glScalef(0.6,1.2,0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef(0.0,0.6,0.0);
glRotatef(R[6],1.0,0.0,0.0);
glTranslatef(0.,0.6,0.0);
glScalef(0.6,1.2,0.5);
glutSolidCube(1.0);
glPopMatrix();

//draw the middle finger

glPushMatrix();
glTranslatef(-0.3,1.5,0.0);
glRotatef(R[2],1.0,0.0,0.0);
glTranslatef(0.0,0.7,0.0);
glPushMatrix();
glScalef(0.7,1.4,0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef(0.0,0.7,0.0);
glRotatef(R[7],1.0,0.,0.0);
glTranslatef(0.0,0.65,0.0); //offset for 0.7
glScalef(0.7,1.4,0.5);
glutSolidCube(1.0);
glPopMatrix();

// ring finger
glPushMatrix();
glTranslatef(0.55,1.5,0.0);
glRotatef(R[3],1.0,0.0,0.0);
glTranslatef(0.0,0.65,0.0);
glPushMatrix();
glScalef(0.6,1.3,0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef(0.0,0.65,0.0);
glRotatef(R[8],1.0,0.0,0.0);
glTranslatef(0.0,0.6,0.0); //offset for joint 0.65
glScalef(0.6,1.3,0.5);
glutSolidCube(1.0);
glPopMatrix();

//pinky finger

glPushMatrix();
glTranslatef(1.3,1.5,0.0);
glRotatef(R[4],1.0,0.0,0.0);
glTranslatef(0.0,0.5,0.0);
glPushMatrix();
glScalef(0.4,1.0,0.5);
glutSolidCube(1.0);
glPopMatrix();
glTranslatef(0.0,0.5,0.0);
glRotatef(R[9],1.0,0.0,0.0);
glTranslatef(0.0,0.45,0.0); //for 0.5 offset
glScalef(0.4,1.0,0.5);
glutSolidCube(1.0);

glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);

glPopMatrix();
glShadeModel(GL_SMOOTH);

glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w,(GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0,(GLfloat)w/(GLfloat)h,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,farView);
}

void keyboard(unsigned char key, int x, int y)
{
switch(key){
case 'Q':
case 'q':
Rx0=(Rx0+5)%360;
glutPostRedisplay();
break;
case 'S':
case 's':
m_bGetdata=false;
break;
case 'R':
case 'r':
m_bGetdata=true;
gData.upData();
for(i=0;i<10;i++)
Roffset[i]=gData.fJoint[i];
break;
case 'E':
case 'e':
m_bGetdata=false;
exit(0);
break;
case 'f':
farView=(farView-5.0);
glutPostRedisplay();
break;
case 'g':
farView=(farView+5.0);
glutPostRedisplay();
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GL_DEPTH) ;
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutTimerFunc(30,update,0);
glutMainLoop();
return 0;
}

beet6
01-21-2002, 09:10 AM
really strange...
helppppppppp

mmcoder
01-21-2002, 09:44 AM
You're right, it shouldn't be red. I compiled it and got a white hand.

Lyr
01-22-2002, 04:18 AM
Try to disable everything you don't realy need.

GL_TEXTURE2D/1D
GL_LIGHTING
GL_FOG
...