Advertisement
PandaProtector23

OpenGL shadow mapping

Dec 13th, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.33 KB | None | 0 0
  1. //This part of program use 2 2d array to draw a map, add a light and generated shadow.
  2. //All the variable i'm using are at the end
  3.  
  4. ///////////////////////////////////////////////////////////////////////
  5. //Function
  6. ///////////////////////////////////////////////////////////////////////
  7. void initShadowsAndMatrices()
  8. {
  9.     //Create the shadow map texture
  10.     glGenTextures(1, &shadowMapTexture);
  11.     glActiveTexture(GL_TEXTURE0);
  12.     glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
  13.     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 0,GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);     
  14.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  15.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  16.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  17.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  18.  
  19.  
  20.     //Use the color as the ambient and diffuse material
  21.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  22.     glEnable(GL_COLOR_MATERIAL);
  23.    
  24.     //White specular material color, shininess 16
  25.     glMaterialfv(GL_FRONT, GL_SPECULAR, white);
  26.     glMaterialf(GL_FRONT, GL_SHININESS, 16.0f);
  27.  
  28.     //Calculate & save matrices
  29.     glPushMatrix();
  30.    
  31.     glLoadIdentity();
  32.     gluPerspective(90.0f, (float)windowWidth/windowHeight, 1.0f, 100.0f);
  33.     glGetFloatv(GL_MODELVIEW_MATRIX, cameraProjectionMatrix);
  34.    
  35.     glLoadIdentity();
  36.     gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,cameraTarget.x, cameraTarget.y, cameraTarget.z,0.0f, 1.0f, 0.0f);
  37.     glGetFloatv(GL_MODELVIEW_MATRIX, cameraViewMatrix);
  38.    
  39.     glLoadIdentity();
  40.     gluPerspective(140.0f, ((float)windowWidth/windowHeight), 1.0f, 100.0f);
  41.     glGetFloatv(GL_MODELVIEW_MATRIX, lightProjectionMatrix);
  42.    
  43.     glLoadIdentity();
  44.     gluLookAt(lightPosition.x, lightPosition.y, lightPosition.z,lightPosition.x, lightPosition.y, 0,0.0f, 1.0f, 0.0f);
  45.     glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);
  46.    
  47.     glPopMatrix();
  48. }
  49.  
  50. //This function do shadow mapping in 3 draw.
  51. void displayHandler()
  52. {
  53.     setCamera();                       //function that simply update camera position
  54.     keyOperation();                //keypress are process in these function
  55.     keySpecialOperation();
  56.  
  57.     //Calculate & save matrice to update light position
  58.     glPushMatrix();
  59.     glLoadIdentity();
  60.     gluLookAt(  lightPosition.x, lightPosition.y, lightPosition.z,lightPosition.x,  lightPosition.y, 0,0.0f, 1.0f, 0.0f);
  61.     glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);
  62.     glPopMatrix();
  63.  
  64.     //First pass - from light's point of view
  65.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  66.     glMatrixMode(GL_PROJECTION);
  67.     glLoadMatrixf(lightProjectionMatrix);
  68.     glMatrixMode(GL_MODELVIEW);
  69.     glLoadMatrixf(lightViewMatrix);
  70.     //Use viewport the same size as the shadow map
  71.     glViewport(0, 0, shadowMapWidth, shadowMapHeight);
  72.     //Draw back faces into the shadow map
  73.     glCullFace(GL_FRONT);
  74.     //Disable color writes, and use flat shading for speed
  75.     glShadeModel(GL_FLAT);
  76.     glColorMask(0, 0, 0, 0);
  77.     //Draw the scene
  78.     drawMap();
  79.     //Read the depth buffer into the shadow map texture
  80.     glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
  81.     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapWidth, shadowMapHeight);
  82.     //restore states
  83.     glCullFace(GL_BACK);
  84.     glShadeModel(GL_SMOOTH);
  85.     glColorMask(1, 1, 1, 1);
  86.  
  87.     //2nd pass - Draw from camera's point of view
  88.     glClear(GL_DEPTH_BUFFER_BIT);
  89.     glMatrixMode(GL_PROJECTION);
  90.     glLoadMatrixf(cameraProjectionMatrix);
  91.     glMatrixMode(GL_MODELVIEW);
  92.     glLoadMatrixf(cameraViewMatrix);
  93.     glViewport(0, 0, windowWidth, windowHeight);
  94.     //Use dim light to represent shadowed areas
  95.     glLightfv(GL_LIGHT1, GL_POSITION, VECTOR4D(lightPosition));
  96.     glLightfv(GL_LIGHT1, GL_AMBIENT, white*0.2f);
  97.     glLightfv(GL_LIGHT1, GL_DIFFUSE, white*0.2f);
  98.     glLightfv(GL_LIGHT1, GL_SPECULAR, black);
  99.  
  100.     glEnable(GL_LIGHT1);
  101.     glEnable(GL_LIGHTING);
  102.     drawMap();
  103.  
  104.     //3rd pass
  105.     //Draw with bright light
  106.     glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
  107.     glLightfv(GL_LIGHT1, GL_SPECULAR, white);
  108.     //Calculate texture matrix for projection
  109.     //This matrix takes us from eye space to the light's clip space
  110.     //It is postmultiplied by the inverse of the current view matrix when specifying                           
  111.     //texgen
  112.    
  113.     MATRIX4X4 textureMatrix=biasMatrix*lightProjectionMatrix*lightViewMatrix;
  114.     //Set up texture coordinate generation.
  115.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  116.     glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
  117.     glEnable(GL_TEXTURE_GEN_S);
  118.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  119.     glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));
  120.     glEnable(GL_TEXTURE_GEN_T);
  121.     glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  122.     glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));
  123.     glEnable(GL_TEXTURE_GEN_R);
  124.     glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  125.     glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));
  126.     glEnable(GL_TEXTURE_GEN_Q);
  127.     //Bind & enable shadow map texture
  128.     glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
  129.     glEnable(GL_TEXTURE_2D);
  130.     //Enable shadow comparison
  131.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB,
  132.         GL_COMPARE_R_TO_TEXTURE);
  133.     //Shadow comparison should be true (ie not in shadow) if r<=texture
  134.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
  135.     //Shadow comparison should generate an INTENSITY result
  136.     glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
  137.     //Set alpha test to discard false comparisons
  138.     glAlphaFunc(GL_GEQUAL, 0.99f);
  139.     glEnable(GL_ALPHA_TEST);
  140.     drawMap(); 
  141.  
  142.     //Disable textures and texgen
  143.     glDisable(GL_TEXTURE_2D);
  144.     glDisable(GL_TEXTURE_GEN_S);
  145.     glDisable(GL_TEXTURE_GEN_T);
  146.     glDisable(GL_TEXTURE_GEN_R);
  147.     glDisable(GL_TEXTURE_GEN_Q);
  148.     //Restore other states
  149.     glDisable(GL_LIGHTING);
  150.     glDisable(GL_ALPHA_TEST);
  151.  
  152.     //Update frames per second counter
  153.     fpsCounter.Update();
  154.     //Print fps
  155.     static char fpsString[32];
  156.     sprintf(fpsString, "%.2f", fpsCounter.GetFps());
  157.    
  158.     //Set matrices for ortho
  159.     glMatrixMode(GL_PROJECTION);
  160.     glPushMatrix();
  161.     glLoadIdentity();
  162.     gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);
  163.  
  164.     glMatrixMode(GL_MODELVIEW);
  165.     glPushMatrix();
  166.     glLoadIdentity();
  167.  
  168.     //Print text
  169.     glRasterPos2f(-1.0f, 0.9f);
  170.     for(unsigned int i=0; i<strlen(fpsString); ++i)
  171.         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, fpsString[i]);
  172.  
  173.     //reset matrices
  174.     glMatrixMode(GL_PROJECTION);
  175.     glPopMatrix();
  176.     glMatrixMode(GL_MODELVIEW);
  177.     glPopMatrix();
  178.  
  179.     glFinish();
  180.     glutSwapBuffers();
  181.     glutPostRedisplay();
  182. }
  183.  
  184. //This function draw a cube ; forget the texture parameter for now
  185. void drawCube(GLfloat x,GLfloat y,GLfloat z,int texture,GLdouble size)
  186. {  
  187. GLfloat v[8][3];
  188.   GLint i;
  189.  
  190.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
  191.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
  192.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
  193.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
  194.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
  195.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
  196.  
  197.   glTranslatef(x, y, z);
  198.  
  199.   for (i = 5; i >= 0; i--) {
  200.     glBegin(GL_QUADS);
  201.     glNormal3fv(&n[i][0]);
  202.     glVertex3fv(&v[faces[i][0]][0]);
  203.     glVertex3fv(&v[faces[i][1]][0]);
  204.     glVertex3fv(&v[faces[i][2]][0]);
  205.     glVertex3fv(&v[faces[i][3]][0]);
  206.     glEnd();
  207.   }
  208.  
  209. }
  210.  
  211. //This function use 2 2d array (1 for height and one for texture) and draw the map.
  212. //It draw the map only once but the light is updatable.
  213. void drawMap()
  214. {
  215.     //Display lists for objects
  216.     static GLuint mapList=0, lightList=0;
  217.     int height;
  218.     int texture;
  219.     int i,j,n;
  220.  
  221.     //Create base if necessary
  222.     if(!mapList)
  223.     {
  224.         mapList=glGenLists(1);
  225.         glNewList(mapList, GL_COMPILE);
  226.         {
  227.             for (i = 0 ; i < MAP_ACROSS ; i++)
  228.             {  
  229.                 for (j = 0 ; j <MAP_DOWN ; j++)
  230.                 {
  231.                     height = heightMap1[j][i];
  232.                     texture = textureMap1[j][i];
  233.                     for (n=1 ; n<=height ; n++)
  234.                     {
  235.                         glColor3f(0.5f, 0.5f, 0.5f);
  236.                         glPushMatrix();
  237.                         drawCube(i*2,-(j*2),n*2,texture,2);
  238.                         glPopMatrix();             
  239.                     }
  240.                 }
  241.             }
  242.            
  243.         }
  244.         glEndList();
  245.     }
  246.  
  247.         lightList=glGenLists(1);
  248.         glNewList(lightList, GL_COMPILE);
  249.         {
  250.             glColor3f(1.0f, 0.0f, 0.0f);
  251.             glPushMatrix();
  252.  
  253.             glTranslatef(lightPosition.x, lightPosition.y, lightPosition.z);
  254.             glutSolidCube(0.5f);
  255.  
  256.             glPopMatrix();
  257.            
  258.         }
  259.         glEndList();
  260.  
  261.     //Draw objects
  262.     glCallList(mapList);
  263.     glCallList(lightList);
  264. }
  265.  
  266. ///////////////////////////////////////////////////////////////////////
  267. //Variable and constant
  268. ///////////////////////////////////////////////////////////////////////
  269. //for cube drawing
  270. static GLfloat n[6][3] =
  271.   {
  272.     {-1.0, 0.0, 0.0},
  273.     {0.0, 1.0, 0.0},
  274.     {1.0, 0.0, 0.0},
  275.     {0.0, -1.0, 0.0},
  276.     {0.0, 0.0, 1.0},
  277.     {0.0, 0.0, -1.0}
  278.   };
  279.   static GLint faces[6][4] =
  280.   {
  281.     {0, 1, 2, 3},
  282.     {3, 2, 6, 7},
  283.     {7, 6, 5, 4},
  284.     {4, 5, 1, 0},
  285.     {5, 6, 2, 1},
  286.     {7, 4, 0, 3}
  287.   };
  288.  
  289. //bias matrix
  290. MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
  291.         0.0f, 0.5f, 0.0f, 0.0f,
  292.         0.0f, 0.0f, 0.5f, 0.0f,
  293.         0.5f, 0.5f, 0.5f, 1.0f);
  294.  
  295. //camera and light
  296. VECTOR3D lightPosition(48.0f,-10.0f,13.0f);
  297. VECTOR3D cameraPosition(50.0f,-10.0f,50.0f);
  298. VECTOR3D cameraTarget(50.0f,-10.0f, 0.0f);
  299.  
  300. //shadow and matrix declaration
  301. int shadowMapWidth = windowWidth,shadowMapHeight = windowHeight;
  302. MATRIX4X4 lightProjectionMatrix, lightViewMatrix;
  303. MATRIX4X4 cameraProjectionMatrix, cameraViewMatrix;
  304. GLuint shadowMapTexture;
  305.  
  306. //i think that's pretty it.
  307.  
  308.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement