180 likes | 337 Views
Computer Graphic And Vision. Computer Science Department 2014-2015. Lighting. Lighting in OpenGL. In the OpenGL lighting model, the light in a scene comes rom several light sources that can be individually turned on and off .
E N D
Computer Graphic And Vision Computer Science Department 2014-2015 Lighting
Lighting in OpenGL • In the OpenGL lighting model, the light in a scene comes rom several light sources that can be individually turned on and off. • The light sources have an effect only when there are surfaces that absorb and reflect light • Each surface is assumed to be composed of a material with various properties
Using Depth Buffer (z-buffer) • A depth buffer works by associating a depth, or distance, from the view plane (usually the near clipping plane), with • To clear the depth buffer: • glClear(GL_DEPTH_BUFFER_BIT); • To enable the using of depth buffer: • glEnable(GL_DEPTH_TEST); • The depth buffer is needed when we use lighting to determine the distance from the light source.
Enabling Lighting • To enable lighting we should use: • glEnable(GL_LIGHTING); • To disable lighting: • glDisable(GL_LIGHTING); • We need to enable each light source that we define: • glEnable(GL_LIGHT0); GL_LIGHT0 GL_LIGHT1 ... GL_LIGHT7
Creating Light Sources • The command used to specify all properties of lights is glLight*(); • voidglLight{if}v(GLenum light, GLenumpname, TYPE[]param); GL_LIGHT0 GL_LIGHT1 ... GL_LIGHT7 GL_AMBIENT GL_DIFFUSE GL_SPECULAR GL_POSITION A pointer to a group of Values Float[] p = {1.0f ,.0f ,.0f 1.0f};
Creating Light Sources • The default values listed for GL_DIFFUSE and GL_SPECULAR in apply only to GL_LIGHT0. • For other lights, the default value is (0.0, 0.0, 0.0, 1.0) for bothGL_DIFFUSE and GL_SPECULAR. • If the fourth value is zero the light source position is far and can not be determined (first three values determine the direction of the light), if it is non-zero the first three values determine the position of the light.
Example LitSpin.java
Materials Color • Like lights, materials have different ambient, diffuse, and specular colors. • For a light, the numbers correspond to a percentage of full intensity for each color. • If R = G = B =1.0, the light is the brightest possible white • If the values are 0.5, the color is still white at half intensity (gray) • If R=G=1 and B=0, the light appears yellow • For materials, the numbers correspond to the reflected proportions of those colors • If R=1, G=0.5, and B=0 for a material, that material reflects all the incoming red light, half the incoming green, and none of the incoming blue light.
Creating Light Sources • To change the material properties we can use glMaterial*(). • voidglMaterial{if}[v](GLenum face, GLenum pname, TYPE *param); GL_FRONT GL_BACK GL_FRONT_AND_BACK GL_AMBIENT GL_DIFFUSE GL_SPECULAR GL_EMISSION GL_SHININESS A pointer to a group of Values Float[] p = {1.0f ,.0f ,.0f 1.0f};
Control the size and brightness of the highlight [0.0 - 128] Example SphereWithLight.java
Emission • By specifying an RGBA color for GL_EMISSION, we can make an object appear to be giving off light of that color. Sphere_Light_Move_Emision.java Sphere_Light_Move.java
Controlling a Light's Position and Direction • A light source is subject to the same matrix transformations as a primitive when glLight*() is called the position or direction is transformed by the current modelview matrix. • The following three different effects change the point in the program at which the light position is set, relative to modeling or viewing transformations: • A light position that remains fixed. • A light that moves around a stationary object. • A light that moves along with the viewpoint.
More Info. • OpenGL programming Guide :The Official Guide to Learning OpenGL (red book) • Chapter 5: Lighting