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
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#define CGLM_FORCE_DEPTH_ZERO_TO_ONE
#include <cglm/cglm.h>
#include <cglm/struct.h>
#include "objload.h"
#define MONKEY
#include "graphics.h"
#include "window.h"
#include "common.h"
u32 width = 640;
u32 height = 480;
window_t window;
graphics_t graphics;
int _create_surface(VkInstance instance, VkSurfaceKHR *surface);
int update_ubo(void *uniform_buffer);
struct ubo {
mat4 model;
mat4 view;
mat4 proj;
};
#ifndef MONKEY
vertex_t vertices[] = {
{{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{ 0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}},
{{-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{ 0.5f, -0.5f, -0.5f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}},
};
#endif
u32 indices[] = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
};
int main(void)
{
int ret = 1;
if(SDL_Init(0)) {
err("SDL_Init: failed");
goto f1;
}
// populate window info
struct window_info win_info = {0};
win_info.title = "Test App";
win_info.flags = SDL_WINDOW_RESIZABLE;
win_info.w = width;
win_info.h = height;
// create window
window = window_create(&win_info);
if(!window) {
err("window_create: failed");
goto f2;
}
#ifdef MONKEY
vertex_t *objvertices;
size_t nvertices = 0;
// Load obj
FILE *fp = fopen("../files/monkey.obj", "r");
objload_t obj = {0};
obj_load(fp, &obj);
struct obj_struct_metadata obj_metadata = {
sizeof(vertex_t),
offsetof(vertex_t, pos),
offsetof(vertex_t, color), 0,
OBJ_STRUCT_VERT_EXISTS |
OBJ_STRUCT_NORM_EXISTS
};
obj_transfer_raw(obj, obj_metadata, &nvertices, NULL);
objvertices = xcalloc(nvertices, sizeof(*objvertices));
obj_transfer_raw(obj, obj_metadata, &nvertices, objvertices);
obj_free(obj);
fclose(fp);
#endif
// get extensions
unsigned int ext_count = 0;
window_extension_info(window, &ext_count, NULL);
char **extensions = xcalloc(ext_count, sizeof(char *));
window_extension_info(window, &ext_count, (const char **)extensions);
// populate the device info
struct graphics_info grph_info = {0};
grph_info.name = "Test App";
grph_info.version = MAKE_VERSION(1, 0, 0);
grph_info.ext_count = ext_count;
grph_info.extensions = (const char * const *)extensions;
#ifndef MONKEY
grph_info.vertices = vertices;
grph_info.nvertices = ARR_SIZE(vertices);
#else
grph_info.vertices = objvertices;
grph_info.nvertices = nvertices;
#endif
grph_info.indices = indices;
grph_info.nindices = ARR_SIZE(indices);
grph_info.ubo_size = sizeof(struct ubo);
grph_info.update_ubo = update_ubo;
grph_info.surface_func = _create_surface;
// create the device
graphics = graphics_create(&grph_info);
if(!graphics) {
err("device_create: failed");
if(extensions) free(extensions);
goto f3;
}
if(extensions) free(extensions);
int running = 1;
while(running) {
SDL_Event windowEvent;
while(SDL_PollEvent(&windowEvent))
if(windowEvent.type == SDL_QUIT) {
running = 0;
break;
}
#ifndef MONKEY
graphics_draw_frame(graphics, grph_info.nindices);
#else
graphics_draw_frame(graphics, grph_info.nvertices);
#endif
}
ret = 0;
#ifdef MONKEY
free(objvertices);
#endif
graphics_destroy(graphics);
f3: window_destroy(window);
f2: SDL_Quit();
f1: return ret;
}
int update_ubo(void *uniform_buffer)
{
struct ubo *ubo = (struct ubo *)uniform_buffer;
static float time = 1.0f;
time += 0.01f;
memcpy(ubo->model, glms_mat4_identity().raw, sizeof(ubo->model));
glm_rotate(ubo->model, time * glm_rad(90.0f),
(vec3){0.0f, 0.0f, 1.0f});
glm_lookat((vec3){2.0f, 2.0f, 0.0f}, (vec3){0.0f, 0.0f, 0.0f}, (vec3){0.0f, 0.0f, 1.0f}, ubo->view);
glm_perspective(glm_rad(45.0f), (float)width/(float)height, 0.1f, 10.0f, ubo->proj);
ubo->proj[1][1] *= -1;
return 0;
}
int _create_surface(VkInstance instance, VkSurfaceKHR *surface)
{
return window_create_surface(window, instance, surface);
}
|