Original Post
Hey all,
I've been trying to get shadows to work on my simple terrain used for testing out a realtime shader for atmospheric phenomenas. The latter works fine, but the shadowing does not.
I am using following sources of information:
[1] Paul's Projects: Shadow Mapping Tutorial For the texture matrix.
[2] Organic Vectory: Shadow Mapping Tutorial For everything else, pretty much.
I initialize my FrameBuffer, Texture, and RenderBuffer as in [2]:
And render in the following way:
Things to note for the above:
- I have confirmed that the light_proj and light_view matrices are good by rendering the terrain using them.
- I have cleared the color and depth buffer before rendering the shadow buffer.
- Calling the above does not mess up my state - rendering the terrain and atmosphere afterwards works correctly (except for shadowing of course).
I render my heightmap using following:
Things to note about the above:
- shadow_mat_tmat and m_shadow_map_tex has been properly tranfered to the heightmap_renderer class.
In my fragment shader, I test for shadows using following code in my fragment shader:
Where position has been transformed with the modelview_inverse matrix in the vertex shader.
I've been on the forum looking, but I cannot find anything with close enough correspondance to what I'm doing here for it to be useful to fix a (probably) small error.
Can anyone spot what I am doing wrong? The result is always a pitch black terrain. There are no glitches or signs of z-fighting. I have tried from multiple viewpoints and multiple angles of sunlight.
Thanks :)
[Edited by - Itzelsnitch on June 29, 2010 1:35:37 PM]
I've been trying to get shadows to work on my simple terrain used for testing out a realtime shader for atmospheric phenomenas. The latter works fine, but the shadowing does not.
I am using following sources of information:
[1] Paul's Projects: Shadow Mapping Tutorial For the texture matrix.
[2] Organic Vectory: Shadow Mapping Tutorial For everything else, pretty much.
I initialize my FrameBuffer, Texture, and RenderBuffer as in [2]:
void atmosphere_effect::shadow_map_init() { shadow_map_size = 512; shadow_map_tex = 0; shadow_map_fb = 0; shadow_map_rb = 0; glGenTextures(1, &shadow_map_tex); glBindTexture(GL_TEXTURE_2D, shadow_map_tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow_map_size, shadow_map_size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); glGenFramebuffers(1, &shadow_map_fb); glBindFramebuffer(GL_FRAMEBUFFER, shadow_map_fb); glGenRenderbuffers(1, &shadow_map_rb); glBindRenderbuffer(GL_RENDERBUFFER, shadow_map_rb); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB8, shadow_map_size, shadow_map_size); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, shadow_map_rb); glBindFramebuffer(GL_FRAMEBUFFER, 0);}And render in the following way:
void atmosphere_effect::render_shadow_buffer() { const glm::vec3 terrain_middle(0.0f, 6360.f, 0.0f); glm::mat4 light_proj = glm::ortho(-100.f, 100.f, -100.f, 100.f, 10.f, 1000.f); glm::mat4 light_view = glm::lookAt(terrain_middle + sun_direction_ * 400.f, terrain_middle, glm::vec3(.05f, 1, 0)); glm::mat4 bias(.5f, .0f, .0f, .0f, .0f, .5f, .0f, .0f, .0f, .0f, .5f, .0f, .5f, .5f, .5f, 1.f); shadow_map_tmat = bias * light_proj * light_view; //Save old viewport int old_vp[4]; glGetIntegerv(GL_VIEWPORT, old_vp); //Render glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glCullFace(GL_FRONT); glViewport(0, 0, shadow_map_size, shadow_map_size); glBindFramebuffer(GL_FRAMEBUFFER, shadow_map_fb); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadow_map_tex, 0); terrain_renderer_->draw(sun_direction_, light_view, light_proj * light_view); //Restore state glBindFramebuffer(GL_FRAMEBUFFER, 0); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glCullFace(GL_BACK); glViewport(old_vp[0], old_vp[1], old_vp[2], old_vp[3]);}Things to note for the above:
- I have confirmed that the light_proj and light_view matrices are good by rendering the terrain using them.
- I have cleared the color and depth buffer before rendering the shadow buffer.
- Calling the above does not mess up my state - rendering the terrain and atmosphere afterwards works correctly (except for shadowing of course).
I render my heightmap using following:
void heightmap_renderer::draw(const glm::vec3 &sun_direction, const glm::mat4 &modelview, const glm::mat4 &modelviewprojection){ m_shader->enable(); glEnable(GL_TEXTURE_2D); /* ... setup of other textures omitted ... */ glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, m_shadow_map_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); /* ... setup of some uniforms omitted ... */ m_shader->set_uniform1i("shadowmap", 4); m_shader->set_uniform("modelview", modelview); m_shader->set_uniform("modelviewprojection", modelviewprojection); m_shader->set_uniform("modelview_inverse", glm::inverse(modelview)); m_shader->set_uniform("shadowmat", shadow_map_tmat); m_geom->draw();}Things to note about the above:
- shadow_mat_tmat and m_shadow_map_tex has been properly tranfered to the heightmap_renderer class.
In my fragment shader, I test for shadows using following code in my fragment shader:
gl_FragColor = shadow2D(shadowmap, vec3(shadowmat * modelview_inverse * vec4(position, 1.0)));Where position has been transformed with the modelview_inverse matrix in the vertex shader.
I've been on the forum looking, but I cannot find anything with close enough correspondance to what I'm doing here for it to be useful to fix a (probably) small error.
Can anyone spot what I am doing wrong? The result is always a pitch black terrain. There are no glitches or signs of z-fighting. I have tried from multiple viewpoints and multiple angles of sunlight.
Thanks :)
[Edited by - Itzelsnitch on June 29, 2010 1:35:37 PM]