如何让VBO工作 [英] How to get VBO working

查看:134
本文介绍了如何让VBO工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用本教程
http://nehe.gamedev.net /data/lessons/lesson.asp?lesson=45

我在Visual Studio 2008中加载它,编译它,它说丢失的文件: GLES / glplatform.h所以我google的文件...然后它呜咽丢失的文件:KHR / khrplatform.h,所以我google也太...然后它一切可能,GLDouble undeclared标识符等等,即使该教程有#include应该有那些。

I load it in my visual studio 2008, compile it, and it says missing file: "GLES/glplatform.h" so i google the file... then it whines missing file: "KHR/khrplatform.h", so i google that too... then it whines everything possible, "GLDouble undeclared identifier" etc etc, even though that tutorial has #include which should have those.

我不知道从哪里开始修复这个,有人可以只给我代码如何正确使用VBO(绘制立方体等),每个代码我试过只是崩溃或不编译。我不能找到任何工作。

I dont know where to start fixing this, could someone just give me code how to use VBO properly (draw a cube etc), every code i have tried just crashes or wont compile. i cant find anything that works.

推荐答案

确定,让我们从你应该检查的开始。在你的编译器中寻找gl.h。如果您找不到它,请下载 Windows SDK 。如果你正在使用Visual Studio适合你,如果不只是采取OpenGL文件。我建议你完整的SDK,因为我还没有发现gl.h分离,可能是因为大家都有它。对于SDK,在Windows Server 2003 R2平台SDK这些OpenGL文件是肯定的,但你应该尝试第一个Windows 7 / Vista SDK。

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.

现在。你提到了GLES。我不知道GLES如何工作,因为我使用GLEW。在许多方面NeHe写了伟大的教程,但他们已经过时了。因此,如果您想继续使用我,请使用 GLEW 。你将包括它在开始+你将不得不提供图书馆。喜欢这个

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

将glew32.dll复制到您的EXE所在的文件夹。

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

现在您应该设置为创建VBO。我想您已经学会了如何不此处的链接创建空白窗口。你将不得不下载GLUT(或更好的freeglut),如果你还没有,但它被广泛使用,你会需要它以后。反之。

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.

在该代码中做一些补充。在main函数中,在CreateWindow下调用init()就像这样

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

并使函数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);
}

也可以使用reshape()func:

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

并在glutDisplayFunc(display)下添加此行;

and add this line under glutDisplayFunc(display);

glutReshapeFunc(reshape);

现在我们准备好了VBO,实际上这只是微不足道的创建窗口,我们可以看到输出,但是你说你有一些问题我最好写下来,对别人。

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.

所以如何使用VBO如果你想看到结果现在这里的代码我会试着告诉你它:
make全局变量

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;

在任何功能之前

部分在init()函数的底部:

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

您应该看到黑色三角形。
所以到代码。你做一些数据。
然后生成未使用的新VBO ID(在我们的示例中每次都将为1)。
之后,使用Bind调用,你实际创建了这个VBO,并使用BufferData调用你的数据到VBO。在显示中,您清除要使用的窗口,选择绘图颜色,现在绑定意味着激活此缓冲区对象。您可以在ARRAY_BUFFER中拥有VBO的编号,但只能有一个处于活动状态。 VertexPointer用于设置VBO的开始和它的元素之间的步距。正如你可以看到,我们使用X和Y坐标作为元素,所以步幅是2 * sizeof(float)。这是因为stride是以字节为单位。 Finnaly DrawArrays是render调用,你可以调用glBegin()和glEnd()。你告诉画什么,在哪个范围。

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:我知道这是一个月老问题,但我认为所有问题应该回答所以任何寻找相同问题的人不会得到这里,并找不到任何东西;)

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:如果这个例子要求DLL你有他们在freeglut或GLEW bin文件; )

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

编辑1:我忘记了,所以不要犯同样的错误,你完成VBO后销毁它,以防止内存泄漏。因为VRAM不是这么大,这可以是严重的这里如何做:

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

这篇关于如何让VBO工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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