Lighting & Material
Example 1/5
#include ”glut.h”void display();void reshape(int, int);void lighting();int main(int argc, char** argv){
glutInit(&argc, argv);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);glutCreateWindow("Planet");glutDisplayFunc(display);glutReshapeFunc(reshape);glutMainLoop();return 0;
}
Example 2/5
void lighting(){
GLfloat mat_diffuse[] = {1.0, 0.0, 0.0, 1.0};GLfloat mat_specular[] = {0.0, 0.0, 1.0, 1.0};GLfloat mat_ambient[] = {0.0, 1.0, 0.0, 1.0};GLfloat mat_shininess[] = {50.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};GLfloat light_position[] = {5.0, 5.0, 10.0, 0.0};
glClearColor(0.0, 0.0, 0.0, 0.0);glShadeModel(GL_SMOOTH);
Example 3/5
// z buffer enableglEnable(GL_DEPTH_TEST);
// enable lightingglEnable(GL_LIGHTING);// set light propertyglEnable(GL_LIGHT0);glLightfv(GL_LIGHT0, GL_POSITION, light_position);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
// set material propertyglMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
Example 4/5
void display(){
lighting();glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glutSolidSphere(0.8, 128, 128);glFlush();
}
void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}
Example 5/5
Specular : blue Diffuse : red Ambient : green
Color Specifying shading model
void glShadeModel(GLenum mode ); GL_FLAT / GL_SMOOTH (default) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/
glfunc03_24rw.asp
GL_FLAT
GL_SMOOTH
Hidden-Surface Removal
Apply HSR in OpenGL glutInitDisplayMode(GLUT_DEPTH);
Enable the depth buffer glEnable(GL_DEPTH_TEST);
Enable depth test glClear(GL_DEPTH_BUFFER_BIT);
When you wish to clean the depth buffer
Lighting 1/5
Enable lighting glEnable(GL_LIGHTING)
Set light property glEnable(GL_LIGHT0) ~
glEnable(GL_LIGHT7)
Lighting 2/5
void glLightfv(GLenum light, GLenum pname, GLfloat param)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_88z8.asp
Pname Def. Value Meaning
GL_AMBIENT (0.0, 0.0, 0.0, 0.0) ambient RGBA intensity of lightGL_DIFFUSE (1.0, 1.0, 1.0, 1.0) diffuse RGBA intensity of lightGL_SPECULAR (1.0, 1.0, 1.0, 1.0) specular RGBA intensity of lightGL_POSITION (0.0, 0.0, 1.0, 0.0) (x, y, z, w) position of lightGL_SPOT_DIRECTION (0.0, 0.0, -1.0) (x, y, z) direction of spotlightGL_SPOT_EXPONENT 0.0 spotlight exponentGL_SPOT_CUTOFF 180.0 spotlight cutoff angleGL_CONSTANT_ATTENUATION 1.0 constant attenuation factorGL_LINEAR_ATTENUATION 0.0 linear attenuation factorGL_QUADRATIC_ATTENUATION 0.0 quadratic attenuation factor
Lighting 3/5
Attenuation d: distance between the light's position and
the vertex kc = GL_CONSTANT_ATTENUATION kl = GL_LINEAR_ATTENUATION kq = GL_QUADRATIC_ATTENUATION If light is directional light, the attenuation is
always 1
2
1
dkdkkFacrornAttenuatio
qlc
Lighting 4/5
How to create a spot light? Define your light as positional light Define light spot direction
GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
Define light spot exponentglLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
Define light spot cutoff The value for GL_SPOT_CUTOFF is restricted to being within
the range [0.0,90.0] (unless it has the special value 180.0 (default)).
Lighting 5/5
Material 1/4
Define material properties of objects. void glMaterial{if}[v](GLenum face,
GLenum pname, TYPE[*] param); face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/
glfunc03_7cq4.aspPname Def. Value Meaning
GL_AMBIENT (0.2, 0.2, 0.2, 1.0) ambient color of materialGL_DIFFUSE (0.8, 0.8, 0.8, 1.0) diffuse color of materialGL_AMBIENT_AND_DIFFUSE ambient and diffuse color of
materialGL_SPECULAR (0.0, 0.0, 0.0, 1.0) specular color of materialGL_SHININESS 0.0 specular exponentGL_EMISSION (0.0, 0.0, 0.0, 1.0) emissive color of materialGL_COLOR_INDEXES (0, 1, 1) ambient, diffuse, and specular color
indices
Material 2/4
No Ambien
t
Gray Ambien
t
Blue Ambien
t
Diffuse Only
Specular
Higher Shinines
s
Emission
Normal Vector
glNormal{34}{isfd}[v](TYPE* normal); Assign normal vector for vertices the normal vector should be assigned
before you assign vertex Normal vectors must be normalize.
OpenGL can automatically normalize normal vector by glEnable( GL_NORMALIZE ) .