OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);

Slides:



Advertisements
Similar presentations
Polygon Rendering Flat Rendering Goraud Rendering Uses Phong Reflectance Phong Rendering.
Advertisements

2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.
Computer Graphics - Class 10
OpenGL Son of the Survival Guide. Last Time on OpenGL Windowing … glut Rendering Primatives Transformations Projections State Management.
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 11: OpenGL 3
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Hidden Surfaces and Shading CS BSP Tree T1T1 T2T2 E if (f 1 (E) < 0) then draw T 1 draw T 2 else draw T 2 draw T 1 f 1 (p) = 0 is the.
Normals Specifying the normal –void glNormal3d( GLdouble nx, GLdouble ny, GLdouble nz ); When using lighting effects, normals must be defined for each.
Now Playing: Multiply Jamie Lidell from Multiply Released June 13, 2005.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
CS425 OpenGL Materials. What Color Is It? green & blue absorbed white light Looks red.
Illumination and Shading
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Development of Interactive 3D Virtual World Applications
Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.
Display Issues Week 5 David Breen Department of Computer Science
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
Shading in OpenGL.
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
Shading 03/19/2003. Lighting Principles Lighting based on how objects reflect light –Surface characteristics –Light color and direction –Global lighting.
CS 481 Preview, Some Lighting Details Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, November 19, 2003.
Shading and Illumination. OpenGL Shading Without ShadingWith Shading.
OpenGL - Lighting, Shading and Material Properties
Computer Graphics I, Fall 2010 Shading in OpenGL.
Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
Introduction to OpenGL Programming RedBook OpenGL.
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Lighting and Shading Part II.
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
RENDERING Introduction to Shading models – Flat and Smooth shading – Adding texture to faces – Adding shadows of objects – Building a camera in a program.
Lighting Dave Shreiner. 2 Lighting Principles Lighting simulates how objects reflect light Lighting simulates how objects reflect light material composition.
Shading NOTE: Some of these slides are from Ed Angel’s presentation at SIGGRAPH February 27, 2008.
Illumination CSE 410. Basic steps of lighting Enable smooth shading Set global ambient light glShadeModel(GL_SMOOTH); glEnable(GL_NORMALIZE); GLfloat.
CS559: Computer Graphics Lecture 16: Shading and OpenGL Li Zhang Spring 2008.
CSC Graphic Programming Lecture 2 OpenGL Lightning.
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Illumination Models and Surface-Rendering Methods CEng 477 Introduction to Computer Graphics.
Lecture 6 Lighting. Introduction What is light? How do we percept colors? Lights in OpenGL.
Chapter 5. Lighting Computer Graphics (spring, 2009) School of Computer Science University of Seoul.
1 Dr. Scott Schaefer Lighting. 2/49 Lighting/Illumination Color is a function of how light reflects from surfaces to the eye Global illumination accounts.
Graphics Graphics Korea University kucg.korea.ac.kr 1 Lights & Material 고려대학교 컴퓨터 그래픽스 연구실.
CSC Graphics Programming
Lighting.
Shading To determine the correct shades of color on the surface of graphical objects.
Lecture 16: Shading and OpenGL Li Zhang Spring 2008
Advanced Graphics Algorithms Ying Zhu Georgia State University
Illumination and Shading
@ 2017 by Jim X. Chen George Mason University
Open GL: Colors and Lighting
Lecture 10, 11, and 12 Lighting & Shading
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Shading in OpenGL Ed Angel
Lighting and Shading Lab 8:.
CSC461: Lecture 24 Lighting and Shading in OpenGL
Lighting Phong's Lighting Model normals
o عَلَّمَهُ الْبَيَانَ
Illumination and Shading
Lighting and Materials
Lighting – Light Sources
Lighting – Material Properties
Lighting and Shading Lab 8:.
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Presentation transcript:

OpenGL Lighting Jian-Liang Lin 2002

Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT); draw_3d_object_A(); draw_3d_object_B(); }

Hidden-Surface Removal -2 Apply HSR: glutInitDisplayMode (GLUT_DEPTH |.... ); glEnable(GL_DEPTH_TEST);... while (1) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); get_viewing_point_from_mouse_position(); draw_3d_object_A(); draw_3d_object_B(); }

Lighting: First glance First to look at a Lit and an Unlit sphere

Lighting: HOW-TO Steps to apply OpenGL Lighting –Define Normal Vectors for Each Vertex of Every Object –Create, Position, and Enable One or More Light Sources –Select a Lighting Model –Define Material Properties for the Objects in the Scene

Lighting: Simple Example -1 A Simple Example: Rendering a Lit Sphere #include void init(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); }

