Line | Count | Source |
1 | | #include "client/renderer.h" |
2 | | |
3 | | #include <raymath.h> |
4 | | #include <rlgl.h> |
5 | | |
6 | | #include <algorithm> |
7 | | #include <array> |
8 | | #include <cmath> |
9 | | #include <cstddef> |
10 | | #include <memory> |
11 | | #include <string> |
12 | | #include <vector> |
13 | | |
14 | | #include "client/glsl.h" |
15 | | #include "client/picking.h" |
16 | | #include "client/scene.h" |
17 | | #include "client/sky.h" |
18 | | #include "server/duels.h" |
19 | | |
20 | | namespace nanommo { |
21 | | |
22 | | namespace { |
23 | | |
24 | | constexpr const char* kVertexShader = R"(#version 330 |
25 | | in vec3 vertexPosition; |
26 | | in vec3 vertexNormal; |
27 | | in vec4 vertexColor; |
28 | | uniform mat4 mvp; |
29 | | uniform mat4 matModel; |
30 | | uniform mat4 matNormal; |
31 | | out vec3 normal; |
32 | | out vec4 color; |
33 | | out float depth; |
34 | | out vec3 world; |
35 | | |
36 | | void main() { |
37 | | normal = mat3(matNormal) * vertexNormal; |
38 | | color = vertexColor; |
39 | | world = (matModel * vec4(vertexPosition, 1.0)).xyz; |
40 | | gl_Position = mvp * vec4(vertexPosition, 1.0); |
41 | | depth = gl_Position.w; |
42 | | } |
43 | | )"; |
44 | | |
45 | | constexpr const char* kSkinnedVertexShader = R"(#version 330 |
46 | | in vec3 vertexPosition; |
47 | | in vec3 vertexNormal; |
48 | | in vec4 vertexBoneIndices; |
49 | | in vec4 vertexBoneWeights; |
50 | | in vec4 vertexColor; |
51 | | uniform mat4 mvp; |
52 | | uniform mat4 matModel; |
53 | | uniform mat4 matNormal; |
54 | | uniform mat4 boneMatrices[128]; |
55 | | out vec3 normal; |
56 | | out vec4 color; |
57 | | out float depth; |
58 | | out vec3 world; |
59 | | |
60 | | void main() { |
61 | | mat4 skin = boneMatrices[int(vertexBoneIndices.x)] * vertexBoneWeights.x |
62 | | + boneMatrices[int(vertexBoneIndices.y)] * vertexBoneWeights.y |
63 | | + boneMatrices[int(vertexBoneIndices.z)] * vertexBoneWeights.z |
64 | | + boneMatrices[int(vertexBoneIndices.w)] * vertexBoneWeights.w; |
65 | | normal = mat3(matNormal) * mat3(skin) * vertexNormal; |
66 | | color = vertexColor; |
67 | | world = (matModel * skin * vec4(vertexPosition, 1.0)).xyz; |
68 | | gl_Position = mvp * skin * vec4(vertexPosition, 1.0); |
69 | | depth = gl_Position.w; |
70 | | } |
71 | | )"; |
72 | | |
73 | | // The light is worked out for each pixel, from the normal the vertex shaders |
74 | | // hand on, so that cel shading's bands follow the surface. Where a cloud is |
75 | | // between the pixel and the sun, less of the sun's light gets through. |
76 | | constexpr const char* kFragmentShader = R"(#version 330 |
77 | | in vec3 normal; |
78 | | in vec4 color; |
79 | | in float depth; |
80 | | in vec3 world; |
81 | | uniform vec4 colDiffuse; |
82 | | // The middle of each ball the clouds are made of, x and z, its height, and |
83 | | // how wide it is, or 0 for none. |
84 | | uniform vec4 clouds[64]; |
85 | | uniform float cloudShade; |
86 | | uniform vec3 sun; |
87 | | uniform vec3 sunlight; |
88 | | uniform vec3 ambient; |
89 | | uniform vec3 fogColor; |
90 | | uniform float fogNear; |
91 | | uniform float fogFar; |
92 | | uniform float emission; |
93 | | uniform float bands; |
94 | | // After rain: from dry to soaked. |
95 | | uniform float wetness; |
96 | | uniform vec3 eye; |
97 | | out vec4 pixel; |
98 | | |
99 | | float Shadowed() { |
100 | | vec3 toward = normalize(sun); |
101 | | float shade = 1.0; |
102 | | for (int i = 0; i < 64; ++i) { |
103 | | vec4 cloud = clouds[i]; |
104 | | if (cloud.w <= 0.0) { |
105 | | continue; |
106 | | } |
107 | | // Where the line from here to the sun passes the ball's height. |
108 | | vec2 up = world.xz + toward.xz / max(toward.y, 0.2) * (cloud.z - world.y); |
109 | | float far = smoothstep(0.3 * cloud.w, 1.1 * cloud.w, length(up - cloud.xy)); |
110 | | shade *= 1.0 - cloudShade * (1.0 - far); |
111 | | } |
112 | | return max(shade, 1.0 - cloudShade); |
113 | | } |
114 | | |
115 | | void main() { |
116 | | float sunlit = max(dot(normalize(normal), normalize(sun)), 0.0) * Shadowed(); |
117 | | // Cel shading, like raylib's example: the sun's light in a few flat bands, |
118 | | // as a cartoon's colours are, where no bands is smooth. |
119 | | if (bands > 0.0) { |
120 | | sunlit = min(floor(sunlit * bands), bands - 1.0) / (bands - 1.0); |
121 | | } |
122 | | vec3 light = ambient + sunlight * sunlit + emission; |
123 | | vec3 lit = colDiffuse.rgb * color.rgb * light; |
124 | | // Wet, what faces up is darker, and the light shines off it, where it |
125 | | // comes back to the eye. |
126 | | vec3 facing = normalize(normal); |
127 | | float wet = wetness * smoothstep(0.55, 0.9, facing.y); |
128 | | vec3 reflected = reflect(-normalize(sun), facing); |
129 | | float shine = pow(max(dot(reflected, normalize(eye - world)), 0.0), 60.0); |
130 | | lit = lit * (1.0 - 0.38 * wet) + sunlight * shine * 0.45 * wet; |
131 | | float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0); |
132 | | pixel = vec4(mix(lit, fogColor, fog), colDiffuse.a * color.a); |
133 | | } |
134 | | )"; |
135 | | |
136 | | // Cel shading's outlines: each vertex pushed out along its normal, in metres, |
137 | | // so that the model is drawn a little bigger. |
138 | | constexpr const char* kOutlineVertexShader = R"(#version 330 |
139 | | in vec3 vertexPosition; |
140 | | in vec3 vertexNormal; |
141 | | uniform mat4 matModel; |
142 | | uniform mat4 matNormal; |
143 | | uniform mat4 matView; |
144 | | uniform mat4 matProjection; |
145 | | uniform float thickness; |
146 | | out float depth; |
147 | | |
148 | | void main() { |
149 | | vec3 normal = normalize(mat3(matNormal) * vertexNormal); |
150 | | vec4 at = matModel * vec4(vertexPosition, 1.0) |
151 | | + vec4(normal * thickness, 0.0); |
152 | | gl_Position = matProjection * matView * at; |
153 | | depth = gl_Position.w; |
154 | | } |
155 | | )"; |
156 | | |
157 | | // For models with bones, posed like the skinned vertex shader. |
158 | | constexpr const char* kSkinnedOutlineVertexShader = R"(#version 330 |
159 | | in vec3 vertexPosition; |
160 | | in vec3 vertexNormal; |
161 | | in vec4 vertexBoneIndices; |
162 | | in vec4 vertexBoneWeights; |
163 | | uniform mat4 matModel; |
164 | | uniform mat4 matNormal; |
165 | | uniform mat4 matView; |
166 | | uniform mat4 matProjection; |
167 | | uniform mat4 boneMatrices[128]; |
168 | | uniform float thickness; |
169 | | out float depth; |
170 | | |
171 | | void main() { |
172 | | mat4 skin = boneMatrices[int(vertexBoneIndices.x)] * vertexBoneWeights.x |
173 | | + boneMatrices[int(vertexBoneIndices.y)] * vertexBoneWeights.y |
174 | | + boneMatrices[int(vertexBoneIndices.z)] * vertexBoneWeights.z |
175 | | + boneMatrices[int(vertexBoneIndices.w)] * vertexBoneWeights.w; |
176 | | vec3 normal = normalize(mat3(matNormal) * mat3(skin) * vertexNormal); |
177 | | vec4 at = matModel * skin * vec4(vertexPosition, 1.0) |
178 | | + vec4(normal * thickness, 0.0); |
179 | | gl_Position = matProjection * matView * at; |
180 | | depth = gl_Position.w; |
181 | | } |
182 | | )"; |
183 | | |
184 | | // Nearly black, into the fog like the rest of the world. |
185 | | constexpr const char* kOutlineFragmentShader = R"(#version 330 |
186 | | in float depth; |
187 | | uniform vec3 fogColor; |
188 | | uniform float fogNear; |
189 | | uniform float fogFar; |
190 | | out vec4 pixel; |
191 | | |
192 | | void main() { |
193 | | float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0); |
194 | | pixel = vec4(mix(vec3(0.05), fogColor, fog), 1.0); |
195 | | } |
196 | | )"; |
197 | | |
198 | | // In metres, so that an outline is thinner the further away it is. A |
199 | | // figure's arms are about 12 centimetres across, so it's a third of that. |
200 | | constexpr float kOutlineThickness = 0.04f; |
201 | | |
202 | | constexpr std::array<float, 3> kPreviewBackground = {0.1f, 0.04f, 0.05f}; |
203 | | |
204 | | // The sun's light in three bands: in shadow, half lit and lit. |
205 | | constexpr float kBands = 3.0f; |
206 | | |
207 | | // A shadow reaches out from a figure's middle about as far as its shoulders |
208 | | // are wide, in metres, and it's black, at about a third of the way to opaque. |
209 | | constexpr float kShadowRadius = 0.42f; |
210 | | // Around the monster the player picked, in the red of its nameplate. |
211 | | constexpr float kRingInner = 0.55f; |
212 | | constexpr float kRingOuter = 0.68f; |
213 | | constexpr Color kRing = {235, 90, 75, 220}; |
214 | | // Where the player's walking to. |
215 | | constexpr Color kDestination = {255, 255, 255, 200}; |
216 | | // A duel's flag, in gold, a pole a little taller than a player, and its |
217 | | // furthest ring fainter, as it's there to be seen from inside. |
218 | | constexpr Color kFlag = {255, 211, 77, 230}; |
219 | | constexpr Color kFlagEdge = {255, 211, 77, 110}; |
220 | | constexpr Vector3 kPole{0.12f, 2.4f, 0.12f}; |
221 | | // A monster's skill on its way, in the skill's colour: its whole shape faint, |
222 | | // as much as has filled stronger, and its edge, to step out past, strongest. |
223 | | constexpr float kTelegraphShape = 0.18f; |
224 | | constexpr float kTelegraphFill = 0.4f; |
225 | | constexpr float kTelegraphEdge = 0.85f; |
226 | | // In metres: the edge's width, and the widest band a shape is drawn in, so |
227 | | // that it lies on the ground. |
228 | | constexpr float kTelegraphRim = 0.12f; |
229 | | constexpr float kBand = 0.5f; |
230 | | constexpr unsigned char kShadowDarkness = 82; |
231 | | |
232 | | // Nothing casts a shadow, so each figure gets a soft dark patch under it, |
233 | | // dark in the middle and fading out at the edge, with each corner on the |
234 | | // ground, so it lies on a slope. |
235 | 0 | void DrawShadow(const Terrain& terrain, Vector3 at, float radius) { |
236 | 0 | constexpr int kSides = 16; |
237 | 0 | auto corner = [&](float distance, int i, unsigned char alpha) { |
238 | 0 | const float turn = 2.0f * PI * i / kSides; |
239 | 0 | const float x = at.x + distance * std::cos(turn); |
240 | 0 | const float z = at.z + distance * std::sin(turn); |
241 | 0 | rlColor4ub(0, 0, 0, alpha); |
242 | | // A little above it, so the ground doesn't cover it. |
243 | 0 | rlVertex3f(x, terrain.Height(x, z) + 0.03f, z); |
244 | 0 | }; |
245 | 0 | const float inner = 0.6f * radius; |
246 | 0 | rlBegin(RL_TRIANGLES); |
247 | 0 | for (int i = 0; i < kSides; ++i) { |
248 | | // The middle, as dark all over, then a ring that fades out. |
249 | 0 | corner(0.0f, i, kShadowDarkness); |
250 | 0 | corner(inner, i + 1, kShadowDarkness); |
251 | 0 | corner(inner, i, kShadowDarkness); |
252 | 0 | corner(inner, i, kShadowDarkness); |
253 | 0 | corner(inner, i + 1, kShadowDarkness); |
254 | 0 | corner(radius, i + 1, 0); |
255 | 0 | corner(inner, i, kShadowDarkness); |
256 | 0 | corner(radius, i + 1, 0); |
257 | 0 | corner(radius, i, 0); |
258 | 0 | } |
259 | 0 | rlEnd(); |
260 | 0 | } |
261 | | |
262 | | // A ring at a figure's feet, which lies on a slope as a shadow does, or a |
263 | | // slice of one, as wide as the arc, in radians, about the way the yaw faces. |
264 | | // A wide one has more sides, a metre or so long, so that it follows the |
265 | | // ground. |
266 | | void DrawRing(const Terrain& terrain, Vector3 at, float inner, float outer, |
267 | 29 | Color color, float yaw = 0.0f, float arc = 2.0f * PI) { |
268 | 29 | const int sides = std::max(32, static_cast<int>(outer * arc)); |
269 | | // The yaw goes round from z towards x, and the turns from x towards z. |
270 | 29 | const float first = PI / 2.0f - yaw - arc / 2.0f; |
271 | 5.56k | auto corner = [&](float distance, int i) { |
272 | 5.56k | const float turn = first + arc * i / sides; |
273 | 5.56k | const float x = at.x + distance * std::cos(turn); |
274 | 5.56k | const float z = at.z + distance * std::sin(turn); |
275 | 5.56k | rlColor4ub(color.r, color.g, color.b, color.a); |
276 | | // A little above the ground, so that the ground doesn't cover it. |
277 | 5.56k | rlVertex3f(x, terrain.Height(x, z) + 0.04f, z); |
278 | 5.56k | }; |
279 | 29 | rlBegin(RL_TRIANGLES); |
280 | 957 | for (int i = 0; i < sides; ++i928 ) { |
281 | 928 | corner(inner, i); |
282 | 928 | corner(outer, i + 1); |
283 | 928 | corner(outer, i); |
284 | 928 | corner(inner, i); |
285 | 928 | corner(inner, i + 1); |
286 | 928 | corner(outer, i + 1); |
287 | 928 | } |
288 | 29 | rlEnd(); |
289 | 29 | } |
290 | | |
291 | | // The shape of a monster's skill on its way: all of it faint, as much as has |
292 | | // filled stronger, and its edges strongest. |
293 | 1 | void DrawTelegraph(const Terrain& terrain, const Telegraph& telegraph) { |
294 | 1 | const monsters::Skill& skill = telegraph.skill; |
295 | 1 | const Color color = GetColor(skill.color()); |
296 | 1 | const Vector3 at{telegraph.at.x, 0.0f, telegraph.at.z}; |
297 | | // From the monster out, in bands, each a slice of a ring, so that a wide |
298 | | // shape lies on the ground too. |
299 | 1 | const auto slice = [&](float inner, float outer, float alpha, float yaw, |
300 | 5 | float arc) { |
301 | 5 | const int bands = static_cast<int>(std::ceil((outer - inner) / kBand)); |
302 | 34 | for (int band = 0; band < bands; ++band29 ) { |
303 | 29 | const float from = inner + static_cast<float>(band) * kBand; |
304 | 29 | DrawRing(terrain, at, from, std::min(from + kBand, outer), |
305 | 29 | Fade(color, alpha), yaw, arc); |
306 | 29 | } |
307 | 5 | }; |
308 | 1 | const float arc = |
309 | 1 | skill.shape() == monsters::CONE ? skill.arc() * DEG2RAD : 2.0f * 0 PI0 ; |
310 | 1 | const float size = skill.size(); |
311 | 1 | slice(0.0f, size, kTelegraphShape, telegraph.yaw, arc); |
312 | 1 | slice(0.0f, size * telegraph.Filled(), kTelegraphFill, telegraph.yaw, arc); |
313 | 1 | slice(size - kTelegraphRim, size, kTelegraphEdge, telegraph.yaw, arc); |
314 | | // A cone's sides are edges too, each a slice as narrow as the rim at the |
315 | | // end. |
316 | 1 | if (skill.shape() == monsters::CONE) { |
317 | 1 | const float side = kTelegraphRim / size; |
318 | 2 | for (const float way : {-1.0f, 1.0f}) { |
319 | 2 | slice(0.0f, size, kTelegraphEdge, |
320 | 2 | telegraph.yaw + way * (arc - side) / 2.0f, side); |
321 | 2 | } |
322 | 1 | } |
323 | 1 | } |
324 | | |
325 | | // The fog as far as it's lifted: from all of it within a couple of metres, to |
326 | | // the atmosphere's own. |
327 | 14 | Atmosphere Lifted(Atmosphere atmosphere, float clear) { |
328 | 14 | constexpr float kNearest = 2.0f; |
329 | 14 | atmosphere.fog_near *= clear; |
330 | 14 | atmosphere.fog_far = kNearest + (atmosphere.fog_far - kNearest) * clear; |
331 | 14 | return atmosphere; |
332 | 14 | } |
333 | | |
334 | 60 | void SetFog(Shader shader, const Atmosphere& atmosphere) { |
335 | | // Into the sky's colour, so that far away things fade into the sky. |
336 | 60 | SetShaderValue(shader, GetShaderLocation(shader, "fogColor"), |
337 | 60 | atmosphere.sky.data(), SHADER_UNIFORM_VEC3); |
338 | 60 | SetShaderValue(shader, GetShaderLocation(shader, "fogNear"), |
339 | 60 | &atmosphere.fog_near, SHADER_UNIFORM_FLOAT); |
340 | 60 | SetShaderValue(shader, GetShaderLocation(shader, "fogFar"), |
341 | 60 | &atmosphere.fog_far, SHADER_UNIFORM_FLOAT); |
342 | 60 | } |
343 | | |
344 | 30 | void SetAtmosphere(Shader shader, const Atmosphere& atmosphere) { |
345 | 30 | SetFog(shader, atmosphere); |
346 | 30 | SetShaderValue(shader, GetShaderLocation(shader, "sun"), |
347 | 30 | atmosphere.sun.data(), SHADER_UNIFORM_VEC3); |
348 | 30 | SetShaderValue(shader, GetShaderLocation(shader, "sunlight"), |
349 | 30 | atmosphere.sunlight.data(), SHADER_UNIFORM_VEC3); |
350 | 30 | SetShaderValue(shader, GetShaderLocation(shader, "ambient"), |
351 | 30 | atmosphere.ambient.data(), SHADER_UNIFORM_VEC3); |
352 | 30 | SetShaderValue(shader, GetShaderLocation(shader, "wetness"), |
353 | 30 | &atmosphere.wetness, SHADER_UNIFORM_FLOAT); |
354 | 30 | } |
355 | | |
356 | | } // namespace |
357 | | |
358 | | Renderer::Renderer(const Content& content) |
359 | 11 | : cube_shader_(LoadGlsl(kVertexShader, kFragmentShader)), |
360 | 11 | character_shader_(LoadGlsl(kSkinnedVertexShader, kFragmentShader)), |
361 | 11 | outline_{.still = LoadGlsl(kOutlineVertexShader, kOutlineFragmentShader), |
362 | 11 | .skinned = LoadGlsl(kSkinnedOutlineVertexShader, |
363 | 11 | kOutlineFragmentShader)}, |
364 | 11 | cube_(LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f))), |
365 | 11 | disc_(LoadModelFromMesh(Disc().Upload())), |
366 | 11 | spire_(LoadModelFromMesh(Spire().Upload())), |
367 | 11 | characters_(content.rig, character_shader_, content.figures), |
368 | 11 | fx_(std::make_unique<Fx>(content.skills, cube_shader_, content.items)), |
369 | 11 | sky_dome_(std::make_unique<Sky>()), |
370 | 11 | weather_(std::make_unique<Weather>()) { |
371 | 33 | for (Model* model : {&cube_, &disc_, &spire_}) { |
372 | 33 | model->materials[0].shader = cube_shader_; |
373 | 33 | } |
374 | 22 | for (const Shader& shader : {outline_.still, outline_.skinned}) { |
375 | 22 | SetShaderValue(shader, GetShaderLocation(shader, "thickness"), |
376 | 22 | &kOutlineThickness, SHADER_UNIFORM_FLOAT); |
377 | 22 | } |
378 | | // Weapons have no bones, so they're drawn with the cube's shader. |
379 | 54 | for (const auto& [id, item] : content.items.by_id) { |
380 | 54 | characters_.Hold(id, item.look(), cube_shader_); |
381 | 54 | characters_.Wear(id, item.look()); |
382 | 54 | } |
383 | 64 | for (const auto& [id, skill] : content.skills.by_id) { |
384 | 64 | characters_.Learn(id, skill.look()); |
385 | 64 | } |
386 | | // Two kinds with one model load it twice, which is fine for a few kinds. |
387 | 17 | for (const auto& [id, kind] : content.monsters.kinds) { |
388 | 17 | monsters_[id] = std::make_unique<Characters>( |
389 | 17 | MonstersFolder(), std::vector<std::string>{kind.look().model()}, |
390 | 17 | kind.look().height(), character_shader_); |
391 | 17 | } |
392 | | |
393 | 11 | tree_ = LoadFaces(TreesFolder() + "/tree.glb"); |
394 | 11 | } |
395 | | |
396 | | // UnloadModel leaves the model's shader alone, as models can share one. |
397 | 11 | Renderer::~Renderer() { |
398 | 11 | UnloadTerrain(); |
399 | 11 | UnloadModel(cube_); |
400 | 11 | UnloadModel(disc_); |
401 | 11 | UnloadModel(spire_); |
402 | 11 | UnloadShader(character_shader_); |
403 | 11 | UnloadShader(outline_.still); |
404 | 11 | UnloadShader(outline_.skinned); |
405 | 11 | UnloadShader(cube_shader_); |
406 | 11 | } |
407 | | |
408 | 2 | void Renderer::set_terrain(const Terrain& terrain, int land) { |
409 | 2 | UnloadTerrain(); |
410 | 2 | terrain_ = terrain; |
411 | 2 | land_ = land; |
412 | 2 | const Look& look = kLooks[static_cast<std::size_t>(land) % kLooks.size()]; |
413 | 2 | atmosphere_ = look.atmosphere; |
414 | 2 | weather_->set_land(terrain, land); |
415 | | // The ground and the trees have no bones either. |
416 | 2 | chunks_ = LoadChunks(terrain, look, tree_, cube_shader_); |
417 | 2 | sea_ = std::make_unique<Sea>(terrain, look); |
418 | 2 | sky_dome_->Gather(terrain.seed()); |
419 | 2 | } |
420 | | |
421 | 0 | void Renderer::set_cel_shading(bool on) { |
422 | 0 | if (on == cel_shading_) { |
423 | 0 | return; |
424 | 0 | } |
425 | 0 | cel_shading_ = on; |
426 | | // A shader keeps a uniform's value until it's set again. With no bands, |
427 | | // which a uniform starts at, the light is smooth. |
428 | 0 | const float bands = on ? kBands : 0.0f; |
429 | 0 | for (const Shader& shader : {cube_shader_, character_shader_}) { |
430 | 0 | SetShaderValue(shader, GetShaderLocation(shader, "bands"), &bands, |
431 | 0 | SHADER_UNIFORM_FLOAT); |
432 | 0 | } |
433 | 0 | } |
434 | | |
435 | 0 | void Renderer::Animate(float seconds) { |
436 | 0 | fx_->Update(figures_, seconds); |
437 | 0 | reactions_.Update(seconds); |
438 | 0 | reactions_.Apply(figures_); |
439 | 0 | fx_->Hold(figures_); |
440 | 0 | clock_ += seconds; |
441 | | // Without the time of day, it's always noon for what glows. |
442 | 0 | protocol::Sky shown = sky_.value_or(protocol::Sky()); |
443 | 0 | if (!sky_ || !showing_.day_night) { |
444 | 0 | shown.set_day(0.5f); |
445 | 0 | } |
446 | 0 | weather_->Update(seconds, shown, sky_.has_value() && showing_.weather, |
447 | 0 | camera_); |
448 | 0 | const Look& look = kLooks[static_cast<std::size_t>(land_) % kLooks.size()]; |
449 | 0 | atmosphere_ = |
450 | 0 | sky_ ? AtmosphereUnder(land_, *sky_, showing_) : look.atmosphere; |
451 | 0 | atmosphere_.wetness = weather_->wetness(); |
452 | | // A bolt lights up the sky, and everything under it, for a moment. |
453 | 0 | const float flash = weather_->flash(); |
454 | 0 | for (int c = 0; c < 3; ++c) { |
455 | 0 | atmosphere_.sky[c] += flash * 0.55f; |
456 | 0 | atmosphere_.zenith[c] += flash * 0.4f; |
457 | 0 | atmosphere_.ambient[c] += flash * 0.45f; |
458 | 0 | atmosphere_.clouds[c] += flash * 0.6f; |
459 | 0 | } |
460 | 0 | if (adjust_) { |
461 | 0 | adjust_(atmosphere_); |
462 | 0 | } |
463 | 0 | } |
464 | | |
465 | 13 | void Renderer::UnloadTerrain() { |
466 | 13 | UnloadChunks(chunks_); |
467 | 13 | sea_.reset(); |
468 | 13 | } |
469 | | |
470 | | void Renderer::DrawCounted(const Mesh& mesh, const Material& material, |
471 | 26 | const Matrix& transform) const { |
472 | 26 | DrawMesh(mesh, material, transform); |
473 | 26 | ++work_.draws; |
474 | 26 | work_.triangles += mesh.triangleCount; |
475 | 26 | } |
476 | | |
477 | 14 | void Renderer::Draw() const { |
478 | 14 | work_ = {}; |
479 | 14 | Clear(); |
480 | 14 | const Atmosphere atmosphere = Lifted(atmosphere_, clear_); |
481 | 14 | SetAtmosphere(cube_shader_, atmosphere); |
482 | 14 | SetAtmosphere(character_shader_, atmosphere); |
483 | 14 | SetFog(outline_.still, atmosphere); |
484 | 14 | SetFog(outline_.skinned, atmosphere); |
485 | 28 | for (const Shader& shader : {cube_shader_, character_shader_}) { |
486 | 28 | SetShaderValue(shader, GetShaderLocation(shader, "eye"), &camera_.position, |
487 | 28 | SHADER_UNIFORM_VEC3); |
488 | 28 | } |
489 | | // A heavy blow nearby shakes the camera, and a skill of ours can take it |
490 | | // in closer, though never past where it looks. |
491 | 14 | Camera3D camera = camera_; |
492 | 14 | const Vector3 toward = Vector3Subtract(camera_.target, camera_.position); |
493 | 14 | const float zoom = std::min(fx_->Zoom(), 0.6f * Vector3Length(toward)); |
494 | 14 | camera.position = |
495 | 14 | Vector3Add(camera.position, Vector3Scale(Vector3Normalize(toward), zoom)); |
496 | 14 | const Vector3 shake = fx_->Shake(camera_.position); |
497 | 14 | camera.position = Vector3Add(camera.position, shake); |
498 | 14 | camera.target = Vector3Add(camera.target, shake); |
499 | 14 | if (terrain_) { |
500 | 3 | sky_dome_->Shade(cube_shader_, camera, atmosphere, clock_); |
501 | 3 | sky_dome_->Shade(character_shader_, camera, atmosphere, clock_); |
502 | 3 | } |
503 | 14 | BeginMode3D(camera); |
504 | | // First, as it's behind everything. |
505 | 14 | if (terrain_) { |
506 | 3 | sky_dome_->Draw(camera, atmosphere, clock_); |
507 | 3 | } |
508 | | // As BeginMode3D set them up, so we cull with what the GPU will use. |
509 | 14 | const Matrix view_projection = |
510 | 14 | MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection()); |
511 | 768 | for (const Chunk& chunk : chunks_) { |
512 | 768 | if (InSight(chunk.box, camera_.position, view_projection, |
513 | 768 | atmosphere.fog_far)) { |
514 | 26 | DrawCounted(chunk.model.meshes[0], chunk.model.materials[0], |
515 | 26 | MatrixIdentity()); |
516 | 26 | } |
517 | 768 | } |
518 | 14 | for (const Cube& cube : cubes_) { |
519 | 1 | const Model& model = cube.shape == Shape::kDisc ? disc_0 |
520 | 1 | : cube.shape == Shape::kSpire ? spire_0 |
521 | 1 | : cube_; |
522 | 1 | DrawModelEx(model, cube.position, {0.0f, 1.0f, 0.0f}, 0.0f, cube.size, |
523 | 1 | cube.color); |
524 | 1 | } |
525 | 14 | if (flag_) { |
526 | 0 | DrawModelEx(spire_, {flag_->x, flag_->y + kPole.y / 2.0f, flag_->z}, |
527 | 0 | {0.0f, 1.0f, 0.0f}, 0.0f, kPole, kFlag); |
528 | 0 | } |
529 | 14 | for (const Figure& figure : figures_) { |
530 | 9 | DrawFigure(figure); |
531 | 9 | } |
532 | 14 | if (terrain_) { |
533 | 3 | fx_->DrawSolid(*terrain_); |
534 | 3 | } |
535 | | // The shadows are see-through, and lie on the ground, so they go after |
536 | | // what's solid, without hiding what's behind them from what comes next. |
537 | 14 | if (terrain_) { |
538 | 3 | rlDisableDepthMask(); |
539 | 3 | BeginBlendMode(BLEND_ALPHA); |
540 | 3 | for (const Figure& figure : figures_) { |
541 | 0 | DrawShadow(*terrain_, figure.position, kShadowRadius); |
542 | 0 | } |
543 | 3 | for (const auto& [monster, telegraph] : telegraphs_) { |
544 | 1 | DrawTelegraph(*terrain_, telegraph); |
545 | 1 | } |
546 | | // After all the shadows, so that none of them, such as a player's |
547 | | // beside the monster, darkens the ring. |
548 | 3 | for (const Figure& figure : figures_) { |
549 | 0 | if (Picked(figure, target_, target_player_)) { |
550 | 0 | DrawRing(*terrain_, figure.position, kRingInner, kRingOuter, kRing); |
551 | 0 | } |
552 | 0 | } |
553 | 3 | if (destination_) { |
554 | 0 | DrawRing(*terrain_, *destination_, kRingInner * 0.6f, kRingOuter * 0.6f, |
555 | 0 | kDestination); |
556 | 0 | } |
557 | 3 | if (flag_) { |
558 | 0 | DrawRing(*terrain_, *flag_, kRingInner, kRingOuter, kFlag); |
559 | 0 | DrawRing(*terrain_, *flag_, Duels::kFlagReach - 0.2f, Duels::kFlagReach, |
560 | 0 | kFlagEdge); |
561 | 0 | } |
562 | | // raylib batches these, and alpha is its blend already, so ending it |
563 | | // doesn't send them. They go now, while the depth mask's off, or a |
564 | | // telegraph's fill would fight the shape under it for each pixel. |
565 | 3 | rlDrawRenderBatchActive(); |
566 | 3 | EndBlendMode(); |
567 | 3 | rlEnableDepthMask(); |
568 | 3 | } |
569 | | // Last, as we see the ground under it through the water. |
570 | 14 | if (sea_) { |
571 | 3 | sea_->Draw(camera_, atmosphere, cel_shading_ ? kBands0 : 0.0f, clock_); |
572 | 3 | ++work_.draws; |
573 | 3 | work_.triangles += sea_->triangles(); |
574 | 3 | } |
575 | | // Over the sea too, as the rain falls into it. |
576 | 14 | weather_->Draw(camera, atmosphere); |
577 | | // See-through, like the light, but lit as the figures are. |
578 | 14 | fx_->DrawGhosts(characters_); |
579 | | // The skills' light goes last, over everything it lights up. |
580 | 14 | if (terrain_) { |
581 | 3 | fx_->DrawLight(camera, *terrain_, atmosphere, characters_); |
582 | 3 | } |
583 | 14 | EndMode3D(); |
584 | 14 | } |
585 | | |
586 | 1 | void Renderer::DrawPreview(const Figure& figure, const Camera3D& camera) const { |
587 | | // Clear, so that the interface's own background shows round the figure. |
588 | 1 | ClearBackground( |
589 | 1 | ColorFromNormalized({kPreviewBackground[0], kPreviewBackground[1], |
590 | 1 | kPreviewBackground[2], 0.0f})); |
591 | | // Lit like the Meadows, wherever we are. |
592 | 1 | Atmosphere preview = kMeadows.atmosphere; |
593 | 1 | preview.sky = kPreviewBackground; |
594 | 1 | SetAtmosphere(cube_shader_, preview); |
595 | 1 | SetAtmosphere(character_shader_, preview); |
596 | 1 | SetFog(outline_.still, preview); |
597 | 1 | SetFog(outline_.skinned, preview); |
598 | | // Nor under the land's clouds, its body or what it holds. |
599 | 1 | constexpr float kNoShade = 0.0f; |
600 | 2 | for (const Shader& shader : {cube_shader_, character_shader_}) { |
601 | 2 | SetShaderValue(shader, GetShaderLocation(shader, "cloudShade"), &kNoShade, |
602 | 2 | SHADER_UNIFORM_FLOAT); |
603 | 2 | } |
604 | 1 | BeginMode3D(camera); |
605 | 1 | DrawFigure(figure); |
606 | 1 | EndMode3D(); |
607 | 1 | } |
608 | | |
609 | 0 | std::optional<Gait> Renderer::GaitOf(const Figure& figure) const { |
610 | 0 | if (figure.monster) { |
611 | 0 | const auto kind = monsters_.find(figure.kind); |
612 | 0 | return kind != monsters_.end() ? kind->second->GaitOf(figure) |
613 | 0 | : std::nullopt; |
614 | 0 | } |
615 | 0 | return characters_.GaitOf(figure); |
616 | 0 | } |
617 | | |
618 | 10 | void Renderer::DrawFigure(const Figure& figure) const { |
619 | 10 | const Characters* looks = &characters_; |
620 | 10 | if (figure.monster) { |
621 | 1 | const auto kind = monsters_.find(figure.kind); |
622 | 1 | looks = kind != monsters_.end() ? kind->second.get() : nullptr0 ; |
623 | 1 | } |
624 | 10 | Color color = figure.color; |
625 | 10 | if (!figure.monster && !figure.npc9 ) { |
626 | 9 | color = figure.features && figure.features->shirt0 |
627 | 9 | ? ShirtColor(*figure.features->shirt)0 |
628 | 9 | : PlayerColor(figure.id); |
629 | 9 | } |
630 | 10 | if (looks != nullptr && looks->size() > 0) { |
631 | 9 | looks->Draw(figure, color, cel_shading_ ? &outline_0 : nullptr); |
632 | 9 | return; |
633 | 9 | } |
634 | 1 | const Vector3& at = figure.position; |
635 | 1 | DrawModelEx(cube_, {at.x, at.y + 0.5f, at.z}, {0.0f, 1.0f, 0.0f}, 0.0f, |
636 | 1 | {1.0f, 1.0f, 1.0f}, color); |
637 | 1 | } |
638 | | |
639 | 14 | void Renderer::Clear() const { |
640 | 14 | const std::array<float, 3>& sky = atmosphere_.sky; |
641 | 14 | ClearBackground(ColorFromNormalized({sky[0], sky[1], sky[2], 1.0f})); |
642 | 14 | } |
643 | | |
644 | | } // namespace nanommo |