Question

I made the first step of deferred rendering, the fbo wit color, normal, position and depth texture. But when Im do the lighting the positions are moving and rotating with me. First pass vertex:

void main(){
    gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;
    vec4 positiontotexture = gl_ModelViewProjectionMartix*gl_Vertex;
}

So how can I get the original position?

Was it helpful?

Solution

I assume that positiontotexture will go to the fragment shader and then be saved in the texture.

When doing first pass it would be good to have View position, not ViewProjection. You can use:

vec4 positiontotexture = gl_ModelViewMartix*gl_Vertex;

in the second pass you can then use:

vec3 position = texture(texPos, vVaryingTexCoord0.xy).rgb;

The previous way is quite simple (yet it is good when learning) but require lot of memory (you have to store 3 element vector). Better way is to recreate view-position from screen position/depth: link to article. Such techniques are used in modern engines. (Thanks andon-m-coleman for the suggestion)

Here is my repo with some basic deferred: https://github.com/fenbf/efg_deferred/tree/master/code/shaders

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top