Skip to content

Instantly share code, notes, and snippets.

@lapwat
Created February 27, 2017 17:46
Show Gist options
  • Save lapwat/8b03fed082caa27425b033dacac62ffb to your computer and use it in GitHub Desktop.
Save lapwat/8b03fed082caa27425b033dacac62ffb to your computer and use it in GitHub Desktop.
#include "./../include/IndexedCubeRenderable.hpp"
#include "./../include/gl_helper.hpp"
#include "./../include/log.hpp"
#include "./../include/Utils.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <GL/glew.h>
IndexedCubeRenderable::IndexedCubeRenderable(ShaderProgramPtr shaderProgram) :
Renderable(shaderProgram),
m_pBuffer(0),
m_cBuffer(0),
m_iBuffer(0)
{
m_model = glm::mat4(1.0);
int nbPoints = 8;
int nbPointsDistincts = 36;
int points[] = {
1,1,1,
1,-1,-1,
1,-1,1,
1,1,-1,
-1,1,1,
-1,1,-1,
-1,-1,1,
-1,-1,-1
};
for (size_t i = 0; i < nbPoints; i++) {
m_positions.push_back(glm::vec3(points[3*i], points[3*i+1], points[3*i+2]));
m_colors.push_back(randomColor());
}
m_indices.push_back(glm::ivec3(0, 1, 2));
// m_indices.push_back(glm::ivec3(1));
// m_indices.push_back(glm::ivec3(2));
glGenBuffers(1, &m_pBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW);
glGenBuffers(1, &m_cBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer);
glBufferData(GL_ARRAY_BUFFER, m_colors.size()*sizeof(glm::vec4), m_colors.data(), GL_STATIC_DRAW);
glGenBuffers(1, &m_iBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size()*sizeof(glm::ivec3), m_indices.data(), GL_STATIC_DRAW);
}
void IndexedCubeRenderable::do_draw()
{
int modelLocation = m_shaderProgram->getUniformLocation("modelMat");
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(m_model));
int positionLocation = m_shaderProgram->getAttributeLocation("vPosition");
glEnableVertexAttribArray(positionLocation);
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
int colorLocation = m_shaderProgram->getAttributeLocation("inColor");
glEnableVertexAttribArray(colorLocation);
glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer);
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, &m_indices);
glDisableVertexAttribArray(positionLocation);
glDisableVertexAttribArray(colorLocation);
}
void IndexedCubeRenderable::do_animate(float time) {}
IndexedCubeRenderable::~IndexedCubeRenderable()
{
glDeleteBuffers(1, &m_pBuffer);
glDeleteBuffers(1, &m_cBuffer);
glDeleteBuffers(1, &m_iBuffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment