Загрузка материала
This commit is contained in:
		
							parent
							
								
									58ceffc40e
								
							
						
					
					
						commit
						9c60da7665
					
				| @ -51,6 +51,17 @@ class Node | |||||||
|         void invalidateParent(); // Проход потомков в глубину с изменением флага parent_changed
 |         void invalidateParent(); // Проход потомков в глубину с изменением флага parent_changed
 | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | // Материал модели
 | ||||||
|  | struct Material | ||||||
|  | { | ||||||
|  |     alignas(16) glm::vec3 ka; // коэф. фонового отражения (цвет фонового освещения)
 | ||||||
|  |     alignas(16) glm::vec3 kd; // коэф. диффузного отражения (цвет объекта)
 | ||||||
|  |     alignas(16) glm::vec3 ks; // коэф. зеркального блика
 | ||||||
|  |     float p; // показатель глянцевости
 | ||||||
|  |     // Значения по умолчанию
 | ||||||
|  |     Material() : ka(0.2f), kd(0.2f), ks(0.2f), p(1) { }; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| // Класс модели
 | // Класс модели
 | ||||||
| class Model : public Node | class Model : public Node | ||||||
| { | { | ||||||
| @ -60,7 +71,7 @@ class Model : public Node | |||||||
|         Model& operator=(const Model& other); // Оператор присваивания
 |         Model& operator=(const Model& other); // Оператор присваивания
 | ||||||
|         virtual ~Model(); |         virtual ~Model(); | ||||||
| 
 | 
 | ||||||
|         void render(const GLuint &model_uniform); // Вызов отрисовки
 |         void render(const GLuint &model_uniform, UBO &material_buffer); // Вызов отрисовки
 | ||||||
| 
 | 
 | ||||||
|         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); // Загрузка индексов в буфер
 | ||||||
| @ -69,6 +80,7 @@ class Model : public Node | |||||||
|         void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов
 |         void set_index_range(size_t first_byteOffset, size_t count); // Ограничение диапазона из буфера индексов
 | ||||||
|         void set_texture(Texture& texture); // Привязка текстуры к модели
 |         void set_texture(Texture& texture); // Привязка текстуры к модели
 | ||||||
|          |          | ||||||
|  |         Material material; // Материал модели
 | ||||||
|     private: |     private: | ||||||
|         VAO vao; |         VAO vao; | ||||||
|         BO vertex_vbo, index_vbo; // вершинный и индексный буферы
 |         BO vertex_vbo, index_vbo; // вершинный и индексный буферы
 | ||||||
|  | |||||||
| @ -17,7 +17,7 @@ class Scene | |||||||
|         Scene(const Scene ©); // Конструктор копирования
 |         Scene(const Scene ©); // Конструктор копирования
 | ||||||
|         Scene& operator=(const Scene& other); // Оператор присваивания
 |         Scene& operator=(const Scene& other); // Оператор присваивания
 | ||||||
| 
 | 
 | ||||||
|         void render(const GLuint &model_uniform); // Рендер сцены
 |         void render(const GLuint &model_uniform, UBO &material_buffer); // Рендер сцены
 | ||||||
| 
 | 
 | ||||||
|         Node root; // Корневой узел
 |         Node root; // Корневой узел
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,7 +1,15 @@ | |||||||
| #version 330 core  | #version 420 core  | ||||||
| 
 | 
 | ||||||
| in vec2 texCoord; | in vec2 texCoord; | ||||||
| 
 | 
 | ||||||
|  | layout(std140, binding = 1) uniform Material | ||||||
|  | { | ||||||
|  |     vec3 ka; | ||||||
|  |     vec3 kd; | ||||||
|  |     vec3 ks; | ||||||
|  |     float p; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| uniform sampler2D tex_diffuse; | uniform sampler2D tex_diffuse; | ||||||
| 
 | 
 | ||||||
| out vec4 color;  | out vec4 color;  | ||||||
|  | |||||||
| @ -212,7 +212,8 @@ Model::Model(const Model& copy) : Node(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),  | ||||||
|  | material(copy.material) | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| @ -233,6 +234,8 @@ Model& Model::operator=(const Model& other) | |||||||
| 
 | 
 | ||||||
|     texture_diffuse = other.texture_diffuse; |     texture_diffuse = other.texture_diffuse; | ||||||
|      |      | ||||||
|  |     material = other.material; | ||||||
|  | 
 | ||||||
|     return *this; |     return *this; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -242,7 +245,7 @@ Model::~Model() | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Вызов отрисовки
 | // Вызов отрисовки
 | ||||||
| void Model::render(const GLuint &model_uniform)  | void Model::render(const GLuint &model_uniform, UBO &material_buffer)  | ||||||
| { | { | ||||||
|     // Загрузим матрицу трансформации
 |     // Загрузим матрицу трансформации
 | ||||||
|     glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &getTransformMatrix()[0][0]); |     glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &getTransformMatrix()[0][0]); | ||||||
| @ -250,6 +253,9 @@ void Model::render(const GLuint &model_uniform) | |||||||
|     // Подключаем текстуры
 |     // Подключаем текстуры
 | ||||||
|     texture_diffuse.use(); |     texture_diffuse.use(); | ||||||
| 
 | 
 | ||||||
|  |     // Загружаем данные о материале
 | ||||||
|  |     material_buffer.load(&material, sizeof(material)); | ||||||
|  | 
 | ||||||
|     // Подключаем VAO
 |     // Подключаем VAO
 | ||||||
|     vao.use(); |     vao.use(); | ||||||
|     // Если есть индексы - рисуем с их использованием
 |     // Если есть индексы - рисуем с их использованием
 | ||||||
|  | |||||||
| @ -27,10 +27,10 @@ Scene& Scene::operator=(const Scene& other) | |||||||
| }  | }  | ||||||
| 
 | 
 | ||||||
| // Рендер сцены
 | // Рендер сцены
 | ||||||
| void Scene::render(const GLuint &model_uniform)  | void Scene::render(const GLuint &model_uniform, UBO &material_buffer)  | ||||||
| { | { | ||||||
|     for (auto & model : models) |     for (auto & model : models) | ||||||
|         model.render(model_uniform); |         model.render(model_uniform, material_buffer); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Перестройка узлов выбранного списка
 | // Перестройка узлов выбранного списка
 | ||||||
| @ -205,8 +205,15 @@ Scene loadOBJtoScene(const char* filename, const char* mtl_directory, const char | |||||||
|         auto s = --result.models.end(); |         auto s = --result.models.end(); | ||||||
|         s->set_index_range(materials_range[i]*sizeof(GLuint), materials_range[i+1]-materials_range[i]); |         s->set_index_range(materials_range[i]*sizeof(GLuint), materials_range[i+1]-materials_range[i]); | ||||||
| 
 | 
 | ||||||
|  |         // Текстуры
 | ||||||
|         Texture diffuse(TEX_DIFFUSE, texture_directory + materials[materials_ids[i]].diffuse_texname); |         Texture diffuse(TEX_DIFFUSE, texture_directory + materials[materials_ids[i]].diffuse_texname); | ||||||
|         s->set_texture(diffuse); |         s->set_texture(diffuse); | ||||||
|  | 
 | ||||||
|  |         // Материал
 | ||||||
|  |         s->material.ka = glm::vec3(materials[materials_ids[i]].ambient[0],  materials[materials_ids[i]].ambient[1],  materials[materials_ids[i]].ambient[2]); | ||||||
|  |         s->material.kd = glm::vec3(materials[materials_ids[i]].diffuse[0],  materials[materials_ids[i]].diffuse[1],  materials[materials_ids[i]].diffuse[2]); | ||||||
|  |         s->material.ks = glm::vec3(materials[materials_ids[i]].specular[0], materials[materials_ids[i]].specular[1], materials[materials_ids[i]].specular[2]); | ||||||
|  |         s->material.p  = (materials[materials_ids[i]].shininess > 0.0f) ? 1000.0f / materials[materials_ids[i]].shininess : 1000.0f; | ||||||
|     }     |     }     | ||||||
| 
 | 
 | ||||||
|     return result; |     return result; | ||||||
|  | |||||||
| @ -93,6 +93,7 @@ int main(void) | |||||||
|     const char* textures_base_shader_names[] = {"tex_diffuse"}; |     const char* textures_base_shader_names[] = {"tex_diffuse"}; | ||||||
|     base.bindTextures(textures_base_shader_names, sizeof(textures_base_shader_names)/sizeof(const char*)); |     base.bindTextures(textures_base_shader_names, sizeof(textures_base_shader_names)/sizeof(const char*)); | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|     // Загрузка сцены из obj файла
 |     // Загрузка сцены из obj файла
 | ||||||
|     Scene scene = loadOBJtoScene("../resources/models/cubes.obj", "../resources/models/", "../resources/textures/");  |     Scene scene = loadOBJtoScene("../resources/models/cubes.obj", "../resources/models/", "../resources/textures/");  | ||||||
|      |      | ||||||
| @ -104,6 +105,7 @@ int main(void) | |||||||
| 
 | 
 | ||||||
|     // Uniform-буферы
 |     // Uniform-буферы
 | ||||||
|     UBO cameraUB(sizeof(glm::mat4)*2, 0); |     UBO cameraUB(sizeof(glm::mat4)*2, 0); | ||||||
|  |     UBO material_data(sizeof(Material), 1); | ||||||
| 
 | 
 | ||||||
|     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Использование уменьшенных версий mipmap  
 |     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Использование уменьшенных версий mipmap  
 | ||||||
| 
 | 
 | ||||||
| @ -118,7 +120,7 @@ int main(void) | |||||||
|         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||||||
| 
 | 
 | ||||||
|         // Тут производится рендер
 |         // Тут производится рендер
 | ||||||
|         scene.render(model_uniform); |         scene.render(model_uniform, material_data); | ||||||
|          |          | ||||||
|         // Представление содержимого буфера цепочки показа на окно
 |         // Представление содержимого буфера цепочки показа на окно
 | ||||||
|         glfwSwapBuffers(window); |         glfwSwapBuffers(window); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user