简体   繁体   English

使用VAO和VBO在3.2中绘制OpenGL线和正方形

[英]Drawing OpenGL Lines & Squares in 3.2 using VAO & VBO's

I've been experimenting with OpenGL 3.2+. 我一直在试验OpenGL 3.2+。

I can successfully render either a line to the screen, or a square made up of two triangles... 我可以成功地渲染一条线到屏幕,或者一个由两个三角形组成的正方形......

I think I'm using VAO and VBO's correctly, yet somehow I can not rending both of them... I experience strange renders. 我认为我正在使用VAO和VBO,但不知怎的,我不能同时使用它们......我遇到了奇怪的渲染。

Obviously, I've coded it wrong... but how are you supposed to use VAO and VBO's when rending multiple objects defined in different Arrays? 显然,我把它编码错了......但是当你渲染不同数组中定义的多个对象时,你应该如何使用VAO和VBO呢?

My code is far too long to post here, so I've linked a copy on Pastebin > Here < 我的代码在这里发布的时间太长了,所以我在Pastebin > Here <上链接了一个副本

When you have multiple objects to display, each in its own buffer array, each buffer needs its own vertex array object handle : 如果要显示多个对象,每个对象都在自己的缓冲区数组中,则每个缓冲区都需要自己的顶点数组对象句柄:

int curr_num_object = 0;

static const int vertex_array_object_fish = curr_num_object++;
static const int vertex_array_object_shark = curr_num_object++; 
static const int vertex_array_object_doughnut = curr_num_object++;


GLuint array_vertex_array_object[curr_num_object]; // one for each drawn object

glGenVertexArrays(curr_num_object, &array_vertex_array_object[0]);

then for each buffer array you bind then load its data onto the GPU : 然后为您绑定的每个缓冲区数组,然后将其数据加载到GPU:

    // -----------------  fish

glBindVertexArray(array_vertex_array_object[vertex_array_object_fish]); // fish VAO

GLuint vertex_buffer_fish;
glGenBuffers(1, & vertex_buffer_fish);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_fish);
glBufferData(GL_ARRAY_BUFFER, audio_model->get_sizeof_fish_array(), audio_model->get_address_fish_array(), GL_DYNAMIC_DRAW);

glVertexAttribPointer(
    0,                  // attribute. No particular reason for 0, but must match the layout in the shader.
    2,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);
glEnableVertexAttribArray(0);

above only deals with the first such buffer array, fish. 上面只涉及第一个这样的缓冲阵列,鱼。 Each subsequent object you wish to display wants a similar set of OpenGL calls. 您希望显示的每个后续对象都需要一组类似的OpenGL调用。 Above is called once outside of your windowing event loop (glfw, glut ...). 在窗口事件循环之外调用上面一次(glfw,glut ...)。 Notice in the 2nd parm to glVertexAttribPointer its a 2D array ... here is its header entry : 请注意第二个参与glVertexAttribPointer的2D数组...这是它的标题条目:

float molecules_location_fish[max_fish][num_dimensions_2D_grid]; //  X & Y per fish

Here is a second object I want to display (doughnut) with its similar calls to above fish : 这是我要显示的第二个对象(甜甜圈),它对上面的鱼有类似的调用:

// -----------

glBindVertexArray(array_vertex_array_object[vertex_array_object_doughnut]); // doughnut VAO
GLuint vertex_buffer_doughnut_box;
glGenBuffers(1, & vertex_buffer_doughnut_box);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_doughnut_box);
glBufferData(GL_ARRAY_BUFFER, audio_model->get_sizeof_doughnut_box_array(), audio_model->get_address_doughnut_box_array(), GL_DYNAMIC_DRAW);

glVertexAttribPointer(
    0,                  // attribute. No particular reason for 0, but must match the layout in the shader.
    3,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);
glEnableVertexAttribArray(0);

// -----------

Now inside your windowing event loop, where perhaps you also make calls to update locations to data of your objects (lines, triangles, ...), you make these OpenGL calls for each object to display : 现在在你的窗口事件循环中,也许你也可以调用更新对象数据(行,三角形......),你可以为每个对象显示以下OpenGL调用:

    // ---------

    glBindVertexArray(array_vertex_array_object[vertex_array_object_fish]);

    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_fish);

    glBufferData(GL_ARRAY_BUFFER, audio_model->get_sizeof_fish_array(), audio_model->get_address_fish_array(), GL_DYNAMIC_DRAW);
    glDrawArrays(GL_POINTS, 0, audio_model->get_curr_num_fish()); // 12*3 indices starting at 0 -> 12 triangles

And for completeness, here are the doughnut calls inside your event loop : 为了完整起见,这里是您的事件循环中的甜甜圈调用:

    glBindVertexArray(array_vertex_array_object[vertex_array_object_doughnut]);

    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_doughnut_box);
    glBufferData(GL_ARRAY_BUFFER,audio_model->get_sizeof_doughnut_box_array(),audio_model->get_address_doughnut_box_array(),GL_DYNAMIC_DRAW);

    glDrawArrays(GL_TRIANGLES, 0, audio_model->get_curr_num_doughnut_boxes());

Notice in my fish I display its 2D as points, whereas the doughnut is 3D and displayed as a set of triangles (not indexed) 在我的鱼中注意我将其显示为2D作为点,而甜甜圈是3D并显示为一组三角形(未编入索引)

Let us know how you get on - this initial speed bump learning OpenGL is ( ^() &)(& Here is a really nice set of tutorials : http://www.opengl-tutorial.org 让我们知道你是如何进行的 - 这个初学速度学习OpenGL是( ^() &)(&这是一套非常好的教程: http//www.opengl-tutorial.org

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

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