GLSL统一缓冲区块为空

时间:2014-08-08 19:11:19

标签: c++ opengl vertex-shader

我想使用glDrawElementsInstanced绘制实例数字。所以我这样设置Uniform buffer block:

struct data
{
    float px, py, pz;
};

static data *buffer;

buffer = new data[100];

for(int i = 0; i < 10; i++)
{
    for(int j = 0; j < 10; j++)
    {
        static int a = 0;
        buffer[a].px = float(i);
        buffer[a].py = float(j);
        buffer[a].pz = 0.0f;
        a++;
    }
}

glGenBuffers(1, &bufd);
glBindBuffer(GL_UNIFORM_BUFFER, bufd);
glBufferData(GL_UNIFORM_BUFFER,
             sizeof(buffer),
             buffer,
             GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, bufd);

此后在顶点着色器中,Uniform块为空。所有的数字都画在同一个地方,就像所有的世界坐标都是0.0,0.0,0.0。我的顶点着色器:

#version 430

in vec4 position;

struct lol//lack of idea
{
    vec3 pozycja;//pozycja means position
};

layout (binding = 0) uniform dane
{
    lol bufin[100];
};

layout ( location = 0 ) uniform mat4 proj_matrix;
layout ( location = 1 ) uniform mat4 camera_matrix;

void main(void)
{
    mat4 mv_matrix = {vec4(1.0, 0.0, 0.0, 0.0),
                      vec4(0.0, 1.0, 0.0, 0.0),
                      vec4(0.0, 0.0, 1.0, 0.0),
                      vec4(bufin[gl_InstanceID].pozycja, 1.0)};


    gl_Position = proj_matrix * camera_matrix * mv_matrix * position;
}

1 个答案:

答案 0 :(得分:3)

您在sizeof(buffer)电话中使用glBufferData()

glBufferData(GL_UNIFORM_BUFFER,
             sizeof(buffer),
             buffer,
             GL_DYNAMIC_DRAW);

buffer被声明为指针:

static data *buffer;

所以sizeof(buffer)是指针的大小,在32位环境中是4个字节,在64位环境中是8个字节。

您需要使用要存储在缓冲区中的数据的实际大小。在您的示例中,这将是:

glBufferData(GL_UNIFORM_BUFFER,
             100 * sizeof(buffer[0]),
             buffer,
             GL_DYNAMIC_DRAW);