Compare commits

...

5 Commits

5 changed files with 166 additions and 6 deletions

View File

@ -64,6 +64,8 @@ private:
glm::vec3 m_diffuse { 0.8f, 0.8f, 0.8f };
glm::vec3 m_specular { 1.0f, 1.0f, 1.0f };
float m_shininess { 32.0f };
float m_opacity { 1.0f };
int m_illum { 2 };
std::unique_ptr<Texture> m_diffuse_tex { nullptr };
public:
@ -77,12 +79,16 @@ public:
inline const float GetSpecularWeight() const { return m_shininess; }
inline const bool HasDiffuseTexture() const { return m_diffuse_tex != nullptr; }
inline const Texture* GetDiffuseTexture() const { return m_diffuse_tex.get(); }
inline const float GetOpacity() const { return m_opacity; }
inline const int GetIllumination() const { return m_illum; }
public:
inline void SetAmbientColor(glm::vec3 ambient) { m_ambient = ambient; }
inline void SetDiffuseColor(glm::vec3 diffuse) { m_diffuse = diffuse; }
inline void SetSpecularColor(glm::vec3 specular) { m_specular = specular; }
inline void SetSpecularWeight(float weight) { m_shininess = weight; }
inline void SetDiffuseTexture(std::unique_ptr<Texture>&& texture) { m_diffuse_tex = std::move(texture); }
inline void SetOpacity(float opacity) { m_opacity = opacity; }
inline void SetIllumination(float illum) { m_illum = illum; }
};
class Mesh {

View File

@ -142,6 +142,20 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
currentMaterial->SetDiffuseColor(glm::vec3(r, g, b));
break;
}
case MtlElement::D:
{
float d;
iss >> d;
currentMaterial->SetOpacity(d);
break;
}
case MtlElement::ILLUM:
{
int illum;
iss >> illum;
currentMaterial->SetIllumination(illum);
break;
}
case MtlElement::MAP_KD:
{
std::string texturePath;
@ -325,7 +339,10 @@ void Object::Render(Shader& shader)
auto material = GetMaterial(mesh.materialName);
shader.setFloat("ambientStrength", 0.2f);
shader.setFloat("specularStrength", material->GetSpecularWeight());
shader.setFloat("shininess", material->GetSpecularWeight());
shader.setFloat("opacity", material->GetOpacity());
shader.setBool("useSpecular", material->GetIllumination() >= 2);
shader.setFloat("specularStrength", 1.0f);
shader.setVec3("ambientColor", material->GetAmbientColor());
shader.setVec3("diffuseColor", material->GetDiffuseColor());
shader.setVec3("specularColor", material->GetSpecularColor());

126
src/shaders/pbr.fs Normal file
View File

@ -0,0 +1,126 @@
#version 410 core
// Output color
out vec4 FragColor;
in vec3 vertexPos;
in vec3 vertexNormal;
in vec2 TexCoords;
uniform vec3 lightPos;
uniform vec3 viewPos;
// From Object Renderer
uniform vec3 ambientColor;
uniform vec3 diffuseColor;
uniform vec3 specularColor; // used as F0 (base reflectance)
uniform float ambientStrength;
uniform float specularStrength;
uniform float shininess; // mapped to roughness
uniform bool useSpecular;
uniform float opacity;
uniform sampler2D diffuseTex;
uniform bool useTexture;
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
// ----------------------------------------------------------------------------
// Helper functions for Cook-Torrance BRDF
// ----------------------------------------------------------------------------
// Normal Distribution Function (GGX/Trowbridge-Reitz)
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH * NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = 3.14159265 * denom * denom;
return num / denom;
}
// Geometry function (Schlick-GGX)
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r * r) / 8.0; // remapped for direct lighting
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
// Smith's geometry function
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx1 = GeometrySchlickGGX(NdotV, roughness);
float ggx2 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
// Fresnel term (Schlick's approximation)
vec3 FresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
// ----------------------------------------------------------------------------
void main()
{
vec3 N = normalize(vertexNormal);
vec3 V = normalize(viewPos - vertexPos);
vec3 L = normalize(lightPos - vertexPos);
vec3 H = normalize(V + L);
// Texture or uniform color
vec3 albedo = (useTexture)
? texture(diffuseTex, TexCoords).rgb
: diffuseColor;
// Map shininess to roughness (inverse relationship)
float roughness = clamp(1.0 - (shininess / 256.0), 0.05, 1.0);
// Base reflectivity (F0)
vec3 F0 = mix(vec3(0.04), specularColor, specularStrength);
// Cook-Torrance BRDF
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
vec3 specular = numerator / denominator;
// Energy conservation
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= (useSpecular ? 1.0 : 1.0); // keep diffuse always unless specular is off
float NdotL = max(dot(N, L), 0.0);
vec3 diffuse = kD * albedo / 3.14159265;
vec3 radiance = LIGHT_COLOR;
vec3 Lo = (diffuse + specular) * radiance * NdotL;
// Ambient (simple, not IBL)
vec3 ambient = ambientStrength * ambientColor * albedo;
vec3 result = ambient + Lo;
// Gamma correction
result = pow(result, vec3(1.0/2.2));
FragColor = vec4(result, opacity);
}

View File

@ -17,7 +17,12 @@ uniform vec3 diffuseColor;
uniform vec3 specularColor;
uniform float ambientStrength;
uniform float specularStrength;
uniform float shininess;
uniform bool useSpecular;
uniform float opacity;
uniform sampler2D diffuseTex;
uniform bool useTexture;
@ -33,8 +38,13 @@ void main()
vec3 reflectDir = reflect(-lightDir, norm);
// Phong components
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * specularColor;
// float spec = pow(max(dot(viewDir, reflectDir), 0.0), clamp(shininess, 2, 256));
// vec3 specular = (useSpecular) ? specularStrength * spec * specularColor : vec3(0.0);
// Blinn Phong
vec3 halfDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(norm, halfDir), 0.0), clamp(shininess, 2.0, 256.0));
vec3 specular = (useSpecular) ? specularStrength * spec * specularColor : vec3(0.0);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * diffuseColor;
@ -46,5 +56,6 @@ void main()
: diffuseColor;
vec3 result = (ambient + diffuse + specular) * texColor;
FragColor = vec4(result, 1.0);
FragColor = vec4(result, opacity);
}

View File

@ -10,7 +10,7 @@ std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
auto texture = std::make_unique<Texture>();
int w, h, c;
unsigned char *data = stbi_load(filename.c_str(), &w, &h, &c, 0);
unsigned char *data = stbi_load(filename.c_str(), &w, &h, &c, 4);
if (!data) {
std::cerr << "ERROR: Failed to load texture under '" << filename << "'" << std::endl;
std::exit(1);
@ -26,7 +26,7 @@ std::unique_ptr<Texture> Texture::LoadFile(const std::string& filename) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// TODO: automatically detect values for this function
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
std::cout << "Loaded texture under '" << filename << "' with size of " << sizeof(data) << " bytes" << std::endl;