问题描述
简介
我正在使用Gradle中的LWJGL 3依赖关系在Java中创建2D OpenGL游戏。
当前,我已经实现了一个VertexArray类,该类处理模型数据(vertices
,indices
,texturecoords
)-包括创建和处理VAOs
和{{1} }。
我实现了着色器,并且可以成功地为投影和变换矩阵加载诸如VBOs
之类的制服。然后,我使用mat4
添加了纹理并将其全部包装在类org.l33tlabs.twl PNGDecoder
中,该类处理绑定纹理单元,取消绑定以及从openGL中删除纹理。在绑定纹理之前,bind函数还会调用Texture
。
代码
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;
}
#version 400核心
entityFragment.glsl
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'!"
Here是主Git存储库的链接,如果您进行克隆,则需要导入和更新子模块(即在Java awt和openGL中制作相同的游戏) Here是GL特定子模块的链接
有明显的问题吗?如果您需要查看更多代码,请告诉我。
解决方法
glGetUniformLocation
如果名称不是指定程序中活动统一变量的名称,则返回-1:
across
if (result == 1) System.err.println("Could not find uniform variable '" + name + "'!");