-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathsgl-sapp.c
254 lines (229 loc) · 7.92 KB
/
sgl-sapp.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//------------------------------------------------------------------------------
// sgl-sapp.c
// Rendering via sokol_gl.h
//------------------------------------------------------------------------------
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_glue.h"
#define SOKOL_GL_IMPL
#include "sokol_gl.h"
#include "dbgui/dbgui.h"
static struct {
sg_pass_action pass_action;
sg_image img;
sg_sampler smp;
sgl_pipeline pip_3d;
} state;
static void init(void) {
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
__dbgui_setup(sapp_sample_count());
// setup sokol-gl
sgl_setup(&(sgl_desc_t){
.logger.func = slog_func,
});
// a checkerboard texture
uint32_t pixels[8][8];
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
pixels[y][x] = ((y ^ x) & 1) ? 0xFFFFFFFF : 0xFF000000;
}
}
state.img = sg_make_image(&(sg_image_desc){
.width = 8,
.height = 8,
.data.subimage[0][0] = SG_RANGE(pixels)
});
// ... and a sampler
state.smp = sg_make_sampler(&(sg_sampler_desc){
.min_filter = SG_FILTER_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
});
/* create a pipeline object for 3d rendering, with less-equal
depth-test and cull-face enabled, note that we don't provide
a shader, vertex-layout, pixel formats and sample count here,
these are all filled in by sokol-gl
*/
state.pip_3d = sgl_make_pipeline(&(sg_pipeline_desc){
.cull_mode = SG_CULLMODE_BACK,
.depth = {
.write_enabled = true,
.compare = SG_COMPAREFUNC_LESS_EQUAL,
},
});
// default pass action
state.pass_action = (sg_pass_action) {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = { 0.0f, 0.0f, 0.0f, 1.0f }
}
};
}
static void draw_triangle(void) {
sgl_defaults();
sgl_begin_triangles();
sgl_v2f_c3b( 0.0f, 0.5f, 255, 0, 0);
sgl_v2f_c3b(-0.5f, -0.5f, 0, 0, 255);
sgl_v2f_c3b( 0.5f, -0.5f, 0, 255, 0);
sgl_end();
}
static void draw_quad(float t) {
static float angle_deg = 0.0f;
float scale = 1.0f + sinf(sgl_rad(angle_deg)) * 0.5f;
angle_deg += 1.0f * t;
sgl_defaults();
sgl_rotate(sgl_rad(angle_deg), 0.0f, 0.0f, 1.0f);
sgl_scale(scale, scale, 1.0f);
sgl_begin_quads();
sgl_v2f_c3b( -0.5f, -0.5f, 255, 255, 0);
sgl_v2f_c3b( 0.5f, -0.5f, 0, 255, 0);
sgl_v2f_c3b( 0.5f, 0.5f, 0, 0, 255);
sgl_v2f_c3b( -0.5f, 0.5f, 255, 0, 0);
sgl_end();
}
// vertex specification for a cube with colored sides and texture coords
static void cube(void) {
sgl_begin_quads();
sgl_c3f(1.0f, 0.0f, 0.0f);
sgl_v3f_t2f(-1.0f, 1.0f, -1.0f, -1.0f, 1.0f);
sgl_v3f_t2f( 1.0f, 1.0f, -1.0f, 1.0f, 1.0f);
sgl_v3f_t2f( 1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2f( 1.0f, 1.0f, -1.0f, -1.0f, -1.0f);
sgl_end();
}
static void draw_cubes(const float t) {
static float rot[2] = { 0.0f, 0.0f };
rot[0] += 1.0f * t;
rot[1] += 2.0f * t;
sgl_defaults();
sgl_load_pipeline(state.pip_3d);
sgl_matrix_mode_projection();
sgl_perspective(sgl_rad(45.0f), 1.0f, 0.1f, 100.0f);
sgl_matrix_mode_modelview();
sgl_translate(0.0f, 0.0f, -12.0f);
sgl_rotate(sgl_rad(rot[0]), 1.0f, 0.0f, 0.0f);
sgl_rotate(sgl_rad(rot[1]), 0.0f, 1.0f, 0.0f);
cube();
sgl_push_matrix();
sgl_translate(0.0f, 0.0f, 3.0f);
sgl_scale(0.5f, 0.5f, 0.5f);
sgl_rotate(-2.0f * sgl_rad(rot[0]), 1.0f, 0.0f, 0.0f);
sgl_rotate(-2.0f * sgl_rad(rot[1]), 0.0f, 1.0f, 0.0f);
cube();
sgl_push_matrix();
sgl_translate(0.0f, 0.0f, 3.0f);
sgl_scale(0.5f, 0.5f, 0.5f);
sgl_rotate(-3.0f * sgl_rad(2*rot[0]), 1.0f, 0.0f, 0.0f);
sgl_rotate(3.0f * sgl_rad(2*rot[1]), 0.0f, 0.0f, 1.0f);
cube();
sgl_pop_matrix();
sgl_pop_matrix();
}
static void draw_tex_cube(const float t) {
static float frame_count = 0.0f;
frame_count += 1.0f * t;
float a = sgl_rad(frame_count);
// texture matrix rotation and scale
float tex_rot = 0.5f * a;
const float tex_scale = 1.0f + sinf(a) * 0.5f;
// compute an orbiting eye-position for testing sgl_lookat()
float eye_x = sinf(a) * 6.0f;
float eye_z = cosf(a) * 6.0f;
float eye_y = sinf(a) * 3.0f;
sgl_defaults();
sgl_load_pipeline(state.pip_3d);
sgl_enable_texture();
sgl_texture(state.img, state.smp);
sgl_matrix_mode_projection();
sgl_perspective(sgl_rad(45.0f), 1.0f, 0.1f, 100.0f);
sgl_matrix_mode_modelview();
sgl_lookat(eye_x, eye_y, eye_z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
sgl_matrix_mode_texture();
sgl_rotate(tex_rot, 0.0f, 0.0f, 1.0f);
sgl_scale(tex_scale, tex_scale, 1.0f);
cube();
}
static void frame(void) {
// frame time multiplier (normalized for 60fps)
const float t = (float)(sapp_frame_duration() * 60.0);
/* compute viewport rectangles so that the views are horizontally
centered and keep a 1:1 aspect ratio
*/
const int dw = sapp_width();
const int dh = sapp_height();
const int ww = dh/2; // not a bug
const int hh = dh/2;
const int x0 = dw/2 - hh;
const int x1 = dw/2;
const int y0 = 0;
const int y1 = dh/2;
// all sokol-gl functions except sgl_draw() can be called anywhere in the frame
sgl_viewport(x0, y0, ww, hh, true);
draw_triangle();
sgl_viewport(x1, y0, ww, hh, true);
draw_quad(t);
sgl_viewport(x0, y1, ww, hh, true);
draw_cubes(t);
sgl_viewport(x1, y1, ww, hh, true);
draw_tex_cube(t);
sgl_viewport(0, 0, dw, dh, true);
/* Render the sokol-gfx default pass, all sokol-gl commands
that happened so far are rendered inside sgl_draw(), and this
is the only sokol-gl function that must be called inside
a sokol-gfx begin/end pass pair.
sgl_draw() also 'rewinds' sokol-gl for the next frame.
*/
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sgl_draw();
__dbgui_draw();
sg_end_pass();
sg_commit();
}
static void cleanup(void) {
__dbgui_shutdown();
sgl_shutdown();
sg_shutdown();
}
sapp_desc sokol_main(int argc, char* argv[]) {
(void)argc; (void)argv;
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = __dbgui_event,
.width = 512,
.height = 512,
.sample_count = 4,
.window_title = "sokol_gl.h (sokol-app)",
.icon.sokol_default = true,
.logger.func = slog_func,
};
}