-
#include <iostream>
#include <vgl.h>
#include <LoadShaders.h>
using namespace std;
- Note, the work in the Makefile section above allowed you to include the second and third file in angle brackets.
-
enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffer, NumBuffers};
enum Attrib_IDs {vPosition = 0};
- He uses these enums to name things nicely.
- I assume that if he wanted more than just triangles, he would add more things to VAO_IDs for example.
- Remember C++ assigns the first enum to 0, the next to one and so on.
- So NumVAOs will be the actual count for the enums.
-
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}
};
- Nothing surprising here.
- We will not be changing any transformation matrices, so everything is drawn within the clipping volume.
-
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangles.vert" },
{ GL_FRAGMENT_SHADER, "triangles.frag"},
{ GL_NONE, NULL}
};
- Here we set up a structure that holds types and names of shaders we will be creating.
- You are required to have a Vertex shader and a Fragment shader.
- These will be in a file in the current directory with the given name.
- We will create these later in this exercise
-
void setup() {
GLuint program;
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
- This generates the space to store all VAOs (only 1 in this case)
- And makes the first one active.
- All action takes place with the current VAO
- We will really not see this since there is only one in this program.
- But more later.
-
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- Make a space to hold actual data
- And copy it over.
-
program = LoadShaders(shaders);
glUseProgram(program);
- This is new.
- LoadShaders is part of the code we linked above.
- It will probably become part of a new OpenGL Toolkit some day
- It is somewhat complex, but it reads, compiles and installs parallel programs on the GPU
- Look at the code in LoadShaders.C (in ~dbennett/RedBook/lib) if you want more detail.
-
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition);
return;
}
- This tells the shaders that the data in the buffer should be associated with vPosition
- More on this later
-
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
return;
}
- This makes the VAO active
- And Draws it.
-
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;
}
- Some change in style, but nothing new here either.