#include #include #include #include #include #include #include #include using std::vector; using glm::vec3; using glm::mat4; Line::Line(const vec3 p, const vec3 r, const vec3 s, vector v, vector i) { this->position = p; this->rotation = r; this->scale = s; this->vertices = v; this->indices = i; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW); glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); } void Line::draw(Shader s) { /*mat4 mod = rotationMatrix(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { std::cout << mod[i][j] << "\t"; } std::cout << std::endl; }*/ s.setMat4("modelMatrix", &modelMatrix()[0][0]); s.use(); glBindVertexArray(VAO); glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, 0); glBindVertexArray(0); } mat4 Line::translationMatrix() { return mat4( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, position.x, position.y, position.z, 1 ); } mat4 Line::rotationMatrix() { mat4 x( 1, 0, 0, 0, 0, cos(rotation.x), sin(rotation.x), 0, 0, -sin(rotation.x), cos(rotation.x), 0, 0, 0, 0, 1 ); mat4 y( cos(rotation.y), 0, -sin(rotation.y), 0, 0, 1, 0, 0, sin(rotation.y), 0, cos(rotation.y), 0, 0, 0, 0, 1 ); mat4 z( cos(rotation.z), sin(rotation.z), 0, 0, -sin(rotation.z), cos(rotation.z), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); return z * y * x; } mat4 Line::scaleMatrix() { return mat4( scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1 ); } mat4 Line::modelMatrix() { return translationMatrix() * rotationMatrix() * scaleMatrix(); }