shader - OpenGL Shadow Map -


i trying basic shadow map reason doesn't render properly.

video of problem

i render house using flat shader:

int shadowmapwidth = window_size_x * (int)shadow_map_ratio; int shadowmapheight =  window_size_y * (int)shadow_map_ratio;  // rendering shadow texture. glactivetexture(gl_texture0); call_gl(glbindtexture(gl_texture_2d, shadowtexture)); // bind framebuffer. call_gl(glbindframebuffer(gl_framebuffer, shadowfbo)); //clear call_gl(glclear(gl_depth_buffer_bit)); call_gl(glviewport(0, 0, shadowmapwidth, shadowmapheight)); call_gl(glcolormask(gl_false, gl_false, gl_false, gl_false)); //render stuff flatshader.use(); flatshader["basecolor"] = glm::vec4(1.0f,1.0f,1.0f,1.0f); flatshader["pvm"] = projectionmatrix*pointlight.viewmatrix*cursor.modelmatrix; cursor.draw(); //binds vao , draws  // revert scene. call_gl(glbindframebuffer(gl_framebuffer, 0)); call_gl(glcolormask(gl_true, gl_true, gl_true, gl_true)); call_gl(glviewport(0, 0, window_size_x, window_size_y)); 

notice render house. don't render floor in depth-buffer pass.

following render quad represents floor using following shader pair:

/* [vert] */  #version 330  in vec3 in_position; in vec2 in_texcoord;  uniform mat4 shadowmatrix; uniform mat4 mvp;  out vec2 uv; out vec4 shadowproj;  void main() {     gl_position = mvp*vec4(in_position,1.0);     shadowproj = shadowmatrix*vec4(in_position,1.0);     uv = in_texcoord; } 

and fragment shader:

/* [frag] */  #version 330  in vec2 uv; in vec4 shadowproj;  out vec4 fragcolor;  uniform sampler2d texturex; uniform sampler2dshadow shadowmap;  void main() {     fragcolor = vec4(texture(texturex, uv).rgb,1);      float shadow = 1.0;     shadow = textureproj(shadowmap,shadowproj);      fragcolor *= shadow;  } 

i draw house again in color , ... floor:

textureshader.use();  gluniform1i(baseimageloc, 0); //texture unit 0 base images. gluniform1i(shadowmaploc, 1); //texture unit 1 shadow maps.  glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, floortexture);  glactivetexture(gl_texture1); glbindtexture(gl_texture_2d, shadowtexture); textureshader["shadowmatrix"] = projectionmatrix*pointlight.viewmatrix*floormatrix; textureshader["mvp"] = projectionmatrix*viewmatrix*floormatrix; call_gl(glbindvertexarray(floorvao)); call_gl(gldrawarrays(gl_triangles,0,18));  glfwswapbuffers(); 

anybody seen behavior before? idea wrong? way light's coordaintes place directly on top of house shadow should directly below house on floor (but ends sideways).

for reference here how generate shadow fbo:

int shadowmapwidth = window_size_x * (int)shadow_map_ratio; int shadowmapheight =  window_size_y * (int)shadow_map_ratio;  glgentextures(1, &shadowtexture); glbindtexture(gl_texture_2d, shadowtexture); glteximage2d(gl_texture_2d, 0, gl_depth_component, shadowmapwidth, shadowmapheight, 0, gl_depth_component, gl_unsigned_byte, 0); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge); gltexparameteri(gl_texture_2d, gl_texture_compare_mode, gl_compare_ref_to_texture); gltexparameteri(gl_texture_2d, gl_texture_compare_func, gl_less); glteximage2d(gl_texture_2d,0,gl_depth_component,shadowmapwidth,shadowmapheight,0,gl_depth_component,gl_float,null); gltexparameteri(gl_texture_2d,gl_texture_compare_mode,gl_compare_r_to_texture); glbindtexture(gl_texture_2d, 0); //unbind texture  glgenframebuffers(1, &shadowfbo); glbindframebuffer(gl_framebuffer, shadowfbo); glframebuffertexture2d(gl_framebuffer, gl_depth_attachment, gl_texture_2d, shadowtexture, 0); gldrawbuffer(gl_none); glreadbuffer(gl_none);  if (glcheckframebufferstatus(gl_framebuffer) != gl_framebuffer_complete) { printf("gl_framebuffer_complete error 0x%x", glcheckframebufferstatus(gl_framebuffer)); }  glcleardepth(1.0f); glenable(gl_depth_test); // needed when rendering shadow map. avoid artifacts. glpolygonoffset(1.0f, 0.0f); glbindframebuffer(gl_framebuffer, 0); //to convert texture coordinates -1 ~ 1    glfloat biasmatrixf[] = {     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 };  biasmatrix = glm::make_mat4(biasmatrixf); 

it looks forgot multiply shadow matrix bias matrix.


Comments

Popular posts from this blog

stringtemplate - StringTemplate4 if conditional with length -