]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/scene.c
979bdcca59e7b672ff61f575760b942155b99848
[dragonblocks_alpha.git] / src / client / scene.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include "client/camera.h"
4 #include "client/client.h"
5 #include "client/scene.h"
6 #include "client/shader.h"
7 #include "day.h"
8 #include "util.h"
9
10 struct Scene scene;
11
12 bool scene_init()
13 {
14         scene.objects = list_create(NULL),
15         pthread_mutex_init(&scene.mtx, NULL);
16
17         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
18
19         char max_texture_units_def[BUFSIZ];
20         sprintf(max_texture_units_def, "#define MAX_TEXTURE_UNITS %d\n", scene.max_texture_units);
21
22         if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog, max_texture_units_def)) {
23                 fprintf(stderr, "Failed to create 3D shader program\n");
24                 return false;
25         }
26
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");
32
33         GLint texture_indices[scene.max_texture_units];
34         for (GLint i = 0; i < scene.max_texture_units; i++)
35                 texture_indices[i] = i;
36
37         glProgramUniform1iv(scene.prog, glGetUniformLocation(scene.prog, "textures"), scene.max_texture_units, texture_indices);
38
39         scene.fov = 86.1f;
40         scene.render_distance = 255.0f + 32.0f;
41
42         return true;
43 }
44
45 static void list_delete_object(void *key, unused void *value, unused void *arg)
46 {
47         object_delete(key);
48 }
49
50 void scene_deinit()
51 {
52         list_clear_func(&scene.objects, &list_delete_object, NULL);
53         pthread_mutex_destroy(&scene.mtx);
54         glDeleteProgram(scene.prog);
55 }
56
57 void scene_add_object(Object *obj)
58 {
59         pthread_mutex_lock(&scene.mtx);
60         list_put(&scene.objects, obj, NULL);
61         pthread_mutex_unlock(&scene.mtx);
62 }
63
64 void scene_render(f64 dtime)
65 {
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wpedantic"
68         mat4x4_mul(scene.VP, scene.projection, camera.view);
69
70         vec4 base_sunlight_dir = {0.0f, 0.0f, -1.0f, 1.0f};
71         vec4 sunlight_dir;
72         mat4x4 sunlight_mat;
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
77
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());
83
84         for (ListPair **pairptr = &scene.objects.first; *pairptr != NULL; ) {
85                 ListPair *pair = *pairptr;
86                 Object *obj = pair->key;
87                 if (obj->remove) {
88                         pthread_mutex_lock(&scene.mtx);
89                         *pairptr = pair->next;
90                         pthread_mutex_unlock(&scene.mtx);
91                         free(pair);
92                         object_delete(obj);
93                 } else {
94                         object_render(obj, dtime);
95                         pairptr = &pair->next;
96                 }
97         }
98 }
99
100 void scene_on_resize(int width, int height)
101 {
102         mat4x4_perspective(scene.projection, scene.fov / 180.0f * M_PI, (float) width / (float) height, 0.01f, scene.render_distance);
103 }
104
105 GLuint scene_get_max_texture_units()
106 {
107         return scene.max_texture_units;
108 }