212 lines
5.6 KiB
C++

#include "ColorCube.h"
glm::vec3 computeNormal(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3)
{
glm::vec3 v1 = p2 - p1;
glm::vec3 v2 = p3 - p1;
return glm::normalize(glm::cross(v1, v2));
}
void ColorCube::iboSetup()
{
std::vector<glm::vec3> cube_vertices =
{ { -1.0, -1.0, 1.0},
{ 1.0, -1.0, 1.0},
{ 1.0, 1.0, 1.0},
{ -1.0, 1.0, 1.0},
{ -1.0, -1.0, -1.0},
{ 1.0, -1.0, -1.0},
{ 1.0, 1.0, -1.0},
{ -1.0, 1.0, -1.0} };
GLfloat cube_colors[] = {
// front colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
// back colors
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 1.0, 1.0,
};
GLushort cube_elements[] = {
0, 1, 2, 2, 3, 0, 1, 5, 6,
6, 2, 1, 7, 6, 5, 5, 4, 7,
4, 0, 3, 3, 7, 4, 4, 5, 1,
1, 0, 4, 3, 2, 6, 6, 7, 3,
};
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_vertices.size() * 3, cube_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &_vbo_colors);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, cube_colors, GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &_iboHandle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboHandle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
glBindVertexArray(0);
}
void ColorCube::vaoSetup()
{
std::vector<glm::vec3> cube_vertices =
{ { -1.0, -1.0, 1.0}, { 1.0, -1.0, 1.0}, { 1.0, 1.0, 1.0},
{ 1.0, 1.0, 1.0}, { -1.0, 1.0, 1.0}, { -1.0, -1.0, 1.0},
{ 1.0, -1.0, 1.0}, { 1.0, -1.0, -1.0}, { 1.0, 1.0, -1.0},
{ 1.0, 1.0, -1.0}, { 1.0, 1.0, 1.0}, { 1.0, -1.0, 1.0},
{ -1.0, 1.0, -1.0}, { 1.0, 1.0, -1.0}, { 1.0, -1.0, -1.0},
{ 1.0, -1.0, -1.0}, { -1.0, -1.0, -1.0}, { -1.0, 1.0, -1.0},
{ -1.0, -1.0, -1.0}, { -1.0, -1.0, 1.0}, { -1.0, 1.0, 1.0},
{ -1.0, 1.0, 1.0}, { -1.0, 1.0, -1.0}, { -1.0, -1.0, -1.0},
{ -1.0, -1.0, -1.0}, { 1.0, -1.0, -1.0}, { 1.0, -1.0, 1.0},
{ 1.0, -1.0, 1.0}, { -1.0, -1.0, 1.0}, { -1.0, -1.0, -1.0},
{ -1.0, 1.0, 1.0}, { 1.0, 1.0, 1.0}, { 1.0, 1.0, -1.0},
{ 1.0, 1.0, -1.0}, { -1.0, 1.0, -1.0}, { -1.0, 1.0, 1.0} };
std::vector<GLfloat> cube_colors = {
1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
};
std::vector<glm::vec3> cube_normals;
for (int i = 0; i < cube_vertices.size() - 2; i += 3)
{
glm::vec3 norm = computeNormal(
cube_vertices[i], cube_vertices[i + 1], cube_vertices[i + 2]);
cube_normals.push_back(norm); cube_normals.push_back(norm); cube_normals.push_back(norm);
}
glGenVertexArrays(1, &_vaoHandle);
glBindVertexArray(_vaoHandle);
glGenBuffers(1, &_vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_vertices.size() * 3, cube_vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0, //attr number = 0
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(0); //attr number = 0
glGenBuffers(1, &_vbo_normals);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_normals);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_normals.size() * 3, cube_normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &_vbo_colors);
glBindBuffer(GL_ARRAY_BUFFER, _vbo_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * cube_colors.size(), cube_colors.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
2,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
ColorCube::ColorCube(bool usingIBO)
{
ibo_used = usingIBO;
if (usingIBO)
iboSetup();
else
vaoSetup();
}
void ColorCube::draw(ShaderProgram *shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
{
_model.glPushMatrix();
effectTransformations();
glBindVertexArray(_vaoHandle);
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
glm::mat4 modelview = view_matrix * _model.getMatrix();
_model.glPopMatrix();
glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(50, 50, 50, 1)));
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
glm::mat4 inverseModelView = glm::inverse(modelview);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
if (ibo_used)
{
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
}
else
glDrawArrays(GL_TRIANGLES, 0, 36);
}
DrawableType ColorCube::getType()
{
return DrawableType::COLOR_CUBE;
}
ColorCube::~ColorCube()
{
}