c++ - Deferred shading in OpenGL? -


i've got strange problem deferred shading implementation. render required information via mrt fbo, diffuse, position , normals in world space, looks this:

enter image description here

this done following setup tree textures:

diffuse = std::shared_ptr<bb::texture>(new bb::texture(gl_texture_2d)); // generates texture id diffuse->bind(); diffuse->texture2d(0, gl_rgb, width, height, 0, gl_rgb, gl_float, 0); // gltexture2d diffuse->parameterf(gl_texture_wrap_s, gl_clamp_to_edge); diffuse->parameterf(gl_texture_wrap_t, gl_clamp_to_edge); diffuse->parameterf(gl_texture_min_filter, gl_linear); diffuse->parameterf(gl_texture_mag_filter, gl_linear); diffuse->unbind(); texture2d(gl_color_attachment0+1, diffuse->getid(), 0); 

these used in drawing stage:

dsshader->bind(); dsshader->enablevertexattribarrays();  ds->diffuse->bind(gl_texture0); // "ds" fbo containing textures ds->position->bind(gl_texture0+1); ds->normal->bind(gl_texture0+2);  dsshader->senduniform("diffuse", 0); dsshader->senduniform("position", 1); dsshader->senduniform("normal", 2);  dsshader->senduniform("camera", camera3d->position.x, camera3d->position.y, camera3d->position.z);  dsout->indexbuffer->bind(); dsout->vertex2buffer->bind(); dsout->vertex2buffer->vertexattribpointer(dsshader->getattriblocation("vertex0"), 2, gl_float, false, 0, 0);  gldrawelements(gl_triangles, dsout->indexbuffer->size(), gl_unsigned_int, 0);  ds->diffuse->unbind(); ds->position->unbind(); ds->normal->unbind();  dsshader->disablevertexattribarrays(); dsshader->unbind(); 

with following shader (just necessary part, light source hardcoded):

struct dirlight{     vec3 direction;     vec4 diffuse, specular; };  uniform sampler2d diffuse; uniform sampler2d position; uniform sampler2d normal;  uniform vec3 camera;  dirlight light0 = dirlight(vec3(1, 1, 0), vec4(0.3), vec4(0.1));  in vec2 vertex;  void main(){     vec4 color = texture(diffuse, vertex)*0.5;     vec3 p = vec3(texture(position, vertex));     vec3 n = normalize(vec3(texture(normal, vertex)));      float ndotl = max(dot(n, normalize(light0.direction)), 0.0); // simple phong      if(ndotl > 0.0){         color += ndotl*light0.diffuse;     }      gl_fragcolor = color; } 

the wierd part is, if set light source direction negative values, lets say:

dirlight light0 = dirlight(vec3(-1, 0, 0), vec4(0.3), vec4(0.1)); 

the final result not shaded. looks right positive values (more or less). here picture of output:

enter image description here

and there problem normals, marked in red area.

ok solution andon m. coleman works, switched internal format gl_rgb8_snorm. :)


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -