I can't get my shadow system work properly. I use the following code to generate my FBO:
int frameBuffer = GL30.glGenFramebuffers();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
GL11.glDrawBuffer(GL11.GL_NONE);
GL11.glReadBuffer(GL11.GL_NONE);
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT16, WIDTH, HEIGHT, 0,
GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
My entities are rendering fine in my default renderer (output vec4). My shadow shader code is straight forward:
#version 430
layout(location = 0) out float fragmentdepth;
void main(void){
fragmentdepth = gl_FragCoord.z;
}
I tried to output a vec4(1.0) without using any FBOs and it all worked fine so my calculations are correct and my shader codes should work as well.
My draw call looks like this:
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
renderShadows();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
renderAllItems();
Display.sync(FPS_CAP);
Display.update();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0.2f,0.1f,0.1f , 1f);
when I call "renderAllItems();" I am also rendering a quad at the top right corner which should display my depth texture. But all I see is a red quad. Shouldn't it be black/white and show something?
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
This attaches a texture to the depth attachment of a framebuffer object.
layout(location = 0) out float fragmentdepth;
That is a user-defined output from a fragment shader. User-defined outputs are mapped to fragment colors, which get written to color attachments of FBOs, not to depth attachments.
The fragment's depth is what gets written to depth attachments. And in an FS, that's the built-in gl_FragDepth
output. However, there's no reason for you to write to it. If you don't write to it, then it will automatically be filled in by gl_FragCoord.z
.
Indeed, if all you want is to render depth, you shouldn't even have a fragment shader.