Загрузка материала

This commit is contained in:
2022-11-23 14:11:12 +03:00
parent 290f609dce
commit e9dddd4dbb
6 changed files with 43 additions and 8 deletions

View File

@@ -212,7 +212,8 @@ Model::Model(const Model& copy) : Node(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)
texture_diffuse(copy.texture_diffuse),
material(copy.material)
{
}
@@ -233,6 +234,8 @@ Model& Model::operator=(const Model& other)
texture_diffuse = other.texture_diffuse;
material = other.material;
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]);
@@ -250,6 +253,9 @@ void Model::render(const GLuint &model_uniform)
// Подключаем текстуры
texture_diffuse.use();
// Загружаем данные о материале
material_buffer.load(&material, sizeof(material));
// Подключаем VAO
vao.use();
// Если есть индексы - рисуем с их использованием

View File

@@ -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)
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();
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);
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;

View File

@@ -93,6 +93,7 @@ int main(void)
const char* textures_base_shader_names[] = {"tex_diffuse"};
base.bindTextures(textures_base_shader_names, sizeof(textures_base_shader_names)/sizeof(const char*));
// Загрузка сцены из obj файла
Scene scene = loadOBJtoScene("../resources/models/cubes.obj", "../resources/models/", "../resources/textures/");
@@ -104,6 +105,7 @@ int main(void)
// Uniform-буферы
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
@@ -118,7 +120,7 @@ int main(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Тут производится рендер
scene.render(model_uniform);
scene.render(model_uniform, material_data);
// Представление содержимого буфера цепочки показа на окно
glfwSwapBuffers(window);