Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Assets/Shaders/normal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

out vec4 frag_color;

in vec3 f_normal;
in vec3 fnormal;

void main() {
frag_color = vec4(f_normal, 1.0);
frag_color = vec4((fnormal + 1.0) / 2.0, 1.0);
}
15 changes: 0 additions & 15 deletions Assets/Shaders/normal.vs

This file was deleted.

158 changes: 158 additions & 0 deletions Assets/Shaders/pbr.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#version 420 core

#include "frag_uniforms.glsl"

struct Material {
vec3 baseColor;
float metallic;
float roughness;
float ao;

bool hasBaseColorMap;
sampler2D baseColorMap;

bool hasMetallicMap;
sampler2D metallicMap;

bool hasRoughnessMap;
sampler2D roughnessMap;

bool hasNormalMap;
sampler2D normalMap;

bool hasAoMap;
sampler2D aoMap;

bool hasEmissiveMap;
sampler2D emissiveMap;
};

uniform Material material;

in vec3 fnormal;
in vec3 ftangent;
in vec3 fbitangent;
in vec3 fposition;
in vec3 fview;
in vec2 ftex_coords;

out vec4 frag_color;
const float PI = 3.14159265359;

vec3 getNormalFromMap() {
if(material.hasNormalMap) {
vec3 tangentNormal = texture(material.normalMap, ftex_coords).xyz * 2.0 - 1.0;

vec3 N = normalize(fnormal);
vec3 T = normalize(ftangent);
// Gram-Schmidt process to re-orthogonalize T
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N, T); // Reconstruct Bitangent

mat3 TBN = mat3(T, B, N);
return normalize(TBN * tangentNormal);
} else {
return fnormal;
}
}
// ----------------------------------------------------------------------------
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 nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;

return nom / denom;
}
// ----------------------------------------------------------------------------
float GeometrySchlickGGX(float NdotV, float roughness) {
float r = (roughness + 1.0);
float k = (r*r) / 8.0;

float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;

return nom / denom;
}
// ----------------------------------------------------------------------------
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 ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);

