3 #include "client/camera.h"
4 #include "client/client.h"
5 #include "client/scene.h"
6 #include "client/shader.h"
14 scene.objects = list_create(NULL),
15 pthread_mutex_init(&scene.mtx, NULL);
17 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
19 char max_texture_units_def[BUFSIZ];
20 sprintf(max_texture_units_def, "#define MAX_TEXTURE_UNITS %d\n", scene.max_texture_units);
22 if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog, max_texture_units_def)) {
23 fprintf(stderr, "Failed to create 3D shader program\n");
27 scene.loc_MVP = glGetUniformLocation(scene.prog, "MVP");
28 scene.loc_daylight = glGetUniformLocation(scene.prog, "daylight");
29 scene.loc_lightDir = glGetUniformLocation(scene.prog, "lightDir");
31 GLint texture_indices[scene.max_texture_units];
32 for (GLint i = 0; i < scene.max_texture_units; i++)
33 texture_indices[i] = i;
35 glProgramUniform1iv(scene.prog, glGetUniformLocation(scene.prog, "textures"), scene.max_texture_units, texture_indices);
38 scene.render_distance = 255.0f;
43 static void list_delete_object(void *key, unused void *value, unused void *arg)
50 list_clear_func(&scene.objects, &list_delete_object, NULL);
51 pthread_mutex_destroy(&scene.mtx);
52 glDeleteProgram(scene.prog);
55 void scene_add_object(Object *obj)
57 pthread_mutex_lock(&scene.mtx);
58 list_put(&scene.objects, obj, NULL);
59 pthread_mutex_unlock(&scene.mtx);
64 #pragma GCC diagnostic push
65 #pragma GCC diagnostic ignored "-Wpedantic"
67 mat4x4_mul(view_proj, scene.projection, camera.view);
69 vec4 base_sunlight_dir = {0.0f, 0.0f, -1.0f, 1.0f};
72 mat4x4_identity(sunlight_mat);
73 mat4x4_rotate(sunlight_mat, sunlight_mat, 1.0f, 0.0f, 0.0f, get_sun_angle() + M_PI / 2.0f);
74 mat4x4_mul_vec4(sunlight_dir, sunlight_mat, base_sunlight_dir);
75 #pragma GCC diagnostic pop
77 glUseProgram(scene.prog);
78 glProgramUniform3f(scene.prog, scene.loc_lightDir, sunlight_dir[0], sunlight_dir[1], sunlight_dir[2]);
79 glProgramUniform1f(scene.prog, scene.loc_daylight, get_daylight());
81 for (ListPair **pairptr = &scene.objects.first; *pairptr != NULL; ) {
82 ListPair *pair = *pairptr;
83 Object *obj = pair->key;
85 pthread_mutex_lock(&scene.mtx);
86 *pairptr = pair->next;
87 pthread_mutex_unlock(&scene.mtx);
91 object_render(obj, view_proj, scene.loc_MVP);
92 pairptr = &pair->next;
97 void scene_on_resize(int width, int height)
99 mat4x4_perspective(scene.projection, scene.fov / 180.0f * M_PI, (float) width / (float) height, 0.01f, scene.render_distance);
102 GLuint scene_get_max_texture_units()
104 return scene.max_texture_units;