0
\$\begingroup\$

Recently, I am making a 3D game. I want to do special post processing effects, like pixelization. I used this to load do everything with FBOS, but it's kindof weird...

In some code for the initialization of the FBO, it stops, and errors out with the error code 1280, which aparently means GL_INVALID_ENUM, and it all started when I put in this code in my FBO class:

GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, renderTex, 0, GL11.GL_TEXTURE_2D);

It errors out, then closes (by if statement). If I don't close it, it renders black, like this:

enter image description here

If I change glFramebufferTexture2D to glFramebufferTexture, it just turns black and no error outputs.

I seriously have no idea how.

maybe it's by the classes that's causing this, so here they are:

Main Class:

package me.pale.main;

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

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.Texture;

import me.pale.model.Voxel;
import me.pale.postprocess.FBO;
import me.pale.postprocess.PostProcessQuad;
import me.pale.shader.Shader;

public class BlockGame {
    
    float fac = 16;
    
    float t = 16;
    
    Texture testtex;
    
    float rot;
    
    Shader shader;
    Shader postprocess;
    
    PostProcessQuad ppq = new PostProcessQuad();
    
    Voxel test = new Voxel();
    
    public void window(String name, int width, int height) throws LWJGLException, FileNotFoundException, IOException {
        
        Display.setDisplayMode(new DisplayMode(width, height));
        Display.create();
        
        FBO fbo = new FBO();
        
        shader = new Shader("res/fragment.glsl", "res/vertex.glsl");
        postprocess = new Shader("res/ppfrag.glsl", "res/ppvert.glsl");
        
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glClearColor(0, 0, 0, 0);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GLU.gluPerspective(90.0f, 1.333f, 0.1f, 650.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthMask(true);
        
        GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), fac);
        
        GL11.glTranslated(0.0f, 0.0f, -5.0f);
        
        
        while (!Display.isCloseRequested()) {
            
            if (fac < 0) {
                
                fac = 0;
                t = 0;
                
            }
            
            if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                
                GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), fac -= 0.025f);
                GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "t"), t -= 0.025f);
                
            }
            
            if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                
                GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), fac += 0.025f);
                GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "t"), t += 0.025f);
                
            }
            
            if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
                
                GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "fac"), 16);
                GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "t"), 16);
                
            }
            
            rot += 0.05f;
            
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  
            GL11.glClearColor(1.0f, 1.0f, 0.2f, 1.0f);
            //GL11.glBindTexture(GL11.GL_TEXTURE_2D, testtex.getTextureID());
            
            
            // shader.start();
            GL11.glPushMatrix();
            GL11.glTranslatef(-4.0f, 0.0f, 0.0f);
            GL11.glBegin(GL11.GL_QUADS);
            GL11.glBindTexture(0, fbo.getRenderTex());
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex3f(-1, 1, -1);
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex3f(1, 1, -1);
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex3f(1, -1, -1);
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex3f(-1, -1, -1);
            GL11.glEnd();
            GL11.glPopMatrix();
            
            fbo.bindFBOS();
            postprocess.start();
            GL11.glPushMatrix();
            GL11.glTranslatef(-4.0f, 0.0f, 0.0f);
            ppq.draw();
            GL11.glPopMatrix();
            fbo.unbind();
            
            postprocess.halt();
            
            shader.start();
            
            GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "f"), rot/10);
            
            GL11.glPushMatrix();
            
                GL11.glRotatef(rot, 1, 1, 1);
            
                test.draw();
                
            
            GL11.glPopMatrix();
            
            shader.halt();
            // shader.halt();
            
            Display.update();
            
        }
        
        fbo.wash();
        postprocess.wash();
        shader.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);
        
    }
    
}

FBO Class:

package me.pale.postprocess;

import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL32;

public class FBO {
    
    int defaultFBO;
    int defaultTEX;
    int defaultDEPTH;
    
    public FBO() {
        
        init();
        
    }

    public int createFBO() {
        
        int fbo = GL30.glGenFramebuffers();
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
        // GL11.glViewport(0, 0, 640, 480);
        GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
        
        return fbo;
        
    }
    
    public int createRenderTexture() {
        
        int renderTex = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, renderTex);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 640, 480, 0, GL11.GL_RGB, 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);
        
        GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, renderTex, 0, GL11.GL_TEXTURE_2D);
        
        int drawBuffer = GL30.GL_COLOR_ATTACHMENT0;
        
        GL20.glDrawBuffers(drawBuffer);
        
        if (GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) != GL30.GL_FRAMEBUFFER_COMPLETE) {
            
            System.out.println("ERR: error occurred making FBO " + GL11.glGetError());
            System.exit(-1);
            
        }
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        
        return renderTex;
        
    }
    
    public void bind(int fbo) {
        
        // GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
        GL11.glViewport(0, 0, 640, 480);
        
    }
    
    public void unbind() {
        
        // GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
        GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
        
    }
    
    public void render() {
        
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
        GL11.glViewport(0, 0, 640, 480);
        
    }
    
    private int createDepthBuffer() {
        
        int depthRenderBuffer = GL30.glGenRenderbuffers();
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthRenderBuffer);
        GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, 640, 480);
        GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthRenderBuffer);
        
        return depthRenderBuffer;
        
    }
    
    public void bindFBOS() {
        
        bind(defaultFBO);
        
    }
    
    public void init() {
        
        defaultFBO = createFBO();
        defaultTEX = createRenderTexture();
        defaultDEPTH = createDepthBuffer();
        unbind();
        
    }
    
    public void wash() {
        
        GL30.glDeleteFramebuffers(defaultFBO);
        GL11.glDeleteTextures(defaultTEX);
        
    }

    public int getRenderTex() {
        return defaultTEX;
    }

    public int getFbo() {
        return defaultFBO;
    }
}

Be in mind that I used this link and ThinMatrix's water tutorial, episode 2.

Any help appreciated!

\$\endgroup\$
1
  • \$\begingroup\$ Ok, so I've looked at the wiki for glframebuffertexture2D, and updated the parameters, but now its all black \$\endgroup\$
    – Pale_Gray
    Commented May 21, 2021 at 11:54

1 Answer 1

1
\$\begingroup\$

Here, you used the wrong order of parameters:

glFramebufferTexture2D(
    GL30.GL_FRAMEBUFFER, 
    GL30.GL_COLOR_ATTACHMENT0, 
    renderTex, 
    0, 
    GL11.GL_TEXTURE_2D);

As specified by the documentation, it should have been:

glFramebufferTexture2D(
    GL30.GL_FRAMEBUFFER, 
    GL30.GL_COLOR_ATTACHMENT0, 
    GL11.GL_TEXTURE_2D, 
    renderTex, 
    0);

Also in Java, I think you can use something like this:

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

which allows you to write

public void render() {
    
    glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
    glViewport(0, 0, 640, 480);
    
}

See! No need to write GL11 or GL30.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .