介绍
我正在使用 Gradle 中的 LWJGL 3 依赖项在 Java 中创建 2D OpenGL 游戏。
目前我已经实现了一个类 VertexArray,它处理模型数据( vertices
, indices
, texturecoords
) - 包括创建和处理 VAOs
和 VBOs
.
我他们实现了着色器,并且可以成功加载制服,例如 mat4
为投影和变换矩阵。然后我使用 org.l33tlabs.twl PNGDecoder
添加纹理并将其全部封装在一个类中 Texture
,它处理绑定(bind)纹理单元、解除绑定(bind)和从 openGL 中删除纹理。绑定(bind)函数也调用 shader.setUniform1f("textureSampler",0);
在绑定(bind)纹理之前。
代码entityVertex.glsl
#version 400 core
in vec4 position;
in vec2 textureCoords;
out vec2 passTextureCoords;
uniform mat4 projectionMatrix;
uniform mat4 transformationMatrix;
void main()
{
gl_Position = projectionMatrix * transformationMatrix * position;
passTextureCoords = textureCoords;
}
entityFragment.glsl
#版本 400 核心in vec2 passTextureCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main()
{
vec4 colour = texture(textureSampler,passTextureCoords);
out_Color = colour;
}
vertexArray render
public void bind() {
glBindVertexArray(vao);
glEnableVertexAttribArray(VERTEX_ATTRIB);
glEnableVertexAttribArray(TCOORD_ATTRIB);
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}
public void unbind() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(VERTEX_ATTRIB);
glDisableVertexAttribArray(TCOORD_ATTRIB);
glBindVertexArray(0);
}
public void draw() {
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
}
public void render(Shader shader, Texture texture) {
texture.bind(shader);
bind();
draw();
texture.unbind();
unbind();
}
texture
public void bind(Shader shader) {
shader.setUniform1f("textureSampler", 0); //<-- Line that throws error
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
问题当我运行它时,我收到错误
"Could not find uniform variable 'textureSampler'!"
从着色器中的这个方法:private Map<String, Integer> locationCache = new HashMap<>();
public int getUniform(String name) {
if (locationCache.containsKey(name))
return locationCache.get(name);
int result = glGetUniformLocation(ID, name);
if (result == 1) System.err.println("Could not find uniform variable '" + name + "'!");
else locationCache.put(name, result);
return result;
}
public void setUniform1f(String name, float value) {
if (!enabled) start();
glUniform1f(getUniform(name), value);
}
Here是一个指向主 Git 存储库的链接,如果你克隆你将需要导入和更新子模块(我在 java awt 和 openGL 中制作相同的游戏)Here是指向 GL 特定子模块的链接
有什么明显的问题吗?如果您需要查看更多代码,请告诉我。
最佳答案
glGetUniformLocation
如果名称不是指定程序中 Activity 统一变量的名称,则返回 -1:if (result == 1) System.err.println("Could not find uniform variable '" + name + "'!");
if (result == -1) System.err.println("Could not find uniform variable '" + name + "'!");
关于java - 统一变量报告未找到 LWJGL 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64177160/