Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static float[] TexEyeMat = {
- 0.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f
- };
- private boolean UseShadow = true;
- private boolean ShowMap = true;
- private float[] ShadowMat = new float[16];
- int DepthW = 512;
- int DepthH = 512;
- int DepthPrecision = 4;
- private void display() {
- /* First pass, depth values only. */
- GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
- renderToShadowMap();
- /* Normal rendering pass */
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- GL11.glPushMatrix();
- drawPlanes();
- light.render();
- GL11.glLoadIdentity();
- camera.lookThrough();
- if (UseShadow) {
- enableShadowing();
- }
- if (ShowMap) {
- showDepthMap();
- }
- GL11.glPopMatrix();
- //CheckGL();
- try {
- //Display.swapBuffers();
- } catch (Exception e) {
- e.printStackTrace(System.err);
- }
- }
- private void renderToShadowMap() {
- int[] vport = {0, 0, 0, 0};
- GL11.glGetInteger(GL11.GL_VIEWPORT, BufferUtility.wrapDirect(vport));
- GL11.glViewport(0, 0, DepthW, DepthH);
- /* Just writing depth values */
- GL11.glDisable(GL11.GL_LIGHTING);
- GL11.glDisable(GL11.GL_TEXTURE_2D);
- GL11.glColorMask(false, false, false, false);
- /* Offset all polys to avoid self-shadowing */
- GL11.glEnable(GL11.GL_POLYGON_OFFSET_FILL);
- GL11.glPolygonOffset(1, 1);
- /* View from current light position */
- GL11.glPushMatrix();
- GL11.glLoadIdentity();
- light.lookThrough();//--------------------------------------------------
- /* Render scene elements that contribute shadows */
- drawPlanes();//---------------------------------------------------------
- /* And save */
- GL11.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT,
- 0, 0, DepthW, DepthH, 0);
- GL11.glPopMatrix();
- /* Calc transformation matrix from texture coords
- to light. We could do the matrix math ourselves,
- but may as well let OpenGL do it. */
- GL11.glPushMatrix();
- GL11.glLoadIdentity();
- GL11.glMultMatrix(BufferUtility.wrapDirect(TexEyeMat));
- GLU.gluPerspective(45.0f, (float) displayMode.getWidth() / displayMode.getHeight(), 0.1f, 100f);
- light.lookThrough();
- GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, BufferUtility.wrapDirect(ShadowMat));
- GL11.glPopMatrix();
- /* Restore everything */
- GL11.glColorMask(true, true, true, true);
- GL11.glEnable(GL11.GL_LIGHTING);
- GL11.glViewport(vport[0], vport[1], vport[2], vport[3]);
- }
- private void enableShadowing() {
- Vector4f v = new Vector4f();
- FloatBuffer buff = BufferUtility.wrapDirect(v);
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- /* Don't forget this! We're only interested in the texture
- depth value, but still need the fragment calculated here
- to replace the original polygon. */
- GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
- /* Projective texture */
- GL11.glEnable(GL11.GL_TEXTURE_GEN_S);
- GL11.glEnable(GL11.GL_TEXTURE_GEN_T);
- GL11.glEnable(GL11.GL_TEXTURE_GEN_R);
- GL11.glEnable(GL11.GL_TEXTURE_GEN_Q);
- /* The shadow matrix transforms a point in eye space coords
- to light-source relative. s,t,r,q are equivalent to the
- x,y,z,w homogenous coords and each is multiplied by one
- row of the shadow matrix. */
- getMat(v, ShadowMat, 0, 4, 8, 12);
- //GL11.glGetMaterial(int face, int pname, params);
- GL11.glTexGen(GL11.GL_S, GL11.GL_EYE_PLANE, buff);
- getMat(v, ShadowMat, 1, 5, 9, 13);
- GL11.glTexGen(GL11.GL_T, GL11.GL_EYE_PLANE, buff);
- getMat(v, ShadowMat, 2, 6, 10, 14);
- GL11.glTexGen(GL11.GL_R, GL11.GL_EYE_PLANE, buff);
- getMat(v, ShadowMat, 3, 7, 11, 15);
- GL11.glTexGen(GL11.GL_Q, GL11.GL_EYE_PLANE, buff);
- /* Shadow depth test */
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
- GL14.GL_TEXTURE_COMPARE_MODE, GL14.GL_COMPARE_R_TO_TEXTURE);
- //.GL_TEXTURE_COMPARE_MODE_ARB
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
- GL14.GL_TEXTURE_COMPARE_FUNC, GL11.GL_LEQUAL);
- /* Generate luminance of 0 or 1 depending on result of shadow
- test. Can instead generate alpha and use alpha testing */
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
- GL14.GL_DEPTH_TEXTURE_MODE, GL11.GL_LUMINANCE);
- }
- void getMat(Vector4f v, float[] mat, int i, int j, int k, int l) {
- v.x = mat[i];
- v.y = mat[j];
- v.z = mat[k];
- v.w = mat[l];
- }
- private void showDepthMap() {
- float scale = 0.5f;
- /* Switch to ortho for display as 2D image */
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glPushMatrix();
- GL11.glLoadIdentity();
- GLU.gluOrtho2D(0, displayMode.getWidth(), 0, displayMode.getHeight());
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glPushMatrix();
- GL11.glLoadIdentity();
- /* We could read the depth values from the buffer into
- memory and then write it back as pixels, but that is
- a major performance hit. It's much quicker to use the
- shadow map as a regular grayscale texture on a quad */
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
- GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
- GL11.glDisable(GL11.GL_TEXTURE_GEN_R);
- GL11.glDisable(GL11.GL_TEXTURE_GEN_Q);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
- GL14.GL_TEXTURE_COMPARE_MODE, GL11.GL_NONE);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
- GL14.GL_DEPTH_TEXTURE_MODE, GL11.GL_LUMINANCE);
- GL11.glDisable(GL11.GL_LIGHTING);
- GL11.glTranslatef(displayMode.getWidth() - DepthW * scale, displayMode.getHeight() - DepthH * scale, 0);
- GL11.glScalef(scale, scale, scale);
- GL11.glBegin(GL11.GL_QUADS);
- GL11.glTexCoord2f(0, 0);
- GL11.glVertex2i(0, 0);
- GL11.glTexCoord2f(1, 0);
- GL11.glVertex2i(DepthW, 0);
- GL11.glTexCoord2f(1, 1);
- GL11.glVertex2i(DepthW, DepthH);
- GL11.glTexCoord2f(0, 1);
- GL11.glVertex2i(0, DepthH);
- GL11.glEnd();
- GL11.glPopMatrix();
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glPopMatrix();
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement