24constexpr const char* kVertexShader = R
"(#version 330
25in vec3 vertexPosition;
30uniform mat4 matNormal;
37 normal = mat3(matNormal) * vertexNormal;
39 world = (matModel * vec4(vertexPosition, 1.0)).xyz;
40 gl_Position = mvp * vec4(vertexPosition, 1.0);
41 depth = gl_Position.w;
45constexpr const char* kSkinnedVertexShader = R
"(#version 330
46in vec3 vertexPosition;
48in vec4 vertexBoneIndices;
49in vec4 vertexBoneWeights;
53uniform mat4 matNormal;
54uniform mat4 boneMatrices[128];
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;
67 world = (matModel * skin * vec4(vertexPosition, 1.0)).xyz;
68 gl_Position = mvp * skin * vec4(vertexPosition, 1.0);
69 depth = gl_Position.w;
76constexpr const char* kFragmentShader = R
"(#version 330
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;
92uniform float emission;
94// After rain: from dry to soaked.
100 vec3 toward = normalize(sun);
102 for (int i = 0; i < 64; ++i) {
103 vec4 cloud = clouds[i];
104 if (cloud.w <= 0.0) {
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);
112 return max(shade, 1.0 - cloudShade);
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.
120 sunlit = min(floor(sunlit * bands), bands - 1.0) / (bands - 1.0);
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);
138constexpr const char* kOutlineVertexShader = R
"(#version 330
139in vec3 vertexPosition;
141uniform mat4 matModel;
142uniform mat4 matNormal;
144uniform mat4 matProjection;
145uniform float thickness;
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;
158constexpr const char* kSkinnedOutlineVertexShader = R
"(#version 330
159in vec3 vertexPosition;
161in vec4 vertexBoneIndices;
162in vec4 vertexBoneWeights;
163uniform mat4 matModel;
164uniform mat4 matNormal;
166uniform mat4 matProjection;
167uniform mat4 boneMatrices[128];
168uniform float thickness;
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;
185constexpr const char* kOutlineFragmentShader = R
"(#version 330
187uniform vec3 fogColor;
188uniform float fogNear;
193 float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0);
194 pixel = vec4(mix(vec3(0.05), fogColor, fog), 1.0);
200constexpr float kOutlineThickness = 0.04f;
202constexpr std::array<float, 3> kPreviewBackground = {0.1f, 0.04f, 0.05f};
205constexpr float kBands = 3.0f;
209constexpr float kShadowRadius = 0.42f;
211constexpr float kRingInner = 0.55f;
212constexpr float kRingOuter = 0.68f;
213constexpr Color kRing = {235, 90, 75, 220};
215constexpr Color kDestination = {255, 255, 255, 200};
218constexpr Color kFlag = {255, 211, 77, 230};
219constexpr Color kFlagEdge = {255, 211, 77, 110};
220constexpr Vector3 kPole{0.12f, 2.4f, 0.12f};
223constexpr float kTelegraphShape = 0.18f;
224constexpr float kTelegraphFill = 0.4f;
225constexpr float kTelegraphEdge = 0.85f;
228constexpr float kTelegraphRim = 0.12f;
229constexpr float kBand = 0.5f;
230constexpr unsigned char kShadowDarkness = 82;
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);
243 rlVertex3f(x, terrain.Height(x, z) + 0.03f, z);
245 const float inner = 0.6f * radius;
246 rlBegin(RL_TRIANGLES);
247 for (
int i = 0; i < kSides; ++i) {
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);
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));
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);
277 rlVertex3f(x, terrain.Height(x, z) + 0.04f, z);
279 rlBegin(RL_TRIANGLES);
280 for (
int i = 0; i < sides; ++i) {
282 corner(outer, i + 1);
285 corner(inner, i + 1);
286 corner(outer, i + 1);
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};
299 const auto slice = [&](
float inner,
float outer,
float alpha,
float yaw,
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);
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);
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);
328 constexpr float kNearest = 2.0f;
330 atmosphere.fog_far = kNearest + (atmosphere.fog_far - kNearest) * clear;
334void SetFog(Shader shader,
const Atmosphere& atmosphere) {
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);
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);
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_;
374 for (
const Shader& shader : {outline_.still, outline_.skinned}) {
375 SetShaderValue(shader, GetShaderLocation(shader,
"thickness"),
376 &kOutlineThickness, SHADER_UNIFORM_FLOAT);
379 for (
const auto& [
id, item] : content.
items.
by_id) {
380 characters_.Hold(id, item.look(), cube_shader_);
381 characters_.Wear(id, item.look());
383 for (
const auto& [
id, skill] : content.
skills.
by_id) {
384 characters_.Learn(id, skill.look());
388 monsters_[id] = std::make_unique<Characters>(
389 MonstersFolder(), std::vector<std::string>{kind.look().model()},
390 kind.look().height(), character_shader_);
402 UnloadShader(character_shader_);
403 UnloadShader(outline_.still);
404 UnloadShader(outline_.skinned);
405 UnloadShader(cube_shader_);
414 weather_->set_land(terrain, land);
416 chunks_ =
LoadChunks(terrain, look, tree_, cube_shader_);
417 sea_ = std::make_unique<Sea>(terrain, look);
418 sky_dome_->Gather(terrain.
seed());
422 if (on == cel_shading_) {
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);
436 fx_->Update(figures_, seconds);
437 reactions_.Update(seconds);
438 reactions_.Apply(figures_);
442 protocol::Sky shown = sky_.value_or(protocol::Sky());
443 if (!sky_ || !showing_.day_night) {
446 weather_->Update(seconds, shown, sky_.has_value() && showing_.weather,
451 atmosphere_.
wetness = weather_->wetness();
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;
461 adjust_(atmosphere_);
465void Renderer::UnloadTerrain() {
470void Renderer::DrawCounted(
const Mesh& mesh,
const Material& material,
471 const Matrix& transform)
const {
472 DrawMesh(mesh, material, transform);
474 work_.triangles += mesh.triangleCount;
485 for (
const Shader& shader : {cube_shader_, character_shader_}) {
486 SetShaderValue(shader, GetShaderLocation(shader,
"eye"), &camera_.position,
487 SHADER_UNIFORM_VEC3);
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));
495 Vector3Add(
camera.position, Vector3Scale(Vector3Normalize(toward), zoom));
496 const Vector3 shake = fx_->Shake(camera_.position);
509 const Matrix view_projection =
510 MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
511 for (
const Chunk& chunk : chunks_) {
512 if (
InSight(chunk.box, camera_.position, view_projection,
514 DrawCounted(chunk.model.meshes[0], chunk.model.materials[0],
518 for (
const Cube& cube : cubes_) {
522 DrawModelEx(model, cube.position, {0.0f, 1.0f, 0.0f}, 0.0f, cube.size,
526 DrawModelEx(spire_, {flag_->x, flag_->y + kPole.y / 2.0f, flag_->z},
527 {0.0f, 1.0f, 0.0f}, 0.0f, kPole, kFlag);
529 for (
const Figure& figure : figures_) {
533 fx_->DrawSolid(*terrain_);
538 rlDisableDepthMask();
539 BeginBlendMode(BLEND_ALPHA);
540 for (
const Figure& figure : figures_) {
541 DrawShadow(*terrain_, figure.position, kShadowRadius);
543 for (
const auto& [monster, telegraph] : telegraphs_) {
544 DrawTelegraph(*terrain_, telegraph);
548 for (
const Figure& figure : figures_) {
549 if (
Picked(figure, target_, target_player_)) {
550 DrawRing(*terrain_, figure.position, kRingInner, kRingOuter, kRing);
554 DrawRing(*terrain_, *destination_, kRingInner * 0.6f, kRingOuter * 0.6f,
558 DrawRing(*terrain_, *flag_, kRingInner, kRingOuter, kFlag);
565 rlDrawRenderBatchActive();
571 sea_->Draw(camera_,
atmosphere, cel_shading_ ? kBands : 0.0f, clock_);
573 work_.triangles += sea_->triangles();
578 fx_->DrawGhosts(characters_);
589 ColorFromNormalized({kPreviewBackground[0], kPreviewBackground[1],
590 kPreviewBackground[2], 0.0f}));
593 preview.sky = kPreviewBackground;
594 SetAtmosphere(cube_shader_, preview);
595 SetAtmosphere(character_shader_, preview);
596 SetFog(outline_.still, preview);
597 SetFog(outline_.skinned, preview);
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);
611 const auto kind = monsters_.find(figure.
kind);
612 return kind != monsters_.end() ? kind->second->GaitOf(figure)
615 return characters_.GaitOf(figure);
618void Renderer::DrawFigure(
const Figure& figure)
const {
621 const auto kind = monsters_.find(figure.
kind);
622 looks = kind != monsters_.end() ? kind->second.get() :
nullptr;
624 Color color = figure.
color;
627 ? ShirtColor(*figure.
features->shirt)
628 : PlayerColor(figure.id);
630 if (looks !=
nullptr && looks->
size() > 0) {
631 looks->
Draw(figure, color, cel_shading_ ? &outline_ :
nullptr);
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);
640 const std::array<float, 3>& sky = atmosphere_.sky;
641 ClearBackground(ColorFromNormalized({sky[0], sky[1], sky[2], 1.0f}));
void Draw(const Figure &figure, Color color, const Outline *outline) const
static constexpr float kFlagReach
std::optional< Gait > GaitOf(const Figure &figure) const
const Atmosphere & atmosphere() const
The atmosphere as the last frame drew it.
Renderer(const Content &content=Content())
void set_cel_shading(bool on)
Cel shading's bands and outlines, on or off. It starts off.
void DrawPreview(const Figure &figure, const Camera3D &camera) const
Draws one figure on a plain background, for previews in the UI.
void set_terrain(const Terrain &terrain, int land)
void Animate(float seconds)
const std::vector< Figure > & figures() const
const Camera3D & camera() const
bool InSight(const BoundingBox &box, Vector3 eye, const Matrix &view_projection, float farthest)
constexpr std::array< Look, 3 > kLooks
In the order of the lands.
Atmosphere AtmosphereUnder(int land, const protocol::Sky &sky, const Showing &showing)
std::vector< Face > LoadFaces(const std::string &path)
Shader LoadGlsl(const char *vertex, const char *fragment)
std::vector< Chunk > LoadChunks(const Terrain &terrain, const Look &look, const std::vector< Face > &tree, Shader shader)
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
std::string TreesFolder()
void UnloadChunks(std::vector< Chunk > &chunks)
Triangles Disc()
A disc a unit across and a unit tall, around its middle, with flat sides.
The sky and the light, which the shaders read each frame.
std::map< uint32_t, items::Item > by_id
A land's colours: its atmosphere, and the colours that go into its meshes.
std::map< uint32_t, monsters::Kind > kinds
std::map< uint32_t, skills::Skill > by_id