summaryrefslogtreecommitdiffhomepage
path: root/examples/models
diff options
context:
space:
mode:
authorRay <[email protected]>2019-01-10 16:57:15 +0100
committerGitHub <[email protected]>2019-01-10 16:57:15 +0100
commit56173d7cf457413fd56e26967becc7c5054aa2cf (patch)
treef1428e583f4ed5ee8c7060c47da55e8ce0194882 /examples/models
parent6056a2a5cf2aa939a10e807e452ed43d4b67f1ce (diff)
parent93471b0a7c75fc675f27fc74dfda281eb8310a7a (diff)
downloadraylib-56173d7cf457413fd56e26967becc7c5054aa2cf.tar.gz
raylib-56173d7cf457413fd56e26967becc7c5054aa2cf.zip
Merge branch 'master' into window-visibility
Diffstat (limited to 'examples/models')
-rw-r--r--examples/models/models_material_pbr.c22
-rw-r--r--examples/models/models_skybox.c6
-rw-r--r--examples/models/resources/shaders/brdf.fs73
-rw-r--r--examples/models/resources/shaders/cubemap.fs4
-rw-r--r--examples/models/resources/shaders/cubemap.vs4
-rw-r--r--examples/models/resources/shaders/skybox.fs4
-rw-r--r--examples/models/resources/shaders/skybox.vs4
-rw-r--r--examples/models/rlights.h10
8 files changed, 60 insertions, 67 deletions
diff --git a/examples/models/models_material_pbr.c b/examples/models/models_material_pbr.c
index a4a10d60..f93c7a68 100644
--- a/examples/models/models_material_pbr.c
+++ b/examples/models/models_material_pbr.c
@@ -64,7 +64,7 @@ int main()
// Send to material PBR shader camera view position
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
- SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, 3);
+ SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
//----------------------------------------------------------------------------------
// Draw
@@ -148,15 +148,15 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
Shader shdrBRDF = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS);
// Setup required shader locations
- SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1);
- SetShaderValuei(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, 1);
- SetShaderValuei(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, 1);
+ SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
+ SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
+ SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE);
mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
- mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, cubemap, BRDF_SIZE);
+ mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE);
UnloadTexture(cubemap);
UnloadTexture(texHDR);
@@ -174,14 +174,14 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR);
// Enable sample usage in shader for assigned textures
- SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, 1);
- SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, 1);
- SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, 1);
- SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, 1);
- SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, 1);
+ SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
+ SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
+ SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
+ SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
+ SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
- SetShaderValuei(mat.shader, renderModeLoc, (int[1]){ 0 }, 1);
+ SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, UNIFORM_INT);
// Set up material properties color
mat.maps[MAP_ALBEDO].color = albedo;
diff --git a/examples/models/models_skybox.c b/examples/models/models_skybox.c
index 6f6002b8..c7f76ecf 100644
--- a/examples/models/models_skybox.c
+++ b/examples/models/models_skybox.c
@@ -21,7 +21,7 @@ int main()
InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
// Define the camera to look into our 3d world
- Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
+ Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 4.0f, 1.0f, 4.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
// Load skybox model
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
@@ -30,11 +30,11 @@ int main()
// Load skybox shader and set required locations
// NOTE: Some locations are automatically set at shader loading
skybox.material.shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs");
- SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, 1);
+ SetShaderValue(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT);
// Load cubemap shader and setup required shader locations
Shader shdrCubemap = LoadShader("resources/shaders/cubemap.vs", "resources/shaders/cubemap.fs");
- SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1);
+ SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
// Load HDR panorama (sphere) texture
Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
diff --git a/examples/models/resources/shaders/brdf.fs b/examples/models/resources/shaders/brdf.fs
index 59ae384a..3e8777d2 100644
--- a/examples/models/resources/shaders/brdf.fs
+++ b/examples/models/resources/shaders/brdf.fs
@@ -1,12 +1,15 @@
/*******************************************************************************************
*
-* rPBR [shader] - Bidirectional reflectance distribution function fragment shader
+* BRDF LUT Generation - Bidirectional reflectance distribution function fragment shader
+*
+* REF: https://github.com/HectorMF/BRDFGenerator
*
* Copyright (c) 2017 Victor Fisac
*
**********************************************************************************************/
#version 330
+
#define MAX_SAMPLES 1024u
// Input vertex attributes (from vertex shader)
@@ -18,43 +21,30 @@ const float PI = 3.14159265359;
// Output fragment color
out vec4 finalColor;
-float DistributionGGX(vec3 N, vec3 H, float roughness);
-float RadicalInverse_VdC(uint bits);
vec2 Hammersley(uint i, uint N);
-vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
+float RadicalInverseVdC(uint bits);
float GeometrySchlickGGX(float NdotV, float roughness);
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
+vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
vec2 IntegrateBRDF(float NdotV, float roughness);
-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 RadicalInverse_VdC(uint bits)
+float RadicalInverseVdC(uint bits)
{
- bits = (bits << 16u) | (bits >> 16u);
- bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
- bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
- bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
- bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
- return float(bits) * 2.3283064365386963e-10; // / 0x100000000
+ bits = (bits << 16u) | (bits >> 16u);
+ bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
+ bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
+ bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
+ bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
+ return float(bits) * 2.3283064365386963e-10; // / 0x100000000
}
+// Compute Hammersley coordinates
vec2 Hammersley(uint i, uint N)
{
- return vec2(float(i)/float(N), RadicalInverse_VdC(i));
+ return vec2(float(i)/float(N), RadicalInverseVdC(i));
}
+// Integrate number of importance samples for (roughness and NoV)
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
{
float a = roughness*roughness;
@@ -85,6 +75,7 @@ float GeometrySchlickGGX(float NdotV, float roughness)
return nom/denom;
}
+// Compute the geometry term for the BRDF given roughness squared, NoV, NoL
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
@@ -97,29 +88,31 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
vec2 IntegrateBRDF(float NdotV, float roughness)
{
- vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV);
float A = 0.0;
- float B = 0.0;
+ float B = 0.0;
+ vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV);
vec3 N = vec3(0.0, 0.0, 1.0);
- for(uint i = 0u; i < MAX_SAMPLES; i++)
+ for (int i = 0; i < MAX_SAMPLES; i++)
{
// Generate a sample vector that's biased towards the preferred alignment direction (importance sampling)
- vec2 Xi = Hammersley(i, MAX_SAMPLES);
- vec3 H = ImportanceSampleGGX(Xi, N, roughness);
- vec3 L = normalize(2.0*dot(V, H)*H - V);
- float NdotL = max(L.z, 0.0);
- float NdotH = max(H.z, 0.0);
- float VdotH = max(dot(V, H), 0.0);
+
+ vec2 Xi = Hammersley(i, MAX_SAMPLES); // Compute a Hammersely coordinate
+ vec3 H = ImportanceSampleGGX(Xi, N, roughness); // Integrate number of importance samples for (roughness and NoV)
+ vec3 L = normalize(2.0*dot(V, H)*H - V); // Compute reflection vector L
+
+ float NdotL = max(L.z, 0.0); // Compute normal dot light
+ float NdotH = max(H.z, 0.0); // Compute normal dot half
+ float VdotH = max(dot(V, H), 0.0); // Compute view dot half
if (NdotL > 0.0)
{
- float G = GeometrySmith(N, V, L, roughness);
- float G_Vis = (G*VdotH)/(NdotH*NdotV);
- float Fc = pow(1.0 - VdotH, 5.0);
+ float G = GeometrySmith(N, V, L, roughness); // Compute the geometry term for the BRDF given roughness squared, NoV, NoL
+ float GVis = (G*VdotH)/(NdotH*NdotV); // Compute the visibility term given G, VoH, NoH, NoV, NoL
+ float Fc = pow(1.0 - VdotH, 5.0); // Compute the fresnel term given VoH
- A += (1.0 - Fc)*G_Vis;
- B += Fc*G_Vis;
+ A += (1.0 - Fc)*GVis; // Sum the result given fresnel, geometry, visibility
+ B += Fc*GVis;
}
}
diff --git a/examples/models/resources/shaders/cubemap.fs b/examples/models/resources/shaders/cubemap.fs
index 09ae62f5..e8e28536 100644
--- a/examples/models/resources/shaders/cubemap.fs
+++ b/examples/models/resources/shaders/cubemap.fs
@@ -9,7 +9,7 @@
#version 330
// Input vertex attributes (from vertex shader)
-in vec3 fragPos;
+in vec3 fragPosition;
// Input uniform values
uniform sampler2D equirectangularMap;
@@ -28,7 +28,7 @@ vec2 SampleSphericalMap(vec3 v)
void main()
{
// Normalize local position
- vec2 uv = SampleSphericalMap(normalize(fragPos));
+ vec2 uv = SampleSphericalMap(normalize(fragPosition));
// Fetch color from texture map
vec3 color = texture(equirectangularMap, uv).rgb;
diff --git a/examples/models/resources/shaders/cubemap.vs b/examples/models/resources/shaders/cubemap.vs
index 6e0bf4e1..5721eaa2 100644
--- a/examples/models/resources/shaders/cubemap.vs
+++ b/examples/models/resources/shaders/cubemap.vs
@@ -16,12 +16,12 @@ uniform mat4 projection;
uniform mat4 view;
// Output vertex attributes (to fragment shader)
-out vec3 fragPos;
+out vec3 fragPosition;
void main()
{
// Calculate fragment position based on model transformations
- fragPos = vertexPosition;
+ fragPosition = vertexPosition;
// Calculate final vertex position
gl_Position = projection*view*vec4(vertexPosition, 1.0);
diff --git a/examples/models/resources/shaders/skybox.fs b/examples/models/resources/shaders/skybox.fs
index 0bd2f320..053a2517 100644
--- a/examples/models/resources/shaders/skybox.fs
+++ b/examples/models/resources/shaders/skybox.fs
@@ -9,7 +9,7 @@
#version 330
// Input vertex attributes (from vertex shader)
-in vec3 fragPos;
+in vec3 fragPosition;
// Input uniform values
uniform samplerCube environmentMap;
@@ -20,7 +20,7 @@ out vec4 finalColor;
void main()
{
// Fetch color from texture map
- vec3 color = texture(environmentMap, fragPos).rgb;
+ vec3 color = texture(environmentMap, fragPosition).rgb;
// Apply gamma correction
color = color/(color + vec3(1.0));
diff --git a/examples/models/resources/shaders/skybox.vs b/examples/models/resources/shaders/skybox.vs
index f40d615c..dcbe6c3d 100644
--- a/examples/models/resources/shaders/skybox.vs
+++ b/examples/models/resources/shaders/skybox.vs
@@ -16,12 +16,12 @@ uniform mat4 projection;
uniform mat4 view;
// Output vertex attributes (to fragment shader)
-out vec3 fragPos;
+out vec3 fragPosition;
void main()
{
// Calculate fragment position based on model transformations
- fragPos = vertexPosition;
+ fragPosition = vertexPosition;
// Remove translation from the view matrix
mat4 rotView = mat4(mat3(view));
diff --git a/examples/models/rlights.h b/examples/models/rlights.h
index 0da3e2cb..19504473 100644
--- a/examples/models/rlights.h
+++ b/examples/models/rlights.h
@@ -158,20 +158,20 @@ Light CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shade
void UpdateLightValues(Shader shader, Light light)
{
// Send to shader light enabled state and type
- SetShaderValuei(shader, light.enabledLoc, (int[1]){ light.enabled }, 1);
- SetShaderValuei(shader, light.typeLoc, (int[1]){ light.type }, 1);
+ SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT);
+ SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT);
// Send to shader light position values
float position[3] = { light.position.x, light.position.y, light.position.z };
- SetShaderValue(shader, light.posLoc, position, 3);
+ SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3);
// Send to shader light target position values
float target[3] = { light.target.x, light.target.y, light.target.z };
- SetShaderValue(shader, light.targetLoc, target, 3);
+ SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3);
// Send to shader light color values
float diff[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255, (float)light.color.b/(float)255, (float)light.color.a/(float)255 };
- SetShaderValue(shader, light.colorLoc, diff, 4);
+ SetShaderValue(shader, light.colorLoc, diff, UNIFORM_VEC4);
}
#endif // RLIGHTS_IMPLEMENTATION \ No newline at end of file