OpenGL VBO不渲染

时间:2014-06-20 15:42:18

标签: c++ opengl c++11 vbo

我正在关注opengl-tutorial.org的教程,但我无法获得第一个要渲染的三角形。一切都很好,没有错误或警告。屏幕上没有任何内容呈现(它只是黑色),预期输出为this。注意:我不是最好的C ++,我主要使用Java和C#,所以如果有任何建议用新的C ++ 11功能来改进我的代码(到目前为止我已经采用的类没有包含任何C + +11),这将是一个很大的帮助。

的main.cpp

#include "Game.h"

int main() {
    Game game;
    game.Start();
    return 0;
}

Game.h

#ifndef GAME_H
#define GAME_H

#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

class Game {
    public:
        Game();
        void Start();
        void Update();
        void Render();
        ~Game();
    private:
        GLFWwindow* window;
        GLuint vertexArrays;
        GLuint vertexBuffer;
};

#endif

Game.cpp

#include "Game.h"

Game::Game() {
    glfwInit();
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    this->window = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL);
    glfwMakeContextCurrent(this->window);
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (err != GLEW_OK) {
        printf("%s\n", glewGetErrorString(err));
        exit(-1);
    }
}

void Game::Start() {
    GLfloat vertexData[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f
    };
    glGenVertexArrays(1, &this->vertexArrays);
    glBindVertexArray(this->vertexArrays);
    glGenBuffers(1, &this->vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
    while (true) {
        glfwPollEvents();
        if (glfwGetKey(this->window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(this->window)) {
            break;
        }
        this->Update();
        this->Render();
    }
}

void Game::Update() {

}

void Game::Render() {
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
    glVertexAttribPointer(
        0,
        3,
        GL_FLOAT,
        GL_FALSE,
        0,
        (void*)0
    );
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
}

Game::~Game() {
    glfwDestroyWindow(this->window);
}

0 个答案:

没有答案