-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube-sapp.c
290 lines (265 loc) · 9.15 KB
/
cube-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
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
//------------------------------------------------------------------------------
// shadows-sapp.c
// Render to an offscreen rendertarget texture, and use this texture
// for rendering shadows to the screen.
//------------------------------------------------------------------------------
#define SOKOL_IMPL
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_glue.h"
#define HANDMADE_MATH_IMPLEMENTATION
#define HANDMADE_MATH_NO_SSE
#include "HandmadeMath.h"
#if !defined(SOKOL_SHDC_ALIGN)
#if defined(_MSC_VER)
#define SOKOL_SHDC_ALIGN(a) __declspec(align(a))
#else
#define SOKOL_SHDC_ALIGN(a) __attribute__((aligned(a)))
#endif
#endif
#define ATTR_colorVS_position (0)
#define ATTR_colorVS_normal (1)
#define SLOT_vs_light_params (0)
#pragma pack(push,1)
SOKOL_SHDC_ALIGN(16) typedef struct vs_light_params_t {
hmm_mat4 model;
hmm_mat4 mvp;
hmm_vec3 diffColor;
uint8_t _pad_204[4];
} vs_light_params_t;
#pragma pack(pop)
#define SLOT_fs_light_params (0)
#pragma pack(push,1)
SOKOL_SHDC_ALIGN(16) typedef struct fs_light_params_t {
hmm_vec3 lightDir;
uint8_t _pad_28[4];
hmm_vec3 eyePos;
uint8_t _pad_44[4];
} fs_light_params_t;
#pragma pack(pop)
static inline const sg_shader_desc* color_shader_desc(sg_backend backend) {
if (backend == SG_BACKEND_GLCORE33) {
static sg_shader_desc desc;
static bool valid;
if (!valid) {
valid = true;
desc.attrs[0].name = "position";
desc.attrs[1].name = "normal";
desc.vs.source = "#version 330\n"
"\n"
"uniform vec4 vs_light_params[9];\n"
"layout(location = 0) in vec4 position;\n"
"out vec4 P;\n"
"out vec3 N;\n"
"layout(location = 1) in vec3 normal;\n"
"out vec3 color;\n"
"\n"
"void main()\n"
"{\n"
" mat4 mvp = mat4(vs_light_params[4], vs_light_params[5], vs_light_params[6], vs_light_params[7]);\n"
" gl_Position = mvp * position;\n"
" mat4 model = mat4(vs_light_params[0], vs_light_params[1], vs_light_params[2], vs_light_params[3]);\n"
" P = model * position;\n"
" N = (model * vec4(normal, 0.0)).xyz;\n"
" color = vs_light_params[8].xyz;\n"
"}\n"
;
desc.vs.entry = "main";
desc.vs.uniform_blocks[0].size = 144;
desc.vs.uniform_blocks[0].layout = SG_UNIFORMLAYOUT_STD140;
desc.vs.uniform_blocks[0].uniforms[0].name = "vs_light_params";
desc.vs.uniform_blocks[0].uniforms[0].type = SG_UNIFORMTYPE_FLOAT4;
desc.vs.uniform_blocks[0].uniforms[0].array_count = 9;
desc.fs.source = "#version 330\n"
"\n"
"uniform vec4 fs_light_params[2];\n"
"\n"
"in vec3 N;\n"
"in vec4 P;\n"
"layout(location = 0) out vec4 fragColor;\n"
"in vec3 color;\n"
"\n"
"vec4 linearToGamma(vec4 c)\n"
"{\n"
" return vec4(pow(c.xyz, vec3(1/2.2)), c.w);\n"
"}\n"
"\n"
"float gammaToLinear(float c)\n"
"{\n"
" return pow(c, 2.2);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" vec3 lightDir = normalize(fs_light_params[0].xyz);\n"
" vec3 normal = normalize(N);\n"
" float incidentLightFrac = dot(normal, lightDir);\n"
" if (incidentLightFrac > 0.0)\n"
" {\n"
" vec3 eye = fs_light_params[1].xyz;\n"
" float reflectedLightFrac = dot("
" reflect(-lightDir, normal),"
" normalize(eye - P.xyz)"
" );\n"
" fragColor = vec4("
" ("
" gammaToLinear(max(reflectedLightFrac, 0.0))"
" * incidentLightFrac"
" )"
" + (color * (incidentLightFrac + 0.25)),"
" 1.0"
" );\n"
" } else {\n"
" fragColor = vec4(color * 0.25, 1.0);\n"
" }\n"
" fragColor = linearToGamma(fragColor);\n"
"}\n"
;
desc.fs.entry = "main";
desc.fs.uniform_blocks[0].size = 32;
desc.fs.uniform_blocks[0].layout = SG_UNIFORMLAYOUT_STD140;
desc.fs.uniform_blocks[0].uniforms[0].name = "fs_light_params";
desc.fs.uniform_blocks[0].uniforms[0].type = SG_UNIFORMTYPE_FLOAT4;
desc.fs.uniform_blocks[0].uniforms[0].array_count = 2;
desc.label = "color_shader";
}
return &desc;
}
return 0;
}
#define SCREEN_SAMPLE_COUNT (4)
static struct {
struct {
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind;
} deflt;
float ry;
} state;
void init(void) {
sg_setup(&(sg_desc){
.context = sapp_sgcontext()
});
/* default pass action: clear to blue-ish */
state.deflt.pass_action = (sg_pass_action) {
.colors[0] = { .action = SG_ACTION_CLEAR, .value = { 0.0f, 0.25f, 1.0f, 1.0f } }
};
/* cube vertex buffer with positions & normals */
float vertices[] = {
/* pos normals */
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, //CUBE BACK FACE
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, //CUBE FRONT FACE
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, //CUBE LEFT FACE
-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, //CUBE RIGHT FACE
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, //CUBE BOTTOM FACE
-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, //CUBE TOP FACE
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
sg_buffer vbuf = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(vertices),
.label = "cube-vertices"
});
/* an index buffer for the cube */
uint16_t indices[] = {
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20,
};
sg_buffer ibuf = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SG_RANGE(indices),
.label = "cube-indices"
});
state.deflt.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
/* don't need to provide buffer stride or attr offsets, no gaps here */
.attrs = {
[ATTR_colorVS_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_colorVS_normal].format = SG_VERTEXFORMAT_FLOAT3
}
},
.shader = sg_make_shader(color_shader_desc(sg_query_backend())),
.index_type = SG_INDEXTYPE_UINT16,
/* Cull back faces when rendering to the screen */
.cull_mode = SG_CULLMODE_BACK,
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true
},
.label = "default-pipeline"
});
/* resource bindings to render the cube */
state.deflt.bind = (sg_bindings){
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
};
state.ry = 0.0f;
}
void frame(void) {
const float t = (float)(sapp_frame_duration() * 60.0);
state.ry += t;
/* Calculate matrices for shadow pass */
const hmm_mat4 rym = HMM_Rotate(state.ry, HMM_Vec3(0.0f,1.0f,0.0f));
const hmm_vec4 light_dir = HMM_MultiplyMat4ByVec4(rym, HMM_Vec4(50.0f,50.0f,-50.0f,0.0f));
/* Calculate matrices for camera pass */
const hmm_mat4 proj = HMM_Perspective(60.0f, sapp_widthf()/sapp_heightf(), 0.01f, 100.0f);
const hmm_mat4 view = HMM_LookAt(HMM_Vec3(5.0f, 5.0f, 5.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
const hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);
/* Initialise fragment uniforms for light shader */
const fs_light_params_t fs_light_params = {
.lightDir = HMM_NormalizeVec3(light_dir.XYZ),
.eyePos = HMM_Vec3(5.0f,5.0f,5.0f)
};
sg_begin_default_pass(&state.deflt.pass_action, sapp_width(), sapp_height());
sg_apply_pipeline(state.deflt.pip);
sg_apply_bindings(&state.deflt.bind);
sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_fs_light_params, &SG_RANGE(fs_light_params));
{
/* Render the cube in the light pass */
const vs_light_params_t vs_light_params = {
.model = HMM_Mat4d(1.0),
.mvp = view_proj,
.diffColor = HMM_Vec3(1.0f,1.0f,0.0f)
};
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_light_params, &SG_RANGE(vs_light_params));
sg_draw(0, 36, 1);
}
sg_end_pass();
sg_commit();
}
void cleanup(void) {
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,
.width = 800,
.height = 600,
.sample_count = SCREEN_SAMPLE_COUNT,
.window_title = "Shadow Rendering (sokol-app)",
.icon.sokol_default = true,
};
}