PBR is now implemented

This commit is contained in:
Warwick 2022-07-25 13:22:42 +01:00
parent 4ee8d24f18
commit 2cd954dc9a
6 changed files with 31 additions and 40 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB

View file

@ -14,5 +14,7 @@ map_Bump wooden box and barrel/boxes_DefaultMaterial_Normal.png
map_Kd wooden box and barrel/boxes_DefaultMaterial_BaseColor.png map_Kd wooden box and barrel/boxes_DefaultMaterial_BaseColor.png
map_Pr wooden box and barrel/boxes_DefaultMaterial_Roughness.jpg map_Pr wooden box and barrel/boxes_DefaultMaterial_Roughness.jpg
map_Pm wooden box and barrel/boxes_DefaultMaterial_Metallic.jpg map_Pm wooden box and barrel/boxes_DefaultMaterial_Metallic.jpg
map_Ps wooden box and barrel/boxes_DefaultMaterial_AO.jpg # map_Ps wooden box and barrel/boxes_DefaultMaterial_AO.jpg
map_RMA wooden box and barrel/boxes_DefaultMaterial_ORM.jpg map_Ps wooden box and barrel/boxes_DefaultMaterial_RMA.jpg
map_RMA wooden box and barrel/boxes_DefaultMaterial_RMA.jpg
map_ORM wooden box and barrel/boxes_DefaultMaterial_ORM.jpg

View file

@ -20,19 +20,13 @@ uniform int tick;
//float metallic = 0.3f; //float metallic = 0.3f;
//float roughness = 0.3f; //float roughness = 0.3f;
float ao = 0.8f; //float ao = 0.8f;
// Handle multiple textures from the Mesh Object (Might not even be used) // Handle multiple textures from the Mesh Object (Might not even be used)
uniform sampler2D texture_diffuse1; uniform sampler2D texture_diffuse1;
uniform sampler2D texture_diffuse2; uniform sampler2D texture_diffuse2;
uniform sampler2D texture_specular1; uniform sampler2D texture_rma1;
uniform sampler2D texture_specular2;
uniform sampler2D texture_metalness1;
uniform sampler2D texture_metalness2;
uniform sampler2D texture_roughness1;
uniform sampler2D texture_roughness2;
uniform sampler2D texture_sheen1;
uniform sampler2D texture_sheen2;
// PBR functions from learnOpenGL.com // PBR functions from learnOpenGL.com
const float PI = 3.14159265359; const float PI = 3.14159265359;
@ -41,6 +35,7 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
float DistributionGGX(vec3 N, vec3 H, float roughness) float DistributionGGX(vec3 N, vec3 H, float roughness)
{ {
float a = roughness*roughness; float a = roughness*roughness;
@ -77,16 +72,21 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
void main() void main()
{ {
vec3 albedo = vec3(texture(texture_diffuse1, ourTexCoord)); vec3 albedo;
float metallic = 1 - texture(texture_metalness1, ourTexCoord).r; albedo.r = pow(texture(texture_diffuse1, ourTexCoord).r, 2.2);
float roughness = 1 - texture(texture_roughness1, ourTexCoord).r; albedo.g = pow(texture(texture_diffuse1, ourTexCoord).g, 2.2);
albedo.b = pow(texture(texture_diffuse1, ourTexCoord).b, 2.2);
float roughness = texture(texture_rma1, ourTexCoord).r;
float metallic = texture(texture_rma1, ourTexCoord).g;
float ao = texture(texture_rma1, ourTexCoord).b;
// Establish ambient lighting // Establish ambient lighting
float ambientStrength = 0.1; float ambientStrength = 0.1;
// Establish a temporary hard coded light position // Establish a temporary hard coded light position
vec3 lightPosition = vec3( (sin(tick / 600.0)*2), 1 + sin(tick / 600.0)*2, 2.0); vec3 lightPosition = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0);
//vec3 lightColor = vec3(1.0, 1.0, 1.0) - sin(tick / 90); //vec3 lightColor = vec3(1.0, 1.0, 1.0) - sin(tick / 90);
vec3 lightColor = vec3(1.0, 1.0, 1.0); vec3 lightColor = vec3(23.47, 21.31, 20.79);
// Normal light maths // Normal light maths
vec3 N = normalize(ourNormCoord); vec3 N = normalize(ourNormCoord);

View file

@ -41,10 +41,7 @@ void Mesh::setupMesh() {
void Mesh::draw(ShaderLoader &shader) { void Mesh::draw(ShaderLoader &shader) {
unsigned int diffuseNr = 1; unsigned int diffuseNr = 1;
unsigned int specularNr = 1; unsigned int rmaNr = 1;
unsigned int metalNr = 1;
unsigned int roughNr = 1;
unsigned int sheenNr = 1;
for (unsigned int i = 0; i < textures.size(); i++) { for (unsigned int i = 0; i < textures.size(); i++) {
// activate proper texture unit before binding // activate proper texture unit before binding
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
@ -53,14 +50,8 @@ void Mesh::draw(ShaderLoader &shader) {
std::string name = textures[i].type; std::string name = textures[i].type;
if (name == "texture_diffuse") if (name == "texture_diffuse")
number = std::to_string(diffuseNr++); number = std::to_string(diffuseNr++);
else if (name == "texture_specular") else if (name == "texture_rma")
number = std::to_string(specularNr++); number = std::to_string(rmaNr++);
else if (name == "texture_metalness")
number = std::to_string(metalNr++);
else if (name == "texture_roughness")
number = std::to_string(roughNr++);
else if (name == "texture_sheen")
number = std::to_string(sheenNr++);
shader.setInt((name + number).c_str(), i); shader.setInt((name + number).c_str(), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id); glBindTexture(GL_TEXTURE_2D, textures[i].id);

View file

@ -97,18 +97,16 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
std::vector<Texture> diffuseMaps = loadMaterialTextures( std::vector<Texture> diffuseMaps = loadMaterialTextures(
material, aiTextureType_DIFFUSE, "texture_diffuse"); material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
std::vector<Texture> specularMaps = loadMaterialTextures(
material, aiTextureType_SPECULAR, "texture_specular"); // WARNING: As assimp updates to keep up with obj's mtl format
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); // aiTextureType_sheen may become incorrect. Using sheen because assimp maps
std::vector<Texture> metalMaps = loadMaterialTextures( // map_Ps to both RMA and sheen, and doesn't seem to use mtl's map_RMA
material, aiTextureType_METALNESS, "texture_metalness"); // anywhere and I can't find an RMA texture type in this version of the
textures.insert(textures.end(), metalMaps.begin(), metalMaps.end()); // library.
std::vector<Texture> roughMaps = loadMaterialTextures( // https://github.com/assimp/assimp/commit/19371af6e65608ffc968df646bf20278e6d99414
material, aiTextureType_DIFFUSE_ROUGHNESS, "texture_roughness"); std::vector<Texture> rmaMaps =
textures.insert(textures.end(), roughMaps.begin(), roughMaps.end()); loadMaterialTextures(material, aiTextureType_SHEEN, "texture_rma");
std::vector<Texture> sheenMaps = loadMaterialTextures( textures.insert(textures.end(), rmaMaps.begin(), rmaMaps.end());
material, aiTextureType_AMBIENT_OCCLUSION, "texture_sheen");
textures.insert(textures.end(), sheenMaps.begin(), sheenMaps.end());
} }
return Mesh(vertices, indecies, textures); return Mesh(vertices, indecies, textures);
} }