forked from GeertArien/learnopengl-examples
-
Notifications
You must be signed in to change notification settings - Fork 4
/
1-instancing.c
109 lines (94 loc) · 3.08 KB
/
1-instancing.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
//------------------------------------------------------------------------------
// Instancing (1)
//------------------------------------------------------------------------------
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_glue.h"
#include "HandmadeMath.h"
#include "1-instancing.glsl.h"
#define LOPGL_APP_IMPL
#include "../lopgl_app.h"
#include "string.h"
/* application state */
static struct {
sg_pipeline pip;
sg_bindings bind;
sg_pass_action pass_action;
HMM_Vec4 translations[100]; // using arrays vec4 to avoid alignment issues with cross shader compilation
} state;
static void init(void) {
lopgl_setup();
/* create shader from code-generated sg_shader_desc */
sg_shader shd = sg_make_shader(simple_shader_desc(sg_query_backend()));
float vertices[] = {
// positions // colors
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
-0.05f, -0.05f, 0.0f, 0.0f, 1.0f,
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
0.05f, 0.05f, 0.0f, 1.0f, 1.0f
};
state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(vertices),
.data = SG_RANGE(vertices),
.label = "quad-vertices"
});
/* a pipeline state object */
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = shd,
.layout = {
.attrs = {
[ATTR_vs_aPos].format = SG_VERTEXFORMAT_FLOAT2,
[ATTR_vs_aColor].format = SG_VERTEXFORMAT_FLOAT3
}
},
.label = "quad-pipeline"
});
/* a pass action to clear framebuffer */
state.pass_action = (sg_pass_action) {
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.1f, 0.1f, 0.1f, 1.0f} }
};
int index = 0;
float offset = 0.1f;
for(int y = -10; y < 10; y += 2) {
for(int x = -10; x < 10; x += 2) {
float x_pos = (float)x / 10.0f + offset;
float y_pos = (float)y / 10.0f + offset;
state.translations[index++] = HMM_V4(x_pos, y_pos, 0.0, 0.0);
}
}
}
void frame(void) {
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
vs_params_t vs_params;
memcpy(vs_params.offsets, state.translations, sizeof(vs_params.offsets));
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 6, 100);
sg_end_pass();
sg_commit();
}
void cleanup(void) {
sg_shutdown();
}
void event(const sapp_event* e) {
if (e->type == SAPP_EVENTTYPE_KEY_DOWN) {
if (e->key_code == SAPP_KEYCODE_ESCAPE) {
sapp_request_quit();
}
}
}
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.width = 800,
.height = 600,
.high_dpi = true,
.window_title = "Instancing (LearnOpenGL)",
};
}