#include "CheckeredFloor.h" void CheckeredFloor::genVertices(std::vector &vertices, std::vector &colors) { float maxX = _size / 2.f, maxY = _size / 2.f; float minX = -_size / 2.f, minY = -_size / 2.f; float side_size = _size / (float)_squares; int color = 1; for (float x = minX; x < maxX; x += side_size) { for (float y = minY; y < maxY; y += side_size) { color *= -1; glm::vec3 tile_color = (color > 0) ? _light_color : _dark_color; //gen 1 carré vertices.emplace_back(x, 0, y, 1); //upleft vertices.emplace_back(x + side_size, 0, y, 1); //upright vertices.emplace_back(x, 0, y + side_size, 1); //downleft vertices.emplace_back(x + side_size, 0, y, 1); //upright vertices.emplace_back(x, 0, y + side_size, 1); //downleft vertices.emplace_back(x + side_size, 0, y + side_size, 1); //downright for (int z = 0; z < 6; z++) colors.push_back(tile_color); } color *= -1; } } void CheckeredFloor::setup() { std::vector floor_vertices; std::vector floor_colors; glGenVertexArrays(1, &_vaoHandle); glBindVertexArray(_vaoHandle); genVertices(floor_vertices, floor_colors); glGenBuffers(1, &_vbo_vertices); glBindBuffer(GL_ARRAY_BUFFER, _vbo_vertices); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * floor_vertices.size() * 4, floor_vertices.data(), GL_STATIC_DRAW); glVertexAttribPointer( 0, //attr number = 0 4, 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) * floor_colors.size() * 3, floor_colors.data(), GL_STATIC_DRAW); glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, (void *)0); glEnableVertexAttribArray(1); glBindVertexArray(0); } CheckeredFloor::CheckeredFloor(int size, int squares) : _size(size), _squares(squares) { setup(); } CheckeredFloor::~CheckeredFloor() { } void CheckeredFloor::draw(ShaderProgram * shader, glm::mat4x4 pv) { _model.glPushMatrix(); effectTransformations(); glBindVertexArray(_vaoHandle); glm::mat4 mvpMatrix = pv * _model.getMatrix(); glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix)); glDrawArrays(GL_TRIANGLES, 0, _squares * _squares * 6); _model.glPopMatrix(); } DrawableType CheckeredFloor::getType() { return DrawableType::CHECKERED_FLOOR; }