Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,

Similar presentations


Presentation on theme: "Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,"— Presentation transcript:

1 Lighting & Material

2 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(); }

3 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);

4 Example 3/5 // z buffer enable glEnable(GL_DEPTH_TEST); // enable lighting glEnable(GL_LIGHTING); // set light property glEnable(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 property glMaterialfv(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); }

5 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(); }

6 Example 5/5 Specular : blue Diffuse : red Ambient : green

7 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

8 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

9 Lighting 1/5 Enable lighting glEnable(GL_LIGHTING) Set light property glEnable(GL_LIGHT0) ~ glEnable(GL_LIGHT7)

10 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 PnameDef. ValueMeaning GL_AMBIENT(0.0, 0.0, 0.0, 0.0)ambient RGBA intensity of light GL_DIFFUSE(1.0, 1.0, 1.0, 1.0) diffuse RGBA intensity of light GL_SPECULAR(1.0, 1.0, 1.0, 1.0) specular RGBA intensity of light GL_POSITION(0.0, 0.0, 1.0, 0.0)(x, y, z, w) position of light GL_SPOT_DIRECTION(0.0, 0.0, -1.0)(x, y, z) direction of spotlight GL_SPOT_EXPONENT0.0spotlight exponent GL_SPOT_CUTOFF180.0 spotlight cutoff angle GL_CONSTANT_ATTENUATION1.0constant attenuation factor GL_LINEAR_ATTENUATION0.0linear attenuation factor GL_QUADRATIC_ATTENUATION0.0quadratic attenuation factor

11 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

12 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 exponent glLightf(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)).

13 Lighting 5/5

14 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.asp PnameDef. ValueMeaning GL_AMBIENT(0.2, 0.2, 0.2, 1.0)ambient color of material GL_DIFFUSE(0.8, 0.8, 0.8, 1.0) diffuse color of material GL_AMBIENT_AND_DIFFUSEambient and diffuse color of material GL_SPECULAR(0.0, 0.0, 0.0, 1.0) specular color of material GL_SHININESS0.0specular exponent GL_EMISSION(0.0, 0.0, 0.0, 1.0)emissive color of material GL_COLOR_INDEXES(0, 1, 1)ambient, diffuse, and specular color indices

15 Material 2/4 No Ambient Gray Ambient Blue Ambient Diffuse Only SpecularHigher Shininess Emission

16 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 ).


Download ppt "Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,"

Similar presentations


Ads by Google