1
2
3
4
5
6
7
8
9
10
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
63
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
From 898af59f7be5a9d0677c00663393f005842e29c0 Mon Sep 17 00:00:00 2001
From: Syndamia <kamen@syndamia.com>
Date: Tue, 16 Apr 2024 19:12:16 +0300
Subject: [PATCH] [h5] Added OrenNayar shader
---
src/main.cpp | 85 ++++++++++++++++++++++++++-----------------------
src/shading.cpp | 46 ++++++++++++++++++++++++++
src/shading.h | 19 +++++++++++
src/vector.h | 5 +++
4 files changed, 116 insertions(+), 39 deletions(-)
diff --git a/src/main.cpp b/src/main.cpp
index ffb9478..8656207 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -37,7 +37,6 @@
#include "shading.h"
Color vfb[VFB_MAX_SIZE][VFB_MAX_SIZE];
-Camera camera;
Color backgroundColor(0, 0, 0);
struct Node {
@@ -81,38 +80,46 @@ Geometry* createRoundedEdgesCube(Vector center, double side, double cornerRadius
Vector csgCenter(-50, 20, 15);
Cube cube(csgCenter, 40);
Sphere sphere(csgCenter, 26);
-void setupScene()
+void setupScene(void)
{
- camera.pos.set(0, 60, -120);
- camera.pitch = -10;
+ camera.yaw = 0;
+ camera.pitch = -30;
+ camera.roll = 0;
+ camera.fov = 90;
+ camera.aspectRatio = 4. / 3.0;
+ camera.pos = Vector(0, 165, 0);
+
camera.beginFrame();
- sphere.uvscaling = 50;
- // Create the floor:
- Texture* floor = new BitmapTexture("data/floor.bmp");
- nodes.push_back(Node{ new Plane(0), new Lambert(Color(0, 0, 0.8), floor)});
- // Create a globe with the world map:
- Phong* globeMat = new Phong;
- globeMat->specularExponent = 120.0f;
- globeMat->diffuseTex = new BitmapTexture("data/world.bmp", 1);
- nodes.push_back(Node{ new Sphere(Vector(50, 30, 15), 30), globeMat});
- // Create a CSG object: a cube with a cut out sphere in the middle
- Texture* checker = new CheckerTexture(Color(0x8d3d3d), Color(0x9c9c9c), 5);
- Phong* phong = new Phong(Color(0.6, 0.4, 0.1), Color(1, 1, 1), 120.0f, checker);
- nodes.push_back(Node{ new CSGDiff(&cube, &sphere), phong});
- // Create a complex CSG object: cube with rounded edges:
- nodes.push_back(Node{ createRoundedEdgesCube(Vector(0, 20, 15), 40, 10),
- new Phong(Color(0.9, 0.4, 0.1), Color(1, 1, 1), 125.0f)});
- /*
- nodes.push_back(Node{ createRoundedEdgesCube(Vector(0, 10, -15), 20, 8),
- new Phong(Color(0.1, 0.4, 0.9), Color(1, 1, 1), 125.0f)});
- nodes.push_back(Node{ createRoundedEdgesCube(Vector(0, 40, 100), 80, 10),
- new Phong(Color(0.4, 0.9, 0.1), Color(1, 1, 1), 125.0f)});
- */
- lightPos.set(+30, +100, -70);
- lightColor.setColor(1, 1, 1);
- lightIntensity = 10000.0;
-}
+ lightPos = Vector(-90, 700, 350);
+ lightColor = Color(1, 1, 1);
+ lightIntensity = 800000;
+
+ Plane* plane = new Plane(-0.01);
+ std::vector<Geometry*> geometries;
+ geometries.push_back(plane);
+
+ Texture* texture = new BitmapTexture("data/floor.bmp", 200);
+ Lambert* lambert = new Lambert(Color(1, 1, 1), texture);
+ Node floor{plane, lambert};
+ nodes.push_back(floor);
+
+ Shader* shaders[7];
+ Color diffuseR(0.6, 0.4, 0.4);
+ Color diffuseG(0.4, 0.6, 0.4);
+ shaders[0] = new Lambert(diffuseR);
+ shaders[1] = new Phong(diffuseR, Color(1, 1, 1), 80);
+ for (int i = 2; i < 7; i++) {
+ shaders[i] = new OrenNayar(diffuseG, (i - 2) / 5.0);
+ }
+
+ for (int i = 0; i < 2; i++) {
+ nodes.push_back(Node{new Sphere(Vector(-30 + 60*i, 15, 200), 22), shaders[i]});
+ }
+ for (int i = 2; i < 7; i++) {
+ nodes.push_back(Node{new Sphere(Vector(-120 + 60*(i-2), 15, 140), 22), shaders[i]});
+ }
+}
Color raytrace(Ray ray)
{
IntersectionInfo closestIntersection;
@@ -180,18 +187,18 @@ int main(int argc, char** argv)
setupScene();
Uint32 start = SDL_GetTicks();
bool shouldExit = false;
- for (double angle = 0; angle < 360 && !shouldExit; angle += 30) {
- double a_rad = toRadians(angle);
- camera.pos = Vector(sin(a_rad) * 120, 60, -cos(a_rad) * 120);
- camera.yaw = angle;
+ // for (double angle = 0; angle < 360 && !shouldExit; angle += 30) {
+ // double a_rad = toRadians(angle);
+ // camera.pos = Vector(sin(a_rad) * 120, 60, -cos(a_rad) * 120);
+ // camera.yaw = angle;
camera.beginFrame();
render();
displayVFB(vfb);
- if (checkForUserExit()) {
- printf("Exited early.\n");
- shouldExit = true;
- }
- }
+ // if (checkForUserExit()) {
+ // printf("Exited early.\n");
+ // shouldExit = true;
+ // }
+ // }
if (!shouldExit) {
Uint32 end = SDL_GetTicks();
printf("Elapsed time: %.2f seconds.\n", (end - start) / 1000.0);
diff --git a/src/shading.cpp b/src/shading.cpp
index 72dbdf2..be5c02b 100644
--- a/src/shading.cpp
+++ b/src/shading.cpp
@@ -29,6 +29,7 @@
Vector lightPos;
Color lightColor;
float lightIntensity;
+Camera camera;
const Color AMBIENT_LIGHT(0.15, 0.15, 0.15);
Color ConstantShader::computeColor(Ray ray, const IntersectionInfo& info)
@@ -69,6 +70,51 @@ Color Lambert::computeColor(Ray ray, const IntersectionInfo& info)
return direct + ambient;
}
+Vector project(const Vector& a, const Vector& norm) {
+ return a - dot(a, norm) * norm;
+}
+
+Color OrenNayar::computeColor(Ray ray, const IntersectionInfo& info) {
+ Color ambient = diffuse * AMBIENT_LIGHT;
+ if (!visible(lightPos, info.ip)) {
+ return ambient;
+ }
+
+ Vector Vi = lightPos - info.ip;
+ Vector Vr = camera.pos - info.ip;
+ Color Li = lightColor * lightIntensity / Vi.lengthSqr();
+ const double albedo = 1.0;
+
+ double thetai = angle(info.norm, Vi);
+ double thetar = angle(info.norm, Vr);
+ double alpha = std::max(thetai, thetar);
+ double beta = std::min(thetai, thetar);
+
+ double cosPhi = dot(normalize(project(Vi, info.norm)),
+ normalize(project(Vr, info.norm))); // cos(phii - phir)
+
+ double betaTerm = 2 * beta / M_PI;
+
+ // C1 is in class
+ double C2 = 0.45 * (sigma2 / (sigma2 + 0.09));
+ if (cosPhi >= 0) C2 *= sin(alpha);
+ else C2 *= sin(alpha) - betaTerm * betaTerm * betaTerm;
+ double C3 = 0.125 * (sigma2 / (sigma2 + 0.09))
+ * sqr((4 * alpha * beta) / sqr(M_PI));
+
+ Color L1 = albedo * Li * cos(thetai)
+ * ( C1
+ + C2 * cosPhi * tan(beta)
+ + C3 * (1 - std::abs(cosPhi)) * tan((alpha + beta) / 2));
+
+ Color L2 = 0.17 * albedo * Li * cos(thetai)
+ * (sigma2 / (sigma2 + 0.13))
+ * (1 - cosPhi * sqr(betaTerm));
+
+ Color Lr = L1 + L2;
+ return diffuse * Lr + ambient;
+}
+
Phong::Phong(Color color, Color spec, float specularExponent, Texture* diffuseTex):
diffuse(color), specular(spec), specularExponent(specularExponent), diffuseTex(diffuseTex) {}
diff --git a/src/shading.h b/src/shading.h
index 2bd1ef2..0815359 100644
--- a/src/shading.h
+++ b/src/shading.h
@@ -28,10 +28,12 @@
#include "vector.h"
#include "geometry.h"
#include "bitmap.h"
+#include "camera.h"
extern Vector lightPos;
extern Color lightColor;
extern float lightIntensity;
+extern Camera camera;
class Shader {
@@ -79,6 +81,23 @@ public:
virtual Color computeColor(Ray ray, const IntersectionInfo& info) override;
};
+class OrenNayar : public Shader {
+ float sigma2;
+ float C1;
+
+public:
+ Color diffuse;
+ float sigma;
+ OrenNayar(
+ Color color = Color(0.5, 0.5, 0.5),
+ float sigma = 0.0
+ ): diffuse(color), sigma(sigma) {
+ sigma2 = sigma * sigma;
+ C1 = 1 - 0.5 * (sigma2 / (sigma2 + 0.33));
+ }
+ virtual Color computeColor(Ray ray, const IntersectionInfo& info) override;
+};
+
class Phong: public Shader {
public:
Color diffuse, specular;
diff --git a/src/vector.h b/src/vector.h
index c80a8c9..ef9baaa 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -159,3 +159,8 @@ inline Vector reflect(const Vector& i, const Vector& n)
{
return 2 * dot(-i, n) * n + i;
}
+
+/// angle between two vectors, in radians
+inline double angle(const Vector& a, const Vector& b) {
+ return acos(dot(normalize(a), normalize(b)));
+}
--
2.42.0
|