0
\$\begingroup\$

Recently, I am making a 3D game using LWJGL, and finally got framebuffer textures to work! The problem is, though, is that I think the default shader that is used for the cube doesn't work or the postprocess shader overrides it? I'm not sure but whenever I edit or change the cube shader, nothing happens and it just stays a pixelated texcoordinate (created already).

Heres an image:

enter image description here

When there should be a texture on it. I tried binding it before its drawn, when its drawn, tried setting the sampler2D uniform, etc, and it still won't work!

Heres code for my Main (Specifically the parts that I think are causing the problem):

while (!Display.isCloseRequested()) {
            
            rot += 0.05f;
            
            // shader.start();
            fbo.bindFBO();
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glClearColor(1.0f, 1.0f, 0.2f, 1.0f);
            GL11.glPushMatrix();
                GL11.glRotatef(rot += 0.01f, 1.0f, 1.0f, 1.0f);
                shader.start();
                testtex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/testx16.png"));
                testtex.bind();
                // GL20.glUniform1i(GL20.glGetUniformLocation(shader.getProgID(), "tex"), testtex.getTextureID());
                test.draw();
                shader.halt();
            GL11.glPopMatrix();
            fbo.unbindFBO();
            // shader.halt();
            
            // renderPP();
            // RENDER FBO QUAD
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            postprocess.start();
            
            GL20.glUniform1f(GL20.glGetUniformLocation(postprocess.getProgID(), "u_Time"), u_Time += 0.005f);
            
            if (fac < 0) {
                
                fac = 0;
                t = 0;
                
            }
            
            if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                
                // GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), fac -= 0.25f);
                GL20.glUniform1f(GL20.glGetUniformLocation(postprocess.getProgID(), "t"), fac -= 0.8f);
                
            }
            
            if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                
                // GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), fac += 0.25f);
                GL20.glUniform1f(GL20.glGetUniformLocation(postprocess.getProgID(), "t"), fac += 0.8f);
                
            }
            
            if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
                
                fac = 1024;
                // GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), 1024);
                GL20.glUniform1f(GL20.glGetUniformLocation(postprocess.getProgID(), "t"), 640);
                
            }
            
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getColorTexture());
            renderQuad();
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
            postprocess.halt();
            GL11.glEnable(GL11.GL_DEPTH_TEST);
            
            // UPDATE DISPLAY
            Display.update();
            
        }
        
        fbo.wash();
        postprocess.wash();
        shader.wash();
        test.wash();
        Display.destroy();
        System.exit(0);
        
    }

    public static void main(String[] args) throws LWJGLException, FileNotFoundException, IOException {
        
        BlockGame bg = new BlockGame();
        bg.window("Game", 640, 480);
        
    }
    
    /* public void renderPP() {
        
        // postprocess.start();
        // GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getColorTexture());
        
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        
        postprocess.start();
        
        GL20.glUniform1i(GL20.glGetUniformLocation(postprocess.getProgID(), "renderTex"), fbo.getColorTexture());
        // GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getColorTexture());
        
        renderQuad();
        postprocess.halt();
        
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        
    } */
    
    public void renderQuad() {
        
        // fbo.bindFBO();
        GL11.glBegin(GL11.GL_TRIANGLES);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(-1, 1, 0);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, 0);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, 0);
        
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, 0);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, 0);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(1, -1, 0);
        
        GL11.glEnd();
        // fbo.unbindFBO();
        
    }

My FBO Class:

package me.pale.postprocess;

import java.nio.ByteBuffer;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL30;

public class FBO {
    
    public static final int NONE = 0;
    public static final int DEPTH_TEXTURE = 1;
    public static final int DEPTH_RENDER_BUFFER = 2;
    
    public int multiplier = 1;
    
    public int i;
    
    private int frameBuffer;
    private int colorTexture;
    private int depthTexture;
    
    private int colorBuffer;
    private int depthBuffer;
    
    public FBO(int DBT) {
        
        initFBO(DBT);
        
    }
    
