#include <iostream> #include <vgl.h> #include <LoadShaders.h> using namespace std;
enum VAO_IDs {Triangles, NumVAOs}; enum Buffer_IDs {ArrayBuffer, NumBuffers}; enum Attrib_IDs {vPosition = 0};
GLuint VAOs[NumVAOs]; GLuint Buffers[NumBuffers]; const GLuint NumVertices = 6; GLfloat vertices[NumVertices][2] = { { -0.90, -0.90}, { 0.85, -0.90}, { -0.90, 0.85}, { 0.90, -0.85}, { 0.90, 0.90}, { -0.85, 0.90} };
ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "triangles.vert" }, { GL_FRAGMENT_SHADER, "triangles.frag"}, { GL_NONE, NULL} };
void setup() { GLuint program; glGenVertexArrays(NumVAOs, VAOs); glBindVertexArray(VAOs[Triangles]);
glGenBuffers(NumBuffers, Buffers); glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
program = LoadShaders(shaders); glUseProgram(program);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glEnableVertexAttribArray(vPosition); return; }
void display(){ glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(VAOs[Triangles]); glDrawArrays(GL_TRIANGLES, 0, NumVertices); glFlush(); return; }
void keypress(unsigned char key, int x, int y) { switch(key) { case 'q': exit(0); } glutPostRedisplay(); return; } int main(int argc, char * argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512, 512); glutInitContextVersion(3,0); glutInitContextProfile(GLUT_CORE_PROFILE); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutKeyboardFunc(keypress); glewExperimental = GL_TRUE; if (glewInit()) { cerr << "Unable to initialize glew, exiting " << endl; exit (-1); } setup(); glutMainLoop(); return 0; }
attribute vec4 vPosition; varying vec4 color; void main() { gl_Position = vPosition; color = vec4(0.0, 1.0, 0.0, 1.0); }
varying vec4 color; void main() { gl_FragColor = color; }