Правка материалов для PBR

This commit is contained in:
parent 126d153878
commit 69567053af
5 changed files with 76 additions and 44 deletions

View File

@ -15,12 +15,13 @@ void calc_tb(const GLuint* indices, const int indices_count, const glm::vec3* ve
struct Material struct Material
{ {
alignas(16) glm::vec3 ka; // коэф. фонового отражения (цвет фонового освещения) alignas(16) glm::vec3 base_color; // Базовый цвет материала
alignas(16) glm::vec3 kd; // коэф. диффузного отражения (цвет объекта) float roughness; // Шероховатость поверхности
alignas(16) glm::vec3 ks; // коэф. зеркального блика float metallic; // Металличность поверхности
float p; // показатель глянцевости float specular; // Интенсивность блика диэлектриков
alignas(16) glm::vec3 emitted; // Излучаемый поверхностью свет
// Значения по умолчанию // Значения по умолчанию
Material() : ka(0.2f), kd(0.2f), ks(0.2f), p(1) { }; Material() : base_color(0.8f), roughness(0.5f), metallic(0.0f), specular(0.5f), emitted(0.0f) { };
}; };
// Идентификатор модели // Идентификатор модели
@ -89,9 +90,11 @@ class Model : public Movable
BO *tangent_vbo, *bitangent_vbo; // буферы с касательными и бикасательными векторами BO *tangent_vbo, *bitangent_vbo; // буферы с касательными и бикасательными векторами
GLuint verteces_count; // Количество вершин GLuint verteces_count; // Количество вершин
GLuint first_index, indices_count; // Первый и количество индексов GLuint first_index, indices_count; // Первый и количество индексов
Texture texture_diffuse; // Диффузная текстура Texture texture_albedo; // Текстура альбедо (цвет поверхности)
Texture texture_ambient; // Текстура фонового освщения Texture texture_roughness; // Текстура шероховатостей
Texture texture_specular; // Текстура зеркального отражения Texture texture_metallic; // Текстура металличности
Texture texture_specular; // Текстура интенсивности блика диэлектриков
Texture texture_emitted; // Текстура излучаемого света
Texture texture_heights; // Текстура высот Texture texture_heights; // Текстура высот
Texture texture_normals; // Текстура нормалей Texture texture_normals; // Текстура нормалей
}; };

View File

