From 2f57ba687c6336cb7161a168b853aca52e75a9ca Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 30 Apr 2011 09:32:38 -0400 Subject: [PATCH] specify vertex attributes and draw triangles --- template/test.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/template/test.cc b/template/test.cc index 08afa6e..756b45a 100644 --- a/template/test.cc +++ b/template/test.cc @@ -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(); }