    public void wash() {
        
        GL30.glDeleteFramebuffers(frameBuffer);
        GL11.glDeleteTextures(colorTexture);
        GL11.glDeleteTextures(depthTexture);
        GL30.glDeleteRenderbuffers(depthBuffer);
        GL30.glDeleteRenderbuffers(colorBuffer);
        
    }
    
    private int createFBO() {
        
        int fbo = GL30.glGenFramebuffers();
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
        GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
        
        return fbo;
        
    }
    
    private int createTexAttach() {
        
        int colt = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, colt);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, 640 * multiplier, 480 * multiplier, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (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);
        GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colt, 0);
        // GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        // GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        
        i = GL11.glGetError();
        
        if (i != 0) {
            
            System.out.println("ERR MAKING TEXTURE " + i);
            
        }
        
        return colt;
        
    }
    
    public int createDTA() {
        
        int st = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, st);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT32, 640 * multiplier, 480 * multiplier, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, st, 0);
        
        return st;
        
    }
    
    public void bindFBO() {
        
        // GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
        GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
        // GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        if (GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) != GL30.GL_FRAMEBUFFER_COMPLETE) {
            
            int i = GL11.glGetError();
            System.out.println("ERR: FAILED TO BIND FBO: " + i);
            System.exit(-1);
            
        }
        
    }
    
    public void unbindFBO() {
        
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
        // GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
        
    }
    
    public void bindandreadFBO() {
        
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, frameBuffer);
        GL11.glReadBuffer(GL30.GL_COLOR_ATTACHMENT0);
        
    }
    
    public int createDBA() {
        
        int db = GL30.glGenRenderbuffers();
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, db);
        GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, 640 * multiplier, 480 * multiplier);
        GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, db);
        
        return db;
        
    }
    
    public void initFBO(int type) {
        
        frameBuffer = createFBO();
        colorTexture = createTexAttach();
        if (type == DEPTH_RENDER_BUFFER) {
            
            depthBuffer = createDBA();
            
        } else if (type == DEPTH_TEXTURE) {
            
            depthTexture = createDTA();
            
        }
        unbindFBO();
        
    }

    public int getFrameBuffer() {
        return frameBuffer;
    }

    public int getColorTexture() {
        return colorTexture;
    }

    public int getDepthTexture() {
        return depthTexture;
    }

    public int getColorBuffer() {
        return colorBuffer;
    }

    public int getDepthBuffer() {
        return depthBuffer;
    }
    
    public void printAllThings() {
        
        System.out.println("FBO ID: " + frameBuffer + ", Color Buffer ID: " + colorBuffer + ", Depth Buffer ID: " + depthBuffer + ", Color Texture ID: " + colorTexture + ", Depth Texture ID: " + depthTexture);
        
    }
    
}

My Voxel Class:

package me.pale.model;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import me.pale.shader.Shader;

public class Voxel {
    
    Texture test;
    
    Shader shader;
    
    int size = 1;
    
    public Voxel() {
        
    }
    
