Problems with creating a depth texture form a frame buffer object

Started by KeKsBoTer, March 17, 2016, 21:25:06

Previous topic - Next topic

KeKsBoTer

Hi,
I used my FrameBufferObject class for a long time and it all worked fine. But know I need the depth texture from the framebuffer and this doesnt work.
I just receive a black Image.
Here is the code of my FrameBufferObject class:
package com.dotcookie.engine.shaders;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_COMPONENT;
import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_RGBA8;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.GL_VIEWPORT_BIT;

import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL30;

import static org.lwjgl.opengl.GL30.*;

public class FrameBufferObject {

    public static boolean isSupported() {
	return GL.getCapabilities().OpenGL30 || GL.getCapabilities().GL_EXT_framebuffer_object;
    }

    int fbo;
    int depthbuffer;
    int fb_texture, depth_texture;
    private int width, height;


    public FrameBufferObject(int width, int height) {
	this.width = width;
	this.height = height;

	fb_texture = glGenTextures();
	glBindTexture(GL_TEXTURE_2D, fb_texture);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, width, height, 0, GL_RGBA,GL_UNSIGNED_BYTE,(java.nio.ByteBuffer) null);

	depth_texture = glGenTextures();
	glBindTexture(GL_TEXTURE_2D, depth_texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
	
	glBindTexture(GL_TEXTURE_2D, 0);

	// frame buffer fbo = glGenFramebuffers(); depthbuffer
	fbo =glGenFramebuffers();
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);

	depthbuffer = glGenRenderbuffers();
	glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer);

	glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer);

	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_texture, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, depth_texture, 0);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {
	    System.out.println("Frame buffer created sucessfully.");
	} else
	    System.out.println("An error occured creating the frame buffer.");
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }

    public int getWidth() {
	return width;
    }

    public int getHeight() {
	return height;
    }

    public int getID() {
	return this.fbo;
    }

    public int getTexture() {
	return this.fb_texture;
    }

    public int getDepthTexture() {
	return this.depth_texture;
    }

    public void bind() {
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
	glPushAttrib(GL_VIEWPORT_BIT);
	glViewport(0, 0, width, height);
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	GL11.glClearColor(120 / 255f, 167 / 255f, 223 / 255f, 1f);
    }

    public void unbind() {
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }

    public void release() {
	GL11.glDeleteTextures(depth_texture);
	GL11.glDeleteTextures(this.fb_texture);
	GL30.glDeleteFramebuffers(fbo);
	GL30.glDeleteRenderbuffers(depthbuffer);
    }
}


My rendering process is something like this:
public void render(float delta) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	GL11.glClearColor(120 / 255f, 167 / 255f, 223 / 255f, 1f);

	buffer.bind();
        //drawing a simple model with texture

	buffer.unbind();
	
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthbuffer.getRefractionDepthTexture());
	//drawing simple rectangle on screen
    }


For the color buffer this works perfectly fine, but the depth buffer texture is just black.

Any suggestions what I did wrong?

Thanks for helping :)