3 #include "client/object.h"
4 #include "client/scene.h"
5 #define OBJECT_VERTEX_ATTRIBUTES 5
7 static VertexAttribute vertex_attributes[OBJECT_VERTEX_ATTRIBUTES] = {
12 .size = sizeof(Vertex3DPosition),
18 .size = sizeof(Vertex3DNormal),
24 .size = sizeof(Vertex3DTextureIndex),
30 .size = sizeof(Vertex3DTextureCoordinates),
37 .size = sizeof(Vertex3DColor),
41 static VertexLayout vertex_layout = {
42 .attributes = vertex_attributes,
43 .count = OBJECT_VERTEX_ATTRIBUTES,
44 .size = sizeof(Vertex3D),
47 Object *object_create()
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};
56 obj->meshes_count = 0;
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));
67 void object_delete(Object *obj)
69 for (size_t i = 0; i < obj->meshes_count; i++)
70 mesh_delete(obj->meshes[i]);
75 void object_set_texture(Object *obj, Texture *texture)
77 if (obj->current_face && obj->current_face->texture == texture->id)
81 .texture = texture->id,
82 .vertices = array_create(sizeof(Vertex3D)),
85 array_append(&obj->faces, &face);
86 obj->current_face = &((ObjectFace *) obj->faces.ptr)[obj->faces.siz - 1];
89 void object_add_vertex(Object *obj, Vertex3D *vertex)
91 array_append(&obj->current_face->vertices, vertex);
94 static int qsort_compare_faces(const void *f1, const void *f2)
96 return ((ObjectFace *) f1)->texture - ((ObjectFace *) f2)->texture;
99 static void add_mesh(Array *meshes, Array *vertices, Array *textures)
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;
114 array_append(meshes, &mesh);
117 *vertices = array_create(sizeof(Vertex3D));
118 *textures = array_create(sizeof(GLuint));
121 bool object_add_to_scene(Object *obj)
123 if (obj->faces.siz == 0)
126 object_transform(obj);
128 qsort(obj->faces.ptr, obj->faces.siz, sizeof(ObjectFace), &qsort_compare_faces);
130 GLuint max_texture_units = scene_get_max_texture_units();
132 Array meshes = array_create(sizeof(Mesh *));
134 Array vertices = array_create(sizeof(Vertex3D));
135 Array textures = array_create(sizeof(GLuint));
137 GLuint texture_id = 0;
138 GLuint texture_index = max_texture_units;
140 for (size_t f = 0; f < obj->faces.siz; f++) {
141 ObjectFace *face = &((ObjectFace *) obj->faces.ptr)[f];
143 if (face->texture != texture_id) {
144 if (++texture_index >= max_texture_units) {
145 add_mesh(&meshes, &vertices, &textures);
149 texture_id = face->texture;
150 array_append(&textures, &texture_id);
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);
159 free(face->vertices.ptr);
161 add_mesh(&meshes, &vertices, &textures);
162 free(obj->faces.ptr);
164 array_copy(&meshes, (void *) &obj->meshes, &obj->meshes_count);
167 scene_add_object(obj);
172 void object_transform(Object *obj)
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
181 #pragma GCC diagnostic push
182 #pragma GCC diagnostic ignored "-Wpedantic"
183 static bool inside_frustum(aabb3f32 box, mat4x4 MVP)
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};
190 mat4x4_mul_vec4(point_NDC, MVP, point);
192 for (int j = 0; j < 3; j++) {
193 float f = point_NDC[j];
195 if (f > -1.0f && f > 1.0f)
204 #pragma GCC diagnostic pop
207 void object_render(Object *obj)
212 if (obj->frustum_culling) {
215 #pragma GCC diagnostic push
216 #pragma GCC diagnostic ignored "-Wpedantic"
217 mat4x4_mul(MVP, scene.VP, obj->transform);
218 #pragma GCC diagnostic pop
220 if (! inside_frustum(obj->box, MVP))
224 glUniformMatrix4fv(scene.loc_model, 1, GL_FALSE, obj->transform[0]);
227 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
229 for (size_t i = 0; i < obj->meshes_count; i++)
230 mesh_render(obj->meshes[i]);
233 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);