2

这也是来自 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();
    }
}

抱歉,阅读此代码的任何人都必须涉水,但我已经盯着这些代码看了一段时间了。我已经读到,当您不使用统一变量时可能会发生这种情况,但我的着色器正在使用它,对吗?

4

1 回答 1

5

您的制服不会以任何方式影响着色器的结果,因此很可能会被完全移除。只有现役制服才有位置。一切正常。

于 2013-10-18T23:32:24.917 回答