令人沮丧的统一变量不会加载

时间:2013-10-18 20:20:04

标签: java glsl vertex-shader uniform

这也来自thebennybox的youtube 3D游戏引擎系列(如果你今天发现了我之前的帖子),我在这个addUniform方法中找到问题真的很麻烦。该位置总是变为0xFFFFFFFF或-1,抛出我定义的错误“无法找到统一变量”uniformFloat“”

我的顶点着色器:

#version 330

layout (location = 0) in vec3 position;

out vec4 colour;

uniform float uniformFloat;

void main()
{
    colour = vec4(clamp(position, 0.0, uniformFloat), 1.0);
    gl_Position = vec4(position, 1.0);
}

我的着色器类:

package com.base.engine;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL32.*;

import java.util.HashMap;

public class Shader {
    private int program;
    private HashMap<String, Integer> uniforms;

    public Shader() {
        program = glCreateProgram();
        uniforms = new HashMap<String, Integer>();

        if (program == 0) {
            System.err.println("Shader Creation failed: could not find valid memory location in constructor");
            System.exit(1);
        }
    }

    public void bind(){
        glUseProgram(program);
    }

    public void addUniform(String uniform){
        int uniformLocation = glGetUniformLocation(program, uniform);

        if (uniformLocation == 0xFFFFFFFF){
            System.err.println("Error: could not find uniform variable \"" + uniform + "\"");
            new Exception().printStackTrace();
            System.exit(1);
        }
        uniforms.put(uniform, uniformLocation);
    }

    public void addVertexShader(String text) {
        addProgram(text, GL_VERTEX_SHADER);
    }

    public void addGeometryShader(String text) {
        addProgram(text, GL_GEOMETRY_SHADER);
    }

    public void addFragmentShader(String text) {
        addProgram(text, GL_FRAGMENT_SHADER);
    }

    public void compileShader() {
        glLinkProgram(program);

        if (glGetProgrami(program, GL_LINK_STATUS) == 0) {
            System.err.println(glGetShaderInfoLog(program, 1024));
            System.exit(1);
        }

        glValidateProgram(program);
        if(glGetProgrami(program, GL_VALIDATE_STATUS) == 0){
            System.err.println(glGetShaderInfoLog(program, 1024));
            System.exit(1);
        }
    }

    private void addProgram(String text, int type) {
        int shader = glCreateShader(type);

        if (shader == 0) {
            System.err.println("Shader creation failed: could not find valid memory location when adding shader");
            System.exit(1);
        }

        glShaderSource(shader, text);
        glCompileShader(shader);

        if (glGetShaderi(shader, GL_COMPILE_STATUS) == 0) {
            System.err.println(glGetShaderInfoLog(shader, 1024));
            System.exit(1);
        }

        glAttachShader(program, shader);
    }

    public void setUniformi(String uniformName, int value){
        glUniform1i(uniforms.get(uniformName), value);
    }

    public void setUniformf(String uniformName, float value){
        glUniform1f(uniforms.get(uniformName), value);
    }

    public void setUniform(String uniformName, Vector3f value){
        glUniform3f(uniforms.get(uniformName), value.getX(), value.getY(), value.getZ());
    }

    public void setUniform(String uniformName, Matrix4f value){
        glUniformMatrix4(uniforms.get(uniformName), true, Util.createFlippedBuffer(value));
    }
}

和我实现的统一变量,Game类:

特别注意shader.addUniform行和update()方法。

package com.base.engine;

import org.lwjgl.input.Keyboard;

public class Game {
    private Mesh mesh;
    private Shader shader;

    public Game() {
        mesh = new Mesh();
        shader = new Shader();

        Vertex[] data = new Vertex[] {new Vertex(new Vector3f(-1, -1, 0)),
                                      new Vertex(new Vector3f(0, 1, 0)),
                                      new Vertex(new Vector3f(1, -1, 0))};
        mesh.addVertices(data);
        shader.addVertexShader(ResourceLoader.loadShader("basicVertex.vs"));
        shader.addFragmentShader(ResourceLoader.loadShader("basicFragment.fs"));
        shader.compileShader();

        shader.addUniform("uniformFloat");

    }

    public void input() {
        if (Input.getKeyDown(Keyboard.KEY_UP)) {
            System.out.println("We've just pressed up");
        }
        if (Input.getKeyUp(Keyboard.KEY_UP)) {
            System.out.println("We've just released up");
        }

        if (Input.getMouseDown(1)) {
            System.out.println("We've just pressed right-mouse at "
                    + Input.getMousePosition().toString());
        }
        if (Input.getMouseUp(1)) {
            System.out.println("We've just released right-mouse");
        }
    }

    float temp = 0.0f;

    public void update() {
        temp += Time.getDelta();
        shader.setUniformf("uniformFloat", (float)Math.sin(temp));
    }

    public void render() {
        shader.bind();
        mesh.draw();
    }
}
抱歉,任何人都必须阅读这段代码,但我现在已经盯着这些了一段时间。我已经读过,当你不使用统一变量时会发生这种情况,但是我的着色器正在使用它,对吗?

1 个答案:

答案 0 :(得分:4)

你的制服不会以任何方式影响着色器的效果,因此它很可能被完全移除。只有活跃的制服才有位置。一切都在发挥作用。