Jump to content

  • Log In with Google      Sign In   
  • Create Account

Weton

Member Since 27 Mar 2013
Offline Last Active Jan 07 2016 03:51 PM

Topics I've Started

compare depth values in omnidirectional shadow mapping

03 January 2016 - 09:08 AM

Hi,

 

I try to do omnidirectional shadows, but I have problems comparing the depth values stored in the cube map with the frament positions.

 

To fill the cube map with the depth values I render my scene in each face with the differend view matrices (in world space) and use the hardware depth buffer. I checked the resultig cube map and it seems to have the right values stored in it.

Then I render my scene and look up the depth values in the cube map and try to compare them to the depth value of the projected fragment position.

The depth from the cube map is in the [0,1] range as expected, but my calcualted fragment deph (`depth_from_pos(lookup)`) returns values in the range [-1,0] and if I render them, they don't look quite right.

uniform samplerCube texShadow;

float depth_from_pos(vec3 pos) {
    // near and far used by the projection matrix for the cube map faces
    float n = 1.0;
    float f = 100.0;

    //// calculate the major depth axis -> max(abs(pos))
    vec3 abs_pos = abs(pos);
    float z = max(abs_pos.x, max(abs_pos.y, abs_pos.z));
    //// project pos to get the z and depth value
    // p_light = [[n,0,0,0], [0,n,0,0], [0,0,-(f+n)/(f-n),-2*f*n*(f-n)], [0,0,-1,0]] // right = top = 1
    // clip = p_light * vec(0,0,-z,1.0)
    // depth = (clip.z / clip.w) * 0.5 + 0.5 // divide by w and transform ndc [-1,1] to [0,1]
    float depth = -f*n/(z*(f-n)) + f/(f-n);
    return depth;
}

...

// transform light direction from view space to world space
// as the cube map faces are in world coordinates
vec3 lookup = mat3(view_inv) * vec3(lightDir);
float shadow_depth = texture(texShadow, normalize(lookup)).r
// get depth of pixel position from the light source 
float frag_shadow_depth = depth_from_pos(lookup);
float visibility = 1.0;
if (shadow_depth < frag_shadow_depth) {
    visibility = 0.0;
}

...

This is the rendered `frag_shadow_depth+1` of a scene with a plane with some cubes seen from above. The light is a bit over the plane in the center of the image.

Attached File  bad depth.png   36.58KB   0 downloads

I would have suspected something which is dark in the center and gets brighter to the edges without this 'x'.

 

I suspect the error in the depth_from_pos function, but I don't know what I'm doing wrong.

Does anybody have some ideas?

 

Thanks in advance smile.png


Wrong Normals in Deferred Renderer

13 October 2015 - 04:41 PM

Hi there,

I try to make a deferred renderer, but it seems like my normals don't face the direction I expcted.

I successfully created a framebuffer with 3 targets color, normal and depth/stencil to wich I can draw and read from in my shaders.
I calculate the view (looking at negative z, up is positive y) and projection matrices wich results in these values:
`v:{ m11: -1, m21: 0, m31: 0, m41: 0, m12: 0, m22: 1, m32: 0, m42: 0, m13: 0, m23: 0, m33: -1, m43: 0, m14: 0, m24: 0, m34: 0, m44: 1 }`
`p:{ m11: -1.2990379, m21: 0, m31: 0, m41: 0, m12: 0, m22: 1.7320507, m32: 0, m42: 0, m13: 0, m23: 0, m33: 1.020202, m43: 1, m14: 0, m24: 0, m34: -2.020202, m44: 0 }`
stored in column major order: m11,m21,m31,m41,m12,...

In the fist pass I render all my ojects to the framebuffer using the following shader:
    #version 420
    #extension GL_ARB_explicit_attrib_location : require
    #extension GL_ARB_explicit_uniform_location : enable

    //CAMERA
    layout(binding=1) uniform CameraBlock {
        mat4 v;
        mat4 p;
        mat4 p_v; //P*V
        //...
    };

    //...

    //MESH
    layout(binding=3, std140) uniform MeshBlock {
        mat4 m;
    };

