简体   繁体   English

C ++ / OpenGL - 绘制一个立方体VBO

[英]C++/OpenGL - Drawing a cube VBO

Recently I started to learn OpenGL and I am beginning to learn the newer assets of it. 最近我开始学习OpenGL,我开始学习它的新资产。 For a while now I've been trying to work with VBOs, to draw a simple cube from a vertex point array. 有一段时间我一直在尝试使用VBO,从顶点数组中绘制一个简单的立方体。 I am having trouble rendering it and understanding some of the arguments in the functions. 我无法渲染它并理解函数中的一些参数。 It throws no errors but nothing is in my view. 它没有引起任何错误,但我认为没有任何错误。

float cubeVertPoints[72]; //An array containing the vertex points

Here is my VBO init 这是我的VBO初始化

void loadBufferData()
{
    glGenBuffers(1, &cubeVBO);
    glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertPoints[0])*3, &cubeVertPoints[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(cubeVertPoints[0])*3, (void*)sizeof(GL_FLOAT));
} 

Drawing 画画

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); 
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(cubeVertPoints[0])*3, (void*)sizeof(GL_FLOAT));

    glDrawArrays(GL_TRIANGLES, 0, 1); 

    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

You are not transferring all your data into the vertex buffer. 您没有将所有数据传输到顶点缓冲区。 The 2nd parameter of glBufferData takes the size of all of your vertex data in bytes. glBufferData的第二个参数采用所有顶点数据的大小(以字节为单位)。 You should set it to sizeof(vertex) * vertex_count . 您应该将其设置为sizeof(vertex) * vertex_count

Also, calling glEnableVertexAttribArray and glVertexAttribPointer in loadBufferData is redundant. 此外,在loadBufferData调用glEnableVertexAttribArrayglVertexAttribPointer是多余的。 You should call them only in your rendering function. 您应该只在渲染功能中调用它们。

Your second problem is with glVertexAttribPointer . 你的第二个问题是glVertexAttribPointer You are not passing the correct offset to your vertex position data in your vertex data "structure". 您没有将正确的偏移量传递给顶点数据“结构”中的顶点位置数据。 Since your vertices only consist of positions, this offset should be 0 . 由于顶点仅由位置组成,因此此偏移量应为0 If you would have positions and colors for each vertex, these offset could be 0 (for position) and sizeof(float) * 3 (for color, because you have 3 coordinates). 如果你有每个顶​​点的位置和颜色,这些偏移可以是0 (对于位置)和sizeof(float) * 3 (对于颜色,因为你有3个坐标)。

Finally, you only draw a single vertex. 最后,你只能得出一个顶点。 You should draw 72/3=24 if your cube has 24 vertices. 如果您的立方体有24个顶点,则应绘制72/3=24

I think you can make your life easier by defining an actual structure for your vertices, like so: 我认为通过为顶点定义实际结构可以让您的生活更轻松,如下所示:

struct Vertex
{
  float position[3];
};

Then, you can compute offsets for each of your vertex positions, colors, etc, with (GLvoid*)(&((Vertex*)NULL)->position) . 然后,您可以使用(GLvoid*)(&((Vertex*)NULL)->position)计算每个顶点位置,颜色等的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM