2017-03-16 106 views
0

画我有以下代码基本三角形的OpenGL

#include <iostream> 
using namespace std; 

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


#define BUFFER_OFFSET(a) ((void*)(a)) 


enum eAttributesIDs 
{ 
    Position = 0, 
    Color = 1 
}; 


void display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glDrawArrays(GL_TRIANGLES, 0, 3); 
    glFlush(); 
} 


void init() 
{ 
    GLfloat vertices[] = 
    { 
     0.f, 1.f, 
     -1.f, -1.f, 
     1.f, -1.f 
    }; 

    GLuint bufferId; 
    glGenBuffers(1, &bufferId); 
    glBindBuffer(GL_ARRAY_BUFFER, bufferId); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

    glEnableVertexAttribArray(Position); 
    glVertexAttribPointer(Position, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); 

    glClearColor(0, 1, 1, 1); 
} 


int main(int argc, char** argv) 
{ 
    // configure and open window via glut 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA); 
    glutInitWindowSize(640, 480); 
    glutInitContextVersion(4, 3); 
    glutInitContextProfile(GLUT_CORE_PROFILE); 
    glutCreateWindow(argv[0]); 

    if (glewInit()) 
    { 
     cerr << "Unable to initialize GLEW" << endl; 
     exit(EXIT_FAILURE); 
    } 

    init(); 

    glutDisplayFunc(display); 
    glutMainLoop(); 

    return 0; 
} 

但我想不出为什么,即使它编译和运行改变窗口的背景色,无三角形出现在屏幕上。

+4

这是一个很好的OpenGL教程:https://learnopengl.com/ –

回答

3

我认为你错过了着色器程序& VAO。在使用Core OpenGL上下文时使用它们是必需的。