    public void draw() throws FileNotFoundException, IOException {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        // ## THE MANY ATTEMPTS ## 
        // shader = new Shader();
        // shader.start();
        // GL13.glActiveTexture(GL13.GL_TEXTURE0);

        // ## WHAT I NORMALLY DID THAT DOESNT WORK NOW \/
        test = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/testx16.png"));
        test.bind();
        // ## WHAT I NORMALLY DID THAT DOESNT WORK NOW /\

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        // GL11.glColor3f(0.5f, 0.5f, 0.5f);
        GL11.glBegin(GL11.GL_TRIANGLES);
        // Front Quad
        GL11.glNormal3f(0, 0, 1);
        // GL11.glColor3f(0.0f, 0.0f, 0.0f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(-1, 1, 1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, 1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, 1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, 1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, 1);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(1, -1, 1);
        
        // Back Quad
        GL11.glNormal3f(0, 0, 1);
        GL11.glColor3f(0.1f, 0.1f, 0.1f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(-1, 1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, -1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, -1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, -1);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(1, -1, -1);
        
        // Left Quad
        GL11.glNormal3f(1, 0, 0);
        GL11.glColor3f(0.2f, 0.2f, 0.2f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(-1, 1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(-1, 1, 1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, -1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(-1, 1, 1);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(-1, -1, 1);
        
        // Right Quad
        GL11.glNormal3f(1, 0, 0);
        GL11.glColor3f(0.3f, 0.3f, 0.3f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(1, 1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, 1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(1, -1, -1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(1, -1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, 1);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(1, -1, 1);
        
        // Top Quad
        GL11.glNormal3f(0, 1, 0);
        GL11.glColor3f(0.4f, 0.4f, 0.4f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(-1, 1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, -1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, 1, 1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, 1, 1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, 1, -1);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(1, 1, 1);
        
        // Bottom Quad
        GL11.glNormal3f(0, 1, 0);
        GL11.glColor3f(0.5f, 0.5f, 0.5f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(-1, -1, -1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, -1, -1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, 1);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(-1, -1, 1);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(1, -1, -1);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(1, -1, 1);
        
        GL11.glEnd();
        // shader.halt();
        
    }
    
    public void wash() {
        
        shader.wash();
        
    }
    
    public void debugDraw() throws IOException {
        
    }

}

And my Vertex and Fragment shaders for both the FBO and the VOXEL:

Voxel:

// VERTEX

#version 120

vec4 a = gl_Vertex;
varying vec4 verpos;
varying vec4 normals;
varying vec4 colors;

out vec4 Normal;

attribute vec2 in_TC;
out vec2 f_TC;

varying vec2 vTexCoord;

void main(void) 
{
    // gl_Position = ;
    
    vTexCoord = gl_MultiTexCoord0.xy;
    
    // Normal = gl_Normal;

    gl_Position = gl_ModelViewProjectionMatrix * a;
    verpos = gl_Vertex;
    normals = vec4(gl_Normal, 1.0);
    colors = gl_Color;
    f_TC = in_TC;
}

// FRAGMENT

#version 120

uniform float u_time;
uniform vec2 u_resolution;

uniform sampler2D tex;

in vec4 Normal;

varying vec2 vTexCoord;

varying vec4 verpos;

varying vec4 normals;

varying vec4 colors;

in vec2 f_TC;

void main(void)
{
    vec2 st = gl_FragCoord.xy/u_resolution;
    
    // gl_FragColor = texture2D(tex, vTexCoord);
    
    // gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

}

FBO:

// VERTEX

#version 120

varying vec2 v_TexCoord;

varying vec4 v_Color;

varying vec4 v_Normal; 

varying vec4 v_Vertex;

void main(void) {

    v_Vertex = gl_Vertex;
    
    v_TexCoord = gl_MultiTexCoord0.xy;
    
    v_Normal = vec4(gl_Normal, 1.0);
    
    v_Color = gl_Color;

    gl_Position = gl_Vertex;
    
    // gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}

// FRAGMENT

#version 120

uniform sampler2D renderTex;

// layout(location = 0) out vec3 color;

varying vec4 v_Vertex;

uniform float u_Time = 0;

varying vec2 v_TexCoord;

varying vec4 v_Normal;

varying vec4 v_Color;

uniform float t = 640;

uniform int texture;

vec2 pixelize() {

    vec2 a = floor(v_TexCoord * t);
    vec2 b = a / t;
    
    return b.rg;

}

void main(void) {
    
    vec4 tex = texture2D(renderTex, pixelize());

    gl_FragColor = tex.rgba; // + sin(u_Time) * 0.6;

}

Thank you for anyone's help!

PS: If I dont start the Voxel shader, the texture appears, but is shaded weird and also I need to use shaders for specific things I want to do in the future.

\$\endgroup\$

You must log in to answer this question.

Browse other questions tagged .