]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/scene.c
Add diffuse lighting, improve skybox and add timelapse
[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_MVP = glGetUniformLocation(scene.prog, "MVP");
28         scene.loc_daylight = glGetUniformLocation(scene.prog, "daylight");
29         scene.loc_lightDir = glGetUniformLocation(scene.prog, "lightDir");
30
31         GLint texture_indices[scene.max_texture_units];
32         for (GLint i = 0; i < scene.max_texture_units; i++)
33                 texture_indices[i] = i;
34
35         glProgramUniform1iv(scene.prog, glGetUniformLocation(scene.prog, "textures"), scene.max_texture_units, texture_indices);
36
37         scene.fov = 86.1f;
38         scene.render_distance = 255.0f;
39
40         return true;
41 }
42
43 static void list_delete_object(void *key, unused void *value, unused void *arg)
44 {
45         object_delete(key);
46 }
47
48 void scene_deinit()
49 {
50         list_clear_func(&scene.objects, &list_delete_object, NULL);
51         pthread_mutex_destroy(&scene.mtx);
52         glDeleteProgram(scene.prog);
53 }
54
55 void scene_add_object(Object *obj)
56 {
57         pthread_mutex_lock(&scene.mtx);
58         list_put(&scene.objects, obj, NULL);
59         pthread_mutex_unlock(&scene.mtx);
60 }
61
62 void scene_render()
63 {
64 #pragma GCC diagnostic push
65 #pragma GCC diagnostic ignored "-Wpedantic"
66         mat4x4 view_proj;
67         mat4x4_mul(view_proj, scene.projection, camera.view);
68
69         vec4 base_sunlight_dir = {0.0f, 0.0f, -1.0f, 1.0f};
70         vec4 sunlight_dir;
71         mat4x4 sunlight_mat;
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
76
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());
80
81         for (ListPair **pairptr = &scene.objects.first; *pairptr != NULL; ) {
82                 ListPair *pair = *pairptr;
83                 Object *obj = pair->key;
84                 if (obj->remove) {
85                         pthread_mutex_lock(&scene.mtx);
86                         *pairptr = pair->next;
87                         pthread_mutex_unlock(&scene.mtx);
88                         free(pair);
89                         object_delete(obj);
90                 } else {
91                         object_render(obj, view_proj, scene.loc_MVP);
92                         pairptr = &pair->next;
93                 }
94         }
95 }
96
97 void scene_on_resize(int width, int height)
98 {
99         mat4x4_perspective(scene.projection, scene.fov / 180.0f * M_PI, (float) width / (float) height, 0.01f, scene.render_distance);
100 }
101
102 GLuint scene_get_max_texture_units()
103 {
104         return scene.max_texture_units;
105 }