Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Created March 12, 2014 13:11
Show Gist options
  • Select an option

  • Save Orpheon/9506617 to your computer and use it in GitHub Desktop.

Select an option

Save Orpheon/9506617 to your computer and use it in GitHub Desktop.
float *grid_vertices;
int index = 0;
grid_vertices = calloc(9, sizeof(float));
grid_vertices[index++] = -5.0;
grid_vertices[index++] = 0.0;
grid_vertices[index++] = -1.0;
grid_vertices[index++] = 5.0;
grid_vertices[index++] = 0.0;
grid_vertices[index++] = -1.0;
grid_vertices[index++] = 0.0;
grid_vertices[index++] = 5.0;
grid_vertices[index++] = -1.0;
// Set up an index buffer
GLushort *grid_indices;
grid_indices = calloc(3, sizeof(GLushort));
int num_vertices = 0;
grid_indices[num_vertices++] = 0;
grid_indices[num_vertices++] = 1;
grid_indices[num_vertices++] = 2;
// Create a VAO
GLuint gridVAO;
glGenVertexArrays(1, &gridVAO);
// Allocate and upload the VBO data
GLuint gridVBO;
glGenBuffers(1, &gridVBO);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 9, grid_vertices, GL_STATIC_DRAW);
// We also need an IBO
GLuint gridIBO;
glGenBuffers(1, &gridIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gridIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * sizeof(GLushort), grid_indices, GL_STATIC_DRAW);
glBindVertexArray(gridVAO);
glVertexAttribPointer(gridVAO, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(gridVAO);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw
glDrawElements(GL_TRIANGLES, 1, GL_UNSIGNED_SHORT, 0);
//Force display to be drawn now
glFlush();
glfwSwapBuffers(window);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment