PDA

View Full Version : Phong lighting.



JaAnTr
11-13-2014, 10:23 AM
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);



Thanks

carsten neumann
11-13-2014, 11:02 AM
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.

JaAnTr
11-13-2014, 11:29 AM
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!

carsten neumann
11-13-2014, 12:32 PM
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 (http://en.wikipedia.org/wiki/Phong_reflection_model) and Blinn-Phong (http://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model) models and how they differ.

GClements
11-14-2014, 05:57 AM
My understand was it was just ambient, diffused and specular light. Is that correct?
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.