Lighting and Texturing Michael Tao Michael Tao Lighting and Texturing 1 / 1
Fixed Function OpenGL Lighting Need to enable lighting Need to configure lights Need to configure triangle material properties Michael Tao Lighting and Texturing 2 / 1
Enable Lighting Enable Lighting: glenable(gl_lighting) Initialize the lights to be activated Lights labeled by GL_LIGHTN for N [0...GL_MAX_LIGHTS-1] Enable light 0: glenable(gl_light0) Set Lighting parameters Individual light parameters: gllighti,gllightf (will explain later) Select flat or smooth shading: glshademodel GL_FLAT,GL_SMOOTH Smooth is Gouraud shading (interpolated values from vertex evaluations) Flat is the value chosen from one vertex Michael Tao Lighting and Texturing 3 / 1
Setting Materials Instead of setting individual colors we change the material parameter states Set normals when specifying vertices (glnormal3f(float,float,float)) Set material parameters like we set colors before (Next slide covers the parameters) Independent materials can be set on the front and back faces Michael Tao Lighting and Texturing 4 / 1
Lighting and Material Parameters Materials and Lights both need Phong-Blinn shading parameters Lighting: gllightf(glenum light, GLenum pname, float param) light: GL_LIGHT0...GL_LIGHTN Materials: glmaterialfv(glenum face, GLenum pname, float* params) face: GL_FRONT,GL_BACK,GL_FRONT_AND_BACK pname: GL_AMBIENT,GL_DIFFUSE,GL_SPECULAR,GL_SHININESS Light doesn t have GL_SHININESS Most params are 4 values for RGBA streams Michael Tao Lighting and Texturing 5 / 1
Results? Gourad Michael Tao Lighting and Texturing 6 / 1
Quick Note on GLSL Implementing gourad vs phong shading is trivial Pass materials, light positions, etc as uniforms (or use built-in variables from fixed function pipeline) Gourad: Evaluate lighting in vertex shader and pass a color to fragment shader Fragment shader interpolates colors passed from vertex shader by default Phong: Pass normal and world-space position from vertex shader to fragment shader Fragment shader uses interpolated Flat: Do the same as Gourad except use flat out vec4 color Michael Tao Lighting and Texturing 7 / 1
Texturing... Need to create texture objects Need to configure texture behvaior Need to push texture data Need to activate textures Need to set texture coordinates to objects Michael Tao Lighting and Texturing 8 / 1
The initialization mess Create texture objects Bind a texture Configure it glenable ( GL_TEXTURE_2D ); GLuint tex [4]; glgentextures (4, tex ); glbindtexture ( GL_TEXTURE_2D, tex [0]) ; gltexparameteri ( GL_ TEXTURE_ 2D, GL_ TEXTURE_ MIN_ FILTER, GL_LINEAR ); gltexparameteri ( GL_ TEXTURE_ 2D, GL_ TEXTURE_ MAX_ FILTER, GL_NEAREST ); gltexparameteri ( GL_ TEXTURE_ 2D, GL_ TEXTURE_ WRAP_ T, GL_CLAMP_TO_EDGE ); Michael Tao Lighting and Texturing 9 / 1
void glteximage2d ( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * data ); Michael Tao Lighting and Texturing 10 / 1 Set The Data target: GL_TEXTURE_2D is almost all you ll need level: just set to 0 for now, until you understand what mipmaps are internalformat: the way the data for the texture should be stored. For now GL_RGBA8 is fine or GL_ALPHA for single values. width/height is obvious border = 0. I m not sure about this myself type is the sort of data you are producing. In general GL_RGBA but if you only want to assert a single value (like depth) use GL_RED format is the pixel data format that you re submitting Generally can use GL_UNSIGNED_INT_8_8_8_8 GL_FLOAT,GL_INT,GL_UNSIGNED_INT can be used when your internal format is a scalar.
Use a Texture Activate a specific texture units Make sure texturing is enabled Set a texture active GL_TEXTUREN for N up to GL_MAX_TEXTURE_UNITS-1 Apparently you can do things with gltexenvi Makes a lot more sense in GLSL :P Bind a texture to the currently active texture Start rendering vertices glenable ( GL_TEXTURE_2D ); glactivetexture ( GL_ TEXTURE0 ); glbindtexture ( GL_TEXTURE_2D, tex ); glbegin ( GL_TRIANGLES ); gltexcoord2f (0,0) ; glvertex2f (0,0) ; gltexcoord2f (1,0) ; glvertex2f (1,0) ;... Michael Tao Lighting and Texturing 11 / 1
Quick Note on GLSL Pass texture coords through gl_multitexcoord0 and write to gl_texcoord[0] Textures are treated as uniform sampler2d objects and are sampled through texture uniform sampler2d mysampler ; int main () {... texture2d ( mysampler,st);// glsl1.2 texture ( mysampler,st);// modern glsl The active texture tells GLSL which uniform to bind to glbindprogram ( programid ); GLuint loc = glgetuniformlocation ( programid," mysampler "); gluniform1i ( loc,0) ;// switch 0 for whichever GL_ TEXTUREN value N we want Michael Tao Lighting and Texturing 12 / 1