]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/sky.c
Client: unify error handling
[dragonblocks_alpha.git] / src / client / sky.c
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include <GL/gl.h>
4 #include "client/camera.h"
5 #include "client/client.h"
6 #include "client/cube.h"
7 #include "client/gl_debug.h"
8 #include "client/mesh.h"
9 #include "client/shader.h"
10 #include "client/sky.h"
11 #include "client/texture.h"
12 #include "client/window.h"
13 #include "day.h"
14
15 static GLuint sun_prog;
16 static GLint sun_loc_MVP;
17 static GLuint sun_texture;
18 typedef struct {
19         v3f32 position;
20         v2f32 textureCoordinates;
21 } __attribute__((packed)) SunVertex;
22 static Mesh sun_mesh = {
23         .layout = &(VertexLayout) {
24                 .attributes = (VertexAttribute[]) {
25                         {GL_FLOAT, 3, sizeof(v3f32)}, // position
26                         {GL_FLOAT, 2, sizeof(v2f32)}, // textureCoordinates
27                 },
28                 .count = 2,
29                 .size = sizeof(SunVertex),
30         },
31         .vao = 0,
32         .vbo = 0,
33         .data = (SunVertex[]) {
34                 {{-0.5, -0.5, +0.0}, {+0.0, +0.0}},
35                 {{+0.5, -0.5, +0.0}, {+1.0, +0.0}},
36                 {{+0.5, +0.5, +0.0}, {+1.0, +1.0}},
37                 {{+0.5, +0.5, +0.0}, {+1.0, +1.0}},
38                 {{-0.5, +0.5, +0.0}, {+0.0, +1.0}},
39                 {{-0.5, -0.5, +0.0}, {+0.0, +0.0}},
40         },
41         .count = 6,
42         .free_data = false,
43 };
44
45 static GLuint skybox_prog;
46 static GLint skybox_loc_VP;
47 static GLint skybox_loc_daylight;
48 static GLuint skybox_texture_day;
49 static GLuint skybox_texture_night;
50 typedef struct {
51         v3f32 position;
52 } __attribute__((packed)) SkyboxVertex;
53 static Mesh skybox_mesh = {
54         .layout = &(VertexLayout) {
55                 .attributes = (VertexAttribute[]) {
56                         {GL_FLOAT, 3, sizeof(v3f32)}, // position
57                 },
58                 .count = 1,
59                 .size = sizeof(SkyboxVertex),
60         },
61         .vao = 0,
62         .vbo = 0,
63         .data = NULL,
64         .count = 36,
65         .free_data = false,
66 };
67
68 static GLuint clouds_prog;
69 static GLint clouds_loc_VP;
70 static GLint clouds_loc_daylight;
71 static Mesh clouds_mesh;
72
73 void sky_init()
74 {
75         clouds_mesh = skybox_mesh;
76         SkyboxVertex skybox_vertices[6][6], clouds_vertices[6][6];
77         for (int f = 0; f < 6; f++) {
78                 for (int v = 0; v < 6; v++) {
79                         skybox_vertices[f][v].position =
80                                 clouds_vertices[f][v].position =
81                                 cube_vertices[f][v].position;
82
83                         clouds_vertices[f][v].position.y = fmax(clouds_vertices[f][v].position.y, 0.0);
84                 }
85         }
86
87         // skybox
88
89         skybox_prog = shader_program_create(RESSOURCE_PATH "shaders/sky/skybox", NULL);
90         glProgramUniform1iv(skybox_prog, glGetUniformLocation(skybox_prog, "textures"), 2, (GLint[]) {0, 1}); GL_DEBUG
91         skybox_loc_VP = glGetUniformLocation(skybox_prog, "VP"); GL_DEBUG
92         skybox_loc_daylight = glGetUniformLocation(skybox_prog, "daylight"); GL_DEBUG
93         skybox_texture_day = texture_load_cubemap(RESSOURCE_PATH "textures/skybox/day", true)->txo;
94         skybox_texture_night = texture_load_cubemap(RESSOURCE_PATH "textures/skybox/night", true)->txo;
95         skybox_mesh.data = skybox_vertices;
96         mesh_upload(&skybox_mesh);
97
98         // sun
99
100         sun_prog = shader_program_create(RESSOURCE_PATH "shaders/sky/sun", NULL);
101         sun_loc_MVP = glGetUniformLocation(sun_prog, "MVP"); GL_DEBUG
102         sun_texture = texture_load(RESSOURCE_PATH "textures/sun.png", false)->txo;
103
104         // clouds
105
106         clouds_prog = shader_program_create(RESSOURCE_PATH "shaders/sky/clouds", NULL);
107         clouds_loc_VP = glGetUniformLocation(clouds_prog, "VP"); GL_DEBUG
108         clouds_loc_daylight = glGetUniformLocation(clouds_prog, "daylight"); GL_DEBUG
109         clouds_mesh.data = clouds_vertices;
110         mesh_upload(&clouds_mesh);
111 }
112
113 void sky_deinit()
114 {
115         glDeleteProgram(sun_prog); GL_DEBUG
116         mesh_destroy(&sun_mesh);
117
118         glDeleteProgram(skybox_prog); GL_DEBUG
119         mesh_destroy(&skybox_mesh);
120
121         glDeleteProgram(clouds_prog); GL_DEBUG
122         mesh_destroy(&clouds_mesh);
123 }
124
125 void sky_render()
126 {
127         f64 daylight = get_daylight();
128         f64 sun_angle = get_sun_angle();
129
130         vec3 sun_pos = {0.0f, cos(sun_angle), sin(sun_angle)};
131         vec3_norm(sun_pos, sun_pos);
132         vec3_scale(sun_pos, sun_pos, 5.0f);
133
134         mat4x4 model;
135         mat4x4_translate(model, sun_pos[0], sun_pos[1], sun_pos[2]);
136         mat4x4_rotate(model, model, 1.0f, 0.0f, 0.0f, sun_angle + M_PI / 2.0f);
137
138         mat4x4 view;
139         mat4x4_dup(view, camera.view);
140         view[3][0] = 0.0f;
141         view[3][1] = 0.0f;
142         view[3][2] = 0.0f;
143
144         mat4x4 vp;
145         mat4x4_mul(vp, window.projection, view);
146
147         mat4x4 mvp;
148         mat4x4_mul(mvp, vp, model);
149
150         glDisable(GL_CULL_FACE); GL_DEBUG
151         glDepthFunc(GL_LEQUAL); GL_DEBUG
152
153         glUseProgram(skybox_prog); GL_DEBUG
154         glUniformMatrix4fv(skybox_loc_VP, 1, GL_FALSE, vp[0]); GL_DEBUG
155         glUniform1f(skybox_loc_daylight, daylight); GL_DEBUG
156         glBindTextureUnit(0, skybox_texture_day); GL_DEBUG
157         glBindTextureUnit(1, skybox_texture_night); GL_DEBUG
158         mesh_render(&skybox_mesh);
159
160         glUseProgram(sun_prog); GL_DEBUG
161         glUniformMatrix4fv(sun_loc_MVP, 1, GL_FALSE, mvp[0]); GL_DEBUG
162         glBindTextureUnit(0, sun_texture); GL_DEBUG
163         mesh_render(&sun_mesh);
164
165         glUseProgram(clouds_prog); GL_DEBUG
166         glUniformMatrix4fv(clouds_loc_VP, 1, GL_FALSE, vp[0]); GL_DEBUG
167         glUniform1f(clouds_loc_daylight, daylight); GL_DEBUG
168         glBindTextureUnit(0, skybox_texture_day); GL_DEBUG
169         mesh_render(&clouds_mesh);
170
171         glDepthFunc(GL_LESS); GL_DEBUG
172         glEnable(GL_CULL_FACE); GL_DEBUG
173 }