-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterial.cpp
97 lines (84 loc) · 3.26 KB
/
Material.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "Material.h"
#include <iostream>
#include <SOIL/SOIL.h>
#pragma comment(lib, "SOIL.lib")
Material::Material()
{
}
Material::~Material()
{
}
Material::Material(tinyobj::material_t const& material)
{
if (!material.diffuse_texname.empty())
{
std::string diffusePath = "./Resources/" + material.diffuse_texname;
m_albeidoTexture = SOIL_load_OGL_texture(diffusePath.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB);
if (m_albeidoTexture == 0)
{
std::cerr << "Failed to load texture from file | ";
std::cerr << SOIL_last_result() << std::endl;
}
glBindTexture(GL_TEXTURE_2D, m_albeidoTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
if (!material.bump_texname.empty())
{
std::string bumpPath = "./Resources/" + material.bump_texname;
m_normalTexture = SOIL_load_OGL_texture(bumpPath.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB);
if (m_normalTexture == 0)
{
std::cerr << "Failed to load texture from file | ";
std::cerr << SOIL_last_result() << std::endl;
}
glBindTexture(GL_TEXTURE_2D, m_normalTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
if (!material.roughness_texname.empty())
{
std::string roughnessPath = "./Resources/" + material.roughness_texname;
m_roughnessTexture = SOIL_load_OGL_texture(roughnessPath.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y);
if (m_roughnessTexture == 0)
{
std::cerr << "Failed to load texture from file | ";
std::cerr << SOIL_last_result() << std::endl;
}
glBindTexture(GL_TEXTURE_2D, m_roughnessTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
if (!material.metallic_texname.empty())
{
std::string metallicPath = "./Resources/" + material.metallic_texname;
m_metallicIoRTexture = SOIL_load_OGL_texture(metallicPath.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB);
if (m_metallicIoRTexture == 0)
{
std::cerr << "Failed to load texture from file | ";
std::cerr << SOIL_last_result() << std::endl;
}
glBindTexture(GL_TEXTURE_2D, m_metallicIoRTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
}
Material::Material(glm::vec4 const& color, float metallic, float roughness, float IOR)
{
m_properties.color = color;
m_properties.metallic = metallic;
m_properties.roughness = roughness;
m_properties.IoR = IOR;
m_properties.useTextures = false;
}
void Material::Bind()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_albeidoTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_normalTexture);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_metallicIoRTexture);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, m_roughnessTexture);
}