OpenGL - 不能绘制三角形

时间:2015-11-23 02:29:18

标签: c++ opengl

所以我试图从一些教程中学习OpenGL,每当我尝试使用VertexAtrribArray绘制一个三角形时,程序会编译,但它不会绘制三角形。 这是代码:

#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;

GLuint VBO;

void draw();
void CreateVertexBuffer();

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);

    glutInitWindowSize(256, 256);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Test");

    glutDisplayFunc(draw);

    glClearColor(0.0,0.0,0.0,0.0);

    GLenum val = glewInit();
    if(val != GLEW_OK){
        cout << "Error: " << glewGetErrorString(val);
    }
    CreateVertexBuffer();
    glutMainLoop();
    return 0;
}

void CreateVertexBuffer(){
    GLfloat verts[] =
    {
      0.0f, 1.0f,
      -1.0f, -1.0f,
      1.0f, -1.0f
    };
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
}

void draw(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);

    glutSwapBuffers();
}

任何帮助将不胜感激,谢谢。

0 个答案:

没有答案