160 likes | 267 Views
Chap 7. O penGL extension. How to. By yourself Query the “OpenGL32.dll”, and get the function address Don’t do it glew http://glew.sourceforge.net/. In your C++ project. Add “include/glew.h” to your “Include Files” folder in .NET Put “lib/glew32s.lib” into your project folder
E N D
How to • By yourself • Query the “OpenGL32.dll”, and get the function address • Don’t do it • glew • http://glew.sourceforge.net/
In your C++ project • Add “include/glew.h” to your “Include Files” folder in .NET • Put “lib/glew32s.lib” into your project folder #define GLEW_STATIC 1 #include "glew.h“ … glutInit(&argc, argv); glewInit();
Some online resources • OpenGL programming guide chapter 9 “Texture Mapping” • http://www.opengl.org/documentation/red_book_1.0/OpenGL_PG_ch09.pdf • An online tutorial on shadow map • http://www.paulsprojects.net/tutorials/smt/smt.html
Example: Shadow map • What is shadow light eye Object Under shadow
What is a shadow map • A depth image as seen from the light • When the depth of a point is greater than the depth in the shadow map, it is under shadow • If you move the viewpoint to the light source position, there are no shadows from that perspective
Overview • 3-pass algorithm • First pass: render the scene from the point of view of the light,and store the depth in the texture (shadow map) • projection * modelview,… • Second pass: render the scene with only ambient light • Third pass: render the scene,and “compare” the depth of the fragment to the shadow map, if it’s greater than the depth in the shadow map, it’s under shadow. if it’s not under shadow, render the fragment with diffuse and specular light. • How can we “compare” in different coordinate?
In OpenGL • First pass Setup the projection and modelview matrix as seen from the light draw your scene glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT, 0 );
void glCopyTexImage2D( GLenum target, GLint level, GLint internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
Third pass void generateTextureMatrix( float* lightPos ) { glMatrixMode(GL_TEXTURE); glLoadIdentity(); glTranslatef( 0.5, 0.5, 0.0 ); glScalef( 0.5, 0.5, 1.0 ); gluPerspective( 80.0, 1.0, 10.0, 100.0 ); gluLookAt( lightPos[0], lightPos[1], lightPos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glEnable( GL_TEXTURE_GEN_S ); glEnable( GL_TEXTURE_GEN_T ); glEnable( GL_TEXTURE_GEN_R ); glEnable( GL_TEXTURE_GEN_Q );
float sPlane[4] = {1.0, 0.0, 0.0, 0.0}; float tPlane[4] = {0.0, 1.0, 0.0, 0.0}; float rPlane[4] = {0.0, 0.0, 1.0, 0.0}; float qPlane[4] = {0.0, 0.0, 0.0, 1.0}; glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR ); glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR ); glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR ); glTexGeni( GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR ); glTexGenfv( GL_S, GL_EYE_PLANE, sPlane ); glTexGenfv( GL_T, GL_EYE_PLANE, tPlane ); glTexGenfv( GL_R, GL_EYE_PLANE, rPlane ); glTexGenfv( GL_Q, GL_EYE_PLANE, qPlane );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL ); glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE ); glMatrixMode(GL_MODELVIEW); }
Not so easy… • Self shadow • Add bias • glPolygonOffset(GLfloat factor, GLfloat units); • Render backface and cull front face when gererating shadow map • Aliasing