PDA

View Full Version : Depth texture issues - All white texture



Illiander
11-09-2014, 10:39 AM
I'm *very* new to GLSL coding, so I might be making an elementary mistake here, but I'm using fragment shaders to get a depth map (actually several depth maps, for two-step depth peeling and shadow mapping), but the shadow maps are coming out as either pure max depth, or pure min depth.

Here's where I set up the framebuffers:


glGenFramebuffers(1, &framebuffers);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffers);


glGenTextures(DEPTH_PEEL_LAYERS, &(rendered_textures[0]));
glGenRenderbuffers(DEPTH_PEEL_LAYERS, &(depthbuffers[0]));

for (int i = 0; i<DEPTH_PEEL_LAYERS; ++i)
{
glBindTexture(GL_TEXTURE_2D, rendered_textures[i]);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, 600, 600, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, rendered_textures[i], 0);

glBindRenderbuffer(GL_RENDERBUFFER, depthbuffers[i]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32F, 600, 600);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffers[i]);
}

// Set the list of draw buffers.
GLenum DrawBuffers[DEPTH_PEEL_LAYERS] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
glDrawBuffers(DEPTH_PEEL_LAYERS, DrawBuffers);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glGenFramebuffers(DEPTH_PEEL_LAYERS, &(depthframebuffers[0]));
glGenTextures(DEPTH_PEEL_LAYERS, depth_textures);

for (int i = 0; i<DEPTH_PEEL_LAYERS; ++i)
{
glBindFramebuffer(GL_FRAMEBUFFER, depthframebuffers[i]);

glBindTexture(GL_TEXTURE_2D, depth_textures[i]);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT32F, 600, 600, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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);

glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_textures[i], 0);

GLenum DrawBuffers[1] = {GL_NONE};
glDrawBuffers(1, DrawBuffers);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

I'm getting the issue with *extremely* simple shaders (vertex is just a passthrough, fragment just writes the depth to the first colour attachment's xyz), so I'm guessing it's an issue in setup, since "gl_FragCoord.z" in the fragment shader always seems to be "1.0"

DragonForce99
11-10-2014, 02:58 AM
you may cannot see the deffrent
add number of objects (near, far, middle, ....)
make your projection's far plane 100 or 50
and see the result if you got the smae result try to calculate dipth

in Vertex Shader

varying vec3 VertexPosition3D;
void main()
{
VertexPosition3D = (Transform*gl_Vertex).xyz;
...
...
...
}

in fragment shader

varying vec3 VertexPosition3D;
uniform vec3 CameraPosition;
void main()
{
float My_Depth = distance(CameraPosition,VertexPosition3D) /farPlane; //try to make far plane 100 or 50
gl_FragColor = vec4(My_Depth,My_Depth,My_Depth,1);
}

Illiander
11-10-2014, 11:56 PM
My far plane is only 10 normally.

I added in the camera position, and the only thing that even approaches correct is displaying the distance from MVP*camera to a fixed point in screen space, and that's only because the camera's X and Y are changing (because of the model matrix).

Could it be my MVP matrix generation is off?

glm::mat4 Camera::GetCameraMatrix(GridPoint _loc)
{
glm::vec3 loc;
loc[0] = double(location.X()-_loc.X()) + local_location.GetX();
loc[1] = double(location.Y()-_loc.Y()) + local_location.GetY();
loc[2] = double(location.Z()-_loc.Z()) + local_location.GetZ();
//loc[3] = 1;

glm::vec3 face;
face[0] = facing.GetX();
face[1] = facing.GetY();
face[2] = facing.GetZ();
//face[3] = 1;

glm::vec3 up(0,1,0);
vec real_up = vec(face).cross(vec(0,1,0).cross(face));

glm::mat4 projection = glm::perspective(float(FoV), float(aspect_ratio), float(near_dist), float(far_dist));
glm::mat4 view = glm::lookAt(loc, loc+face, real_up.vec3());

return projection*view;
}

Illiander
11-17-2014, 02:56 AM
And with some farther investigation, it seems like my depth coordinate is ending up in the 'w' component of the location, rather than the 'z' component.

Weird, but I can work with that.

It would still be nice to figure out where I'm going wrong with my pipeline though.