OpenGL索引缓冲区对象元素顺序绘制不正确

问题描述 投票:0回答:1

enter image description here

注意我的程序如何绘制一个三角形,但是我想在代码中表达的是绘制一个正方形。我相信tri_indicies索引缓冲区对象正确地排列了这些元素,以便应绘制正方形,但是在执行程序时,我在tri_indicies中定义的绘制顺序不会反映在窗口中。尽管我对元素顺序的更改没有影响我的渲染输出,但我不确定该错误是否源于tri_indicies,但我想相信它在这里,但很有可能在其他地方。

我的程序特别使用抽象来详细描述VertexBuffer,VertexArray和IndexBuffer。

const int buffer_object_size = 8;
const int index_buffer_object_size = 6;
float tri_verticies[buffer_object_size] = {
    -0.7f, -0.7f, // 0
     0.7f, -0.7f, // 1
     0.7f,  0.7f, // 2
    -0.7f,  0.7f  // 3
};
unsigned int tri_indicies[index_buffer_object_size] = {
    0, 1, 2,
    2, 3, 0
};

VertexArray vertexArray;                
VertexBuffer vertexBuffer(tri_verticies, buffer_object_size * sizeof(float)); // no call vertexBuffer.bind() constructor does it    

VertexBufferLayout vertexBufferLayout;
vertexBufferLayout.push<float>(3);
vertexArray.add_buffer(vertexBuffer, vertexBufferLayout);

IndexBuffer indexBuffer(tri_indicies, index_buffer_object_size);        

ShaderManager shaderManager;
ShaderSource shaderSource = shaderManager.parse_shader("BasicUniform.shader");  // ensure debug working dir is relative to $(ProjectDir)
unsigned int shader = shaderManager.create_shader(shaderSource.vertex_source, shaderSource.fragment_source);
MyGLCall(glUseProgram(shader));

稍后,主要是我有一个循环,应该将我的正方形绘制到屏幕上并淡出1.0f0.0f之间的蓝色值。

while (!glfwWindowShouldClose(window))
{
    MyGLCall(glClear(GL_COLOR_BUFFER_BIT));

    vertexArray.bind();
    indexBuffer.bind();

    MyGLCall(glDrawElements(GL_TRIANGLES, index_buffer_object_size, GL_UNSIGNED_INT, nullptr)); // nullptr since we bind buffers using glGenBuffers

    if (blue > 1.0f) {
        increment_color = -0.05f;
    }
    else if (blue < 0.0f) {
        increment_color = 0.05f;
    }

    blue += increment_color;

    glfwSwapBuffers(window);
    glfwPollEvents();
}
opengl renderer vertex vertex-buffer index-buffer
1个回答
0
投票

tri_verticies数组由具有2个分量(x, y)的顶点坐标组成。因此,用于指定通用顶点属性数据数组的元组大小必须为2,而不是3:

vertexBufferLayout.push<float>(3);vertexBufferLayout.push<float>(2);

© www.soinside.com 2019 - 2024. All rights reserved.