gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:28:55 24 4
gpt4 key购买 nike

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

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

最佳答案

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

关于java - 令人沮丧的统一变量不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19458379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com