c++ - texture(...) function always returns 0 -
i have been trying shadow mapping work quite time , still no closer month ago. beginning think may issue gl drivers because haven't been able see side effects such "light acne."
i told start using sampler2dshadow
instead of sampler2d
, did. however, whenever sample map using texture(...)
result 0. why happening?
i have tried entering manual depthcoord values , still 0. result of shadowmap (i.e way has been setup?) or driver issue?
framebuffer setup
framebuffername = 0; glgenframebuffers(1, &framebuffername); glbindframebuffer(gl_framebuffer, framebuffername); glgentextures(1, &depthtexture); glbindtexture(gl_texture_2d, depthtexture); gltexparameteri( gl_texture_2d, gl_texture_min_filter, gl_nearest ); gltexparameteri( gl_texture_2d, gl_texture_mag_filter, gl_nearest ); gltexparameteri( gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge ); gltexparameteri( gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge ); gltexparameteri(gl_texture_2d, gl_texture_compare_func, gl_lequal); gltexparameteri(gl_texture_2d, gl_texture_compare_mode, gl_compare_ref_to_texture); glteximage2d(gl_texture_2d, 0,gl_depth_component, 512, 512, 0, gl_depth_component, gl_float, 0); glframebuffertexture(gl_framebuffer, gl_depth_attachment, depthtexture, 0); gldrawbuffer(gl_none); glreadbuffer(gl_none); glbindframebuffer(gl_framebuffer, 0);
fragment shader
uniform sampler2dshadow shadowmap; in vec4 depthcoord; void main(){ //the final color after lighting stored 'finalcolor' float visibility = 1.0; if (texture(shadowmap, depthcoord.xyz) == 0.0){ visibility = 0.1; } color = vec4(finalcolor.rgb * visibility, 1.0); }
rendering code
glm::vec3 lightinvdir = glm::vec3(0,-10,-10); glm::mat4 depthprojectionmatrix = glm::ortho<float>(-10,10,-10,10,-10,20); glm::mat4 depthviewmatrix = glm::lookat(lightinvdir, glm::vec3(0,0,0), glm::vec3(0,1,0)); glm::mat4 depthmodelmatrix = glm::mat4(1.0); glm::mat4 depthmvp = depthprojectionmatrix * depthviewmatrix * depthmodelmatrix; glm::mat4 biasmatrix( 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); glm::mat4 depthbiasmvp = biasmatrix*depthmvp; /* shadow */ subshader->useshader(); glviewport(0,0,512,512); gluniformmatrix4fv(glgetuniformlocation(subshader->getshader(),"depthmvp"), 1, gl_false, &depthmvp[0][0]); glbindframebuffer(gl_framebuffer, shadows::framebuffer()); renderbuffer(objbuffer); glbindframebuffer(gl_framebuffer, 0); /* clear */ glclear(gl_color_buffer_bit | gl_depth_buffer_bit); /* shader */ objshader->useshader(); glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, shadows::shadowmap()); gluniform1f(glgetuniformlocation(objshader->getshader(),"shadowmap"),0); /* render scene */ gluniformmatrix4fv(glgetuniformlocation(objshader->getshader(),"depthbiasmvp"), 1, gl_false, &depthbiasmvp[0][0]); glviewport(0,0,640,480); gluniform1f(glgetuniformlocation(objshader->getshader(),"renderingpass"),1.0); renderbuffer(objbuffer);
the majority of code has been learned sites such (http://www.opengl-tutorial.org/) modifications based on (http://www.fabiensanglard.net/shadowmapping/index.php) , red book.
Comments
Post a Comment