specify vertex attributes and draw triangles

This commit is contained in:
Josh Holtrop 2011-04-30 09:32:38 -04:00
parent 1434828ad9
commit 2f57ba687c

View File

@ -12,6 +12,9 @@ using namespace std;
#define WIDTH 800
#define HEIGHT 600
#define STRIDE(a, b) ((unsigned long)(b) - (unsigned long)(a))
#define OFFSET(a, b) ((const GLvoid *)STRIDE((a), (b)))
const GLfloat data[][2][3] = {
{{-1.0, -0.6, 0.0}, {1, 0, 0}},
{{-0.2, -0.6, 0.0}, {0, 1, 0}},
@ -24,6 +27,7 @@ const GLushort indices[] = {
0, 1, 2, 3, 4, 5
};
GLuint program, vs, fs, data_vbo, index_vbo;
GLint pos_loc, color_loc;
char * loadFile(const char *fname)
{
@ -155,6 +159,9 @@ bool init(int width, int height)
return false;
}
pos_loc = glGetAttribLocation(program, "pos");
color_loc = glGetAttribLocation(program, "color");
data_vbo = makeBuffer(GL_ARRAY_BUFFER, data, sizeof(data));
index_vbo = makeBuffer(GL_ELEMENT_ARRAY_BUFFER, indices, sizeof(indices));
@ -164,6 +171,16 @@ bool init(int width, int height)
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, data_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_vbo);
glEnableVertexAttribArray(pos_loc);
glEnableVertexAttribArray(color_loc);
glVertexAttribPointer(pos_loc, 3, GL_FLOAT, GL_FALSE,
STRIDE(&data[0], &data[1]), OFFSET(&data[0], &data[0][0]));
glVertexAttribPointer(color_loc, 3, GL_FLOAT, GL_FALSE,
STRIDE(&data[0], &data[1]), OFFSET(&data[0], &data[0][1]));
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]),
GL_UNSIGNED_SHORT, STRIDE(&indices, &indices[0]));
SDL_GL_SwapBuffers();
}