Lighting: Simple Example -2 void display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere (1.0, 20, 16); glFlush (); } void reshape (int w, int h) { ….// omitted … } int main(int argc, char** argv) { … // omitted.. }

Lighting: Normal Vectors Define Normal Vectors for Each Vertex of Every Object –In the example, the normals for the sphere are defined as part of the glutSolidSphere() routine. –You may have to define normal vectors by call glNormal{3,4}{i,s,f,d}[v]( … ); yourself. –Normal vectors must be normalized. OpenGL can automatically normalize normal vectors for you by calling glEnable( GL_NORMALIZE );

Lighting: Creation -1 Creating Light Sources – void glLight{if}(GLenum light, GLenum pname, TYPE param); void glLight{if}v(GLenum light, GLenum pname, TYPE *param); Creates the light specified by light, which can be GL_LIGHT0, GL_LIGHT1,..., or GL_LIGHT7. At most 8 in OpenGL spec., but may be more in others ’ implementation. The characteristic of the light being set is defined by pname. param indicates the values to which the pname characteristic is set.

Lighting: Creation -2

Lighting: Creation -3 Note: –The default values listed for GL_DIFFUSE and GL_SPECULAR in last page apply only to GL_LIGHT0. –For other lights, the default value is (0.0, 0.0, 0.0, 1.0 ) for both GL_DIFFUSE and GL_SPECULAR. Example: GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Lighting: Position and Attenuation Position ( x, y, z, w ) –w is ZERO, Directional light –W is NONZERO, Positional light Attenuation d = distance between the light's position and the vertex k c = GL_CONSTANT_ATTENUATION k l = GL_LINEAR_ATTENUATION k q = GL_QUADRATIC_ATTENUATION –If light is directional light, the attenuation is 1

Lighting: Spot Light -1 Spot Lights –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);

Lighting: Spot Light -2 –Define light spot cutoff glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); By default, the spotlight feature is disabled because the GL_SPOT_CUTOFF parameter is 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).

Lighting: Multiple Lights You can define multiple lights in your scene and enable or disable them –glEnable( GL_LIGHT0 ); –glDisable( GL_LIGHT7 ); Enable OpenGL Lighting –glEnable( GL_LIGHTING );

Lighting: Controlling a Light's Position & Direction –1 OpenGL treats the position and direction of a light source just as it treats the position of a geometric primitive – MODELVIEW Transformation will be applied. Three types of control –A light position that remains fixed –A light that moves around a stationary object –A light that moves along with the viewpoint

Lighting: Controlling a Light's Position & Direction –2 Keeping the Light Stationary glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-1.5, 1.5, -1.5*h/w, 1.5*h/w, -10.0, 10.0); else glOrtho (-1.5*w/h, 1.5*w/h, -1.5, 1.5, -10.0, 10.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); … /* later in init() */ GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, position); /* NO other MODELVIEW transformation is set…*/

Lighting: Controlling a Light's Position & Direction –3 Independently Moving the Light static GLdouble spin; void display(void) { GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glPushMatrix(); glRotated(spin, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glPopMatrix(); glutSolidTorus (0.275, 0.85, 8, 15); glPopMatrix(); glFlush(); }

Lighting: Controlling a Light's Position & Direction –4 Moving the Light Source Together with Your Viewpoint GLfloat light_position() = { 0.0, 0.0, 0.0, 1.0 }; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, light_position); …… static GLdouble ex, ey, ez, upx, upy, upz; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gluLookAt (ex, ey, ez, 0.0, 0.0, 0.0, upx, upy, upz); glutSolidTorus (0.275, 0.85, 8, 15); glPopMatrix(); glFlush(); }

Lighting: Lighting Model –1 Selecting a lighting model – void glLightModel{if}(GLenum pname, TYPEparam); void glLightModel{if}v(GLenum pname, TYPE *param); Sets properties of the lighting model. The characteristic of the lighting model being set is defined by pname, which specifies a named parameter. param indicates the values to which the pname characteristic is set

Lighting: Lighting Model –2

Lighting: Materials –1 Defining Material Properties – void glMaterial{if}(GLenum face, GLenum pname, TYPEparam); void glMaterial{if}v(GLenum face, GLenum pname, TYPE *param); Specifies a current material property for use in lighting calculations. face can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK. The particular material property being set is identified by pname and the desired values for that property are given by param. GL_AMBIENT_AND_DIFFUSE allows you to set both

Lighting: Materials –2

Lighting: Materials –3 Diffuse –Diffuse reflectance plays the most important role in determining what you perceive the color of an object to be. –It's affected by the color of the incident diffuse light and the angle of the incident light relative to the normal direction.

Lighting: Materials –4 Ambient –Ambient reflectance affects the overall color of the object –An object's total ambient reflectance is affected by the global ambient light and ambient light from individual light sources.

Lighting: Materials –5 Specular & Shininess –Specular reflection from an object produces highlights. –Unlike ambient and diffuse reflection, the amount of specular reflection seen by a viewer does depend on the location of the viewpoint. –You can control the size and brightness of the highlight. (with GL_SHININESS)

Lighting: Materials –6 Emission –you can make an object appear to be giving off light of that color. –Since most real-world objects (except lights) don't emit light, you'll probably use this feature mostly to simulate lamps and other light sources in a scene.

Lighting: Materials –7 No ambient Reflection Gray ambient Reflection Blue ambient Reflection Diffuse Add Higher Emission Only Specular Shininess

Lighting: Materials –8 Another technique for minimizing performance costs associated with changing material properties is to use glColorMaterial(). – void glColorMaterial(GLenum face, GLenum mode); Causes the material property (or properties) specified by mode of the specified material face (or faces) specified by face to track the value of the current color at all times. A change to the current color (using glColor*() ) immediately updates the specified material properties. The face parameter can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK (the default). The mode parameter can be GL_AMBIENT, GL_DIFFUSE, GL_AMBIENT_AND_DIFFUSE (the default), GL_SPECULAR, or GL_EMISSION. At any given time, only one mode is active. glColorMaterial() has no effect on color-index lighting.

Any Question? ?