feat: pbr try
This commit is contained in:
@ -23,7 +23,7 @@ Renderer::Renderer()
|
|||||||
|
|
||||||
m_shader.init(
|
m_shader.init(
|
||||||
FileManager::read("./src/shaders/simple.vs"),
|
FileManager::read("./src/shaders/simple.vs"),
|
||||||
FileManager::read("./src/shaders/simple.fs")
|
FileManager::read("./src/shaders/pbr.fs")
|
||||||
);
|
);
|
||||||
|
|
||||||
m_model = glm::mat4(1.f);
|
m_model = glm::mat4(1.f);
|
||||||
|
@ -393,29 +393,97 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// void Object::Render(Shader& shader)
|
||||||
|
// {
|
||||||
|
// for (auto &mesh : m_meshes) {
|
||||||
|
// auto material = GetMaterial(mesh.materialName);
|
||||||
|
|
||||||
|
// shader.setFloat("ambientStrength", 0.2f);
|
||||||
|
// 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());
|
||||||
|
|
||||||
|
// if (material->HasDiffuseTexture()) {
|
||||||
|
// shader.setBool("useTexture", true);
|
||||||
|
// glActiveTexture(GL_TEXTURE0);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||||
|
// shader.setInt("diffuseTex", 0);
|
||||||
|
// } else {
|
||||||
|
// shader.setBool("useTexture", false);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// mesh.Render();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
void Object::Render(Shader& shader)
|
void Object::Render(Shader& shader)
|
||||||
{
|
{
|
||||||
for (auto &mesh : m_meshes) {
|
for (auto &mesh : m_meshes)
|
||||||
|
{
|
||||||
auto material = GetMaterial(mesh.materialName);
|
auto material = GetMaterial(mesh.materialName);
|
||||||
|
|
||||||
shader.setFloat("ambientStrength", 0.2f);
|
|
||||||
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());
|
|
||||||
|
|
||||||
|
// --- Basic material properties ---
|
||||||
|
shader.setFloat("opacity", material->GetOpacity());
|
||||||
|
|
||||||
|
// Albedo (base color)
|
||||||
|
shader.setVec3("albedo", material->GetDiffuseColor());
|
||||||
|
|
||||||
|
// Metallic and roughness (defaults)
|
||||||
|
shader.setFloat("metallic", 0.8f);
|
||||||
|
shader.setFloat("roughness", 0.5f);
|
||||||
|
shader.setFloat("ao", 1.0f); // default ambient occlusion if none
|
||||||
|
|
||||||
|
// --- Optional textures ---
|
||||||
|
int texUnit = 0;
|
||||||
|
|
||||||
|
// Albedo texture
|
||||||
if (material->HasDiffuseTexture()) {
|
if (material->HasDiffuseTexture()) {
|
||||||
shader.setBool("useTexture", true);
|
shader.setBool("useAlbedoMap", true);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||||
shader.setInt("diffuseTex", 0);
|
shader.setInt("albedoTex", texUnit++);
|
||||||
} else {
|
} else {
|
||||||
shader.setBool("useTexture", false);
|
shader.setBool("useAlbedoMap", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Metallic texture
|
||||||
|
// if (material->HasMetallicTexture()) {
|
||||||
|
if (false) {
|
||||||
|
shader.setBool("useMetallicMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetMetallicTexture()->GetID());
|
||||||
|
shader.setInt("metallicTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
shader.setBool("useMetallicMap", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Roughness texture
|
||||||
|
// if (material->HasRoughnessTexture()) {
|
||||||
|
if (false) {
|
||||||
|
shader.setBool("useRoughnessMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetRoughnessTexture()->GetID());
|
||||||
|
shader.setInt("roughnessTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
shader.setBool("useRoughnessMap", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// AO texture
|
||||||
|
// if (material->HasAoTexture()) {
|
||||||
|
if (false) {
|
||||||
|
shader.setBool("useAoMap", true);
|
||||||
|
glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// glBindTexture(GL_TEXTURE_2D, material->GetAoTexture()->GetID());
|
||||||
|
shader.setInt("aoTex", texUnit++);
|
||||||
|
} else {
|
||||||
|
shader.setBool("useAoMap", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Render mesh ---
|
||||||
mesh.Render();
|
mesh.Render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,42 @@
|
|||||||
#version 410 core
|
#version 410 core
|
||||||
|
|
||||||
// Output color
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec3 vertexPos;
|
in vec3 vertexPos;
|
||||||
in vec3 vertexNormal;
|
in vec3 vertexNormal;
|
||||||
in vec2 TexCoords;
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
// Lighting inputs
|
||||||
uniform vec3 lightPos;
|
uniform vec3 lightPos;
|
||||||
uniform vec3 viewPos;
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
// From Object Renderer
|
// Material parameters
|
||||||
uniform vec3 ambientColor;
|
uniform vec3 albedo; // Base color (replaces diffuseColor)
|
||||||
uniform vec3 diffuseColor;
|
uniform float metallic; // 0 = dielectric, 1 = metal
|
||||||
uniform vec3 specularColor; // used as F0 (base reflectance)
|
uniform float roughness; // 0 = smooth mirror, 1 = rough
|
||||||
|
uniform float ao; // Ambient occlusion
|
||||||
|
|
||||||
uniform float ambientStrength;
|
// Textures
|
||||||
uniform float specularStrength;
|
uniform sampler2D albedoTex;
|
||||||
uniform float shininess; // mapped to roughness
|
uniform sampler2D metallicTex;
|
||||||
uniform bool useSpecular;
|
uniform sampler2D roughnessTex;
|
||||||
|
uniform sampler2D aoTex;
|
||||||
|
uniform bool useAlbedoMap;
|
||||||
|
uniform bool useMetallicMap;
|
||||||
|
uniform bool useRoughnessMap;
|
||||||
|
uniform bool useAoMap;
|
||||||
|
|
||||||
uniform float opacity;
|
uniform float opacity;
|
||||||
|
|
||||||
uniform sampler2D diffuseTex;
|
// Used for emissive light sources
|
||||||
uniform bool useTexture;
|
uniform bool isLight;
|
||||||
|
|
||||||
|
#define PI 3.14159265359
|
||||||
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
#define LIGHT_COLOR vec3(1.0, 1.0, 1.0)
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Helper functions for Cook-Torrance BRDF
|
// Helper functions
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// Normal Distribution Function (GGX/Trowbridge-Reitz)
|
|
||||||
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
||||||
{
|
{
|
||||||
float a = roughness * roughness;
|
float a = roughness * roughness;
|
||||||
@ -41,16 +46,15 @@ float DistributionGGX(vec3 N, vec3 H, float roughness)
|
|||||||
|
|
||||||
float num = a2;
|
float num = a2;
|
||||||
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||||
denom = 3.14159265 * denom * denom;
|
denom = PI * denom * denom;
|
||||||
|
|
||||||
return num / denom;
|
return num / denom;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Geometry function (Schlick-GGX)
|
|
||||||
float GeometrySchlickGGX(float NdotV, float roughness)
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
||||||
{
|
{
|
||||||
float r = (roughness + 1.0);
|
float r = (roughness + 1.0);
|
||||||
float k = (r * r) / 8.0; // remapped for direct lighting
|
float k = (r * r) / 8.0;
|
||||||
|
|
||||||
float num = NdotV;
|
float num = NdotV;
|
||||||
float denom = NdotV * (1.0 - k) + k;
|
float denom = NdotV * (1.0 - k) + k;
|
||||||
@ -58,69 +62,77 @@ float GeometrySchlickGGX(float NdotV, float roughness)
|
|||||||
return num / denom;
|
return num / denom;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Smith's geometry function
|
|
||||||
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
||||||
{
|
{
|
||||||
float NdotV = max(dot(N, V), 0.0);
|
float NdotV = max(dot(N, V), 0.0);
|
||||||
float NdotL = max(dot(N, L), 0.0);
|
float NdotL = max(dot(N, L), 0.0);
|
||||||
float ggx1 = GeometrySchlickGGX(NdotV, roughness);
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||||
float ggx2 = GeometrySchlickGGX(NdotL, roughness);
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||||
|
|
||||||
return ggx1 * ggx2;
|
return ggx1 * ggx2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fresnel term (Schlick's approximation)
|
vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
||||||
vec3 FresnelSchlick(float cosTheta, vec3 F0)
|
|
||||||
{
|
{
|
||||||
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Main
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
if (isLight) {
|
||||||
|
vec3 emissive = LIGHT_COLOR * 10.0; // bright light source
|
||||||
|
FragColor = vec4(emissive, 1.0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inputs
|
||||||
vec3 N = normalize(vertexNormal);
|
vec3 N = normalize(vertexNormal);
|
||||||
vec3 V = normalize(viewPos - vertexPos);
|
vec3 V = normalize(viewPos - vertexPos);
|
||||||
vec3 L = normalize(lightPos - vertexPos);
|
vec3 L = normalize(lightPos - vertexPos);
|
||||||
vec3 H = normalize(V + L);
|
vec3 H = normalize(V + L);
|
||||||
|
|
||||||
// Texture or uniform color
|
// Base color (albedo)
|
||||||
vec3 albedo = (useTexture)
|
vec3 baseColor = useAlbedoMap ? texture(albedoTex, TexCoords).rgb : albedo;
|
||||||
? texture(diffuseTex, TexCoords).rgb
|
|
||||||
: diffuseColor;
|
|
||||||
|
|
||||||
// Map shininess to roughness (inverse relationship)
|
float metal = useMetallicMap ? texture(metallicTex, TexCoords).r : metallic;
|
||||||
float roughness = clamp(1.0 - (shininess / 256.0), 0.05, 1.0);
|
float rough = useRoughnessMap ? texture(roughnessTex, TexCoords).r : roughness;
|
||||||
|
float aoValue = useAoMap ? texture(aoTex, TexCoords).r : ao;
|
||||||
|
|
||||||
// Base reflectivity (F0)
|
// Reflectance at normal incidence (F0)
|
||||||
vec3 F0 = mix(vec3(0.04), specularColor, specularStrength);
|
vec3 F0 = vec3(0.04); // typical dielectric reflectance
|
||||||
|
F0 = mix(F0, baseColor, metal); // metals use albedo as F0
|
||||||
|
|
||||||
// Cook-Torrance BRDF
|
// Cook-Torrance BRDF
|
||||||
float NDF = DistributionGGX(N, H, roughness);
|
float NDF = DistributionGGX(N, H, rough);
|
||||||
float G = GeometrySmith(N, V, L, roughness);
|
float G = GeometrySmith(N, V, L, rough);
|
||||||
vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
|
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
||||||
|
|
||||||
vec3 numerator = NDF * G * F;
|
vec3 numerator = NDF * G * F;
|
||||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
|
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
|
||||||
vec3 specular = numerator / denominator;
|
vec3 specular = numerator / denominator;
|
||||||
|
|
||||||
// Energy conservation
|
// kS is specular reflection, kD is diffuse reflection (energy conservation)
|
||||||
vec3 kS = F;
|
vec3 kS = F;
|
||||||
vec3 kD = vec3(1.0) - kS;
|
vec3 kD = vec3(1.0) - kS;
|
||||||
kD *= (useSpecular ? 1.0 : 1.0); // keep diffuse always unless specular is off
|
kD *= 1.0 - metal;
|
||||||
|
|
||||||
float NdotL = max(dot(N, L), 0.0);
|
float NdotL = max(dot(N, L), 0.0);
|
||||||
|
|
||||||
vec3 diffuse = kD * albedo / 3.14159265;
|
vec3 radiance = LIGHT_COLOR; // single light source color/intensity
|
||||||
vec3 radiance = LIGHT_COLOR;
|
|
||||||
|
|
||||||
vec3 Lo = (diffuse + specular) * radiance * NdotL;
|
vec3 Lo = (kD * baseColor / PI + specular) * radiance * NdotL;
|
||||||
|
|
||||||
// Ambient (simple, not IBL)
|
// Ambient (IBL approximation using ao)
|
||||||
vec3 ambient = ambientStrength * ambientColor * albedo;
|
vec3 ambient = vec3(0.03) * baseColor * aoValue;
|
||||||
|
|
||||||
vec3 result = ambient + Lo;
|
vec3 color = ambient + Lo;
|
||||||
|
|
||||||
// Gamma correction
|
// HDR tonemapping and gamma correction
|
||||||
result = pow(result, vec3(1.0/2.2));
|
color = color / (color + vec3(1.0));
|
||||||
|
color = pow(color, vec3(1.0 / 2.2));
|
||||||
|
|
||||||
FragColor = vec4(result, opacity);
|
FragColor = vec4(color, opacity);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user