LWJGL Shadow Mapping Problem

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • LWJGL Shadow Mapping Problem

    Hallo erstmal, ich habe schon seit einigen Tagen das Problem, dass die Shadow Maps, die ich in Java (LWJGL) eingebaut habe, nicht funktionieren.
    Hier ist der Code der Render - Passage:

    Source Code

    1. float r = 0.5f;
    2. float g = 0.8f;
    3. float b = 1.0f;
    4. glClearColor(r, g, b, 1);
    5. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    6. int w = Display.getWidth();
    7. int h = Display.getHeight();
    8. float aspect = w / (float) h;
    9. glEnable(GL_DEPTH_TEST);
    10. glDepthFunc(GL_LEQUAL);
    11. glClearDepth(1.0);
    12. glShadeModel(GL_SMOOTH);
    13. if (true) {
    14. glColorMask(false, false, false, false);
    15. glViewport(0, 0, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE);
    16. glMatrixMode(GL_PROJECTION);
    17. glLoadIdentity();
    18. gluPerspective(70, aspect, 0.05f, 1000f);
    19. glMatrixMode(GL_MODELVIEW);
    20. for (int i = 0; i < lights.size(); i++) {
    21. Light light = lights.get(i);
    22. for (int d = 0; d < 6; d++) {
    23. glBindFramebuffer(GL_FRAMEBUFFER, cubeMaps[i][d]);
    24. glClear(GL_DEPTH_BUFFER_BIT);
    25. glLoadIdentity();
    26. if (d == 0) glRotatef(90, 0, 1, 0);
    27. if (d == 1) glRotatef(270, 0, 1, 0);
    28. if (d == 2) glRotatef(90, 1, 0, 0);
    29. if (d == 3) glRotatef(-90, 1, 0, 0);
    30. if (d == 4) glRotatef(0, 0, 0, 1);
    31. if (d == 5) glRotatef(180, 0, 0, 1);
    32. glLight(GL_LIGHT0, GL_POSITION, GLX.getf(0, 0, 0, 1));
    33. glTranslatef(-(float) light.pos.x, -(float) light.pos.y, -(float) light.pos.z);
    34. glCallList(displayList);
    35. }
    36. }
    37. glBindFramebuffer(GL_FRAMEBUFFER, 0);
    38. glColorMask(true, true, true, true);
    39. }
    40. glViewport(0, 0, w, h);
    41. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    42. glMatrixMode(GL_PROJECTION);
    43. glLoadIdentity();
    44. gluPerspective(70, aspect, 0.05f, 1000f);
    45. glMatrixMode(GL_MODELVIEW);
    46. glLoadIdentity();
    47. glRotatef(yRot, 1, 0, 0);
    48. glRotatef(xRot, 0, 1, 0);
    49. glTranslatef(-xCam, -yCam, -zCam);
    50. glEnable(GL_BLEND);
    51. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    52. glEnable(GL_ALPHA_TEST);
    53. glAlphaFunc(GL_GREATER, 0.1f);
    54. glEnable(GL_COLOR_MATERIAL);
    55. Shader.depthShader.enable();
    56. Shader.depthShader.bind("texture", 0);
    57. glClientActiveTexture(GL_TEXTURE0);
    58. glActiveTexture(GL_TEXTURE0);
    59. glBindTexture(GL_TEXTURE_2D, terrainTexture);
    60. glEnable(GL_TEXTURE_2D);
    61. glDisable(GL_LIGHTING);
    62. glCallList(displayList);
    63. glDisable(GL_TEXTURE_2D);
    64. glDisable(GL_COLOR_MATERIAL);
    65. glDisable(GL_ALPHA_TEST);
    66. glDepthMask(false);
    67. glEnable(GL_BLEND);
    68. glBlendFunc(GL_ONE, GL_ONE);
    69. glColorMask(true, true, true, true);
    70. Shader.lightShader.enable();
    71. Shader.lightShader.bind("shadowMap", 1);
    72. for (int i = 0; i < lights.size(); i++) {
    73. glClientActiveTexture(GL_TEXTURE1);
    74. glActiveTexture(GL_TEXTURE1);
    75. glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapTextures[i]);
    76. Light light = lights.get(i);
    77. float xx = (float) light.pos.x;
    78. float yy = (float) light.pos.y;
    79. float zz = (float) light.pos.z;
    80. float rr = light.r;
    81. float gg = light.g;
    82. float bb = light.b;
    83. glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 1.0f / (light.size));
    84. glLight(GL_LIGHT0, GL_POSITION, GLX.getf(xx, yy, zz, 1));
    85. glLight(GL_LIGHT0, GL_DIFFUSE, GLX.getf(rr, gg, bb, 1));
    86. glLight(GL_LIGHT0, GL_AMBIENT, GLX.getf(0, 0, 0, 1));
    87. glLight(GL_LIGHT0, GL_SPECULAR, GLX.getf(rr, gg, bb, 1));
    88. glMaterial(GL_FRONT, GL_SPECULAR, GLX.getf(rr, gg, bb, 1));
    89. glMaterialf(GL_FRONT, GL_SHININESS, 16.0f);
    90. glCallList(displayList);
    91. }
    92. Shader.lightShader.disable();
    93. glDepthMask(true);
    94. glDisable(GL_BLEND);
    95. glDisable(GL_CULL_FACE);
    Display All


    Und hier der Code für das Erstellen des Framebuffers:

    Source Code

    1. IntBuffer ib = MemoryTracker.createIntBuffer(6);
    2. for (int i = 0; i < cubeMaps.length; i++) {
    3. int shadowMap = cubeMapTextures[i] = glGenTextures();
    4. ib.clear();
    5. glGenFramebuffers(ib);
    6. glBindTexture(GL_TEXTURE_CUBE_MAP, shadowMap);
    7. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    8. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    9. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    10. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    11. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    12. for (int j = 0; j < 6; j++) {
    13. int id = cubeMaps[i][j] = ib.get(j);
    14. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, 0, GL_DEPTH_COMPONENT24, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (ByteBuffer) null);
    15. glBindFramebuffer(GL_FRAMEBUFFER, id);
    16. glDrawBuffer(GL_NONE);
    17. glReadBuffer(GL_NONE);
    18. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, shadowMap, 0);
    19. glBindFramebuffer(GL_FRAMEBUFFER, 0);
    20. }
    21. int error = glGetError();
    22. if (error != 0) {
    23. System.out.println(error + ": " + gluErrorString(error));
    24. }
    25. }
    Display All


    Vielen Dank für eure Hilfe :D
    LG
  • Und hier natürlich noch die Shaders:

    Vertex:
    #version 130

    uniform samplerCubeShadow shadowMap;

    out vec3 diffuse1;
    out vec3 normal;
    out vec4 verpos;
    out vec3 lpos, ldir;

    void main() {
    normal = gl_NormalMatrix * gl_Normal;
    verpos = gl_ModelViewMatrix * gl_Vertex;
    lpos = (gl_LightSource[0].position.xyz - verpos.xyz).xyz;
    ldir = (gl_TextureMatrix[0] * (((gl_LightSource[0].position - verpos) * vec4(-1.0, -1.0, 1.0, 1.0)))).xyz;

    diffuse1 = (gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse).rgb * gl_Color.rgb;

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }

    Fragment:
    #version 130

    uniform samplerCube shadowMap;

    in vec3 diffuse1;
    in vec3 normal;
    in vec4 verpos;
    in vec3 lpos, ldir;

    void main() {
    vec3 color = vec3(0, 0, 0);

    float NdotL = dot(normal, lpos.xyz);
    if(NdotL > 0.0) {
    NdotL = dot(normal, normalize(lpos.xyz));
    float dist = length(ldir);
    float att = (1.0 / (gl_LightSource[0].quadraticAttenuation * dist + 0.5)) * NdotL;
    color += att * diffuse1;
    }

    gl_FragColor = vec4(color*0.5, 1);
    }