顶点缓冲区对象 Open GL

发布于 2024-10-18 20:16:09 字数 1787 浏览 1 评论 0原文

我对 Open GL 很陌生,我正在尝试构建未弃用的代码。现在我无法掌握的是VBO。这就是我到目前为止所得到的一切,你能解释一下我应该做什么吗?另外,我有 OpenGL 编程指南,所以如果您能指出一些要阅读的页面,那也会非常有帮助。

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

GLuint ID[2];
GLfloat PositionData[][2] ={{50,50},{100,50},{50,100}, {100,50}, {100, 100}, {50, 100}};
GLfloat Colors[][3] = {{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3}};

void init(){
    glewInit();
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(GL_FLAT);
    glGenBuffers(2, ID);
    glBindBuffer(GL_ARRAY_BUFFER, ID[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(PositionData), PositionData, GL_STATIC_DRAW);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, ID[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 12);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDeleteBuffers(2, ID);
    glFlush();
}

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

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("I don't get VBOs");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

I am really new to Open GL and I am trying to build non deprecated code. Now what I can't grasp is VBO. This is all I got so far, can you please explain what I'm supposed to be doing. Also, I have the OpenGL programming guide, so if you can point out some pages to read that would be very helpful as well.

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

GLuint ID[2];
GLfloat PositionData[][2] ={{50,50},{100,50},{50,100}, {100,50}, {100, 100}, {50, 100}};
GLfloat Colors[][3] = {{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3}};

void init(){
    glewInit();
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(GL_FLAT);
    glGenBuffers(2, ID);
    glBindBuffer(GL_ARRAY_BUFFER, ID[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(PositionData), PositionData, GL_STATIC_DRAW);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, ID[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 12);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDeleteBuffers(2, ID);
    glFlush();
}

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

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("I don't get VBOs");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北渚 2024-10-25 20:16:09

我快速浏览了你的代码,发现一件事很突出:你完全没有理解 VBO 的要点。在每个绘制调用中,您创建 VBO 对象,上传数据,尝试绘制它们(这不起作用,因为缺少提供顶点属性意义的着色器),然后删除它们。

VBO 的全部要点是将几何数据上传到 GL 对象一次,然后仅通过其抽象对象 ID 句柄和索引数组来引用它,这些数据也将放置在缓冲区对象中。

但是该代码中还有很多其他内容,例如在重塑回调中设置投影矩阵。一方面,在 OpenGL-3 核心中,您不再拥有那些内置矩阵。都是着色器制服。然后您可以在渲染处理程序中根据需要设置这些矩阵。

我认为我真的应该编写权威的 OpenGL-3 核心教程,展示正确的习惯用法。

I did a quick look on your code, an one thing stood out: You're completely missing the point of VBOs. In each draw call you're creating the VBO objects, upload the data, try to draw them (it won't work, because the shaders giving the vertex attributes sense are misssing) and then delete them.

The whole point of VBO is to upload the geometry data into a GL object once and then only refer to it by it's abstract object ID handle and index arrays, which one would also place in a buffer object.

But there's so much else screwed in that code, like setting projection matrix in reshape callback. For one thing, in OpenGL-3 core you don't have those built-in matrices anymore. It's all shader uniforms. And then you set those matrices on demand in the rendering handler.

I think I should really write that definitive OpenGL-3 core tutorial, demonstrating the proper idioms.

疏忽 2024-10-25 20:16:09

您正在使用索引 0 和 1 设置自定义顶点属性,但我没有看到使用它们的着色器。如果您还没有着色器,那么它将无法工作,因为固定功能管道接受特定属性(由 glVertexPointerglColorPointer 根据您的情况设置)。

如果您决定制作一个着色器(您的目标是为 GL-3 核心制作它,对吗?),请不要忘记在链接之前用您在 GLSL 代码中使用的实际名称分配顶点属性:

glBindAttribLocation( program_id, attribute_id, attribute_variable_name )

You are setting up custom vertex attributes with indices 0 and 1, but I don't see a shader that uses them. If you don't have a shader yet, then it will not work, because Fixed-Function pipeline accepts specific attributes (set up by glVertexPointer and glColorPointer for your case).

If you decide to make a shader (your goal is to make it for GL-3 core, right?), don't forget to assign your vertex attributes with actual names you use in GLSL code before linking it:

glBindAttribLocation( program_id, attribute_id, attribute_variable_name )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文