Skip to content

Instantly share code, notes, and snippets.

@vijfhoek
Created December 7, 2013 20:06
Show Gist options
  • Save vijfhoek/7847885 to your computer and use it in GitHub Desktop.
Save vijfhoek/7847885 to your computer and use it in GitHub Desktop.
#include "Block.h"
#include <GL\glew.h>
#include <GLFW\glfw3.h>
Block::Block(const unsigned int blockID, const Vec3<double> position)
: blockID(blockID), position(position)
{
// Generate a vertex array
double vertices[] =
{
position.x, position.y, position.z,
position.x + 1, position.y, position.z,
position.x + 1, position.y + 1, position.z,
position.x, position.y + 1, position.z,
position.x, position.y, position.z + 1,
position.x + 1, position.y, position.z + 1,
position.x + 1, position.y + 1, position.z + 1,
position.x, position.y + 1, position.z + 1,
};
// "Generate" an index array
const unsigned char indices[] =
{
1, 2, 5, 6, // x+
0, 4, 7, 3, // x-
0, 1, 5, 4, // y+
2, 3, 7, 6, // y-
3, 2, 1, 0, // z+
4, 5, 6, 7 // z-
};
// Generate buffers for the vertex and index arrays
glGenBuffers(1, &_vertexBuffer);
glGenBuffers(1, &_indexBuffer);
// Bind the vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
// Send vertex array to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(long long) * 8 * 3, vertices, GL_STATIC_DRAW);
// Bind the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
// Send indx array to the GPU
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(char) * 4 * 6, indices, GL_STATIC_DRAW);
}
Block::~Block()
{
glDeleteBuffers(1, &_vertexBuffer);
}
void Block::render()
{
// Bind vertex and index buffers
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
// Enable vertex array for writing
glEnableClientState(GL_VERTEX_ARRAY);
// Define the array of vertex data
glVertexPointer(3, GL_DOUBLE, 0, 0);
// Draw 6 quads using offset from index buffer
glDrawElements(GL_QUADS, 4 * 6, GL_UNSIGNED_BYTE, 0);
// Disable vertex array for writing
glDisableClientState(GL_VERTEX_ARRAY);
// Unbind (bind to 0) vertex and index buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment