-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.h
384 lines (316 loc) · 8.8 KB
/
renderer.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// Created by doug on 4/16/15.
//
#ifndef PHYSICA_GAME_RENDER_H
#define PHYSICA_GAME_RENDER_H
#include "typedefs.h"
#include "physica_math.h"
#include "camera.h"
#include "stb_truetype.h"
struct tex2 {
i32 width, height, pitch;
u32 texture_id;
u32* pixels;
};
struct glyph_spec_ {
tex2 texture;
v2 hotspot;
f32 advance_width;
f32 left_side_bearing;
};
struct font_spec_ {
tex2 texture;
stbtt_bakedchar baked_chars[96];
};
struct window_description_ {
i32 width, height;
};
typedef v3 color_;
typedef v4 rgba_;
enum render_type_ {
RENDER_TYPE_RECT,
RENDER_TYPE_TEXTURE,
RENDER_TYPE_CIRCLE,
RENDER_TYPE_RECT_OUTLINE,
RENDER_TYPE_CIRCLE_OUTLINE,
RENDER_TYPE_COLOR_PICKER,
};
enum render_flags_ {
RENDER_WIREFRAME = 0x1,
};
struct render_rect_ {
// required:
u32 type;
v2 center;
b32 parallax;
v2 diagonal;
f32 orientation;
color_ color;
};
struct render_color_picker_ {
// required:
u32 type;
v2 center;
b32 parallax;
v2 diagonal;
v3 hsv;
};
struct render_texture_ {
// required:
u32 type;
v2 center;
b32 parallax;
rgba_ tint;
tex2 texture;
v2 hotspot;
rect_i source_rect;
f32 pixel_size;
f32 orientation;
};
struct render_circle_ {
// required:
u32 type;
v2 center;
b32 parallax;
f32 radius;
color_ color;
};
struct render_object_ {
f32 z;
union {
struct {
u32 type;
v2 center;
b32 parallax;
};
render_rect_ render_rect;
render_color_picker_ render_color_picker;
render_texture_ render_texture;
render_circle_ render_circle;
};
};
struct frame_buffer_ {
u32 id;
u32 texture;
i32 width;
i32 height;
};
struct render_group_ {
frame_buffer_ frame_buffer;
rgba_ lighting;
vec<render_object_> objects;
};
struct render_task_ {
window_description_ buffer;
camera_ camera;
render_group_* render_group;
rect_i clip_rect;
};
enum gl_resouce_ {
RES_SOLIDS_PROG,
RES_SOLIDS_VAO_RECT,
RES_SOLIDS_VAO_CIRCLE,
RES_SOLIDS_VERTEX_MODELSPACE,
RES_SOLIDS_DRAW_COLOR,
RES_SOLIDS_TRANSFORM,
RES_SOLIDS_LIGHTING,
RES_TEXTURES_PROG,
RES_TEXTURES_VAO_RECT,
RES_TEXTURES_VERTEX_MODELSPACE,
RES_TEXTURES_VERTEX_UV,
RES_TEXTURES_TINT,
RES_TEXTURES_TRANSFORM,
RES_TEXTURES_UV_TRANSFORM,
RES_TEXTURES_SAMPLER,
RES_TEXTURES_LIGHTING,
RES_TEXTURES_TEXTURE_ID,
RES_SOLID_PARTICLES_PROG,
RES_SOLID_PARTICLES_VAO_RECT,
RES_SOLID_PARTICLES_VERTEX_MODELSPACE,
RES_SOLID_PARTICLES_CENTER,
RES_SOLID_PARTICLES_SCALING,
RES_SOLID_PARTICLES_DRAW_COLOR,
RES_SOLID_PARTICLES_CENTER_BUFFER,
RES_SOLID_PARTICLES_SCALING_BUFFER,
RES_SOLID_PARTICLES_DRAW_COLOR_BUFFER,
RES_SOLID_PARTICLES_VIEW_TRANSFORM,
RES_SOLID_PARTICLES_LIGHTING,
RES_GRADIENT_PROG,
RES_GRADIENT_VAO_RECT,
RES_GRADIENT_VERTEX_MODELSPACE,
RES_GRADIENT_VIEWPORT,
RES_GRADIENT_START_COLOR,
RES_GRADIENT_END_COLOR,
RES_GRADIENT_GRADIENT_START,
RES_GRADIENT_GRADIENT_END,
RES_COLOR_PICKER_PROG,
RES_COLOR_PICKER_VAO_RECT,
RES_COLOR_PICKER_VERTEX_MODELSPACE,
RES_COLOR_PICKER_HSV,
RES_COLOR_PICKER_MIN_P,
RES_COLOR_PICKER_MAX_P,
RES_COLOR_PICKER_TRANSFORM,
RES_COUNT,
};
enum gl_program_id_ {
GL_PROG_SOLIDS,
GL_PROG_TEXTURES,
GL_PROG_SOLID_PARTICLES,
GL_PROG_COUNT,
};
enum gl_vao_id_ {
GL_VAO_RECT,
GL_VAO_CIRCLE,
GL_VAO_COUNT,
};
enum gl_attrib_id_ {
GL_ATTRIB_VERTEX_MODELSPACE,
GL_ATTRIB_VERTEX_UV,
GL_ATTRIB_PARTICLE_CENTER,
GL_ATTRIB_PARTICLE_SCALING,
GL_ATTRIB_PARTICLE_DRAW_COLOR,
GL_ATTRIB_COUNT,
};
enum gl_uniform_id_ {
GL_UNIFORM_MAIN_TRANSFORM,
GL_UNIFORM_UV_TRANSFORM,
GL_UNIFORM_SAMPLER,
GL_UNIFORM_DRAW_COLOR,
GL_UNIFORM_LIGHTING,
GL_UNIFORM_COUNT,
};
enum gl_dynamic_vbo_id_ {
GL_DYNAMIC_CENTERS,
GL_DYNAMIC_SCALING,
GL_DYNAMIC_DRAW_COLOR,
GL_DYNAMIC_BUFFER_COUNT,
};
struct gl_program_ {
u32 id;
u32 vaos[GL_VAO_COUNT];
u32 dynamic_buffers[GL_DYNAMIC_BUFFER_COUNT];
i32 attribs[GL_ATTRIB_COUNT];
i32 uniforms[GL_UNIFORM_COUNT];
};
struct gl_programs_ {
union {
i32 i_res_ids[RES_COUNT];
u32 u_res_ids[RES_COUNT];
};
gl_program_ programs[GL_PROG_COUNT];
};
#pragma pack(push, 1)
struct bitmap_header_ {
u16 file_type;
u32 file_size;
u16 reserved_1;
u16 reserved_2;
u32 bitmap_offset;
u32 size;
i32 width;
i32 height;
u16 planes;
u16 bits_per_pixel;
u32 compression;
u32 size_of_bitmap;
i32 horz_resolution;
i32 vert_resolution;
u32 colors_used;
u32 colors_important;
u32 red_mask;
u32 green_mask;
u32 blue_mask;
};
#pragma pack(pop)
gl_programs_ load_programs();
frame_buffer_ default_frame_buffer(i32 width, i32 height);
frame_buffer_ create_frame_buffer(i32 width, i32 height);
tex2 load_image(char* filename);
tex2 load_bmp(char* filename, i32 scaling);
render_object_* push_rect(render_group_* render_group,
color_ color,
v2 center,
v2 diagonal,
f32 orientation = 0.0f,
f32 z = 0.0f,
b32 parallax = false);
render_object_* push_rect(render_group_* render_group,
color_ color,
rect r) {
return push_rect(render_group,
color,
0.5f * (r.min + r.max),
r.max - r.min);
}
render_object_* push_color_picker(render_group_* render_group,
v3 hsv,
rect r);
render_object_* push_rect_outline(render_group_* render_group,
color_ color,
v2 center,
v2 diagonal,
f32 orientation,
f32 z,
b32 parallax = false);
render_object_* push_circle(render_group_* render_group,
color_ color,
v2 center,
f32 radius,
f32 z = 0.0f,
b32 parallax = false);
render_object_* push_texture(render_group_* render_group,
v2 center,
v2 hotspot,
f32 pixel_size,
tex2 texture,
rect_i source_rect,
rgba_ tint,
f32 orientation,
f32 z,
b32 parallax = false);
struct transient_state_;
void draw_render_group(transient_state_* transient_state,
gl_programs_* programs,
camera_ camera,
render_group_* render_group);
void clear_render_group(render_group_ render_group);
void draw_bmp(window_description_ buffer,
rect_i clip_rect,
tex2 bitmap,
v2 center,
f32 source_pixel_size,
v2 hotspot,
f32 orientation);
void draw_bmp(tex2 dest,
tex2 source,
i32 dest_hotspot_x, i32 dest_hotspot_y,
f32 source_pixel_size,
u32 source_left, u32 source_top,
u32 source_width, u32 source_height,
f32 source_hotspot_x, f32 source_hotspot_y, f32 orientation);
void draw_bmp(window_description_ buffer,
tex2 bitmap,
i32 dest_hotspot_x, i32 dest_hotspot_y,
f32 source_pixel_size,
u32 source_left, u32 source_top,
u32 source_width, u32 source_height,
f32 source_hotspot_x, f32 source_hotspot_y, f32 orientation);
void draw_circ_outline(window_description_ buffer,
rect_i clip_rect,
color_ color,
v2 center,
f32 radius);
void draw_rectangle(window_description_ buffer,
rect_i clip_rect,
color_ color, i32 center_x, i32 center_y, u32 width, u32 height,
f32 orientation = 0.0f);
void draw_rectangle(window_description_ buffer,
color_ color, i32 center_x, i32 center_y, u32 width, u32 height,
f32 orientation = 0.0f);
void add_debug_point(v2 point, i32 color);
void draw_debug_points();
void present_seconary_buffer();
v2 invert_y(v2 position, window_description_ window) {
return v2 {position.x, window.height - position.y - 1.0f};
}
#endif //PHYSICA_GAME_RENDER_H