Trouble with simple VBO example

扶醉桌前 提交于 2019-12-12 05:03:02

问题


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.

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

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.

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?


回答1:


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.

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

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



来源:https://stackoverflow.com/questions/13633038/trouble-with-simple-vbo-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!