return ggx1 * ggx2;
}
// ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
// ----------------------------------------------------------------------------
void main() {
vec3 albedo = pow(material.hasBaseColorMap ? texture(material.baseColorMap, ftex_coords).rgb : material.baseColor, vec3(2.2));
float metallic = material.hasMetallicMap ? texture(material.metallicMap, ftex_coords).b : material.metallic;
float roughness = material.hasRoughnessMap ? texture(material.roughnessMap, ftex_coords).g : material.roughness;
float ao = material.hasAoMap ? texture(material.aoMap, ftex_coords).length() : material.ao;

vec3 N = getNormalFromMap();
vec3 V = normalize(fview - fposition);

// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);

// reflectance equation
vec3 Lo = vec3(0.0);
for(int i = 0; i < light_count; ++i) {
// calculate per-light radiance
vec3 L = normalize(lights[i].position - fposition);
vec3 H = normalize(V + L);
float distance = length(lights[i].position - fposition);
float attenuation = 1.0 / (1 + lights[i].distance_dropoff * distance * distance);
vec3 radiance = lights[i].color * attenuation;

// 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.0001; // + 0.0001 to prevent divide by zero
vec3 specular = numerator / denominator;

// kS is equal to Fresnel
vec3 kS = F;
// for energy conservation, the diffuse and specular light can't
// be above 1.0 (unless the surface emits light); to preserve this
// relationship the diffuse component (kD) should equal 1.0 - kS.
vec3 kD = vec3(1.0) - kS;
// multiply kD by the inverse metalness such that only non-metals
// have diffuse lighting, or a linear blend if partly metal (pure metals
// have no diffuse light).
kD *= 1.0 - metallic;

// scale light by NdotL
float NdotL = max(dot(N, L), 0.0);

// add to outgoing radiance Lo
Lo += (kD * albedo / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
}

//TODO IBL
vec3 ambient = vec3(0.03) * albedo * ao;

vec3 emissive = material.hasEmissiveMap ? texture(material.emissiveMap, ftex_coords).rgb : vec3(0.0);
vec3 color = ambient + Lo + emissive;

// HDR tonemapping
color = color / (color + vec3(1.0));
// gamma correct
color = pow(color, vec3(1.0/2.2));

frag_color = vec4(color, 1.0);
}
12 changes: 0 additions & 12 deletions Assets/Shaders/picking.vs

This file was deleted.

4 changes: 4 additions & 0 deletions Assets/Shaders/phong.vs → Assets/Shaders/skinning.vs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ void main() {
if(bone_ids == ivec4(-1)) {
totalPosition = vec4(vertex, 1.0f);
totalNormal = normal;
totalTangent = tangent;
totalBitangent = bitangent;
} else {
for(int i = 0 ; i < MAX_BONE_INFLUENCE ; i++) {
if(bone_ids[i] == -1) continue;

if(bone_ids[i] >= MAX_BONES) {
totalPosition = vec4(vertex, 1.0f);
totalNormal = normal;
totalTangent = tangent;
totalBitangent = bitangent;
break;
}

Expand Down
13 changes: 0 additions & 13 deletions Assets/Shaders/solid.vs

This file was deleted.

2 changes: 1 addition & 1 deletion ICE/Graphics/include/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <string>

namespace ICE {
enum class TextureFormat { None = 0, RGB = 1, RGBA = 2, Float16 = 3, MONO8 = 4 };
enum class TextureFormat { None = 0, SRGB8, SRGBA8, RGB8, RGBA8, Float16, MONO8 };

enum class TextureWrap { None = 0, Clamp = 1, Repeat = 2 };

Expand Down
52 changes: 52 additions & 0 deletions ICE/GraphicsAPI/OpenGL/include/OpenGLTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,58 @@
#include <string>

namespace ICE {

constexpr GLenum textureFormatToGLInternalFormat(TextureFormat format) {
switch (format) {
case TextureFormat::SRGB8:
return GL_SRGB8;
case TextureFormat::SRGBA8:
return GL_SRGB8_ALPHA8;
case TextureFormat::RGB8:
return GL_RGB8;
case TextureFormat::RGBA8:
return GL_RGBA8;
case TextureFormat::Float16:
return GL_RGBA16F;
case TextureFormat::MONO8:
return GL_R8;
default:
return GL_RGBA8;
}
}

constexpr int textureFormatToAlignment(TextureFormat format) {
switch (format) {
case TextureFormat::SRGB8:
case TextureFormat::RGB8:
return 1;
case TextureFormat::SRGBA8:
case TextureFormat::RGBA8:
case TextureFormat::Float16:
return 4;
case TextureFormat::MONO8:
return 1;
default:
return 4;
}
}

constexpr int textureFormatToChannels(TextureFormat format) {
switch (format) {
case TextureFormat::SRGB8:
case TextureFormat::RGB8:
return 3;
case TextureFormat::SRGBA8:
case TextureFormat::RGBA8:
case TextureFormat::Float16:
return 4;
case TextureFormat::MONO8:
return 1;
default:
return 4;
}
}

class OpenGLTexture2D : public Texture2D {
public:
OpenGLTexture2D(const std::string &file);
Expand Down
23 changes: 4 additions & 19 deletions ICE/GraphicsAPI/OpenGL/src/OpenGLTexture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ICE {
OpenGLTexture2D::OpenGLTexture2D(const std::string &file) : file(file) {
int channels, w, h;
void *data = Texture::getDataFromFile(file, &w, &h, &channels);
loadData(data, w, h, channels == 4 ? TextureFormat::RGBA : TextureFormat::RGB);
loadData(data, w, h, channels == 4 ? TextureFormat::RGBA8 : TextureFormat::RGB8);
stbi_image_free(data);
}

Expand All @@ -23,24 +23,13 @@ OpenGLTexture2D::OpenGLTexture2D(const void *data, size_t w, size_t h, TextureFo
void OpenGLTexture2D::loadData(const void *data, size_t w, size_t h, TextureFormat fmt) {
width = w;
height = h;
storageFormat = GL_RGBA;
dataFormat = GL_RGBA;

glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);

if (fmt == TextureFormat::RGBA) {
format = TextureFormat::RGBA;
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
} else if (fmt == TextureFormat::RGB) {
storageFormat = dataFormat = GL_RGB;
format = TextureFormat::RGB;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
} else if (fmt == TextureFormat::MONO8) {
storageFormat = dataFormat = GL_RED;
format = TextureFormat::MONO8;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
storageFormat = textureFormatToGLInternalFormat(fmt);
dataFormat = (textureFormatToChannels(fmt) == 4) ? GL_RGBA : (textureFormatToChannels(fmt) == 3) ? GL_RGB : GL_RED;
glPixelStorei(GL_UNPACK_ALIGNMENT, textureFormatToAlignment(fmt));

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Expand All @@ -53,10 +42,6 @@ void OpenGLTexture2D::loadData(const void *data, size_t w, size_t h, TextureForm
}

void OpenGLTexture2D::setData(void *data, uint32_t size) {
uint32_t bpp = (format == TextureFormat::RGBA) ? 4 : 3;
if (size != bpp * width * height) {
throw ICEException("Texture size corrupted");
}
glTextureSubImage2D(id, 0, 0, 0, width, height, dataFormat, GL_UNSIGNED_BYTE, data);
}

Expand Down
2 changes: 1 addition & 1 deletion ICE/GraphicsAPI/OpenGL/src/OpenGLTextureCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void OpenGLTextureCube::bind(uint32_t slot) const {
}

TextureFormat OpenGLTextureCube::getFormat() const {
return TextureFormat::RGB;
return TextureFormat::RGB8;
}

uint32_t OpenGLTextureCube::getWidth() const {
Expand Down
2 changes: 2 additions & 0 deletions ICE/IO/include/ModelLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class ModelLoader : public IAssetLoader<Model> {
Eigen::Vector3f aiVec3ToEigen(const aiVector3D &vec);
Eigen::Quaternionf aiQuatToEigen(const aiQuaternion &q);

constexpr TextureFormat getTextureFormat(aiTextureType type, int channels);

AssetBank &ref_bank;
std::shared_ptr<GraphicsFactory> m_graphics_factory;
};
Expand Down
Loading