0
votes

After testing a few things in my OpenGL application, I know that my textures are not loading correctly because the texture coordinates are failing to get from the vertex shader to the fragment shader (or atleast they are all passed as (0,0). I just don't know why.

Here are my positions, indices and texture coordinates for a square:

private static final float[] VERTICES = {
    -0.5f,  0.5f, 0f,
    -0.5f, -0.5f, 0,
     0.5f, -0.5f, 0f,
     0.5f,  0.5f, 0f
};

private static final int[] INDICES = {
    0, 1, 2,
    3, 0, 2
};

private static final float[] TEXTURE_COORDINATES = {
    0.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 1.0f,
    1.0f, 0.0f
};

Here is how I insert the texture coordinates into the vao at index 1. For the sake of brevity I won't show the other parts of my vao loader since they are in working order.

int vboTexCoordID = GL15.glGenBuffers();
ModelManager.recordVBO(vboTexCoordID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboTexCoordID);

FloatBuffer texCoordBuffer = BufferUtils.createFloatBuffer(texCoord.length);
verticesBuffer.put(texCoord);
verticesBuffer.flip();

GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texCoordBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(1,2,GL11.GL_FLOAT, false,  0,0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

This is my render method:

    model.getShader().start(); // this calls GL20.glUseProgram()
    GL30.glBindVertexArray(model.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexId());
    GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVerticesCount(), GL11.GL_UNSIGNED_INT,0);
    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);
    GL30.glBindVertexArray(0);
    model.getShader().stop();

and finally mt vertex shader and fragment shader in that order:

#version 330 core

in vec3 position;
in vec2 texCoords;

out vec2 pass_texCoords;

void main(void){

    gl_Position = vec4(position.x, position.y, position.z, 1.0);
    pass_texCoords = texCoords;

}

fragment:

#version 330 core

in vec2 pass_texCoords;

out vec4 out_Color;

uniform sampler2D textureSampler;

void main(void){

    out_Color = texture(textureSampler, pass_texCoords);

}
1
How do you know that the texCoords attribute has location 1?derhass
Because that is where I put it.zephos2014

1 Answers

2
votes

You never add data to texCoordBuffer:

FloatBuffer texCoordBuffer = BufferUtils.createFloatBuffer(texCoord.length);
verticesBuffer.put(texCoord);
verticesBuffer.flip();

GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texCoordBuffer, GL15.GL_STATIC_DRAW);

texCoordBuffer is empty when you call glBufferData(), since you store the texture coordinates in verticesBuffer. You need to change this to:

FloatBuffer texCoordBuffer = BufferUtils.createFloatBuffer(texCoord.length);
texCoordBuffer.put(texCoord);
texCoordBuffer.flip();

GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texCoordBuffer, GL15.GL_STATIC_DRAW);

You also need to make sure that the locations you use for the vertex attributes match up with the vertex shader. The easiest way to do this is to use layout directives in the vertex shader code:

layout(location=0) in vec3 position;
layout(location=1) in vec2 texCoords;

This specifies that the vertex shader will read the positions from attribute 0, and the texture coordinates from attribute 1.