diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/maths.h | 2 | ||||
| -rw-r--r-- | core/mesh.cpp | 3 | ||||
| -rw-r--r-- | core/png.cpp | 70 | ||||
| -rw-r--r-- | core/png.h | 27 | ||||
| -rw-r--r-- | core/skylight.h | 96 |
5 files changed, 99 insertions, 99 deletions
diff --git a/core/maths.h b/core/maths.h index 19be818..cbb684b 100644 --- a/core/maths.h +++ b/core/maths.h @@ -1383,7 +1383,7 @@ CUDA_CALLABLE inline bool PointInTriangle(Vec3 a, Vec3 b, Vec3 c, Vec3 p) return true; } -inline void ClosestPointBetweenLineSegments(const Vec3& p, const Vec3& q, const Vec3& r, const Vec3& s, float& u, float& v) +CUDA_CALLABLE inline void ClosestPointBetweenLineSegments(const Vec3& p, const Vec3& q, const Vec3& r, const Vec3& s, float& u, float& v) { Vec3 d1 = q-p; Vec3 d2 = s-r; diff --git a/core/mesh.cpp b/core/mesh.cpp index 371e664..43bd87a 100644 --- a/core/mesh.cpp +++ b/core/mesh.cpp @@ -814,7 +814,7 @@ Mesh* CreateDiscMesh(float radius, uint32_t segments) Mesh* m = new Mesh(); m->m_positions.resize(numVerts); - m->m_normals.resize(numVerts); + m->m_normals.resize(numVerts, Vec3(0.0f, 1.0f, 0.0f)); m->m_positions[0] = Point3(0.0f); m->m_positions[1] = Point3(0.0f, 0.0f, radius); @@ -827,7 +827,6 @@ Mesh* CreateDiscMesh(float radius, uint32_t segments) nextVert = 1; m->m_positions[nextVert] = Point3(radius*Sin((float(i)/segments)*k2Pi), 0.0f, radius*Cos((float(i)/segments)*k2Pi)); - m->m_normals[nextVert] = Vector3(0.0f, 1.0f, 0.0f); m->m_indices.push_back(0); m->m_indices.push_back(i); diff --git a/core/png.cpp b/core/png.cpp new file mode 100644 index 0000000..8c86bb1 --- /dev/null +++ b/core/png.cpp @@ -0,0 +1,70 @@ +#include "png.h" + +#include <iostream> + +#define STB_IMAGE_IMPLEMENTATION +#include "../external/stb_image/stb_image.h" + + +bool PngLoad(const char* filename, PngImage& image) +{ + int x, y, c; + + uint8_t* data = stbi_load(filename, &x, &y, &c, 4); + + if (data) + { + int s = x*y; + + image.m_data = new uint32_t[s]; + memcpy(image.m_data, data, s*sizeof(char)*4); + + image.m_width = (unsigned short)x; + image.m_height = (unsigned short)y; + + stbi_image_free(data); + + return true; + } + else + { + return false; + } +} + +void PngFree(PngImage& image) +{ + delete[] image.m_data; +} + +bool HdrLoad(const char* filename, HdrImage& image) +{ + int x, y, c; + + float* data = stbi_loadf(filename, &x, &y, &c, 4); + + if (data) + { + int s = x*y; + + image.m_data = new float[s*4]; + memcpy(image.m_data, data, s*sizeof(float)*4); + + image.m_width = (unsigned short)x; + image.m_height = (unsigned short)y; + + stbi_image_free(data); + + return true; + } + else + { + return false; + } +} + +void HdrFree(HdrImage& image) +{ + delete[] image.m_data; +} + diff --git a/core/png.h b/core/png.h new file mode 100644 index 0000000..cd5960a --- /dev/null +++ b/core/png.h @@ -0,0 +1,27 @@ +#pragma once + +#include "types.h" + +struct PngImage +{ + uint16_t m_width; + uint16_t m_height; + + // pixels are always assumed to be 32 bit + uint32_t* m_data; +}; + +bool PngLoad(const char* filename, PngImage& image); +void PngFree(PngImage& image); + +struct HdrImage +{ + uint16_t m_width; + uint16_t m_height; + + float* m_data; +}; + +bool HdrLoad(const char* filename, HdrImage& image); +void HdrFree(HdrImage& image); + diff --git a/core/skylight.h b/core/skylight.h deleted file mode 100644 index ff0857d..0000000 --- a/core/skylight.h +++ /dev/null @@ -1,96 +0,0 @@ -#pragma once - -#include "maths.h" - -// implements Perez's model for sky luminance -inline float SkyDistribution(float theta, float gamma, float a, float b, float c, float d, float e) -{ - float cosGamma = cosf(gamma); - float cosTheta = cosf(theta); - - return (1.0f + a*expf(b / cosTheta))*(1.0f + c*expf(d*gamma) + e*cosGamma*cosGamma); -} - -inline float SkyLuminance(float theta, float gamma, float zenith, float sunTheta, float a, float b, float c, float d, float e) -{ - float l = zenith * (SkyDistribution(theta, gamma, a, b, c, d, e) / SkyDistribution(0.0f, sunTheta, a, b, c, d, e)); - return l; -} - -inline float Lerp2(const float ab[2], float t) -{ - return ab[0]*t + ab[1]; -} - -inline Colour SkyLight(float theta, float phi, float sunTheta, float sunPhi, float t) -{ - // need cosTheta > 0.0 - theta = Clamp(theta, 0.0f, kPi*0.5f-1.0e-6f); - - // calculate arc-length between sun and patch being calculated - const float gamma = acosf(cosf(sunTheta)*cosf(theta) + sinf(sunTheta)*sinf(theta)*cosf(abs(phi-sunPhi))); - - const float xcoeff [5][2] = { { -0.0193f, -0.2592f }, - { -0.0665f, 0.0008f }, - { -0.0004f, 0.2125f }, - { -0.0641f, -0.8989f }, - { -0.0033f, 0.0452f } }; - - const float ycoeff [5][2] = { {-0.0167f, -0.2608f }, - { -0.0950f, 0.0092f }, - { -0.0079f, 0.2102f }, - { -0.0441f, -1.6537f }, - { -0.0109f, 0.0529f } }; - - const float Ycoeff [5][2] = { { 0.1787f, -1.4630f }, - { -0.3554f, 0.4275f }, - { -0.0227f, 5.3251f }, - { 0.1206f, -2.5771f }, - { -0.0670f, 0.3703f } }; - - const Matrix44 zxcoeff(0.00166f, -0.02903f, 0.11693f, 0.0f, - -0.00375f, 0.06377f, -0.21196f, 0.0f, - 0.00209f, -0.03202f, 0.06052f, 0.0f, - 0.0f, 0.00394f, 0.25886f, 0.0f); - - const Matrix44 zycoeff(0.00275f, -0.04214f, 0.15346f, 0.0f, - -0.00610f, 0.08970f, -0.26756f, 0.0f, - 0.00317f, -0.04153f, 0.06670f, 0.0f, - 0.0f, 0.00516f, 0.26688f, 0.0f); - - - const Vec4 b(sunTheta*sunTheta*sunTheta, sunTheta*sunTheta, sunTheta, 1.0f); - const Vec4 a(t*t, t, 1.0f, 0.0f); - - // calculate the zenith values for current turbidity and sun position - const float zx = Dot3(a, zxcoeff*b); - const float zy = Dot3(a, zycoeff*b); - const float zY = (4.0453f * t - 4.9710f)*tanf((4.0f/9.0f - t/120.0f)*(kPi-2.0f*sunTheta)) - 0.2155f*t + 2.4192f; - - float x = SkyLuminance(theta, gamma, zx, sunTheta, Lerp2(xcoeff[0], t), - Lerp2(xcoeff[1], t), - Lerp2(xcoeff[2], t), - Lerp2(xcoeff[3], t), - Lerp2(xcoeff[4], t)); - - - float y = SkyLuminance(theta, gamma, zy, sunTheta, Lerp2(ycoeff[0], t), - Lerp2(ycoeff[1], t), - Lerp2(ycoeff[2], t), - Lerp2(ycoeff[3], t), - Lerp2(ycoeff[4], t)); - - float Y = SkyLuminance(theta, gamma, zY, sunTheta, Lerp2(Ycoeff[0], t), - Lerp2(Ycoeff[1], t), - Lerp2(Ycoeff[2], t), - Lerp2(Ycoeff[3], t), - Lerp2(Ycoeff[4], t)); - - - // convert Yxy to XYZ and then to RGB - Colour XYZ = YxyToXYZ(Y, x, y); - Colour RGB = XYZToLinear(XYZ.r, XYZ.g, XYZ.b); - - return RGB; -} - |