LAB 8 SHADING MODELS Shading refers to how
LAB 8:
SHADING MODELS • Shading refers to how the lighting equations are applied to a rasterized poly. • Open. GL supports two shading models: 1. GL_ SMOOTH. 2. GL_FLAT.
SMOOTH MODEL & FLAT MODEL
POLYGON SHADING • GL_SMOOTH: Lighting is evaluated at each vertex, and pixel coolers are linearly • interpolated across polys. This is more expensive, but it looks much better. • gl. Shade. Model(GL_SMOOTH);
SMOOTH MODEL void display (void) { gl. Begin(GL_TRIANGLES); gl. Shade. Model(GL_SMOOTH); gl. Color 3 f(1, 0, 0); gl. Vertex 2 f(5, 5); gl. Color 3 f(0, 1, 0); gl. Vertex 2 f(25, 5); gl. Color 3 f(0, 0, 1); gl. Vertex 2 f(5, 25); gl. End(); }
GOURAUD SHADING • GL_FLAT: Lighting is evaluated once per poly, and the resulting colour value is • used for the whole thing. • gl. Shade. Model(GL_FLAT);
TYPES OF LIGHTING IMPLEMENTED 1) Ambient: No source point; affects all polys independent of position, orientation and viewing angle; used as a ‘fudge’ to approximate 2 nd order and higher reflections. GLfloat ambient 0[] = {1, 0, 0, 1}; //red, green, blue, alpha 2) Diffuse: Light scattered in all directions after it hits a poly; dependant upon incident angle. (GL_DIFFUSE is (1. 0, 1. 0) for GL_LIGHT 0, ) The default value for any other light (GL_LIGHT 1, . . . , GL_LIGHT 7) is (0. 0, 0. 0). GLfloat diffuse 0[] = {1, 0, 0, 1}; 3) Specular: ‘Shininess’ ; dependant upon incident and viewing angles. GL_SPECULAR is (1. 0, 1. 0) for GL_LIGHT 0 and (0. 0, 0. 0) for any other light. GLfloat spedular 0[] = {1, 1, 1, 1}; 4) Emissive: Makes a poly appear as though it is glowing; does not actually give off light. GLfloat light 0_pos[] = {1. 0, 2. 0, 3. 0, 1. 0};
DIFFERENT TYPES OF LIGHT
the alpha value can be safely ignored.
TYPES OF LIGHTING IMPLEMENTED
TYPES OF LIGHTING IMPLEMENTED
ENABLE & DISABLE • opengl has two important functions called gl. Enable(GL_LIGHTING) and gl. Disable(GL_LIGHTING). • while these functions don't enable or disable anything in particular, they are reliant on the parameter passed to them. so in other words, you will be using these functions a lot when programming in opengl to turn a particular feature on or off.
EXERCISE void init(){ gl. Clear. Color(1. 0, 0. 0); //gl. Shade. Model(GL_SMOOTH); gl. Enable(GL_LIGHTING); gl. Enable(GL_LIGHT 0); } void display(){ // Create light components GLfloat ambient. Light[] = { 1. 0 f, 0. 0 f, 1. 0 f }; GLfloat diffuse. Light[] = { 0. 8 f, 0. 8, 1. 0 f }; GLfloat specular. Light[] = { 0. 9 f, 1. 0 f }; GLfloat position[] = { -8. 5 f, 5. 0 f, -10. 0 f, 1. 0 f }; // Assign created components to GL_LIGHT 0 gl. Lightfv(GL_LIGHT 0, GL_AMBIENT, ambient. Light); gl. Lightfv(GL_LIGHT 0, GL_DIFFUSE, diffuse. Light); gl. Lightfv(GL_LIGHT 0, GL_SPECULAR, specular. Light); gl. Lightfv(GL_LIGHT 0, GL_POSITION, position); gl. Clear(GL_COLOR_BUFFER_BIT); gl. Color 3 f(1. 0, 1. 0); gl. Load. Identity(); glu. Look. At(0, 0, 5, 0, 0. 0, 1. 0, 0. 0); glut. Solid. Sphere(1. 5, 30, 10); //glut. Solid. Teapot(1. 5); gl. Flush(); }
Exercise void main(int argc, char** argv){ glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size(800, 500); glut. Init. Window. Position(100, 100); glut. Create. Window(""); init(); glut. Display. Func(display); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(-1, 1, 1. 5, 20); gl. Matrix. Mode(GL_MODELVIEW); glut. Main. Loop(); }
- Slides: 14