-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.h
244 lines (211 loc) · 4.36 KB
/
scene.h
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
234
235
236
237
238
239
240
241
242
243
244
#ifndef SCENE_H
#define SCENE_H
#include "types.h"
#include "math_3d.h"
#include "list.h"
#include "alloc.h"
#include "bvh.h"
#define EPSILON 1e-5
struct rgba {
union {
struct {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint8_t a, b, g, r;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
uint8_t r, g, b, a;
#else
#error "Unknown endianess"
#endif
};
uint32_t rgba8888;
};
};
struct camera {
vec3_t pos;
vec3_t dir;
float pitch;
float yaw;
};
struct opencl;
struct sdl;
enum {
HEAP_SIZE = 1<<30,
CHUNK_SIZE = 1<<10,
};
struct scene {
uint32_t dont_use_bvh;
uint32_t width;
uint32_t height;
float fov;
vec3_t backcolor;
mat4_t c2w;
float bias;
uint32_t ray_depth;
uint32_t samples_per_pixel;
uint64_t num_verts;
struct camera cam;
__global struct rgba *framebuffer;
__global struct ray_cast_state *ray_states;
__global void *heap;
struct allocator alloc;
struct bvhtree bvhtree;
struct opencl *opencl;
struct sdl *sdl;
struct list_head mesh_objects;
struct list_head notmesh_objects;
struct list_head lights;
struct {
uint64_t rays;
} stat;
};
enum {
RAY_CAST_CALL,
RAY_CAST_REFLECT_YIELD,
RAY_CAST_RR_REFRACT_YIELD,
RAY_CAST_RR_REFLECT_YIELD,
};
struct ray_cast_state {
uint32_t type;
union {
struct {
vec3_t hit_color;
__global struct object *hit_object;
} reflect;
struct {
float kr;
vec3_t dir;
vec3_t hit_normal;
vec3_t hit_point;
vec3_t bias;
uint8_t outside;
} rr_refract;
struct {
float kr;
vec3_t refract_color;
} rr_reflect;
};
};
enum material_type {
MATERIAL_PHONG,
MATERIAL_REFLECT,
MATERIAL_REFLECT_REFRACT,
};
enum pattern_type {
PATTERN_UNKNOWN,
PATTERN_CHECK,
PATTERN_LINE
};
struct pattern {
enum pattern_type type;
float scale;
float angle;
};
struct object;
/* OpenCL does not support function pointers, se la vie */
#ifndef __OPENCL__
struct object_ops {
void (*destroy)(struct object *obj);
int (*unmap)(struct object *obj);
bool (*intersect)(__global struct object *obj, const vec3_t *orig, const vec3_t *dir,
float *near, uint32_t *index, vec2_t *uv);
void (*get_surface_props)(__global struct object *obj, const vec3_t *hit_point,
const vec3_t *dir, uint32_t index, const vec2_t *uv,
vec3_t *hit_normal,
vec2_t *hit_tex_coords);
};
#endif
enum object_type {
UNKNOWN_OBJECT = 0,
SPHERE_OBJECT,
PLANE_OBJECT,
MESH_OBJECT,
};
struct object {
uint32_t type; /* object type */
const struct object_ops *ops;
struct list_head entry;
mat4_t o2w;
enum material_type material;
struct pattern pattern;
float albedo;
float ior; /* index of refraction */
vec3_t Kd; /* diffuse weight for each RGB channel */
vec3_t Ks; /* specular weight for each RGB channel */
float n; /* specular exponent */
float r; /* reflection coef */
};
struct sphere {
struct object obj;
float radius;
float radius_pow2;
vec3_t center;
};
struct plane {
struct object obj;
/*
* Components of generic plane equation: Ax + By + Cy + d = 0,
* where normal = (A, B, C)
*/
vec3_t normal;
float d;
/* Basis on 3D plane to make UV mapping */
vec3_t b1;
vec3_t b2;
};
struct triangle_mesh {
struct object obj;
bool smooth_shading; /* smooth shading */
uint32_t num_verts; /* number of vertices */
__global vec3_t *vertices; /* vertex positions */
__global vec3_t *normals; /* vertex normals */
__global vec2_t *sts; /* texture coordinates */
};
struct light;
/* OpenCL does not support function pointers, se la vie */
#ifndef __OPENCL__
struct light_ops {
void (*destroy)(struct light *light);
int (*unmap)(struct light *light);
void (*illuminate)(__global struct light *light, const vec3_t *orig,
vec3_t *dir, vec3_t *intensity, float *distance);
};
#endif
enum light_type {
UNKNOWN_LIGHT = 0,
DISTANT_LIGHT,
POINT_LIGHT,
};
struct light {
uint32_t type; /* light type */
const struct light_ops *ops;
struct list_head entry;
vec3_t color;
float intensity;
};
struct distant_light {
struct light light;
vec3_t dir;
};
struct point_light {
struct light light;
vec3_t pos;
};
enum ray_type {
PRIMARY_RAY,
SHADOW_RAY
};
struct intersection {
__global struct object *hit_object;
float near;
vec2_t uv;
uint32_t index;
};
struct ray_cast_input {
__global struct scene *scene;
vec3_t orig;
vec3_t dir;
};
struct ray_cast_output {
vec3_t color;
};
#endif /* SCENE_H */