PDA

View Full Version : color problem



beet6
01-15-2002, 02:11 PM
Hi, I am a beginner

I wrote some code to draw a virtual hand; it was white, but after I added a light it was shown red. Even delete the light and material property, it always stays red. Really confused thanks for your comments...

#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
//#include <getData.h>


static int R1=0,R2=45.0,R3=0,R4=0,R5=0,R6=0;
GLfloat nearView=-10.0;


void inti(void)
{
/* GLfloat mat_ambient[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={100.0};
GLfloat light_position[]={0.0,0.0,4.0,0,0};
GLfloat white_light[]={1.0,1.0,1.0,1.0};

glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_ambient);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
glLightfv(GL_LIGHT0,GL_AMBIENT,white_light);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// glEnable(GL_DEPTH_TEST); */

glClearColor(0.,0.,0.,0.);
glShadeModel(GL_SMOOTH);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

//|GL_DEPTH_BUFFER_BIT

// draw the palm
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0,0.0,-15.0);
glTranslatef(0.0,-1.5,0.0);
glRotatef(R1,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(R2,0.0,0.0,1.0);
glTranslatef(0.0,1.1,0.0);
glScalef(1.0,2.2,0.5);
glutSolidCube(1.0);
glPopMatrix();

//draw the index finger

glPushMatrix();
glTranslatef(-1.15,1.5,0.0);
glRotatef(R3,1.0,0.0,0.0);
glTranslatef(0.0,1.2,0.0);
glScalef(0.7,2.4,0.5);
glutSolidCube(1.0);
glPopMatrix();

//draw the middle finger

glPushMatrix();
glTranslatef(-0.3,1.5,0.0);
glRotatef(R4,1.0,0.0,0.0);
glTranslatef(0.0,1.4,0.0);
glScalef(0.8,2.8,0.5);
glutSolidCube(1.0);
glPopMatrix();

// ring finger
glPushMatrix();
glTranslatef(0.55,1.5,0.0);
glRotatef(R5,1.0,0.0,0.0);
glTranslatef(0.0,1.25,0.0);
glScalef(0.7,2.5,0.5);
glutSolidCube(1.0);
glPopMatrix();

//pinky finger

glPushMatrix();
glTranslatef(1.25,1.5,0.0);
glRotatef(R6,1.0,0.0,0.0);
glTranslatef(0.0,1.0,0.0);
glScalef(0.5,2.0,0.5);
glutSolidCube(1.0);
glPopMatrix();

glPopMatrix();

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,-15.0);
}


void keyboard(unsigned char key,int x,int y)
{
switch(key){
case 'z':
R1=(5+R1)%360;
glutPostRedisplay();
break;
case 'x':
R2=(5+R2)%360;
glutPostRedisplay();
break;
case 'c':
R3=(5+R3)%360;
glutPostRedisplay();
break;
case 'v':
R4=(5+R4)%360;
glutPostRedisplay();
break;
case 'b':
R5=(5+R5)%360;
glutPostRedisplay();
break;
case 'n':
R6=(5+R6)%360;
glutPostRedisplay();
break;
case 'Y':
nearView=(nearView-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]);
inti();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Questions Burner
01-16-2002, 01:56 AM
Did you calculate the normals and use glNormal to set the normals?

mikael_aronsson
01-16-2002, 03:21 AM
Hi !

As you have enabled lighting the glColor() call is useless, the glMaterialfv() is used to decide the "color" of the primitives, but you missed the diffuse color in your glMaterialfv calls, you just set the ambient and specular values, this could be the problem.

Mikael

beet6
01-16-2002, 12:38 PM
An example in Red book shows exactly as I did: only set the GL_SPECULAR and GL_SHINIESS parameters...