-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevrdemo.c
233 lines (211 loc) · 7.39 KB
/
levrdemo.c
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
225
226
227
228
229
230
231
232
233
#define APP_IMPLEMENTATION
#define APP_WINDOWS
#include "app.h"
#include <stdlib.h>
#include <stdio.h>
#include "levr.h"
LEVR_Screen screen = {0};
LEVR_State state = {0};
LEVR_Grid grid = {0};
LEVR_MaterialSet ms = {0};
LEVR_Camera cam = {0};
LEVR_Scene scene = {0};
#define SCWIDTH 256
#define SCHEIGHT 144
void draw_pixel(LEVR_State* state, LEVR_Pixel* pixel){
screen.pixels[pixel->x + pixel->y * screen.width] = pixel->color;
}
void CreateWalls()
{
int i, j, k;
for (i = 0; i < grid.width; i++)
{
grid.voxels[i*grid.width*grid.height + 1*grid.width + 1] = 3;
grid.voxels[1*grid.width*grid.height + i*grid.width + 1] = 2;
grid.voxels[1*grid.width*grid.height + 1*grid.width + i] = 1;
}
}
void CreateGrid()
{
grid.width = 512;
grid.height = 512;
grid.depth = 512;
grid.voxels = malloc(grid.width * grid.height * grid.depth * sizeof(uint8_t));
uint8_t *buf = malloc(256 * 256 * 256 * sizeof(uint8_t));
FILE *file = fopen("sponza.raw", "r");
int c;
int i = 0;
int j = 0;
int k = 0;
while ((c = fgetc(file)) != EOF)
{
buf[i] = c - '0';
i++;
}
fclose(file);
for(i = 0; i < 256; i++){
for(j = 0; j < 256; j++){
for(k = 0; k < 256; k++){
grid.voxels[(k)*grid.width*grid.height + (j)*grid.height + (i)] = buf[k*255*255 + j*255+i];
}
}
}
free(buf);
grid.voxels[(50)*grid.width*grid.height + (20)*grid.height + (50)] = 4;
}
void InitMaterials(){
ms.max_materials = 6;
LEVR_Material air = {0};
air.color = LEVR_rgb_to_u32(0, 0, 0);
LEVR_Material red = {0};
red.color = LEVR_rgb_to_u32(100, 0, 0);
red.emittance = 0;
red.ambient = 0.5;
red.diffuse = 0;
LEVR_Material green = {0};
green.color = LEVR_rgb_to_u32(0, 100, 0);
green.emittance = 0;
green.ambient = 0.5;
green.diffuse = 0;
LEVR_Material blue = {0};
blue.color = LEVR_rgb_to_u32(0, 0, 100);
blue.emittance = 0;
blue.ambient = 0.5;
blue.diffuse = 0;
LEVR_Material light = {0};
light.color = 0xFFFFFF;
light.emittance = 1.0;
LEVR_Material yellow = {0};
yellow.color = LEVR_rgb_to_u32(0, 100, 100);
yellow.emittance = 0;
yellow.ambient = 0.5;
yellow.diffuse = 0;
ms.materials = malloc(sizeof(LEVR_Material) * ms.max_materials);
ms.materials[0] = air;
ms.materials[1] = red;
ms.materials[2] = green;
ms.materials[3] = blue;
ms.materials[4] = light;
ms.materials[5] = yellow;
}
void CreateCamera(){
cam.width = SCWIDTH;
cam.height = SCHEIGHT;
LEVR_vec3 pos = {9, 9, 9};
LEVR_vec3 dir = {0.1, 0.1, 0};
LEVR_vec3 up = {0, 1, 0};
cam.pos = pos;
cam.dir = dir;
cam.up = up;
cam.fov = 70;
}
int app_proc( app_t* app ){
LEVR_screen_resize(&screen, SCWIDTH, SCHEIGHT);
LEVR_screen_flush(&screen, 0xffffff);
CreateCamera();
CreateGrid();
//CreateWalls();
InitMaterials();
int cell = 0;
scene.grid = grid;
scene.max_lights = 2;
scene.lights = malloc(scene.max_lights * sizeof(LEVR_vec3i));
scene.lights[0] = (LEVR_vec3i){50, 20, 50};
scene.lights[1] = (LEVR_vec3i){30, 10, 60};
state.set_pixel = &draw_pixel;
state.flags = 0;
app_screenmode(app, APP_SCREENMODE_WINDOW );
app_window_size(app, 1024, 576);
app_interpolation(app, APP_INTERPOLATION_NONE);
LEVR_vec3 forward = {0, 0, 1};
LEVR_vec3 right = {1, 0, 0};
LEVR_vec3 up = {0, 1, 0};
LEVR_REAL speed = 2;
while( app_yield( app ) != APP_STATE_EXIT_REQUESTED ){
LEVR_vec3 cameraForward = LEVR_vec3_rotate(forward, cam.dir);
LEVR_vec3 cameraRight = LEVR_vec3_rotate(right, cam.dir);
LEVR_vec3 cameraUp = LEVR_vec3_rotate(up, cam.dir);
app_input_t input = app_input(app);
for (int i = 0; i < input.count; i++) {
if (input.events[i].type == APP_INPUT_SCROLL_WHEEL) {
if (input.events[i].data.wheel_delta > 0)
{;
} else if(input.events[i].data.wheel_delta < 0){
;
}
}
if (input.events[i].type == APP_INPUT_MOUSE_DELTA && app->has_focus){
cam.dir.y += input.events[i].data.mouse_delta.x;
cam.dir.x += input.events[i].data.mouse_delta.y;
app_pointer_pos(app, 512, 576 / 2);
}
if(input.events[i].type == APP_INPUT_KEY_DOWN){
if(input.events[i].data.key == APP_KEY_LEFT){
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 0;
scene.lights[0].x += 1;
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 4;
}
if(input.events[i].data.key == APP_KEY_RIGHT){
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 0;
scene.lights[0].x += -1;
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 4;
}
if(input.events[i].data.key == APP_KEY_UP){
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 0;
scene.lights[0].z += 1;
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 4;
}
if(input.events[i].data.key == APP_KEY_DOWN){
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 0;
scene.lights[0].z += -1;
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 4;
}
if(input.events[i].data.key == APP_KEY_W){
cam.pos = LEVR_vec3_add(cam.pos, LEVR_vec3_scalef(cameraForward, speed));
}
if(input.events[i].data.key == APP_KEY_S){
cam.pos = LEVR_vec3_add(cam.pos, LEVR_vec3_scalef(cameraForward, -speed));
}
if(input.events[i].data.key == APP_KEY_A){
cam.pos = LEVR_vec3_add(cam.pos, LEVR_vec3_scalef(cameraRight, -speed));
}
if(input.events[i].data.key == APP_KEY_D){
cam.pos = LEVR_vec3_add(cam.pos, LEVR_vec3_scalef(cameraRight, speed));
}
if(input.events[i].data.key == APP_KEY_Q){
cam.pos = LEVR_vec3_add(cam.pos, LEVR_vec3_scalef(cameraUp, -speed));
}
if(input.events[i].data.key == APP_KEY_E){
cam.pos = LEVR_vec3_add(cam.pos, LEVR_vec3_scalef(cameraUp, speed));
}
if(input.events[i].data.key == APP_KEY_F){
cam.fov += 5;
}
if(input.events[i].data.key == APP_KEY_G){
cam.fov -= 5;
}
if(input.events[i].data.key == APP_KEY_R){
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 0;
scene.lights[0].y += -1;
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 4;
}
if(input.events[i].data.key == APP_KEY_T){
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 0;
scene.lights[0].y += 1;
scene.grid.voxels[scene.lights[0].z*grid.width*grid.height + scene.lights[0].y*grid.height + scene.lights[0].x] = 4;
}
if(input.events[i].data.key == APP_KEY_H){
scene.grid.voxels[cell] = 3;
cell++;
}
}
}
LEVR_render(&state, cam, scene, ms);
app_present( app, screen.pixels, screen.width, screen.height, 0xffffff, 0x000000 );
}
return 0;
}
int main( int argc, char** argv ){
(void) argc, argv;
return app_run( app_proc, NULL, NULL, NULL, NULL );
}