VBO的使用

翻译 2012年03月21日 13:48:18

原文: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);

相关文章推荐

【OpenGL4.0】GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形

以前都是用Cg的,现在改用GLSL,又要重新学,不过两种语言很多都是相通的。 下面的例子是实现绘制一个三角形的简单程序。采用了VBO(veretx buffer object)、VA...

VBO的使用2

OpenGL VBO 说明 自: http://hi.baidu.com/luckwxx623/blog/item/34e36445b8fad92ecffca3e1.html   顶点缓...

关于使用VBO技术注意问题1

// 使用缓冲区 glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState( GL_VERTEX_ARRAY ); ...

glReadPixels glDrawPixels glCopyPixels与VBO结合 使用24位的位图

与上篇的相结合#include #include #include #include #include #include #include #pragma comment(lib,...

opengl中VBO的使用演示

如下是我做的一个示例代码,具体的就不说了,程序运行需要glew和glut库,找不到这两个库的朋友可以跟我联系。 #include #include #include #include #...

opengles之VBO和FBO的结合使用

FBO的理论详见:http://blog.csdn.net/xiajun07061225/article/details/7283929/ FBO的创建: class Fram...

jogl使用VBO

jogl使用VBO,可以把绘图的顶点数据(点坐标、颜色。。。。)传给显卡,避免CPU遍历数据一个一个的传,极大提升性能(在图元极大的时候很明显) 以下例子,利用vbo技术画三角形package tes...
  • sidihuo
  • sidihuo
  • 2015年04月17日 17:51
  • 876

OpenGL(二十四) VAO、VBO和着色器使用示例

1. 新建一个工程,新建一个头文件Shader.h,内容如下: #ifndef _SHADER_H_ #define _SHADER_H_ #include #include #incl...
  • dcrmg
  • dcrmg
  • 2016年12月06日 23:33
  • 598

OpenGL进阶(九) - GLSL中VAO和VBO的使用

回顾 Vertex Shader : 基于GLSL的语法写的一种类C的代码,可以用来控制顶点的特性,比如位置,颜色还有纹理坐标等。 Fragment Shader:用于计算每个片段的颜色,可用于实现光...

GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形

以前都是用Cg的,现在改用GLSL,又要重新学,不过两种语言很多都是相通的。 下面的例子是实现绘制一个三角形的简单程序。采用了VBO(veretx buffer object)、VAO(ve...
  • flyrxue
  • flyrxue
  • 2012年08月22日 10:08
  • 704
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VBO的使用
举报原因:
原因补充:

(最多只允许输入30个字)