Класс групповой модели
This commit is contained in:
		
							parent
							
								
									51792bba34
								
							
						
					
					
						commit
						1770c6518b
					
				| @ -11,13 +11,27 @@ | |||||||
| #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(); // Конструктор без параметров
 | ||||||
|         Model(const Model& copy); // Конструктор копирования
 |         Model(const Model& copy); // Конструктор копирования
 | ||||||
|         ~Model(); |         ~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_verteces(glm::vec3* verteces, GLuint count); // Загрузка вершин в буфер
 | ||||||
|         void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер
 |         void load_indices(GLuint* indices, GLuint count); // Загрузка индексов в буфер
 | ||||||
|         void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер
 |         void load_texCoords(glm::vec2* texCoords, GLuint count); // Загрузка текстурных координат в буфер
 | ||||||
| @ -25,11 +39,6 @@ class Model | |||||||
|         void set_texture(Texture& texture); // Привязка текстуры к модели
 |         void set_texture(Texture& texture); // Привязка текстуры к модели
 | ||||||
|         void set_index_range(GLuint beg, GLuint count); // Ограничение диапазона из буфера индексов
 |         void set_index_range(GLuint beg, GLuint 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,12 @@ class Model | |||||||
|         Texture texture_diffuse; // Диффузная текстура
 |         Texture texture_diffuse; // Диффузная текстура
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | // Класс сгруппированной модели
 | ||||||
|  | class GrouptedModel: public Movable | ||||||
|  | { | ||||||
|  |     public: | ||||||
|  |         void render(const GLuint &mvp_uniform); // Вызов отрисовки
 | ||||||
|  |         std::vector<Model> parts; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| #endif // MODEL_H
 | #endif // MODEL_H
 | ||||||
|  | |||||||
| @ -2,10 +2,13 @@ | |||||||
| #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(0), indices_count(0),  | Model::Model() : verteces_count(0), first_index(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) |  | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| @ -15,8 +18,7 @@ Model::Model(const Model& copy) : | |||||||
| vao(copy.vao),  | vao(copy.vao),  | ||||||
| verteces_count(copy.verteces_count), first_index(copy.first_index), indices_count(copy.indices_count),  | 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),  | 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) |  | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| @ -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]); |     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> | #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); | ||||||
|     // Перемещение модели
 |     // Перемещение модели
 | ||||||
| @ -299,3 +301,11 @@ 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) | ||||||
|  | { | ||||||
|  |     glm::mat4 transform = this->getTransformMatrix(); | ||||||
|  |     for (auto& model : parts) | ||||||
|  |         model.render(mvp_uniform, transform); | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user