-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
158 lines (124 loc) · 5.37 KB
/
main.c
File metadata and controls
158 lines (124 loc) · 5.37 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
//
// main.c
// RayThrower
//
// Created by Max Scribner on 4/12/19.
// Copyright © 2019 Max Scribner. All rights reserved.
//
#define false 0
#define true 1
#include <SDL2/SDL.h>
#include <stdio.h>
#include "physics.h"
#define RECURSION_COUNT 8
#define FLAT_WARP 0
#define GRAVITY_WARP 1
#define COLOR_RENDER 0
#define ITERATIONS_RENDER 1
#define DISTANCE_RENDER 2
const int screenHeight = 512;
const int screenWidth = 512;
const float traceRatio = 1.0/8;
const int traceHeight = screenHeight * traceRatio;
const int traceWidth = screenWidth * traceRatio;
const float frustum_dist = 1;
int draw_type = 0;
int warp_type = FLAT_WARP;
extern Object object;
extern vec3 (*acceleration_field)(vec3);
int main(int argc, const char * argv[]) {
if (SDL_Init (SDL_INIT_VIDEO) == 0) {
SDL_Window *window;
SDL_Renderer *renderer;
int exit = false;
int change = true;
SDL_Event event;
if (SDL_CreateWindowAndRenderer(screenWidth, screenHeight, 0, &window, &renderer) == 0) {
while (!exit) {
SDL_Rect drawRect;
drawRect.w = drawRect.h = 1/traceRatio;
if (change) {
SDL_RenderClear(renderer);
for(int x=0; x<traceWidth; x++) {
for(int y=0; y<traceHeight; y++) {
drawRect.x = x/traceRatio;
drawRect.y = y/traceRatio;
Light l;
l.position.x = l.position.y = 0;
l.position.z = -frustum_dist;
vec3 start_v = {(x-traceWidth/2.0)/traceWidth, (y-traceHeight/2.0)/traceHeight, frustum_dist};
start_v = normalize(start_v);
l.velocity = start_v;
Object *intersect;
int count = RECURSION_COUNT;
acceleration_field = warp_type == FLAT_WARP ? &flat_field: &gravity_field;
while ((intersect = update_light(&l)) == NULL && (count--) > 0 ) {}
Color c;
if (intersect) {
switch(draw_type) {
case COLOR_RENDER:
c = intersect->color(intersect, l.position);
break;
case ITERATIONS_RENDER:
c.r = c.b = c.g = 256*((float) count)/RECURSION_COUNT;
break;
default: break;
}
} else {
c.r = 16;
c.g = 16;
c.b = 16;
}
SDL_SetRenderDrawColor(renderer,
c.r,
c.g,
c.b,
SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &drawRect);
}
}
SDL_RenderPresent(renderer);
change = true;
}
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit = true;
} else if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.scancode) {
case SDL_SCANCODE_PERIOD:
object.pos.z += 0.1;
change = true;
break;
case SDL_SCANCODE_COMMA:
object.pos.z -= 0.1;
change = true;
break;
case SDL_SCANCODE_P:
printf("Dist = %f\n", object.pos.z);
break;
case SDL_SCANCODE_F:
warp_type = FLAT_WARP;
break;
case SDL_SCANCODE_G:
warp_type = GRAVITY_WARP;
break;
case SDL_SCANCODE_C:
draw_type = COLOR_RENDER;
break;
case SDL_SCANCODE_I:
draw_type = ITERATIONS_RENDER;
break;
default:
break;
}
}
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
}
printf("Hello, World!\n");
return 0;
}