Advertisement
Guest User

Untitled

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