-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (133 loc) · 4.78 KB
/
Copy pathmain.cpp
File metadata and controls
182 lines (133 loc) · 4.78 KB
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
#include<SDL2/SDL.h>
#include<cmath>
#define WIDTH 1200
#define HEIGHT 600
#define CIRCLE_RADIUS 40
#define SHADOW_CIRCLE_RADIUS 140
#define RAY_NUMBERS 550
#define RAY_THICKNESS 1
#define COLOR_WHITE 0xffffff
#define COLOR_BLACK 0x000000
#define COLOR_YELLOW 0xFFFF00
#define OBSTACLE_SPEED_X 3
#define OBSTACLE_SPEED_Y 2
typedef struct Circle{
double x ;
double y ;
double radius;
} Circle;
typedef struct Ray{
double x_start ;
double y_start ;
double angle ;
}Ray;
void FillCircle(SDL_Surface* , Circle , Uint32);
void generate_rays(Circle , Ray[]);
void FillRays(SDL_Surface* , Ray[] , Uint32, Circle);
void loop(Circle& circle , Circle& shadow_circle , Ray rays[RAY_NUMBERS] , SDL_Rect earse_rect , int obstacle_speed_x , int obstacle_speed_y);
void destroy_window();
SDL_Window* window = SDL_CreateWindow("Ray Tracing", SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED , WIDTH , HEIGHT , 0);
SDL_Surface* surface = SDL_GetWindowSurface(window);
int main(int argc , char** args){
Circle circle = {200, 200 , CIRCLE_RADIUS};
Circle shadow_circle = {550 , 300, SHADOW_CIRCLE_RADIUS};
SDL_Rect earse_rect = (SDL_Rect){0 , 0 , WIDTH , HEIGHT};
Ray rays[RAY_NUMBERS];
generate_rays(circle , rays);
loop(circle , shadow_circle , rays , earse_rect , OBSTACLE_SPEED_X , OBSTACLE_SPEED_X);
destroy_window();
return 0;
}
void loop(Circle& circle , Circle& shadow_circle , Ray rays[RAY_NUMBERS] , SDL_Rect earse_rect , int obstacle_speed_x , int obstacle_speed_y){
SDL_Event event ;
bool running = true;
while(running){
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
running = false ;
SDL_Delay(10);
}
if(event.type == SDL_MOUSEMOTION && event.motion.state != 0){
circle.x = event.motion.x;
circle.y = event.motion.y;
generate_rays(circle , rays);
}
}
shadow_circle.y += obstacle_speed_y;
shadow_circle.x += obstacle_speed_x;
if(shadow_circle.y - shadow_circle.radius < 0){
obstacle_speed_y = -obstacle_speed_y;
}
if(shadow_circle.x - shadow_circle.radius < 0){
obstacle_speed_x = -obstacle_speed_x;
}
if(shadow_circle.y + shadow_circle.radius > HEIGHT){
obstacle_speed_y = -obstacle_speed_y;
}
if(shadow_circle.x + shadow_circle.radius > WIDTH){
obstacle_speed_x = -obstacle_speed_x;
}
SDL_FillRect(surface ,&earse_rect , COLOR_BLACK);
FillRays(surface , rays , COLOR_YELLOW ,shadow_circle);
FillCircle(surface , circle , COLOR_WHITE);
FillCircle(surface , shadow_circle , COLOR_WHITE);
SDL_UpdateWindowSurface(window);
}
}
void destroy_window(){
SDL_DestroyWindow(window);
SDL_Quit();
}
void FillCircle(SDL_Surface* surface , Circle circle , Uint32 color){
double radius_squared = pow(circle.radius , 2);
/**
* We try to make a Circle inside the rectangle
* where the rectangle start from the (x - r) and goes to (x + r) in x-axis and in y axis it start from the (y - r) and goes to (y + r)
* and then generate the pixel of the width and height 1 X 1 to fill the whole area
*/
for(double x = circle.x - circle.radius ; x <= circle.x + circle.radius ; x++){
for(double y = circle.y - circle.radius ; y <= circle.y + circle.radius ; y++){
double distance_squared = pow(x - circle.x , 2) + pow(y - circle.y , 2);
if(distance_squared < radius_squared){
SDL_Rect pixel = (SDL_Rect) {int(x), int(y), 1 , 1};
SDL_FillRect(surface , &pixel , color);
}
}
}
}
void generate_rays(Circle circle , Ray rays[RAY_NUMBERS]){
// 2 * pi * i / total no. of rays to find the angle where ray form
for(int i = 0 ; i < RAY_NUMBERS ; i++){
double angle = (double)i / RAY_NUMBERS * 2 * M_PI;
Ray ray = {circle.x , circle.y , angle};
rays[i] = ray;
}
}
void FillRays(SDL_Surface* surface , Ray rays[RAY_NUMBERS] , Uint32 color , Circle circle){
double radius_sqaured = pow(circle.radius , 2);
for(int i =0 ; i< RAY_NUMBERS ; i++){
Ray ray = rays[i];
/**
* We fill the windows with the ray if the ray hit the object ot it touch the end of the screen then we stop
*/
bool end_of_screen = false;
double x_draw = ray.x_start;
double y_draw = ray.y_start;
while(!end_of_screen){
x_draw += cos(ray.angle);
y_draw += sin(ray.angle);
SDL_Rect ray_point = (SDL_Rect){(int)x_draw , (int)y_draw , RAY_THICKNESS , RAY_THICKNESS};
SDL_FillRect(surface , &ray_point , color);
if(x_draw < 0 || x_draw > WIDTH){
end_of_screen = true;
}
if(y_draw < 0 || y_draw > HEIGHT ){
end_of_screen = true;
}
double distance_squared = pow(x_draw - circle.x , 2) + pow(y_draw - circle.y , 2);
if(distance_squared < radius_sqaured){
break;
}
}
}
}