From 4d986d5a3875afdd382c28f65baddb87e32b82be Mon Sep 17 00:00:00 2001 From: "R.E. Kovalev" Date: Fri, 31 Mar 2023 11:57:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=20=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=BF=D0=BF=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Model.h | 31 +++++++++++++++++++++++++------ src/Model.cpp | 34 ++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/include/Model.h b/include/Model.h index 8927609..11a80c7 100644 --- a/include/Model.h +++ b/include/Model.h @@ -11,7 +11,21 @@ #define DEFAULT_MTL_DIR "./" void loadOBJtoVector(const char* filename, std::vector& scene, const char* mtl_directory = DEFAULT_MTL_DIR, const char* texture_directory = DEFAULT_MTL_DIR); -class Model +// Класс определяющий положение, вращение и размер объекта +class Movable +{ + public: + Movable(); // Конструктор без параметров + Movable(const Movable& copy); // Конструктор копирования + + glm::vec3 position; // позиция модели + glm::vec3 rotation; // поворот модели + glm::vec3 scale; // масштабирование модели + glm::mat4 getTransformMatrix(); // Матрица трансформации модели +}; + +// Класс модели с примененным материалом +class Model : public Movable { public: Model(); // Конструктор без параметров @@ -25,11 +39,6 @@ class Model void set_texture(Texture& texture); // Привязка текстуры к модели void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов - glm::vec3 position; // позиция модели - glm::vec3 rotation; // поворот модели - glm::vec3 scale; // мастабирование модели - glm::mat4 getTransformMatrix(); // Матрица трансформации модели - private: VAO vao; BO vertex_vbo, index_vbo; // вершинный и индексный буферы @@ -39,4 +48,14 @@ class Model Texture texture_diffuse; // Диффузная текстура }; +// Класс сгруппированной модели +class GrouptedModel: public Movable +{ + public: + void render(const GLuint &mvp_uniform); // Вызов отрисовки + std::vector parts; + + Model& operator[](int index); // Оператор[i] для простого доступа к частям модели +}; + #endif // MODEL_H diff --git a/src/Model.cpp b/src/Model.cpp index c71b7d1..79329c5 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -2,21 +2,23 @@ #include "Camera.h" extern Camera camera; +Movable::Movable() : position(0), rotation(0), scale(1) {} +// Конструктор копирования +Movable::Movable(const Movable& copy) : position(copy.position), rotation(copy.rotation), scale(copy.scale) {} + // Конструктор без параметров Model::Model() : verteces_count(0), first_index_byteOffset(0), indices_count(0), -vertex_vbo(VERTEX), index_vbo(ELEMENT), normals_vbo(VERTEX), texCoords_vbo(VERTEX), -position(0), rotation(0), scale(1) +vertex_vbo(VERTEX), index_vbo(ELEMENT), normals_vbo(VERTEX), texCoords_vbo(VERTEX) { } // Конструктор копирования -Model::Model(const Model& copy) : +Model::Model(const Model& copy) : Movable(copy), vao(copy.vao), verteces_count(copy.verteces_count), first_index_byteOffset(copy.first_index_byteOffset), indices_count(copy.indices_count), vertex_vbo(copy.vertex_vbo), index_vbo(copy.index_vbo), normals_vbo(copy.normals_vbo), texCoords_vbo(copy.texCoords_vbo), -texture_diffuse(copy.texture_diffuse), -position(copy.position), rotation(copy.rotation), scale(copy.scale) +texture_diffuse(copy.texture_diffuse) { } @@ -150,7 +152,7 @@ void Model::load_normals(glm::vec3* normals, GLuint count) #include // Матрица трансформации модели -glm::mat4 Model::getTransformMatrix() +glm::mat4 Movable::getTransformMatrix() { glm::mat4 transformMatrix = glm::mat4(1.0f); // Перемещение модели @@ -302,3 +304,23 @@ void loadOBJtoVector(const char* filename, std::vector& scene, const char s->set_texture(diffuse); } } + +// Вызов отрисовки групповой модели +void GrouptedModel::render(const GLuint &mvp_uniform) +{ + for (auto& model : parts) + { + model.position += position; + model.rotation += rotation; + model.scale *= scale; + model.render(mvp_uniform); + model.position -= position; + model.rotation -= rotation; + model.scale /= scale; + } +} + +Model& GrouptedModel::operator[](int index) +{ + return parts[index]; +}