我正在尝试在OpenGL中渲染一个白色方块,我有这个功能:
void main_loop(window_data *window)
{
printf("%s\n", glGetString(GL_VERSION));
GLuint vertex_array_objects, vertex_buffer_objects;
glGenVertexArrays(1, &vertex_array_objects);
glBindVertexArray(vertex_array_objects);
glGenBuffers(1, &vertex_buffer_objects);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_objects);
GLfloat vertices[6][2] = {
{-0.90, -0.90},
{0.85, -0.90},
{-0.90, 0.85},
{0.90, -0.85},
{0.90, 0.90},
{-0.85, 0.90}};
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vertices),
vertices,
GL_STATIC_DRAW);
glVertexAttribPointer(0,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window->glfw_window))
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vertex_array_objects);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window->glfw_window);
glfwPollEvents();
}
return;
}
哪种方法非常好,但后来我尝试将其拆分为两个函数,如:
void initialize_image(GLuint *vArray, GLuint *vBuffer, GLfloat vertices[][2])
{
glGenVertexArrays(1, vArray);
glBindVertexArray(*vArray);
glGenBuffers(1, vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, *vBuffer);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vertices),
vertices,
GL_STATIC_DRAW);
glVertexAttribPointer(
0,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0);
return;
}
然后我在main_loop函数中调用它(就在while循环之前):
initialize_image(&vArray, &vBuffer, vertices);
但那让我一脸黑幕。 可能导致这种情况的原因是什么?
答案 0 :(得分:2)
当你将指针传递给顶点数组时,它的sizeof()是指针大小,而不是你的数据大小!
将额外的顶点大小参数传递给您的函数,并使用sizeof of sizeof进行glBufferData调用。