统一变量报告未找到 LWJGL 3

huangapple go评论81阅读模式
英文:

Uniform variable reported not found LWJGL 3

问题

介绍

我正在使用Gradle在Java中创建一个2D OpenGL游戏,使用LWJGL 3依赖项。

目前,我已经实现了一个名为VertexArray的类,它处理模型数据(verticesindicestexturecoords),包括创建和处理VAOsVBOs

然后,我实现了着色器(Shaders),并可以成功加载像投影和变换矩阵这样的uniform变量。然后,我使用了org.l33tlabs.twl PNGDecoder添加了纹理,并将所有这些包装在一个名为Texture的类中,该类处理了绑定纹理单元、解绑和从OpenGL中删除纹理。在绑定函数之前,绑定函数还会调用shader.setUniform1f("textureSampler", 0);

代码

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

#version 400 core

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); // <-- 报错的行
    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);
}

这里是主Git存储库的链接,如果您克隆它,需要导入和更新子模块(我正在用Java AWT和OpenGL制作相同的游戏)。
这里是GL特定子模块的链接。

有明显的问题吗?如果您需要查看更多代码,请告诉我。

英文:

Introduction

I'm creating a 2D OpenGL game in Java using the LWJGL 3 dependencies in Gradle.

Currently I have implemented a class VertexArray, which handles model data (vertices, indices, texturecoords) - including creating and handling the VAOs and VBOs.

I them implemented Shaders and can successfully load uniforms such as mat4 for the projection and transformation matrix. I then added in textures using the org.l33tlabs.twl PNGDecoder and wrapped it all in a class Texture, that handles binding the texture unit, unbinding and deleting the texture from openGL. The bind function also calls shader.setUniform1f(&quot;textureSampler&quot;,0); before binding the texture.

Code

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
#version 400 core

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(&quot;textureSampler&quot;, 0); //&lt;-- Line that throws error
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
}

public void unbind() {
    glBindTexture(GL_TEXTURE_2D, 0);
}

Problem

When I run this, I get the error &quot;Could not find uniform variable &#39;textureSampler&#39;!&quot; from this method in the shader:

private Map&lt;String, Integer&gt; locationCache = new HashMap&lt;&gt;();

public int getUniform(String name) {
    if (locationCache.containsKey(name))
        return locationCache.get(name);
    int result = glGetUniformLocation(ID, name);
    if (result == 1) System.err.println(&quot;Could not find uniform variable &#39;&quot; + name + &quot;&#39;!&quot;);
    else locationCache.put(name, result);
    return result;
}

public void setUniform1f(String name, float value) {
    if (!enabled) start();
    glUniform1f(getUniform(name), value);
}

Here is a link to the main Git repo, if you clone you will need to import and updade submodules (im making the same game in java awt and openGL)
Here is a link to the GL specific submodule

Any obvious issues? If you need to see more code please let me know.

答案1

得分: 1

glGetUniformLocation如果名称不是指定程序中活动统一变量的名称,则返回-1:

if (result == -1) System.err.println("无法找到统一变量'" + name + "'!");
英文:

glGetUniformLocation returns -1 if the name is not the name of an active uniform variable in the specified program:

<s>if (result == 1) System.err.println(&quot;Could not find uniform variable &#39;&quot; + name + &quot;&#39;!&quot;);</s>

if (result == -1) System.err.println(&quot;Could not find uniform variable &#39;&quot; + name + &quot;&#39;!&quot;);

huangapple
  • 本文由 发表于 2020年10月3日 03:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64177160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定