Класс групповой модели
This commit is contained in:
parent
51792bba34
commit
1770c6518b
@ -11,13 +11,27 @@
|
||||
#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);
|
||||
|
||||
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(); // Конструктор без параметров
|
||||
Model(const Model& copy); // Конструктор копирования
|
||||
~Model();
|
||||
void render(const GLuint &mvp_uniform); // Вызов отрисовки
|
||||
void render(const GLuint &mvp_uniform, const glm::mat4& global_tranform = glm::mat4(1)); // Вызов отрисовки
|
||||
void load_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер
|
||||
void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер
|
||||
void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер
|
||||
@ -25,11 +39,6 @@ class Model
|
||||
void set_texture(Texture& texture); // Привязка текстуры к модели
|
||||
void set_index_range(GLuint beg, GLuint count); // Ограничение диапазона из буфера индексов
|
||||
|
||||
glm::vec3 position; // позиция модели
|
||||
glm::vec3 rotation; // поворот модели
|
||||
glm::vec3 scale; // мастабирование модели
|
||||
glm::mat4 getTransformMatrix(); // Матрица трансформации модели
|
||||
|
||||
private:
|
||||
VAO vao;
|
||||
BO vertex_vbo, index_vbo; // вершинный и индексный буферы
|
||||
@ -39,4 +48,12 @@ class Model
|
||||
Texture texture_diffuse; // Диффузная текстура
|
||||
};
|
||||
|
||||
// Класс сгруппированной модели
|
||||
class GrouptedModel: public Movable
|
||||
{
|
||||
public:
|
||||
void render(const GLuint &mvp_uniform); // Вызов отрисовки
|
||||
std::vector<Model> parts;
|
||||
};
|
||||
|
||||
#endif // MODEL_H
|
||||
|
@ -2,10 +2,13 @@
|
||||
#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(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)
|
||||
{
|
||||
|
||||
}
|
||||
@ -15,8 +18,7 @@ Model::Model(const Model& copy) :
|
||||
vao(copy.vao),
|
||||
verteces_count(copy.verteces_count), first_index(copy.first_index), 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)
|
||||
{
|
||||
|
||||
}
|
||||
@ -27,10 +29,10 @@ Model::~Model()
|
||||
}
|
||||
|
||||
// Вызов отрисовки
|
||||
void Model::render(const GLuint &mvp_uniform)
|
||||
void Model::render(const GLuint &mvp_uniform, const glm::mat4& global_tranform)
|
||||
{
|
||||
// Расчитаем матрицу трансформации
|
||||
glm::mat4 mvp = camera.getVP() * this->getTransformMatrix();
|
||||
glm::mat4 mvp = camera.getVP() * global_tranform * this->getTransformMatrix();
|
||||
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, &mvp[0][0]);
|
||||
|
||||
// Подключаем текстуры
|
||||
@ -147,7 +149,7 @@ void Model::load_normals(glm::vec3* normals, GLuint count)
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
// Матрица трансформации модели
|
||||
glm::mat4 Model::getTransformMatrix()
|
||||
glm::mat4 Movable::getTransformMatrix()
|
||||
{
|
||||
glm::mat4 transformMatrix = glm::mat4(1.0f);
|
||||
// Перемещение модели
|
||||
@ -299,3 +301,11 @@ void loadOBJtoVector(const char* filename, std::vector<Model>& scene, const char
|
||||
s->set_texture(diffuse);
|
||||
}
|
||||
}
|
||||
|
||||
// Вызов отрисовки групповой модели
|
||||
void GrouptedModel::render(const GLuint &mvp_uniform)
|
||||
{
|
||||
glm::mat4 transform = this->getTransformMatrix();
|
||||
for (auto& model : parts)
|
||||
model.render(mvp_uniform, transform);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user