glMaterial

hello… i am confused and i need your help…
in my project, i have drawn sme shapes and given them different colours… now , i must add some lighting…

my question is… do i have to “produce” now the colours i had before by applying the command glMaterial appropriately for each colour??

cause, i think that there is a command that does it automatically… i mean apply the transformations to the existing colours…
is there??

i dont konw if you get me… but i dont understand many things about lighting…:frowning:

glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
glEnable ( GL_COLOR_MATERIAL ) ;

Then no need to change anything. glColor will be fine with lighting.

Read the docs for details and other options :
http://www.opengl.org/sdk/docs/man/xhtml/glColorMaterial.xml

Explanations on the old red book :
http://www.glprogramming.com/red/chapter05.html

(by the way all this is totally useless when you do GLSL shaders)

okkk.thanks for your help…
well i have done the following:

define my initi() like this:

void initi() {
	glutInitWindowPosition(50,50);
	glutInitWindowSize(width,height);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutCreateWindow("pacman");

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glOrtho(-50,50,-50,50,-2000,2000);
	gluPerspective(75,1,50,2000);


	glMatrixMode(GL_MODELVIEW);
	gluLookAt(-30,-30,90,0,0,0,0,1,0);

	//Enabling the shading model
	glEnable(GL_LIGHTING);

	//Enabling normal vector normalization
	glEnable(GL_NORMALIZE);

	//Disabling global ambient light (In this example no ambient light will be present.)
	GLfloat globalAmbient[]={0,0,0};
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,globalAmbient);

	//Defining the position of the point light source GL_LIGHT0 at (x,y,z)=(0,0,40)
	GLfloat light0Position[]={-30,-30,90,1};
	glLightfv(GL_LIGHT0,GL_POSITION,light0Position);

	//Defining "diffuse" lighting properties for GL_LIGHT0
	GLfloat light0Diffuse[]={1,1,1};
	glLightfv(GL_LIGHT0,GL_DIFFUSE,light0Diffuse);

	//Defining "specular" lighting properties for GL_LIGHT0
	GLfloat light0Specular[]={1,1,1};
	glLightfv(GL_LIGHT0,GL_SPECULAR,light0Specular);

	glEnable(GL_LIGHT0);
}

and a part of my display is this…

if (x[i][j]=='*') {
				glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
				glEnable ( GL_COLOR_MATERIAL ) ;
				glColor3f(1,0,0);
				drawcube(cnt1,cnt2,temp1,temp2);
				glDisable ( GL_COLOR_MATERIAL ) ;
			}
....
else if (x[i][j]=='#') {
				glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
				glEnable ( GL_COLOR_MATERIAL ) ;
				glColor3f(0,0,1);
				drawsphere(cnt1,cnt2,temp1,temp2);
				fan1=i;//apo8ikefsi 8eseon fantasmatos
				fan2=j;
				glDisable ( GL_COLOR_MATERIAL ) ;
			}

the problem is: all the spheres appearing black … and all the cubes have some of thei sides black (back and their left one)

my result is this:
image

That is because you see the back faces of the objects.
You need to :

  • make sure the polygons are defined in CCW order, and activate backface culling :
    glEnable(GL_CULL_FACE);
  • ask for a depth buffer :
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glEnable(GL_DEPTH_TEST);