@ -7,9 +7,11 @@
#include <string> #include <string>
enum TexType { enum TexType {
TEX_DIFFUSE, TEX_ALBEDO,
TEX_AMBIENT, TEX_ROUGHNESS,
TEX_METALLIC,
TEX_SPECULAR, TEX_SPECULAR,
TEX_EMITTED,
TEX_HEIGHTS, TEX_HEIGHTS,
TEX_NORMAL, TEX_NORMAL,
TEX_AVAILABLE_COUNT TEX_AVAILABLE_COUNT
@ -36,8 +38,8 @@ class Texture : public BaseTexture
{ {
public: public:
Texture(GLuint type = TEX_AVAILABLE_COUNT, const std::string& filename = ""); // Загрузка текстуры с диска или использование "пустой" Texture(GLuint type = TEX_AVAILABLE_COUNT, const std::string& filename = ""); // Загрузка текстуры с диска или использование "пустой"
Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере Texture(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере
Texture(GLuint width, GLuint height, void* data, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера без привязки к буферу с загрузкой пикселей по указателю Texture(GLuint width, GLuint height, void* data, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера без привязки к буферу с загрузкой пикселей по указателю
Texture(const Texture& other); // Конструктор копирования Texture(const Texture& other); // Конструктор копирования
Texture& operator=(const Texture& other); // Оператор присваивания Texture& operator=(const Texture& other); // Оператор присваивания
@ -49,7 +51,7 @@ class Texture : public BaseTexture
class TextureArray : public BaseTexture class TextureArray : public BaseTexture
{ {
public: public:
TextureArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере TextureArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере
TextureArray(const TextureArray& other); // Конструктор копирования TextureArray(const TextureArray& other); // Конструктор копирования
TextureArray& operator=(const TextureArray& other); // Оператор присваивания TextureArray& operator=(const TextureArray& other); // Оператор присваивания
@ -62,7 +64,7 @@ class TextureCube : public BaseTexture
{ {
public: public:
TextureCube(GLuint type = TEX_AVAILABLE_COUNT, const std::string (&filename)[6] = {""}); // Загрузка текстуры с диска или использование "пустой" TextureCube(GLuint type = TEX_AVAILABLE_COUNT, const std::string (&filename)[6] = {""}); // Загрузка текстуры с диска или использование "пустой"
TextureCube(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере TextureCube(GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере
TextureCube(const TextureCube& other); // Конструктор копирования TextureCube(const TextureCube& other); // Конструктор копирования
TextureCube& operator=(const TextureCube& other); // Оператор присваивания TextureCube& operator=(const TextureCube& other); // Оператор присваивания
@ -74,7 +76,7 @@ class TextureCube : public BaseTexture
class TextureCubeArray : public BaseTexture class TextureCubeArray : public BaseTexture
{ {
public: public:
TextureCubeArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере TextureCubeArray(GLuint levels, GLuint width, GLuint height, GLuint attachment, GLuint texType = TEX_ALBEDO, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Конструктор текстуры заданного размера для использования в буфере
TextureCubeArray(const TextureCubeArray& other); // Конструктор копирования TextureCubeArray(const TextureCubeArray& other); // Конструктор копирования
TextureCubeArray& operator=(const TextureCubeArray& other); // Оператор присваивания TextureCubeArray& operator=(const TextureCubeArray& other); // Оператор присваивания

View File

@ -51,7 +51,7 @@ void BulbDebug::render(ShaderProgram &shaderProgram, UBO &material_buffer)
glUniform3fv(direction_uniform, 1, &data->direction[0]); glUniform3fv(direction_uniform, 1, &data->direction[0]);
// Зададим параметры материала сфере действия // Зададим параметры материала сфере действия
bulb_model.parts[0].material.ka = data->color; bulb_model.parts[0].material.base_color = data->color;
bulb_model.parts[0].position = data->position; bulb_model.parts[0].position = data->position;
bulb_model.parts[0].scale = glm::vec3(data->radius); bulb_model.parts[0].scale = glm::vec3(data->radius);
@ -67,7 +67,7 @@ void BulbDebug::render(ShaderProgram &shaderProgram, UBO &material_buffer)
glUniform1f(angle_uniform, 180); // Зададим параметры материала сфере действия glUniform1f(angle_uniform, 180); // Зададим параметры материала сфере действия
// Зададим цвет для колбы (первая в составе модели) // Зададим цвет для колбы (первая в составе модели)
bulb_model.parts[1].material.ka = data->color; bulb_model.parts[1].material.base_color = data->color;
bulb_model.position = data->position; bulb_model.position = data->position;
glm::mat4 transform = bulb_model.getTransformMatrix(); glm::mat4 transform = bulb_model.getTransformMatrix();

View File

@ -35,7 +35,7 @@ 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),
tangent_vbo(copy.tangent_vbo), bitangent_vbo(copy.bitangent_vbo), tangent_vbo(copy.tangent_vbo), bitangent_vbo(copy.bitangent_vbo),
texture_diffuse(copy.texture_diffuse), texture_ambient(copy.texture_ambient), texture_specular(copy.texture_specular), texture_albedo(copy.texture_albedo), texture_roughness(copy.texture_roughness), texture_metallic(copy.texture_metallic), texture_specular(copy.texture_specular), texture_emitted(copy.texture_emitted),
texture_heights(copy.texture_heights), texture_normals(copy.texture_normals), material(copy.material), texture_heights(copy.texture_heights), texture_normals(copy.texture_normals), material(copy.material),
normalmapped(copy.normalmapped), parallaxmapped(copy.parallaxmapped), displacementmapped(copy.displacementmapped) normalmapped(copy.normalmapped), parallaxmapped(copy.parallaxmapped), displacementmapped(copy.displacementmapped)
{ {
@ -92,9 +92,11 @@ void Model::render(ShaderProgram &shaderProgram, UBO &material_buffer, const glm
glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &model[0][0]); glUniformMatrix4fv(model_uniform, 1, GL_FALSE, &model[0][0]);
// Подключаем текстуры // Подключаем текстуры
texture_diffuse.use(); texture_albedo.use();
texture_ambient.use(); texture_roughness.use();
texture_metallic.use();
texture_specular.use(); texture_specular.use();
texture_emitted.use();
texture_heights.use(); texture_heights.use();
texture_normals.use(); texture_normals.use();
@ -248,14 +250,25 @@ void Model::set_texture(Texture& texture)
GLuint type = texture.getType(); GLuint type = texture.getType();
switch(type) switch(type)
{ {
case TEX_DIFFUSE: case TEX_ALBEDO:
texture_diffuse = texture; texture_albedo = texture;
material.base_color.r = -1;
break; break;
case TEX_AMBIENT: case TEX_ROUGHNESS:
texture_ambient = texture; texture_roughness = texture;
material.roughness = -1;
break;
case TEX_METALLIC:
texture_metallic = texture;
material.metallic = -1;
break; break;
case TEX_SPECULAR: case TEX_SPECULAR:
texture_specular = texture; texture_specular = texture;
material.specular = -1;
break;
case TEX_EMITTED:
texture_emitted = texture;
material.emitted.r = -1;
break; break;
case TEX_HEIGHTS: case TEX_HEIGHTS:
texture_heights = texture; texture_heights = texture;
@ -501,23 +514,39 @@ GrouptedModel loadOBJtoGroupted(const char* filename, const char* mtl_directory,
auto s = --result.parts.end(); auto s = --result.parts.end();
s->set_index_range(materials_range[i], materials_range[i+1]-materials_range[i]); s->set_index_range(materials_range[i], materials_range[i+1]-materials_range[i]);
// Текстуры
Texture diffuse(TEX_DIFFUSE, texture_directory + materials[materials_ids[i]].diffuse_texname);
s->set_texture(diffuse);
Texture ambient(TEX_AMBIENT, texture_directory + materials[materials_ids[i]].ambient_texname);
s->set_texture(ambient);
Texture specular(TEX_SPECULAR, texture_directory + materials[materials_ids[i]].specular_texname);
s->set_texture(specular);
Texture normal(TEX_NORMAL, texture_directory + materials[materials_ids[i]].normal_texname);
s->set_texture(normal);
Texture heights(TEX_HEIGHTS, texture_directory + materials[materials_ids[i]].bump_texname);
s->set_texture(heights);
// Материал // Материал
s->material.ka = pow(glm::vec3(materials[materials_ids[i]].ambient[0], materials[materials_ids[i]].ambient[1], materials[materials_ids[i]].ambient[2]), glm::vec3(1/inv_gamma)); s->material.base_color = pow(glm::vec3(materials[materials_ids[i]].diffuse[0], materials[materials_ids[i]].diffuse[1], materials[materials_ids[i]].diffuse[2]), glm::vec3(1/inv_gamma));
s->material.kd = pow(glm::vec3(materials[materials_ids[i]].diffuse[0], materials[materials_ids[i]].diffuse[1], materials[materials_ids[i]].diffuse[2]), glm::vec3(1/inv_gamma)); s->material.roughness = 1 - sqrt(materials[materials_ids[i]].shininess/1000); // шероховатость поверхности
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.metallic = (materials[materials_ids[i]].ambient[0] + materials[materials_ids[i]].ambient[1] + materials[materials_ids[i]].ambient[2]) / 3.0f;
s->material.p = (materials[materials_ids[i]].shininess > 0.0f) ? 1000.0f / materials[materials_ids[i]].shininess : 1000.0f; s->material.specular = (materials[materials_ids[i]].specular[0] + materials[materials_ids[i]].specular[1] + materials[materials_ids[i]].specular[2]) / 3.0f;
s->material.emitted = pow(glm::vec3(materials[materials_ids[i]].emission[0], materials[materials_ids[i]].emission[1], materials[materials_ids[i]].emission[2]), glm::vec3(1/inv_gamma));
// Текстуры
if (!materials[materials_ids[i]].diffuse_texname.empty())
{
Texture diffuse(TEX_ALBEDO, texture_directory + materials[materials_ids[i]].diffuse_texname);
s->set_texture(diffuse);
}
if (!materials[materials_ids[i]].ambient_texname.empty())
{
Texture ambient(TEX_METALLIC, texture_directory + materials[materials_ids[i]].ambient_texname);
s->set_texture(ambient);
}
if (!materials[materials_ids[i]].specular_texname.empty())
{
Texture specular(TEX_SPECULAR, texture_directory + materials[materials_ids[i]].specular_texname);
s->set_texture(specular);
}
if (!materials[materials_ids[i]].normal_texname.empty())
{
Texture normal(TEX_NORMAL, texture_directory + materials[materials_ids[i]].normal_texname);
s->set_texture(normal);
}
if (!materials[materials_ids[i]].bump_texname.empty())
{
Texture heights(TEX_HEIGHTS, texture_directory + materials[materials_ids[i]].bump_texname);
s->set_texture(heights);
}
} }
return result; return result;

View File

@ -119,8 +119,6 @@ int main(void)
GrouptedModel scene = loadOBJtoGroupted("../resources/models/blob.obj", "../resources/models/", "../resources/textures/"); GrouptedModel scene = loadOBJtoGroupted("../resources/models/blob.obj", "../resources/models/", "../resources/textures/");
scene.scale = glm::vec3(0.01); scene.scale = glm::vec3(0.01);
scene.position.z = 1; scene.position.z = 1;
scene.parts[0].material.kd = {0.5,0.5,0.5};
scene.parts[0].material.ka = {0.05,0.05,0.05};
scene.set_group_id((GLuint64) &scene); scene.set_group_id((GLuint64) &scene);
// Установка цвета очистки буфера цвета // Установка цвета очистки буфера цвета
@ -320,7 +318,7 @@ int main(void)
rectangle.scale = glm::vec3(4); rectangle.scale = glm::vec3(4);
// Текстуры для прямоугольника // Текстуры для прямоугольника
Texture rectangle_diffuse(TEX_DIFFUSE, "../resources/textures/rekovalev_diffusemap.png"); Texture rectangle_diffuse(TEX_ALBEDO, "../resources/textures/rekovalev_diffusemap.png");
rectangle.set_texture(rectangle_diffuse); rectangle.set_texture(rectangle_diffuse);
Texture rectangle_normal(TEX_NORMAL, "../resources/textures/rekovalev_normalmap.png"); Texture rectangle_normal(TEX_NORMAL, "../resources/textures/rekovalev_normalmap.png");
rectangle.set_texture(rectangle_normal); rectangle.set_texture(rectangle_normal);
@ -375,7 +373,7 @@ int main(void)
// Модель скайбокса // Модель скайбокса
Model skybox; Model skybox;
skybox.load_verteces(skybox_verticies, sizeof(skybox_verticies)/sizeof(glm::vec3)); skybox.load_verteces(skybox_verticies, sizeof(skybox_verticies)/sizeof(glm::vec3));
TextureCube skybox_texture(TEX_DIFFUSE, { "../resources/textures/skybox/px.jpg" TextureCube skybox_texture(TEX_ALBEDO, { "../resources/textures/skybox/px.jpg"
, "../resources/textures/skybox/nx.jpg" , "../resources/textures/skybox/nx.jpg"
, "../resources/textures/skybox/py.jpg" , "../resources/textures/skybox/py.jpg"
, "../resources/textures/skybox/ny.jpg" , "../resources/textures/skybox/ny.jpg"