Java-如何在LWJGL中使用顶点缓冲区对象? [英] Java - how to use the vertex buffers object in LWJGL?

查看:126
本文介绍了Java-如何在LWJGL中使用顶点缓冲区对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用顶点缓冲区对象.

I am trying to use the vertex buffers object.

起初,在遇到这种令人讨厌的情况之前,没有任何问题:

At first there wasn't any problem until I got into this nasty situation:

glPointSize(2.0f);
glBegin(GL_POINTS);
for (Entity p : points) {
    glVertex3f(p.x, p.y, p.z);
}
glEnd();

如何将其转换为顶点缓冲区对象"渲染?

How can I convert this into the Vertex Buffers Object render?

我的意思是,正如您所看到的,数据(x,y,z)每次都针对每个点进行更改(这是一个循环).

I mean as you can see, the data (x, y, z) are changed every time for each point (it's a loop ).

那么我该如何在其中实现顶点缓冲区对象"渲染?

So how can i implement the Vertex Buffers Object render into this?

推荐答案

基本上,您希望将所有顶点数据放入FloatBuffer中,然后将其传递给OpenGL.我创建了一个VBO的小示例,该示例存储了三角形的顶点和颜色并进行渲染以及如何删除它!

Basically what you want it putting all the vertex data into a FloatBuffer, and then pass it to OpenGL. I've created a little example of a VBO storing Vertices and Colors for a Triangle and rendering it and also how to delete it!

这是您创建实际的顶点和颜色缓冲区并将其绑定到VBO的代码.

This is the code where you create the actual Vertex and Color Buffer and bind them to the VBO.

int vertices = 3;

int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,

FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });
vertex_data.flip();

FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });
color_data.flip();

int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

int vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

如果愿意,您当然可以在vertex_datacolor_data中添加更多的顶点和颜色!但是请始终记住,顶点数据的数量需要与颜色数据的数量相匹配,反之亦然!

You can of course add more Vertices and Colors to the vertex_data and color_data if you want to! But always remember that the amount of vertex data, need to match with the amount of color data and vice versa!

重要:仅创建一次VBO,并且仅在必要时进行更新!不要为每个帧都创建它们,因为与使用立即模式进行渲染相比,它们最终将导致帧速率更差!

Important: Only create the VBO(s) once, and only update them when necessary! Don't create them for each frame, since them you will end up with a frame-rate worse than when using immediate mode for rendering!

这是您需要调用以呈现VBO的代码.

This is the code you need to call, to render the VBO.

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);

glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, vertices);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

删除VBO

然后,当您使用完VBO并且不再需要它时,可以通过以下操作将其删除.

Deleting the VBO

Then when you're done with the VBO and you don't need it anymore, you can delete it by doing the following.

glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_color_handle);

这篇关于Java-如何在LWJGL中使用顶点缓冲区对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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