如何让 VBO 工作

How to get VBO working(如何让 VBO 工作)
本文介绍了如何让 VBO 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

我在我的 Visual Studio 2008 中加载它,编译它,它说缺少文件:GLES/glplatform.h"所以我谷歌这个文件......然后它抱怨缺少文件:KHR/khrplatform.h",所以我也用谷歌搜索......然后它会抱怨一切可能,GLDouble undeclared identifier"等等,即使那个教程有#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 Platform 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.

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

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

glutCreateWindow ("Your 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() 函数:

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,这是我将尝试告诉您的代码:制作全局变量

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).那是因为步幅以字节为单位.Finnaly DrawArrays 是渲染调用,就像您调用 glBegin() 和 glEnd() 一样.你告诉你要画什么,在哪个范围内.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:我知道这是一个多月的问题,但我认为应该回答所有问题,这样任何寻找相同问题的人都不会到这里却什么也找不到;)

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

Edit1:我忘记了,所以不要犯同样的错误,在你完成 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 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)