]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/object.c
Add fog and face culling
[dragonblocks_alpha.git] / src / client / object.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "client/object.h"
4 #include "client/scene.h"
5 #define OBJECT_VERTEX_ATTRIBUTES 5
6
7 static VertexAttribute vertex_attributes[OBJECT_VERTEX_ATTRIBUTES] = {
8         // position
9         {
10                 .type = GL_FLOAT,
11                 .length = 3,
12                 .size = sizeof(Vertex3DPosition),
13         },
14         // normal
15         {
16                 .type = GL_FLOAT,
17                 .length = 3,
18                 .size = sizeof(Vertex3DNormal),
19         },
20         // textureIndex
21         {
22                 .type = GL_FLOAT,
23                 .length = 1,
24                 .size = sizeof(Vertex3DTextureIndex),
25         },
26         // textureCoordinates
27         {
28                 .type = GL_FLOAT,
29                 .length = 2,
30                 .size = sizeof(Vertex3DTextureCoordinates),
31
32         },
33         // color
34         {
35                 .type = GL_FLOAT,
36                 .length = 3,
37                 .size = sizeof(Vertex3DColor),
38         },
39 };
40
41 static VertexLayout vertex_layout = {
42         .attributes = vertex_attributes,
43         .count = OBJECT_VERTEX_ATTRIBUTES,
44         .size = sizeof(Vertex3D),
45 };
46
47 Object *object_create()
48 {
49         Object *obj = malloc(sizeof(Object));
50         obj->pos = (v3f32) {0.0f, 0.0f, 0.0f};
51         obj->rot = (v3f32) {0.0f, 0.0f, 0.0f};
52         obj->scale = (v3f32) {1.0f, 1.0f, 1.0f};
53         obj->angle = 0.0f;
54         obj->remove = false;
55         obj->meshes = NULL;
56         obj->meshes_count = 0;
57         obj->visible = true;
58         obj->wireframe = false;
59         obj->box = (aabb3f32) {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
60         obj->frustum_culling = false;
61         obj->current_face = NULL;
62         obj->faces = array_create(sizeof(ObjectFace));
63
64         return obj;
65 }
66
67 void object_delete(Object *obj)
68 {
69         for (size_t i = 0; i < obj->meshes_count; i++)
70                 mesh_delete(obj->meshes[i]);
71
72         free(obj);
73 }
74
75 void object_set_texture(Object *obj, Texture *texture)
76 {
77         if (obj->current_face && obj->current_face->texture == texture->id)
78                 return;
79
80         ObjectFace face = {
81                 .texture = texture->id,
82                 .vertices = array_create(sizeof(Vertex3D)),
83         };
84
85         array_append(&obj->faces, &face);
86         obj->current_face = &((ObjectFace *) obj->faces.ptr)[obj->faces.siz - 1];
87 }
88
89 void object_add_vertex(Object *obj, Vertex3D *vertex)
90 {
91         array_append(&obj->current_face->vertices, vertex);
92 }
93
94 static int qsort_compare_faces(const void *f1, const void *f2)
95 {
96         return ((ObjectFace *) f1)->texture - ((ObjectFace *) f2)->texture;
97 }
98
99 static void add_mesh(Array *meshes, Array *vertices, Array *textures)
100 {
101         if (vertices->siz > 0) {
102                 Mesh *mesh = mesh_create();
103                 mesh->vertices = vertices->ptr;
104                 mesh->vertices_count = vertices->siz;
105                 mesh->free_vertices = true;
106                 mesh->free_textures = true;
107                 mesh->layout = &vertex_layout;
108                 size_t textures_count;
109                 array_copy(textures, (void *) &mesh->textures, &textures_count);
110                 mesh->textures_count = textures_count;
111
112                 free(textures->ptr);
113
114                 array_append(meshes, &mesh);
115         }
116
117         *vertices = array_create(sizeof(Vertex3D));
118         *textures = array_create(sizeof(GLuint));
119 }
120
121 bool object_add_to_scene(Object *obj)
122 {
123         if (obj->faces.siz == 0)
124                 return false;
125
126         object_transform(obj);
127
128         qsort(obj->faces.ptr, obj->faces.siz, sizeof(ObjectFace), &qsort_compare_faces);
129
130         GLuint max_texture_units = scene_get_max_texture_units();
131
132         Array meshes = array_create(sizeof(Mesh *));
133
134         Array vertices = array_create(sizeof(Vertex3D));
135         Array textures = array_create(sizeof(GLuint));
136
137         GLuint texture_id = 0;
138         GLuint texture_index = max_texture_units;
139
140         for (size_t f = 0; f < obj->faces.siz; f++) {
141                 ObjectFace *face = &((ObjectFace *) obj->faces.ptr)[f];
142
143                 if (face->texture != texture_id) {
144                         if (++texture_index >= max_texture_units) {
145                                 add_mesh(&meshes, &vertices, &textures);
146                                 texture_index = 0;
147                         }
148
149                         texture_id = face->texture;
150                         array_append(&textures, &texture_id);
151                 }
152
153                 for (size_t v = 0; v < face->vertices.siz; v++) {
154                         Vertex3D *vertex = &((Vertex3D *) face->vertices.ptr)[v];
155                         vertex->textureIndex = texture_index;
156                         array_append(&vertices, vertex);
157                 }
158
159                 free(face->vertices.ptr);
160         }
161         add_mesh(&meshes, &vertices, &textures);
162         free(obj->faces.ptr);
163
164         array_copy(&meshes, (void *) &obj->meshes, &obj->meshes_count);
165         free(meshes.ptr);
166
167         scene_add_object(obj);
168
169         return true;
170 }
171
172 void object_transform(Object *obj)
173 {
174         mat4x4_translate(obj->transform, obj->pos.x, obj->pos.y, obj->pos.z);
175 #pragma GCC diagnostic push
176 #pragma GCC diagnostic ignored "-Wpedantic"
177         mat4x4_rotate(obj->transform, obj->transform, obj->rot.x, obj->rot.y, obj->rot.z, obj->angle);
178         mat4x4_scale_aniso(obj->transform, obj->transform, obj->scale.x, obj->scale.y, obj->scale.z);
179 #pragma GCC diagnostic pop
180 }
181 #pragma GCC diagnostic push
182 #pragma GCC diagnostic ignored "-Wpedantic"
183 static bool inside_frustum(aabb3f32 box, mat4x4 MVP)
184 {
185         for (int x = 0; x <= 1; x++) {
186                 for (int y = 0; y <= 1; y++) {
187                         for (int z = 0; z <= 1; z++) {
188                                 vec4 point = {x ? box.min.x : box.max.x, y ? box.min.y : box.max.y, z ? box.min.z : box.max.z, 1.0};
189                                 vec4 point_NDC;
190                                 mat4x4_mul_vec4(point_NDC, MVP, point);
191
192                                 for (int j = 0; j < 3; j++) {
193                                         float f = point_NDC[j];
194
195                                         if (f > -1.0f && f > 1.0f)
196                                                 return true;
197                                 }
198                         }
199                 }
200         }
201
202         return false;
203 }
204 #pragma GCC diagnostic pop
205
206
207 void object_render(Object *obj)
208 {
209         if (! obj->visible)
210                 return;
211
212         if (obj->frustum_culling) {
213                 mat4x4 MVP;
214
215 #pragma GCC diagnostic push
216 #pragma GCC diagnostic ignored "-Wpedantic"
217                 mat4x4_mul(MVP, scene.VP, obj->transform);
218 #pragma GCC diagnostic pop
219
220                  if (! inside_frustum(obj->box, MVP))
221                         return;
222         }
223
224         glUniformMatrix4fv(scene.loc_model, 1, GL_FALSE, obj->transform[0]);
225
226         if (obj->wireframe)
227                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
228
229         for (size_t i = 0; i < obj->meshes_count; i++)
230                 mesh_render(obj->meshes[i]);
231
232         if (obj->wireframe)
233                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
234 }