Download presentation
Presentation is loading. Please wait.
1
Lighting and Shading Lab 8:
2
Shading models Shading refers to how the lighting equations are applied to a rasterized poly. OpenGL supports two shading models: GL_ SMOOTH. GL_FLAT.
3
Smooth Model & Flat Model
4
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. glShadeModel(GL_SMOOTH);
5
Smooth Model glBegin(GL_TRIANGLES); glEnd(); void display (void) {
glShadeModel(GL_SMOOTH); glColor3f(1,0,0); glVertex2f(5,5); glColor3f(0,1,0); glVertex2f(25,5); glColor3f(0,0,1); glVertex2f(5,25); glEnd(); }
6
Gouraud shading GL_FLAT: Lighting is evaluated once per poly, and the resulting colour value is used for the whole thing. glShadeModel(GL_FLAT);
7
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 2nd order and higher reflections. GLfloat ambient0[] = {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, 1.0, 1.0) for GL_LIGHT0, ) The default value for any other light (GL_LIGHT1, ... , GL_LIGHT7) is (0.0, 0.0, 0.0, 0.0). GLfloat diffuse0[] = {1, 0, 0, 1}; 3) Specular: ‘Shininess’ ; dependant upon incident and viewing angles. GL_SPECULAR is (1.0, 1.0, 1.0, 1.0) for GL_LIGHT0 and (0.0, 0.0, 0.0, 0.0) for any other light. GLfloat spedular0[] = {1, 1, 1, 1}; 4) Emissive: Makes a poly appear as though it is glowing; does not actually give off light. GLfloat light0_pos[] = {1.0, 2.0, 3.0, 1.0};
8
Different types of light
9
the alpha value can be safely ignored.
10
Types of lighting implemented
11
Types of lighting implemented
12
Enable & Disable opengl has two important functions called glEnable(GL_LIGHTING) and glDisable(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.
13
Exercise void init(){ glClearColor(1.0, 1.0, 1.0, 0.0); //glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } void display(){ // Create light components GLfloat ambientLight[] = { 1.0f, 0.0f, 0.0f, 1.0f }; GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f }; GLfloat specularLight[] = { 0.9f, 0.9f, 0.9f, 1.0f }; GLfloat position[] = { -8.5f, 5.0f, -10.0f, 1.0f }; // Assign created components to GL_LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); glLightfv(GL_LIGHT0, GL_POSITION, position); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glLoadIdentity(); gluLookAt(0, 0, 5, 0, 0.0, 0.0, 0.0, 1.0, 0.0); glutSolidSphere(1.5,30,10); //glutSolidTeapot(1.5); glFlush();
14
Exercise void main(int argc, char** argv){ glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800, 500); glutInitWindowPosition(100, 100); glutCreateWindow(""); init(); glutDisplayFunc(display); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1, 1, -1, 1, 1.5, 20); glMatrixMode(GL_MODELVIEW); glutMainLoop(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.