Phong lighting.

Hi, I need to implement Phong lighting on a 3d cube. Am I right in thinking that all I need to do to achieve this is have ambient, diffused and specular light?

I’ve given it a go but I don’t think it’s working properly. The entire cube is covered in a turquoise light. Here’s my code for the lighting.

	//Add ambient light
	GLfloat ambientColour[] = { 0.0f, 1.0f, 0.0f, 0.0f };
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColour);		//Specifies ambient intensity of the entire scene.

	//Add positioned light
	GLfloat lightColour0[] = { 0.0f, 0.0f, 1.0f, 0.0f };
	GLfloat lightPos0[] = { 0.0f, 0.0f, 2.0f, 1.0f };
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColour0);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightPos0);

	//Add directed light
	GLfloat lightColour1[] = {0.0f, 0.0f, 1.0f, 0.0f};
	GLfloat lightPos1[] = {0.0f, 0.0f, 2.0f, 1.0f};
	glLightfv(GL_LIGHT1, GL_SPECULAR, lightColour1);
	glLightfv(GL_LIGHT1, GL_SPECULAR, lightPos1);


If you want to set a light’s position (or direction for a directional light source) you need to pass GL_POSITION as second argument to glLightfv.

I’m not sure I do want to set a light’s position. I don’t fully understand how to do phong lighting. My understand was it was just ambient, diffused and specular light. Is that correct?

Thanks for the help!

My comment was targeted at these lines that you posted:

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColour0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightPos0);

The variable names suggest you want to set the light’s position, but you are overwriting the diffuse color of the light source.

Since you are not writing your own shaders, but instead are using the OpenGL fixed function lighting you get OpenGL’s lighting model - I think that is not 100% identical to Phong’s lighting model, but rather uses the Blinn-Phong model and adds an emission term; the difference is not huge in practice though.
Wikipedia has descriptions of the Phong and Blinn-Phong models and how they differ.

The lighting equation has emissive, ambient, diffuse and specular terms.

The contribution from the diffuse and specular terms depends upon the direction of the light relative to the vertex being lit, so you need to specify either a position or direction for the light in order for those terms to be meaningful.