JOGL Passing Integer Value to Fragment Shader

74 Views Asked by At

In my OpenGL application I am trying to pass an integer to the vertex shader and then forward it to the fragment shader to use as a zIndex in a texture array. However when I pass the value using a VBO the integer value always seems to be zero.

I am using a VBO to pass vertices, normals, and texture coordinates and they all work as expected. The only value that is not being passed it seems is the integer value. I have also tried doing this with a float and encounter the same behavior.

Vertex Shader

#version 410

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNorm;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in uint aDepth;

out vec2 TexCoords;
out vec3 Normal;
out vec3 FragPos;
out uint vDepth;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
     vDepth = aDepth;
     TexCoords = aTexCoords;
     gl_Position = projection * view * model * vec4(aPos, 1.0);
}

Fragment Shader

#version 410

in vec2 TexCoords;
flat in uint vDepth;

uniform sampler2DArray textureArray;

out vec4 FragColor;


void main()
{
    if(vDepth == 0) {
        FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    } else {
        FragColor = vec4(0.0, 1.0, 0.0, 1.0);
    }

//    FragColor = texture(textureArray, vec3(TexCoords, vDepth));
}

VAO/VBO creation

    public Block(GL4 gl, int shaderProgram, float[] vertices, float[] normals, float[] textureCoordinates, int blockType) {

        gl.glUseProgram(shaderProgram);

        // setup object
        gl.glGenVertexArrays(1, vao, 0);
        gl.glGenBuffers(vbo.length, vbo, 0);

        // bind to vao
        gl.glBindVertexArray(vao[0]);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
        gl.glBufferData(GL_ARRAY_BUFFER, (long) vertices.length * Float.BYTES, Buffers.newDirectFloatBuffer(vertices), GL_STATIC_DRAW);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
        gl.glBufferData(GL_ARRAY_BUFFER, (long) normals.length * Float.BYTES, Buffers.newDirectFloatBuffer(normals), GL_STATIC_DRAW);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
        gl.glBufferData(GL_ARRAY_BUFFER, (long) textureCoordinates.length * Float.BYTES, Buffers.newDirectFloatBuffer(textureCoordinates), GL_STATIC_DRAW);

        // ISSUE IS HERE //
        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[3]);
        IntBuffer blockBuffer = Buffers.newDirectIntBuffer(blockType);
        gl.glBufferData(GL_ARRAY_BUFFER, Integer.BYTES, blockBuffer, GL_STATIC_DRAW);

        // bind to indices
        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
        gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.BYTES, 0);
        gl.glEnableVertexAttribArray(0);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
        gl.glVertexAttribPointer(1, 3, GL_FLOAT, false, 3 * Float.BYTES, 0);
        gl.glEnableVertexAttribArray(1);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
        gl.glVertexAttribPointer(2, 2, GL_FLOAT, false, 2 * Float.BYTES, 0);
        gl.glEnableVertexAttribArray(2);


        // ISSUE IS HERE //
        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[3]);
        gl.glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, Integer.BYTES, 0);
        gl.glEnableVertexAttribArray(3);

        gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
1

There are 1 best solutions below

5
Rabbid76 On

The 2nd argument of glBufferData is the size of the buffer:

gl.glBufferData(GL_ARRAY_BUFFER, Integer.BYTES, blockBuffer, GL_STATIC_DRAW);

gl.glBufferData(GL_ARRAY_BUFFER, Integer.BYTES * blockBuffer.limit(), blockBuffer, GL_STATIC_DRAW);

Note that the vertex shader input is an attribute and the buffer must therefore have one value for each vertex.