-1
\$\begingroup\$

Recently, I've been making a 3D game and I have been having trouble with making multiple shaders. I've researched this for a bit, and told that I had to use gluseshader or shader.bind() in my code, but the post processing shader overrides the cube shader.

Here is my code

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.GL13;
import org.lwjgl.opengl.GL20;
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.PostProcessShader;
import me.pale.shader.Shader;
import me.pale.texture.Tex;

public class BlockGame {
    
    float fac = 640;
    
    float t = 640;
    
    float u_Time = 0;
    
    Texture testtex;
    
    PostProcessShader postprocess;
    
    FBO fbo;
    
    float rot;
    
    PostProcessQuad ppq = new PostProcessQuad();
    
    Voxel test = new Voxel();
    
    // FBO fbo;
    
    public void window(String name, int width, int height) throws LWJGLException, FileNotFoundException, IOException {
        
        Display.setDisplayMode(new DisplayMode(width, height));
        Display.create();
        
        fbo = new FBO(FBO.DEPTH_RENDER_BUFFER);
        postprocess = new PostProcessShader();
        Shader shader = new Shader();
        Tex testtex = new Tex();
        
        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);
        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);
        
        // fbo.printAllThings();
        testtex.loadTexture("res/test.png");
        System.out.println(testtex.getTextureID());
        System.out.println(fbo.getColorTexture());

        System.out.println("PROGRAM ID FOR POST PROCESSING: " + postprocess.getProgID());
        System.out.println("PROGRAM ID FOR DEFAULT SHADER: " + shader.getProgID());
        
        float x = 0;
        
        while (!Display.isCloseRequested()) {

            
            rot += 0.05f;
            // shader.start();
            shader.start(shader.getProgID());
            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();
                // shader.start(shader.getProgID());
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
                // GL13.glActiveTexture(GL13.GL_TEXTURE0 + 1);
                GL13.glActiveTexture(GL13.GL_TEXTURE0);
                GL11.glBindTexture(GL11.GL_TEXTURE_2D, testtex.getTextureID());
                // GL20.glUniform1i(GL20.glGetUniformLocation(shader.getProgID(), "tex"), 1);
                GL11.glRotatef(rot += 0.01f, 1.0f, 1.0f, 1.0f);
                test.draw();
                // GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
                // shader.halt();
            GL11.glPopMatrix();
            fbo.unbindFBO();
            
            // renderPP();
            // RENDER FBO QUAD
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            /* postprocess.start(postprocess.getProgID());
            
            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);
                
            }
            
            // GL20.glUniform1i(GL20.glGetUniformLocation(postprocess.getProgID(), "renderTex"), fbo.getColorTexture());
            // GL13.glActiveTexture(GL13.GL_TEXTURE0);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getColorTexture());
            renderQuad();
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
            postprocess.halt(); */
            
            postProcess();
            GL11.glEnable(GL11.GL_DEPTH_TEST);
            
            // UPDATE DISPLAY
            Display.update();
            
        }
        
        fbo.wash();
        postprocess.wash();
        shader.wash();
        testtex.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 postProcess() {
        
        postprocess.start(postprocess.getProgID());
        
        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);
            
        }
        
        // GL20.glUniform1i(GL20.glGetUniformLocation(postprocess.getProgID(), "renderTex"), fbo.getColorTexture());
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.getColorTexture());
        renderQuad();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        postprocess.halt();
        
    }
    
    /* 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();
        
    }
    
}

The main problem I am having is using multiple shaders. I use one shader for the post processing (render FBO texture to quad), and a cube shader (the shader that textures the cube), but after I got FBOs working, shaders have been a hassle. Like the cube shader isn't working and the post-process shader is overriding it or overpowering it. It is also using a pixelized texcoord, one that I am not using at all in either shader files

This is currently what it is doing:

enter image description here

But its supposed to render a texture on it like this:

enter image description here

This is a workable solution, but the solution to this problem to make the texture render was to not start the cube shader shader.start(shader.getProgID());. I need shaders if I want to do some special effects per voxel group, and multiple shaders for other things in the future.

the cube shader, shader.start(shader.getProgID()); is supposed to render a texture on the cube that is rendered on the fbo on the screen. The post processing shader, postprocess.start(postprocess.getProgID()); is supposed to render the fbo to a texture, and put it on the quad, then renders the quad wrapped onto the screen. When I add shader.start(shader.getProgID());, it does not render the texture, but renders a pixelized texcoord, one that I am only using in the postprocess shader that is able to pixelize the fbo. When I specify no gl_FragColor in the cube shader (shader.start(shader.getProgID());), it still renders the pixelized texcoords. When I specify a gl_FragColor, it renders the pixelized texcoords, even if the gl_FragColor has a vec4 assigned to it, like vec4(1.0, 0.0, 0.0, 1.0);

It turns out that during debugging, it turns out that both the cube texture and the fbo texture have the same ID... How do I fix this?

Thanks for your help!

\$\endgroup\$
1
  • \$\begingroup\$ It's not clear from reading this post what problem you need help solving. Is it the same one as your previous post? If so, I'd recommend editing that one to clarify it, rather than deleting and re-posting a more vague version. \$\endgroup\$
    – DMGregory
    May 30 at 1:38
0
\$\begingroup\$

After having an enlightenment, there was an error in the shader code that the shader program wasn't catching because it was a vec2 method, but weird. Anyways, I got it working there was an error in shader code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.