4 #include "client/camera.h"
5 #include "client/client.h"
6 #include "client/cube.h"
7 #include "client/mesh.h"
8 #include "client/scene.h"
9 #include "client/shader.h"
10 #include "client/sky.h"
11 #include "client/texture.h"
18 GLint skybox_loc_daylight;
19 GLuint skybox_textures[2];
27 GLint clouds_loc_daylight;
34 } __attribute__((packed)) VertexSunPosition;
39 } __attribute__((packed)) VertexSunTextureCoordinates;
43 VertexSunPosition position;
44 VertexSunTextureCoordinates textureCoordinates;
45 } __attribute__((packed)) VertexSun;
47 static VertexAttribute sun_vertex_attributes[2] = {
52 .size = sizeof(VertexSunPosition),
58 .size = sizeof(VertexSunTextureCoordinates),
62 static VertexLayout sun_vertex_layout = {
63 .attributes = sun_vertex_attributes,
65 .size = sizeof(VertexSun),
68 static VertexSun sun_vertices[6] = {
69 {{-0.5, -0.5, +0.0}, {+0.0, +0.0}},
70 {{+0.5, -0.5, +0.0}, {+1.0, +0.0}},
71 {{+0.5, +0.5, +0.0}, {+1.0, +1.0}},
72 {{+0.5, +0.5, +0.0}, {+1.0, +1.0}},
73 {{-0.5, +0.5, +0.0}, {+0.0, +1.0}},
74 {{-0.5, -0.5, +0.0}, {+0.0, +0.0}},
80 } __attribute__((packed)) VertexSkyboxPosition;
84 VertexSkyboxPosition position;
85 } __attribute__((packed)) VertexSkybox;
87 static VertexAttribute skybox_vertex_attributes[2] = {
92 .size = sizeof(VertexSkyboxPosition),
96 static VertexLayout skybox_vertex_layout = {
97 .attributes = skybox_vertex_attributes,
99 .size = sizeof(VertexSkybox),
102 static VertexSkybox skybox_vertices[6][6];
103 static VertexSkybox clouds_vertices[6][6];
109 if (! shader_program_create(RESSOURCEPATH "shaders/sky/skybox", &sky.skybox_prog, NULL)) {
110 fprintf(stderr, "Failed to create skybox shader program\n");
114 sky.skybox_loc_VP = glGetUniformLocation(sky.skybox_prog, "VP");
115 sky.skybox_loc_daylight = glGetUniformLocation(sky.skybox_prog, "daylight");
117 sky.skybox_textures[0] = texture_create_cubemap(RESSOURCEPATH "textures/skybox/day");
118 sky.skybox_textures[1] = texture_create_cubemap(RESSOURCEPATH "textures/skybox/night");
120 GLint texture_indices[2];
121 for (GLint i = 0; i < 2; i++)
122 texture_indices[i] = i;
124 glProgramUniform1iv(sky.skybox_prog, glGetUniformLocation(sky.skybox_prog, "textures"), 2, texture_indices);
126 for (int f = 0; f < 6; f++) {
127 for (int v = 0; v < 6; v++) {
128 skybox_vertices[f][v].position.x = cube_vertices[f][v].position.x;
129 skybox_vertices[f][v].position.y = cube_vertices[f][v].position.y;
130 skybox_vertices[f][v].position.z = cube_vertices[f][v].position.z;
134 sky.skybox_mesh = mesh_create();
135 sky.skybox_mesh->textures = sky.skybox_textures;
136 sky.skybox_mesh->textures_count = 2;
137 sky.skybox_mesh->free_textures = false;
138 sky.skybox_mesh->vertices = skybox_vertices;
139 sky.skybox_mesh->vertices_count = 36;
140 sky.skybox_mesh->free_vertices = false;
141 sky.skybox_mesh->layout = &skybox_vertex_layout;
145 if (! shader_program_create(RESSOURCEPATH "shaders/sky/sun", &sky.sun_prog, NULL)) {
146 fprintf(stderr, "Failed to create sun shader program\n");
150 sky.sun_loc_MVP = glGetUniformLocation(sky.sun_prog, "MVP");
152 sky.sun_texture = texture_load(RESSOURCEPATH "textures/sun.png", false);
154 sky.sun_mesh = mesh_create();
155 sky.sun_mesh->textures = &sky.sun_texture->id;
156 sky.sun_mesh->textures_count = 1;
157 sky.sun_mesh->free_textures = false;
158 sky.sun_mesh->vertices = sun_vertices;
159 sky.sun_mesh->vertices_count = 6;
160 sky.sun_mesh->free_vertices = false;
161 sky.sun_mesh->layout = &sun_vertex_layout;
165 if (! shader_program_create(RESSOURCEPATH "shaders/sky/clouds", &sky.clouds_prog, NULL)) {
166 fprintf(stderr, "Failed to create clouds shader program\n");
170 sky.clouds_loc_VP = glGetUniformLocation(sky.clouds_prog, "VP");
171 sky.clouds_loc_daylight = glGetUniformLocation(sky.clouds_prog, "daylight");
173 for (int f = 0; f < 6; f++) {
174 for (int v = 0; v < 6; v++) {
175 clouds_vertices[f][v].position.x = cube_vertices[f][v].position.x;
176 clouds_vertices[f][v].position.y = fmax(cube_vertices[f][v].position.y, 0.0);
177 clouds_vertices[f][v].position.z = cube_vertices[f][v].position.z;
181 sky.clouds_mesh = mesh_create();
182 sky.clouds_mesh->textures = sky.skybox_textures;
183 sky.clouds_mesh->textures_count = 1;
184 sky.clouds_mesh->free_textures = false;
185 sky.clouds_mesh->vertices = clouds_vertices;
186 sky.clouds_mesh->vertices_count = 36;
187 sky.clouds_mesh->free_vertices = false;
188 sky.clouds_mesh->layout = &skybox_vertex_layout;
195 glDeleteProgram(sky.skybox_prog);
196 glDeleteTextures(1, &sky.skybox_textures[0]);
197 glDeleteTextures(1, &sky.skybox_textures[1]);
198 mesh_delete(sky.skybox_mesh);
200 glDeleteProgram(sky.sun_prog);
201 mesh_delete(sky.sun_mesh);
204 #pragma GCC diagnostic push
205 #pragma GCC diagnostic ignored "-Wpedantic"
208 f64 daylight = get_daylight();
209 f64 sun_angle = get_sun_angle();
211 vec3 sun_pos = {0.0f, cos(sun_angle), sin(sun_angle)};
212 vec3_norm(sun_pos, sun_pos);
213 vec3_scale(sun_pos, sun_pos, 5.0f);
216 mat4x4_translate(model, sun_pos[0], sun_pos[1], sun_pos[2]);
217 mat4x4_rotate(model, model, 1.0f, 0.0f, 0.0f, sun_angle + M_PI / 2.0f);
220 mat4x4_dup(view, camera.view);
226 mat4x4_mul(VP, scene.projection, view);
229 mat4x4_mul(MVP, VP, model);
231 glDisable(GL_CULL_FACE);
232 glDepthFunc(GL_LEQUAL);
234 glUseProgram(sky.skybox_prog);
235 glUniformMatrix4fv(sky.skybox_loc_VP, 1, GL_FALSE, VP[0]);
236 glUniform1f(sky.skybox_loc_daylight, daylight);
237 mesh_render(sky.skybox_mesh);
239 glUseProgram(sky.sun_prog);
240 glUniformMatrix4fv(sky.sun_loc_MVP, 1, GL_FALSE, MVP[0]);
241 mesh_render(sky.sun_mesh);
243 glUseProgram(sky.clouds_prog);
244 glUniformMatrix4fv(sky.clouds_loc_VP, 1, GL_FALSE, VP[0]);
245 glUniform1f(sky.clouds_loc_daylight, daylight);
246 mesh_render(sky.clouds_mesh);
248 glDepthFunc(GL_LESS);
249 glEnable(GL_CULL_FACE);
251 #pragma GCC diagnostic pop