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
|
#include <stdio.h>
#include "render/window.h"
#include "render/renderer.h"
#include "render/mesh.h"
#define len(X) sizeof(X) / sizeof(*X)
int main() {
struct window *win = window_init("vk");
if (!win)
return -1;
struct renderer *ren = renderer_init(win);
if (!ren) {
fputs("failed to init renderer\n", stderr);
return -1;
}
struct vertex vertices[] = {
{ .position = { -0.25, -0.25, -0.25 }, .color = { 0.f, 0.f, 0.f, 0.5f } },
{ .position = { 0.25, -0.25, -0.25,}, .color = { 1.f, 0.f, 0.f, 0.5f } },
{ .position = { -0.25, 0.25, -0.25,}, .color = { 0.f, 1.f, 0.f, 0.5f } },
{ .position = { -0.25, 0.25, -0.25,}, .color = { 0.f, 1.f, 0.f, 0.5f } },
{ .position = { 0.25, -0.25, -0.25,}, .color = { 1.f, 0.f, 0.f, 0.5f } },
{ .position = { 0.25, 0.25, -0.25,}, .color = { 1.f, 1.f, 0.f, 0.5f } },
{ .position = { -0.25, -0.25, 0.25,}, .color = { 0.f, 0.f, 1.f, 0.5f } },
{ .position = { -0.25, 0.25, 0.25,}, .color = { 0.f, 1.f, 1.f, 0.5f } },
{ .position = { 0.25, -0.25, 0.25,}, .color = { 1.f, 0.f, 1.f, 0.5f } },
{ .position = { 0.25, -0.25, 0.25,}, .color = { 1.f, 0.f, 1.f, 0.5f } },
{ .position = { -0.25, 0.25, 0.25,}, .color = { 0.f, 1.f, 1.f, 0.5f } },
{ .position = { 0.25, 0.25, 0.25,}, .color = { 1.f, 1.f, 1.f, 0.5f } },
{ .position = { 0.25, 0.25, 0.25,}, .color = { 1.f, 1.f, 1.f, 0.5f } },
{ .position = { 0.25, 0.25, -0.25,}, .color = { 1.f, 1.f, 0.f, 0.5f } },
{ .position = { 0.25, -0.25, 0.25,}, .color = { 1.f, 0.f, 1.f, 0.5f } },
{ .position = { 0.25, -0.25, 0.25,}, .color = { 1.f, 0.f, 1.f, 0.5f } },
{ .position = { 0.25, 0.25, -0.25,}, .color = { 1.f, 1.f, 0.f, 0.5f } },
{ .position = { 0.25, -0.25, -0.25,}, .color = { 1.f, 0.f, 0.f, 0.5f } },
{ .position = { -0.25, 0.25, 0.25,}, .color = { 0.f, 1.f, 1.f, 0.5f } },
{ .position = { -0.25, -0.25, 0.25,}, .color = { 0.f, 0.f, 1.f, 0.5f } },
{ .position = { -0.25, 0.25, -0.25,}, .color = { 0.f, 1.f, 0.f, 0.5f } },
{ .position = { -0.25, 0.25, -0.25,}, .color = { 0.f, 1.f, 0.f, 0.5f } },
{ .position = { -0.25, -0.25, 0.25,}, .color = { 0.f, 0.f, 1.f, 0.5f } },
{ .position = { -0.25, -0.25, -0.25,}, .color = { 0.f, 0.f, 0.f, 0.5f } },
{ .position = { 0.25, 0.25, 0.25,}, .color = { 1.f, 1.f, 1.f, 0.5f } },
{ .position = { -0.25, 0.25, 0.25,}, .color = { 0.f, 1.f, 1.f, 0.5f } },
{ .position = { 0.25, 0.25, -0.25,}, .color = { 1.f, 1.f, 0.f, 0.5f } },
{ .position = { 0.25, 0.25, -0.25,}, .color = { 1.f, 1.f, 0.f, 0.5f } },
{ .position = { -0.25, 0.25, 0.25,}, .color = { 0.f, 1.f, 1.f, 0.5f } },
{ .position = { -0.25, 0.25, -0.25,}, .color = { 0.f, 1.f, 0.f, 0.5f } },
{ .position = { 0.25, -0.25, 0.25,}, .color = { 1.f, 0.f, 1.f, 0.5f } },
{ .position = { 0.25, -0.25, -0.25,}, .color = { 1.f, 0.f, 0.f, 0.5f } },
{ .position = { -0.25, -0.25, 0.25,}, .color = { 0.f, 0.f, 1.f, 0.5f } },
{ .position = { -0.25, -0.25, 0.25,}, .color = { 0.f, 0.f, 1.f, 0.5f } },
{ .position = { 0.25, -0.25, -0.25,}, .color = { 1.f, 0.f, 0.f, 0.5f } },
{ .position = { -0.25, -0.25, -0.25,}, .color = { 0.f, 0.f, 0.f, 0.5f } },
};
uint32_t indices[len(vertices)];
for (size_t i = 0; i < len(vertices); i++)
indices[i] = i;
struct mesh meshes[] = {
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 1, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {0, 1, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 0, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 1, 0}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 0, 0}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {0, 0, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {0.5, 0, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 1, 0.5}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 1, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 0.5, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 1, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {0.5, 1, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 1, 1}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {2, 1, 3}),
upload_mesh(ren, len(vertices), vertices, len(indices), indices, (struct vec3) {1, 0.5, 1}),
};
while (!win->should_close) {
renderer_draw(ren, len(meshes), meshes);
wl_display_roundtrip(win->dpy);
}
for (size_t i = 0; i < len(meshes); i++)
mesh_destroy(ren, &meshes[i]);
renderer_destroy(ren);
window_destroy(win);
return 0;
}
|