diff --git a/zenovis/xinxinoptix/DeflMatShader.cu b/zenovis/xinxinoptix/DeflMatShader.cu index 8a9f79413d..f765674a32 100644 --- a/zenovis/xinxinoptix/DeflMatShader.cu +++ b/zenovis/xinxinoptix/DeflMatShader.cu @@ -184,14 +184,6 @@ static __inline__ __device__ bool isBadVector(float3& vector) { return isBadVector(tmp); } -static __inline__ __device__ float3 sphereUV(float3 &direction) { - - return float3 { - atan2(direction.x, direction.z) / (2.0f*M_PIf) + 0.5f, - direction.y * 0.5f + 0.5f, 0.0f - }; -} - extern "C" __global__ void __anyhit__shadow_cutout() { @@ -234,7 +226,7 @@ extern "C" __global__ void __anyhit__shadow_cutout() attrs.pos = P; attrs.nrm = N; - attrs.uv = sphereUV(_normal_object_); + attrs.uv = sphereUV(_normal_object_, false); attrs.clr = {}; attrs.tang = {}; @@ -505,7 +497,7 @@ extern "C" __global__ void __closesthit__radiance() attrs.pos = P; attrs.nrm = N; - attrs.uv = sphereUV(_normal_object_); + attrs.uv = sphereUV(_normal_object_, false); attrs.clr = {}; attrs.tang = {}; diff --git a/zenovis/xinxinoptix/Light.cu b/zenovis/xinxinoptix/Light.cu index 4e1b542789..edbf61c832 100644 --- a/zenovis/xinxinoptix/Light.cu +++ b/zenovis/xinxinoptix/Light.cu @@ -21,14 +21,6 @@ static __inline__ __device__ void evalSurface(float4* uniforms) { //GENERATED_BEGIN_MARK //GENERATED_END_MARK -} - -static __inline__ __device__ float3 sphereUV(float3 &direction) { - - return float3 { - atan2(direction.x, direction.z) / (2.0f*M_PIf) + 0.5f, - direction.y * 0.5f + 0.5f, 0.0f - }; } static __inline__ __device__ bool checkLightGAS(uint instanceId) { diff --git a/zenovis/xinxinoptix/Sampling.h b/zenovis/xinxinoptix/Sampling.h index 76358988a7..33eb6ca7d7 100644 --- a/zenovis/xinxinoptix/Sampling.h +++ b/zenovis/xinxinoptix/Sampling.h @@ -137,6 +137,17 @@ namespace rtgems { } } +static __host__ __device__ __inline__ float3 sphereUV(const float3 &dir, bool internal) { +//https://en.wikipedia.org/wiki/UV_mapping + + auto x = internal? dir.x:-dir.x; + + auto u = 0.5f + atan2f(dir.z, x) * 0.5f / M_PIf; + auto v = 0.5f + asinf(dir.y) / M_PIf; + + return float3 {u, v, 0.0f}; +} + // *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org // Licensed under Apache License 2.0 (NO WARRANTY, etc. see website) diff --git a/zenovis/xinxinoptix/proceduralSky.h b/zenovis/xinxinoptix/proceduralSky.h index 80e5b6395f..47dd61b9f2 100644 --- a/zenovis/xinxinoptix/proceduralSky.h +++ b/zenovis/xinxinoptix/proceduralSky.h @@ -1,4 +1,5 @@ #pragma once +#include "Sampling.h" #include "zxxglslvec.h" #include "TraceStuff.h" @@ -304,8 +305,8 @@ static __inline__ __device__ vec3 hdrSky2( .rotX(to_radians(params.sky_rot_x)) .rotZ(to_radians(params.sky_rot_z)) .rotY(to_radians(params.sky_rot)); - float u = atan2(dir.z, dir.x) / 3.1415926f * 0.5f + 0.5f; - float v = asin(dir.y) / 3.1415926f + 0.5f; + + vec3 uv = sphereUV(dir, true); vec3 col = vec3(0); for(int jj=-2;jj<=2;jj++) { @@ -313,7 +314,7 @@ static __inline__ __device__ vec3 hdrSky2( { float dx = (float)ii / (float)(params.skynx); float dy = (float)jj / (float)(params.skyny); - col = col + (vec3)texture2D(params.sky_texture, vec2(u + dx, v + dy)) * params.sky_strength; + col = col + (vec3)texture2D(params.sky_texture, vec2(uv[0] + dx, uv[1] + dy)) * params.sky_strength; } } @@ -328,12 +329,13 @@ static __inline__ __device__ vec3 hdrSky( .rotX(to_radians(params.sky_rot_x)) .rotZ(to_radians(params.sky_rot_z)) .rotY(to_radians(params.sky_rot)); - float u = atan2(dir.z, dir.x) / 3.1415926f * 0.5f + 0.5f; - float v = asin(dir.y) / 3.1415926f + 0.5f; - vec3 col = (vec3)texture2D(params.sky_texture, vec2(u, v)) * params.sky_strength; + + vec3 uv = sphereUV(dir, true); + + vec3 col = (vec3)texture2D(params.sky_texture, vec2(uv[0], uv[1])) * params.sky_strength; vec3 col2 = clamp(col, vec3(0.0f), vec3(upperBound)); - int i = u * params.skynx; - int j = v * params.skyny; + int i = uv[0] * params.skynx; + int j = uv[1] * params.skyny; //float p = params.skycdf[params.skynx * params.skyny + j * params.skynx + i]; pdf = luminance(col) / params.envavg / (2.0f * M_PIf * M_PIf); return mix(col, col2, isclamp);