nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
renderer.cc
Go to the documentation of this file.
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
20namespace nanommo {
21
22namespace {
23
24constexpr const char* kVertexShader = R"(#version 330
25in vec3 vertexPosition;
26in vec3 vertexNormal;
27in vec4 vertexColor;
28uniform mat4 mvp;
29uniform mat4 matModel;
30uniform mat4 matNormal;
31out vec3 normal;
32out vec4 color;
33out float depth;
34out vec3 world;
35
36void 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
45constexpr const char* kSkinnedVertexShader = R"(#version 330
46in vec3 vertexPosition;
47in vec3 vertexNormal;
48in vec4 vertexBoneIndices;
49in vec4 vertexBoneWeights;
50in vec4 vertexColor;
51uniform mat4 mvp;
52uniform mat4 matModel;
53uniform mat4 matNormal;
54uniform mat4 boneMatrices[128];
55out vec3 normal;
56out vec4 color;
57out float depth;
58out vec3 world;
59
60void 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.
76constexpr const char* kFragmentShader = R"(#version 330
77in vec3 normal;
78in vec4 color;
79in float depth;
80in vec3 world;
81uniform 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.
84uniform vec4 clouds[64];
85uniform float cloudShade;
86uniform vec3 sun;
87uniform vec3 sunlight;
88uniform vec3 ambient;
89uniform vec3 fogColor;
90uniform float fogNear;
91uniform float fogFar;
92uniform float emission;
93uniform float bands;
94// After rain: from dry to soaked.
95uniform float wetness;
96uniform vec3 eye;
97out vec4 pixel;
98
99float 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
115void 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.
138constexpr const char* kOutlineVertexShader = R"(#version 330
139in vec3 vertexPosition;
140in vec3 vertexNormal;
141uniform mat4 matModel;
142uniform mat4 matNormal;
143uniform mat4 matView;
144uniform mat4 matProjection;
145uniform float thickness;
146out float depth;
147
148void 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.
158constexpr const char* kSkinnedOutlineVertexShader = R"(#version 330
159in vec3 vertexPosition;
160in vec3 vertexNormal;
161in vec4 vertexBoneIndices;
162in vec4 vertexBoneWeights;
163uniform mat4 matModel;
164uniform mat4 matNormal;
165uniform mat4 matView;
166uniform mat4 matProjection;
167uniform mat4 boneMatrices[128];
168uniform float thickness;
169out float depth;
170
171void 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.
185constexpr const char* kOutlineFragmentShader = R"(#version 330
186in float depth;
187uniform vec3 fogColor;
188uniform float fogNear;
189uniform float fogFar;
190out vec4 pixel;
191
192void 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.
200constexpr float kOutlineThickness = 0.04f;
201
202constexpr 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.
205constexpr 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.
209constexpr float kShadowRadius = 0.42f;
210// Around the monster the player picked, in the red of its nameplate.
211constexpr float kRingInner = 0.55f;
212constexpr float kRingOuter = 0.68f;
213constexpr Color kRing = {235, 90, 75, 220};
214// Where the player's walking to.
215constexpr 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.
218constexpr Color kFlag = {255, 211, 77, 230};
219constexpr Color kFlagEdge = {255, 211, 77, 110};
220constexpr 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.
223constexpr float kTelegraphShape = 0.18f;
224constexpr float kTelegraphFill = 0.4f;
225constexpr 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.
228constexpr float kTelegraphRim = 0.12f;
229constexpr float kBand = 0.5f;
230constexpr 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.
235void DrawShadow(const Terrain& terrain, Vector3 at, float radius) {
236 constexpr int kSides = 16;
237 auto corner = [&](float distance, int i, unsigned char alpha) {
238 const float turn = 2.0f * PI * i / kSides;
239 const float x = at.x + distance * std::cos(turn);
240 const float z = at.z + distance * std::sin(turn);
241 rlColor4ub(0, 0, 0, alpha);
242 // A little above it, so the ground doesn't cover it.
243 rlVertex3f(x, terrain.Height(x, z) + 0.03f, z);
244 };
245 const float inner = 0.6f * radius;
246 rlBegin(RL_TRIANGLES);
247 for (int i = 0; i < kSides; ++i) {
248 // The middle, as dark all over, then a ring that fades out.
249 corner(0.0f, i, kShadowDarkness);
250 corner(inner, i + 1, kShadowDarkness);
251 corner(inner, i, kShadowDarkness);
252 corner(inner, i, kShadowDarkness);
253 corner(inner, i + 1, kShadowDarkness);
254 corner(radius, i + 1, 0);
255 corner(inner, i, kShadowDarkness);
256 corner(radius, i + 1, 0);
257 corner(radius, i, 0);
258 }
259 rlEnd();
260}
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.
266void DrawRing(const Terrain& terrain, Vector3 at, float inner, float outer,
267 Color color, float yaw = 0.0f, float arc = 2.0f * PI) {
268 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 const float first = PI / 2.0f - yaw - arc / 2.0f;
271 auto corner = [&](float distance, int i) {
272 const float turn = first + arc * i / sides;
273 const float x = at.x + distance * std::cos(turn);
274 const float z = at.z + distance * std::sin(turn);
275 rlColor4ub(color.r, color.g, color.b, color.a);
276 // A little above the ground, so that the ground doesn't cover it.
277 rlVertex3f(x, terrain.Height(x, z) + 0.04f, z);
278 };
279 rlBegin(RL_TRIANGLES);
280 for (int i = 0; i < sides; ++i) {
281 corner(inner, i);
282 corner(outer, i + 1);
283 corner(outer, i);
284 corner(inner, i);
285 corner(inner, i + 1);
286 corner(outer, i + 1);
287 }
288 rlEnd();
289}
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.
293void DrawTelegraph(const Terrain& terrain, const Telegraph& telegraph) {
294 const monsters::Skill& skill = telegraph.skill;
295 const Color color = GetColor(skill.color());
296 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 const auto slice = [&](float inner, float outer, float alpha, float yaw,
300 float arc) {
301 const int bands = static_cast<int>(std::ceil((outer - inner) / kBand));
302 for (int band = 0; band < bands; ++band) {
303 const float from = inner + static_cast<float>(band) * kBand;
304 DrawRing(terrain, at, from, std::min(from + kBand, outer),
305 Fade(color, alpha), yaw, arc);
306 }
307 };
308 const float arc =
309 skill.shape() == monsters::CONE ? skill.arc() * DEG2RAD : 2.0f * PI;
310 const float size = skill.size();
311 slice(0.0f, size, kTelegraphShape, telegraph.yaw, arc);
312 slice(0.0f, size * telegraph.Filled(), kTelegraphFill, telegraph.yaw, arc);
313 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 if (skill.shape() == monsters::CONE) {
317 const float side = kTelegraphRim / size;
318 for (const float way : {-1.0f, 1.0f}) {
319 slice(0.0f, size, kTelegraphEdge,
320 telegraph.yaw + way * (arc - side) / 2.0f, side);
321 }
322 }
323}
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.
327Atmosphere Lifted(Atmosphere atmosphere, float clear) {
328 constexpr float kNearest = 2.0f;
329 atmosphere.fog_near *= clear;
330 atmosphere.fog_far = kNearest + (atmosphere.fog_far - kNearest) * clear;
331 return atmosphere;
332}
333
334void SetFog(Shader shader, const Atmosphere& atmosphere) {
335 // Into the sky's colour, so that far away things fade into the sky.
336 SetShaderValue(shader, GetShaderLocation(shader, "fogColor"),
337 atmosphere.sky.data(), SHADER_UNIFORM_VEC3);
338 SetShaderValue(shader, GetShaderLocation(shader, "fogNear"),
339 &atmosphere.fog_near, SHADER_UNIFORM_FLOAT);
340 SetShaderValue(shader, GetShaderLocation(shader, "fogFar"),
341 &atmosphere.fog_far, SHADER_UNIFORM_FLOAT);
342}
343
344void SetAtmosphere(Shader shader, const Atmosphere& atmosphere) {
345 SetFog(shader, atmosphere);
346 SetShaderValue(shader, GetShaderLocation(shader, "sun"),
347 atmosphere.sun.data(), SHADER_UNIFORM_VEC3);
348 SetShaderValue(shader, GetShaderLocation(shader, "sunlight"),
349 atmosphere.sunlight.data(), SHADER_UNIFORM_VEC3);
350 SetShaderValue(shader, GetShaderLocation(shader, "ambient"),
351 atmosphere.ambient.data(), SHADER_UNIFORM_VEC3);
352 SetShaderValue(shader, GetShaderLocation(shader, "wetness"),
353 &atmosphere.wetness, SHADER_UNIFORM_FLOAT);
354}
355
356} // namespace
357
359 : cube_shader_(LoadGlsl(kVertexShader, kFragmentShader)),
360 character_shader_(LoadGlsl(kSkinnedVertexShader, kFragmentShader)),
361 outline_{.still = LoadGlsl(kOutlineVertexShader, kOutlineFragmentShader),
362 .skinned = LoadGlsl(kSkinnedOutlineVertexShader,
363 kOutlineFragmentShader)},
364 cube_(LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f))),
365 disc_(LoadModelFromMesh(Disc().Upload())),
366 spire_(LoadModelFromMesh(Spire().Upload())),
367 characters_(content.rig, character_shader_, content.figures),
368 fx_(std::make_unique<Fx>(content.skills, cube_shader_, content.items)),
369 sky_dome_(std::make_unique<Sky>()),
370 weather_(std::make_unique<Weather>()) {
371 for (Model* model : {&cube_, &disc_, &spire_}) {
372 model->materials[0].shader = cube_shader_;
373 }
374 for (const Shader& shader : {outline_.still, outline_.skinned}) {
375 SetShaderValue(shader, GetShaderLocation(shader, "thickness"),
376 &kOutlineThickness, SHADER_UNIFORM_FLOAT);
377 }
378 // Weapons have no bones, so they're drawn with the cube's shader.
379 for (const auto& [id, item] : content.items.by_id) {
380 characters_.Hold(id, item.look(), cube_shader_);
381 characters_.Wear(id, item.look());
382 }
383 for (const auto& [id, skill] : content.skills.by_id) {
384 characters_.Learn(id, skill.look());
385 }
386 // Two kinds with one model load it twice, which is fine for a few kinds.
387 for (const auto& [id, kind] : content.monsters.kinds) {
388 monsters_[id] = std::make_unique<Characters>(
389 MonstersFolder(), std::vector<std::string>{kind.look().model()},
390 kind.look().height(), character_shader_);
391 }
392
393 tree_ = LoadFaces(TreesFolder() + "/tree.glb");
394}
395
396// UnloadModel leaves the model's shader alone, as models can share one.
398 UnloadTerrain();
399 UnloadModel(cube_);
400 UnloadModel(disc_);
401 UnloadModel(spire_);
402 UnloadShader(character_shader_);
403 UnloadShader(outline_.still);
404 UnloadShader(outline_.skinned);
405 UnloadShader(cube_shader_);
406}
407
408void Renderer::set_terrain(const Terrain& terrain, int land) {
409 UnloadTerrain();
410 terrain_ = terrain;
411 land_ = land;
412 const Look& look = kLooks[static_cast<std::size_t>(land) % kLooks.size()];
413 atmosphere_ = look.atmosphere;
414 weather_->set_land(terrain, land);
415 // The ground and the trees have no bones either.
416 chunks_ = LoadChunks(terrain, look, tree_, cube_shader_);
417 sea_ = std::make_unique<Sea>(terrain, look);
418 sky_dome_->Gather(terrain.seed());
419}
420
422 if (on == cel_shading_) {
423 return;
424 }
425 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 const float bands = on ? kBands : 0.0f;
429 for (const Shader& shader : {cube_shader_, character_shader_}) {
430 SetShaderValue(shader, GetShaderLocation(shader, "bands"), &bands,
431 SHADER_UNIFORM_FLOAT);
432 }
433}
434
435void Renderer::Animate(float seconds) {
436 fx_->Update(figures_, seconds);
437 reactions_.Update(seconds);
438 reactions_.Apply(figures_);
439 fx_->Hold(figures_);
440 clock_ += seconds;
441 // Without the time of day, it's always noon for what glows.
442 protocol::Sky shown = sky_.value_or(protocol::Sky());
443 if (!sky_ || !showing_.day_night) {
444 shown.set_day(0.5f);
445 }
446 weather_->Update(seconds, shown, sky_.has_value() && showing_.weather,
447 camera_);
448 const Look& look = kLooks[static_cast<std::size_t>(land_) % kLooks.size()];
449 atmosphere_ =
450 sky_ ? AtmosphereUnder(land_, *sky_, showing_) : look.atmosphere;
451 atmosphere_.wetness = weather_->wetness();
452 // A bolt lights up the sky, and everything under it, for a moment.
453 const float flash = weather_->flash();
454 for (int c = 0; c < 3; ++c) {
455 atmosphere_.sky[c] += flash * 0.55f;
456 atmosphere_.zenith[c] += flash * 0.4f;
457 atmosphere_.ambient[c] += flash * 0.45f;
458 atmosphere_.clouds[c] += flash * 0.6f;
459 }
460 if (adjust_) {
461 adjust_(atmosphere_);
462 }
463}
464
465void Renderer::UnloadTerrain() {
466 UnloadChunks(chunks_);
467 sea_.reset();
468}
469
470void Renderer::DrawCounted(const Mesh& mesh, const Material& material,
471 const Matrix& transform) const {
472 DrawMesh(mesh, material, transform);
473 ++work_.draws;
474 work_.triangles += mesh.triangleCount;
475}
476
477void Renderer::Draw() const {
478 work_ = {};
479 Clear();
480 const Atmosphere atmosphere = Lifted(atmosphere_, clear_);
481 SetAtmosphere(cube_shader_, atmosphere);
482 SetAtmosphere(character_shader_, atmosphere);
483 SetFog(outline_.still, atmosphere);
484 SetFog(outline_.skinned, atmosphere);
485 for (const Shader& shader : {cube_shader_, character_shader_}) {
486 SetShaderValue(shader, GetShaderLocation(shader, "eye"), &camera_.position,
487 SHADER_UNIFORM_VEC3);
488 }
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 Camera3D camera = camera_;
492 const Vector3 toward = Vector3Subtract(camera_.target, camera_.position);
493 const float zoom = std::min(fx_->Zoom(), 0.6f * Vector3Length(toward));
494 camera.position =
495 Vector3Add(camera.position, Vector3Scale(Vector3Normalize(toward), zoom));
496 const Vector3 shake = fx_->Shake(camera_.position);
497 camera.position = Vector3Add(camera.position, shake);
498 camera.target = Vector3Add(camera.target, shake);
499 if (terrain_) {
500 sky_dome_->Shade(cube_shader_, camera, atmosphere, clock_);
501 sky_dome_->Shade(character_shader_, camera, atmosphere, clock_);
502 }
503 BeginMode3D(camera);
504 // First, as it's behind everything.
505 if (terrain_) {
506 sky_dome_->Draw(camera, atmosphere, clock_);
507 }
508 // As BeginMode3D set them up, so we cull with what the GPU will use.
509 const Matrix view_projection =
510 MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
511 for (const Chunk& chunk : chunks_) {
512 if (InSight(chunk.box, camera_.position, view_projection,
513 atmosphere.fog_far)) {
514 DrawCounted(chunk.model.meshes[0], chunk.model.materials[0],
515 MatrixIdentity());
516 }
517 }
518 for (const Cube& cube : cubes_) {
519 const Model& model = cube.shape == Shape::kDisc ? disc_
520 : cube.shape == Shape::kSpire ? spire_
521 : cube_;
522 DrawModelEx(model, cube.position, {0.0f, 1.0f, 0.0f}, 0.0f, cube.size,
523 cube.color);
524 }
525 if (flag_) {
526 DrawModelEx(spire_, {flag_->x, flag_->y + kPole.y / 2.0f, flag_->z},
527 {0.0f, 1.0f, 0.0f}, 0.0f, kPole, kFlag);
528 }
529 for (const Figure& figure : figures_) {
530 DrawFigure(figure);
531 }
532 if (terrain_) {
533 fx_->DrawSolid(*terrain_);
534 }
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 if (terrain_) {
538 rlDisableDepthMask();
539 BeginBlendMode(BLEND_ALPHA);
540 for (const Figure& figure : figures_) {
541 DrawShadow(*terrain_, figure.position, kShadowRadius);
542 }
543 for (const auto& [monster, telegraph] : telegraphs_) {
544 DrawTelegraph(*terrain_, telegraph);
545 }
546 // After all the shadows, so that none of them, such as a player's
547 // beside the monster, darkens the ring.
548 for (const Figure& figure : figures_) {
549 if (Picked(figure, target_, target_player_)) {
550 DrawRing(*terrain_, figure.position, kRingInner, kRingOuter, kRing);
551 }
552 }
553 if (destination_) {
554 DrawRing(*terrain_, *destination_, kRingInner * 0.6f, kRingOuter * 0.6f,
555 kDestination);
556 }
557 if (flag_) {
558 DrawRing(*terrain_, *flag_, kRingInner, kRingOuter, kFlag);
559 DrawRing(*terrain_, *flag_, Duels::kFlagReach - 0.2f, Duels::kFlagReach,
560 kFlagEdge);
561 }
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 rlDrawRenderBatchActive();
566 EndBlendMode();
567 rlEnableDepthMask();
568 }
569 // Last, as we see the ground under it through the water.
570 if (sea_) {
571 sea_->Draw(camera_, atmosphere, cel_shading_ ? kBands : 0.0f, clock_);
572 ++work_.draws;
573 work_.triangles += sea_->triangles();
574 }
575 // Over the sea too, as the rain falls into it.
576 weather_->Draw(camera, atmosphere);
577 // See-through, like the light, but lit as the figures are.
578 fx_->DrawGhosts(characters_);
579 // The skills' light goes last, over everything it lights up.
580 if (terrain_) {
581 fx_->DrawLight(camera, *terrain_, atmosphere, characters_);
582 }
583 EndMode3D();
584}
585
586void Renderer::DrawPreview(const Figure& figure, const Camera3D& camera) const {
587 // Clear, so that the interface's own background shows round the figure.
588 ClearBackground(
589 ColorFromNormalized({kPreviewBackground[0], kPreviewBackground[1],
590 kPreviewBackground[2], 0.0f}));
591 // Lit like the Meadows, wherever we are.
592 Atmosphere preview = kMeadows.atmosphere;
593 preview.sky = kPreviewBackground;
594 SetAtmosphere(cube_shader_, preview);
595 SetAtmosphere(character_shader_, preview);
596 SetFog(outline_.still, preview);
597 SetFog(outline_.skinned, preview);
598 // Nor under the land's clouds, its body or what it holds.
599 constexpr float kNoShade = 0.0f;
600 for (const Shader& shader : {cube_shader_, character_shader_}) {
601 SetShaderValue(shader, GetShaderLocation(shader, "cloudShade"), &kNoShade,
602 SHADER_UNIFORM_FLOAT);
603 }
604 BeginMode3D(camera);
605 DrawFigure(figure);
606 EndMode3D();
607}
608
609std::optional<Gait> Renderer::GaitOf(const Figure& figure) const {
610 if (figure.monster) {
611 const auto kind = monsters_.find(figure.kind);
612 return kind != monsters_.end() ? kind->second->GaitOf(figure)
613 : std::nullopt;
614 }
615 return characters_.GaitOf(figure);
616}
617
618void Renderer::DrawFigure(const Figure& figure) const {
619 const Characters* looks = &characters_;
620 if (figure.monster) {
621 const auto kind = monsters_.find(figure.kind);
622 looks = kind != monsters_.end() ? kind->second.get() : nullptr;
623 }
624 Color color = figure.color;
625 if (!figure.monster && !figure.npc) {
626 color = figure.features && figure.features->shirt
627 ? ShirtColor(*figure.features->shirt)
628 : PlayerColor(figure.id);
629 }
630 if (looks != nullptr && looks->size() > 0) {
631 looks->Draw(figure, color, cel_shading_ ? &outline_ : nullptr);
632 return;
633 }
634 const Vector3& at = figure.position;
635 DrawModelEx(cube_, {at.x, at.y + 0.5f, at.z}, {0.0f, 1.0f, 0.0f}, 0.0f,
636 {1.0f, 1.0f, 1.0f}, color);
637}
638
639void Renderer::Clear() const {
640 const std::array<float, 3>& sky = atmosphere_.sky;
641 ClearBackground(ColorFromNormalized({sky[0], sky[1], sky[2], 1.0f}));
642}
643
644} // namespace nanommo
std::size_t size() const
Definition characters.h:48
void Draw(const Figure &figure, Color color, const Outline *outline) const
static constexpr float kFlagReach
Definition duels.h:32
std::optional< Gait > GaitOf(const Figure &figure) const
Definition renderer.cc:609
const Atmosphere & atmosphere() const
The atmosphere as the last frame drew it.
Definition renderer.h:70
Renderer(const Content &content=Content())
Definition renderer.cc:358
void set_cel_shading(bool on)
Cel shading's bands and outlines, on or off. It starts off.
Definition renderer.cc:421
void DrawPreview(const Figure &figure, const Camera3D &camera) const
Draws one figure on a plain background, for previews in the UI.
Definition renderer.cc:586
void set_terrain(const Terrain &terrain, int land)
Definition renderer.cc:408
void Animate(float seconds)
Definition renderer.cc:435
const std::vector< Figure > & figures() const
Definition renderer.h:96
void Draw() const
Definition renderer.cc:477
const Camera3D & camera() const
Definition renderer.h:76
void Clear() const
Definition renderer.cc:639
uint32_t seed() const
Definition terrain.h:82
Definition chat.cc:9
bool InSight(const BoundingBox &box, Vector3 eye, const Matrix &view_projection, float farthest)
Definition chunks.cc:55
constexpr std::array< Look, 3 > kLooks
In the order of the lands.
Definition atmosphere.h:164
Atmosphere AtmosphereUnder(int land, const protocol::Sky &sky, const Showing &showing)
Definition daylight.cc:237
Triangles Spire()
Definition cube.cc:59
std::vector< Face > LoadFaces(const std::string &path)
Definition trees.cc:30
Shader LoadGlsl(const char *vertex, const char *fragment)
Definition glsl.cc:32
std::vector< Chunk > LoadChunks(const Terrain &terrain, const Look &look, const std::vector< Face > &tree, Shader shader)
Definition chunks.cc:14
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
Definition picking.cc:86
std::string TreesFolder()
Definition trees.cc:26
constexpr Look kMeadows
Definition atmosphere.h:80
void UnloadChunks(std::vector< Chunk > &chunks)
Definition chunks.cc:48
Triangles Disc()
A disc a unit across and a unit tall, around its middle, with flat sides.
Definition cube.cc:39
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
ItemTable items
Definition content.h:27
MonsterTable monsters
Definition content.h:28
SkillTable skills
Definition content.h:36
uint32_t kind
A monster's or an NPC's kind, by its id in the content folder.
Definition animator.h:33
Color color
A monster's tint, or the colour of what an NPC wears that's dyed.
Definition animator.h:35
Vector3 position
Definition animator.h:63
std::optional< Features > features
Definition animator.h:38
std::map< uint32_t, items::Item > by_id
Definition items.h:19
A land's colours: its atmosphere, and the colours that go into its meshes.
Definition atmosphere.h:69
Atmosphere atmosphere
Definition atmosphere.h:70
std::map< uint32_t, monsters::Kind > kinds
Definition monsters.h:18
std::map< uint32_t, skills::Skill > by_id
Definition skills.h:17