我在设置函数时遇到了困难,需要为我生成和绑定缓冲区。
我有两个功能,如下所示:
GLuint vertex_buffer(const GLfloat * thing)
{
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(thing), thing, GL_STATIC_DRAW);
return vertexbuffer;
}
GLuint cube_vertex_buffer()
{
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube), cube, GL_STATIC_DRAW);
return vertexbuffer;
}
我为多维数据集声明了一些全局数据:
static const GLfloat cube[] = {
-1.0f,-1.0f,-1.0f, // triangle 1 : begin
-1.0f,-1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, // triangle 1 : end
1.0f, 1.0f,-1.0f, // triangle 2 : begin
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f,-1.0f, // triangle 2 : end
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f
};
当我打电话
GLuint vertexbuffer = cube_vertex_buffer();
一切都很完美,但是当我试着打电话时
GLuint vertexbuffer = vertex_buffer(cube);
立方体被破坏,看起来像是随机放置的三角形。
我尝试将参数的类型更改为vertex_buffer函数,但它似乎没有做多少好事。我不知道它是否可以将数组作为指针作为参数传递给这类东西。有没有办法让vertex_buffer函数工作?
答案 0 :(得分:1)
sizeof(thing)不等于sizeof(cube)。这可能很重要
答案 1 :(得分:0)
sizeof(thing) != sizeof(cube)
sizeof(thing) == sizeof(GLfloat*)
sizeof(cube) == sizeof(GLfloat) * 3 * 36
像这样更改函数vertex_buffer
:
GLuint vertex_buffer(const GLfloat * thing, GLsizeiptr n)
{
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, n, thing, GL_STATIC_DRAW);
return vertexbuffer;
}
然后致电
GLuint vertexbuffer = vertex_buffer(cube, sizeof(cube));