Класс групповой модели

This commit is contained in:
parent 2f38a89248
commit 4d986d5a38
2 changed files with 53 additions and 12 deletions

View File

@ -11,7 +11,21 @@
#define DEFAULT_MTL_DIR "./" #define DEFAULT_MTL_DIR "./"
void loadOBJtoVector(const char* filename, std::vector<class Model>& scene, const char* mtl_directory = DEFAULT_MTL_DIR, const char* texture_directory = DEFAULT_MTL_DIR); void loadOBJtoVector(const char* filename, std::vector<class Model>& 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: public:
Model(); // Конструктор без параметров Model(); // Конструктор без параметров
@ -25,11 +39,6 @@ class Model
void set_texture(Texture& texture); // Привязка текстуры к модели void set_texture(Texture& texture); // Привязка текстуры к модели
void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов
glm::vec3 position; // позиция модели
glm::vec3 rotation; // поворот модели
glm::vec3 scale; // мастабирование модели
glm::mat4 getTransformMatrix(); // Матрица трансформации модели
private: private:
VAO vao; VAO vao;
BO vertex_vbo, index_vbo; // вершинный и индексный буферы BO vertex_vbo, index_vbo; // вершинный и индексный буферы
@ -39,4 +48,14 @@ class Model
Texture texture_diffuse; // Диффузная текстура Texture texture_diffuse; // Диффузная текстура
}; };
// Класс сгруппированной модели
class GrouptedModel: public Movable
{
public:
void render(const GLuint &mvp_uniform); // Вызов отрисовки
std::vector<Model> parts;
Model& operator[](int index); // Оператор[i] для простого доступа к частям модели
};
#endif // MODEL_H #endif // MODEL_H

View File

@ -2,21 +2,23 @@
#include "Camera.h" #include "Camera.h"
extern Camera camera; 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), Model::Model() : verteces_count(0), first_index_byteOffset(0), indices_count(0),
vertex_vbo(VERTEX), index_vbo(ELEMENT), normals_vbo(VERTEX), texCoords_vbo(VERTEX), vertex_vbo(VERTEX), index_vbo(ELEMENT), normals_vbo(VERTEX), texCoords_vbo(VERTEX)
position(0), rotation(0), scale(1)
{ {
} }
// Конструктор копирования // Конструктор копирования
Model::Model(const Model& copy) : Model::Model(const Model& copy) : Movable(copy),
vao(copy.vao), vao(copy.vao),
verteces_count(copy.verteces_count), first_index_byteOffset(copy.first_index_byteOffset), indices_count(copy.indices_count), 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), 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), texture_diffuse(copy.texture_diffuse)
position(copy.position), rotation(copy.rotation), scale(copy.scale)
{ {
} }
@ -150,7 +152,7 @@ void Model::load_normals(glm::vec3* normals, GLuint count)
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
// Матрица трансформации модели // Матрица трансформации модели
glm::mat4 Model::getTransformMatrix() glm::mat4 Movable::getTransformMatrix()
{ {
glm::mat4 transformMatrix = glm::mat4(1.0f); glm::mat4 transformMatrix = glm::mat4(1.0f);
// Перемещение модели // Перемещение модели
@ -302,3 +304,23 @@ void loadOBJtoVector(const char* filename, std::vector<Model>& scene, const char
s->set_texture(diffuse); 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];
}