3 #include "client/frustum.h"
4 #include "client/object.h"
5 #include "client/scene.h"
6 #define OBJECT_VERTEX_ATTRIBUTES 5
8 static VertexAttribute vertex_attributes[OBJECT_VERTEX_ATTRIBUTES] = {
13 .size = sizeof(Vertex3DPosition),
19 .size = sizeof(Vertex3DNormal),
25 .size = sizeof(Vertex3DTextureIndex),
31 .size = sizeof(Vertex3DTextureCoordinates),
38 .size = sizeof(Vertex3DColor),
42 static VertexLayout vertex_layout = {
43 .attributes = vertex_attributes,
44 .count = OBJECT_VERTEX_ATTRIBUTES,
45 .size = sizeof(Vertex3D),
48 Object *object_create()
50 Object *obj = malloc(sizeof(Object));
51 obj->pos = (v3f32) {0.0f, 0.0f, 0.0f};
52 obj->rot = (v3f32) {0.0f, 0.0f, 0.0f};
53 obj->scale = (v3f32) {1.0f, 1.0f, 1.0f};
57 obj->meshes_count = 0;
59 obj->wireframe = false;
60 obj->frustum_culling = false;
61 obj->transparent = false;
62 obj->box = (aabb3f32) {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
63 obj->current_face = NULL;
64 obj->faces = array_create(sizeof(ObjectFace));
65 obj->on_render = NULL;
71 void object_delete(Object *obj)
73 for (size_t i = 0; i < obj->meshes_count; i++)
74 mesh_delete(obj->meshes[i]);
79 void object_set_texture(Object *obj, Texture *texture)
81 if (obj->current_face && obj->current_face->texture == texture->id)
85 .texture = texture->id,
86 .vertices = array_create(sizeof(Vertex3D)),
89 array_append(&obj->faces, &face);
90 obj->current_face = &((ObjectFace *) obj->faces.ptr)[obj->faces.siz - 1];
93 void object_add_vertex(Object *obj, Vertex3D *vertex)
95 array_append(&obj->current_face->vertices, vertex);
98 static int qsort_compare_faces(const void *f1, const void *f2)
100 return ((ObjectFace *) f1)->texture - ((ObjectFace *) f2)->texture;
103 static void add_mesh(Array *meshes, Array *vertices, Array *textures)
105 if (vertices->siz > 0) {
106 Mesh *mesh = mesh_create();
107 mesh->vertices = vertices->ptr;
108 mesh->vertices_count = vertices->siz;
109 mesh->free_vertices = true;
110 mesh->free_textures = true;
111 mesh->layout = &vertex_layout;
112 size_t textures_count;
113 array_copy(textures, (void *) &mesh->textures, &textures_count);
114 mesh->textures_count = textures_count;
118 array_append(meshes, &mesh);
121 *vertices = array_create(sizeof(Vertex3D));
122 *textures = array_create(sizeof(GLuint));
125 bool object_add_to_scene(Object *obj)
127 if (obj->faces.siz == 0)
130 object_transform(obj);
132 qsort(obj->faces.ptr, obj->faces.siz, sizeof(ObjectFace), &qsort_compare_faces);
134 GLuint max_texture_units = scene_get_max_texture_units();
136 Array meshes = array_create(sizeof(Mesh *));
138 Array vertices = array_create(sizeof(Vertex3D));
139 Array textures = array_create(sizeof(GLuint));
141 GLuint texture_id = 0;
142 GLuint texture_index = max_texture_units;
144 for (size_t f = 0; f < obj->faces.siz; f++) {
145 ObjectFace *face = &((ObjectFace *) obj->faces.ptr)[f];
147 if (face->texture != texture_id) {
148 if (++texture_index >= max_texture_units) {
149 add_mesh(&meshes, &vertices, &textures);
153 texture_id = face->texture;
154 array_append(&textures, &texture_id);
157 for (size_t v = 0; v < face->vertices.siz; v++) {
158 Vertex3D *vertex = &((Vertex3D *) face->vertices.ptr)[v];
159 vertex->textureIndex = texture_index;
160 array_append(&vertices, vertex);
163 free(face->vertices.ptr);
165 add_mesh(&meshes, &vertices, &textures);
166 free(obj->faces.ptr);
168 array_copy(&meshes, (void *) &obj->meshes, &obj->meshes_count);
171 scene_add_object(obj);
176 void object_transform(Object *obj)
178 mat4x4_translate(obj->transform, obj->pos.x, obj->pos.y, obj->pos.z);
179 #pragma GCC diagnostic push
180 #pragma GCC diagnostic ignored "-Wpedantic"
181 mat4x4_rotate(obj->transform, obj->transform, obj->rot.x, obj->rot.y, obj->rot.z, obj->angle);
182 mat4x4_scale_aniso(obj->transform, obj->transform, obj->scale.x, obj->scale.y, obj->scale.z);
183 #pragma GCC diagnostic pop
186 bool object_before_render(Object *obj, f64 dtime)
189 obj->on_render(obj, dtime);
194 if (obj->frustum_culling) {
195 aabb3f32 box = {v3f32_add(obj->box.min, obj->pos), v3f32_add(obj->box.max, obj->pos)};
197 if (! frustum_is_visible(box))
204 void object_render(Object *obj)
206 glUniformMatrix4fv(scene.loc_model, 1, GL_FALSE, obj->transform[0]);
209 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
211 for (size_t i = 0; i < obj->meshes_count; i++)
212 mesh_render(obj->meshes[i]);
215 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);