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("textureSampler",0);
before binding the texture.
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); //<-- Line that throws error
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
When I run this, I get the error "Could not find uniform variable 'textureSampler'!"
from this method in the shader:
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 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.
glGetUniformLocation
returns -1 if the name is not the name of an active uniform variable in the specified program:
if (result == 1) System.err.println("Could not find uniform variable '" + name + "'!");
if (result == -1) System.err.println("Could not find uniform variable '" + name + "'!");