windows 如何让 VBO 工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3121472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:43:15  来源:igfitidea点击:

How to get VBO working

c++windowsvisual-studio-2008opengl

提问by Newbie

I tried to use this tutorial http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45

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

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.

我将它加载到我的 Visual Studio 2008 中,编译它,它说缺少文件:“GLES/glplatform.h”所以我用谷歌搜索文件......然后它抱怨丢失文件:“KHR/khrplatform.h”,所以我谷歌那也是......然后它抱怨一切可能,“GLDouble undeclared identifier”等等,即使那个教程有#include应该有那些。

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.

我不知道从哪里开始解决这个问题,有人可以给我代码如何正确使用 VBO(绘制立方体等),我尝试过的每个代码都崩溃或无法编译。我找不到任何有效的东西。

回答by Raven

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.

好的,让我们从您应该检查的内容开始。在您的编译器中查找 gl.h 。如果您找不到它,请下载Windows SDK。如果您使用的是适合您的 Visual Studio,如果不是只使用 OpenGL 文件。我建议你完整的SDK,因为我还没有单独找到gl.h,可能是因为每个人都有。 至于SDK,在Windows Server 2003 R2 Platform SDK中这些openGL文件是肯定的,但你应该先尝试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

现在。你提到了 GLES。我不知道 GLES 是如何工作的,因为我使用 GLEW。在许多方面,NeHe 编写了很棒的教程,但它们已经过时了。所以如果你想继续和我一起,使用GLEW。您将在开始时包含它+您必须提供库。像这样

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

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

此外,您还必须将glew32.dll 复制到您的EXE 所在的文件夹中。

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.

现在您应该准备好创建 VBO。如果不是这里的链接,我想您已经学会了如何创建空白窗口。如果您还没有下载 GLUT(或更好的 freeglut),您将不得不下载它,但它被广泛使用,无论如何您以后都会需要它。

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

我们将在该代码中添加一些内容。在主函数中,像这样在 CreateWindow 调用下调用 init()

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

and make function init look like this:

并使函数 init 看起来像这样:

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

also make reshape() func:

还使 reshape() 函数:

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

并在 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.

现在我们已经为 VBO 做好了准备,实际上这只是简单的创建窗口,我们可以在其中看到输出,但是正如您所说,您遇到了一些问题,我最好将其写下来,至于其他问题。

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

那么如何使用 VBO 如果你想现在看到结果这里的代码我会试着告诉你:制作全局变量

GLuint ID;

before any of functions

在任何功能之前

add this part on bottom of init() function:

在 init() 函数的底部添加这部分:

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.

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

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

PS:我知道这是一个多月的问题,但我认为所有问题都应该得到回答,所以任何寻找相同问题的人都不会到这里却一无所获;)

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

PSS:如果此示例要求提供 DLL,则您将它们放在 freeglut 或 GLEW bin 文件中;)

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:

Edit1:我忘记了,所以不要犯同样的错误,在你完成 VBO 后销毁它以防止内存泄漏。由于 VRAM 不是很大,这可能很严重,这是如何做到的:

glDeleteBuffers(1, &ID);