Bulk update NvPro-Samples 03/18/21
This commit is contained in:
parent
a2b80ab819
commit
2da588b7e6
113 changed files with 3529 additions and 1508 deletions
123
ray_tracing_ao/shaders/ao.comp
Normal file
123
ray_tracing_ao/shaders/ao.comp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#version 460
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_EXT_nonuniform_qualifier : enable
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_scalar_block_layout : enable
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_EXT_ray_query : enable
|
||||
#include "raycommon.glsl"
|
||||
|
||||
|
||||
const int GROUP_SIZE = 16;
|
||||
layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE) in;
|
||||
layout(set = 0, binding = 0, rgba32f) uniform image2D inImage;
|
||||
layout(set = 0, binding = 1, r32f) uniform image2D outImage;
|
||||
layout(set = 0, binding = 2) uniform accelerationStructureEXT topLevelAS;
|
||||
|
||||
|
||||
// See AoControl
|
||||
layout(push_constant) uniform params_
|
||||
{
|
||||
float rtao_radius;
|
||||
int rtao_samples;
|
||||
float rtao_power;
|
||||
int rtao_distance_based;
|
||||
int frame_number;
|
||||
int max_samples;
|
||||
};
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Tracing a ray and returning the weight based on the distance of the hit
|
||||
//
|
||||
float TraceRay(in rayQueryEXT rayQuery, in vec3 origin, in vec3 direction)
|
||||
{
|
||||
uint flags = gl_RayFlagsNoneEXT;
|
||||
if(rtao_distance_based == 0)
|
||||
flags = gl_RayFlagsTerminateOnFirstHitEXT;
|
||||
|
||||
rayQueryInitializeEXT(rayQuery, topLevelAS, flags, 0xFF, origin, 0.0f, direction, rtao_radius);
|
||||
|
||||
// Start traversal: return false if traversal is complete
|
||||
while(rayQueryProceedEXT(rayQuery))
|
||||
{
|
||||
}
|
||||
|
||||
// Returns type of committed (true) intersection
|
||||
if(rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
|
||||
{
|
||||
// Got an intersection == Shadow
|
||||
if(rtao_distance_based == 0)
|
||||
return 1;
|
||||
float length = 1 - (rayQueryGetIntersectionTEXT(rayQuery, true) / rtao_radius);
|
||||
return length; // * length;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
float occlusion = 0.0;
|
||||
|
||||
ivec2 size = imageSize(inImage);
|
||||
// Check if not outside boundaries
|
||||
if(gl_GlobalInvocationID.x >= size.x || gl_GlobalInvocationID.y >= size.y)
|
||||
return;
|
||||
|
||||
// Initialize the random number
|
||||
uint seed = tea(size.x * gl_GlobalInvocationID.y + gl_GlobalInvocationID.x, frame_number);
|
||||
|
||||
// Retrieving position and normal
|
||||
vec4 gBuffer = imageLoad(inImage, ivec2(gl_GlobalInvocationID.xy));
|
||||
|
||||
// Shooting rays only if a fragment was rendered
|
||||
if(gBuffer != vec4(0))
|
||||
{
|
||||
vec3 origin = gBuffer.xyz;
|
||||
vec3 normal = DecompressUnitVec(floatBitsToUint(gBuffer.w));
|
||||
vec3 direction;
|
||||
|
||||
// Move origin slightly away from the surface to avoid self-occlusion
|
||||
origin = OffsetRay(origin, normal);
|
||||
|
||||
// Finding the basis (tangent and bitangent) from the normal
|
||||
vec3 n, tangent, bitangent;
|
||||
ComputeDefaultBasis(normal, tangent, bitangent);
|
||||
|
||||
// Sampling hemiphere n-time
|
||||
for(int i = 0; i < rtao_samples; i++)
|
||||
{
|
||||
// Cosine sampling
|
||||
float r1 = rnd(seed);
|
||||
float r2 = rnd(seed);
|
||||
float sq = sqrt(1.0 - r2);
|
||||
float phi = 2 * M_PI * r1;
|
||||
vec3 direction = vec3(cos(phi) * sq, sin(phi) * sq, sqrt(r2));
|
||||
direction = direction.x * tangent + direction.y * bitangent + direction.z * normal;
|
||||
// Initializes a ray query object but does not start traversal
|
||||
rayQueryEXT rayQuery;
|
||||
|
||||
occlusion += TraceRay(rayQuery, origin, direction);
|
||||
}
|
||||
|
||||
// Computing occlusion
|
||||
occlusion = 1 - (occlusion / rtao_samples);
|
||||
occlusion = pow(clamp(occlusion, 0, 1), rtao_power);
|
||||
}
|
||||
|
||||
|
||||
// Writting out the AO
|
||||
if(frame_number == 0)
|
||||
{
|
||||
imageStore(outImage, ivec2(gl_GlobalInvocationID.xy), vec4(occlusion));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Accumulating over time
|
||||
float old_ao = imageLoad(outImage, ivec2(gl_GlobalInvocationID.xy)).x;
|
||||
float new_result = mix(old_ao, occlusion, 1.0f / float(frame_number + 1));
|
||||
imageStore(outImage, ivec2(gl_GlobalInvocationID.xy), vec4(new_result));
|
||||
}
|
||||
}
|
||||
90
ray_tracing_ao/shaders/frag_shader.frag
Normal file
90
ray_tracing_ao/shaders/frag_shader.frag
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
#version 460
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_EXT_nonuniform_qualifier : enable
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_scalar_block_layout : enable
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_EXT_ray_query : enable
|
||||
|
||||
#include "raycommon.glsl"
|
||||
#include "wavefront.glsl"
|
||||
|
||||
layout(push_constant) uniform shaderInformation
|
||||
{
|
||||
vec3 lightPosition;
|
||||
uint instanceId;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
}
|
||||
pushC;
|
||||
|
||||
// clang-format off
|
||||
// Incoming
|
||||
//layout(location = 0) flat in int matIndex;
|
||||
layout(location = 1) in vec2 fragTexCoord;
|
||||
layout(location = 2) in vec3 fragNormal;
|
||||
layout(location = 3) in vec3 viewDir;
|
||||
layout(location = 4) in vec3 worldPos;
|
||||
// Outgoing
|
||||
layout(location = 0) out vec4 outColor;
|
||||
layout(location = 1) out vec4 outGbuffer;
|
||||
// Buffers
|
||||
layout(binding = 1, scalar) buffer MatColorBufferObject { WaveFrontMaterial m[]; } materials[];
|
||||
layout(binding = 2, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc;
|
||||
layout(binding = 3) uniform sampler2D[] textureSamplers;
|
||||
layout(binding = 4, scalar) buffer MatIndex { int i[]; } matIdx[];
|
||||
//layout(binding = 7, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
|
||||
// clang-format on
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
// Object of this instance
|
||||
int objId = scnDesc.i[pushC.instanceId].objId;
|
||||
|
||||
// Material of the object
|
||||
int matIndex = matIdx[nonuniformEXT(objId)].i[gl_PrimitiveID];
|
||||
WaveFrontMaterial mat = materials[nonuniformEXT(objId)].m[matIndex];
|
||||
|
||||
vec3 N = normalize(fragNormal);
|
||||
|
||||
// Vector toward light
|
||||
vec3 L;
|
||||
float lightDistance;
|
||||
float lightIntensity = pushC.lightIntensity;
|
||||
if(pushC.lightType == 0)
|
||||
{
|
||||
vec3 lDir = pushC.lightPosition - worldPos;
|
||||
float d = length(lDir);
|
||||
lightIntensity = pushC.lightIntensity / (d * d);
|
||||
L = normalize(lDir);
|
||||
lightDistance = d;
|
||||
}
|
||||
else
|
||||
{
|
||||
L = normalize(pushC.lightPosition - vec3(0));
|
||||
lightDistance = 10000;
|
||||
}
|
||||
|
||||
|
||||
// Diffuse
|
||||
vec3 diffuse = computeDiffuse(mat, L, N);
|
||||
diffuse = vec3(1);
|
||||
// if(mat.textureId >= 0)
|
||||
// {
|
||||
// int txtOffset = scnDesc.i[pushC.instanceId].txtOffset;
|
||||
// uint txtId = txtOffset + mat.textureId;
|
||||
// vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz;
|
||||
// diffuse *= diffuseTxt;
|
||||
// }
|
||||
//lightIntensity = 1;
|
||||
// Specular
|
||||
vec3 specular = vec3(0); //computeSpecular(mat, viewDir, L, N);
|
||||
lightIntensity = 1;
|
||||
// Result
|
||||
outColor = vec4(lightIntensity * (diffuse + specular), 1);
|
||||
|
||||
|
||||
outGbuffer.rgba = vec4(worldPos, uintBitsToFloat(CompressUnitVec(N)));
|
||||
}
|
||||
15
ray_tracing_ao/shaders/passthrough.vert
Normal file
15
ray_tracing_ao/shaders/passthrough.vert
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#version 450
|
||||
layout (location = 0) out vec2 outUV;
|
||||
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
|
||||
gl_Position = vec4(outUV * 2.0f - 1.0f, 1.0f, 1.0f);
|
||||
}
|
||||
23
ray_tracing_ao/shaders/post.frag
Normal file
23
ray_tracing_ao/shaders/post.frag
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#version 450
|
||||
layout(location = 0) in vec2 outUV;
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
|
||||
layout(set = 0, binding = 0) uniform sampler2D noisyTxt;
|
||||
layout(set = 0, binding = 1) uniform sampler2D aoTxt;
|
||||
|
||||
|
||||
layout(push_constant) uniform shaderInformation
|
||||
{
|
||||
float aspectRatio;
|
||||
}
|
||||
pushc;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 uv = outUV;
|
||||
float gamma = 1. / 2.2;
|
||||
vec4 color = texture(noisyTxt, uv);
|
||||
float ao = texture(aoTxt, uv).x;
|
||||
|
||||
fragColor = pow(color * ao, vec4(gamma));
|
||||
}
|
||||
190
ray_tracing_ao/shaders/raycommon.glsl
Normal file
190
ray_tracing_ao/shaders/raycommon.glsl
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/* Copyright (c) 2014-2018, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
//-
|
||||
// This utility compresses a normal(x,y,z) to a uint and decompresses it
|
||||
|
||||
|
||||
#define C_Stack_Max 3.402823466e+38f
|
||||
uint CompressUnitVec(vec3 nv)
|
||||
{
|
||||
// map to octahedron and then flatten to 2D (see 'Octahedron Environment Maps' by Engelhardt & Dachsbacher)
|
||||
if((nv.x < C_Stack_Max) && !isinf(nv.x))
|
||||
{
|
||||
const float d = 32767.0f / (abs(nv.x) + abs(nv.y) + abs(nv.z));
|
||||
int x = int(roundEven(nv.x * d));
|
||||
int y = int(roundEven(nv.y * d));
|
||||
if(nv.z < 0.0f)
|
||||
{
|
||||
const int maskx = x >> 31;
|
||||
const int masky = y >> 31;
|
||||
const int tmp = 32767 + maskx + masky;
|
||||
const int tmpx = x;
|
||||
x = (tmp - (y ^ masky)) ^ maskx;
|
||||
y = (tmp - (tmpx ^ maskx)) ^ masky;
|
||||
}
|
||||
uint packed = (uint(y + 32767) << 16) | uint(x + 32767);
|
||||
if(packed == ~0u)
|
||||
return ~0x1u;
|
||||
return packed;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ~0u;
|
||||
}
|
||||
}
|
||||
|
||||
float ShortToFloatM11(const int v) // linearly maps a short 32767-32768 to a float -1-+1 //!! opt.?
|
||||
{
|
||||
return (v >= 0) ? (uintBitsToFloat(0x3F800000u | (uint(v) << 8)) - 1.0f) :
|
||||
(uintBitsToFloat((0x80000000u | 0x3F800000u) | (uint(-v) << 8)) + 1.0f);
|
||||
}
|
||||
vec3 DecompressUnitVec(uint packed)
|
||||
{
|
||||
if(packed != ~0u) // sanity check, not needed as isvalid_unit_vec is called earlier
|
||||
{
|
||||
int x = int(packed & 0xFFFFu) - 32767;
|
||||
int y = int(packed >> 16) - 32767;
|
||||
const int maskx = x >> 31;
|
||||
const int masky = y >> 31;
|
||||
const int tmp0 = 32767 + maskx + masky;
|
||||
const int ymask = y ^ masky;
|
||||
const int tmp1 = tmp0 - (x ^ maskx);
|
||||
const int z = tmp1 - ymask;
|
||||
float zf;
|
||||
if(z < 0)
|
||||
{
|
||||
x = (tmp0 - ymask) ^ maskx;
|
||||
y = tmp1 ^ masky;
|
||||
zf = uintBitsToFloat((0x80000000u | 0x3F800000u) | (uint(-z) << 8)) + 1.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
zf = uintBitsToFloat(0x3F800000u | (uint(z) << 8)) - 1.0f;
|
||||
}
|
||||
return normalize(vec3(ShortToFloatM11(x), ShortToFloatM11(y), zf));
|
||||
}
|
||||
else
|
||||
{
|
||||
return vec3(C_Stack_Max);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
// Avoiding self intersections (see Ray Tracing Gems, Ch. 6)
|
||||
//
|
||||
vec3 OffsetRay(in vec3 p, in vec3 n)
|
||||
{
|
||||
const float intScale = 256.0f;
|
||||
const float floatScale = 1.0f / 65536.0f;
|
||||
const float origin = 1.0f / 32.0f;
|
||||
|
||||
ivec3 of_i = ivec3(intScale * n.x, intScale * n.y, intScale * n.z);
|
||||
|
||||
vec3 p_i = vec3(intBitsToFloat(floatBitsToInt(p.x) + ((p.x < 0) ? -of_i.x : of_i.x)),
|
||||
intBitsToFloat(floatBitsToInt(p.y) + ((p.y < 0) ? -of_i.y : of_i.y)),
|
||||
intBitsToFloat(floatBitsToInt(p.z) + ((p.z < 0) ? -of_i.z : of_i.z)));
|
||||
|
||||
return vec3(abs(p.x) < origin ? p.x + floatScale * n.x : p_i.x, //
|
||||
abs(p.y) < origin ? p.y + floatScale * n.y : p_i.y, //
|
||||
abs(p.z) < origin ? p.z + floatScale * n.z : p_i.z);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////// AO //////////////////////////////////////
|
||||
#define EPS 0.05
|
||||
const float M_PI = 3.141592653589;
|
||||
|
||||
void ComputeDefaultBasis(const vec3 normal, out vec3 x, out vec3 y)
|
||||
{
|
||||
// ZAP's default coordinate system for compatibility
|
||||
vec3 z = normal;
|
||||
const float yz = -z.y * z.z;
|
||||
y = normalize(((abs(z.z) > 0.99999f) ? vec3(-z.x * z.y, 1.0f - z.y * z.y, yz) :
|
||||
vec3(-z.x * z.z, yz, 1.0f - z.z * z.z)));
|
||||
|
||||
x = cross(y, z);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
// Random
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// Generate a random unsigned int from two unsigned int values, using 16 pairs
|
||||
// of rounds of the Tiny Encryption Algorithm. See Zafar, Olano, and Curtis,
|
||||
// "GPU Random Numbers via the Tiny Encryption Algorithm"
|
||||
uint tea(uint val0, uint val1)
|
||||
{
|
||||
uint v0 = val0;
|
||||
uint v1 = val1;
|
||||
uint s0 = 0;
|
||||
|
||||
for(uint n = 0; n < 16; n++)
|
||||
{
|
||||
s0 += 0x9e3779b9;
|
||||
v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4);
|
||||
v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e);
|
||||
}
|
||||
|
||||
return v0;
|
||||
}
|
||||
|
||||
uvec2 pcg2d(uvec2 v)
|
||||
{
|
||||
v = v * 1664525u + 1013904223u;
|
||||
|
||||
v.x += v.y * 1664525u;
|
||||
v.y += v.x * 1664525u;
|
||||
|
||||
v = v ^ (v >> 16u);
|
||||
|
||||
v.x += v.y * 1664525u;
|
||||
v.y += v.x * 1664525u;
|
||||
|
||||
v = v ^ (v >> 16u);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
// Generate a random unsigned int in [0, 2^24) given the previous RNG state
|
||||
// using the Numerical Recipes linear congruential generator
|
||||
uint lcg(inout uint prev)
|
||||
{
|
||||
uint LCG_A = 1664525u;
|
||||
uint LCG_C = 1013904223u;
|
||||
prev = (LCG_A * prev + LCG_C);
|
||||
return prev & 0x00FFFFFF;
|
||||
}
|
||||
|
||||
// Generate a random float in [0, 1) given the previous RNG state
|
||||
float rnd(inout uint seed)
|
||||
{
|
||||
return (float(lcg(seed)) / float(0x01000000));
|
||||
}
|
||||
61
ray_tracing_ao/shaders/vert_shader.vert
Normal file
61
ray_tracing_ao/shaders/vert_shader.vert
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#version 450
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_EXT_scalar_block_layout : enable
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
|
||||
#include "wavefront.glsl"
|
||||
|
||||
// clang-format off
|
||||
layout(binding = 2, set = 0, scalar) buffer ScnDesc { sceneDesc i[]; } scnDesc;
|
||||
// clang-format on
|
||||
|
||||
layout(binding = 0) uniform UniformBufferObject
|
||||
{
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 viewI;
|
||||
}
|
||||
ubo;
|
||||
|
||||
layout(push_constant) uniform shaderInformation
|
||||
{
|
||||
vec3 lightPosition;
|
||||
uint instanceId;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
}
|
||||
pushC;
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 1) in vec3 inNormal;
|
||||
layout(location = 2) in vec3 inColor;
|
||||
layout(location = 3) in vec2 inTexCoord;
|
||||
|
||||
|
||||
//layout(location = 0) flat out int matIndex;
|
||||
layout(location = 1) out vec2 fragTexCoord;
|
||||
layout(location = 2) out vec3 fragNormal;
|
||||
layout(location = 3) out vec3 viewDir;
|
||||
layout(location = 4) out vec3 worldPos;
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
mat4 objMatrix = scnDesc.i[pushC.instanceId].transfo;
|
||||
mat4 objMatrixIT = scnDesc.i[pushC.instanceId].transfoIT;
|
||||
|
||||
vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1));
|
||||
|
||||
worldPos = vec3(objMatrix * vec4(inPosition, 1.0));
|
||||
viewDir = vec3(worldPos - origin);
|
||||
fragTexCoord = inTexCoord;
|
||||
fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0));
|
||||
// matIndex = inMatID;
|
||||
|
||||
gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0);
|
||||
}
|
||||
58
ray_tracing_ao/shaders/wavefront.glsl
Normal file
58
ray_tracing_ao/shaders/wavefront.glsl
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
struct Vertex
|
||||
{
|
||||
vec3 pos;
|
||||
vec3 nrm;
|
||||
vec3 color;
|
||||
vec2 texCoord;
|
||||
};
|
||||
|
||||
struct WaveFrontMaterial
|
||||
{
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 transmittance;
|
||||
vec3 emission;
|
||||
float shininess;
|
||||
float ior; // index of refraction
|
||||
float dissolve; // 1 == opaque; 0 == fully transparent
|
||||
int illum; // illumination model (see http://www.fileformat.info/format/material/)
|
||||
int textureId;
|
||||
};
|
||||
|
||||
struct sceneDesc
|
||||
{
|
||||
int objId;
|
||||
int txtOffset;
|
||||
mat4 transfo;
|
||||
mat4 transfoIT;
|
||||
};
|
||||
|
||||
|
||||
vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal)
|
||||
{
|
||||
// Lambertian
|
||||
float dotNL = max(dot(normal, lightDir), 0.0);
|
||||
vec3 c = mat.diffuse * dotNL;
|
||||
if(mat.illum >= 1)
|
||||
c += mat.ambient;
|
||||
return c;
|
||||
}
|
||||
|
||||
vec3 computeSpecular(WaveFrontMaterial mat, vec3 viewDir, vec3 lightDir, vec3 normal)
|
||||
{
|
||||
if(mat.illum < 2)
|
||||
return vec3(0);
|
||||
|
||||
// Compute specular only if not in shadow
|
||||
const float kPi = 3.14159265;
|
||||
const float kShininess = max(mat.shininess, 4.0);
|
||||
|
||||
// Specular
|
||||
const float kEnergyConservation = (2.0 + kShininess) / (2.0 * kPi);
|
||||
vec3 V = normalize(-viewDir);
|
||||
vec3 R = reflect(-lightDir, normal);
|
||||
float specular = kEnergyConservation * pow(max(dot(V, R), 0.0), kShininess);
|
||||
|
||||
return vec3(mat.specular * specular);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue