├── BasicTypes.h ├── BmpWriter.h ├── Buffer.h ├── Light.h ├── Main.cpp ├── Material.h ├── Op.h ├── Plane.h ├── README ├── Raytrace.py ├── Scene.h ├── Sphere.h ├── TestScene.h ├── Tile.h └── Util.h /BasicTypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include 24 | 25 | /////////////////////////////////////////////////////////////////////////////// 26 | struct Null {}; 27 | 28 | static const int64_t fixed_shift = (1 << 16); 29 | static const int64_t fixed_root = (1 << 8); 30 | 31 | template 32 | struct Fixed 33 | { 34 | static const int64_t Value = VAL; 35 | 36 | static float ToFloat() 37 | { 38 | return Value / (float)fixed_shift; 39 | } 40 | }; 41 | 42 | typedef Fixed One; 43 | typedef Fixed<0> Zero; 44 | typedef Fixed<0x7fffffffffffffffLL> MaxFixed; 45 | typedef Fixed<0x8000000000000000LL> MinFixed; 46 | /////////////////////////////////////////////////////////////////////////////// 47 | template 48 | struct Vec3 49 | { 50 | typedef X_ X; 51 | typedef Y_ Y; 52 | typedef Z_ Z; 53 | }; 54 | 55 | typedef Vec3, Fixed<0>, Fixed<0> > Vec3Zero; 56 | /////////////////////////////////////////////////////////////////////////////// 57 | template struct Ray; 58 | 59 | template 61 | struct Ray, Vec3 > 62 | { 63 | typedef Vec3 Origin; 64 | typedef Vec3 Dir; 65 | }; 66 | /////////////////////////////////////////////////////////////////////////////// 67 | enum HitType 68 | { 69 | Missed, 70 | Hit 71 | }; 72 | 73 | template 74 | struct HitResult : public DIST 75 | { 76 | static const HitType Type = TYPE; 77 | }; 78 | /////////////////////////////////////////////////////////////////////////////// 79 | -------------------------------------------------------------------------------- /BmpWriter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include 24 | #include 25 | 26 | struct BMPHEAD 27 | { 28 | int filesize; 29 | short reserved[2]; 30 | int headersize; 31 | int infoSize; 32 | int width; 33 | int height; 34 | short biPlanes; 35 | short bits; 36 | int biCompression; 37 | int biSizeImage; 38 | int biXPelsPerMeter; 39 | int biYPelsPerMeter; 40 | int biClrUsed; 41 | int biClrImportant; 42 | }; 43 | 44 | void WriteBitmap(const char * filename, int width, int height, void * pbuffer) 45 | { 46 | unsigned char * buf = (unsigned char *) pbuffer; 47 | int pitch = ((width * 3) + 3) & (-4); 48 | 49 | BMPHEAD bh; 50 | memset((char *)&bh,0,sizeof(BMPHEAD)); 51 | bh.headersize = 54L; 52 | bh.infoSize = 0x28L; 53 | bh.width = width; 54 | bh.height = height; 55 | bh.biPlanes = 1; 56 | bh.bits = 24; 57 | bh.biCompression = 0L; 58 | bh.biSizeImage = 54 + (pitch * height); 59 | int pad = pitch - (width * 3); 60 | unsigned char padbuf[] = {0,0,0}; 61 | 62 | FILE * file = fopen(filename, "wb"); 63 | fwrite("BM", 1, 2, file); 64 | fwrite(&bh, 1, sizeof(bh), file); 65 | for (int i=(height-1); i>=0; i--) 66 | { 67 | fwrite(buf + (i * width * 3), 1, width * 3, file); 68 | if (pad > 0) 69 | fwrite(padbuf,1,pad,file); 70 | } 71 | fclose(file); 72 | } 73 | -------------------------------------------------------------------------------- /Buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | 24 | #include "Tile.h" 25 | 26 | // General Case 27 | template class Function, int tilew=16, int tileh=16> 29 | struct Buffer 30 | { 31 | typedef Tile Data; 32 | typedef Buffer Next; 33 | 34 | template 35 | static void Visit(Visitor v) 36 | { 37 | Data::Visit(v); 38 | Next::Visit(v); 39 | } 40 | }; 41 | 42 | // First column 43 | template class Function, int tilew, int tileh> 45 | struct Buffer<0,y,width,height,Function,tilew,tileh> 46 | { 47 | typedef Tile<0,y,tilew,tileh,Function> Data; 48 | typedef Buffer Next; 49 | typedef Buffer<0,y+tileh,width,height,Function,tilew,tileh> NextRow; 50 | 51 | template 52 | static void Visit(Visitor v) 53 | { 54 | Data::Visit(v); 55 | Next::Visit(v); 56 | NextRow::Visit(v); 57 | } 58 | }; 59 | 60 | // Terminal 61 | template class Function, int tilew, int tileh> 63 | struct Buffer<0,height,width,height,Function,tilew,tileh> 64 | { 65 | template 66 | static void Visit(Visitor v) {} 67 | }; 68 | 69 | template class Function, int tilew, int tileh> 71 | struct Buffer 72 | { 73 | template 74 | static void Visit(Visitor v) {} 75 | }; 76 | -------------------------------------------------------------------------------- /Light.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | 24 | template 25 | struct PointLight 26 | { 27 | typedef Position_ Position; 28 | typedef Color_ Color; 29 | typedef Next_ Next; 30 | 31 | template 32 | struct CalcLight 33 | { 34 | // Light distance 35 | typedef typename Length::Result>::Result Dist; 37 | 38 | // Pixel -> Light direction 39 | typedef typename Div::Result, Dist>::Result Dir; 41 | 42 | // Shadow test 43 | typedef typename Scene::template IntersectScene >::Result ShadowHit; 45 | typedef typename If::Result shadow; 46 | 47 | // NDL 48 | typedef typename Max::Result, Zero>::Result NDL; 49 | 50 | // attenuation 1 / D^2 51 | typedef typename Div, 52 | typename Mul::Result>::Result>::Result atten; 53 | 54 | // Combine 55 | typedef typename Mul::Result diff0; 56 | typedef typename Mul::Result diff1; 57 | typedef typename Mul::Result Result; 58 | }; 59 | }; 60 | 61 | template 62 | struct DirectionalLight 63 | { 64 | typedef _Next Next; 65 | 66 | typedef typename Sub::Result NegDir; 67 | 68 | template 69 | struct CalcLight 70 | { 71 | // NDL 72 | typedef typename Max::Result>::Result NDL; 73 | 74 | // Shadowing 75 | typedef typename Scene::template IntersectScene >::Result ShadowHit; 77 | typedef typename If::Result shadow; 78 | 79 | // Combine 80 | typedef typename Mul::Result>::Result Result; 81 | }; 82 | }; 83 | 84 | template 85 | struct AmbientLight 86 | { 87 | typedef _Next Next; 88 | 89 | template 90 | struct CalcLight 91 | { 92 | typedef Color Result; 93 | }; 94 | }; 95 | 96 | template 97 | struct SumLighting 98 | { 99 | typedef typename Add::Result>::Result Current; 100 | typedef typename SumLighting::Result Result; 101 | }; 102 | 103 | template 104 | struct SumLighting 105 | { 106 | typedef Total Result; 107 | }; 108 | -------------------------------------------------------------------------------- /Main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #include 24 | #include 25 | #include "BmpWriter.h" 26 | #include "TestScene.h" 27 | #include "Tile.h" 28 | #include "Buffer.h" 29 | 30 | #ifndef OUTW 31 | #define OUTW 4 32 | #define OUTH 4 33 | #define UX 0 34 | #define UY 0 35 | #define TILEW 2 36 | #define WRITE_BMP 37 | #endif 38 | 39 | struct BGR 40 | { 41 | unsigned char b; 42 | unsigned char g; 43 | unsigned char r; 44 | }; 45 | 46 | struct PixelWriter 47 | { 48 | BGR * buffer; 49 | 50 | template 51 | void Call(int _x, int _y) 52 | { 53 | int x = _x - UX; 54 | int y = _y - UY; 55 | buffer[(y * TILEW) + x].r = (unsigned char)(255 * std::min(RES::X::ToFloat(),1.f)); 56 | buffer[(y * TILEW) + x].g = (unsigned char)(255 * std::min(RES::Y::ToFloat(),1.f)); 57 | buffer[(y * TILEW) + x].b = (unsigned char)(255 * std::min(RES::Z::ToFloat(),1.f)); 58 | } 59 | }; 60 | 61 | int main() 62 | { 63 | #if defined(WRITE_BMP) 64 | BGR buf[OUTW*OUTH]; 65 | #else 66 | BGR buf[TILEW]; 67 | #endif 68 | 69 | memset(buf, 0, sizeof(buf)); 70 | 71 | PixelWriter pix; 72 | pix.buffer = &buf[0]; 73 | 74 | #if defined(WRITE_BMP) 75 | Buffer<0,0,OUTW,OUTH,SceneTracer::Tracer, 76 | TILEW,1>::Visit(pix); 77 | WriteBitmap("output.bmp", OUTW, OUTH, &buf[0]); 78 | #else 79 | Tile::Tracer>::Visit(pix); 80 | fwrite(&buf[0], 1, sizeof(buf), stdout); 81 | #endif 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Material.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include "Light.h" 24 | 25 | // Material evaluation 26 | template 27 | struct EvalMaterial 28 | { 29 | typedef typename Material::template Evaluate::Result Result; 31 | }; 32 | 33 | // Terminal cases 34 | template 35 | struct EvalMaterial 36 | { 37 | typedef Vec3Zero Result; 38 | }; 39 | 40 | template 41 | struct EvalMaterial 42 | { 43 | typedef Vec3Zero Result; 44 | }; 45 | 46 | // A simple lit material 47 | template 48 | struct FlatColor 49 | { 50 | typedef Vec3 Color; 51 | 52 | template 53 | struct Evaluate 54 | { 55 | typedef typename SumLighting::Result Lighting; 56 | typedef typename Mul::Result Result; 57 | }; 58 | }; 59 | 60 | // A simple reflective material 61 | template 62 | struct Reflective 63 | { 64 | typedef Vec3 Color; 65 | 66 | template 67 | struct Evaluate 68 | { 69 | // Calculate reflection ray 70 | typedef typename Dot::Result VDN; 71 | typedef typename Sub, VDN>::Result>::Result>::Result ReflDir; 73 | typedef Ray ReflRay; 74 | 75 | // Get reflection hit and shading 76 | typedef typename Scene::template IntersectScene::Result ReflectionHit; 77 | static const bool is_hit = ReflectionHit::One::Type == Hit; 78 | typedef typename EvalMaterial::Result ReflectionColor; 80 | 81 | // Sum lighting 82 | typedef typename SumLighting::Result Lighting; 83 | 84 | // Add reflection and lighting * color 85 | typedef typename Add::Result, 86 | typename Mul::Result>::Result Result; 87 | }; 88 | }; 89 | -------------------------------------------------------------------------------- /Op.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include "BasicTypes.h" 24 | 25 | /////////////////////////////////////////////////////////////////////////////// 26 | template 27 | struct Add 28 | { 29 | typedef Fixed Result; 30 | }; 31 | 32 | template 33 | struct Add, Vec3 > 34 | { 35 | typedef Vec3::Result, typename Add::Result, 36 | typename Add::Result> Result; 37 | }; 38 | /////////////////////////////////////////////////////////////////////////////// 39 | template 40 | struct Sub 41 | { 42 | typedef Fixed Result; 43 | }; 44 | 45 | template 46 | struct Sub, Vec3 > 47 | { 48 | typedef Vec3::Result, typename Sub::Result, 49 | typename Sub::Result> Result; 50 | }; 51 | /////////////////////////////////////////////////////////////////////////////// 52 | template 53 | struct Mul 54 | { 55 | typedef Fixed<(L::Value * R::Value) / fixed_shift> Result; 56 | }; 57 | 58 | template 60 | struct Mul, Vec3 > 61 | { 62 | typedef Vec3::Result, typename Mul::Result, 63 | typename Mul::Result> Result; 64 | }; 65 | 66 | template 67 | struct Mul, Scalar> 68 | { 69 | typedef Vec3::Result, typename Mul::Result, 70 | typename Mul::Result> Result; 71 | }; 72 | 73 | template 74 | struct Mul > 75 | { 76 | typedef Vec3::Result, typename Mul::Result, 77 | typename Mul::Result> Result; 78 | }; 79 | /////////////////////////////////////////////////////////////////////////////// 80 | template 81 | struct Div 82 | { 83 | typedef Fixed<(L::Value * fixed_shift) / R::Value> Result; 84 | }; 85 | 86 | template 88 | struct Div, Vec3 > 89 | { 90 | typedef Vec3::Result, typename Div::Result, 91 | typename Div::Result> Result; 92 | }; 93 | 94 | template 95 | struct Div, Scalar> 96 | { 97 | typedef Vec3::Result, typename Div::Result, 98 | typename Div::Result> Result; 99 | }; 100 | /////////////////////////////////////////////////////////////////////////////// 101 | template struct Dot; 102 | 103 | template 105 | struct Dot, Vec3 > 106 | { 107 | typedef typename Add::Result, typename Mul::Result>::Result, 108 | typename Mul::Result>::Result Result; 109 | }; 110 | /////////////////////////////////////////////////////////////////////////////// 111 | template 112 | struct ISqrt; 113 | 114 | template 115 | struct ISqrt 116 | { 117 | static const int64_t Result = R; 118 | }; 119 | 120 | template 121 | struct ISqrt<0,L,U> 122 | { 123 | static const int64_t Result = 0; 124 | }; 125 | 126 | template 127 | struct ISqrt 128 | { 129 | static const int64_t t0 = (L + U) / 2; 130 | static const int64_t t1 = t0 * t0; 131 | static const bool CND = t1 >= V; 132 | static const int64_t Result = ISqrt::Result; 134 | }; 135 | 136 | template 137 | struct SqrtImpl 138 | { 139 | static const bool CND = val < One::Value; 140 | static const int64_t UPPER = CND ? (val * fixed_root) : ((val/fixed_root)+1); 141 | typedef Fixed::Result * fixed_root> Result; 142 | }; 143 | 144 | template 145 | struct Sqrt 146 | { 147 | typedef typename SqrtImpl::Result Result; 148 | }; 149 | /////////////////////////////////////////////////////////////////////////////// 150 | template 151 | struct Length 152 | { 153 | typedef typename Sqrt::Result>::Result Result; 154 | }; 155 | /////////////////////////////////////////////////////////////////////////////// 156 | template struct Normalize 157 | { 158 | typedef typename Div::Result>::Result Result; 159 | }; 160 | /////////////////////////////////////////////////////////////////////////////// 161 | template struct Max; 162 | 163 | template 164 | struct Max, Fixed > 165 | { 166 | static const int64_t val = (a >= b) ? a : b; 167 | typedef Fixed Result; 168 | }; 169 | /////////////////////////////////////////////////////////////////////////////// 170 | -------------------------------------------------------------------------------- /Plane.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include "BasicTypes.h" 24 | #include "Op.h" 25 | #include "Util.h" 26 | 27 | /////////////////////////////////////////////////////////////////////////////// 28 | template 29 | struct Plane 30 | { 31 | typedef N Normal; 32 | typedef D Distance; 33 | }; 34 | /////////////////////////////////////////////////////////////////////////////// 35 | template 36 | struct CalcPlaneHit 37 | { 38 | typedef HitResult Result; 39 | }; 40 | 41 | template 42 | struct CalcPlaneHit 43 | { 44 | typedef typename Div::Result HitDist; 45 | static const bool CND = (HitDist::Value < 0); 46 | typedef If, 47 | HitResult > Result; 48 | }; 49 | 50 | template 51 | struct IntersectPlane 52 | { 53 | typedef typename Dot::Result DN; 54 | typedef typename Dot::Result ON; 55 | typedef typename Sub, typename Add::Result>::Result NUMER; 56 | 57 | static const bool CND = DN::Value < 0; 58 | 59 | typedef typename CalcPlaneHit::Result intersection; 60 | }; 61 | 62 | template struct Intersect; 63 | 64 | template 65 | struct Intersect, Plane > : 66 | public IntersectPlane::intersection 67 | { 68 | private: 69 | typedef typename IntersectPlane::intersection Base; 70 | 71 | public: 72 | typedef typename CalcPosition::Result Position; 73 | typedef NORM Normal; 74 | typedef DIR View; 75 | }; 76 | /////////////////////////////////////////////////////////////////////////////// 77 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ================================================ 2 | TemplateTrace - Description 3 | ================================================ 4 | TemplateTrace is a C++ compile-time raytracer written for the fun of it. 5 | Run Raytrace.py specifying --width, --height and --output to generate an image. 6 | The script will invoke gcc on Main.cpp to generate two program instances at 7 | a time and with each program instance covering a 4x1 portion of the final 8 | generated image. As you can imagine this can be a very lengthy process... 9 | Modify TestScene.h if you want to modify the scene being traced. 10 | 11 | 12 | 13 | ================================================= 14 | TemplateTrace - License Info 15 | ================================================= 16 | Copyright (c) 2011 Chris Lentini 17 | http://divergentcoder.com 18 | 19 | Permission is hereby granted, free of charge, to any person obtaining a copy of 20 | this software and associated documentation files (the "Software"), to deal in 21 | the Software without restriction, including without limitation the rights to use, 22 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 23 | Software, and to permit persons to whom the Software is furnished to do so, 24 | subject to the following conditions: 25 | 26 | The above copyright notice and this permission notice shall be included in all 27 | copies or substantial portions of the Software. 28 | 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 31 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 32 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 33 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 34 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 | -------------------------------------------------------------------------------- /Raytrace.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2011 Chris Lentini 2 | # http://divergentcoder.com 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | # this software and associated documentation files (the "Software"), to deal in 6 | # the Software without restriction, including without limitation the rights to use, 7 | # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 8 | # Software, and to permit persons to whom the Software is furnished to do so, 9 | # subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in all 12 | # copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | 21 | from subprocess import Popen, PIPE 22 | from sys import stdout 23 | import struct 24 | from optparse import OptionParser 25 | from datetime import datetime 26 | 27 | ################################################################################ 28 | 29 | cmdline = OptionParser() 30 | cmdline.add_option("--output", help="Specifies bitmap destination file", 31 | action="store", dest="filename", default="output.bmp") 32 | cmdline.add_option("--width", help="Specifies output image width", 33 | action="store", dest="width", default=64) 34 | cmdline.add_option("--height", help="Specifies output image height", 35 | action="store", dest="height", default=64) 36 | cmdline.add_option("--tilew", help="Specifies the tiling width", 37 | action="store", dest="tilew", default=4) 38 | 39 | (options, args) = cmdline.parse_args() 40 | 41 | width = int(options.width) 42 | height = int(options.height) 43 | tilew = int(options.tilew) 44 | 45 | ################################################################################ 46 | 47 | def write_header(file): 48 | global width, height 49 | l0 = struct.pack(' 30 | struct SceneNode 31 | { 32 | template 33 | struct IntersectScene 34 | { 35 | typedef Pair, Material> res0; 36 | typedef typename Next::template IntersectScene::Result res1; 37 | 38 | static const bool is_less = res0::One::Value < res1::One::Value; 39 | 40 | typedef typename If, 42 | If >::Result Result; 43 | }; 44 | }; 45 | /////////////////////////////////////////////////////////////////////////////// 46 | template 47 | struct SceneNode 48 | { 49 | template 50 | struct IntersectScene 51 | { 52 | typedef Intersect intr0; 53 | typedef Pair Result; 54 | }; 55 | }; 56 | /////////////////////////////////////////////////////////////////////////////// 57 | template 58 | struct SceneTracer 59 | { 60 | private: 61 | static const int CX = Width/2; 62 | static const int CY = Height/2; 63 | 64 | public: 65 | template 66 | struct Tracer 67 | { 68 | typedef typename Normalize, 69 | ToFixed, 70 | One>, 71 | Vec3, 72 | ToFixed, 73 | One> >::Result>::Result Dir; 74 | 75 | typedef typename Scene::template IntersectScene >::Result Intersection; 76 | 77 | static const bool is_hit = Intersection::One::Type == Hit; 78 | typedef typename EvalMaterial::Result Result; 80 | }; 81 | }; 82 | /////////////////////////////////////////////////////////////////////////////// 83 | -------------------------------------------------------------------------------- /Sphere.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include "BasicTypes.h" 24 | #include "Op.h" 25 | #include "Util.h" 26 | 27 | /////////////////////////////////////////////////////////////////////////////// 28 | template struct Sphere 29 | { 30 | typedef P Position; 31 | typedef R Radius; 32 | }; 33 | /////////////////////////////////////////////////////////////////////////////// 34 | template 35 | struct CalcSphereHit 36 | { 37 | private: 38 | typedef typename Sqrt::Result Root; 39 | typedef typename Sub::Result I0; 40 | typedef typename Add::Result I1; 41 | 42 | static const bool CND0 = I1::Value > 0; 43 | static const bool CND1 = I0::Value < 0; 44 | 45 | public: 46 | typedef If, HitResult >, 47 | HitResult > Result; 48 | }; 49 | 50 | template 51 | struct SphereHit : public HitResult {}; 52 | 53 | template 54 | struct SphereHit : public CalcSphereHit::Result {}; 55 | 56 | template 57 | struct IntersectSphere 58 | { 59 | typedef typename Sub::Result V; 60 | typedef typename Dot::Result B; 61 | typedef typename Mul::Result R2; 62 | typedef typename Dot::Result V2; 63 | typedef typename Mul::Result B2; 64 | 65 | typedef typename Add::Result>::Result D2; 66 | typedef SphereHit<(D2::Value > 0), D2, B> intersection; 67 | }; 68 | 69 | template 70 | struct SphereNormal 71 | { 72 | typedef typename Normalize::Result>::Result Result; 73 | }; 74 | 75 | template 76 | struct SphereNormal 77 | { 78 | typedef Vec3 Result; 79 | }; 80 | 81 | template struct Intersect; 82 | 83 | template 84 | struct Intersect, Sphere > : 85 | public IntersectSphere::intersection 86 | { 87 | private: 88 | typedef typename IntersectSphere::intersection Base; 89 | public: 90 | typedef typename CalcPosition::Result Position; 91 | typedef typename SphereNormal::Result Normal; 92 | typedef DIR View; 93 | }; 94 | /////////////////////////////////////////////////////////////////////////////// 95 | -------------------------------------------------------------------------------- /TestScene.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | #pragma once 24 | #include "Scene.h" 25 | #include "Light.h" 26 | #include "Material.h" 27 | 28 | // Materials 29 | typedef FlatColor Red; 30 | typedef FlatColor Green; 31 | typedef Reflective > Blue; 32 | typedef Reflective > White; 33 | 34 | // Scene primitives 35 | typedef SceneNode >, 36 | ToFixed<1,0,1> >, Red, Null> node0; 37 | 38 | typedef SceneNode,Zero,ToFixed<2,0,1> >, 39 | ToFixed<1,1,2> >, Green, node0> node1; 40 | 41 | typedef SceneNode,ToFixed<2,0,1>,ToFixed<3,0,1> >, 42 | ToFixed<1,1,2> >, Blue, node1> node2; 43 | 44 | typedef SceneNode, One>, White, node2> TestScene; 45 | 46 | // Lights 47 | typedef PointLight, ToFixed<3,0,1> >, 48 | Vec3, ToFixed<1,2,3>, One>, Null> light0; 49 | 50 | typedef DirectionalLight,Fixed<-65536>,Fixed<-65536> > >::Result, 51 | Vec3, ToFixed<0,1,2>, ToFixed<0,4,5> >, light0> light1; 52 | 53 | typedef AmbientLight, ToFixed<0,1,8>, ToFixed<0,1,8> >, light1> TestLights; 54 | -------------------------------------------------------------------------------- /Tile.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | 24 | // General Case 25 | template class Function, int x=ux, int y=uy> 27 | struct Tile 28 | { 29 | typedef typename Function::Result Result; 30 | typedef Tile Next; 31 | 32 | template 33 | static void Visit(Visitor v) 34 | { 35 | v.template Call(x,y); 36 | Next::Visit(v); 37 | } 38 | }; 39 | 40 | // First column 41 | template class Function, int y> 43 | struct Tile 44 | { 45 | typedef typename Function::Result Result; 46 | 47 | typedef Tile Next; 48 | typedef Tile NextRow; 49 | 50 | template 51 | static void Visit(Visitor v) 52 | { 53 | v.template Call(ux,y); 54 | Next::Visit(v); 55 | NextRow::Visit(v); 56 | } 57 | }; 58 | 59 | // Terminators 60 | template class Function, int x, int y> 62 | struct Tile 63 | { 64 | template 65 | static void Visit(Visitor v) {} 66 | }; 67 | 68 | template class Function, int y> 70 | struct Tile 71 | { 72 | template 73 | static void Visit(Visitor v) {} 74 | }; 75 | -------------------------------------------------------------------------------- /Util.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2011 Chris Lentini 3 | http://divergentcoder.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 9 | Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | #pragma once 23 | #include "BasicTypes.h" 24 | #include "Op.h" 25 | 26 | /////////////////////////////////////////////////////////////////////////////// 27 | template 28 | struct ToFixed : public Fixed<(ipart * fixed_shift) + ((numer * fixed_shift) / denom)> 29 | {}; 30 | /////////////////////////////////////////////////////////////////////////////// 31 | template 32 | struct If : public Then 33 | { 34 | typedef Then Result; 35 | }; 36 | 37 | template 38 | struct If : public Else 39 | { 40 | typedef Else Result; 41 | }; 42 | /////////////////////////////////////////////////////////////////////////////// 43 | template 44 | struct Pair 45 | { 46 | typedef A One; 47 | typedef B Two; 48 | }; 49 | /////////////////////////////////////////////////////////////////////////////// 50 | template 51 | struct CalcPosition 52 | { 53 | typedef typename Add::Result>::Result Result; 54 | }; 55 | 56 | template 57 | struct CalcPosition 58 | { 59 | typedef Vec3Zero Result; 60 | }; 61 | /////////////////////////////////////////////////////////////////////////////// 62 | --------------------------------------------------------------------------------