Advertisement
Guest User

Untitled

a guest
Jul 27th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1.     // position the light
  2.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos_);
  3.  
  4.     // switch the framebuffer for the first pass
  5.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
  6.     glViewport(0, 0, 512, 512);
  7.     glClearColor(0.5, 0.2, 0.1, 1.0f);
  8.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  9.    
  10.    
  11.     // set up the projection parameters from the light's POV
  12.     glMatrixMode(GL_PROJECTION);
  13.     glPushMatrix();
  14.         glLoadIdentity();
  15.         gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
  16.  
  17.     glMatrixMode(GL_MODELVIEW);
  18.     glPushMatrix();
  19.         glLoadIdentity();
  20.         // translate to the light's position
  21.         gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
  22.  
  23.         // render the scene to get the depth information
  24.         renderSceneElements();
  25.     glPopMatrix();
  26.    
  27.     // end the projection modification
  28.     glMatrixMode(GL_PROJECTION);
  29.     glPopMatrix();
  30.     glMatrixMode(GL_MODELVIEW);
  31.  
  32.     // switch back to the system framebuffer for the second pass
  33.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  34.     glClearColor(1,1,1,1);
  35.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  36.     glViewport(0, 0, 512, 512);
  37.    
  38.     // matrix defining the planes for S, T, R, Q components for texture generation
  39.     float planeMatrix[16];
  40.     glPushMatrix();
  41.         glLoadIdentity();
  42.         // compensate for the eye-coordinate to texture coordinate conversion: [-1,1] to [0,1]
  43.         glTranslatef(0.5f, 0.5f, 0.5f);
  44.         glScalef(0.5f, 0.5f, 0.5f);
  45.    
  46.         // do the perspective projection and translate to the light's position
  47.         gluPerspective(lightFOV_, lightAspect_, lightNear_, lightFar_);
  48.         gluLookAt(lightPos_[0], lightPos_[1], lightPos_[2], -1.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f);
  49.        
  50.         glGetFloatv(GL_MODELVIEW_MATRIX, planeMatrix);
  51.     glPopMatrix();
  52.    
  53.     // go from OpenGL's column-major to row-major matrix form
  54.     transposeMatrix16(planeMatrix);
  55.  
  56.     // set up the type for texture generation
  57.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  58.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  59.     glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  60.     glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  61.    
  62.     // data for texture generation
  63.     glTexGenfv(GL_S, GL_OBJECT_PLANE, &planeMatrix[0]);
  64.     glTexGenfv(GL_T, GL_OBJECT_PLANE, &planeMatrix[4]);
  65.     glTexGenfv(GL_R, GL_OBJECT_PLANE, &planeMatrix[8]);
  66.     glTexGenfv(GL_Q, GL_OBJECT_PLANE, &planeMatrix[12]);
  67.  
  68.     glEnable(GL_TEXTURE_GEN_S);
  69.     glEnable(GL_TEXTURE_GEN_T);
  70.     glEnable(GL_TEXTURE_GEN_R);
  71.     glEnable(GL_TEXTURE_GEN_Q);
  72.    
  73.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  74.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  75.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  76.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  77.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  78.     glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
  79.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
  80.    
  81.     glEnable(GL_TEXTURE_2D);
  82.     glBindTexture(GL_TEXTURE_2D, shadowmap_);
  83.  
  84.     glEnable(GL_LIGHTING);
  85.     glEnable(GL_LIGHT0);
  86.  
  87.     renderSceneElements();
  88.  
  89.     glDisable(GL_LIGHTING);
  90.     glDisable(GL_LIGHT0);
  91.  
  92.     glDisable(GL_TEXTURE_2D);
  93.  
  94.     glDisable(GL_TEXTURE_GEN_Q);
  95.     glDisable(GL_TEXTURE_GEN_R);
  96.     glDisable(GL_TEXTURE_GEN_T);
  97.     glDisable(GL_TEXTURE_GEN_S);
  98.  
  99.     glPushMatrix();
  100.         glEnable(GL_TEXTURE_2D);
  101.         //glBindTexture(GL_TEXTURE_2D, shadowmap_);
  102.         glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_NONE);
  103.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  104.         glTranslatef(3.0f, 2.0f, 5.0f);
  105.         glBegin(GL_QUADS);
  106.             glTexCoord2f(0.0f, 0.0f);
  107.             glVertex3f(0.0f, 0.0f, 0.0f);
  108.  
  109.             glTexCoord2f(1.0f, 0.0f);
  110.             glVertex3f(3.0f, 0.0f, 0.0f);
  111.  
  112.             glTexCoord2f(1.0f, 1.0f);
  113.             glVertex3f(3.0f, 3.0f, 0.0f);
  114.  
  115.             glTexCoord2f(0.0f, 1.0f);
  116.             glVertex3f(0.0f, 3.0f, 0.0f);
  117.         glEnd();
  118.         glDisable(GL_TEXTURE_2D);
  119.     glPopMatrix();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement