Browse code

Add material color

Robert Cranston authored on 26/10/2023 09:31:01
Showing 2 changed files

1 1
Binary files a/doc/output.tga and b/doc/output.tga differ
... ...
@@ -36,31 +36,39 @@ struct Ray
36 36
     vec3 direction;
37 37
 };
38 38
 
39
+//// Material
40
+struct Material
41
+{
42
+    vec3 color;
43
+};
44
+
39 45
 //// Trace
40 46
 struct Trace
41 47
 {
42
-    float distance;
48
+    float    distance;
49
+    Material material;
43 50
 };
44 51
 
45 52
 //// Circle
46 53
 struct Circle
47 54
 {
48
-    vec3  center;
49
-    float radius;
55
+    vec3     center;
56
+    float    radius;
57
+    Material material;
50 58
     Trace trace(Ray const & ray) const
51 59
     {
52 60
         auto const offs = center - ray.origin;
53 61
         auto const proj = dot(offs, ray.direction);
54 62
         if (proj < 0.0F) // Past.
55
-            return {infinity};
63
+            return {infinity, {}};
56 64
         auto const perp2 = dot(offs, offs) - proj * proj;
57 65
         auto const pene2 = radius * radius - perp2;
58 66
         if (pene2 < 0.0F) // Miss.
59
-            return {infinity};
67
+            return {infinity, {}};
60 68
         auto const dist = proj - sqrt(pene2);
61 69
         if (dist < 0.0F) // Inside.
62
-            return {infinity};
63
-        return {dist};
70
+            return {infinity, {}};
71
+        return {dist, material};
64 72
     }
65 73
 };
66 74
 
... ...
@@ -70,10 +78,11 @@ using Shapes = std::vector<Circle>;
70 78
 //// Scene
71 79
 struct Scene
72 80
 {
73
-    Shapes shapes;
81
+    Material background;
82
+    Shapes   shapes;
74 83
     Trace trace(Ray const & ray) const
75 84
     {
76
-        auto nearest = Trace{infinity};
85
+        auto nearest = Trace{infinity, background};
77 86
         for (auto const & shape : shapes)
78 87
         {
79 88
             auto const trace = shape.trace(ray);
... ...
@@ -181,12 +190,14 @@ int main(int argc, char const * argv[])
181 190
         100.0F,                  // far
182 191
     };
183 192
     auto const scene = Scene{
193
+        // background
194
+        {vec3(0.2F, 0.2F, 0.5F)},
184 195
         // shapes
185 196
         {
186
-            {vec3( 2.0F, -1.0F, -10.0F), 3.0F},
187
-            {vec3( 1.0F,  2.0F,  -7.0F), 2.0F},
188
-            {vec3(-1.0F,  0.0F,  -4.0F), 1.0F},
189
-            {vec3( 0.0F, -1.0F,  -3.0F), 0.2F},
197
+            {vec3( 2.0F, -1.0F, -10.0F), 3.0F, {vec3(1.0F, 0.0F, 0.0F)}},
198
+            {vec3( 1.0F,  2.0F,  -7.0F), 2.0F, {vec3(0.0F, 1.0F, 0.0F)}},
199
+            {vec3(-1.0F,  0.0F,  -4.0F), 1.0F, {vec3(0.0F, 0.0F, 1.0F)}},
200
+            {vec3( 0.0F, -1.0F,  -3.0F), 0.2F, {vec3(0.0F, 1.0F, 1.0F)}},
190 201
         },
191 202
     };
192 203
     auto const tonemap = ACES{vec3(1.0F)};
... ...
@@ -195,7 +206,7 @@ int main(int argc, char const * argv[])
195 206
     output.render([&](uvec2 const & size, uvec2 const & pixel) {
196 207
         auto const ray   = camera.ray(size, pixel);
197 208
         auto const trace = scene.trace(ray);
198
-        auto const rgb   = vec3(min(trace.distance / 100.0F, 100.0F));
209
+        auto const rgb   = trace.material.color;
199 210
         auto const srgb  = convertLinearToSRGB(tonemap.transform(rgb));
200 211
         auto const alpha = 1.0F;
201 212
         return vec4(srgb, alpha);