├── RayTracer ├── file.ppm ├── Source │ ├── Primitive.cpp │ ├── Scripting.h │ ├── QualityDesc.h │ ├── KDTree.h │ ├── Camera.h │ ├── ObjLoader.h │ ├── Sphere.h │ ├── Matrix4x4.h │ ├── Shape.h │ ├── PathRenderer.h │ ├── Shape.cpp │ ├── RayRenderer.h │ ├── Inlines.h │ ├── Renderer.h │ ├── Ray.h │ ├── Primitive.h │ ├── Camera.cpp │ ├── Point.h │ ├── Quaternion.h │ ├── BoundingBox.h │ ├── Ray.cpp │ ├── Normal.h │ ├── TriangleMesh.h │ ├── Point.cpp │ ├── Quaternion.cpp │ ├── Sphere.cpp │ ├── Normal.cpp │ ├── Vector.cpp │ ├── Vector.h │ ├── BoundingBox.cpp │ ├── Color.h │ ├── Matrix4x4.cpp │ ├── Transform.h │ ├── ObjLoader.cpp │ ├── Renderer.cpp │ ├── TriangleMesh.cpp │ ├── Color.cpp │ ├── KDTree.cpp │ ├── RayRenderer.cpp │ ├── Transform.cpp │ ├── PathRenderer.cpp │ └── Tracer.cpp └── RayTracer.vcxproj ├── Lua ├── Source │ ├── lua.hpp │ ├── lapi.h │ ├── lundump.h │ ├── lprefix.h │ ├── lualib.h │ ├── lstring.h │ ├── ldebug.h │ ├── ldo.h │ ├── lfunc.h │ ├── lzio.h │ ├── ltable.h │ ├── lzio.c │ ├── linit.c │ ├── ltm.h │ ├── lvm.h │ ├── lctype.h │ ├── lctype.c │ ├── llex.h │ ├── lmem.h │ ├── lmem.c │ ├── lcode.h │ ├── lparser.h │ ├── lopcodes.c │ ├── lfunc.c │ ├── ltm.c │ ├── lcorolib.c │ ├── lgc.h │ ├── lstring.c │ ├── ldump.c │ ├── lbitlib.c │ ├── llimits.h │ └── lundump.c └── Lua.vcxproj ├── LuaTest ├── main.cpp ├── LuaTest.vcxproj.filters └── LuaTest.vcxproj ├── ray.lua ├── RayTracer.sln ├── README.md └── .gitignore /RayTracer/file.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmoak3/Tracer/HEAD/RayTracer/file.ppm -------------------------------------------------------------------------------- /RayTracer/Source/Primitive.cpp: -------------------------------------------------------------------------------- 1 | #include "Primitive.h" 2 | 3 | int Primitive::currShapeID = 1; 4 | -------------------------------------------------------------------------------- /RayTracer/Source/Scripting.h: -------------------------------------------------------------------------------- 1 | #ifndef SCRIPTING_H 2 | #define SCRIPTING_H 3 | 4 | #include "Renderer.h" 5 | 6 | Renderer* init_renderer_from_script(char const * filename, std::string* err); 7 | 8 | #endif -------------------------------------------------------------------------------- /Lua/Source/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /RayTracer/Source/QualityDesc.h: -------------------------------------------------------------------------------- 1 | #ifndef QUALITYDESC_H 2 | #define QUALITYDESC_H 3 | 4 | struct QualityDesc 5 | { 6 | int Samples = 4; 7 | int LightSamples = 4; 8 | int GlossyReflectiveSamples = 4; 9 | int Depth = 1; 10 | 11 | //Makes it look better for less samples 12 | bool PathEnableDirectLighting = true; 13 | bool PathEnableIndirectIllum = true; 14 | }; 15 | 16 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/KDTree.h: -------------------------------------------------------------------------------- 1 | #ifndef KDTREE_H 2 | #define KDTREE_H 3 | #include 4 | #include "TriangleMesh.h" 5 | 6 | class KDNode 7 | { 8 | public: 9 | KDNode() {}; 10 | ~KDNode() {delete Left, Right;}; 11 | 12 | BoundingBox Bounds; 13 | KDNode *Left; 14 | KDNode *Right; 15 | std::vector Objects; 16 | bool Leaf; 17 | 18 | static KDNode * Build(std::vector *scene, int depth=0); 19 | bool Intersect(const Ray & ray, Hit * hit); 20 | }; 21 | 22 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Camera.h: -------------------------------------------------------------------------------- 1 | #ifndef CAMERA_H 2 | #define CAMERA_H 3 | #include "Vector.h" 4 | #include "Point.h" 5 | #include "Transform.h" 6 | //FIX ME 7 | class Camera 8 | { 9 | public: 10 | Camera() {}; 11 | Camera(const Transform &proj, const int width, const int height, float focalDist); 12 | ~Camera() {}; 13 | 14 | Transform ScreenToWorld, WorldToScreen; 15 | Transform RasterToScreen, RasterToWorld; 16 | Transform WorldToFarPlane; 17 | int width, height, focalDist; 18 | 19 | }; 20 | 21 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/ObjLoader.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJLOADER_H 2 | #define OBJLOADER_H 3 | #include "TriangleMesh.h" 4 | #include 5 | 6 | class ObjLoader 7 | { 8 | public: 9 | ObjLoader() {} 10 | ~ObjLoader() {} 11 | TriangleMesh Construct(char* file, const Transform * o2w, const Transform * w2o, Material material); 12 | 13 | bool LoadMesh(char* file, std::vector *points, 14 | std::vector * indices, 15 | std::vector * normals, 16 | std::vector * uv); 17 | }; 18 | 19 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Sphere.h: -------------------------------------------------------------------------------- 1 | #ifndef SPHERE_H 2 | #define SPHERE_H 3 | 4 | #include "Shape.h" 5 | #include "Point.h" 6 | #include "Ray.h" 7 | 8 | class Sphere : public Shape 9 | { 10 | public: 11 | Sphere(const Transform *o2w, const Transform *w2o, const Material &material, float r); 12 | ~Sphere() {}; 13 | bool Intersect(const Ray &ray, Hit *hit) const; 14 | bool CanIntersect() const; 15 | BoundingBox ObjectBound() const; 16 | BoundingBox WorldBound() const; 17 | Material GetMaterial() const; 18 | float Radius; 19 | BoundingBox ObjectBounds; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /LuaTest/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lua.hpp" 4 | 5 | static int l_print(lua_State* L) { 6 | const char* str = luaL_checkstring(L, 1); 7 | //std::cout << str << std::endl; 8 | std::cout << str; 9 | return 0; 10 | } 11 | 12 | int main(int argc, char* argv[]) { 13 | 14 | lua_State* L; 15 | 16 | L = luaL_newstate(); 17 | luaL_openlibs(L); 18 | 19 | lua_register(L, "print", l_print); 20 | 21 | luaL_dostring(L, "while true do print(\"hello world\\n\") end"); 22 | 23 | lua_close(L); 24 | 25 | getchar(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /RayTracer/Source/Matrix4x4.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX4X4_H 2 | #define MATRIX4X4_H 3 | 4 | struct Matrix4x4 5 | { 6 | Matrix4x4(); 7 | Matrix4x4(const float mat[4][4]); 8 | Matrix4x4(float t00, float t01, float t02, float t03, 9 | float t10, float t11, float t12, float t13, 10 | float t20, float t21, float t22, float t23, 11 | float t30, float t31, float t32, float t33); 12 | friend Matrix4x4 Transpose(const Matrix4x4 &mat); 13 | static Matrix4x4 Mul(const Matrix4x4 &m1, const Matrix4x4 &m2); 14 | friend Matrix4x4 Inverse(const Matrix4x4 &mat); 15 | bool operator==(const Matrix4x4 &mat); 16 | 17 | float m[4][4]; 18 | }; 19 | 20 | #endif -------------------------------------------------------------------------------- /Lua/Source/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /RayTracer/Source/Shape.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAPE_H 2 | #define SHAPE_H 3 | 4 | #include "Primitive.h" 5 | #include "Color.h" 6 | #include "Transform.h" 7 | #include "Ray.h" 8 | #include "Inlines.h" 9 | 10 | class Shape : public Primitive 11 | { 12 | public: 13 | Shape(const Transform *o2w, const Transform *w2o, const Material &material); 14 | virtual ~Shape() {}; 15 | 16 | virtual bool Intersect(const Ray &ray, Hit *hit) const; 17 | virtual bool CanIntersect() const; 18 | virtual BoundingBox ObjectBound() const; 19 | virtual BoundingBox WorldBound() const; 20 | virtual Material GetMaterial() const; 21 | 22 | Material Mat; 23 | }; 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /RayTracer/Source/PathRenderer.h: -------------------------------------------------------------------------------- 1 | #ifndef PATHRENDERER_H 2 | #define PATHRENDERER_H 3 | #include "Renderer.h" 4 | 5 | class PathRenderer : public Renderer 6 | { 7 | public: 8 | PathRenderer(std::vector* scene, const Camera &ccamera, const QualityDesc &quality); 9 | ~PathRenderer() {delete Lights;}; 10 | 11 | protected: 12 | RGB CalcDirectLighting(const Ray &reflRay, const Hit &hit, RNG &rng); 13 | void Render(); 14 | RGB Trace(const Ray &reflRay, RNG &rng); 15 | RGB DirectLightIndirectIllumTrace(const Ray &reflRay, RNG &rng); 16 | bool ShadowTest(const Ray &ray); 17 | std::vector *Lights; // for shadow tests (speed!) 18 | }; 19 | 20 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Shape.cpp: -------------------------------------------------------------------------------- 1 | #include "Shape.h" 2 | #include "Transform.h" 3 | 4 | 5 | Shape::Shape(const Transform *o2w, const Transform *w2o, const Material &material) 6 | : Primitive(0, o2w, w2o) 7 | { 8 | Mat = material; 9 | } 10 | 11 | bool Shape::Intersect(const Ray &ray, Hit * hit) const 12 | { 13 | return false; 14 | } 15 | 16 | bool Shape::CanIntersect() const 17 | { 18 | return true; 19 | } 20 | 21 | BoundingBox Shape::ObjectBound() const 22 | { 23 | return BoundingBox(); 24 | } 25 | 26 | BoundingBox Shape::WorldBound() const 27 | { 28 | return BoundingBox(); 29 | } 30 | 31 | Material Shape::GetMaterial() const 32 | { 33 | return Mat; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /RayTracer/Source/RayRenderer.h: -------------------------------------------------------------------------------- 1 | #ifndef RAYRENDERER_H 2 | #define RAYRENDERER_H 3 | #include "Renderer.h" 4 | 5 | class RayRenderer : public Renderer 6 | { 7 | public: 8 | RayRenderer(std::vector* scene, const Camera &ccamera, const QualityDesc &quality); 9 | ~RayRenderer() {delete Lights;}; 10 | 11 | protected: 12 | void Render(); 13 | RGB Trace(const Ray &reflRay, RNG &rng); 14 | bool ShadowTest(const Ray &ray); 15 | RGB computeColor(const Ray &reflRay, const Hit & hit, RNG &rng); 16 | 17 | std::vector *Lights; // for shadow tests (speed!) 18 | int LightSamples; 19 | int GlossyReflectiveSamples; 20 | float InvLightSamples; 21 | float InvGlossyReflectiveSamples; 22 | int Depth; 23 | }; 24 | 25 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Inlines.h: -------------------------------------------------------------------------------- 1 | #ifndef INLINES_H 2 | #define INLINES_H 3 | 4 | 5 | #include "Point.h" 6 | #include "Vector.h" 7 | #include "Normal.h" 8 | #include "Quaternion.h" 9 | #include "Camera.h" 10 | #include 11 | #include 12 | #include 13 | 14 | inline bool Quadratic(float A, float B, float C, float *t0, float *t1) 15 | { 16 | float discrim = (B*B - 4.f * A*C); 17 | if (discrim <= 0.f) return false; 18 | float rtDiscrim = sqrtf(discrim); 19 | 20 | float q; 21 | 22 | if (B < 0) 23 | q = -0.5f*(B - rtDiscrim); 24 | else 25 | q = -0.5f*(B + rtDiscrim); 26 | *t0 = q / A; 27 | *t1 = C / q; 28 | if (*t1 <= *t0) std::swap(*t0, *t1); 29 | return true; 30 | } 31 | 32 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Renderer.h: -------------------------------------------------------------------------------- 1 | #ifndef RENDERER_H 2 | #define RENDERER_H 3 | #include "Shape.h" 4 | #include "Camera.h" 5 | #include "QualityDesc.h" 6 | #include "KDTree.h" 7 | #include 8 | 9 | #include 10 | 11 | typedef std::mt19937 RNG; 12 | 13 | class Renderer 14 | { 15 | public: 16 | Renderer(std::vector* scene, const Camera &ccamera, const QualityDesc &quality); 17 | virtual ~Renderer() {delete Root, Scene;} 18 | virtual void Render(); 19 | 20 | protected: 21 | virtual bool FindClosest(const Ray &ray, Hit *hit); 22 | void ThreadedTrace(int y, RGB *imgBuffer, RNG &rng); 23 | virtual RGB Trace(const Ray &reflRay, RNG &rng); 24 | std::vector *Scene; 25 | Camera Cam; 26 | int Samples; 27 | int Height; 28 | int Width; 29 | float InvSamples; 30 | KDNode *Root; 31 | QualityDesc Quality; 32 | }; 33 | 34 | 35 | 36 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Ray.h: -------------------------------------------------------------------------------- 1 | #ifndef RAY_H 2 | #define RAY_H 3 | 4 | class Vector; 5 | class Point; 6 | #include "Color.h" 7 | #include 8 | #include "Point.h" 9 | #include "Vector.h" 10 | 11 | struct Hit 12 | { 13 | Normal normal = Normal(1.f, 0.f, 0.f); 14 | float tHit = INFINITY; 15 | float eps = 0.f; 16 | Material material; 17 | int shapeID = -1; 18 | int type = -1; 19 | }; 20 | 21 | 22 | class Ray 23 | { 24 | public: 25 | Ray(); 26 | Ray(const Point &oo, const Vector &dd, float start, float end = INFINITY, float t = 0.f, int dep = 0, float rIndex = 1.f); 27 | Ray(const Point &oo, const Vector &dd, const Ray &parent, float start, float end); 28 | ~Ray() {}; 29 | 30 | void UpdateInverse(); 31 | Point o; 32 | Vector d; 33 | float refrIndex; 34 | 35 | mutable float mint, maxt; 36 | float time; 37 | int sign[3]; 38 | int depth; 39 | Vector invd; 40 | }; 41 | 42 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Primitive.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIMITIVE_H 2 | #define PRIMITIVE_H 3 | 4 | #include "Point.h" 5 | #include "Color.h" 6 | #include "Transform.h" 7 | #include "BoundingBox.h" 8 | 9 | //TYPE 10 | //0 -> Shape 11 | //1 -> Light 12 | 13 | class Primitive 14 | { 15 | public: 16 | Primitive(const int type, const Transform *o2w, const Transform *w2o) 17 | :ObjectToWorld(o2w), WorldToObject(w2o) 18 | {Type = type; ShapeID = currShapeID; ++currShapeID;}; 19 | virtual ~Primitive() {}; 20 | 21 | virtual bool Intersect(const Ray &ray, Hit *hit) const = 0; 22 | virtual bool CanIntersect() const = 0; 23 | virtual BoundingBox ObjectBound() const = 0; 24 | virtual BoundingBox WorldBound() const = 0; 25 | virtual Material GetMaterial() const = 0; 26 | const Transform *WorldToObject, *ObjectToWorld; 27 | int Type; 28 | int ShapeID; 29 | static int currShapeID; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /RayTracer/Source/Camera.cpp: -------------------------------------------------------------------------------- 1 | #include "Camera.h" 2 | #include 3 | #include "Point.h" 4 | #include "Transform.h" 5 | #include 6 | 7 | Camera::Camera(const Transform &proj, const int width, const int height, float focalDist) 8 | : width(width), height(height), focalDist(focalDist) 9 | { 10 | assert(focalDist == height && width == height); 11 | ScreenToWorld = proj; 12 | WorldToScreen = Inverse(proj); 13 | Transform RasterToScreenTranslation = Translate(Vector(-1.f, 1.f, 0.f)); 14 | Transform RasterToScreenScale = Scale(2.f/(float)width, -2.f/(float)height, 0.f); 15 | RasterToScreen = RasterToScreenTranslation(RasterToScreenScale); 16 | RasterToWorld = ScreenToWorld(RasterToScreen); 17 | WorldToFarPlane = (ScreenToWorld) 18 | (Translate(Vector(0.f, 0.f,focalDist))) 19 | (Scale(width/2.f,height/2.f, 0.f)) 20 | (Translate(Vector(-1.f,1.f, 0.f))) 21 | (Scale(2.f/(float)width, -2.f/(float)height, 0.f)); 22 | } 23 | -------------------------------------------------------------------------------- /Lua/Source/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, 27 | const char* name); 28 | 29 | /* dump one chunk; from ldump.c */ 30 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 31 | void* data, int strip); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /LuaTest/LuaTest.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /Lua/Source/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /RayTracer/Source/Point.h: -------------------------------------------------------------------------------- 1 | #ifndef POINT_H 2 | #define POINT_H 3 | 4 | #include "Vector.h" 5 | 6 | 7 | class Point 8 | { 9 | public: 10 | Point(); 11 | Point(float xx, float yy, float zz); 12 | Point(const Vector & v); 13 | ~Point() {}; 14 | 15 | Point operator+(const Vector &v) const; 16 | Point& operator+=(const Vector &v); 17 | Point operator-(const Vector &v) const; 18 | Vector operator-(const Point &p) const; 19 | Point& operator-=(const Vector &v); 20 | Point operator/(float f) const; 21 | Point& operator/=(float f); 22 | float operator[](int i) const; 23 | float& operator[](int i); 24 | bool HasNans() const; 25 | 26 | float x, y, z; 27 | int filler; 28 | }; 29 | 30 | 31 | inline float Distance(const Point &p1, const Point &p2) { return (p1 - p2).Length(); } 32 | 33 | inline Point Average(const Point &p1, const Point &p2) 34 | { 35 | Point p((p1.x+p2.x)*0.5f, (p1.y+p2.y)*0.5f, (p1.z+p2.z)*0.5f); 36 | 37 | return p; 38 | } 39 | 40 | inline float DistanceSquared(const Point &p1, const Point &p2) { return Vector(p1 - p2).LengthSquared(); } 41 | 42 | #endif -------------------------------------------------------------------------------- /RayTracer/Source/Quaternion.h: -------------------------------------------------------------------------------- 1 | #ifndef QUATERNION_H 2 | #define QUATERNION_H 3 | #include "Vector.h" 4 | 5 | class Quaternion 6 | { 7 | public: 8 | Quaternion(); 9 | Quaternion(const Vector& vv, float ww); 10 | ~Quaternion() {}; 11 | 12 | Quaternion operator+(const Quaternion& q) const; 13 | Quaternion& operator+=(const Quaternion& q); 14 | 15 | Quaternion operator-(const Quaternion& q) const; 16 | Quaternion& operator-=(const Quaternion& q); 17 | 18 | Quaternion operator*(const Quaternion& q) const; 19 | Quaternion& operator*=(const Quaternion& q); 20 | 21 | Quaternion operator*(float f) const; 22 | Quaternion& operator*=(float f); 23 | 24 | Quaternion operator/(float f) const; 25 | Quaternion& operator/=(float f); 26 | 27 | Vector v; 28 | float w; 29 | }; 30 | 31 | inline float Dot(const Quaternion& q1, const Quaternion& q2) { return Dot(q1.v, q2.v) + q1.w * q2.w; } 32 | 33 | inline Quaternion Normalize(const Quaternion &q) { return q / sqrtf(Dot(q, q)); } 34 | 35 | inline Quaternion operator*(float f, const Quaternion &q) { return q*f; } 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /RayTracer/Source/BoundingBox.h: -------------------------------------------------------------------------------- 1 | #ifndef BOUNDINGBOX_H 2 | #define BOUNDINGBOX_H 3 | 4 | #include "Point.h" 5 | #include "Ray.h" 6 | #include 7 | 8 | class BoundingBox 9 | { 10 | public: 11 | BoundingBox() {}; 12 | BoundingBox(bool canInstersect) {Intersectable = canInstersect;}; 13 | BoundingBox(const Point& p); 14 | BoundingBox(const Point& min, const Point& max); 15 | BoundingBox(const BoundingBox & box, bool canIntersect = true); 16 | ~BoundingBox(); 17 | 18 | Point operator[](int i) const; 19 | 20 | Point GetCenter() const; 21 | bool CanIntersect() const; 22 | bool Intersect(const Ray& ray) const; 23 | bool Intersect(const Ray& ray, Hit * hit) const; 24 | bool Contains(const Point &p) const; 25 | bool Touches(const BoundingBox &box) const; 26 | Point Min, Max; // indexable thru array! 27 | bool Intersectable; 28 | }; 29 | 30 | inline BoundingBox Union(const BoundingBox &b1, const BoundingBox &b2) 31 | { 32 | BoundingBox newBB; 33 | newBB.Min.x = std::min(b1.Min.x, b2.Min.x); 34 | newBB.Min.y = std::min(b1.Min.y, b2.Min.y); 35 | newBB.Min.z = std::min(b1.Min.z, b2.Min.z); 36 | newBB.Max.x = std::max(b1.Max.x, b2.Max.x); 37 | newBB.Max.y = std::max(b1.Max.y, b2.Max.y); 38 | newBB.Max.z = std::max(b1.Max.z, b2.Max.z); 39 | return newBB; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /RayTracer/Source/Ray.cpp: -------------------------------------------------------------------------------- 1 | #include "Ray.h" 2 | #include "Point.h" 3 | #include "Vector.h" 4 | #include "Color.h" 5 | 6 | Ray::Ray() 7 | { 8 | o = Point(0.f, 0.f, 0.f); 9 | d = Vector(0.f, 0.f, 0.f); 10 | mint = 5e-4; 11 | maxt = INFINITY; 12 | time = 0.f; 13 | depth = 0; 14 | refrIndex = 0.f; 15 | invd = Vector(0.f,0.f,0.f); 16 | sign[0] = (invd.x < 0); 17 | sign[1] = (invd.y < 0); 18 | sign[2] = (invd.z < 0); 19 | } 20 | 21 | Ray::Ray(const Point &oo, const Vector &dd, float start, float end, float t, int dep, float rIndex) 22 | { 23 | o = oo; 24 | d = dd; 25 | mint = start; 26 | maxt = end; 27 | time = t; 28 | depth = dep; 29 | refrIndex = rIndex; 30 | invd = Vector(1.f/d.x, 1.f/d.y, 1.f/d.z); 31 | sign[0] = (invd.x < 0); 32 | sign[1] = (invd.y < 0); 33 | sign[2] = (invd.z < 0); 34 | } 35 | 36 | Ray::Ray(const Point &oo, const Vector &dd, const Ray &parent, float start, float end = INFINITY) 37 | { 38 | o = oo; 39 | d = dd; 40 | mint = start; 41 | maxt = end; 42 | time = parent.time; 43 | depth = parent.depth; 44 | refrIndex = 0.f; 45 | invd = Vector(1.f/d.x, 1.f/d.y, 1.f/d.z); 46 | sign[0] = (invd.x < 0); 47 | sign[1] = (invd.y < 0); 48 | sign[2] = (invd.z < 0); 49 | } 50 | 51 | void Ray::UpdateInverse() 52 | { 53 | invd = Vector(1.f/d.x, 1.f/d.y, 1.f/d.z); 54 | sign[0] = (invd.x < 0); 55 | sign[1] = (invd.y < 0); 56 | sign[2] = (invd.z < 0); 57 | } -------------------------------------------------------------------------------- /RayTracer/Source/Normal.h: -------------------------------------------------------------------------------- 1 | #ifndef NORMAL_H 2 | #define NORMAL_H 3 | 4 | class Vector; 5 | #include 6 | 7 | class Normal 8 | { 9 | public: 10 | explicit Normal(); 11 | explicit Normal(float xx, float yy, float zz); 12 | explicit Normal(const Vector &v); 13 | 14 | Normal operator+(const Normal &n) const; 15 | Normal& operator+=(const Normal &v); 16 | Normal operator-(const Normal &v) const; 17 | Normal& operator-=(const Normal &v); 18 | Normal operator*(float f) const; 19 | Normal& operator*=(float f); 20 | Normal operator/(float f) const; 21 | Normal& operator/=(float f); 22 | Normal operator-() const; 23 | float operator[](int i) const; 24 | float& operator[](int i); 25 | 26 | float Length() const; 27 | float LengthSquared() const; 28 | 29 | bool HasNans() const; 30 | 31 | float x, y, z; 32 | }; 33 | 34 | inline Normal Normalize(const Normal &n) 35 | { 36 | return n / n.Length(); 37 | } 38 | 39 | inline Normal Cross(const Normal &n1, const Normal &n2) 40 | { 41 | return Normal((n1.y * n2.z) - (n1.z * n2.y), 42 | (n1.z * n2.x) - (n1.x * n2.z), 43 | (n1.x * n2.y) - (n1.y * n2.x)); 44 | } 45 | 46 | inline float Dot(const Normal &n1, const Normal &n2) 47 | { 48 | return (n1.x*n2.x + n1.y*n2.y + n1.z*n2.z); 49 | } 50 | 51 | inline Normal operator*(float f, const Normal &n) { return n*f; } 52 | 53 | inline float AbsDot(const Normal &n1, const Normal &n2) { return fabsf(Dot(n1, n2)); } 54 | 55 | #endif -------------------------------------------------------------------------------- /Lua/Source/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_UTF8LIBNAME "utf8" 33 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 34 | 35 | #define LUA_BITLIBNAME "bit32" 36 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 37 | 38 | #define LUA_MATHLIBNAME "math" 39 | LUAMOD_API int (luaopen_math) (lua_State *L); 40 | 41 | #define LUA_DBLIBNAME "debug" 42 | LUAMOD_API int (luaopen_debug) (lua_State *L); 43 | 44 | #define LUA_LOADLIBNAME "package" 45 | LUAMOD_API int (luaopen_package) (lua_State *L); 46 | 47 | 48 | /* open all previous libraries */ 49 | LUALIB_API void (luaL_openlibs) (lua_State *L); 50 | 51 | 52 | 53 | #if !defined(lua_assert) 54 | #define lua_assert(x) ((void)0) 55 | #endif 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /RayTracer/Source/TriangleMesh.h: -------------------------------------------------------------------------------- 1 | #ifndef TRIANGLEMESH_H 2 | #define TRIANGLEMESH_H 3 | 4 | #include "Shape.h" 5 | #include 6 | 7 | class Triangle; 8 | 9 | class TriangleMesh : public Shape 10 | { 11 | public: 12 | TriangleMesh(const Transform * o2w, const Transform * w2o, Material material, int numTris, int numVerts, 13 | const int * vertIndices, const Point * vertPoints, 14 | const Normal * normals, const float * uv); 15 | ~TriangleMesh() {delete Triangles;}; 16 | 17 | void CalculateNormals(); 18 | bool CanIntersect() const; 19 | bool Intersect(const Ray &ray, Hit * hit) const; 20 | BoundingBox ObjectBound() const; // objectspace bbox 21 | BoundingBox WorldBound() const; // worldspace bbox, saves time 22 | int NumTris; 23 | int NumVerts; 24 | int * VertIndices; 25 | Point * VertPoints; 26 | Normal * Normals; 27 | float * UVs; 28 | BoundingBox WorldBounds; 29 | 30 | std::vector *Triangles; 31 | }; 32 | 33 | 34 | class Triangle : public Shape 35 | { 36 | public: 37 | Triangle(const Transform * o2w, const Transform *w2o, 38 | Material material, const TriangleMesh * mesh, int num); 39 | ~Triangle() {} 40 | 41 | bool Intersect(const Ray &ray, Hit * hit) const; 42 | bool CanIntersect() const; 43 | Normal GetNormal() const; 44 | BoundingBox ObjectBound() const; 45 | BoundingBox WorldBound() const; 46 | const TriangleMesh * Mesh; 47 | int *Vert; 48 | int Num; 49 | BoundingBox WorldBounds; 50 | }; 51 | 52 | #endif -------------------------------------------------------------------------------- /Lua/Source/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.56 2014/07/18 14:46:47 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | #define sizestring(s) sizelstring((s)->len) 17 | 18 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 19 | #define sizeudata(u) sizeludata((u)->len) 20 | 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Lua/Source/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.12 2014/11/10 14:46:05 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 29 | const TValue *p2, 30 | const char *msg); 31 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 32 | const TValue *p2); 33 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 34 | const TValue *p2); 35 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 36 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 37 | LUAI_FUNC void luaG_traceexec (lua_State *L); 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /RayTracer/Source/Point.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.h" 2 | #include "Vector.h" 3 | #include 4 | #include 5 | 6 | Point::Point() 7 | { 8 | x = y = z = 0.f; 9 | } 10 | 11 | Point::Point(float xx, float yy, float zz) 12 | { 13 | x = xx; y = yy; z = zz; 14 | assert(!HasNans()); 15 | } 16 | 17 | Point::Point(const Vector & v) 18 | { 19 | x = v.x; y = v.y; z = v.z; 20 | } 21 | 22 | Point Point::operator+(const Vector &v) const 23 | { 24 | return Point(x + v.x, y + v.y, z + v.z); 25 | } 26 | 27 | Point& Point::operator+=(const Vector &v) 28 | { 29 | x += v.x; y += v.y; z += v.z; 30 | return *this; 31 | } 32 | 33 | Point Point::operator-(const Vector &v) const 34 | { 35 | return Point(x - v.x, y - v.y, z - v.z); 36 | } 37 | 38 | Vector Point::operator-(const Point &p) const 39 | { 40 | return Vector(x - p.x, y - p.y, z - p.z); 41 | } 42 | 43 | Point& Point::operator-=(const Vector &v) 44 | { 45 | x -= v.x; y -= v.y; z -= v.z; 46 | return *this; 47 | } 48 | 49 | Point Point::operator/(float f) const 50 | { 51 | assert(f != 0); 52 | float inv = 1.f / f; 53 | return Point(x * inv, y * inv, z * inv); 54 | } 55 | 56 | Point& Point::operator/=(float f) 57 | { 58 | assert(f != 0); 59 | float inv = 1.f / f; 60 | x *= inv; y *= inv; z *= inv; 61 | return *this; 62 | } 63 | 64 | float Point::operator[](int i) const 65 | { 66 | assert(i >= 0 && i < 3); 67 | return (&x)[i]; 68 | } 69 | 70 | float& Point::operator[](int i) 71 | { 72 | assert(i >= 0 && i < 3); 73 | return (&x)[i]; 74 | } 75 | 76 | bool Point::HasNans() const 77 | { 78 | return isnan(x) || isnan(y) || isnan(z); 79 | } -------------------------------------------------------------------------------- /Lua/Source/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by 'runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /Lua/Source/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** Upvalues for Lua closures 27 | */ 28 | struct UpVal { 29 | TValue *v; /* points to stack or to its own value */ 30 | lu_mem refcount; /* reference counter */ 31 | union { 32 | struct { /* (when open) */ 33 | UpVal *next; /* linked list */ 34 | int touched; /* mark to avoid cycles with dead threads */ 35 | } open; 36 | TValue value; /* the value (when closed) */ 37 | } u; 38 | }; 39 | 40 | #define upisopen(up) ((up)->v != &(up)->u.value) 41 | 42 | 43 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 44 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 45 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 46 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 47 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 48 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 49 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 50 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 51 | int pc); 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /RayTracer/Source/Quaternion.cpp: -------------------------------------------------------------------------------- 1 | #include "Quaternion.h" 2 | #include "Inlines.h" 3 | 4 | 5 | Quaternion::Quaternion() 6 | { 7 | v = Vector(0.f, 0.f, 0.f); 8 | w = 1.f; 9 | } 10 | 11 | Quaternion::Quaternion(const Vector& vv, float ww) 12 | { 13 | v = vv; 14 | w = ww; 15 | } 16 | 17 | Quaternion Quaternion::operator+(const Quaternion& q) const 18 | { 19 | return Quaternion(v + q.v, w + q.w); 20 | } 21 | 22 | Quaternion& Quaternion::operator+=(const Quaternion& q) 23 | { 24 | v += q.v; 25 | w += q.w; 26 | return *this; 27 | } 28 | 29 | Quaternion Quaternion::operator-(const Quaternion& q) const 30 | { 31 | return Quaternion(v - q.v, w - q.w); 32 | } 33 | 34 | Quaternion& Quaternion::operator-=(const Quaternion& q) 35 | { 36 | v -= q.v; 37 | w -= q.w; 38 | return *this; 39 | } 40 | 41 | Quaternion Quaternion::operator*(const Quaternion& q) const 42 | { 43 | return Quaternion( 44 | Cross(v, q.v) + w * q.v + q.w * v, 45 | w * q.w - Dot(v, q.v)); 46 | } 47 | 48 | Quaternion& Quaternion::operator*=(const Quaternion& q) 49 | { 50 | float ww = w; 51 | w = ww * q.w - Dot(v, q.v); 52 | v = Cross(v, q.v) + ww * q.v + q.w * v; 53 | return *this; 54 | } 55 | 56 | Quaternion Quaternion::operator*(float f) const 57 | { 58 | return Quaternion(v * f, w * f); 59 | } 60 | 61 | Quaternion& Quaternion::operator*=(float f) 62 | { 63 | v *= f; 64 | w *= f; 65 | return *this; 66 | } 67 | 68 | Quaternion Quaternion::operator/(float f) const 69 | { 70 | float inv = 1.f / f; 71 | return Quaternion(v * inv, f * inv); 72 | } 73 | 74 | Quaternion& Quaternion::operator/=(float f) 75 | { 76 | float inv = 1.f / f; 77 | v *= inv; 78 | w *= inv; 79 | return *this; 80 | } -------------------------------------------------------------------------------- /Lua/Source/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 48 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 49 | void *data); 50 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; /* reader function */ 60 | void *data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Lua/Source/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | #define wgkey(n) (&(n)->i_key.nk) 22 | 23 | #define invalidateTMcache(t) ((t)->flags = 0) 24 | 25 | 26 | /* returns the key, given the value of a table entry */ 27 | #define keyfromval(v) \ 28 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 29 | 30 | 31 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 32 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 33 | TValue *value); 34 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 35 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 36 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 37 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 38 | LUAI_FUNC Table *luaH_new (lua_State *L); 39 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 40 | unsigned int nhsize); 41 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 42 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 43 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 44 | LUAI_FUNC int luaH_getn (Table *t); 45 | 46 | 47 | #if defined(LUA_DEBUG) 48 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 49 | LUAI_FUNC int luaH_isdummy (Node *n); 50 | #endif 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Lua/Source/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | /* ------------------------------------------------------------------------ */ 70 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 71 | if (n > buff->buffsize) { 72 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 73 | luaZ_resizebuffer(L, buff, n); 74 | } 75 | return buff->buffer; 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /Lua/Source/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove _PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {"_G", luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | #if defined(LUA_COMPAT_BITLIB) 54 | {LUA_BITLIBNAME, luaopen_bit32}, 55 | #endif 56 | {NULL, NULL} 57 | }; 58 | 59 | 60 | LUALIB_API void luaL_openlibs (lua_State *L) { 61 | const luaL_Reg *lib; 62 | /* "require" functions from 'loadedlibs' and set results to global table */ 63 | for (lib = loadedlibs; lib->func; lib++) { 64 | luaL_requiref(L, lib->name, lib->func, 1); 65 | lua_pop(L, 1); /* remove lib */ 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /RayTracer/Source/Sphere.cpp: -------------------------------------------------------------------------------- 1 | #include "Sphere.h" 2 | #include "Point.h" 3 | #include "Ray.h" 4 | #include "Inlines.h" 5 | #include "Transform.h" 6 | #include "Color.h" 7 | 8 | Sphere::Sphere(const Transform *o2w, const Transform *w2o, const Material &material, float r) 9 | : Shape(o2w, w2o, material) 10 | { 11 | Radius = r; 12 | 13 | //This is not ok, fix this ASAP 14 | if (material.Emissive>0.f) Type = 1; 15 | 16 | ObjectBounds = ObjectBound(); 17 | } 18 | 19 | bool Sphere::CanIntersect() const 20 | { 21 | return true; 22 | } 23 | 24 | bool Sphere::Intersect(const Ray &ray, Hit *hit) const 25 | { 26 | //if (!CanIntersect()) 27 | // return false; 28 | 29 | Ray r; 30 | (*WorldToObject)(ray, &r); 31 | 32 | //SLOWER WITH BOUNDING BOX? 33 | if (!ObjectBounds.Intersect(r)) 34 | return false; 35 | 36 | float A = r.d.x*r.d.x + r.d.y*r.d.y + r.d.z*r.d.z; 37 | float B = 2.f*(r.d.x*r.o.x + r.d.y*r.o.y + r.d.z*r.o.z); 38 | float C = r.o.x*r.o.x + r.o.y*r.o.y + r.o.z*r.o.z - Radius*Radius; 39 | 40 | float t0, t1 = 0.f; 41 | if (!Quadratic(A, B, C, &t0, &t1)) 42 | return false; 43 | 44 | if (t0 > r.maxt || t1 < r.mint) 45 | return false; 46 | float thit = t0; 47 | if (t0 < r.mint) 48 | { 49 | thit = t1; 50 | if (thit > r.maxt) 51 | return false; 52 | } 53 | Point hitOnSphere = r.o + r.d*thit; 54 | 55 | Normal normal(hitOnSphere-Point(0.f, 0.f, 0.f)); 56 | normal = Normalize(normal); 57 | 58 | (*ObjectToWorld)(normal, &normal); 59 | 60 | hit->tHit = thit; 61 | hit->normal = normal; 62 | hit->material = GetMaterial(); 63 | hit->shapeID = ShapeID; 64 | hit->eps = 5e-4 * thit; 65 | hit->type = Type; 66 | 67 | return true; 68 | } 69 | 70 | Material Sphere::GetMaterial() const 71 | { 72 | return Mat; 73 | } 74 | 75 | BoundingBox Sphere::ObjectBound() const 76 | { 77 | return BoundingBox(Point(-Radius, -Radius, -Radius), Point(Radius, Radius, Radius)); 78 | } 79 | 80 | BoundingBox Sphere::WorldBound() const 81 | { 82 | return (*ObjectToWorld)(BoundingBox(Point(-Radius, -Radius, -Radius), Point(Radius, Radius, Radius))); 83 | } -------------------------------------------------------------------------------- /RayTracer/Source/Normal.cpp: -------------------------------------------------------------------------------- 1 | #include "Normal.h" 2 | #include "Vector.h" 3 | #include 4 | #include 5 | 6 | Normal::Normal() 7 | { 8 | x = y = z = 0.f; 9 | } 10 | 11 | Normal::Normal(float xx, float yy, float zz) 12 | { 13 | x = xx; y = yy; z = zz; 14 | } 15 | 16 | Normal::Normal(const Vector &v) 17 | { 18 | x = v.x; y = v.y; z = v.z; 19 | } 20 | 21 | Normal Normal::operator+(const Normal &n) const 22 | { 23 | return Normal(x + n.x, y + n.y, z + n.z); 24 | } 25 | 26 | Normal& Normal::operator+=(const Normal &n) 27 | { 28 | x += n.x; y += n.y; z += n.z; 29 | return *this; 30 | } 31 | 32 | Normal Normal::operator-(const Normal &n) const 33 | { 34 | return Normal(x - n.x, y - n.y, z - n.z); 35 | } 36 | 37 | Normal& Normal::operator-=(const Normal &n) 38 | { 39 | x -= n.x; y -= n.y; z -= n.z; 40 | return *this; 41 | } 42 | 43 | Normal Normal::operator*(float f) const 44 | { 45 | return Normal(x * f, y * f, z * f); 46 | } 47 | 48 | Normal& Normal::operator*=(float f) 49 | { 50 | x *= f; y *= f; z *= f; 51 | return *this; 52 | } 53 | 54 | Normal Normal::operator/(float f) const 55 | { 56 | assert(f != 0.f); 57 | float inv = 1.f / f; 58 | return Normal(x * inv, y * inv, z * inv); 59 | } 60 | 61 | Normal& Normal::operator/=(float f) 62 | { 63 | assert(f != 0.f); 64 | float inv = 1.f / f; 65 | x *= inv; y *= inv; z *= inv; 66 | return *this; 67 | } 68 | 69 | Normal Normal::operator-() const 70 | { 71 | return Normal(-x, -y, -z); 72 | } 73 | 74 | float Normal::operator[](int i) const 75 | { 76 | assert(i > 0 && i < 3); 77 | return (&x)[i]; 78 | } 79 | 80 | float& Normal::operator[](int i) 81 | { 82 | assert(i > 0 && i < 3); 83 | return (&x)[i]; 84 | } 85 | 86 | float Normal::Length() const 87 | { 88 | return sqrtf(LengthSquared()); 89 | } 90 | 91 | float Normal::LengthSquared() const 92 | { 93 | return (x*x + y*y + z*z); 94 | } 95 | 96 | bool Normal::HasNans() const 97 | { 98 | return isnan(x) || isnan(y) || isnan(z); 99 | } 100 | -------------------------------------------------------------------------------- /Lua/Source/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | #define objtypename(x) ttypename(ttnov(x)) 55 | 56 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 57 | 58 | 59 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 60 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 61 | TMS event); 62 | LUAI_FUNC void luaT_init (lua_State *L); 63 | 64 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 65 | const TValue *p2, TValue *p3, int hasres); 66 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 67 | StkId res, TMS event); 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 69 | StkId res, TMS event); 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 71 | const TValue *p2, TMS event); 72 | 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /Lua/Source/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | #define tonumber(o,n) \ 31 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 32 | 33 | #define tointeger(o,i) \ 34 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) 35 | 36 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 37 | 38 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 39 | 40 | 41 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 42 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 43 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 44 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 45 | LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); 46 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 47 | StkId val); 48 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 49 | StkId val); 50 | LUAI_FUNC void luaV_finishOp (lua_State *L); 51 | LUAI_FUNC void luaV_execute (lua_State *L); 52 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 53 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); 54 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 55 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 56 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /ray.lua: -------------------------------------------------------------------------------- 1 | white = rgb.new(1, 1, 1) 2 | gray = rgb.new(0.6, 0.6, 0.6) 3 | 4 | dom = 0.85 5 | sub = 0.4 6 | 7 | whiteWall = material.new() 8 | whiteWall.Color = rgb.new(dom, dom, dom) 9 | whiteWall.Specular = 0 10 | whiteWall.Diffuse = 1 11 | whiteWall.GlossyReflective = 1 12 | whiteWall.Reflective = 1 13 | whiteWall.Refractive = 1 14 | whiteWall.RefrAbsorbance = 1 15 | 16 | grayWall = material.new() 17 | grayWall.Color = gray 18 | grayWall.Specular = 0 19 | grayWall.Diffuse = 1 20 | grayWall.GlossyReflective = 1 21 | grayWall.Reflective = 1 22 | grayWall.Refractive = 1 23 | grayWall.RefrAbsorbance = 1 24 | 25 | blueWall = material.new() 26 | blueWall.Color = rgb.new(sub, sub, dom) 27 | blueWall.Specular = 0 28 | blueWall.Diffuse = 1 29 | blueWall.GlossyReflective = 1 30 | blueWall.Reflective = 1 31 | blueWall.Refractive = 1 32 | blueWall.RefrAbsorbance = 1 33 | 34 | redWall = material.new() 35 | redWall.Color = rgb.new(dom, sub, sub) 36 | redWall.Specular = 0 37 | redWall.Diffuse = 1 38 | redWall.GlossyReflective = 1 39 | redWall.Reflective = 1 40 | redWall.Refractive = 1 41 | redWall.RefrAbsorbance = 1 42 | 43 | mat = material.new() 44 | mat.Color = rgb.new(1, 0, 0) 45 | mat.Specular = 0.8 46 | mat.Diffuse = 1.0 47 | mat.GlossyReflective = 0.0 48 | mat.Reflective = 1.0 49 | mat.Refractive = 1.0 50 | mat.RefrAbsorbance = 1.0 51 | 52 | scene.add("Sphere", whiteWall, 160, 0, 163, 0) 53 | scene.add("Sphere", whiteWall, 160, 0, -163, 0) 54 | scene.add("Sphere", whiteWall, 160, 0, 0, -170) 55 | scene.add("Sphere", whiteWall, 160, 0, 0, 163) 56 | scene.add("Sphere", blueWall, 160, -163, 0, 0) 57 | scene.add("Sphere", redWall, 160, 163, 0, 0) 58 | 59 | scene.add("Sphere", mat, 0.8, 1.0, -2.2, -2.0) 60 | 61 | lightmat = material.new() 62 | lightmat.Color = white 63 | lightmat.Emissive = 3.0 64 | 65 | scene.add("Sphere", lightmat, 1.0, 0.0, 1.9, 1.0) 66 | 67 | t = transform.translate(vector.new(0, -1, -8)) 68 | 69 | dim = 512 70 | scene.set("Camera", t, dim, dim, dim) 71 | 72 | scene.set("LightSamples", 1) 73 | scene.set("GlossyReflectiveSamples", 1) 74 | scene.set("Depth", 1) 75 | scene.set("Samples", 1) 76 | scene.set("PathEnableDirectLighting", true) 77 | scene.set("PathEnableIndirectIllum", true) 78 | 79 | -------------------------------------------------------------------------------- /Lua/Source/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /RayTracer/Source/Vector.cpp: -------------------------------------------------------------------------------- 1 | #include "Vector.h" 2 | #include "Normal.h" 3 | #include "Point.h" 4 | #include 5 | #include 6 | 7 | Vector::Vector() 8 | { 9 | x = y = z = 0.f; 10 | } 11 | 12 | Vector::Vector(float xx, float yy, float zz) 13 | { 14 | x = xx; y = yy; z = zz; 15 | assert(!HasNans()); 16 | } 17 | 18 | Vector::Vector(const Point &p1, const Point &p2) 19 | { 20 | x = p2.x - p1.x; y = p2.y - p1.y; z = p2.z - p1.z; 21 | } 22 | 23 | Vector::Vector(const Normal &n) 24 | { 25 | x = n.x; y = n.y; z = n.z; 26 | } 27 | 28 | Vector::Vector(const Vector &v, bool ignoreNaNs) 29 | { 30 | x = v.x; y = v.y; z = v.z; 31 | if (!ignoreNaNs) 32 | assert(!HasNans()); 33 | } 34 | 35 | Vector Vector::operator+(const Vector &v) const 36 | { 37 | return Vector(x + v.x, y + v.y, z + v.z); 38 | } 39 | 40 | Vector& Vector::operator+=(const Vector &v) 41 | { 42 | x += v.x; y += v.y; z += v.z; 43 | return *this; 44 | } 45 | 46 | Vector Vector::operator-(const Vector &v) const 47 | { 48 | return Vector(x - v.x, y - v.y, z - v.z); 49 | } 50 | 51 | Vector& Vector::operator-=(const Vector &v) 52 | { 53 | x -= v.x; y -= v.y; z -= v.z; 54 | return *this; 55 | } 56 | 57 | Vector Vector::operator-() const 58 | { 59 | return Vector(-x, -y, -z); 60 | } 61 | 62 | 63 | Vector Vector::operator*(float f) const 64 | { 65 | return Vector(x * f, y * f, z * f); 66 | } 67 | 68 | Vector& Vector::operator*=(float f) 69 | { 70 | x *= f; y *= f; z *= f; 71 | return *this; 72 | } 73 | 74 | Vector Vector::operator/(float f) const 75 | { 76 | if (f==0.f) 77 | bool dsa = false; 78 | assert(f != 0.f); 79 | float inv = 1.f / f; 80 | return Vector(x * inv, y * inv, z * inv); 81 | } 82 | 83 | Vector& Vector::operator/=(float f) 84 | { 85 | assert(f != 0); 86 | float inv = 1.f / f; 87 | x *= inv; y *= inv; z *= inv; 88 | return *this; 89 | } 90 | 91 | float Vector::operator[](int i) const 92 | { 93 | assert(i >= 0 && i < 3); 94 | return (&x)[i]; 95 | } 96 | 97 | float& Vector::operator[](int i) 98 | { 99 | assert(i >= 0 && i < 3); 100 | return (&x)[i]; 101 | } 102 | 103 | float Vector::Length() const 104 | { 105 | return sqrtf(LengthSquared()); 106 | } 107 | 108 | float Vector::LengthSquared() const 109 | { 110 | return (x*x + y*y + z*z); 111 | } 112 | 113 | bool Vector::HasNans() const 114 | { 115 | return isnan(x) || isnan(y) || isnan(z); 116 | } 117 | 118 | -------------------------------------------------------------------------------- /Lua/Source/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /RayTracer/Source/Vector.h: -------------------------------------------------------------------------------- 1 | #ifndef VECTOR_H 2 | #define VECTOR_H 3 | 4 | #include "Normal.h" 5 | #include 6 | #include 7 | 8 | class Point; 9 | 10 | class Vector 11 | { 12 | public: 13 | Vector(); 14 | Vector(float xx, float yy, float zz); 15 | Vector(const Point &p1, const Point &p2); 16 | explicit Vector(const Normal &n); 17 | Vector(const Vector &v, bool ignoreNaNs = false); 18 | ~Vector() {}; 19 | 20 | Vector operator+(const Vector &v) const; 21 | Vector& operator+=(const Vector &v); 22 | Vector operator-(const Vector &v) const; 23 | Vector& operator-=(const Vector &v); 24 | Vector operator*(float f) const; 25 | Vector& operator*=(float f); 26 | Vector operator/(float f) const; 27 | Vector& operator/=(float f); 28 | Vector operator-() const; 29 | float operator[](int i) const; 30 | float& operator[](int i); 31 | 32 | float Length() const; 33 | float LengthSquared() const; 34 | 35 | bool HasNans() const; 36 | 37 | float x, y, z; 38 | int filler; 39 | }; 40 | 41 | inline Vector Normalize(const Vector &v) 42 | { 43 | return v / v.Length(); 44 | } 45 | 46 | inline Vector Cross(const Vector &v1, const Vector &v2) 47 | { 48 | return Vector((v1.y * v2.z) - (v1.z * v2.y), 49 | (v1.z * v2.x) - (v1.x * v2.z), 50 | (v1.x * v2.y) - (v1.y * v2.x)); 51 | } 52 | 53 | 54 | inline float Dot(const Vector &v1, const Normal &n2) { return (v1.x*n2.x + v1.y*n2.y + v1.z*n2.z); } 55 | 56 | inline float Dot(const Normal &n1, const Vector &v2) { return (n1.x*v2.x + n1.y*v2.y + n1.z*v2.z); } 57 | 58 | inline float Dot(const Vector &v1, const Vector &v2) { return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); } 59 | 60 | inline float AbsDot(const Vector &v1, const Vector &v2) { return fabsf(Dot(v1, v2)); } 61 | 62 | inline Vector operator*(float f, const Vector &v) { return v*f; } 63 | 64 | inline void CoordinateSystem(Vector &v1, Vector &v2, Vector &v3) 65 | { 66 | if (fabsf(v1.x) > fabsf(v1.y)) 67 | { 68 | float invLen = 1.f / (fabsf(v1.x*v1.x + v1.z*v1.z)); 69 | v2 = Vector(-v1.z * invLen, 0.f, v1.x * invLen); 70 | } 71 | else 72 | { 73 | float invLen = 1.f / (fabsf(v1.y*v1.y + v1.z*v1.z)); 74 | v2 = Vector(0.f, v1.z * invLen, -v1.y * invLen); 75 | } 76 | v3 = Cross(v1, v2); 77 | } 78 | 79 | inline Vector Lerp(const Vector & v1, const Vector & v2, float t) 80 | { 81 | return v1 + t*(v2-v1); 82 | } 83 | 84 | inline Vector SLerpNormalized(const Vector & v1, const Vector & v2, float t) 85 | { 86 | float theta = acos(Dot(v1, v2)); 87 | float sinTheta = sin(theta); 88 | return (sin((1.f-t)*theta)/sinTheta *v1 + sin(t*theta)/sinTheta *v2); 89 | } 90 | 91 | #endif -------------------------------------------------------------------------------- /Lua/Source/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | char decpoint; /* locale decimal point */ 73 | } LexState; 74 | 75 | 76 | LUAI_FUNC void luaX_init (lua_State *L); 77 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 78 | TString *source, int firstchar); 79 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 80 | LUAI_FUNC void luaX_next (LexState *ls); 81 | LUAI_FUNC int luaX_lookahead (LexState *ls); 82 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 83 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 84 | 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /Lua/Source/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /RayTracer/Source/BoundingBox.cpp: -------------------------------------------------------------------------------- 1 | #include "BoundingBox.h" 2 | #include 3 | #include 4 | BoundingBox::BoundingBox(const Point& p) 5 | { 6 | Min = p; 7 | Max = p; 8 | Intersectable = true; 9 | } 10 | 11 | BoundingBox::BoundingBox(const Point& min, const Point& max) 12 | { 13 | Min = min; 14 | Max = max; 15 | Intersectable = true; 16 | } 17 | 18 | BoundingBox::BoundingBox(const BoundingBox & box, bool canIntersect) 19 | { 20 | Min = box.Min; 21 | Max = box.Max; 22 | Intersectable = canIntersect; 23 | } 24 | 25 | BoundingBox::~BoundingBox() 26 | { 27 | } 28 | 29 | Point BoundingBox::operator[](int i) const 30 | { 31 | return (&Min)[i*12]; 32 | } 33 | 34 | bool BoundingBox::CanIntersect() const 35 | { 36 | return Intersectable; 37 | } 38 | 39 | Point BoundingBox::GetCenter() const 40 | { 41 | Point center = Average(Min, Max); 42 | return center; 43 | } 44 | 45 | bool BoundingBox::Intersect(const Ray& ray) const 46 | { 47 | float tmin, tmax, tymin, tymax, tzmin, tzmax; 48 | tmin = ((&Min)[1*ray.sign[0]].x - ray.o.x) * ray.invd.x; 49 | tmax = ((&Min)[1 - 1*ray.sign[0]].x - ray.o.x) * ray.invd.x; 50 | tymin = ((&Min)[1*ray.sign[1]].y - ray.o.y) * ray.invd.y; 51 | tymax = ((&Min)[1-1*ray.sign[1]].y - ray.o.y) * ray.invd.y; 52 | if ((tmin > tymax) || (tymin > tmax)) 53 | return false; 54 | if (tymin > tmin) 55 | tmin = tymin; 56 | if (tymax < tmax) 57 | tmax = tymax; 58 | tzmin = ((&Min)[1*ray.sign[2]].z - ray.o.z) * ray.invd.z; 59 | tzmax = ((&Min)[1-1*ray.sign[2]].z - ray.o.z) * ray.invd.z; 60 | if ((tmin > tzmax) || (tzmin > tmax)) 61 | return false; 62 | if (tzmin > tmin) 63 | tmin = tzmin; 64 | if (tzmax < tmax) 65 | tmax = tzmax; 66 | return ((tmax > ray.mint) && (tmin < ray.maxt)); 67 | } 68 | 69 | bool BoundingBox::Intersect(const Ray& ray, Hit * hit) const 70 | { 71 | float t0 = ray.mint; 72 | float t1 = ray.maxt; 73 | for (int i = 0; i < 3; ++i) 74 | { 75 | float invRayDir = 1.f / ray.d[i]; 76 | float tNear = (Min[i] - ray.o[i]) * invRayDir; 77 | float tFar = (Max[i] - ray.o[i]) * invRayDir; 78 | if (tNear > tFar) 79 | std::swap(tNear, tFar); 80 | t0 = tNear > t0 ? tNear : t0; 81 | t1 = tFar < t1 ? tFar : t1; 82 | if (t0 > t1) return false; 83 | } 84 | return true; 85 | } 86 | 87 | bool BoundingBox::Contains(const Point& p) const 88 | { 89 | bool in = p.x >= Min.x && p.x <= Max.x; 90 | in = in && p.y >= Min.y && p.y <= Max.y; 91 | in = in && p.z >= Min.z && p.z <= Max.z; 92 | return in; 93 | } 94 | 95 | bool BoundingBox::Touches(const BoundingBox &box) const 96 | { 97 | return (((Max.z > box.Min.z) && (Min.z < box.Max.z)) && 98 | ((Max.x > box.Min.x) && (Min.x < box.Max.x)) && 99 | ((Max.y > box.Min.y) && (Min.y < box.Max.y))); 100 | } -------------------------------------------------------------------------------- /RayTracer/Source/Color.h: -------------------------------------------------------------------------------- 1 | #ifndef COLOR_H 2 | #define COLOR_H 3 | 4 | class Ray; 5 | struct Hit; 6 | class Vector; 7 | #include 8 | #include 9 | typedef std::mt19937 RNG; 10 | 11 | struct RGB 12 | { 13 | float red=0.f, green=0.f, blue=0.f; 14 | RGB() 15 | { 16 | red = 0.f; 17 | green = 0.f; 18 | blue = 0.f; 19 | } 20 | ~RGB() {} 21 | RGB(float r, float g, float b) 22 | { 23 | red = r; 24 | green = g; 25 | blue = b; 26 | } 27 | RGB(bool white) 28 | { 29 | if (white) 30 | { 31 | red = 1.f; 32 | green = 1.f; 33 | blue = 1.f; 34 | } 35 | else 36 | { 37 | red = 0.f; 38 | green = 0.f; 39 | blue = 0.f; 40 | } 41 | } 42 | RGB operator*(float f) const 43 | { 44 | RGB c; 45 | c.red = f * red; 46 | c.blue = f * blue; 47 | c.green = f * green; 48 | return c; 49 | } 50 | RGB operator*(const RGB & col) const 51 | { 52 | RGB c; 53 | c.red = col.red * red; 54 | c.blue = col.blue * blue; 55 | c.green = col.green * green; 56 | return c; 57 | } 58 | RGB operator+(const RGB &col) const 59 | { 60 | RGB c; 61 | c.red = col.red + red; 62 | c.blue = col.blue + blue; 63 | c.green = col.green + green; 64 | return c; 65 | } 66 | RGB operator-(const RGB &col) const 67 | { 68 | RGB c; 69 | c.red = red - col.red; 70 | c.blue = blue - col.blue; 71 | c.green = green - col.green; 72 | return c; 73 | } 74 | RGB& operator*=(float f) 75 | { 76 | *this = *this * f; 77 | return *this; 78 | } 79 | RGB& operator*=(const RGB & col) 80 | { 81 | *this = *this * col; 82 | return *this; 83 | } 84 | RGB& operator+=(const RGB &col) 85 | { 86 | *this = *this + col; 87 | return *this; 88 | } 89 | RGB& operator-=(const RGB &col) 90 | { 91 | *this = *this - col; 92 | return *this; 93 | } 94 | 95 | void Bound(float min, float max) 96 | { 97 | red = std::min(red, max); 98 | green = std::min(green, max); 99 | blue = std::min(blue, max); 100 | 101 | red = std::max(red, min); 102 | green = std::max(green, min); 103 | blue = std::max(blue, min); 104 | } 105 | 106 | bool IsBlack() const 107 | { 108 | return (red < 0.001f && green < 0.001f && blue < 0.001f); 109 | } 110 | }; 111 | 112 | class Material 113 | { 114 | public: 115 | Material() {Specular = 1.f; Diffuse = 1.f; Reflective = 1.f; 116 | GlossyReflective = 0.0f; Emissive = 0.f; 117 | Refractive = 1.0f; RefrAbsorbance = 1.f;}; 118 | ~Material() {}; 119 | 120 | Ray ReflectRay(const Ray &ray, const Hit &hit, bool path, RNG &rng) const; 121 | Ray RefractRay(const Ray &ray, const Hit &hit, bool * isValid) const; 122 | Ray CalcReflectLerp(const Ray &ray, Ray &r, const Hit &hit, bool path, RNG &rng) const; 123 | Ray CalcReflectApprox(const Ray &ray, Ray &r, const Hit &hit, RNG &rng) const; 124 | RGB Color; 125 | float Specular, Diffuse, Reflective, GlossyReflective, Refractive, RefrAbsorbance; 126 | float Emissive; 127 | }; 128 | 129 | #endif 130 | -------------------------------------------------------------------------------- /RayTracer.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30501.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RayTracer", "RayTracer\RayTracer.vcxproj", "{DB9CB3C5-C472-4108-992D-A92BFB8396DA}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lua", "Lua\Lua.vcxproj", "{50B3B885-208D-4847-80FF-0476845F710C}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LuaTest", "LuaTest\LuaTest.vcxproj", "{2A94B8D9-37F0-451B-B450-9D94F8AD0127}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|ARM = Debug|ARM 15 | Debug|Win32 = Debug|Win32 16 | Debug|x64 = Debug|x64 17 | Release|ARM = Release|ARM 18 | Release|Win32 = Release|Win32 19 | Release|x64 = Release|x64 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Debug|ARM.ActiveCfg = Debug|Win32 23 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Debug|Win32.ActiveCfg = Debug|Win32 24 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Debug|Win32.Build.0 = Debug|Win32 25 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Debug|x64.ActiveCfg = Debug|Win32 26 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Release|ARM.ActiveCfg = Release|Win32 27 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Release|Win32.ActiveCfg = Release|Win32 28 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Release|Win32.Build.0 = Release|Win32 29 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Release|x64.ActiveCfg = Release|Win32 30 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA}.Release|x64.Build.0 = Release|Win32 31 | {50B3B885-208D-4847-80FF-0476845F710C}.Debug|ARM.ActiveCfg = Debug|Win32 32 | {50B3B885-208D-4847-80FF-0476845F710C}.Debug|Win32.ActiveCfg = Debug|Win32 33 | {50B3B885-208D-4847-80FF-0476845F710C}.Debug|x64.ActiveCfg = Debug|Win32 34 | {50B3B885-208D-4847-80FF-0476845F710C}.Release|ARM.ActiveCfg = Release|Win32 35 | {50B3B885-208D-4847-80FF-0476845F710C}.Release|Win32.ActiveCfg = Release|Win32 36 | {50B3B885-208D-4847-80FF-0476845F710C}.Release|Win32.Build.0 = Release|Win32 37 | {50B3B885-208D-4847-80FF-0476845F710C}.Release|x64.ActiveCfg = Release|Win32 38 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Debug|ARM.ActiveCfg = Debug|Win32 39 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Debug|Win32.ActiveCfg = Debug|Win32 40 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Debug|x64.ActiveCfg = Debug|Win32 41 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Release|ARM.ActiveCfg = Release|Win32 42 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Release|Win32.ActiveCfg = Release|Win32 43 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Release|Win32.Build.0 = Release|Win32 44 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127}.Release|x64.ActiveCfg = Release|Win32 45 | EndGlobalSection 46 | GlobalSection(SolutionProperties) = preSolution 47 | HideSolutionNode = FALSE 48 | EndGlobalSection 49 | EndGlobal 50 | -------------------------------------------------------------------------------- /Lua/Source/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.89 2014/11/02 19:33:33 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | l_noret luaM_toobig (lua_State *L) { 69 | luaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | global_State *g = G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | lua_assert((realosize == 0) == (block == NULL)); 82 | #if defined(HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | api_check( nsize > realosize, 89 | "realloc cannot fail when shrinking a block"); 90 | luaC_fullgc(L, 1); /* try to free some memory... */ 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tracer 2 | A "simple" C++ RayTracer made to learn more about graphics, with a simple Lua interface by Github user stephanwilliams. 3 | 4 | The goals are to produce high quality images fast using interesting techniques. 5 | Started out as a lightning fast Raytracer (phong/diffuse/shadow, refraction, aa samples etc), but now has pathtracer renderer alongside it. 6 | It's pretty cool, and hopefully it'll be cooler soon. 7 | 8 | 9 | The Lua interface currently only supports spheres - just comment out the code that branches to Lua if you want to render triangles with this! 10 | 11 | Images: 12 | ![ScreenShot](http://i.imgur.com/QGuxkGy.png) 13 | Path Tracer, no fast convergence techniques, pre-optimization - 300 samples, 1000x1000 ~ 4 Hours 14 | 15 | ![ScreenShot](http://i.imgur.com/DhQFTTH.png) 16 | Path Tracer, MultiCore Direct Illumination and Diffuse Inter-reflectance - 100 samples, 512x512 ~ 47 Seconds (46608 MS) 17 | 18 | ![ScreenShot](http://i.imgur.com/92z2vF9.png) 19 | Path Tracer, Pre-optimizations, Direct Illumination and Diffuse Inter-reflectance - 100 samples, 512x512 ~ 8 Hours (29935139 MS) 20 | 21 | ![ScreenShot](http://i.imgur.com/9HevjW9.png) 22 | Ray Tracer pre-optimizations - 1000x1000 ~ Somewhere less than 20 Minutes 23 | 24 | ![ScreenShot](http://i.imgur.com/OcpYo2K.png) 25 | Ray Tracer, pre-KDTree and optimizations, faking GI using low sample Glossy Reflections - 1000x1000 ~ 5 Hours 26 | 27 | 28 | Features: 29 | ---------- 30 | * RayTracer 31 | * Triangle Mesh and Primitive Shape Support 32 | * Direct Illumination 33 | * Specular, Diffuse, and Glossy Reflections supported 34 | * Per-Point Monte Carlo Light Sampling 35 | * Per-Point Monte Carlo Shadow Sampling 36 | * Per-Pixel Monte Carlo AA Sampling 37 | * Per-Point Monte Carlo Glossy Reflection Sampling 38 | * Refraction 39 | * Translucency 40 | * High Speed 41 | * Multithreading Support 42 | * KDTrees for Triangle Meshes 43 | * Early outs help avoid unnecessary recursion 44 | * Some CPU optimizations 45 | * Fast Bounding Box Intersections 46 | 47 | 48 | * PathTracer 49 | * Pretty sample-convergence based images 50 | * Russian Roulette Ray elimination 51 | * Per-Pixel Monte Carlo AA Sampling 52 | * Triangle Mesh and Primitive Shape Support 53 | * High Speed 54 | * Multithreading Support 55 | * Options For Fast Convergence for Few Samples 56 | * Direct Illumination 57 | * Diffuse, Specular, and Shadow Tests 58 | * Diffuse Inter-reflectance to approximate Global Illumination 59 | * Importance Distribution Reflections Across Surfaces 60 | * KDTrees for Triangle Meshes 61 | * Some CPU optimizations 62 | * Early outs help avoid unnecessary recursion 63 | * Fast Bounding Box Intersections 64 | 65 | Quality object passed to renderer object of your choice (path, ray) that describes how many samples of each type to take, which light approx techniques (Direct Illumination + Diffuse Inter-reflectance vs doing a bajillion bouncing rays) to use to speed up, etc. 66 | 67 | 68 | My Computer 69 | ----------- 70 | * MSI GT70 Laptop 71 | * Intel Core™ i7-3610QM 72 | * NVIDIA GeForce GTX 670M | 3GB GDDR5 73 | * Windows 8.1 74 | * 12 GB DDR3 RAM 75 | 76 | 77 | Imgur album of renders showing its progress: http://imgur.com/a/e5kgZ 78 | ----------------------------------------------------------------------- 79 | -------------------------------------------------------------------------------- /Lua/Source/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 41 | 42 | 43 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 44 | 45 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 46 | 47 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 48 | 49 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 50 | 51 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 52 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 53 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 54 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 55 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 56 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 57 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 58 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 59 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); 60 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 63 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 65 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 67 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 68 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 69 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 70 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 71 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 72 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 73 | LUAI_FUNC int luaK_jump (FuncState *fs); 74 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 75 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 76 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 77 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 78 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 79 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 80 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 81 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 82 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 83 | expdesc *v2, int line); 84 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 85 | 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /RayTracer/Source/Matrix4x4.cpp: -------------------------------------------------------------------------------- 1 | #include "Matrix4x4.h" 2 | #include 3 | #include 4 | #include 5 | 6 | Matrix4x4::Matrix4x4() 7 | { 8 | m[0][0] = 1.f; 9 | m[0][1] = 0.f; 10 | m[0][2] = 0.f; 11 | m[0][3] = 0.f; 12 | 13 | m[1][0] = 0.f; 14 | m[1][1] = 1.f; 15 | m[1][2] = 0.f; 16 | m[1][3] = 0.f; 17 | 18 | m[2][0] = 0.f; 19 | m[2][1] = 0.f; 20 | m[2][2] = 1.f; 21 | m[2][3] = 0.f; 22 | 23 | m[3][0] = 0.f; 24 | m[3][1] = 0.f; 25 | m[3][2] = 0.f; 26 | m[3][3] = 1.f; 27 | } 28 | 29 | Matrix4x4::Matrix4x4(const float mat[4][4]) 30 | { 31 | for (int i = 0; i < 4; ++i) 32 | for (int j = 0; j < 4; ++j) 33 | m[i][j] = mat[i][j]; 34 | } 35 | 36 | Matrix4x4::Matrix4x4(float t00, float t01, float t02, float t03, 37 | float t10, float t11, float t12, float t13, 38 | float t20, float t21, float t22, float t23, 39 | float t30, float t31, float t32, float t33) 40 | { 41 | m[0][0] = t00; 42 | m[0][1] = t01; 43 | m[0][2] = t02; 44 | m[0][3] = t03; 45 | 46 | m[1][0] = t10; 47 | m[1][1] = t11; 48 | m[1][2] = t12; 49 | m[1][3] = t13; 50 | 51 | m[2][0] = t20; 52 | m[2][1] = t21; 53 | m[2][2] = t22; 54 | m[2][3] = t23; 55 | 56 | m[3][0] = t30; 57 | m[3][1] = t31; 58 | m[3][2] = t32; 59 | m[3][3] = t33; 60 | } 61 | 62 | Matrix4x4 Transpose(const Matrix4x4 &mat) 63 | { 64 | return Matrix4x4(mat.m[0][0], mat.m[1][0], mat.m[2][0], mat.m[3][0], 65 | mat.m[0][1], mat.m[1][1], mat.m[2][1], mat.m[3][1], 66 | mat.m[0][2], mat.m[1][2], mat.m[2][2], mat.m[3][2], 67 | mat.m[0][3], mat.m[1][3], mat.m[2][3], mat.m[3][3]); 68 | } 69 | 70 | bool Matrix4x4::operator==(const Matrix4x4 &mat) 71 | { 72 | for (int i = 0; i < 4; ++i) 73 | for (int j = 0; j < 4; ++j) 74 | if (mat.m[i][j] != m[i][j]) 75 | return false; 76 | return true; 77 | } 78 | 79 | Matrix4x4 Matrix4x4::Mul(const Matrix4x4 &m1, const Matrix4x4 &m2) 80 | { 81 | Matrix4x4 r; 82 | for (int i = 0; i < 4; ++i) 83 | for (int j = 0; j < 4;++j) 84 | r.m[i][j] = m1.m[i][0] * m2.m[0][j] + 85 | m1.m[i][1] * m2.m[1][j] + 86 | m1.m[i][2] * m2.m[2][j] + 87 | m1.m[i][3] * m2.m[3][j]; 88 | return r; 89 | } 90 | 91 | Matrix4x4 Inverse(const Matrix4x4 &mat) 92 | { 93 | //SPECIAL THANKS TO 94 | //PHYSICALLY BASED RENDERING: FROM THEORY TO IMPLEMENTATION 95 | //BY MATT PHARR AND GREG HUMPHREYS 96 | //FOR THEIR GREAT IMPLEMENTATION OF 97 | //OF A NUMERICALLY STABLE GUASSIAN-JORDAN ROUTINE!!! 98 | int indxc[4], indxr[4]; 99 | int ipiv[4] = { 0, 0, 0, 0 }; 100 | float minv[4][4]; 101 | memcpy(minv, mat.m, 4 * 4 * sizeof(float)); 102 | for (int i = 0; i < 4; ++i) { 103 | int irow = -1, icol = -1; 104 | float big = 0.; 105 | for (int j = 0; j < 4; j++) { 106 | if (ipiv[j] != 1) { 107 | for (int k = 0; k < 4; ++k) { 108 | if (ipiv[k] == 0) { 109 | if (fabsf(minv[j][k]) >= big) { 110 | big = float(fabsf(minv[j][k])); 111 | irow = j; 112 | icol = k; 113 | } 114 | } 115 | assert(ipiv[k] <= 1); 116 | } 117 | } 118 | } 119 | ++ipiv[icol]; 120 | if (irow != icol) { 121 | for (int k = 0; k < 4; ++k) 122 | std::swap(minv[irow][k], minv[icol][k]); 123 | } 124 | indxr[i] = irow; 125 | indxc[i] = icol; 126 | assert(minv[icol][icol] != 0.f); 127 | float pivinv = 1.f / minv[icol][icol]; 128 | minv[icol][icol] = 1.f; 129 | for (int j = 0; j < 4; j++) 130 | minv[icol][j] *= pivinv; 131 | 132 | for (int j = 0; j < 4; ++j) { 133 | if (j != icol) { 134 | float save = minv[j][icol]; 135 | minv[j][icol] = 0; 136 | for (int k = 0; k < 4; k++) 137 | minv[j][k] -= minv[icol][k] * save; 138 | } 139 | } 140 | } 141 | for (int j = 3; j >= 0; --j) { 142 | if (indxr[j] != indxc[j]) { 143 | for (int k = 0; k < 4; k++) 144 | std::swap(minv[k][indxr[j]], minv[k][indxc[j]]); 145 | } 146 | } 147 | return Matrix4x4(minv); 148 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | #NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /Lua/Source/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.74 2014/10/25 11:50:46 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in 'k' */ 25 | VKFLT, /* nval = numerical float value */ 26 | VKINT, /* nval = numerical integer value */ 27 | VNONRELOC, /* info = result register */ 28 | VLOCAL, /* info = local register */ 29 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 30 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 31 | VJMP, /* info = instruction pc */ 32 | VRELOCABLE, /* info = instruction pc */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | 38 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 39 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 40 | 41 | typedef struct expdesc { 42 | expkind k; 43 | union { 44 | struct { /* for indexed variables (VINDEXED) */ 45 | short idx; /* index (R/K) */ 46 | lu_byte t; /* table (register or upvalue) */ 47 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 48 | } ind; 49 | int info; /* for generic use */ 50 | lua_Number nval; /* for VKFLT */ 51 | lua_Integer ival; /* for VKINT */ 52 | } u; 53 | int t; /* patch list of 'exit when true' */ 54 | int f; /* patch list of 'exit when false' */ 55 | } expdesc; 56 | 57 | 58 | /* description of active local variable */ 59 | typedef struct Vardesc { 60 | short idx; /* variable index in stack */ 61 | } Vardesc; 62 | 63 | 64 | /* description of pending goto statements and label statements */ 65 | typedef struct Labeldesc { 66 | TString *name; /* label identifier */ 67 | int pc; /* position in code */ 68 | int line; /* line where it appeared */ 69 | lu_byte nactvar; /* local level where it appears in current block */ 70 | } Labeldesc; 71 | 72 | 73 | /* list of labels or gotos */ 74 | typedef struct Labellist { 75 | Labeldesc *arr; /* array */ 76 | int n; /* number of entries in use */ 77 | int size; /* array size */ 78 | } Labellist; 79 | 80 | 81 | /* dynamic structures used by the parser */ 82 | typedef struct Dyndata { 83 | struct { /* list of active local variables */ 84 | Vardesc *arr; 85 | int n; 86 | int size; 87 | } actvar; 88 | Labellist gt; /* list of pending gotos */ 89 | Labellist label; /* list of active labels */ 90 | } Dyndata; 91 | 92 | 93 | /* control of blocks */ 94 | struct BlockCnt; /* defined in lparser.c */ 95 | 96 | 97 | /* state needed to generate code for a given function */ 98 | typedef struct FuncState { 99 | Proto *f; /* current function header */ 100 | struct FuncState *prev; /* enclosing function */ 101 | struct LexState *ls; /* lexical state */ 102 | struct BlockCnt *bl; /* chain of current blocks */ 103 | int pc; /* next position to code (equivalent to 'ncode') */ 104 | int lasttarget; /* 'label' of last 'jump label' */ 105 | int jpc; /* list of pending jumps to 'pc' */ 106 | int nk; /* number of elements in 'k' */ 107 | int np; /* number of elements in 'p' */ 108 | int firstlocal; /* index of first local var (in Dyndata array) */ 109 | short nlocvars; /* number of elements in 'f->locvars' */ 110 | lu_byte nactvar; /* number of active local variables */ 111 | lu_byte nups; /* number of upvalues */ 112 | lu_byte freereg; /* first free register */ 113 | } FuncState; 114 | 115 | 116 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 117 | Dyndata *dyd, const char *name, int firstchar); 118 | 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /LuaTest/LuaTest.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | {50b3b885-208d-4847-80ff-0476845f710c} 19 | 20 | 21 | 22 | {2A94B8D9-37F0-451B-B450-9D94F8AD0127} 23 | LuaTest 24 | 25 | 26 | 27 | Application 28 | true 29 | v120 30 | MultiByte 31 | 32 | 33 | Application 34 | false 35 | v120 36 | true 37 | MultiByte 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Level3 53 | Disabled 54 | true 55 | 56 | 57 | true 58 | 59 | 60 | 61 | 62 | Level3 63 | MaxSpeed 64 | true 65 | true 66 | true 67 | ..\Lua\Source;%(AdditionalIncludeDirectories) 68 | 69 | 70 | true 71 | true 72 | true 73 | $(OutDir)/Lua.lib;%(AdditionalDependencies) 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Lua/Source/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 21 | "MOVE", 22 | "LOADK", 23 | "LOADKX", 24 | "LOADBOOL", 25 | "LOADNIL", 26 | "GETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "SETTABUP", 30 | "SETUPVAL", 31 | "SETTABLE", 32 | "NEWTABLE", 33 | "SELF", 34 | "ADD", 35 | "SUB", 36 | "MUL", 37 | "MOD", 38 | "POW", 39 | "DIV", 40 | "IDIV", 41 | "BAND", 42 | "BOR", 43 | "BXOR", 44 | "SHL", 45 | "SHR", 46 | "UNM", 47 | "BNOT", 48 | "NOT", 49 | "LEN", 50 | "CONCAT", 51 | "JMP", 52 | "EQ", 53 | "LT", 54 | "LE", 55 | "TEST", 56 | "TESTSET", 57 | "CALL", 58 | "TAILCALL", 59 | "RETURN", 60 | "FORLOOP", 61 | "FORPREP", 62 | "TFORCALL", 63 | "TFORLOOP", 64 | "SETLIST", 65 | "CLOSURE", 66 | "VARARG", 67 | "EXTRAARG", 68 | NULL 69 | }; 70 | 71 | 72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 73 | 74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 75 | /* T A B C mode opcode */ 76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 123 | }; 124 | 125 | -------------------------------------------------------------------------------- /Lua/Source/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | 23 | 24 | 25 | CClosure *luaF_newCclosure (lua_State *L, int n) { 26 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); 27 | CClosure *c = gco2ccl(o); 28 | c->nupvalues = cast_byte(n); 29 | return c; 30 | } 31 | 32 | 33 | LClosure *luaF_newLclosure (lua_State *L, int n) { 34 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); 35 | LClosure *c = gco2lcl(o); 36 | c->p = NULL; 37 | c->nupvalues = cast_byte(n); 38 | while (n--) c->upvals[n] = NULL; 39 | return c; 40 | } 41 | 42 | /* 43 | ** fill a closure with new closed upvalues 44 | */ 45 | void luaF_initupvals (lua_State *L, LClosure *cl) { 46 | int i; 47 | for (i = 0; i < cl->nupvalues; i++) { 48 | UpVal *uv = luaM_new(L, UpVal); 49 | uv->refcount = 1; 50 | uv->v = &uv->u.value; /* make it closed */ 51 | setnilvalue(uv->v); 52 | cl->upvals[i] = uv; 53 | } 54 | } 55 | 56 | 57 | UpVal *luaF_findupval (lua_State *L, StkId level) { 58 | UpVal **pp = &L->openupval; 59 | UpVal *p; 60 | UpVal *uv; 61 | lua_assert(isintwups(L) || L->openupval == NULL); 62 | while (*pp != NULL && (p = *pp)->v >= level) { 63 | lua_assert(upisopen(p)); 64 | if (p->v == level) /* found a corresponding upvalue? */ 65 | return p; /* return it */ 66 | pp = &p->u.open.next; 67 | } 68 | /* not found: create a new upvalue */ 69 | uv = luaM_new(L, UpVal); 70 | uv->refcount = 0; 71 | uv->u.open.next = *pp; /* link it to list of open upvalues */ 72 | uv->u.open.touched = 1; 73 | *pp = uv; 74 | uv->v = level; /* current value lives in the stack */ 75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ 76 | L->twups = G(L)->twups; /* link it to the list */ 77 | G(L)->twups = L; 78 | } 79 | return uv; 80 | } 81 | 82 | 83 | void luaF_close (lua_State *L, StkId level) { 84 | UpVal *uv; 85 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) { 86 | lua_assert(upisopen(uv)); 87 | L->openupval = uv->u.open.next; /* remove from 'open' list */ 88 | if (uv->refcount == 0) /* no references? */ 89 | luaM_free(L, uv); /* free upvalue */ 90 | else { 91 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 92 | uv->v = &uv->u.value; /* now current value lives here */ 93 | luaC_upvalbarrier(L, uv); 94 | } 95 | } 96 | } 97 | 98 | 99 | Proto *luaF_newproto (lua_State *L) { 100 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); 101 | Proto *f = gco2p(o); 102 | f->k = NULL; 103 | f->sizek = 0; 104 | f->p = NULL; 105 | f->sizep = 0; 106 | f->code = NULL; 107 | f->cache = NULL; 108 | f->sizecode = 0; 109 | f->lineinfo = NULL; 110 | f->sizelineinfo = 0; 111 | f->upvalues = NULL; 112 | f->sizeupvalues = 0; 113 | f->numparams = 0; 114 | f->is_vararg = 0; 115 | f->maxstacksize = 0; 116 | f->locvars = NULL; 117 | f->sizelocvars = 0; 118 | f->linedefined = 0; 119 | f->lastlinedefined = 0; 120 | f->source = NULL; 121 | return f; 122 | } 123 | 124 | 125 | void luaF_freeproto (lua_State *L, Proto *f) { 126 | luaM_freearray(L, f->code, f->sizecode); 127 | luaM_freearray(L, f->p, f->sizep); 128 | luaM_freearray(L, f->k, f->sizek); 129 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 130 | luaM_freearray(L, f->locvars, f->sizelocvars); 131 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 132 | luaM_free(L, f); 133 | } 134 | 135 | 136 | /* 137 | ** Look for n-th local variable at line 'line' in function 'func'. 138 | ** Returns NULL if not found. 139 | */ 140 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 141 | int i; 142 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 143 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 144 | local_number--; 145 | if (local_number == 0) 146 | return getstr(f->locvars[i].varname); 147 | } 148 | } 149 | return NULL; /* not found */ 150 | } 151 | 152 | -------------------------------------------------------------------------------- /RayTracer/Source/Transform.h: -------------------------------------------------------------------------------- 1 | #ifndef TRANSFORM_H 2 | #define TRANSFORM_H 3 | 4 | #include "Matrix4x4.h" 5 | #include "Vector.h" 6 | #include "Point.h" 7 | #include "Ray.h" 8 | #include "BoundingBox.h" 9 | 10 | class Transform 11 | { 12 | public: 13 | Transform(); 14 | Transform(const float mat[4][4]); 15 | Transform(const Matrix4x4 &mat); 16 | Transform(const Matrix4x4 &mat, const Matrix4x4 &inv); 17 | ~Transform() {}; 18 | 19 | friend Transform Inverse(const Transform &t); 20 | bool IsIdentity(); 21 | bool HasScale(); 22 | 23 | bool operator==(const Transform &t); 24 | bool operator!=(const Transform &t); 25 | 26 | Point operator()(const Point &p) const; 27 | void operator()(const Point &p, Point *pTrans) const; 28 | 29 | Vector operator()(const Vector &v) const; 30 | void operator()(const Vector &v, Vector *vTrans) const; 31 | 32 | Normal operator()(const Normal &n) const; 33 | void operator()(const Normal &n, Normal *nTrans) const; 34 | 35 | Ray operator()(const Ray &r) const; 36 | void operator()(const Ray &r, Ray *rTrans) const; 37 | 38 | BoundingBox operator()(const BoundingBox &bbox) const; 39 | void operator()(const BoundingBox &bbox, BoundingBox *bboxTrans) const; 40 | 41 | Transform operator()(const Transform &trans) const; 42 | void operator()(const Transform &trans, Transform *transTrans) const; 43 | 44 | private: 45 | Matrix4x4 m; 46 | Matrix4x4 mInv; 47 | }; 48 | 49 | inline Transform Translate(const Vector &v) 50 | { 51 | Matrix4x4 mat(1, 0, 0, v.x, 52 | 0, 1, 0, v.y, 53 | 0, 0, 1, v.z, 54 | 0, 0, 0, 1); 55 | 56 | Matrix4x4 inv(1, 0, 0, -v.x, 57 | 0, 1, 0, -v.y, 58 | 0, 0, 1, -v.z, 59 | 0, 0, 0, 1); 60 | 61 | return Transform(mat, inv); 62 | } 63 | 64 | inline Transform Scale(float x, float y, float z) 65 | { 66 | Matrix4x4 mat(x, 0, 0, 0, 67 | 0, y, 0, 0, 68 | 0, 0, z, 0, 69 | 0, 0, 0, 1); 70 | 71 | Matrix4x4 inv(1.f / x, 0, 0, 0, 72 | 0, 1.f / y, 0, 0, 73 | 0, 0, 1.f / z, 0, 74 | 0, 0, 0, 1); 75 | 76 | return Transform(mat, inv); 77 | } 78 | 79 | inline Transform RotateX(float angle) 80 | { 81 | float sint = sinf((0.017453f*angle)); 82 | float cost = cosf((0.017453f*angle)); 83 | Matrix4x4 mat(1, 0, 0, 0, 84 | 0, cost, -sint, 0, 85 | 0, sint, cost, 0, 86 | 0, 0, 0, 1); 87 | 88 | 89 | return Transform(mat, Transpose(mat)); 90 | } 91 | 92 | inline Transform RotateY(float angle) 93 | { 94 | float sint = sinf((0.017453f*angle)); 95 | float cost = cosf((0.017453f*angle)); 96 | Matrix4x4 mat(cost, 0, sint, 0, 97 | 0, 1, 0, 0, 98 | -sint, 0, cost, 0, 99 | 0, 0, 0, 1); 100 | 101 | return Transform(mat, Transpose(mat)); 102 | } 103 | 104 | inline Transform RotateZ(float angle) 105 | { 106 | float sint = sinf((0.017453f*angle)); 107 | float cost = cosf((0.017453f*angle)); 108 | Matrix4x4 mat(cost, -sint, 0, 0, 109 | sint, cost, 0, 0, 110 | 0, 0, 1, 0, 111 | 0, 0, 0, 1); 112 | 113 | return Transform(mat, Transpose(mat)); 114 | } 115 | 116 | inline Transform Rotate(float angle, const Vector &axis) 117 | { 118 | Vector a = Normalize(axis); 119 | float s = sinf(0.017453f*angle); 120 | float c = cosf(0.017453f*angle); 121 | float m[4][4]; 122 | 123 | m[0][0] = a.x*a.x + (1.f - a.x*a.x)*c; 124 | m[0][1] = a.x*a.y*(1.f - c) - a.z*s; 125 | m[0][2] = a.x*a.z*(1.f - c) + a.y*s; 126 | m[0][3] = 0; 127 | 128 | m[1][0] = a.x*a.y*(1.f - c) + a.z*s; 129 | m[1][1] = a.y*a.y + (1.f - a.y*a.y)*c; 130 | m[1][2] = a.y*a.z*(1.f - c) - a.x*s; 131 | m[1][3] = 0; 132 | 133 | m[2][0] = a.x*a.z*(1.f - c) - a.y*s; 134 | m[2][1] = a.y*a.z*(1.f - c) + a.x*s; 135 | m[2][2] = a.z*a.z + (1.f - a.z*a.z)*c; 136 | m[2][3] = 0; 137 | 138 | m[3][0] = 0; 139 | m[3][1] = 0; 140 | m[3][2] = 0; 141 | m[3][3] = 1; 142 | 143 | Matrix4x4 mat(m); 144 | return Transform(mat, Transpose(mat)); 145 | } 146 | 147 | inline Transform LookAt(const Point &pos, const Point &look, const Vector &up) 148 | { 149 | float m[4][4]; 150 | Vector dir = Normalize(look - pos); 151 | Vector left = Normalize(Cross(Normalize(up), dir)); 152 | Vector newUp = Cross(dir, left); 153 | m[0][0] = left.x; 154 | m[1][0] = left.y; 155 | m[2][0] = left.z; 156 | m[3][0] = 0.f; 157 | m[0][1] = newUp.x; 158 | m[1][1] = newUp.y; 159 | m[2][1] = newUp.z; 160 | m[3][1] = 0.f; 161 | m[0][2] = dir.x; 162 | m[1][2] = dir.y; 163 | m[2][2] = dir.z; 164 | m[3][2] = 0.f; 165 | 166 | m[0][3] = pos.x; 167 | m[1][3] = pos.y; 168 | m[2][3] = pos.z; 169 | m[3][3] = 1; 170 | } 171 | 172 | 173 | 174 | #endif 175 | -------------------------------------------------------------------------------- /Lua/Source/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.33 2014/11/21 12:15:57 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | #include "lvm.h" 25 | 26 | 27 | static const char udatatypename[] = "userdata"; 28 | 29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 30 | "no value", 31 | "nil", "boolean", udatatypename, "number", 32 | "string", "table", "function", udatatypename, "thread", 33 | "proto" /* this last case is used for tests only */ 34 | }; 35 | 36 | 37 | void luaT_init (lua_State *L) { 38 | static const char *const luaT_eventname[] = { /* ORDER TM */ 39 | "__index", "__newindex", 40 | "__gc", "__mode", "__len", "__eq", 41 | "__add", "__sub", "__mul", "__mod", "__pow", 42 | "__div", "__idiv", 43 | "__band", "__bor", "__bxor", "__shl", "__shr", 44 | "__unm", "__bnot", "__lt", "__le", 45 | "__concat", "__call" 46 | }; 47 | int i; 48 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 50 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 51 | } 52 | } 53 | 54 | 55 | /* 56 | ** function to be used with macro "fasttm": optimized for absence of 57 | ** tag methods 58 | */ 59 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 60 | const TValue *tm = luaH_getstr(events, ename); 61 | lua_assert(event <= TM_EQ); 62 | if (ttisnil(tm)) { /* no tag method? */ 63 | events->flags |= cast_byte(1u<metatable; 75 | break; 76 | case LUA_TUSERDATA: 77 | mt = uvalue(o)->metatable; 78 | break; 79 | default: 80 | mt = G(L)->mt[ttnov(o)]; 81 | } 82 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 83 | } 84 | 85 | 86 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 87 | const TValue *p2, TValue *p3, int hasres) { 88 | ptrdiff_t result = savestack(L, p3); 89 | setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */ 90 | setobj2s(L, L->top++, p1); /* 1st argument */ 91 | setobj2s(L, L->top++, p2); /* 2nd argument */ 92 | if (!hasres) /* no result? 'p3' is third argument */ 93 | setobj2s(L, L->top++, p3); /* 3rd argument */ 94 | /* metamethod may yield only when called from Lua code */ 95 | luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); 96 | if (hasres) { /* if has result, move it to its place */ 97 | p3 = restorestack(L, result); 98 | setobjs2s(L, p3, --L->top); 99 | } 100 | } 101 | 102 | 103 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 104 | StkId res, TMS event) { 105 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 106 | if (ttisnil(tm)) 107 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 108 | if (ttisnil(tm)) return 0; 109 | luaT_callTM(L, tm, p1, p2, res, 1); 110 | return 1; 111 | } 112 | 113 | 114 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 115 | StkId res, TMS event) { 116 | if (!luaT_callbinTM(L, p1, p2, res, event)) { 117 | switch (event) { 118 | case TM_CONCAT: 119 | luaG_concaterror(L, p1, p2); 120 | case TM_BAND: case TM_BOR: case TM_BXOR: 121 | case TM_SHL: case TM_SHR: case TM_BNOT: { 122 | lua_Number dummy; 123 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 124 | luaG_tointerror(L, p1, p2); 125 | else 126 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 127 | /* else go through */ 128 | } 129 | default: 130 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 131 | } 132 | } 133 | } 134 | 135 | 136 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 137 | TMS event) { 138 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) 139 | return -1; /* no metamethod */ 140 | else 141 | return !l_isfalse(L->top); 142 | } 143 | 144 | -------------------------------------------------------------------------------- /Lua/Source/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco (lua_State *L) { 22 | lua_State *co = lua_tothread(L, 1); 23 | luaL_argcheck(L, co, 1, "thread expected"); 24 | return co; 25 | } 26 | 27 | 28 | static int auxresume (lua_State *L, lua_State *co, int narg) { 29 | int status; 30 | if (!lua_checkstack(co, narg)) { 31 | lua_pushliteral(L, "too many arguments to resume"); 32 | return -1; /* error flag */ 33 | } 34 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 35 | lua_pushliteral(L, "cannot resume dead coroutine"); 36 | return -1; /* error flag */ 37 | } 38 | lua_xmove(L, co, narg); 39 | status = lua_resume(co, L, narg); 40 | if (status == LUA_OK || status == LUA_YIELD) { 41 | int nres = lua_gettop(co); 42 | if (!lua_checkstack(L, nres + 1)) { 43 | lua_pop(co, nres); /* remove results anyway */ 44 | lua_pushliteral(L, "too many results to resume"); 45 | return -1; /* error flag */ 46 | } 47 | lua_xmove(co, L, nres); /* move yielded values */ 48 | return nres; 49 | } 50 | else { 51 | lua_xmove(co, L, 1); /* move error message */ 52 | return -1; /* error flag */ 53 | } 54 | } 55 | 56 | 57 | static int luaB_coresume (lua_State *L) { 58 | lua_State *co = getco(L); 59 | int r; 60 | r = auxresume(L, co, lua_gettop(L) - 1); 61 | if (r < 0) { 62 | lua_pushboolean(L, 0); 63 | lua_insert(L, -2); 64 | return 2; /* return false + error message */ 65 | } 66 | else { 67 | lua_pushboolean(L, 1); 68 | lua_insert(L, -(r + 1)); 69 | return r + 1; /* return true + 'resume' returns */ 70 | } 71 | } 72 | 73 | 74 | static int luaB_auxwrap (lua_State *L) { 75 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 76 | int r = auxresume(L, co, lua_gettop(L)); 77 | if (r < 0) { 78 | if (lua_isstring(L, -1)) { /* error object is a string? */ 79 | luaL_where(L, 1); /* add extra info */ 80 | lua_insert(L, -2); 81 | lua_concat(L, 2); 82 | } 83 | return lua_error(L); /* propagate error */ 84 | } 85 | return r; 86 | } 87 | 88 | 89 | static int luaB_cocreate (lua_State *L) { 90 | lua_State *NL; 91 | luaL_checktype(L, 1, LUA_TFUNCTION); 92 | NL = lua_newthread(L); 93 | lua_pushvalue(L, 1); /* move function to top */ 94 | lua_xmove(L, NL, 1); /* move function from L to NL */ 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_cowrap (lua_State *L) { 100 | luaB_cocreate(L); 101 | lua_pushcclosure(L, luaB_auxwrap, 1); 102 | return 1; 103 | } 104 | 105 | 106 | static int luaB_yield (lua_State *L) { 107 | return lua_yield(L, lua_gettop(L)); 108 | } 109 | 110 | 111 | static int luaB_costatus (lua_State *L) { 112 | lua_State *co = getco(L); 113 | if (L == co) lua_pushliteral(L, "running"); 114 | else { 115 | switch (lua_status(co)) { 116 | case LUA_YIELD: 117 | lua_pushliteral(L, "suspended"); 118 | break; 119 | case LUA_OK: { 120 | lua_Debug ar; 121 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 122 | lua_pushliteral(L, "normal"); /* it is running */ 123 | else if (lua_gettop(co) == 0) 124 | lua_pushliteral(L, "dead"); 125 | else 126 | lua_pushliteral(L, "suspended"); /* initial state */ 127 | break; 128 | } 129 | default: /* some error occurred */ 130 | lua_pushliteral(L, "dead"); 131 | break; 132 | } 133 | } 134 | return 1; 135 | } 136 | 137 | 138 | static int luaB_yieldable (lua_State *L) { 139 | lua_pushboolean(L, lua_isyieldable(L)); 140 | return 1; 141 | } 142 | 143 | 144 | static int luaB_corunning (lua_State *L) { 145 | int ismain = lua_pushthread(L); 146 | lua_pushboolean(L, ismain); 147 | return 2; 148 | } 149 | 150 | 151 | static const luaL_Reg co_funcs[] = { 152 | {"create", luaB_cocreate}, 153 | {"resume", luaB_coresume}, 154 | {"running", luaB_corunning}, 155 | {"status", luaB_costatus}, 156 | {"wrap", luaB_cowrap}, 157 | {"yield", luaB_yield}, 158 | {"isyieldable", luaB_yieldable}, 159 | {NULL, NULL} 160 | }; 161 | 162 | 163 | 164 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 165 | luaL_newlib(L, co_funcs); 166 | return 1; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /RayTracer/Source/ObjLoader.cpp: -------------------------------------------------------------------------------- 1 | #include "ObjLoader.h" 2 | #include 3 | #include 4 | #include 5 | TriangleMesh ObjLoader::Construct(char* file, const Transform * o2w, const Transform * w2o, Material material) 6 | { 7 | std::fstream fs; 8 | 9 | std::vector I; 10 | std::vector P; 11 | std::vector N; 12 | std::vector UV; 13 | bool result = LoadMesh(file, &P, &I, &N, &UV); 14 | assert(result); 15 | //convert to int/float/point/normal pointers! 16 | int * indices = new int[I.size()]; 17 | for (int i=0;i *points, 46 | std::vector * indices, 47 | std::vector * normals, 48 | std::vector * uv) 49 | { 50 | char header[64]; 51 | std::vector* tempNormals = new std::vector(); 52 | std::ifstream modelFile; 53 | modelFile.open(file, std::ios::in); 54 | while(modelFile.good()) 55 | { 56 | float dummy = 0.f; 57 | char data[64]; 58 | modelFile >> header; 59 | 60 | if (strcmp(header, "v")==0) 61 | { 62 | //printf("Adding Points:"); 63 | for (int i=0;i<3;++i) 64 | { 65 | modelFile >> data; 66 | points->push_back(atof(data)); 67 | //printf(" %f", dummy); 68 | } 69 | //printf("\n"); 70 | 71 | } 72 | else if (strcmp(header, "vt")==0) 73 | { 74 | //printf("Adding UVs:"); 75 | for (int i=0;i<2;++i) 76 | { 77 | modelFile >> data; 78 | uv->push_back(atof(data)); 79 | //printf(" %f", dummy); 80 | } 81 | //printf("\n"); 82 | } 83 | else if (strcmp(header, "vn")==0) 84 | { 85 | //printf("Adding Normals:"); 86 | for (int i=0;i<3;++i) 87 | { 88 | modelFile >> data; 89 | tempNormals->push_back(atof(data)); 90 | //printf(" %f", dummy); 91 | } 92 | //printf("\n"); 93 | } 94 | else if (strcmp(header, "f")==0) 95 | { 96 | //printf("Adding Indices:"); 97 | int n=-1; 98 | for (int i=0;i<3;++i) 99 | { 100 | char read[64]; 101 | char tri[64]; 102 | char norm[64]; 103 | for (int j=0;j<64;++j) 104 | { 105 | read[j] = 0; 106 | norm[j] = 0; 107 | tri[j] = 0; 108 | } 109 | modelFile >> read; 110 | //printf("%s\n", read); 111 | int j=0; 112 | for (j=0;j<64;++j) 113 | { 114 | if (read[j] == '/' && read[j+1] == '/') 115 | { 116 | j+=2; 117 | break; 118 | } 119 | tri[j] = read[j]; 120 | } 121 | int k=0; 122 | for (j;j<64;++j) 123 | { 124 | if (read[j] == ' ' || read[j] == '\n') 125 | break; 126 | norm[k] = read[j]; 127 | ++k; 128 | } 129 | int bummy = atoi(tri); 130 | n = atoi(norm); 131 | //printf("%s %i\n", norm, n); 132 | assert(n!=0); 133 | //printf(" %i", bummy); 134 | assert(bummy-1>-1); 135 | assert(bummysize()); 136 | indices->push_back(bummy-1); 137 | //normals->push_back((*tempNormals)[n]); 138 | //normals->push_back((*tempNormals)[n+1]); 139 | } 140 | normals->push_back((*tempNormals)[(n-1)*3]); 141 | normals->push_back((*tempNormals)[(n-1)*3+1]); 142 | normals->push_back((*tempNormals)[(n-1)*3+2]); 143 | //printf("\n"); 144 | } 145 | } 146 | modelFile.close(); 147 | 148 | if (0) 149 | { 150 | std::ofstream check; 151 | check.open("check.txt", std::ios::out); 152 | if (check.bad()) 153 | return false; 154 | 155 | for (int i=0;isize();i+=3) 156 | { 157 | check << "v " << (*points)[i] << " " << (*points)[i+1] << " " << (*points)[i+2] << std::endl; 158 | } 159 | for (int i=0;isize();i+=3) 160 | { 161 | check << "vn " << (*normals)[i] << " " << (*normals)[i+1] << " " << (*normals)[i+2] << std::endl; 162 | } 163 | for (int i=0;isize();i+=3) 164 | { 165 | check << "f " << (*indices)[i] << " " << (*indices)[i+1] << " " << (*indices)[i+2] << std::endl; 166 | } 167 | check.close(); 168 | } 169 | delete tempNormals; 170 | return true; 171 | } -------------------------------------------------------------------------------- /RayTracer/Source/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | #include "Ray.h" 4 | #include 5 | #include "Sphere.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define MAX_COLOR 0.999f 12 | #define MIN_COLOR 0.078f 13 | 14 | static const int NumThreads = 5; 15 | std::mutex MyMutex; 16 | 17 | 18 | inline float r(RNG& rng) 19 | { 20 | std::uniform_real_distribution d(-0.5f, 0.5f); 21 | return d(rng); 22 | //return (float)rand()/((float)RAND_MAX) - 0.5f; 23 | } 24 | 25 | inline float r1(RNG &rng) 26 | { 27 | std::uniform_real_distribution d(0.f, 1.f); 28 | return d(rng); 29 | //return (float)rand()/((float)RAND_MAX)); 30 | } 31 | 32 | inline long double GetMS() 33 | { 34 | timeb t; 35 | ftime(&t); 36 | return 1000.0*t.time+t.millitm; 37 | } 38 | 39 | Renderer::Renderer(std::vector* scene, const Camera &ccamera, const QualityDesc & quality) 40 | { 41 | Cam = ccamera; 42 | Scene = new std::vector(); 43 | Samples = quality.Samples; 44 | InvSamples = 1.f/(float)Samples; 45 | Quality = quality; 46 | 47 | std::vector *Tris = new std::vector(); 48 | std::vector::iterator iScene; 49 | for (iScene = scene->begin(); iScene!=scene->end();++iScene) 50 | { 51 | if ((*iScene)->Type == 2) 52 | { 53 | Tris->push_back((Triangle*)(*iScene)); 54 | } 55 | else 56 | Scene->push_back((*iScene)); 57 | } 58 | Root = KDNode::Build(Tris); 59 | } 60 | 61 | void Renderer::ThreadedTrace(int y, RGB *imgBuffer, RNG &rng) 62 | { 63 | for (int x=0;x0; 77 | Vector direction = Normalize((destination - origin) + jitter*j); 78 | Ray ray(origin, direction, 0.0f); 79 | 80 | //Begin tracing 81 | pixelColor += Trace(ray, rng); 82 | } 83 | pixelColor *= InvSamples; 84 | 85 | pixelColor.Bound(MIN_COLOR, MAX_COLOR); 86 | pixelColor *= 255.f; 87 | 88 | imgBuffer[y*Width+x] = pixelColor; 89 | } 90 | } 91 | 92 | 93 | void Renderer::Render() 94 | { 95 | srand(time(0)); 96 | Height = Cam.height; 97 | Width = Cam.width; 98 | 99 | std::thread thrd[NumThreads]; 100 | RNG* rng = new RNG[NumThreads]; 101 | 102 | RGB *imgBuffer = new RGB[Width*Height]; 103 | 104 | long double startTime = GetMS(); 105 | int lastPercent = 0; 106 | int y = 0; 107 | while (y < Height) 108 | { 109 | int numCreatedThreads = 0; 110 | for (int i=0;i=Height) 117 | break; 118 | } 119 | 120 | for (int i=0;iIntersect(ray, &triHit); 161 | //else do primitives: 162 | 163 | std::vector::iterator iScene; 164 | Hit currHit, bestHit; 165 | for (iScene = Scene->begin(); iScene!=Scene->end(); ++iScene) 166 | { 167 | if ((*iScene)->Intersect(ray, &currHit) && currHit.tHit < bestHit.tHit) 168 | { 169 | bestHit = currHit; 170 | didWeHit = true; 171 | } 172 | } 173 | *hit = triHit.tHit < bestHit.tHit ? triHit : bestHit; 174 | return didWeHit; 175 | } -------------------------------------------------------------------------------- /Lua/Source/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSswpallgc 2 42 | #define GCSswpfinobj 3 43 | #define GCSswptobefnz 4 44 | #define GCSswpend 5 45 | #define GCScallfin 6 46 | #define GCSpause 7 47 | 48 | 49 | #define issweepphase(g) \ 50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 51 | 52 | 53 | /* 54 | ** macro to tell when main invariant (white objects cannot point to black 55 | ** ones) must be kept. During a collection, the sweep 56 | ** phase may break the invariant, as objects turned white may point to 57 | ** still-black objects. The invariant is restored when sweep ends and 58 | ** all objects are white again. 59 | */ 60 | 61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** some useful bit tricks 66 | */ 67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 68 | #define setbits(x,m) ((x) |= (m)) 69 | #define testbits(x,m) ((x) & (m)) 70 | #define bitmask(b) (1<<(b)) 71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 72 | #define l_setbit(x,b) setbits(x, bitmask(b)) 73 | #define resetbit(x,b) resetbits(x, bitmask(b)) 74 | #define testbit(x,b) testbits(x, bitmask(b)) 75 | 76 | 77 | /* Layout for bit use in 'marked' field: */ 78 | #define WHITE0BIT 0 /* object is white (type 0) */ 79 | #define WHITE1BIT 1 /* object is white (type 1) */ 80 | #define BLACKBIT 2 /* object is black */ 81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */ 82 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | #define luaC_condGC(L,c) \ 105 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 106 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 107 | 108 | 109 | #define luaC_barrier(L,p,v) { \ 110 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 111 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 112 | 113 | #define luaC_barrierback(L,p,v) { \ 114 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 115 | luaC_barrierback_(L,p); } 116 | 117 | #define luaC_objbarrier(L,p,o) { \ 118 | if (isblack(p) && iswhite(o)) \ 119 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 120 | 121 | #define luaC_upvalbarrier(L,uv) \ 122 | { if (iscollectable((uv)->v) && !upisopen(uv)) \ 123 | luaC_upvalbarrier_(L,uv); } 124 | 125 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 126 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 127 | LUAI_FUNC void luaC_step (lua_State *L); 128 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 129 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 130 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 131 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 132 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 133 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 134 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 135 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 136 | 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /RayTracer/Source/TriangleMesh.cpp: -------------------------------------------------------------------------------- 1 | #include "TriangleMesh.h" 2 | 3 | TriangleMesh::TriangleMesh(const Transform * o2w, const Transform * w2o, Material material, int numTris, int numVerts, 4 | const int * vertIndices, const Point * vertPoints, 5 | const Normal * normals, const float * uv) 6 | : Shape(o2w, w2o, material) 7 | { 8 | NumTris = numTris; 9 | NumVerts = numVerts; 10 | VertIndices = new int[3 * NumTris]; 11 | printf("Loaded Mesh W/ NumTris: %i\n", NumTris); 12 | memcpy(VertIndices, vertIndices, 3 * NumTris * sizeof(int)); 13 | 14 | if (vertPoints != NULL) 15 | { 16 | VertPoints = new Point[NumVerts]; 17 | memcpy(VertPoints, vertPoints, NumVerts * sizeof(Point)); 18 | for (int i=0;i(); 44 | for (int i=0;ipush_back(Tri); 49 | } 50 | } 51 | 52 | bool TriangleMesh::CanIntersect() const 53 | { 54 | return true; //should be false; too slow 55 | } 56 | 57 | bool TriangleMesh::Intersect(const Ray & ray, Hit * hit) const 58 | { 59 | //if (!CanIntersect()) 60 | // return false; 61 | 62 | if (!WorldBounds.Intersect(ray)) // BB 63 | return false;//speed 64 | 65 | for (int currTri=0;currTriVertIndices[3*num]); 106 | Num = num; 107 | WorldBounds = WorldBound();//speed 108 | Type = 2; 109 | } 110 | 111 | bool Triangle::CanIntersect() const 112 | { 113 | return true; 114 | } 115 | 116 | BoundingBox Triangle::ObjectBound() const 117 | { 118 | BoundingBox bounds; 119 | Point p1 = Mesh->VertPoints[Vert[0]]; 120 | Point p2 = Mesh->VertPoints[Vert[1]]; 121 | Point p3 = Mesh->VertPoints[Vert[2]]; 122 | bounds = Union(Union(Union(bounds, (*WorldToObject)(p1)), 123 | (*WorldToObject)(p2)), (*WorldToObject)(p3)); 124 | return bounds; 125 | } 126 | 127 | BoundingBox Triangle::WorldBound() const 128 | { 129 | BoundingBox bounds; 130 | Point p1 = Mesh->VertPoints[Vert[0]]; 131 | Point p2 = Mesh->VertPoints[Vert[1]]; 132 | Point p3 = Mesh->VertPoints[Vert[2]]; 133 | bounds = Union(Union(Union(bounds, p1), p2), p3); 134 | return bounds; 135 | } 136 | 137 | bool Triangle::Intersect(const Ray & ray, Hit * hit) const 138 | { 139 | //if (!CanIntersect()) 140 | // return false; 141 | if (!WorldBounds.Intersect(ray)) 142 | return false; 143 | Ray r = ray; 144 | 145 | Point a = Mesh->VertPoints[Vert[0]]; 146 | Point b = Mesh->VertPoints[Vert[1]]; 147 | Point c = Mesh->VertPoints[Vert[2]]; 148 | Vector e1 = b-a; 149 | Vector e2 = c-a; 150 | Vector s1 = Cross(r.d, e2); 151 | 152 | float denominator = Dot(s1, e1); 153 | if (denominator==0.f) 154 | return false; 155 | float invDenom = 1.f/denominator; 156 | Vector s = r.o - a; 157 | float b1 = Dot(s, s1)*invDenom; 158 | if (b1 < 0 || b1 > 1.f) 159 | return false; 160 | 161 | Vector s2 = Cross(s, e1); 162 | float b2 = Dot(r.d, s2)*invDenom; 163 | if (b2 < 0.f || b1+b2 > 1.f) 164 | return false; 165 | 166 | float t = Dot(e2, s2)*invDenom; 167 | if (t < r.mint || t > r.maxt) 168 | return false; 169 | 170 | hit->tHit = t; 171 | hit->eps = 1e-3*t; 172 | hit->shapeID = ShapeID; 173 | hit->type = Type; 174 | hit->material = Mat; 175 | hit->normal = Mesh->Normals[Num]; 176 | return true; 177 | } 178 | 179 | Normal Triangle::GetNormal() const 180 | { 181 | assert(false); 182 | return Normal(); 183 | } -------------------------------------------------------------------------------- /RayTracer/Source/Color.cpp: -------------------------------------------------------------------------------- 1 | #include "Color.h" 2 | #include "Ray.h" 3 | #include 4 | #include "Renderer.h" 5 | #include 6 | 7 | inline float ra(RNG& rng) 8 | { 9 | std::uniform_real_distribution d(-0.5f, 0.5f); 10 | return d(rng); 11 | //return (float)rand()/((float)RAND_MAX) - 0.5f; 12 | } 13 | 14 | inline float ra1(RNG &rng) 15 | { 16 | std::uniform_real_distribution d(0.f, 1.f); 17 | return d(rng); 18 | //return (float)rand()/((float)RAND_MAX)); 19 | } 20 | 21 | 22 | Ray Material::ReflectRay(const Ray &ray, const Hit &hit, bool path, RNG &rng) const 23 | { 24 | Ray r(Point(), Vector(), hit.eps, ray.maxt, ray.time, ray.depth+1, hit.material.Refractive); 25 | 26 | //Save the trouble 27 | if (!path && (Reflective < 0.001f || GlossyReflective < 0.001f || ray.depth != 0)) 28 | { 29 | r.o = ray.o + ray.d*hit.tHit; 30 | r.d = Normalize(ray.d - Vector(hit.normal*Dot(ray.d, hit.normal)*2.f)); 31 | r.UpdateInverse(); 32 | return r; 33 | } 34 | return CalcReflectLerp(ray, r, hit, path, rng); 35 | //return CalcReflectApprox(ray, r, hit); 36 | } 37 | 38 | Ray Material::CalcReflectLerp(const Ray &ray, Ray &r, const Hit &hit, bool path, RNG &rng) const 39 | { 40 | // For lord and land - and pipeline stalls 41 | if (path) 42 | { 43 | if (Specular > 0.001f) 44 | { 45 | r.o = ray.o + ray.d*hit.tHit; 46 | 47 | Vector properRefl = Normalize(ray.d - Vector(2.f*(Dot(ray.d, hit.normal))*hit.normal)); 48 | Vector jitter = Vector(ra(rng)*Specular, ra(rng)*Specular, ra(rng)*Specular); 49 | r.d = Normalize(properRefl+jitter); 50 | r.UpdateInverse(); 51 | return r; 52 | } 53 | r.o = ray.o + ray.d*hit.tHit; 54 | Vector basis1 = Dot(hit.normal, ray.d) < 0.f ? 55 | Vector(hit.normal) : 56 | -Vector(hit.normal); 57 | Vector temp = Vector(0.f, 1.f, 0.f); 58 | Vector basis2 = fabsf(Dot(temp, Vector(hit.normal))) == 1.f ? 59 | Cross(Vector(1.f, 0.f, 0.f), basis1) : 60 | Cross(temp, basis1); 61 | basis2 = Normalize(basis2); 62 | Vector basis3 = Cross(basis1, basis2); 63 | float u = ra1(rng)*6.28319f; 64 | float v = ra1(rng); 65 | float w = sqrt(v); 66 | r.d = Normalize(basis2*cos(u)*w + basis3*sin(u)*w + basis1*sqrt(1.f-v)); 67 | r.UpdateInverse(); 68 | return r; 69 | } 70 | else 71 | { 72 | r.o = ray.o + ray.d*hit.tHit; 73 | Vector basis1 = Dot(hit.normal, ray.d) < 0.f ? 74 | Vector(hit.normal) : 75 | -Vector(hit.normal); 76 | Vector temp = Vector(0.f, 1.f, 0.f); 77 | Vector basis2 = fabsf(Dot(temp, Vector(hit.normal))) == 1.f ? 78 | Cross(Vector(1.f, 0.f, 0.f), basis1) : 79 | Cross(temp, basis1); 80 | basis2 = Normalize(basis2); 81 | Vector basis3 = Cross(basis1, basis2); 82 | float u = ra1(rng)*6.28319f; 83 | float v = ra1(rng); 84 | float w = sqrt(v); 85 | Vector jitter = Normalize(basis2*cos(u)*w + basis3*sin(u)*w + basis1*sqrt(1.f-v)); 86 | 87 | Vector properReflection = Normalize(ray.d - Vector(2.f*(Dot(ray.d, hit.normal))*hit.normal)); 88 | r.d = Normalize(Lerp(properReflection, jitter, GlossyReflective)); 89 | r.UpdateInverse(); 90 | return r; 91 | } 92 | } 93 | 94 | Ray Material::CalcReflectApprox(const Ray &ray, Ray &r, const Hit &hit, RNG &rng) const 95 | { 96 | float dx = ra(rng)*GlossyReflective; 97 | float dy = ra(rng)*GlossyReflective; 98 | int tries = 0; 99 | int maxTries = 20; 100 | while ((dx*dx) + (dy*dy) > (GlossyReflective*GlossyReflective) && tries < maxTries) 101 | { 102 | dx = ra(rng)*GlossyReflective; 103 | dy = ra(rng)*GlossyReflective; 104 | ++tries; 105 | } 106 | if (tries >= maxTries) 107 | { 108 | dx = 0.f; 109 | dy = 0.f; 110 | } 111 | 112 | Vector v1 = ray.d - Vector(2.f*(Dot(ray.d, hit.normal))*hit.normal); 113 | Vector temp = Vector(Cross(v1, Vector(0.0f, 1.0f, 0.0f)), true); 114 | Vector v2 = temp.HasNans() ? 115 | Cross(v1, Vector(0.0f, 0.f, 1.f)) : 116 | temp; 117 | Vector v3 = Cross(v2, v1); 118 | r.d = Normalize(v1 + v2*dx + v3*dy*GlossyReflective); 119 | r.o = ray.o + ray.d*hit.tHit; 120 | r.UpdateInverse(); 121 | return r; 122 | } 123 | 124 | Ray Material::RefractRay(const Ray &ray, const Hit &hit, bool * isValid) const 125 | { 126 | if (hit.material.Refractive > ray.refrIndex + 0.01f 127 | && hit.material.Refractive < ray.refrIndex - 0.01f) 128 | return Ray(Point(), Vector(), hit.eps, ray.maxt, ray.time, ray.depth+1, hit.material.Refractive); 129 | 130 | Ray r(Point(), Vector(), hit.eps, ray.maxt, ray.time, ray.depth+1, hit.material.Refractive); 131 | float n = ray.refrIndex/r.refrIndex; 132 | Normal N(hit.normal); 133 | float cosInc = -Dot(N, ray.d); 134 | if (cosInc < 0.f) 135 | { 136 | N = -N; 137 | cosInc = -Dot(N, ray.d); 138 | } 139 | float cosT2 = 1.f - n*n*(1.f - (cosInc*cosInc)); 140 | if (cosT2 <= 0.f || !(*isValid)) 141 | { 142 | *isValid = false; 143 | return Ray(); 144 | } 145 | *isValid = true; 146 | r.o = ray.o + ray.d*hit.tHit; 147 | r.d = Normalize(n*ray.d + Vector((n*cosInc - sqrt(cosT2))*N)); 148 | r.UpdateInverse(); 149 | return r; 150 | } 151 | 152 | -------------------------------------------------------------------------------- /RayTracer/Source/KDTree.cpp: -------------------------------------------------------------------------------- 1 | #include "KDTree.h" 2 | #include 3 | #include 4 | 5 | 6 | struct AxisComparatorX 7 | { 8 | bool operator()(Triangle* p1, Triangle* p2) const 9 | { 10 | return p1->WorldBound().GetCenter()[0] < p2->WorldBound().GetCenter()[0]; 11 | } 12 | }; 13 | struct AxisComparatorY 14 | { 15 | bool operator()(Triangle* p1, Triangle* p2) const 16 | { 17 | return p1->WorldBound().GetCenter()[1] < p2->WorldBound().GetCenter()[1]; 18 | } 19 | }; 20 | struct AxisComparatorZ 21 | { 22 | bool operator()(Triangle* p1, Triangle* p2) const 23 | { 24 | return p1->WorldBound().GetCenter()[2] < p2->WorldBound().GetCenter()[2]; 25 | } 26 | }; 27 | 28 | KDNode* KDNode::Build(std::vector* scene, int depth) 29 | { 30 | if (scene->size() == 0) 31 | { 32 | KDNode * node = new KDNode(); 33 | node->Bounds; 34 | node->Left = new KDNode(); 35 | node->Right = new KDNode(); 36 | node->Leaf = true; 37 | node->Objects = *scene; 38 | return node; 39 | } 40 | int total = scene->size(); 41 | if (depth > 24 || total < 6) 42 | { 43 | BoundingBox bounds = (*scene->begin())->WorldBounds; 44 | std::vector::iterator iScene; 45 | for (iScene = scene->begin()+1; iScene != scene->end(); ++iScene) 46 | bounds = Union(bounds, (*iScene)->WorldBounds); 47 | 48 | KDNode * node = new KDNode(); 49 | node->Bounds = bounds; 50 | node->Left = new KDNode(); 51 | node->Right = new KDNode(); 52 | node->Leaf = true; 53 | node->Objects = *scene; 54 | return node; 55 | } 56 | //get axis 57 | int axis = depth % 3; 58 | 59 | //get median-ish axis value from scene! 60 | int medLoc = (scene->size())/2; 61 | std::vector sortedObj; 62 | for (int i=0;i::iterator iSort; 72 | //for (iSort = sortedObj.begin(); iSort != sortedObj.end(); ++iSort) 73 | // printf("%f, %f, %f\n", 74 | // (*iSort)->WorldBound().GetCenter()[0], 75 | // (*iSort)->WorldBound().GetCenter()[1], 76 | // (*iSort)->WorldBound().GetCenter()[2]); 77 | //printf("sorted %i\n", axis); 78 | 79 | std::vector leftObjs(sortedObj.begin(), sortedObj.begin()+medLoc); 80 | std::vector rightObjs(sortedObj.begin()+medLoc, sortedObj.end()); 81 | //assert(leftObjs.size()+rightObjs.size()==scene->size()); 82 | 83 | BoundingBox bounds = (*scene->begin())->WorldBounds; 84 | std::vector::iterator iScene; 85 | for (iScene = scene->begin()+1; iScene != scene->end(); ++iScene) 86 | bounds = Union(bounds, (*iScene)->WorldBounds); 87 | 88 | /*std::vector::iterator iLeft; 89 | std::vector::iterator iRight; 90 | std::vector leftQueue; 91 | std::vector rightQueue; 92 | 93 | for (iLeft = leftObjs.begin(); iLeft != leftObjs.end(); ++iLeft) 94 | { 95 | for (iRight = rightObjs.begin(); iRight != rightObjs.end(); ++iRight) 96 | { 97 | if ((*iRight)->WorldBound().Touches((*iLeft)->WorldBound())) 98 | rightQueue.push_back(*iLeft); 99 | if ((*iLeft)->WorldBound().Touches((*iRight)->WorldBound())) 100 | leftQueue.push_back(*iRight); 101 | } 102 | } 103 | for (iLeft = leftQueue.begin(); iLeft != leftQueue.end(); ++iLeft) 104 | leftObjs.push_back(*iLeft); 105 | for (iRight = rightQueue.begin(); iRight != rightQueue.end(); ++iRight) 106 | rightObjs.push_back(*iRight);*/ 107 | 108 | if (leftObjs.size() == scene->size() || rightObjs.size() == scene->size()) 109 | { 110 | KDNode * node = new KDNode(); 111 | node->Bounds = bounds; 112 | node->Left = new KDNode(); 113 | node->Right = new KDNode(); 114 | node->Leaf = true; 115 | node->Objects = *scene; 116 | 117 | return node; 118 | } 119 | 120 | KDNode * node = new KDNode(); 121 | node->Bounds = bounds; 122 | node->Left = KDNode::Build(&leftObjs, depth+1); 123 | node->Right = KDNode::Build(&rightObjs, depth+1); 124 | node->Leaf = false; 125 | node->Objects = *scene; 126 | 127 | return node; 128 | } 129 | 130 | bool KDNode::Intersect(const Ray & ray, Hit * hit) 131 | { 132 | if (Objects.size() != 0 && Bounds.Intersect(ray)) 133 | { 134 | if (!Leaf) 135 | { 136 | Hit left, right; 137 | bool l = false; 138 | bool r = false; 139 | l = Left->Intersect(ray, &left); 140 | r = Right->Intersect(ray, &right); 141 | *hit = left.tHit < right.tHit ? left : right; 142 | return l || r; 143 | } 144 | else 145 | { 146 | Hit currHit, bestHit; 147 | bool didWeHit = false; 148 | std::vector::iterator iScene; 149 | for (iScene = Objects.begin(); iScene!=Objects.end(); ++iScene) 150 | { 151 | if ((*iScene)->Intersect(ray, &currHit) && currHit.tHit < bestHit.tHit) 152 | { 153 | bestHit = currHit; 154 | didWeHit = true; 155 | } 156 | } 157 | *hit = bestHit; 158 | return didWeHit; 159 | } 160 | } 161 | return false; 162 | } 163 | -------------------------------------------------------------------------------- /Lua/Source/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lstring_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | 24 | 25 | 26 | /* 27 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 28 | ** compute its hash 29 | */ 30 | #if !defined(LUAI_HASHLIMIT) 31 | #define LUAI_HASHLIMIT 5 32 | #endif 33 | 34 | 35 | /* 36 | ** equality for long strings 37 | */ 38 | int luaS_eqlngstr (TString *a, TString *b) { 39 | size_t len = a->len; 40 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); 41 | return (a == b) || /* same instance or... */ 42 | ((len == b->len) && /* equal length and ... */ 43 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 44 | } 45 | 46 | 47 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 48 | unsigned int h = seed ^ cast(unsigned int, l); 49 | size_t l1; 50 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 51 | for (l1 = l; l1 >= step; l1 -= step) 52 | h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); 53 | return h; 54 | } 55 | 56 | 57 | /* 58 | ** resizes the string table 59 | */ 60 | void luaS_resize (lua_State *L, int newsize) { 61 | int i; 62 | stringtable *tb = &G(L)->strt; 63 | if (newsize > tb->size) { /* grow table if needed */ 64 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 65 | for (i = tb->size; i < newsize; i++) 66 | tb->hash[i] = NULL; 67 | } 68 | for (i = 0; i < tb->size; i++) { /* rehash */ 69 | TString *p = tb->hash[i]; 70 | tb->hash[i] = NULL; 71 | while (p) { /* for each node in the list */ 72 | TString *hnext = p->hnext; /* save next */ 73 | unsigned int h = lmod(p->hash, newsize); /* new position */ 74 | p->hnext = tb->hash[h]; /* chain it */ 75 | tb->hash[h] = p; 76 | p = hnext; 77 | } 78 | } 79 | if (newsize < tb->size) { /* shrink table if needed */ 80 | /* vanishing slice should be empty */ 81 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 82 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 83 | } 84 | tb->size = newsize; 85 | } 86 | 87 | 88 | 89 | /* 90 | ** creates a new string object 91 | */ 92 | static TString *createstrobj (lua_State *L, const char *str, size_t l, 93 | int tag, unsigned int h) { 94 | TString *ts; 95 | GCObject *o; 96 | size_t totalsize; /* total size of TString object */ 97 | totalsize = sizelstring(l); 98 | o = luaC_newobj(L, tag, totalsize); 99 | ts = gco2ts(o); 100 | ts->len = l; 101 | ts->hash = h; 102 | ts->extra = 0; 103 | memcpy(getaddrstr(ts), str, l * sizeof(char)); 104 | getaddrstr(ts)[l] = '\0'; /* ending 0 */ 105 | return ts; 106 | } 107 | 108 | 109 | void luaS_remove (lua_State *L, TString *ts) { 110 | stringtable *tb = &G(L)->strt; 111 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 112 | while (*p != ts) /* find previous element */ 113 | p = &(*p)->hnext; 114 | *p = (*p)->hnext; /* remove element from its list */ 115 | tb->nuse--; 116 | } 117 | 118 | 119 | /* 120 | ** checks whether short string exists and reuses it or creates a new one 121 | */ 122 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 123 | TString *ts; 124 | global_State *g = G(L); 125 | unsigned int h = luaS_hash(str, l, g->seed); 126 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; 127 | for (ts = *list; ts != NULL; ts = ts->hnext) { 128 | if (l == ts->len && 129 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 130 | /* found! */ 131 | if (isdead(g, ts)) /* dead (but not collected yet)? */ 132 | changewhite(ts); /* resurrect it */ 133 | return ts; 134 | } 135 | } 136 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { 137 | luaS_resize(L, g->strt.size * 2); 138 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ 139 | } 140 | ts = createstrobj(L, str, l, LUA_TSHRSTR, h); 141 | ts->hnext = *list; 142 | *list = ts; 143 | g->strt.nuse++; 144 | return ts; 145 | } 146 | 147 | 148 | /* 149 | ** new string (with explicit length) 150 | */ 151 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 152 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 153 | return internshrstr(L, str, l); 154 | else { 155 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) 156 | luaM_toobig(L); 157 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); 158 | } 159 | } 160 | 161 | 162 | /* 163 | ** new zero-terminated string 164 | */ 165 | TString *luaS_new (lua_State *L, const char *str) { 166 | return luaS_newlstr(L, str, strlen(str)); 167 | } 168 | 169 | 170 | Udata *luaS_newudata (lua_State *L, size_t s) { 171 | Udata *u; 172 | GCObject *o; 173 | if (s > MAX_SIZE - sizeof(Udata)) 174 | luaM_toobig(L); 175 | o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); 176 | u = gco2u(o); 177 | u->len = s; 178 | u->metatable = NULL; 179 | setuservalue(L, u, luaO_nilobject); 180 | return u; 181 | } 182 | 183 | -------------------------------------------------------------------------------- /Lua/Source/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.34 2014/11/02 19:19:04 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct { 23 | lua_State *L; 24 | lua_Writer writer; 25 | void *data; 26 | int strip; 27 | int status; 28 | } DumpState; 29 | 30 | 31 | /* 32 | ** All high-level dumps go through DumpVector; you can change it to 33 | ** change the endianness of the result 34 | */ 35 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) 36 | 37 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) 38 | 39 | 40 | static void DumpBlock (const void *b, size_t size, DumpState *D) { 41 | if (D->status == 0) { 42 | lua_unlock(D->L); 43 | D->status = (*D->writer)(D->L, b, size, D->data); 44 | lua_lock(D->L); 45 | } 46 | } 47 | 48 | 49 | #define DumpVar(x,D) DumpVector(&x,1,D) 50 | 51 | 52 | static void DumpByte (int y, DumpState *D) { 53 | lu_byte x = (lu_byte)y; 54 | DumpVar(x, D); 55 | } 56 | 57 | 58 | static void DumpInt (int x, DumpState *D) { 59 | DumpVar(x, D); 60 | } 61 | 62 | 63 | static void DumpNumber (lua_Number x, DumpState *D) { 64 | DumpVar(x, D); 65 | } 66 | 67 | 68 | static void DumpInteger (lua_Integer x, DumpState *D) { 69 | DumpVar(x, D); 70 | } 71 | 72 | 73 | static void DumpString (const TString *s, DumpState *D) { 74 | if (s == NULL) 75 | DumpByte(0, D); 76 | else { 77 | size_t size = s->len + 1; /* include trailing '\0' */ 78 | if (size < 0xFF) 79 | DumpByte(cast_int(size), D); 80 | else { 81 | DumpByte(0xFF, D); 82 | DumpVar(size, D); 83 | } 84 | DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */ 85 | } 86 | } 87 | 88 | 89 | static void DumpCode (const Proto *f, DumpState *D) { 90 | DumpInt(f->sizecode, D); 91 | DumpVector(f->code, f->sizecode, D); 92 | } 93 | 94 | 95 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D); 96 | 97 | static void DumpConstants (const Proto *f, DumpState *D) { 98 | int i; 99 | int n = f->sizek; 100 | DumpInt(n, D); 101 | for (i = 0; i < n; i++) { 102 | const TValue *o = &f->k[i]; 103 | DumpByte(ttype(o), D); 104 | switch (ttype(o)) { 105 | case LUA_TNIL: 106 | break; 107 | case LUA_TBOOLEAN: 108 | DumpByte(bvalue(o), D); 109 | break; 110 | case LUA_TNUMFLT: 111 | DumpNumber(fltvalue(o), D); 112 | break; 113 | case LUA_TNUMINT: 114 | DumpInteger(ivalue(o), D); 115 | break; 116 | case LUA_TSHRSTR: 117 | case LUA_TLNGSTR: 118 | DumpString(tsvalue(o), D); 119 | break; 120 | default: 121 | lua_assert(0); 122 | } 123 | } 124 | } 125 | 126 | 127 | static void DumpProtos (const Proto *f, DumpState *D) { 128 | int i; 129 | int n = f->sizep; 130 | DumpInt(n, D); 131 | for (i = 0; i < n; i++) 132 | DumpFunction(f->p[i], f->source, D); 133 | } 134 | 135 | 136 | static void DumpUpvalues (const Proto *f, DumpState *D) { 137 | int i, n = f->sizeupvalues; 138 | DumpInt(n, D); 139 | for (i = 0; i < n; i++) { 140 | DumpByte(f->upvalues[i].instack, D); 141 | DumpByte(f->upvalues[i].idx, D); 142 | } 143 | } 144 | 145 | 146 | static void DumpDebug (const Proto *f, DumpState *D) { 147 | int i, n; 148 | n = (D->strip) ? 0 : f->sizelineinfo; 149 | DumpInt(n, D); 150 | DumpVector(f->lineinfo, n, D); 151 | n = (D->strip) ? 0 : f->sizelocvars; 152 | DumpInt(n, D); 153 | for (i = 0; i < n; i++) { 154 | DumpString(f->locvars[i].varname, D); 155 | DumpInt(f->locvars[i].startpc, D); 156 | DumpInt(f->locvars[i].endpc, D); 157 | } 158 | n = (D->strip) ? 0 : f->sizeupvalues; 159 | DumpInt(n, D); 160 | for (i = 0; i < n; i++) 161 | DumpString(f->upvalues[i].name, D); 162 | } 163 | 164 | 165 | static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { 166 | if (D->strip || f->source == psource) 167 | DumpString(NULL, D); /* no debug info or same source as its parent */ 168 | else 169 | DumpString(f->source, D); 170 | DumpInt(f->linedefined, D); 171 | DumpInt(f->lastlinedefined, D); 172 | DumpByte(f->numparams, D); 173 | DumpByte(f->is_vararg, D); 174 | DumpByte(f->maxstacksize, D); 175 | DumpCode(f, D); 176 | DumpConstants(f, D); 177 | DumpUpvalues(f, D); 178 | DumpProtos(f, D); 179 | DumpDebug(f, D); 180 | } 181 | 182 | 183 | static void DumpHeader (DumpState *D) { 184 | DumpLiteral(LUA_SIGNATURE, D); 185 | DumpByte(LUAC_VERSION, D); 186 | DumpByte(LUAC_FORMAT, D); 187 | DumpLiteral(LUAC_DATA, D); 188 | DumpByte(sizeof(int), D); 189 | DumpByte(sizeof(size_t), D); 190 | DumpByte(sizeof(Instruction), D); 191 | DumpByte(sizeof(lua_Integer), D); 192 | DumpByte(sizeof(lua_Number), D); 193 | DumpInteger(LUAC_INT, D); 194 | DumpNumber(LUAC_NUM, D); 195 | } 196 | 197 | 198 | /* 199 | ** dump Lua function as precompiled chunk 200 | */ 201 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 202 | int strip) { 203 | DumpState D; 204 | D.L = L; 205 | D.writer = w; 206 | D.data = data; 207 | D.strip = strip; 208 | D.status = 0; 209 | DumpHeader(&D); 210 | DumpByte(f->sizeupvalues, &D); 211 | DumpFunction(f, NULL, &D); 212 | return D.status; 213 | } 214 | 215 | -------------------------------------------------------------------------------- /RayTracer/Source/RayRenderer.cpp: -------------------------------------------------------------------------------- 1 | #include "RayRenderer.h" 2 | #include "Sphere.h" 3 | 4 | #define MAX_COLOR 0.999f 5 | #define MIN_COLOR 0.078f 6 | 7 | inline float r(RNG& rng) 8 | { 9 | std::uniform_real_distribution d(-0.5f, 0.5f); 10 | return d(rng); 11 | //return (float)rand()/((float)RAND_MAX) - 0.5f; 12 | } 13 | 14 | inline float r1(RNG &rng) 15 | { 16 | std::uniform_real_distribution d(0.f, 1.f); 17 | return d(rng); 18 | //return (float)rand()/((float)RAND_MAX)); 19 | } 20 | 21 | RayRenderer::RayRenderer(std::vector* scene, const Camera &ccamera, const QualityDesc &quality) 22 | :Renderer(scene, ccamera, quality) 23 | { 24 | Lights = new std::vector(); 25 | std::vector::iterator iScene; 26 | for (iScene = Scene->begin(); iScene!=Scene->end(); ++iScene) 27 | { 28 | if ((*iScene)->Type == 1) 29 | Lights->push_back((*iScene)); 30 | } 31 | LightSamples = quality.LightSamples; 32 | GlossyReflectiveSamples = quality.GlossyReflectiveSamples; 33 | Depth = quality.Depth; 34 | InvLightSamples = 1.f/(float)LightSamples; 35 | InvGlossyReflectiveSamples = 1.f/(float)GlossyReflectiveSamples; 36 | } 37 | 38 | void RayRenderer::Render() 39 | { 40 | const int height = Cam.height; 41 | const int width = Cam.width; 42 | printf("%ix%i Ray %i AA Samples, %i LightSamples, %i GlossyReflectionSamples, %i Depth\n", width, height, Samples, LightSamples, GlossyReflectiveSamples, Depth); 43 | Renderer::Render(); 44 | } 45 | 46 | RGB RayRenderer::computeColor(const Ray &reflRay, const Hit &hit, RNG &rng) 47 | { 48 | Normal normal = hit.normal; 49 | Material shapeMaterial = hit.material; 50 | Point hitPos = reflRay.o + hit.tHit*reflRay.d; 51 | Vector v = -reflRay.d; 52 | if (Dot(normal, reflRay.d) > 0.f && hit.material.RefrAbsorbance > 0.9f) 53 | normal = -normal; 54 | 55 | RGB finalColor; 56 | std::vector::iterator iLight; 57 | if (hit.type == 1) 58 | return hit.material.Color; 59 | 60 | for (iLight = Lights->begin(); iLight!=Lights->end(); ++iLight) 61 | { 62 | Sphere * currLight = dynamic_cast(*iLight); 63 | //FIX, SPHERE IS HARDCODED IN LIGHT CALCS!!! 64 | Material lightMaterial = currLight->GetMaterial(); 65 | float radius = currLight->Radius; 66 | Point lightPos = (*currLight->ObjectToWorld)(Point(0.f, 0.f, 0.f)); 67 | 68 | RGB sampleColor; 69 | for (int i=0;i Depth) 107 | return RGB(); 108 | 109 | Hit bestHit; 110 | if (FindClosest(reflRay, &bestHit)) 111 | { 112 | RGB sampleColor; 113 | int GlossyReflectiveSamplesToRun = 1; 114 | bool isGlossyReflective = 115 | (reflRay.depth == 0 116 | && bestHit.material.GlossyReflective > 0.001f 117 | && bestHit.material.Reflective > 0.001f); 118 | 119 | if (isGlossyReflective) //Only primary rays should diffuse... it's very slow! 120 | GlossyReflectiveSamplesToRun = GlossyReflectiveSamples; 121 | RGB absorb = bestHit.material.Color*bestHit.material.RefrAbsorbance*-bestHit.tHit; 122 | RGB transparency = RGB(expf(absorb.red), expf(absorb.green), expf(absorb.blue)); 123 | for (int i=0;i 0.f && !c.IsBlack(); 135 | bool canRefr = isRefr && !transparency.IsBlack(); 136 | sampleColor += c + 137 | (canRefl ? c*Trace(nextReflRay, rng)*refl : RGB()) 138 | + (canRefr ? transparency*Trace(nextRefrRay, rng) : RGB()); 139 | //OPTIMIZE FURTHER! 140 | } 141 | else 142 | sampleColor += c; 143 | } 144 | 145 | if (isGlossyReflective) // If a primary ray we have done diffusion so average 146 | sampleColor *= InvGlossyReflectiveSamples; 147 | 148 | return sampleColor; 149 | } 150 | 151 | return RGB(); 152 | } 153 | 154 | bool RayRenderer::ShadowTest(const Ray &ray) 155 | { 156 | std::vector::iterator iScene; 157 | Hit currHit; 158 | for (iScene = Scene->begin(); iScene!=Scene->end(); ++iScene) 159 | { 160 | if ((*iScene)->Intersect(ray, &currHit) && currHit.type == 0 && currHit.material.RefrAbsorbance > 0.4f) 161 | return true; 162 | } 163 | return Root->Intersect(ray, &currHit); 164 | } 165 | -------------------------------------------------------------------------------- /RayTracer/Source/Transform.cpp: -------------------------------------------------------------------------------- 1 | #include "Transform.h" 2 | #include 3 | #include 4 | #include 5 | #include "Normal.h" 6 | 7 | Transform::Transform() 8 | { 9 | m = Matrix4x4(); 10 | mInv = m; 11 | } 12 | 13 | Transform::Transform(const float mat[4][4]) 14 | { 15 | m = Matrix4x4(mat[0][0], mat[0][1], mat[0][2], mat[0][3], 16 | mat[1][0], mat[1][1], mat[1][2], mat[1][3], 17 | mat[2][0], mat[2][1], mat[2][2], mat[2][3], 18 | mat[3][0], mat[3][1], mat[3][2], mat[3][3]); 19 | mInv = Inverse(m); 20 | } 21 | 22 | Transform::Transform(const Matrix4x4 &mat) 23 | { 24 | m = mat; 25 | mInv = Inverse(m); 26 | } 27 | 28 | Transform::Transform(const Matrix4x4 &mat, const Matrix4x4 &inv) 29 | { 30 | m = mat; 31 | mInv = inv; 32 | } 33 | 34 | Transform Inverse(const Transform &t) 35 | { 36 | return Transform(t.mInv, t.m); 37 | } 38 | 39 | bool Transform::IsIdentity() 40 | { 41 | Matrix4x4 i; 42 | return (m == i); 43 | } 44 | 45 | bool Transform::HasScale() 46 | { 47 | assert(false); 48 | // NOT IMPLEMENTED/NECESSARY??? 49 | return false; 50 | } 51 | 52 | bool Transform::operator==(const Transform &t) 53 | { 54 | return (m == t.m && mInv == t.mInv); 55 | } 56 | 57 | bool Transform::operator!=(const Transform &t) 58 | { 59 | return !(m == t.m && mInv == t.mInv); 60 | } 61 | 62 | Point Transform::operator()(const Point &p) const 63 | { 64 | float x = p.x; float y = p.y; float z = p.z; 65 | float newX = m.m[0][0]*x + m.m[0][1]*y + m.m[0][2]*z + m.m[0][3]; 66 | float newY = m.m[1][0]*x + m.m[1][1]*y + m.m[1][2]*z + m.m[1][3]; 67 | float newZ = m.m[2][0]*x + m.m[2][1]*y + m.m[2][2]*z + m.m[2][3]; 68 | float newW = m.m[3][0]*x + m.m[3][1]*y + m.m[3][2]*z + m.m[3][3]; 69 | if (newW == 1.f) return Point(newX, newY, newZ); 70 | else return (Point(newX, newY, newZ)/newW); 71 | } 72 | 73 | void Transform::operator()(const Point &p, Point *pTrans) const 74 | { 75 | float x = p.x; float y = p.y; float z = p.z; 76 | pTrans->x = m.m[0][0]*x + m.m[0][1]*y + m.m[0][2]*z + m.m[0][3]; 77 | pTrans->y = m.m[1][0]*x + m.m[1][1]*y + m.m[1][2]*z + m.m[1][3]; 78 | pTrans->z = m.m[2][0]*x + m.m[2][1]*y + m.m[2][2]*z + m.m[2][3]; 79 | float newW = m.m[3][0]*x + m.m[3][1]*y + m.m[3][2]*z + m.m[3][3]; 80 | if (newW != 1.f) (*pTrans)/=newW; 81 | } 82 | 83 | Vector Transform::operator()(const Vector &v) const 84 | { 85 | float x = v.x; float y = v.y; float z = v.z; 86 | return Vector(m.m[0][0]*x + m.m[0][1]*y + m.m[0][2]*z, 87 | m.m[1][0]*x + m.m[1][1]*y + m.m[1][2]*z, 88 | m.m[2][0]*x + m.m[2][1]*y + m.m[2][2]*z); 89 | } 90 | 91 | void Transform::operator()(const Vector &v, Vector *vTrans) const 92 | { 93 | float x = v.x; float y = v.y; float z = v.z; 94 | vTrans->x = m.m[0][0]*x + m.m[0][1]*y + m.m[0][2]*z; 95 | vTrans->y = m.m[1][0]*x + m.m[1][1]*y + m.m[1][2]*z; 96 | vTrans->z = m.m[2][0]*x + m.m[2][1]*y + m.m[2][2]*z; 97 | } 98 | 99 | Normal Transform::operator()(const Normal &n) const 100 | { 101 | float x = n.x; float y = n.y; float z = n.z; 102 | return Normal(mInv.m[0][0]*x + mInv.m[1][0]*y + mInv.m[2][0]*z, 103 | mInv.m[0][1]*x + mInv.m[1][1]*y + mInv.m[2][1]*z, 104 | mInv.m[0][2]*x + mInv.m[1][2]*y + mInv.m[2][2]*z); 105 | } 106 | 107 | void Transform::operator()(const Normal &n, Normal *nTrans) const 108 | { 109 | float x = n.x; float y = n.y; float z = n.z; 110 | nTrans->x = mInv.m[0][0]*x + mInv.m[1][0]*y + mInv.m[2][0]*z; 111 | nTrans->y = mInv.m[0][1]*x + mInv.m[1][1]*y + mInv.m[2][1]*z; 112 | nTrans->z = mInv.m[0][2]*x + mInv.m[1][2]*y + mInv.m[2][2]*z; 113 | } 114 | 115 | Ray Transform::operator()(const Ray &r) const 116 | { 117 | Ray ret = r; 118 | (*this)(ret.o, &ret.o); 119 | (*this)(ret.d, &ret.d); 120 | ret.UpdateInverse(); 121 | return ret; 122 | } 123 | 124 | void Transform::operator()(const Ray &r, Ray *rTrans) const 125 | { 126 | (*this)(r.o, &(rTrans->o)); 127 | (*this)(r.d, &(rTrans->d)); 128 | rTrans->maxt = r.maxt; 129 | rTrans->mint = r.mint; 130 | rTrans->depth = r.depth; 131 | rTrans->refrIndex = r.refrIndex; 132 | rTrans->time = r.time; 133 | rTrans->UpdateInverse(); 134 | } 135 | 136 | BoundingBox Transform::operator()(const BoundingBox &bbox) const 137 | { 138 | const Transform &T = (*this); 139 | BoundingBox ret( T(Point(bbox.Min.x, bbox.Min.y, bbox.Min.z))); 140 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Min.y, bbox.Min.z))); 141 | ret = Union(ret, T(Point(bbox.Min.x, bbox.Max.y, bbox.Min.z))); 142 | ret = Union(ret, T(Point(bbox.Min.x, bbox.Min.y, bbox.Max.z))); 143 | ret = Union(ret, T(Point(bbox.Min.x, bbox.Max.y, bbox.Max.z))); 144 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Max.y, bbox.Min.z))); 145 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Min.y, bbox.Max.z))); 146 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Max.y, bbox.Max.z))); 147 | return ret; 148 | } 149 | 150 | void Transform::operator()(const BoundingBox &bbox, BoundingBox *bboxTrans) const 151 | { 152 | const Transform &T = (*this); 153 | BoundingBox ret( T(Point(bbox.Min.x, bbox.Min.y, bbox.Min.z))); 154 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Min.y, bbox.Min.z))); 155 | ret = Union(ret, T(Point(bbox.Min.x, bbox.Max.y, bbox.Min.z))); 156 | ret = Union(ret, T(Point(bbox.Min.x, bbox.Min.y, bbox.Max.z))); 157 | ret = Union(ret, T(Point(bbox.Min.x, bbox.Max.y, bbox.Max.z))); 158 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Max.y, bbox.Min.z))); 159 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Min.y, bbox.Max.z))); 160 | ret = Union(ret, T(Point(bbox.Max.x, bbox.Max.y, bbox.Max.z))); 161 | bboxTrans->Min = ret.Min; 162 | bboxTrans->Max = ret.Max; 163 | } 164 | 165 | Transform Transform::operator()(const Transform &trans) const 166 | { 167 | Matrix4x4 m1 = Matrix4x4::Mul(m, trans.m); 168 | Matrix4x4 m2 = Matrix4x4::Mul(trans.mInv, mInv); 169 | return Transform(m1, m2); 170 | } 171 | 172 | void Transform::operator()(const Transform &trans, Transform *transTrans) const 173 | { 174 | transTrans->m = Matrix4x4::Mul(m, trans.m); 175 | transTrans->mInv = Matrix4x4::Mul(trans.mInv, mInv); 176 | } 177 | 178 | -------------------------------------------------------------------------------- /RayTracer/Source/PathRenderer.cpp: -------------------------------------------------------------------------------- 1 | #include "PathRenderer.h" 2 | #include "Sphere.h" 3 | #include 4 | #define MAX_COLOR 0.999f 5 | #define MIN_COLOR 0.078f 6 | 7 | inline float r(RNG& rng) 8 | { 9 | std::uniform_real_distribution d(-0.5f, 0.5f); 10 | return d(rng); 11 | //return (float)rand()/((float)RAND_MAX) - 0.5f; 12 | } 13 | 14 | inline float r1(RNG &rng) 15 | { 16 | std::uniform_real_distribution d(0.f, 1.f); 17 | return d(rng); 18 | //return (float)rand()/((float)RAND_MAX)); 19 | } 20 | 21 | PathRenderer::PathRenderer(std::vector* scene, const Camera &ccamera, const QualityDesc &quality) 22 | :Renderer(scene, ccamera, quality) 23 | { 24 | Lights = new std::vector(); 25 | std::vector::iterator iScene; 26 | for (iScene = Scene->begin(); iScene!=Scene->end(); ++iScene) 27 | { 28 | if ((*iScene)->Type == 1) 29 | Lights->push_back((*iScene)); 30 | } 31 | } 32 | 33 | void PathRenderer::Render() 34 | { 35 | printf("%ix%i Path %i Samples\n", Cam.width, Cam.height, Samples); 36 | Renderer::Render(); 37 | } 38 | 39 | 40 | RGB PathRenderer::CalcDirectLighting(const Ray &reflRay, const Hit &hit, RNG &rng) 41 | { 42 | Normal normal = hit.normal; 43 | Material shapeMaterial = hit.material; 44 | Point hitPos = reflRay.o + hit.tHit*reflRay.d; 45 | Vector v = -reflRay.d; 46 | if (Dot(normal, reflRay.d) > 0.f && hit.material.RefrAbsorbance > 0.9f) 47 | normal = -normal; 48 | 49 | RGB finalColor; 50 | std::vector::iterator iLight; 51 | if (hit.type == 1) 52 | return hit.material.Color; 53 | 54 | for (iLight = Lights->begin(); iLight!=Lights->end(); ++iLight) 55 | { 56 | Sphere * currLight = dynamic_cast(*iLight); 57 | //FIX, SPHERE IS HARDCODED IN LIGHT CALCS!!! 58 | Material lightMaterial = currLight->GetMaterial(); 59 | float radius = currLight->Radius; 60 | Point lightPos = (*currLight->ObjectToWorld)(Point(0.f, 0.f, 0.f)); 61 | 62 | Vector jitter = Vector(r(rng), r(rng), r(rng)); 63 | jitter = radius*Normalize(jitter); 64 | 65 | Vector l = ((lightPos+jitter) - hitPos); 66 | float distToLight = l.Length(); 67 | l = Normalize(l); 68 | 69 | if (ShadowTest(Ray(hitPos, l, hit.eps, distToLight))) 70 | continue; 71 | 72 | Vector r = Vector(2.f*Dot(l, normal)*normal) - l; 73 | 74 | RGB specular = RGB(); 75 | if (shapeMaterial.Specular > 0.001f) 76 | { 77 | float spec = pow(std::min(1.0f, std::max(0.f, Dot(r, v))),20.f); 78 | specular = lightMaterial.Color*shapeMaterial.Specular*spec*lightMaterial.Color*lightMaterial.Emissive*0.15915f; 79 | } 80 | 81 | RGB diffuse = RGB(); 82 | if (shapeMaterial.Diffuse > 0.001f) 83 | { 84 | float diff = std::min(1.0f, std::max(0.f, Dot(normal, l))); 85 | diffuse = shapeMaterial.Color*diff*shapeMaterial.Diffuse*lightMaterial.Color*lightMaterial.Emissive*0.31831f; 86 | } 87 | 88 | float invsize = 1.f;//Lights->size(); 89 | finalColor += (diffuse + specular)*invsize; 90 | //finalColor.Bound(MIN_COLOR, MAX_COLOR); 91 | } 92 | 93 | return finalColor; 94 | } 95 | 96 | RGB PathRenderer::Trace(const Ray &reflRay, RNG &rng) 97 | { 98 | if (Quality.PathEnableIndirectIllum && Quality.PathEnableDirectLighting) 99 | return DirectLightIndirectIllumTrace(reflRay, rng); 100 | 101 | Hit bestHit; 102 | if (FindClosest(reflRay, &bestHit)) 103 | { 104 | if (bestHit.type == 1) 105 | return bestHit.material.Color*bestHit.material.Emissive; 106 | 107 | RGB c = bestHit.material.Color; 108 | float maxRefl = c.red>c.green && c.red>c.blue ? c.red : c.green>c.blue ? c.green : c.blue; 109 | if (Quality.PathEnableDirectLighting) 110 | { 111 | if (reflRay.depth+1>2) 112 | { 113 | if (r1(rng) < maxRefl*0.9f) 114 | c *= (0.9f*maxRefl); 115 | else 116 | return RGB(true); 117 | } 118 | } 119 | else 120 | { 121 | if (reflRay.depth+1>5) 122 | { 123 | if (r1(rng) < maxRefl*0.9f) 124 | c *= (0.9f*maxRefl); 125 | else 126 | return RGB(true); 127 | } 128 | } 129 | 130 | Ray nextReflRay = bestHit.material.ReflectRay(reflRay, bestHit, true, rng); 131 | 132 | if (Quality.PathEnableDirectLighting) 133 | { 134 | c += CalcDirectLighting(reflRay, bestHit, rng); 135 | c.Bound(MIN_COLOR, MAX_COLOR); 136 | } 137 | 138 | return c*Trace(nextReflRay, rng); 139 | } 140 | else 141 | return RGB(); 142 | } 143 | 144 | //Only do Indirect Illumination if DirectLight also enabled. 145 | //Saves on branching in above function, and it looks like 146 | //garbage alone, anyway 147 | RGB PathRenderer::DirectLightIndirectIllumTrace(const Ray &reflRay, RNG &rng) 148 | { 149 | Hit bestHit; 150 | if (FindClosest(reflRay, &bestHit)) 151 | { 152 | if (bestHit.type == 1 && reflRay.depth>0) 153 | return RGB(); 154 | if (bestHit.type == 1 && reflRay.depth==0) 155 | return bestHit.material.Color*bestHit.material.Emissive; 156 | 157 | RGB c = RGB();// = bestHit.material.Color; 158 | float maxRefl = c.red>c.green && c.red>c.blue ? c.red : c.green>c.blue ? c.green : c.blue; 159 | 160 | float rr = 1.f; 161 | if (reflRay.depth+1>2) 162 | { 163 | if (r1(rng) < maxRefl) 164 | rr = (1.f/maxRefl); 165 | else 166 | return RGB(); 167 | } 168 | //rr = std::min(1.f, rr); 169 | Ray nextReflRay = bestHit.material.ReflectRay(reflRay, bestHit, true, rng); 170 | 171 | c += CalcDirectLighting(reflRay, bestHit, rng); 172 | c += DirectLightIndirectIllumTrace(nextReflRay, rng)* 173 | 2.f*Dot(bestHit.normal, nextReflRay.d)*bestHit.material.Color*rr; 174 | //c.Bound(MIN_COLOR, MAX_COLOR); 175 | return c; 176 | } 177 | else 178 | return RGB(); 179 | } 180 | 181 | 182 | bool PathRenderer::ShadowTest(const Ray &ray) 183 | { 184 | std::vector::iterator iScene; 185 | Hit currHit; 186 | for (iScene = Scene->begin(); iScene!=Scene->end(); ++iScene) 187 | { 188 | if ((*iScene)->Intersect(ray, &currHit) && currHit.type == 0 && currHit.material.RefrAbsorbance > 0.4f) 189 | return true; 190 | } 191 | return Root->Intersect(ray, &currHit); 192 | } 193 | -------------------------------------------------------------------------------- /Lua/Source/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | /* number of bits to consider in a number */ 23 | #if !defined(LUA_NBITS) 24 | #define LUA_NBITS 32 25 | #endif 26 | 27 | 28 | /* 29 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 30 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 31 | ** number of bits in a lua_Unsigned.) 32 | */ 33 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 34 | 35 | 36 | /* macro to trim extra bits */ 37 | #define trim(x) ((x) & ALLONES) 38 | 39 | 40 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 41 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 42 | 43 | 44 | 45 | static lua_Unsigned andaux (lua_State *L) { 46 | int i, n = lua_gettop(L); 47 | lua_Unsigned r = ~(lua_Unsigned)0; 48 | for (i = 1; i <= n; i++) 49 | r &= luaL_checkunsigned(L, i); 50 | return trim(r); 51 | } 52 | 53 | 54 | static int b_and (lua_State *L) { 55 | lua_Unsigned r = andaux(L); 56 | lua_pushunsigned(L, r); 57 | return 1; 58 | } 59 | 60 | 61 | static int b_test (lua_State *L) { 62 | lua_Unsigned r = andaux(L); 63 | lua_pushboolean(L, r != 0); 64 | return 1; 65 | } 66 | 67 | 68 | static int b_or (lua_State *L) { 69 | int i, n = lua_gettop(L); 70 | lua_Unsigned r = 0; 71 | for (i = 1; i <= n; i++) 72 | r |= luaL_checkunsigned(L, i); 73 | lua_pushunsigned(L, trim(r)); 74 | return 1; 75 | } 76 | 77 | 78 | static int b_xor (lua_State *L) { 79 | int i, n = lua_gettop(L); 80 | lua_Unsigned r = 0; 81 | for (i = 1; i <= n; i++) 82 | r ^= luaL_checkunsigned(L, i); 83 | lua_pushunsigned(L, trim(r)); 84 | return 1; 85 | } 86 | 87 | 88 | static int b_not (lua_State *L) { 89 | lua_Unsigned r = ~luaL_checkunsigned(L, 1); 90 | lua_pushunsigned(L, trim(r)); 91 | return 1; 92 | } 93 | 94 | 95 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { 96 | if (i < 0) { /* shift right? */ 97 | i = -i; 98 | r = trim(r); 99 | if (i >= LUA_NBITS) r = 0; 100 | else r >>= i; 101 | } 102 | else { /* shift left */ 103 | if (i >= LUA_NBITS) r = 0; 104 | else r <<= i; 105 | r = trim(r); 106 | } 107 | lua_pushunsigned(L, r); 108 | return 1; 109 | } 110 | 111 | 112 | static int b_lshift (lua_State *L) { 113 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2)); 114 | } 115 | 116 | 117 | static int b_rshift (lua_State *L) { 118 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 119 | } 120 | 121 | 122 | static int b_arshift (lua_State *L) { 123 | lua_Unsigned r = luaL_checkunsigned(L, 1); 124 | lua_Integer i = luaL_checkinteger(L, 2); 125 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 126 | return b_shift(L, r, -i); 127 | else { /* arithmetic shift for 'negative' number */ 128 | if (i >= LUA_NBITS) r = ALLONES; 129 | else 130 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 131 | lua_pushunsigned(L, r); 132 | return 1; 133 | } 134 | } 135 | 136 | 137 | static int b_rot (lua_State *L, lua_Integer d) { 138 | lua_Unsigned r = luaL_checkunsigned(L, 1); 139 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 140 | r = trim(r); 141 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 142 | r = (r << i) | (r >> (LUA_NBITS - i)); 143 | lua_pushunsigned(L, trim(r)); 144 | return 1; 145 | } 146 | 147 | 148 | static int b_lrot (lua_State *L) { 149 | return b_rot(L, luaL_checkinteger(L, 2)); 150 | } 151 | 152 | 153 | static int b_rrot (lua_State *L) { 154 | return b_rot(L, -luaL_checkinteger(L, 2)); 155 | } 156 | 157 | 158 | /* 159 | ** get field and width arguments for field-manipulation functions, 160 | ** checking whether they are valid. 161 | ** ('luaL_error' called without 'return' to avoid later warnings about 162 | ** 'width' being used uninitialized.) 163 | */ 164 | static int fieldargs (lua_State *L, int farg, int *width) { 165 | lua_Integer f = luaL_checkinteger(L, farg); 166 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 167 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 168 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 169 | if (f + w > LUA_NBITS) 170 | luaL_error(L, "trying to access non-existent bits"); 171 | *width = (int)w; 172 | return (int)f; 173 | } 174 | 175 | 176 | static int b_extract (lua_State *L) { 177 | int w; 178 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); 179 | int f = fieldargs(L, 2, &w); 180 | r = (r >> f) & mask(w); 181 | lua_pushunsigned(L, r); 182 | return 1; 183 | } 184 | 185 | 186 | static int b_replace (lua_State *L) { 187 | int w; 188 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); 189 | lua_Unsigned v = luaL_checkunsigned(L, 2); 190 | int f = fieldargs(L, 3, &w); 191 | int m = mask(w); 192 | v &= m; /* erase bits outside given width */ 193 | r = (r & ~(m << f)) | (v << f); 194 | lua_pushunsigned(L, r); 195 | return 1; 196 | } 197 | 198 | 199 | static const luaL_Reg bitlib[] = { 200 | {"arshift", b_arshift}, 201 | {"band", b_and}, 202 | {"bnot", b_not}, 203 | {"bor", b_or}, 204 | {"bxor", b_xor}, 205 | {"btest", b_test}, 206 | {"extract", b_extract}, 207 | {"lrotate", b_lrot}, 208 | {"lshift", b_lshift}, 209 | {"replace", b_replace}, 210 | {"rrotate", b_rrot}, 211 | {"rshift", b_rshift}, 212 | {NULL, NULL} 213 | }; 214 | 215 | 216 | 217 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 218 | luaL_newlib(L, bitlib); 219 | return 1; 220 | } 221 | 222 | 223 | #else /* }{ */ 224 | 225 | 226 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 227 | return luaL_error(L, "library 'bit32' has been deprecated"); 228 | } 229 | 230 | #endif /* } */ 231 | -------------------------------------------------------------------------------- /RayTracer/RayTracer.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | {50b3b885-208d-4847-80ff-0476845f710c} 63 | 64 | 65 | 66 | {DB9CB3C5-C472-4108-992D-A92BFB8396DA} 67 | RayTracer 68 | Tracer 69 | 70 | 71 | 72 | Application 73 | true 74 | v120 75 | MultiByte 76 | 77 | 78 | Application 79 | false 80 | v120 81 | true 82 | MultiByte 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | $(SolutionDir)$(Configuration)\ 96 | 97 | 98 | 99 | Level3 100 | Disabled 101 | true 102 | Z:\repos\RayTracer\Lua\Source;%(AdditionalIncludeDirectories) 103 | 104 | 105 | true 106 | ..\Release\Lua.lib;%(AdditionalDependencies) 107 | 108 | 109 | 110 | 111 | Level3 112 | MaxSpeed 113 | true 114 | true 115 | true 116 | 117 | 118 | true 119 | true 120 | true 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /Lua/Source/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.125 2014/12/19 13:30:23 roberto Exp $ 3 | ** Limits, basic types, and some other 'installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | /* 18 | ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count 19 | ** the total memory used by Lua (in bytes). Usually, 'size_t' and 20 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. 21 | */ 22 | #if defined(LUAI_MEM) /* { external definitions? */ 23 | typedef LUAI_UMEM lu_mem; 24 | typedef LUAI_MEM l_mem; 25 | #elif LUAI_BITSINT >= 32 /* }{ */ 26 | typedef size_t lu_mem; 27 | typedef ptrdiff_t l_mem; 28 | #else /* 16-bit ints */ /* }{ */ 29 | typedef unsigned long lu_mem; 30 | typedef long l_mem; 31 | #endif /* } */ 32 | 33 | 34 | /* chars used as small naturals (so that 'char' is reserved for characters) */ 35 | typedef unsigned char lu_byte; 36 | 37 | 38 | /* maximum value for size_t */ 39 | #define MAX_SIZET ((size_t)(~(size_t)0)) 40 | 41 | /* maximum size visible for Lua (must be representable in a lua_Integer */ 42 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ 43 | : (size_t)(LUA_MAXINTEGER)) 44 | 45 | 46 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) 47 | 48 | #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) 49 | 50 | 51 | #define MAX_INT INT_MAX /* maximum value of an int */ 52 | 53 | 54 | /* 55 | ** conversion of pointer to integer: 56 | ** this is for hashing only; there is no problem if the integer 57 | ** cannot hold the whole pointer value 58 | */ 59 | #define point2int(p) ((unsigned int)((size_t)(p) & UINT_MAX)) 60 | 61 | 62 | 63 | /* type to ensure maximum alignment */ 64 | #if defined(LUAI_USER_ALIGNMENT_T) 65 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 66 | #else 67 | typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; 68 | #endif 69 | 70 | 71 | 72 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */ 73 | typedef LUAI_UACNUMBER l_uacNumber; 74 | typedef LUAI_UACINT l_uacInt; 75 | 76 | 77 | /* internal assertions for in-house debugging */ 78 | #if defined(lua_assert) 79 | #define check_exp(c,e) (lua_assert(c), (e)) 80 | /* to avoid problems with conditions too long */ 81 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 82 | #else 83 | #define lua_assert(c) ((void)0) 84 | #define check_exp(c,e) (e) 85 | #define lua_longassert(c) ((void)0) 86 | #endif 87 | 88 | /* 89 | ** assertion for checking API calls 90 | */ 91 | #if defined(LUA_USE_APICHECK) 92 | #include 93 | #define luai_apicheck(e) assert(e) 94 | #else 95 | #define luai_apicheck(e) lua_assert(e) 96 | #endif 97 | 98 | 99 | #define api_check(e,msg) luai_apicheck((e) && msg) 100 | 101 | 102 | #if !defined(UNUSED) 103 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 104 | #endif 105 | 106 | 107 | #define cast(t, exp) ((t)(exp)) 108 | 109 | #define cast_void(i) cast(void, (i)) 110 | #define cast_byte(i) cast(lu_byte, (i)) 111 | #define cast_num(i) cast(lua_Number, (i)) 112 | #define cast_int(i) cast(int, (i)) 113 | #define cast_uchar(i) cast(unsigned char, (i)) 114 | 115 | 116 | /* cast a signed lua_Integer to lua_Unsigned */ 117 | #if !defined(l_castS2U) 118 | #define l_castS2U(i) ((lua_Unsigned)(i)) 119 | #endif 120 | 121 | /* 122 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is 123 | ** not strict ISO C, but two-complement architectures should 124 | ** work fine. 125 | */ 126 | #if !defined(l_castU2S) 127 | #define l_castU2S(i) ((lua_Integer)(i)) 128 | #endif 129 | 130 | 131 | /* 132 | ** non-return type 133 | */ 134 | #if defined(__GNUC__) 135 | #define l_noret void __attribute__((noreturn)) 136 | #elif defined(_MSC_VER) && _MSC_VER >= 1200 137 | #define l_noret void __declspec(noreturn) 138 | #else 139 | #define l_noret void 140 | #endif 141 | 142 | 143 | 144 | /* 145 | ** maximum depth for nested C calls and syntactical nested non-terminals 146 | ** in a program. (Value must fit in an unsigned short int.) 147 | */ 148 | #if !defined(LUAI_MAXCCALLS) 149 | #define LUAI_MAXCCALLS 200 150 | #endif 151 | 152 | /* 153 | ** maximum number of upvalues in a closure (both C and Lua). (Value 154 | ** must fit in an unsigned char.) 155 | */ 156 | #define MAXUPVAL UCHAR_MAX 157 | 158 | 159 | /* 160 | ** type for virtual-machine instructions; 161 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 162 | */ 163 | #if LUAI_BITSINT >= 32 164 | typedef unsigned int Instruction; 165 | #else 166 | typedef unsigned long Instruction; 167 | #endif 168 | 169 | 170 | 171 | 172 | /* minimum size for the string table (must be power of 2) */ 173 | #if !defined(MINSTRTABSIZE) 174 | #define MINSTRTABSIZE 64 /* minimum size for "predefined" strings */ 175 | #endif 176 | 177 | 178 | /* minimum size for string buffer */ 179 | #if !defined(LUA_MINBUFFER) 180 | #define LUA_MINBUFFER 32 181 | #endif 182 | 183 | 184 | #if !defined(lua_lock) 185 | #define lua_lock(L) ((void) 0) 186 | #define lua_unlock(L) ((void) 0) 187 | #endif 188 | 189 | #if !defined(luai_threadyield) 190 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 191 | #endif 192 | 193 | 194 | /* 195 | ** these macros allow user-specific actions on threads when you defined 196 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 197 | ** created/deleted/resumed/yielded. 198 | */ 199 | #if !defined(luai_userstateopen) 200 | #define luai_userstateopen(L) ((void)L) 201 | #endif 202 | 203 | #if !defined(luai_userstateclose) 204 | #define luai_userstateclose(L) ((void)L) 205 | #endif 206 | 207 | #if !defined(luai_userstatethread) 208 | #define luai_userstatethread(L,L1) ((void)L) 209 | #endif 210 | 211 | #if !defined(luai_userstatefree) 212 | #define luai_userstatefree(L,L1) ((void)L) 213 | #endif 214 | 215 | #if !defined(luai_userstateresume) 216 | #define luai_userstateresume(L,n) ((void)L) 217 | #endif 218 | 219 | #if !defined(luai_userstateyield) 220 | #define luai_userstateyield(L,n) ((void)L) 221 | #endif 222 | 223 | 224 | 225 | /* 226 | ** macro to control inclusion of some hard tests on stack reallocation 227 | */ 228 | #if !defined(HARDSTACKTESTS) 229 | #define condmovestack(L) ((void)0) 230 | #else 231 | /* realloc stack keeping its size */ 232 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 233 | #endif 234 | 235 | #if !defined(HARDMEMTESTS) 236 | #define condchangemem(L) condmovestack(L) 237 | #else 238 | #define condchangemem(L) \ 239 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 240 | #endif 241 | 242 | #endif 243 | -------------------------------------------------------------------------------- /Lua/Lua.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {50B3B885-208D-4847-80FF-0476845F710C} 15 | Lua 16 | 17 | 18 | 19 | Application 20 | true 21 | v120 22 | MultiByte 23 | 24 | 25 | StaticLibrary 26 | false 27 | v120 28 | true 29 | MultiByte 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Level3 45 | Disabled 46 | true 47 | 48 | 49 | true 50 | 51 | 52 | 53 | 54 | Level3 55 | MaxSpeed 56 | true 57 | true 58 | true 59 | 60 | 61 | true 62 | true 63 | true 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /Lua/Source/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.41 2014/11/02 19:19:04 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstring.h" 23 | #include "lundump.h" 24 | #include "lzio.h" 25 | 26 | 27 | #if !defined(luai_verifycode) 28 | #define luai_verifycode(L,b,f) /* empty */ 29 | #endif 30 | 31 | 32 | typedef struct { 33 | lua_State *L; 34 | ZIO *Z; 35 | Mbuffer *b; 36 | const char *name; 37 | } LoadState; 38 | 39 | 40 | static l_noret error(LoadState *S, const char *why) { 41 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); 42 | luaD_throw(S->L, LUA_ERRSYNTAX); 43 | } 44 | 45 | 46 | /* 47 | ** All high-level loads go through LoadVector; you can change it to 48 | ** adapt to the endianness of the input 49 | */ 50 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) 51 | 52 | static void LoadBlock (LoadState *S, void *b, size_t size) { 53 | if (luaZ_read(S->Z, b, size) != 0) 54 | error(S, "truncated"); 55 | } 56 | 57 | 58 | #define LoadVar(S,x) LoadVector(S,&x,1) 59 | 60 | 61 | static lu_byte LoadByte (LoadState *S) { 62 | lu_byte x; 63 | LoadVar(S, x); 64 | return x; 65 | } 66 | 67 | 68 | static int LoadInt (LoadState *S) { 69 | int x; 70 | LoadVar(S, x); 71 | return x; 72 | } 73 | 74 | 75 | static lua_Number LoadNumber (LoadState *S) { 76 | lua_Number x; 77 | LoadVar(S, x); 78 | return x; 79 | } 80 | 81 | 82 | static lua_Integer LoadInteger (LoadState *S) { 83 | lua_Integer x; 84 | LoadVar(S, x); 85 | return x; 86 | } 87 | 88 | 89 | static TString *LoadString (LoadState *S) { 90 | size_t size = LoadByte(S); 91 | if (size == 0xFF) 92 | LoadVar(S, size); 93 | if (size == 0) 94 | return NULL; 95 | else { 96 | char *s = luaZ_openspace(S->L, S->b, --size); 97 | LoadVector(S, s, size); 98 | return luaS_newlstr(S->L, s, size); 99 | } 100 | } 101 | 102 | 103 | static void LoadCode (LoadState *S, Proto *f) { 104 | int n = LoadInt(S); 105 | f->code = luaM_newvector(S->L, n, Instruction); 106 | f->sizecode = n; 107 | LoadVector(S, f->code, n); 108 | } 109 | 110 | 111 | static void LoadFunction(LoadState *S, Proto *f, TString *psource); 112 | 113 | 114 | static void LoadConstants (LoadState *S, Proto *f) { 115 | int i; 116 | int n = LoadInt(S); 117 | f->k = luaM_newvector(S->L, n, TValue); 118 | f->sizek = n; 119 | for (i = 0; i < n; i++) 120 | setnilvalue(&f->k[i]); 121 | for (i = 0; i < n; i++) { 122 | TValue *o = &f->k[i]; 123 | int t = LoadByte(S); 124 | switch (t) { 125 | case LUA_TNIL: 126 | setnilvalue(o); 127 | break; 128 | case LUA_TBOOLEAN: 129 | setbvalue(o, LoadByte(S)); 130 | break; 131 | case LUA_TNUMFLT: 132 | setfltvalue(o, LoadNumber(S)); 133 | break; 134 | case LUA_TNUMINT: 135 | setivalue(o, LoadInteger(S)); 136 | break; 137 | case LUA_TSHRSTR: 138 | case LUA_TLNGSTR: 139 | setsvalue2n(S->L, o, LoadString(S)); 140 | break; 141 | default: 142 | lua_assert(0); 143 | } 144 | } 145 | } 146 | 147 | 148 | static void LoadProtos (LoadState *S, Proto *f) { 149 | int i; 150 | int n = LoadInt(S); 151 | f->p = luaM_newvector(S->L, n, Proto *); 152 | f->sizep = n; 153 | for (i = 0; i < n; i++) 154 | f->p[i] = NULL; 155 | for (i = 0; i < n; i++) { 156 | f->p[i] = luaF_newproto(S->L); 157 | LoadFunction(S, f->p[i], f->source); 158 | } 159 | } 160 | 161 | 162 | static void LoadUpvalues (LoadState *S, Proto *f) { 163 | int i, n; 164 | n = LoadInt(S); 165 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc); 166 | f->sizeupvalues = n; 167 | for (i = 0; i < n; i++) 168 | f->upvalues[i].name = NULL; 169 | for (i = 0; i < n; i++) { 170 | f->upvalues[i].instack = LoadByte(S); 171 | f->upvalues[i].idx = LoadByte(S); 172 | } 173 | } 174 | 175 | 176 | static void LoadDebug (LoadState *S, Proto *f) { 177 | int i, n; 178 | n = LoadInt(S); 179 | f->lineinfo = luaM_newvector(S->L, n, int); 180 | f->sizelineinfo = n; 181 | LoadVector(S, f->lineinfo, n); 182 | n = LoadInt(S); 183 | f->locvars = luaM_newvector(S->L, n, LocVar); 184 | f->sizelocvars = n; 185 | for (i = 0; i < n; i++) 186 | f->locvars[i].varname = NULL; 187 | for (i = 0; i < n; i++) { 188 | f->locvars[i].varname = LoadString(S); 189 | f->locvars[i].startpc = LoadInt(S); 190 | f->locvars[i].endpc = LoadInt(S); 191 | } 192 | n = LoadInt(S); 193 | for (i = 0; i < n; i++) 194 | f->upvalues[i].name = LoadString(S); 195 | } 196 | 197 | 198 | static void LoadFunction (LoadState *S, Proto *f, TString *psource) { 199 | f->source = LoadString(S); 200 | if (f->source == NULL) /* no source in dump? */ 201 | f->source = psource; /* reuse parent's source */ 202 | f->linedefined = LoadInt(S); 203 | f->lastlinedefined = LoadInt(S); 204 | f->numparams = LoadByte(S); 205 | f->is_vararg = LoadByte(S); 206 | f->maxstacksize = LoadByte(S); 207 | LoadCode(S, f); 208 | LoadConstants(S, f); 209 | LoadUpvalues(S, f); 210 | LoadProtos(S, f); 211 | LoadDebug(S, f); 212 | } 213 | 214 | 215 | static void checkliteral (LoadState *S, const char *s, const char *msg) { 216 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ 217 | size_t len = strlen(s); 218 | LoadVector(S, buff, len); 219 | if (memcmp(s, buff, len) != 0) 220 | error(S, msg); 221 | } 222 | 223 | 224 | static void fchecksize (LoadState *S, size_t size, const char *tname) { 225 | if (LoadByte(S) != size) 226 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); 227 | } 228 | 229 | 230 | #define checksize(S,t) fchecksize(S,sizeof(t),#t) 231 | 232 | static void checkHeader (LoadState *S) { 233 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ 234 | if (LoadByte(S) != LUAC_VERSION) 235 | error(S, "version mismatch in"); 236 | if (LoadByte(S) != LUAC_FORMAT) 237 | error(S, "format mismatch in"); 238 | checkliteral(S, LUAC_DATA, "corrupted"); 239 | checksize(S, int); 240 | checksize(S, size_t); 241 | checksize(S, Instruction); 242 | checksize(S, lua_Integer); 243 | checksize(S, lua_Number); 244 | if (LoadInteger(S) != LUAC_INT) 245 | error(S, "endianness mismatch in"); 246 | if (LoadNumber(S) != LUAC_NUM) 247 | error(S, "float format mismatch in"); 248 | } 249 | 250 | 251 | /* 252 | ** load precompiled chunk 253 | */ 254 | LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, 255 | const char *name) { 256 | LoadState S; 257 | LClosure *cl; 258 | if (*name == '@' || *name == '=') 259 | S.name = name + 1; 260 | else if (*name == LUA_SIGNATURE[0]) 261 | S.name = "binary string"; 262 | else 263 | S.name = name; 264 | S.L = L; 265 | S.Z = Z; 266 | S.b = buff; 267 | checkHeader(&S); 268 | cl = luaF_newLclosure(L, LoadByte(&S)); 269 | setclLvalue(L, L->top, cl); 270 | incr_top(L); 271 | cl->p = luaF_newproto(L); 272 | LoadFunction(&S, cl->p, NULL); 273 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); 274 | luai_verifycode(L, buff, cl->p); 275 | return cl; 276 | } 277 | 278 | -------------------------------------------------------------------------------- /RayTracer/Source/Tracer.cpp: -------------------------------------------------------------------------------- 1 | #include "Inlines.h" 2 | #include "Shape.h" 3 | #include "Sphere.h" 4 | #include "TriangleMesh.h" 5 | #include 6 | #include "PathRenderer.h" 7 | #include "RayRenderer.h" 8 | #include 9 | #include "ObjLoader.h" 10 | #include "QualityDesc.h" 11 | #include "Scripting.h" 12 | 13 | 14 | void addTrisToScene(std::vector *scene, TriangleMesh* mesh) 15 | { 16 | for (int i=0;iNumTris;++i) 17 | { 18 | Primitive *Tri = new Triangle( 19 | mesh->ObjectToWorld, mesh->WorldToObject, 20 | mesh->Mat, mesh, i); 21 | scene->push_back(Tri); 22 | } 23 | } 24 | 25 | void addSphereTris(std::vector *scene, Material mat, float r, float x, float y, float z, float a=1.f, float b=1.f, float c=1.f) 26 | { 27 | Transform *T = new Transform((Translate(Vector(x, y, z)))(Scale(r,r,r))); 28 | Transform* InvT = new Transform(Inverse(*T)); 29 | 30 | ObjLoader loader; 31 | TriangleMesh * sp = new TriangleMesh(loader.Construct("sphere.obj", T, InvT, mat)); 32 | addTrisToScene(scene, sp); 33 | } 34 | 35 | void addSphere(std::vector *scene, Material mat, float r, float x, float y, float z, float a=1.f, float b=1.f, float c=1.f) 36 | { 37 | Transform *T = new Transform((Translate(Vector(x, y, z)))(Scale(a,b,c))); 38 | Transform* InvT = new Transform(Inverse(*T)); 39 | scene->push_back(new Sphere(T, InvT, mat, r)); 40 | } 41 | 42 | void addBoxTris(std::vector *scene, Material mat, float r, float x, float y, float z) 43 | { 44 | ObjLoader loader; 45 | Transform *T = new Transform((Translate(Vector(x, y, z)))(Scale(r,r,r))); 46 | Transform* InvT = new Transform(Inverse(*T)); 47 | TriangleMesh * box = new TriangleMesh(loader.Construct("box.obj", T, InvT, mat)); 48 | addTrisToScene(scene, box); 49 | } 50 | 51 | void addDrag(std::vector *scene, Material mat, Transform * T) 52 | { 53 | ObjLoader loader; 54 | Transform* InvT = new Transform(Inverse(*T)); 55 | TriangleMesh * drag = new TriangleMesh(loader.Construct("dragon.obj", T, InvT, mat)); 56 | addTrisToScene(scene, drag); 57 | } 58 | 59 | 60 | int main(int argc, char * argv[]) 61 | { 62 | std::string err; 63 | Renderer* renderer = init_renderer_from_script("..\\ray.lua", &err); 64 | if (renderer == NULL) 65 | { 66 | printf(err.c_str()); 67 | printf("\n"); 68 | } 69 | else 70 | { 71 | renderer->Render(); 72 | } 73 | 74 | return 0; 75 | } 76 | 77 | int stuff() 78 | { 79 | printf("\n\n"); 80 | std::vector *scene = new std::vector(); 81 | 82 | RGB white; white.red = 1.f; white.green = 1.f; white.blue = 1.f; 83 | RGB grey; grey.red = 0.6f; grey.green = 0.6f; grey.blue = 0.6f; 84 | 85 | float dominant = 0.85f; 86 | float sub = 0.4f; 87 | 88 | Material whiteWall; 89 | //whiteWall.Color = white; 90 | whiteWall.Color.red = dominant; whiteWall.Color.green = dominant; whiteWall.Color.blue = dominant; 91 | whiteWall.Specular = 0.0f; 92 | whiteWall.Diffuse = 1.f; 93 | whiteWall.GlossyReflective = 1.f; 94 | whiteWall.Reflective = 1.f; 95 | whiteWall.Refractive = 1.f; 96 | whiteWall.RefrAbsorbance = 1.f; 97 | 98 | Material greyWall; 99 | greyWall.Color = grey; 100 | //greyWall.Color.red = dominant; greyWall.Color.green = dominant; greyWall.Color.blue = dominant; 101 | greyWall.Specular = 0.0f; 102 | greyWall.Diffuse = 1.f; 103 | greyWall.GlossyReflective = 1.f; 104 | greyWall.Reflective = 1.f; 105 | greyWall.Refractive = 1.f; 106 | greyWall.RefrAbsorbance = 1.f; 107 | 108 | Material blueWall; 109 | blueWall.Color.red = sub; blueWall.Color.green = sub; blueWall.Color.blue = dominant; 110 | blueWall.Specular = 0.0f; 111 | blueWall.Diffuse = 1.f; 112 | blueWall.GlossyReflective = 1.f; 113 | blueWall.Reflective = 1.f; 114 | blueWall.Refractive = 1.f; 115 | blueWall.RefrAbsorbance = 1.f; 116 | 117 | Material redWall; 118 | redWall.Color.red = dominant; redWall.Color.green = sub; redWall.Color.blue = sub; 119 | redWall.Specular = 0.0f; 120 | redWall.Diffuse = 1.f; 121 | redWall.GlossyReflective = 1.f; 122 | redWall.Reflective = 1.f; 123 | redWall.Refractive = 1.f; 124 | redWall.RefrAbsorbance = 1.f; 125 | 126 | Material color2; 127 | color2.Color.red = 0.7f; color2.Color.green = 1.f; color2.Color.blue = 0.7f; 128 | color2.Specular = 1.0f; 129 | color2.Diffuse = 0.8f; 130 | color2.GlossyReflective = 0.0f; 131 | color2.Reflective = 1.f; 132 | color2.Refractive = 1.f; 133 | color2.RefrAbsorbance = 1.f; 134 | 135 | Material color3; 136 | color3.Color.red = 1.0f; color3.Color.green = 1.0f; color3.Color.blue = 1.0f; 137 | color3.Specular = 1.0f; 138 | color3.Diffuse = 0.1f; 139 | color3.GlossyReflective = 0.f; 140 | color3.Reflective = 1.0f; 141 | color3.Refractive = 1.05f; 142 | color3.RefrAbsorbance = 0.15f; 143 | 144 | Material color4; 145 | color4.Color.red = 0.f; color4.Color.green = 0.f; color4.Color.blue = 0.0f; 146 | color4.Specular = 0.f; 147 | color4.Diffuse = 0.f; 148 | color4.GlossyReflective = 0.0f; 149 | color4.Reflective = 0.0f; 150 | color4.Refractive = 1.f; 151 | color4.RefrAbsorbance = 0.f; 152 | 153 | Material color6; 154 | color6.Color.red = sub; color6.Color.green = dominant; color6.Color.blue = sub; 155 | color6.Specular = 0.8f; 156 | color6.Diffuse = 1.f; 157 | color6.GlossyReflective = 0.f; 158 | color6.Reflective = 1.f; 159 | color6.Refractive = 1.f; 160 | color6.RefrAbsorbance = 1.f; 161 | 162 | Material color7; 163 | color7.Color.red = 1.0f; color7.Color.green = 1.f; color7.Color.blue = 0.0f; 164 | color7.Specular = .0f; 165 | color7.Diffuse = 1.f; 166 | color7.GlossyReflective = 0.f; 167 | color7.Reflective = 0.f; 168 | color7.Refractive = 1.f; 169 | color7.RefrAbsorbance = 1.f; 170 | 171 | Material dragonColor; 172 | dragonColor.Color.red = sub; dragonColor.Color.green = dominant; dragonColor.Color.blue = sub; 173 | dragonColor.Specular = 0.8f; 174 | dragonColor.Diffuse = 1.f; 175 | dragonColor.GlossyReflective = 1.f; 176 | dragonColor.Reflective = 1.f; 177 | dragonColor.Refractive = 1.f; 178 | dragonColor.RefrAbsorbance = 1.f; 179 | 180 | addSphere(scene, whiteWall, 160.f, 0.f, 163.f, 0.f);// Y = 6 units 181 | addSphere(scene, whiteWall, 160.f, 0.f, -163.f, 0.f);// Y = 6 units 182 | addSphere(scene, whiteWall, 160.f, 0.f, 0.f, -170.f);// Z = 13 units 183 | addSphere(scene, whiteWall, 160.f, 0.f, 0.f, 163.f); // X = 6 units 184 | addSphere(scene, blueWall, 160.f, -163.f, 0.f, 0.f); 185 | addSphere(scene, redWall, 160.f, 163.f, 0.f, 0.f); 186 | 187 | addSphere(scene, color6, 0.8f, 1.0f, -2.2f, -2.f); 188 | std::vector smallSphereData; 189 | if (1) 190 | for (int i=0;i<8;++i) 191 | { 192 | for (int j=0;j<8;++j) 193 | { 194 | //addSphere(scene, color7, 0.25f, (j-3.5f)*0.5f, (i-3.5f)*0.5f, 2.9f); 195 | } 196 | } 197 | 198 | Transform *drag = new Transform((Translate(Vector(0.f, -2.8f, 1.f))) 199 | (RotateY(-45)) 200 | (RotateX(-90)) 201 | (Scale(0.6f, 0.6f, 0.6f))); 202 | 203 | //addDrag(scene, dragonColor, drag); 204 | 205 | white *= 1.f; 206 | Material lightMat; lightMat.Color = white; 207 | lightMat.Emissive = 3.f; 208 | 209 | addSphere(scene, lightMat, 1.f, 0.f, 1.9f, 1.f); 210 | 211 | Transform camTrans = (Translate(Vector(0.f, -1.0f, -8.f))); 212 | 213 | QualityDesc quality; 214 | 215 | //RayTracer-only Qualities 216 | quality.LightSamples = 1; 217 | quality.GlossyReflectiveSamples = 1; 218 | quality.Depth = 1; 219 | 220 | quality.Samples = 1; 221 | quality.PathEnableDirectLighting = true; 222 | quality.PathEnableIndirectIllum = true; 223 | float dim = 512; 224 | Camera camera(camTrans, dim, dim, dim); 225 | Renderer* renderer = new PathRenderer(scene, camera, quality); 226 | renderer->Render(); 227 | 228 | delete scene; 229 | return 0; 230 | } 231 | --------------------------------------------------------------------------------