VBO的使用

原文:http://stackoverflow.com/questions/3121472/how-to-get-vbo-working

ok so let's start with what you should check for. In your compiler look for gl.h . If you won't find it download Windows SDK. If you are using Visual Studio good for you, if not take just the OpenGL files. I suggest you full SDK because I haven't yet found gl.h separatelly, probably because everybody has it.. As for the SDK, in Windows Server 2003 R2 Platform SDK these openGL files are for sure, but you should try first Windows 7/Vista SDK.

Now. You mentioned GLES. I don't know how GLES work because I use GLEW. In many ways NeHe wrote great tutorials but they're getting outdated. So if you like to continue with me, use GLEW. You will include it on start + you will have to provide library. Like this

#include <GL/glew.h>
#pragma comment(lib,"glew32.lib")

Also you will have to copy glew32.dll to folder where your EXE is.

Now you should be set up for creating VBO. I guess that you already learned how to create blank window if not here's link. You will have to download GLUT(or better freeglut) if you didn't already, but it is widely used and you will need it later on anyway.

We'll do some additions in that code. In main func, call init() under CreateWindow call like this

glutCreateWindow ("You’re first OpenGL Window");
init();

and make function init look like this:

void init(){
    glewInit();
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glShadeModel(GL_FLAT);
    glEnableClientState(GL_VERTEX_ARRAY);
}

also make reshape() func:

void reshape(int w, int h){
    glViewport(0,0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0f, (GLdouble) w, 0.0f, (GLdouble) h);
}

and add this line under glutDisplayFunc(display);

glutReshapeFunc(reshape);

Now we are ready for VBO actually this was just trivial creating of window where we can see output but as you said you had some problems I better wrote it down, as for others.

So how to use VBO if you want to see result now here's code I'll try to tell you about it: make global variable

GLuint ID;

before any of functions

add this part on bottom of init() function:

float data[][2] = {{50,50},{100,50},{75,100}};
glGenBuffers(1,&ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

and this part into display function which is empty in this moment:

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glVertexPointer(2, GL_FLOAT, 2*sizeof(float), 0);
glDrawArrays(GL_POLYGON,0,3);
glFlush();

You should see black triangle. So to the code. You make some sort of data. Then you generate new VBO ID which is not in use(will be 1 in our example everytime :) ). After this with Bind call you actually create this VBO and with BufferData call you asign your data to that VBO. In display, you clear window for use, select drawing color and now the Bind means ACTIVATE this buffer object. You can have number of VBO in ARRAY_BUFFER but only one can be active. VertexPointer is use to set begining of VBO and strides between it's elements. As you can see we used X and Y coordinates as elements so stride is 2*sizeof(float). That's because stride is in bytes. Finnaly DrawArrays is render call, something as you would call glBegin() and glEnd(). You tell what to draw, and in which range. glFlush() is just used for showing rendered stuff.

If you got lost somewhere in code here it is on one place:

#include <windows.h>
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>

#pragma comment(lib,"glew32.lib")

GLuint ID;

void init(){
    glewInit();
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glShadeModel(GL_FLAT);
    glEnableClientState(GL_VERTEX_ARRAY);
    float data[][2] = {{50,50},{100,50},{75,100}};
    glGenBuffers(1,&ID);
    glBindBuffer(GL_ARRAY_BUFFER, ID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}

void reshape(int w, int h){
    glViewport(0,0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0f, (GLdouble) w, 0.0f, (GLdouble) h);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f,0.0f,0.0f);
    glBindBuffer(GL_ARRAY_BUFFER, ID);
    glVertexPointer(2, GL_FLOAT, 2*sizeof(float), 0);
    glDrawArrays(GL_TRIANGLES,0,3);
    glFlush();  
}

int main(int argc, char **argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(300,300);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

PS: I know this is month old question but I think all questions should be answered so anyone looking for same problem won't get here and find nothing ;)

PSS: If this example asks for DLL you have them in freeglut or GLEW bin files ;)

Edit1: I forgot for that so don't make same mistake, after you are finished with VBO destroy it to prevent memory leak. As VRAM isn't so big this can be serious here's how to do it:

glDeleteBuffers(1, &ID);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt OpenGL提供了一种实现高效渲染的机制,它使用了VAO(Vertex Array Object)和VBO(Vertex Buffer Object)技术。 VAO是一种对象,用于存储顶点数据、顶点属性以及它们之间的关联关系。它可以理解为一个顶点属性的容器。通过使用VAO,我们可以将顶点数据存储在显存中,而不是每次渲染时都从CPU传输顶点数据到显存,从而提高渲染的效率。 VBO是一个存储顶点数据的缓冲区对象。通过将顶点数据存储在VBO中,我们可以将数据一次性地传输到显存中,并且可以高效地管理和使用这些数据。同时,VBO还可以提供顶点缓存和索引缓存功能,用于顶点的重用和图元的索引。 Qt OpenGL提供了许多方便的API来操作VAO和VBO。我们可以使用QOpenGLVertexArrayObject类来创建和管理VAO,通过调用QOpenGLBuffer类的相关方法来创建和管理VBO。例如,我们可以使用QOpenGLVertexArrayObject::bind()和QOpenGLVertexArrayObject::release()方法来绑定和释放VAO,使用QOpenGLBuffer::bind()和QOpenGLBuffer::release()方法来绑定和释放VBO使用VAO和VBO可以大大简化OpenGL代码的编写,并且能够有效提高渲染效率。我们可以将需要渲染的数据一次性地传输到显存中,并且设置好相应的渲染状态,然后每次渲染时只需要绑定VAO进行渲染即可,不需要重复的数据传输和状态设置操作。 综上所述,Qt OpenGL的VAO和VBO技术可以帮助我们实现高效渲染,提高应用程序的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值