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
|
#include "model.h"
#include "gldefs.h"
#include "macros.h"
#include "sys.h"
#include <string.h>
mat
get_model_matrix(v3 pos, v3 angles, v3 scale)
{
mat result = make_scale(scale);
result = rotate_mat(result, angles);
result = translate_mat(result, pos);
return result;
/*
mat translate = make_scale(transform.scale);
mat rotate = make_rotate(transform.scale);
mat scale = make_translate(transform.scale);
return mulmat(mulmat(translate, rotate), scale);
*/
}
static void
init_mesh_buffers(struct mesh *mesh)
{
gl_gen_vertex_arrays(1, &mesh->vao);
gl_gen_buffers(1, &mesh->vbo);
gl_gen_buffers(1, &mesh->ebo);
gl_bind_vertex_array(mesh->vao);
gl_bind_buffer(gl_array_buffer, mesh->vbo);
gl_bind_buffer(gl_element_array_buffer, mesh->ebo);
u64 size = mesh->nvertices * sizeof(struct vertex);
gl_buffer_data(gl_array_buffer, size, mesh->vertices, gl_static_draw);
size = mesh->nindices * sizeof(u32);
gl_buffer_data(gl_element_array_buffer, size, mesh->indices, gl_static_draw);
u64 vertex_size = sizeof(struct vertex);
gl_enable_vertex_attrib_array(pos_loc);
gl_vertex_attrib_pointer(pos_loc, 3, gl_float, 0, vertex_size,
(void *)offsetof(struct vertex, pos));
gl_enable_vertex_attrib_array(norm_loc);
gl_vertex_attrib_pointer(norm_loc, 3, gl_float, 0, vertex_size,
(void *)offsetof(struct vertex, norm));
gl_enable_vertex_attrib_array(texc_loc);
gl_vertex_attrib_pointer(texc_loc, 2, gl_float, 0, vertex_size,
(void*)offsetof(struct vertex, texc));
gl_bind_buffer(gl_element_array_buffer, 0);
gl_bind_buffer(gl_array_buffer, 0);
gl_bind_vertex_array(0);
}
struct mesh
init_mesh(v3 pos, v3 angles, v3 scale,
i32 nvertices, struct vertex *vertices,
i32 nindices, u32 *indices, struct texture *texture)
{
struct mesh mesh = {0};
mesh.pos = pos;
mesh.angles = angles;
mesh.scale = scale;
mesh.nvertices = nvertices;
mesh.vertices = vertices;
mesh.nindices = nindices;
mesh.indices = indices;
mesh.texture = texture;
init_mesh_buffers(&mesh);
return mesh;
}
void
clear_mesh(struct mesh *mesh)
{
gl_bind_vertex_array(mesh->vao);
gl_disable_vertex_attrib_array(pos_loc);
gl_disable_vertex_attrib_array(norm_loc);
gl_disable_vertex_attrib_array(texc_loc);
gl_bind_vertex_array(0);
gl_delete_vertex_arrays(1, &mesh->vao);
gl_delete_buffers(1, &mesh->vbo);
gl_delete_buffers(1, &mesh->ebo);
}
struct mesh
gen_quad(struct arena *arena, v3 pos, v3 angles, v3 scale, f32 width, f32 height)
{
if (width < 0.1f)
info("quad width less than 0.1... no");
width = max(0.1f, width);
if (height < 0.1f)
info("quad height less than 0.1... no");
height = max(0.1f, height);
if (scale.x < 0.1f || scale.y < 0.1f || scale.z < 0.1f)
info("quad scale less than 0.1... no");
scale = maxv3((v3){0.1f, 0.1f, 0.1f}, scale);
i32 nvertices = 4;
u64 size = nvertices * sizeof(struct vertex);
struct vertex *vertices = push_arena(arena, size);
memset(vertices, 0, size);
vertices[0].pos = (v3){-width/2.0f, -height/2.0f, 0.0f};
vertices[1].pos = (v3){-width/2.0f, height/2.0f, 0.0f};
vertices[1].texc = (v2){0.0f, height};
vertices[2].pos = (v3){width/2.0f, -height/2.0f, 0.0f};
vertices[2].texc = (v2){width, 0.0f};
vertices[3].pos = (v3){width/2.0f, height/2.0f, 0.0f};
vertices[3].texc = (v2){width, height};
i32 nindices = 6;
size = nindices * sizeof(u32);
u32 *indices = push_arena(arena, size);
indices[0] = 0;
indices[1] = 1;
indices[2] = 3;
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;
return init_mesh(pos, angles, scale, nvertices, vertices, nindices, indices, 0);
}
struct model
init_model(v3 pos, v3 angles, v3 scale, i32 nmeshes, struct mesh *meshes)
{
struct model model = {0};
model.pos = pos;
model.angles = angles;
model.scale = scale;
model.nmeshes = nmeshes;
model.meshes = meshes;
return model;
}
|