//VERTEX_SHADER
    layout(location=0) in vec3 inPos;
    layout(location=1) in vec2 inTexCoord;
    layout(location=2) in vec3 inNormal;

    out vec2 texCoord;
    out vec3 normal;

    void main() {
        gl_Position =  p_v*m * vec4(inPos, 1.0);
        texCoord = inTexCoord;
        normal = normalize(transpose(inverse(mat3(v*m))) * inNormal.xyz);
    }

Using the view and perspective matrix from above the normals point in the wrong (?) direction. But when I only use 'm' instead of 'v*m' I get the result I expected.

Attached File  normals.png   8.62KB   4 downloads

Are my matrices wrong or didn't I calculate the normals correctly?
I am surprised, that the view matrix isn't an identity matrix.

Thank you in advance for your help smile.png

Cannot Clear/Draw to Framebuffer

12 August 2014 - 02:43 PM

Hi,

 

I'm trying to clear/draw to a framebuffer but I don't get the expected result. Instead of the clear color/expected object the framebuffer stays black with some white pixels. After running the application multiple times the white pixel pattern changes slightly (=> due to uninitialized memory?).

glGetError returns NO_ERROR and glCheckFramebufferStatus returns FRAMEBUFFER_COMPLETE.

Does anybody know what's going wrong?

 

This is the content of the framebuffer taken from apitrace after the glClear

Color0:

Attached File  Color0.png   20.35KB   4 downloads

Depth:

Attached File  Depth.png   22.1KB   4 downloads

Stencil:

Attached File  Stencil.png   51.77KB   4 downloads

 

This are the opengl calls duped by apitrace

glGenVertexArrays(n = 1, arrays = &1)
glGenFramebuffers(n = 1, framebuffers = &1)
glGenTextures(n = 1, textures = &1)
glBindTexture(target = GL_TEXTURE_2D, texture = 1)
glTexStorage2D(target = GL_TEXTURE_2D, levels = 1, internalformat = GL_RGBA16F, width = 640, height = 480)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_BASE_LEVEL, param = 0)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_MAX_LEVEL, param = 1)
glGenTextures(n = 1, textures = &2)
glBindTexture(target = GL_TEXTURE_2D, texture = 2)
glTexStorage2D(target = GL_TEXTURE_2D, levels = 1, internalformat = GL_RGBA16F, width = 640, height = 480)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_BASE_LEVEL, param = 0)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_MAX_LEVEL, param = 1)
glGenTextures(n = 1, textures = &3)
glBindTexture(target = GL_TEXTURE_2D, texture = 3)
glTexStorage2D(target = GL_TEXTURE_2D, levels = 1, internalformat = GL_DEPTH24_STENCIL8, width = 640, height = 480)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_BASE_LEVEL, param = 0)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_MAX_LEVEL, param = 1)
glGenTextures(n = 1, textures = &4)
glBindTexture(target = GL_TEXTURE_2D, texture = 4)
glTexStorage2D(target = GL_TEXTURE_2D, levels = 1, internalformat = GL_DEPTH24_STENCIL8, width = 640, height = 480)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_BASE_LEVEL, param = 0)
glTexParameteri(target = GL_TEXTURE_2D, pname = GL_TEXTURE_MAX_LEVEL, param = 1)
//compiling shders genBuffers... no framebuffer operations
glViewport(x = 0, y = 0, width = 640, height = 480)
glBindFramebuffer(target = GL_DRAW_FRAMEBUFFER, framebuffer = 1)
glFramebufferTexture(target = GL_DRAW_FRAMEBUFFER, attachment = GL_COLOR_ATTACHMENT0, texture = 1, level = 0)
glFramebufferTexture(target = GL_DRAW_FRAMEBUFFER, attachment = GL_COLOR_ATTACHMENT1, texture = 2, level = 0)
glFramebufferTexture(target = GL_DRAW_FRAMEBUFFER, attachment = GL_DEPTH_STENCIL_ATTACHMENT, texture = 3, level = 0)
glClearColor(red = 0.3, green = 0.3, blue = 0.3, alpha = 1)
glDepthMask(flag = GL_TRUE)
glClearDepth(depth = 1)
glClear(mask = GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)

Thank you for your help!


PARTNERS