]> Git Repo - raytracer.git/blob - main.cpp
first commit
[raytracer.git] / main.cpp
1 #include <iostream>
2 #include "common.h"
3 #include "geometry.h"
4 #include "constants.h"
5 #include "sdl_funcs.h"
6 #include "objects.h"
7 #include "raytrace.h"
8
9 SDL_Event event;
10
11 void render(const std::vector<Sphere> &spheres, const std::vector<Light> &lights,const Cam &cam) {
12     const float fov  = cam.fov;
13     std::vector<vec3> framebuffer(SCREEN_WIDTH*SCREEN_HEIGHT);
14
15
16     #pragma omp parallel for
17     for (size_t j = 0; j<SCREEN_HEIGHT; j++) { // actual rendering loop
18         for (size_t i = 0; i<SCREEN_WIDTH; i++) {
19             float dir_x =  (i + 0.5) -  SCREEN_WIDTH/2.;
20             float dir_y = -(j + 0.5) + SCREEN_HEIGHT/2.;    // this flips the image at the same time
21             float dir_z = -SCREEN_HEIGHT/(2.*tan(fov/2.));
22             framebuffer[i+j*SCREEN_WIDTH] = cast_ray(cam.pos, ((vec3{dir_x, dir_y, dir_z}+cam.dir)).normalize(), spheres, lights);
23         }
24     }
25     size_t index = 0;
26     char * texture_pixels = sdl_pixels_lock();
27
28     for (vec3 &c : framebuffer) {
29         float max = std::max(c[0], std::max(c[1], c[2]));
30         if (max>1) c = c*(1./max);
31         texture_pixels[index++] = (char)(255 * c[0]); 
32         texture_pixels[index++] = (char)(255 * c[1]);
33         texture_pixels[index++] = (char)(255 * c[2]);
34     }
35     sdl_pixels_unlock();
36
37
38     
39 }
40 void signal_hand(int signum) {
41    std::cout << "Caught signal " << signum << std::endl;
42    sdl_close(0);
43 }
44
45 int main() {
46     int winsizeX, winsizeY;
47     signal(SIGINT, signal_hand);
48         if( !sdl_init() )
49         {
50                 printf( "Failed to initialize!\n" );
51         exit(-1);
52         }
53
54         //Free resources and close SDL
55     const Material      ivory = {1.0, {0.6,  0.3, 0.1, 0.0}, {0.4, 0.4, 0.3},   50.};
56     const Material      glass = {1.5, {0.0,  0.5, 0.1, 0.8}, {0.6, 0.7, 0.8},  125.};
57     const Material red_rubber = {1.0, {0.9,  0.1, 0.0, 0.0}, {0.3, 0.1, 0.1},   10.};
58     const Material     mirror = {1.0, {0.0, 10.0, 0.8, 0.0}, {1.0, 1.0, 1.0}, 1425.};
59
60     std::vector<Sphere> spheres = {
61         Sphere{vec3{-3,    0,   -16}, 2,      ivory},
62         Sphere{vec3{-1.0, -1.5, -12}, 2,      glass},
63         Sphere{vec3{ 1.5, -0.5, -18}, 3, red_rubber},
64         Sphere{vec3{ 7,    5,   -18}, 4,     mirror}
65     };
66
67     std::vector<Light> lights = {
68         {{-20, 20,  20}, 1.5},
69         {{ 30, 50, -25}, 1.8},
70         {{ 30, 20,  30}, 1.7}
71     };
72
73     Cam cam = {
74         {1,1,0},
75         {-1,-1,0},
76         M_PI_2
77     };
78
79
80     while(1){
81
82         int x,y;
83         SDL_PumpEvents();
84         while( SDL_PollEvent( &event ) )
85         {      
86             if( event.type == SDL_QUIT )
87                 sdl_close(0);
88             if(event.type == SDL_MOUSEMOTION){
89                 SDL_GetMouseState(&x,&y);
90                 SDL_GetWindowSize(sdl_getwindow(), &winsizeX,&winsizeY);
91                 cam.dir = vec3{((float)x)*((float)SCREEN_WIDTH/(float)winsizeX),((float)-y)*((float)SCREEN_HEIGHT/(float)winsizeY),0};
92             }
93             if( event.type == SDL_KEYDOWN )
94             {
95                         //Select surfaces based on key press
96                 switch( event.key.keysym.sym )
97                 {
98                 case SDLK_w:
99                     cam.pos.z -= 1;
100                 break;
101                 case SDLK_s:
102                     cam.pos.z += 1;
103                 break;
104                 case SDLK_a:
105                     cam.pos.x -= 1;
106                 break;
107                 case SDLK_d:
108                     cam.pos.x += 1;
109                 break;
110                 case SDLK_SPACE:
111                     cam.pos.y += 1;
112                 break;
113                 case SDLK_LSHIFT:
114                     cam.pos.y -= 1;
115                 break;
116                 default:
117                 break;
118                 }
119             }
120         }
121         render(spheres, lights,cam);
122         sdl_frame();
123     }
124     sdl_free();
125     return -2; // We shouldn't get here
126 }
This page took 0.028009 seconds and 4 git commands to generate.