started work on CompoundShape
This commit is contained in:
@@ -18,6 +18,7 @@ sources = [
|
||||
'src/util.cpp',
|
||||
'src/settings.cpp',
|
||||
'src/Shape.cpp',
|
||||
'src/CompoundShape.cpp',
|
||||
'src/Shader.cpp',
|
||||
'src/Camera.cpp',
|
||||
|
||||
@@ -26,7 +27,7 @@ sources = [
|
||||
|
||||
cpp_args = [
|
||||
'-DVERTEX_SHADER_PATH="../src/shaders/shader.vert"',
|
||||
'-DFRAGMENT_SHADER_PATH="../src/shaders/shader.frag"'
|
||||
'-DFRAGMENT_SHADER_PATH="../src/shaders/shader.frag"',
|
||||
]
|
||||
|
||||
exe = executable(
|
||||
|
||||
24
src/CompoundShape.cpp
Normal file
24
src/CompoundShape.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "CompoundShape.hpp"
|
||||
|
||||
// constructors
|
||||
|
||||
CompoundShape::CompoundShape(const std::vector<Shape> shapes)
|
||||
: shapes { shapes }
|
||||
, transform { 1.0f } {
|
||||
|
||||
for (auto& shape : this->shapes) {
|
||||
shape.init();
|
||||
}
|
||||
}
|
||||
|
||||
// public
|
||||
|
||||
void CompoundShape::draw() {
|
||||
for (auto& shape : this->shapes) {
|
||||
glm::mat4 orig_transform { shape.transform };
|
||||
shape.transform *= shape.transform;
|
||||
shape.draw();
|
||||
shape.transform = orig_transform;
|
||||
}
|
||||
}
|
||||
|
||||
17
src/CompoundShape.hpp
Normal file
17
src/CompoundShape.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Shape.hpp"
|
||||
|
||||
class CompoundShape {
|
||||
public:
|
||||
std::vector<Shape> shapes;
|
||||
|
||||
CompoundShape(const std::vector<Shape> shapes);
|
||||
|
||||
void draw();
|
||||
|
||||
private:
|
||||
glm::mat4 transform;
|
||||
};
|
||||
@@ -16,12 +16,9 @@
|
||||
|
||||
Shape::Shape(
|
||||
std::shared_ptr<Shader> shader,
|
||||
ShapeInfo& shapeInfo,
|
||||
glm::vec3 pos
|
||||
ShapeInfo& shapeInfo
|
||||
)
|
||||
: pos { pos }
|
||||
, rotation { 0.0f, 0.0f, 0.0f }
|
||||
, scale { 1.0f, 1.0f, 1.0f }
|
||||
: transform { 1.0f }
|
||||
, vertices { shapeInfo.vertices }
|
||||
, indices { shapeInfo.indices }
|
||||
, vao { 0 }
|
||||
@@ -29,6 +26,17 @@ Shape::Shape(
|
||||
, ebo { 0 }
|
||||
, shader {shader} {
|
||||
|
||||
}
|
||||
|
||||
Shape::~Shape() {
|
||||
glDeleteVertexArrays(1, &this->vao);
|
||||
glDeleteBuffers(1, &this->vbo);
|
||||
glDeleteBuffers(1, &this->ebo);
|
||||
}
|
||||
|
||||
// public
|
||||
|
||||
void Shape::init() {
|
||||
int orig_vao { 0 };
|
||||
int orig_vbo { 0 };
|
||||
int orig_ebo { 0 };
|
||||
@@ -61,24 +69,27 @@ Shape::Shape(
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, orig_ebo);
|
||||
}
|
||||
|
||||
// private
|
||||
void Shape::reset_transform() {
|
||||
this->transform = glm::mat4 { 1.0f };
|
||||
}
|
||||
|
||||
void Shape::translate(const glm::vec3& translation) {
|
||||
this->transform = glm::translate(this->transform, translation);
|
||||
}
|
||||
|
||||
void Shape::rotate(const float rotation_rad, const glm::vec3& axis) {
|
||||
this->transform = glm::rotate(this->transform, rotation_rad, axis);
|
||||
}
|
||||
|
||||
void Shape::scale(const glm::vec3& scale_amount) {
|
||||
this->transform = glm::scale(this->transform, scale_amount);
|
||||
}
|
||||
|
||||
void Shape::draw() const {
|
||||
int orig_vao { 0 };
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &orig_vao);
|
||||
|
||||
glm::mat4 model { 1.0f };
|
||||
model = glm::translate(model, this->pos);
|
||||
for (int i { 0 }; i < this->rotation.length(); i++) {
|
||||
const float rot_amount { this->rotation[i] };
|
||||
if (rot_amount != 0.0f) {
|
||||
glm::vec3 rot_axis { 0.0f };
|
||||
rot_axis[i] = 1.0f;
|
||||
model = glm::rotate(model, glm::radians(rot_amount), rot_axis);
|
||||
}
|
||||
}
|
||||
model = glm::scale(model, this->scale);
|
||||
this->shader->set_mat4("model", model);
|
||||
this->shader->set_mat4("model", transform);
|
||||
|
||||
glBindVertexArray(this->vao);
|
||||
glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, nullptr);
|
||||
|
||||
21
src/headers/CompoundShape.hpp
Normal file
21
src/headers/CompoundShape.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Shape.hpp"
|
||||
|
||||
class CompoundShape {
|
||||
public:
|
||||
std::vector<Shape> shapes;
|
||||
|
||||
CompoundShape(const std::vector<Shape> shapes);
|
||||
|
||||
void draw() const;
|
||||
void reset_transform();
|
||||
void translate(const glm::vec3& translation);
|
||||
void rotate(const float rotation_rad, const glm::vec3& axis);
|
||||
void scale(const glm::vec3& scale_amount);
|
||||
|
||||
private:
|
||||
glm::mat4 transform;
|
||||
};
|
||||
17
src/headers/IShape.hpp
Normal file
17
src/headers/IShape.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class IShape {
|
||||
public:
|
||||
glm::mat4 transform;
|
||||
|
||||
virtual ~IShape() {};
|
||||
|
||||
virtual void init() = 0;
|
||||
virtual void draw() const = 0;
|
||||
virtual void reset_transform() = 0;
|
||||
virtual void translate(const glm::vec3& translation) = 0;
|
||||
virtual void rotate(const float rotation_rad, const glm::vec3& axis) = 0;
|
||||
virtual void scale(const glm::vec3& scale_amount) = 0;
|
||||
};
|
||||
@@ -14,17 +14,21 @@ struct ShapeInfo {
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
glm::vec3 pos;
|
||||
glm::vec3 rotation;
|
||||
glm::vec3 scale;
|
||||
glm::mat4 transform;
|
||||
|
||||
Shape(
|
||||
std::shared_ptr<Shader> shader,
|
||||
ShapeInfo& shapeInfo,
|
||||
glm::vec3 pos = { 0.0f, 0.0f, 0.0f }
|
||||
ShapeInfo& shapeInfo
|
||||
);
|
||||
|
||||
~Shape();
|
||||
|
||||
void init();
|
||||
void draw() const;
|
||||
void reset_transform();
|
||||
void translate(const glm::vec3& translation);
|
||||
void rotate(const float rotation_rad, const glm::vec3& axis);
|
||||
void scale(const glm::vec3& scale_amount);
|
||||
|
||||
private:
|
||||
std::vector<float> vertices;
|
||||
|
||||
30
src/main.cpp
30
src/main.cpp
@@ -3,6 +3,7 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
@@ -14,6 +15,7 @@
|
||||
#include "Camera.hpp"
|
||||
#include "Shader.hpp"
|
||||
#include "Shape.hpp"
|
||||
#include "CompoundShape.hpp"
|
||||
#include "quit.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
@@ -176,17 +178,22 @@ int main() {
|
||||
auto shader { std::make_shared<Shader>(settings.vertex_shader_path, settings.fragment_shader_path) };
|
||||
shader->use();
|
||||
|
||||
|
||||
Shape plane { shader, shapes.at("square") };
|
||||
plane.pos.y = -1.0f;
|
||||
plane.rotation.x = 90.0f;
|
||||
plane.scale *= 100;
|
||||
plane.init();
|
||||
plane.translate(glm::vec3 { 0.0f, -1.0f, 0.0f });
|
||||
plane.rotate(glm::radians(90.0f), glm::vec3 { 1.0f, 0.0f, 0.0f });
|
||||
plane.scale(glm::vec3 { 1.0f } * 100.0f);
|
||||
|
||||
Shape square { shader, shapes.at("square") };
|
||||
Shape cube { shader, shapes.at("cube") };
|
||||
Shape tri1 { shader, shapes.at("triangle") };
|
||||
Shape tri2 { shader, shapes.at("triangle") };
|
||||
tri2.rotate(glm::radians(180.0f), glm::vec3 { 0.0f, 0.0f, 1.0f });
|
||||
CompoundShape rectangle { {
|
||||
tri1, tri2
|
||||
} };
|
||||
|
||||
long unsigned int frame { 0 };
|
||||
double last_frame_time { 0 };
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
const double current_time { glfwGetTime() };
|
||||
delta = current_time - last_frame_time;
|
||||
@@ -220,15 +227,8 @@ int main() {
|
||||
shader->set_vec4("color", glm::normalize(glm::vec4 { 207.0f, 90.0f, 28.0f, 1.0f }));
|
||||
plane.draw();
|
||||
|
||||
shader->set_vec4("color", { 0.0f, 0.0f, 1.0f, 1.0f });
|
||||
square.pos.x = std::cos(current_time);
|
||||
square.pos.y = std::sin(current_time);
|
||||
square.rotation.z = glm::degrees(current_time) + 45.0f;
|
||||
square.draw();
|
||||
|
||||
// shader->set_vec4("color", { 1.0f, 1.0f, 1.0f, 1.0f });
|
||||
// cube.rotation.y += 45.0f * delta;
|
||||
// cube.draw();
|
||||
shader->set_vec4("color", glm::vec4 { 0.0f, 0.0f, 0.0f, 1.0f });
|
||||
rectangle.draw();
|
||||
|
||||
const double time_since_last_frame { glfwGetTime() - last_frame_time };
|
||||
if (time_since_last_frame < fps_lock) {
|
||||
|
||||
Reference in New Issue
Block a user