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_model = glGetUniformLocation(scene.prog, "model");
28 scene.loc_VP = glGetUniformLocation(scene.prog, "VP");
29 scene.loc_daylight = glGetUniformLocation(scene.prog, "daylight");
30 scene.loc_lightDir = glGetUniformLocation(scene.prog, "lightDir");
31 scene.loc_cameraPos = glGetUniformLocation(scene.prog, "cameraPos");
33 GLint texture_indices[scene.max_texture_units];
34 for (GLint i = 0; i < scene.max_texture_units; i++)
35 texture_indices[i] = i;
37 glProgramUniform1iv(scene.prog, glGetUniformLocation(scene.prog, "textures"), scene.max_texture_units, texture_indices);
40 scene.render_distance = 255.0f + 32.0f;
45 static void list_delete_object(void *key, unused void *value, unused void *arg)
52 list_clear_func(&scene.objects, &list_delete_object, NULL);
53 pthread_mutex_destroy(&scene.mtx);
54 glDeleteProgram(scene.prog);
57 void scene_add_object(Object *obj)
59 pthread_mutex_lock(&scene.mtx);
60 list_put(&scene.objects, obj, NULL);
61 pthread_mutex_unlock(&scene.mtx);
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wpedantic"
68 mat4x4_mul(scene.VP, scene.projection, camera.view);
70 vec4 base_sunlight_dir = {0.0f, 0.0f, -1.0f, 1.0f};
73 mat4x4_identity(sunlight_mat);
74 mat4x4_rotate(sunlight_mat, sunlight_mat, 1.0f, 0.0f, 0.0f, get_sun_angle() + M_PI / 2.0f);
75 mat4x4_mul_vec4(sunlight_dir, sunlight_mat, base_sunlight_dir);
76 #pragma GCC diagnostic pop
78 glUseProgram(scene.prog);
79 glUniformMatrix4fv(scene.loc_VP, 1, GL_FALSE, scene.VP[0]);
80 glUniform3f(scene.loc_lightDir, sunlight_dir[0], sunlight_dir[1], sunlight_dir[2]);
81 glUniform3f(scene.loc_cameraPos, camera.eye[0], camera.eye[1], camera.eye[2]);
82 glUniform1f(scene.loc_daylight, get_daylight());
84 for (ListPair **pairptr = &scene.objects.first; *pairptr != NULL; ) {
85 ListPair *pair = *pairptr;
86 Object *obj = pair->key;
88 pthread_mutex_lock(&scene.mtx);
89 *pairptr = pair->next;
90 pthread_mutex_unlock(&scene.mtx);
95 pairptr = &pair->next;
100 void scene_on_resize(int width, int height)
102 mat4x4_perspective(scene.projection, scene.fov / 180.0f * M_PI, (float) width / (float) height, 0.01f, scene.render_distance);
105 GLuint scene_get_max_texture_units()
107 return scene.max_texture_units;