Advertisement
StephenDsDude

Shadow Methods Port?

Jul 2nd, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.82 KB | None | 0 0
  1.     static float[] TexEyeMat = {
  2.         0.5f, 0.0f, 0.0f, 0.0f,
  3.         0.0f, 0.5f, 0.0f, 0.0f,
  4.         0.0f, 0.0f, 0.5f, 0.0f,
  5.         0.5f, 0.5f, 0.5f, 1.0f
  6.     };
  7.     private boolean UseShadow = true;
  8.     private boolean ShowMap = true;
  9.     private float[] ShadowMat = new float[16];
  10.     int DepthW = 512;
  11.     int DepthH = 512;
  12.     int DepthPrecision = 4;
  13.  
  14.  
  15.  
  16.  
  17.     private void display() {
  18.         /* First pass, depth values only. */
  19.         GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
  20.         renderToShadowMap();
  21.  
  22.         /* Normal rendering pass */
  23.  
  24.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  25.         GL11.glPushMatrix();
  26.         drawPlanes();
  27.         light.render();
  28.         GL11.glLoadIdentity();
  29.         camera.lookThrough();
  30.         if (UseShadow) {
  31.             enableShadowing();
  32.         }
  33.         if (ShowMap) {
  34.             showDepthMap();
  35.         }
  36.  
  37.         GL11.glPopMatrix();
  38.         //CheckGL();
  39.         try {
  40.             //Display.swapBuffers();
  41.         } catch (Exception e) {
  42.             e.printStackTrace(System.err);
  43.         }
  44.     }
  45.    
  46.    
  47.    
  48.    
  49.     private void renderToShadowMap() {
  50.         int[] vport = {0, 0, 0, 0};
  51.  
  52.         GL11.glGetInteger(GL11.GL_VIEWPORT, BufferUtility.wrapDirect(vport));
  53.         GL11.glViewport(0, 0, DepthW, DepthH);
  54.  
  55.         /* Just writing depth values */
  56.         GL11.glDisable(GL11.GL_LIGHTING);
  57.         GL11.glDisable(GL11.GL_TEXTURE_2D);
  58.         GL11.glColorMask(false, false, false, false);
  59.  
  60.         /* Offset all polys to avoid self-shadowing */
  61.         GL11.glEnable(GL11.GL_POLYGON_OFFSET_FILL);
  62.         GL11.glPolygonOffset(1, 1);
  63.  
  64.         /* View from current light position */
  65.         GL11.glPushMatrix();
  66.         GL11.glLoadIdentity();
  67.         light.lookThrough();//--------------------------------------------------
  68.  
  69.         /* Render scene elements that contribute shadows */
  70.         drawPlanes();//---------------------------------------------------------
  71.  
  72.         /* And save */
  73.         GL11.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT,
  74.                 0, 0, DepthW, DepthH, 0);
  75.  
  76.         GL11.glPopMatrix();
  77.  
  78.         /* Calc transformation matrix from texture coords
  79.         to light. We could do the matrix math ourselves,
  80.         but may as well let OpenGL do it. */
  81.         GL11.glPushMatrix();
  82.         GL11.glLoadIdentity();
  83.         GL11.glMultMatrix(BufferUtility.wrapDirect(TexEyeMat));
  84.         GLU.gluPerspective(45.0f, (float) displayMode.getWidth() / displayMode.getHeight(), 0.1f, 100f);
  85.         light.lookThrough();
  86.  
  87.         GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, BufferUtility.wrapDirect(ShadowMat));
  88.  
  89.         GL11.glPopMatrix();
  90.  
  91.         /* Restore everything */
  92.         GL11.glColorMask(true, true, true, true);
  93.         GL11.glEnable(GL11.GL_LIGHTING);
  94.         GL11.glViewport(vport[0], vport[1], vport[2], vport[3]);
  95.     }
  96.    
  97.    
  98.    
  99.     private void enableShadowing() {
  100.         Vector4f v = new Vector4f();
  101.         FloatBuffer buff = BufferUtility.wrapDirect(v);
  102.  
  103.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  104.         /* Don't forget this! We're only interested in the texture
  105.         depth value, but still need the fragment calculated here
  106.         to replace the original polygon. */
  107.         GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
  108.  
  109.         /* Projective texture */
  110.         GL11.glEnable(GL11.GL_TEXTURE_GEN_S);
  111.         GL11.glEnable(GL11.GL_TEXTURE_GEN_T);
  112.         GL11.glEnable(GL11.GL_TEXTURE_GEN_R);
  113.         GL11.glEnable(GL11.GL_TEXTURE_GEN_Q);
  114.  
  115.         /* The shadow matrix transforms a point in eye space coords
  116.         to light-source relative. s,t,r,q are equivalent to the
  117.         x,y,z,w homogenous coords and each is multiplied by one
  118.         row of the shadow matrix.                             */
  119.         getMat(v, ShadowMat, 0, 4, 8, 12);
  120.         //GL11.glGetMaterial(int face, int pname, params);
  121.         GL11.glTexGen(GL11.GL_S, GL11.GL_EYE_PLANE, buff);
  122.         getMat(v, ShadowMat, 1, 5, 9, 13);
  123.         GL11.glTexGen(GL11.GL_T, GL11.GL_EYE_PLANE, buff);
  124.         getMat(v, ShadowMat, 2, 6, 10, 14);
  125.         GL11.glTexGen(GL11.GL_R, GL11.GL_EYE_PLANE, buff);
  126.         getMat(v, ShadowMat, 3, 7, 11, 15);
  127.         GL11.glTexGen(GL11.GL_Q, GL11.GL_EYE_PLANE, buff);
  128.  
  129.         /* Shadow depth test */
  130.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
  131.                 GL14.GL_TEXTURE_COMPARE_MODE, GL14.GL_COMPARE_R_TO_TEXTURE);
  132.         //.GL_TEXTURE_COMPARE_MODE_ARB
  133.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
  134.                 GL14.GL_TEXTURE_COMPARE_FUNC, GL11.GL_LEQUAL);
  135.  
  136.         /* Generate luminance of 0 or 1 depending on result of shadow
  137.         test. Can instead generate alpha and use alpha testing   */
  138.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
  139.                 GL14.GL_DEPTH_TEXTURE_MODE, GL11.GL_LUMINANCE);
  140.     }
  141.    
  142.    
  143.    
  144.     void getMat(Vector4f v, float[] mat, int i, int j, int k, int l) {
  145.         v.x = mat[i];
  146.         v.y = mat[j];
  147.         v.z = mat[k];
  148.         v.w = mat[l];
  149.     }
  150.    
  151.    
  152.    
  153.     private void showDepthMap() {
  154.         float scale = 0.5f;
  155.  
  156.         /* Switch to ortho for display as 2D image */
  157.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  158.         GL11.glPushMatrix();
  159.         GL11.glLoadIdentity();
  160.         GLU.gluOrtho2D(0, displayMode.getWidth(), 0, displayMode.getHeight());
  161.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  162.         GL11.glPushMatrix();
  163.         GL11.glLoadIdentity();
  164.  
  165.         /* We could read the depth values from the buffer into
  166.         memory and then write it back as pixels, but that is
  167.         a major performance hit. It's much quicker to use the
  168.         shadow map as a regular grayscale texture on a quad */
  169.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  170.         GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
  171.         GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
  172.         GL11.glDisable(GL11.GL_TEXTURE_GEN_R);
  173.         GL11.glDisable(GL11.GL_TEXTURE_GEN_Q);
  174.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
  175.                 GL14.GL_TEXTURE_COMPARE_MODE, GL11.GL_NONE);
  176.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
  177.                 GL14.GL_DEPTH_TEXTURE_MODE, GL11.GL_LUMINANCE);
  178.  
  179.         GL11.glDisable(GL11.GL_LIGHTING);
  180.         GL11.glTranslatef(displayMode.getWidth() - DepthW * scale, displayMode.getHeight() - DepthH * scale, 0);
  181.         GL11.glScalef(scale, scale, scale);
  182.         GL11.glBegin(GL11.GL_QUADS);
  183.         GL11.glTexCoord2f(0, 0);
  184.         GL11.glVertex2i(0, 0);
  185.         GL11.glTexCoord2f(1, 0);
  186.         GL11.glVertex2i(DepthW, 0);
  187.         GL11.glTexCoord2f(1, 1);
  188.         GL11.glVertex2i(DepthW, DepthH);
  189.         GL11.glTexCoord2f(0, 1);
  190.         GL11.glVertex2i(0, DepthH);
  191.         GL11.glEnd();
  192.  
  193.         GL11.glPopMatrix();
  194.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  195.         GL11.glPopMatrix();
  196.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  197.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement