opengl JOGL向片段着色器传递对象值

zfciruhq  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(167)

在我的OpenGL应用程序中,我试图将一个整数传递给顶点着色器,然后将其转发给片段着色器作为纹理数组中的zIndex。然而,当我使用VBO传递值时,整数值似乎总是为零。
我使用VBO来传递顶点,法线和纹理坐标,它们都按预期工作。唯一没有传递的值似乎是整数值。我也尝试用浮点数来做这件事,遇到了同样的行为。
顶点着色器

#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);
}

字符串
片段着色器

#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 /虚拟BO创建

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);
    }

gfttwv5a

gfttwv5a1#

glBufferData的第二个参数是缓冲区的大小:
gl.glBufferData(GL_ARRAY_BUFFER, Integer.BYTES, blockBuffer, GL_STATIC_DRAW);

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

字符串
请注意,顶点着色器输入是一个属性,因此缓冲区必须为每个顶点提供一个值。

相关问题