forked from DinoZ1729/Ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
224 lines (203 loc) · 6.39 KB
/
main.cpp
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
#ifdef _WIN32
#include <windows.h>
#endif
#include <cstring>
#include <iostream>
#include <unistd.h>
#include "functions.cpp"
#define PI 3.14159265358979323846
using namespace std;
using namespace ray;
// screen dimensions
#define WIDTH 800
#define HEIGHT 600
//Create a namespace
namespace ray{
// width and height of each character in pixels
const int dW = 4, dH = 8;
// set cursor at start to avoid flickering
void gotoxy(short x, short y) {
#ifdef _WIN32
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
#else
printf("%c[%d;%df", 0x1B, y, x);
#endif
}
char palette[] = " .:;~=#OB8%&";
typedef struct Ball {
double center[3];
double radius;
double color; // on the interval from 0 (dark) to 1 (light)
double coeff; // on the scale from 0 to 1 how much does the ball reflect light
// (1 means it's the perfect mirror)
} ball;
class camera {
public:
double x, y, z;
double matrix[16], inv[16];
camera(double r, double alfa, double beta) {
// alfa is camera's angle along the xy plane.
// beta is camera's angle along z axis
// r is the distance from the camera to the origin
double a = sin(alfa), b = cos(alfa), c = sin(beta), d = cos(beta);
x = r * b * d;
y = r * a * d;
z = r * c;
// matrix
matrix[3] = matrix[7] = matrix[11] = 0;
matrix[15] = 1;
// x
matrix[0] = -a;
matrix[1] = b;
matrix[2] = 0;
// y
matrix[4] = b * c;
matrix[5] = a * c;
matrix[6] = -d;
// z
matrix[8] = b * d;
matrix[9] = a * d;
matrix[10] = c;
matrix[12] = x;
matrix[13] = y;
matrix[14] = z;
// invert
ray::invert(inv, matrix);
}
double rayTrace(double origin[3], double unit[3], ball balls[], int n,
double altitute, double coeff, int limit) {
double color;
double distanceToPlane =
-(origin[2] + altitute) / unit[2]; // this is signed distance
int index = -1;
double distance;
for (int i = 0; i < n; i++) {
double diff[3];
ray::vector(diff, origin, balls[i].center);
double discriminant = ray::dot(unit, diff) * ray::dot(unit, diff) +
balls[i].radius * balls[i].radius - dot(diff, diff);
if (discriminant < 0)
continue;
distance = -ray::dot(unit, diff) - sqrt(discriminant);
if (distance <= 0)
continue;
index = i;
break;
}
if (index == -1) {
if (unit[2] > 0) {
return 0; // ray hit the sky
} else // ray hit the groung
{
double tx = origin[0] + distanceToPlane * unit[0],
ty = origin[1] + distanceToPlane * unit[1];
double color = ray::clamp(1 / (1 + distanceToPlane / 10), 0, 1);
double origin2[3] = {origin[0] + distanceToPlane * unit[0],
origin[1] + distanceToPlane * unit[1],
origin[2] + distanceToPlane * unit[2]};
double unit2[3] = {unit[0], unit[1], -unit[2]};
if ((int)(floor(tx) + floor(ty)) % 2 == 0)
return (1 - coeff) * color + coeff * rayTrace(origin2, unit2, balls,
n, altitute, coeff,
limit - 1);
else
return 0;
}
}
if (unit[2] < 0 && distance > distanceToPlane) // ray hit the groung
{
double tx = origin[0] + distanceToPlane * unit[0],
ty = origin[1] + distanceToPlane * unit[1];
return (double)((int)(floor(tx) + floor(ty)) % 2);
}
// ray hit a ball
double origin2[3] = {origin[0] + unit[0] * distance,
origin[1] + unit[1] * distance,
origin[2] + unit[2] * distance};
double normal[3];
ray::vector(normal, origin2, balls[index].center);
ray::normalize(normal);
double k = 2 * ray::dot(unit, normal);
ray::scale(normal, k);
double unit2[3];
ray::vector(unit2, unit, normal);
if (limit == 0)
return balls[index].color;
return (1 - balls[index].coeff) * balls[index].color +
balls[index].coeff *
rayTrace(origin2, unit2, balls, n, altitute, coeff, limit - 1);
}
};
//End namespace ray
}
int main() {
// ball declaration::
ball balls[3];
balls[0].center[0] = 0;
balls[0].center[1] = 0;
balls[0].center[2] = 0;
balls[0].radius = 1;
balls[0].color = 1;
balls[0].coeff = 0.9;
balls[1].center[0] = -3;
balls[1].center[1] = 0;
balls[1].center[2] = 0;
balls[1].radius = 0.5;
balls[1].color = 1;
balls[1].coeff = 0.7;
balls[2].center[0] = 0;
balls[2].center[1] = -3;
balls[2].center[2] = 0;
balls[2].radius = 0.5;
balls[2].color = 1;
balls[2].coeff = 0.7;
double alfa = 0, beta = PI / 2, r = 1.9;
// starting screen
for (int i = 0; i < HEIGHT / dH - 4; i++) {
for (int j = 0; j < WIDTH / dW; j++) {
putchar('@');
}
putchar('\n');
}
getchar();
gotoxy(0, 0);
while (1) {
char platno[HEIGHT / dH * (WIDTH / dW + 1) + 1];
camera cam(r, alfa, beta);
int p = 0;
for (int i = 0; i < HEIGHT / dH; i++) {
for (int j = 0; j < WIDTH / dW; j++) {
double origin[3] = {cam.x, cam.y, cam.z};
double unit[3] = {
-((double)(j - WIDTH / dW / 2) + 0.5) / (double)(WIDTH / dW / 2),
((double)(i - HEIGHT / dH / 2) + 0.5) / (double)(WIDTH / dH / 2),
-1};
ray::transformVector(unit, cam.matrix);
unit[0] -= cam.x;
unit[1] -= cam.y;
unit[2] -= cam.z;
ray::normalize(unit);
double luminance = cam.rayTrace(origin, unit, balls, 3, 2, 0.3, 5);
int color = (int)((strlen(palette) - 1) * luminance);
platno[p++] = palette[color];
}
platno[p++] = '\n';
}
platno[p] = 0;
// display:
// puts is very fast
puts(platno);
// sleeping to reduce frames count
// maybe there is a better way than sleeping to sync
sleep(5);
// instead of system("cls") i used this because it looks smoother
gotoxy(0, 0);
// update camera position
// using very small angle increments to get a smoother transition
alfa += 0.0003 * PI;
if (beta > PI / 2000)
beta -= 0.0003 * PI;
}
return 0;
}