绘制OpenGL行&使用VAO& VBO的 [英] Drawing OpenGL Lines & Squares in 3.2 using VAO & VBO's

查看:190
本文介绍了绘制OpenGL行&使用VAO& VBO的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用OpenGL 3.2 +。

I've been experimenting with OpenGL 3.2+.

我可以成功地渲染一行到屏幕,或者一个由两个三角形组成的正方形...

I can successfully render either a line to the screen, or a square made up of two triangles...

我想我正确地使用了VAO和VBO,但是我无法呈现它们两个...我遇到了奇怪的渲染。

I think I'm using VAO and VBO's correctly, yet somehow I can not rending both of them... I experience strange renders.

很显然,我编码错了...但是,当你在不同的Arrays中定义多个对象时,你应该如何使用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?

我的代码太长,无法在此发布,因此我已在Pastebin上关联了一个副本>此处<

My code is far too long to post here, so I've linked a copy on 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]);

然后对每个绑定的缓冲区数组,然后将其数据加载到GPU上:

then for each buffer array you bind then load its data onto the 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);



只处理第一个这样的缓冲数组,鱼。您希望显示的每个后续对象都需要一组类似的OpenGL调用。上面是在你的窗口事件循环外调用的(glfw,glut ...)。注意在第二个parm到glVertexAttribPointer它的一个2D数组...这里是其头条目:

above only deals with the first such buffer array, fish. Each subsequent object you wish to display wants a similar set of OpenGL calls. Above is called once outside of your windowing event loop (glfw, glut ...). Notice in the 2nd parm to glVertexAttribPointer its a 2D array ... here is its header entry :

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);

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

现在在你的窗口事件循环中,你也可以调用更新位置到对象的数据(线,三角形,...),你可以让每个对象的OpenGL调用显示:

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 :

    // ---------

    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());

注意在我的鱼我显示其2D作为点,而甜甜圈是3D并显示为一个集(不索引)

Notice in my fish I display its 2D as points, whereas the doughnut is 3D and displayed as a set of triangles (not indexed)

让我们知道你是怎么做到的 - 这个初始减速碰撞学习OpenGL是( ^() (&这里是一组非常不错的教程: http://www.opengl-tutorial.org

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行&amp;使用VAO&amp; VBO的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