麻烦简单的VBO例子 [英] Trouble with simple VBO example

查看:157
本文介绍了麻烦简单的VBO例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个超级简单GLFW和VBO例如跑步,但我坚持。我使用在glBegin glEnd 其他项目,但我想更新我的code到与OpenGL ES的2工作,但现在我只想尽可能的向前兼容。

I'm trying to get a super simple GLFW and VBO example running, but I'm stuck. I've used glBegin and glEnd for other projects, but I'm trying to update my code to work with OpenGL ES 2, but for now I just want to be as forward compatible as possible.

,我想出了以下code:

From some examples online, I've come up with the following code:

#include <stdlib.h>
#include <GL/glfw3.h>
#include <stdio.h>

#define bool int
#define true 1
#define false 0

const int width = 800;
const int height = 600;
bool opened = true;

GLuint glTriangle;
GLfloat triangle[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f,  1.0f, 0.0f,
};

void init() {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glGenBuffers(1, &glTriangle);
    glBindBuffer(GL_ARRAY_BUFFER, glTriangle);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
}

void display() {
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, glTriangle);

    glVertexAttribPointer(0L, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
}

int windowClose(GLFWwindow window) {
    opened = false;
    return GL_TRUE;
}

void main() {
    if (!glfwInit()) {
        printf("glfwInit didn't work\n");
        return;
    }

    GLFWwindow window = glfwCreateWindow(width, height, GLFW_WINDOWED, "Test", 0);
    glfwMakeContextCurrent(window);

    init();

    glfwSetWindowCloseCallback(window, windowClose);

    glfwSwapInterval(1);

    while(opened) {
        display();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
}

这code运行,但我得到的是一个黑色的屏幕时,我期望看到一个白色的三角形。我试图避免任何花哨像着色器/纹理,直到我至少得到的东西涂在屏幕上。

This code runs, but all I get is a black screen when I expected to see a white triangle. I've tried to avoid anything fancy like shaders/textures until I at least get something painted to the screen.

如果它的事项,我与编译:通过梅萨司机的gcc -o测试-lGL -lglfw -lm test.c以在Linux上使用OpenGL 2.1版本。

If it matters, I'm compiling with: gcc -o test -lGL -lglfw -lm test.c on Linux with OpenGL version 2.1 through the Mesa driver.

我是什么做错了吗?如果我需要指定颜色信息,什么是一个简单的方法来做到这一点?

What am I doing wrong? If I need to specify color information, what is a simple way to do that?

推荐答案

您不应该使用 glEnableVertexAttribArray / glVertexAttribPointer 与固定管道。它的可能的工作(我不是正面的,我觉得ATTRIB 0威力别名到顶点数组ATTRIB,但不能肯定)。这不是真正正确的这样做。

You shouldn't be using glEnableVertexAttribArray / glVertexAttribPointer with the fixed pipeline. It might work (I'm not positive, I think attrib 0 might alias to the vertex attrib array, but not sure). It's not really correct to do so.

如果你没有,你应该使用 glEnableClientState glVertexPointer 顶点数据的着色器。

If you don't have a shader you should be using glEnableClientState and glVertexPointer for vertex data.

默认颜色为(1,1,1,1),所以我觉得你应该对颜色确定为现在。

The default color is (1,1,1,1), so I think you should be ok on colors for now.

这篇关于麻烦简单的VBO例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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