-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
100 lines (80 loc) · 3.15 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
#include "simpleSDL.h"
#include "element.h"
#include "sphere.h"
#include "plane.h"
#include "material.h"
#include <algorithm>
static const int SCREEN_INIT_ERROR = 5;
static const int RETURN_OK = 0;
static const int SCREEN_WIDTH = 512;
static const int SCREEN_HEIGH = 384;
// viewAngle is 90 deg. or PI/2
static const float halfViewAngleX = static_cast<float> (M_PI/ 2.0f) / 2.0f;
static const float halfViewAngleY =
halfViewAngleX * \
(static_cast<float> (SCREEN_HEIGH) / static_cast<float> (SCREEN_WIDTH));
// |angle a/
// | /
// | 1 /
// - - - - |---*
//1/tan a | /
// | / viewAngle / 2 (or halfViewAngle)
// camera :-------
// \
// \
// \
// a = M_PI/2 - viewAngle/2
// tan a = 1 / range x <=> range x = 1 / tan a
static const float rangeX = 1 / tanf(M_PI / 2.0f - halfViewAngleX);
static const float rangeY = 1 / tanf(M_PI / 2.0f - halfViewAngleY);
void screenPixelDirection(Direction &direction, float x, float y) {
direction[0] = rangeX * (x - SCREEN_WIDTH / 2.0f) / SCREEN_WIDTH;
direction[1] = rangeY * (y - SCREEN_HEIGH / 2.0f) / SCREEN_HEIGH;
direction[2] = 1.f;
direction.makeUnitVector();
}
Pixel lightToPixel(Light l) {
Pixel p;
p.R = static_cast<Uint8> (std::min(255.f, l.light * l.col.Red * 255.0f));
p.G = static_cast<Uint8> (std::min(255.f, l.light * l.col.Green * 255.0f));
p.B = static_cast<Uint8> (std::min(255.f, l.light * l.col.Blue * 255.0f));
return p;
}
int main(int argc, char *argv[]) {
Uint32 *screen; //This pointer will reference the backbuffer
screen = InitVideo(SCREEN_WIDTH, SCREEN_HEIGH);
if (screen == nullptr) exit(SCREEN_INIT_ERROR);
LightRay camera_ray(Position(0, 0, -3), Direction(0, 0, 1));
World world;
Sphere* mov = new Sphere(Position(3, 3, 10), 5, Material::glass());
world.elements.push_back(mov);
world.elements.push_back(new Sphere(Position(-3, 3, 10), 5, Material::RedPlastic()));
world.elements.push_back(new Sphere(Position(-3, -3, 10), 4, Material::silver()));
world.elements.push_back(new Plane());
while (1) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_MOUSEMOTION) {
world.globalLight.direction[0] += 0.01f * (event.motion.xrel);
world.globalLight.direction[1] += 0.01f * (event.motion.yrel);
}
if (event.type == SDL_KEYDOWN) {
exit(RETURN_OK);
}
if (event.type == SDL_QUIT) {
exit(RETURN_OK);
}
}
Uint32 t1 = SDL_GetTicks();
mov->translate(Direction(0.f, 0.f, 0.1f * (1 - 2 * (((int) (t1 / 2000.0f)) % 2))));
for (int x = 0; x < SCREEN_WIDTH; x++) for (int y = 0; y < SCREEN_HEIGH; y++) {
screenPixelDirection(camera_ray.direction, static_cast<float> (x), static_cast<float> (y));
Light value = world.rayTracing(camera_ray, nullptr);
setPixelValue(screen, x, y, lightToPixel(value));
}
showImage(screen);
printf("time: %1ums\n", SDL_GetTicks() - t1);
}
freeRenderer(screen);
return RETURN_OK;
}