We replace our MMORPG's Mixamo characters with low poly people, made of parts that a protocol buffer file describes: limbs with round ends and balls, each bound to one bone of the bots' skeletons with raylib's GPU skinning, so their animations still pose them. First a look at what we tried in a lab, people in clothes, one-colour stick figures, a skeleton and a spider with inverse kinematics, and why we kept the people. Each player gets a skin and a way to wear their hair from their id, the outline gets a hull with smooth normals, the server sends what each player wears, and an item's look gets parts of its own. A new player starts in a shirt of their own colour, trousers and shoes, which are items, so the leather cap, tunic, boots and buckler show on whoever wears them too, with a shadow under each figure. This is episode 44 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
Topics: Graphics
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep044/end.
- 4:57
ep044/step-1A figure in the contentBrowseChanges - 18:05
ep044/step-2On the bots' bonesBrowseChanges - 32:01
ep044/step-3The outlineBrowseChanges - 35:02
ep044/step-4What players wearBrowseChanges - 36:53
ep044/step-5Clothes as itemsBrowseChanges - 43:17
ep044/step-6Clothes to start inBrowseChanges - 48:40
ep044/step-7Boots and a bucklerBrowseChanges - 50:29
ep044/step-8ShadowsBrowseChanges
Transcript
Intro
0:00 Hello and welcome back to episode forty-four, where we're going to draw our players as low poly people, built from parts that the content describes, each with their own skin and hair, and give them clothes that are items. Let's begin with a quick look at where we're going to be by the end of the episode.
0:18 arthur is a low poly person now, and the clothes are items. Each part moves with a bone of the character we had, so arthur walks the way it did, with a shadow underneath.
0:29 On the website of the series you can find more information about the project. The code for the project is open source on GitHub, tagged at each step of each episode so that you can follow along.
Last time
0:41 Now, before we get to today's work, let's quickly go over what we did last time. We tried three ways of making our sea move. Ripples move the light on a flat sea, in the fragment shader. Waves move a grid of triangles around the camera, in the vertex shader. And facets light each of those triangles flat, which we kept, as they match our low-poly world. The vertex shader reads the ground's heights from a texture, so it knows how deep the water is, and the waves calm down at the shore, where the water foams. And each land got a liquid of its own, so the lava in the Ashlands is thick and slow, and glows at the tops of its waves.
Today
1:20 Our players are Mixamo's X Bot and Y Bot, which stay out of the repo, as Mixamo's terms don't let us share its files, so a fresh clone draws cubes. I tried, in a lab branch, drawing figures of our own on the bots' bones, out of simple parts, and we're going to look at what came out, and pick a look. Then we're going to describe a person in the content, as parts, with a skin and hair each, and dress the bots' skeletons in it, so that their clips pose it. The cel shading's outline gets a mesh of its own, and the server sends what each player wears. An item's look gets parts of its own, so a new player's shirt, trousers and shoes, or a cap and a tunic, show on whoever wears them. And each figure gets a shadow under it. All right, let's get started, with what the lab came up with.
What we tried
2:08 On the left is X Bot, as the game draws it now. The rest are figures that the lab draws on X Bot's and Y Bot's skeletons, without their meshes. Two are sticks of one colour, with a ball for the head, like the figures of a stick figure animation app, and six are people in clothes, with hair, beards and belts. Each part is a simple shape, a stick with round ends, a low-poly ball, a ring or a cone, and each moves with one bone, all the way. So Mixamo's clips pose them the way they posed X Bot's mesh, the walk, and the swing, with the sword in the right hand, and the death.
2:46 Next to X Bot is Quaternius's skeleton, which our monsters are now, and then a skeleton of parts on its rig, with a skull, glowing eye sockets, ribs, and knobbly joints. The spider has a rig of its own, with twenty-six bones, which no clip animates, so the lab poses it in code. It rears up to attack, with its front legs raised, and strikes, and when it dies, it sinks, and draws its legs in, while the skeleton falls into a pile of bones.
3:13 As it walks, each foot stays where it is on the ground, until the body has left it behind, then steps a little ahead of where it would rest, by about as far as the body goes in a step. The feet step four at a time, the first and third on one side with the second and fourth on the other. Between the hip and the ankle, two-bone inverse kinematics puts the knee, from the lengths of the two bones, and the body leans with the ground under its feet. It's how a creature with no animations can still move well, and it's a big part of why I'd like the spider in the game one day.
3:44 From the game's camera, fifteen metres away, the figures are small. The sticks read as a colour each, with a big head. The people read as a shirt, trousers and a head of hair, which is enough to tell them apart, and they look like the people of a world. With cel shading on, each figure gets the game's outline, five centimetres out along its normals, which is a lot on limbs this thin.
4:09 So we're going to keep the people, as they look like the people of a world, and each player gets a skin and a way to wear their hair, from a small set. What they wear is going to be items. A cap or a tunic is an item already, and its look is going to add parts over the body, so what you loot shows on you, for everyone to see. A new player starts in a shirt, trousers and shoes, and the shirt takes the player's colour, so we can still tell players apart from far away. The outline is going to be a little thinner, and each figure gets a shadow, as the lab's did. The skeleton and the spider stay in the lab for now, as the monsters are a job of their own. And the figures go on the bots' skeletons, with their clips, so a clone still needs Mixamo's files to see them, until we have a rig and clips of our own.
A figure in the content
4:57 A figure is going to be content, like our items and monsters, so it starts with a protocol buffer file, figures dot proto, in the content folder, in a package of its own. Offsets come first, as most of what we're going to write is where a part goes. An offset is an x, a y and a z, in metres. Its axes are the figure's own, as it stands in the pose its rig was made in: x to its left, y up and z forward. Then a part, a shape that moves with one of the figure's bones. Its shape is an enum, which starts at none, as proto three gives a field we leave out its zero, so a part we forget to give a shape stays as none, and the content can refuse it. A limb is a stick with round ends, which can be thicker at one end, for an arm or a leg, and a ball can be an egg, when its radii differ, for a head or a hand. The bone it moves with goes by its name in Mixamo's rig, such as LeftArm. A part starts at a bone's joint, from, or its own bone's when that's empty, moved by an offset, and a ball's middle is there. A limb ends at another joint, moved by an offset too, so a limb can stop short of a joint, or go past it. Its radius at each end comes next, and flatten makes a limb thinner from front to back, for a chest, or from top to bottom, for a foot along the ground, as a share of its width. A limb without one is round, as wide each way. A ball has its three radii, and each part has a colour, as a hex number like our tints. A white part takes on the colour it's drawn in, the skin's on the body, and the hair's on the hair, which is how each player is going to have a skin and hair of their own. And mirrored puts a part on the left, and its twin on the right, so that we write the arms and the legs once. A part can belong to the body or to something the figure wears, and its lengths are in metres, for a figure one point eight metres tall. A way to wear the hair is a list of parts over the head, in white, so that they take on the hair's colour, and one with no parts is bald. Last comes the file's message, with a player's body, under what they wear, as a list of parts. Then the skins and the hair colours that players can have, as hex colours, and the ways to wear the hair. Each player has one of each, which their id picks. Figures dot h reads the file and checks it. We take c std int, string view and vector, abseil's status and status or, the header protoc generates from our proto, and protobuf's repeated pointer field, which is how a repeated message comes in C++. Check part says what's wrong with a part, if anything: no shape, no bone to move with, a limb with no end or no thickness, or a ball with no size. Parse figures reads the file in text format, like our other content, or says what's wrong with it. That's a field that figures dot proto doesn't have, or a part that's wrong, in the body or in a way to wear the hair. Features is what a player looks like under what they wear: their skin, and their hair's colour, each white when there are none to pick from. Then which way they wear their hair, by its place in the list, or minus one when there are none. Features of picks them from the figures' lists by the player's id, so that each client draws a player alike. And unfold makes the list of parts that gets drawn, each mirrored one followed by its twin on the right, on the right's bones, with its offsets the other way across. It takes the parts as protobuf hands them over, and gives back a plain vector. In figures dot c c, we take string, string view and vector, abseil's str cat, for the errors, and our text header, which parses protobuf's text format and says on which line it went wrong. To the right turns a bone on the left into its twin on the right, as Mixamo names a left bone with Left where its twin has Right, such as LeftArm and RightArm. So a name that starts with Left gets Right instead, and any other name, such as the head's, stays as it is. Check part refuses a part with no bone first, and keeps a reference to the bone's name, for the messages that follow. Then it goes by its shape, in a switch, which keeps the three cases side by side. A limb needs a bone to end at, and a radius above zero at each end, or it would come out as nothing, with no error. Its message names the limb's bone, and says what a limb needs. A ball needs a size above zero each way, as a ball's three radii are separate, so that an egg can be any shape, and a ball with one of them at zero would be flat, with nothing to see. Anything else has no shape, which is what a part we forgot to give one gets. Each refusal of a shape names the part's bone, as a figure has a lot of parts, and a message that says which bone it's on takes us straight to the line in the file. Parse figures starts with an empty message for the file, then parses the text into it with parse text, the helper the other content files use, and hands back its error if that fails. Then it checks each part of the body. So a part that's missing its size or its end stops the game from starting, with a message that names its bone, instead of a figure that quietly draws nothing there. Then each part of each way to wear the hair, with a message that starts by saying it's the hair's, as a hair's parts are on the head's bone too. A message that named the head alone would send us to the body's head first. Features of multiplies the id by a large odd number, which scatters its bits, so that players who join one after another don't look alike. Pick shifts that down by a byte for each feature, a different byte for each, and takes what's left over when it's divided by the list's length, which is a place in the list. That's from zero to one less than the list's length. Each list that has something in it gives its pick, the skin, the hair's colour, and the way to wear it, and a list that's empty leaves its feature as features starts it. Unfold goes through the parts in order, into a vector of its own, and keeps each one. A part that isn't mirrored is done with there. For a mirrored one, it adds a copy, on the bones to the right, with the x of each offset turned the other way, as x goes to the figure's left. The to offset turns round like the from offset, for limbs that end off a joint, and the copy keeps its mirrored flag, which nothing reads after unfold. In the content's CMake lists, figures dot proto goes in with the other protos, and figures dot c c with the sources. Then the generated figures dot p b dot c c is compiled without warnings, like the others, as protoc writes it.
11:26 The person goes in figures dot text p b, which starts like our other content files, with the two lines that tell an editor which proto and which message it holds. Then what a player is: a person under what they wear. There's a limb for each bone of the arms and legs, a flat one for each foot, and one each for the chest, the belly and the neck. Then a ball for each hand, the hips and the head, which has eyes. Each part is placed on Mixamo's rig as it stands in its T-pose, with its arms out to the sides, and each part but the eyes is white, which takes on the player's skin. Each limb is a stick with round ends, so where two meet at a joint, their ends overlap, and the joint looks round. The arm comes first, a limb on the left arm's bone, from the shoulder to the elbow, which is where the forearm starts. Its radius is five point eight centimetres at the shoulder and five at the elbow, and it's mirrored, so the right arm gets one too. The forearm goes on from the elbow to the wrist, a little thinner again. The hand is a ball on the hand's bone, five centimetres past the wrist along the arm, and thicker than the wrist, so it reads as a fist. Each of the three is on its own bone, so the arm bends at the elbow and the wrist the way the bot's did. The leg is two limbs, the thigh, from the hip to the knee, and the shin, from the knee to the ankle, each a little thinner at its lower end. The foot goes from the heel, a little behind and below the ankle, to the ball of the foot, a little above and ahead of the toes' joint. Flatten makes it seventy percent as thick as it's wide, as it lies along the ground. The foot is on the foot's bone, so it turns at the ankle as the bot's foot does when it steps. The chest goes from the middle of the spine to ten centimetres under the neck, on the bone of the chest, and it's widest at the top, where its round end makes the shoulders. The belly goes from just above the hips up to the middle of the spine, on the spine's bone, so the body bends at the waist the way the bot's spine does. Both are flattened to about seven tenths as deep as they're wide. The hips are a ball, a little wider than the belly, which fills the gap between the belly and the tops of the legs, and the neck goes up to the head, as thick as the elbow. With the hips on their own bone, the body moves in four pieces, so it bends at the waist and at the neck the way the bot does. The head is a ball eleven and a half centimetres above the head's joint, and a little forward, with a radius of seventeen and a half centimetres, so it's big, a little like a doll's. And the eyes are two small eggs at the front of the head, taller than they're wide, and nearly black. Then come the skins, six of them, from pale to dark, and five hair colours, black, brown, red, fair and grey. The first way to wear the hair is short, a ball over the top and the back of the head, a little bigger than the head, and flatter. Its middle is five centimetres higher than the head's, and a little behind it, so it covers the crown and leaves the face clear. Long hair has the short hair's ball too, and a tall ball down the back of the neck, twenty centimetres from its middle to its top, so the hair falls to the shoulders. Each way to wear the hair lists all of its parts, so the short hair's ball is written out again, which keeps each way whole on its own. A tail is short hair with a small ball tied at the back, high on the head, and a limb that hangs down from it, twenty centimetres, thinner at its end. It's on the head's bone like the rest of the hair, so it turns with the head, as if it were stiff. A real tail would swing, which would need a bone of its own. A beard is a flat ball along the jaw and the chin, below the head's middle and a little in front of it, so it shows under the eyes and round the chin. It comes with short hair, and then on its own, for a bald head with a beard. Last comes bald, a way to wear the hair with no parts, so there's no hair to draw. In content dot h, the content takes figures dot h, and gets the figures, first, as it's the first file we read. In content dot c c, read content reads figures dot text p b first, with parse figures, before the items, so each of the other files is read when the one before it is fine. The client hash goes over the figures too, as the hash says whether a world has the content that a client reads, and the clients draw the players from the figures.
15:38 In the content's tests, we take set, for the features the players get, and vector, for the unfolded parts, and the test that reads the game's content checks that there's a body in the figures file. Then a test that the content refuses figures that don't add up. An arm with its bone, its end and both radii is accepted, so when that arm without its second radius is refused, the radius is the reason. A part with no bone is refused, and so is one with no shape. A limb with one radius is refused too, with a message that names its bone, the left arm, and says what a limb needs. Each refusal is an expect equal on the error's text, so a refusal for another reason would fail the test, as its text would be another's. A ball with all three radii is accepted, and that ball with two of its three is refused. Each check parses a whole figures file, of a line or two of text. A way to wear the hair with a ball of all three radii is accepted, and with two of them it's refused, with a message that says it's the hair's. Some of the strings are split over two lines, which the compiler joins into one, as we saw in episode two. A new test parses figures with two skins, one shade of hair, and three ways to wear it, each with no parts. That's all the test needs, as the picking reads the lists, and not the parts. The ids start at one, as the world's do, so the loop goes from one to eight. Over the first eight players, each entry of both lists comes up, so the picks spread over them, which the test checks by collecting what each player gets. And each player has the one shade there is. Features of is asked about each of them in turn. The last check hands it an empty figures message, made right in the call, with nothing to pick from. A player then is white and bald, which is what features starts with. Then comes a test of unfold, which takes the body's repeated field, the way the characters are going to, and the text for it has two parts, a mirrored hand, four centimetres out on the left, and a head. It's parsed the way the game's file is, and the unfolded parts go in a vector. Unfolded, there are three, the left hand first, then the right hand, with its offset at minus four centimetres, and then the head, which isn't mirrored, as a single part.
17:51 Docker builds the content's tests at this step, sped up here, and runs them on their own, as they don't need the database or Redis. The three new tests pass, along with the content's other seven, one of which reads the game's own files.
On the bots' bones
18:06 On the client, figures dot h takes raylib, for its model, vector, and the parts' generated header. Dress puts the parts in place of a model's meshes. Each part is bound to its own bone alone, where that bone rests in the model's bind pose, which is the pose its mesh was made in, so the model's clips pose the parts the way they posed its mesh. Metre is a metre in the model's units, as our parts are in metres, and a model can be in any unit, and a part on a bone the model doesn't have is left out. Parts gives the parts as a mesh of their own, bound to the model's bones the way dress binds them, for something that goes over the model's own mesh, and doesn't take its place, such as the hair. In figures dot c c, we take raymath, raylib's maths, for the vectors, then from the standard library algorithm, array, c math and numbers, which has pi, string, utility and vector. Then abseil's str cat, and our triangles header, for the face normal it works out. Pi comes from numbers, as a float, for the turns round each limb. A limb has eight sides, and each of its ends is a cap of two rings of a ball, its pole and one fifty-five degrees round from it, so it's round, but low-poly, like our trees and our ground. Copy is like the one our triangles have, a template, so it copies a vector of any type into raylib's memory. Mem alloc and mem free are raylib's allocator, which its unload functions free with, so whatever a mesh holds has to come from there. It takes room for the values with mem alloc, as many as the vector has, fills it from the vector with the ranges algorithm, and returns the pointer. The builder collects triangles that share no corners, like the ground's, so each is lit flat, with each corner bound to one bone. Add takes a triangle's corners, and the shape's smooth normal at each, which says which side of it is outside. The face's normal points out of the side where the corners go anticlockwise, which is the side OpenGL draws, so when it points in, against the smooth normals, we swap two corners and turn it round. That way our shapes can hand corners over in either order. Each corner goes in with its position, the face's normal and the part's colour. Then come its bones, as raylib's skinning takes up to four bones for each corner, each with a weight, which blends how they move it. Ours moves with its first bone all the way, a weight of one, and none for the other three, so each part is rigid, like a jointed doll's. Upload copies the arrays into a mesh, its bones and weights too, and hands it to the GPU, where our skinned vertex shader poses it with the bones' matrices, as it did the bots' meshes. The vertex count is how many floats of positions there are, over three, and each three corners are a triangle, as none are shared. The builder keeps its arrays as vectors, the positions, normals and weights as floats, and the colours and bone indices as bytes, which is how raylib takes them. The arrays grow as the parts go in, and upload copies them once, at the end. The builder's five vectors are private, so nothing but add and upload touches them. Across gives two ways round a limb's axis, square to it and to each other, for the corners of its rings. The first goes across the figure, or to the front for a limb that goes across it, as the arms do in the T-pose, so a flat chest or foot is flat the right way. The hint is the front, for an axis that's mostly across, or the way across otherwise. Taking the hint's part along the axis away leaves a way square to it, and the cross product of the axis with that gives the second way. A ring is its middle, its radius, and how far its normals lean from the axis, from none at a pole to a quarter turn round the side. Add limb builds a stick from a to b, six rings from end to end, a cap at each end, and the sides between them. It takes a radius for each end, then its flatten, its colour and the bone its corners are bound to. Its axis goes from one end to the other, with the two ways across it. From a, the rings are the pole past it, a ring fifty-five degrees round from it, and the ring at a, then the ring at b, and the cap's two at b's end. Each ring's lean goes from none at the first pole, through a quarter turn at a and b, to a half turn at the last. A corner of a ring is a share of a full turn, two pi, so the eight go round evenly, half a side along, so that a flat side faces the front. The point is the ring's middle, plus its radius along the first way, and flatten times its radius along the second, which squashes the limb that way. Its normal leans out from the axis by the ring's lean, which is the smooth normal of a round cap. The lambda hands each corner back as a pair, its position and its normal, and it captures the axis, the ways across and the flatten by reference, as it reads them for each corner. Between each pair of rings, each side is a quad, with two corners on each ring, from the corner lambda, the last wrapping round to the first. A quad is two triangles, except at the poles, where a ring's corners all meet in one point, so each quad there is a single triangle, at the first pole and, in the next branch, at the last. Elsewhere, each quad's four corners go in as two triangles that share a diagonal, so a limb is eight sides of five bands. Add ball starts from an icosahedron, the twenty-sided die, which is the roundest of the regular solids, with twenty equal triangles. Its twelve corners come from the golden ratio. They sit on three golden rectangles at right angles to each other, each from minus one to one along one axis, and minus t to t along another. Each is pushed out to the ball's surface, with its twenty faces as three corners each. That's the icosahedron's usual list of faces, by the corners' places in the array. Place stretches a point on the unit ball out to the radii, around the middle. An egg's normal leans away from its long axis, so here the radii divide the point, where they multiply it in place. Add hands a triangle to the builder, placed and lit, in the ball's colour, on its bone. Each face is split in four, at the middles of its sides, pushed out to the ball: a triangle at each of its corners, and one in the middle, between the three new points. The middle of a side is the sum of its two corners, normalised, which puts it back on the unit ball. That's eighty faces, round enough to read as a ball and still faceted, and each goes in through add, four to a face. Bone named finds a bone in the model's skeleton by its name. Mixamo's rig puts a prefix, mixamorig and a colon, in front of each bone's name, which our content leaves out, so it makes the full name first. Then it goes through the skeleton's bones, which raylib keeps with their names, and compares each to the full name. Metres turns an offset in metres into the model's units. A metre comes in from the characters, in the model's units, so a part's sizes come out right on any model. Build makes the parts' triangles, each part into one builder, for one mesh. The bind pose is where each bone rests, in the model's own space, which is where its mesh was made, so our parts go there. For each part, we find its bone, the joint it starts at, which is its own bone's unless it names one, and the joint it ends at, and leave a part out when the model doesn't have its bones. Bone named gives minus one for a bone it can't find, so a part on, say, a tail is skipped without an error, and the figure still draws. Get color turns the hex colour into raylib's, and the part starts at its joint, moved by its offset, which metres turns into the model's units. A ball's middle is there, and its size is its three radii, in the model's units too. A limb whose end's bone was found ends at its other joint, moved by its offset, with a flatten of one when it doesn't have one, which is round. Add limb builds it, with each radius in the model's units. Build hands back the builder's upload, and it stays in the unnamed namespace, as nothing outside this file uses it. Dress builds the mesh first, then the model's own meshes are unloaded, and their array and the mesh materials freed. raylib allocated those arrays when it loaded the model, so mem free hands them back. The model gets one mesh, our parts', drawn with its first material. The parts' colours are their corners', so the material's colour is white, as raylib multiplies the two. And parts builds the mesh and hands it back, for whoever puts it over the model.
26:40 In characters dot h, the characters take the figures' generated header, so that a character can be made with the figures. Their constructor takes them, empty by default, so the monsters' characters stay as their models are. With the figures, each model is dressed in their body, in place of its mesh, and each player is drawn in their own skin and hair. Draw draws a model in the colour it's given, and a player's figure in their skin, with their hair. A garment is something over the figure, as a model that shares the character's bones. A character keeps whether it's dressed, and a garment for each way to wear the hair, by its place in the figures' list, but for bald, which has nothing to draw. And the characters keep a copy of the figures, for the players' skins and hair. In characters dot c c, we take the client's figures header, for dress and parts, and the content's, for unfold and features of. The constructor keeps a copy of the figures it's given. Each model is sized to its height by its mesh, as before, so a figure is as tall as the model was. Then, with a body, the model is dressed in its parts, unfolded, with a metre in the model's units as one over its scale. The clips, the hand's bone for the sword and the skills' clips go by the model's bones, which dressing leaves alone, so they're all found as before. Each way to wear the hair that has parts gets a garment, a copy of the model, with the hair's parts for its mesh. The copy shares the model's arrays, its bones, their matrices and its materials, so it's posed with the figure, and it costs one mesh. The garment goes into the character's map under the way's place in the list, and once each way has its garment, the character is dressed. The destructor unloads each hair's mesh, and frees its array, before the model goes. In draw, a lambda draws a model with its outline first, at the figure's place, its turn and its scale, in a colour. It captures what it needs by reference, the outline and the matrix that places it among them. A model that isn't dressed, which is a monster's, is drawn in the colour, as before. A player's figure is drawn in their skin, from what features of picks for their id, then their hair over it, in its colour, when they have any. Find gives the end of the map for a bald player, so they get no hair. In renderer dot c c, the players' characters, X Bot and Y Bot, get the content's figures. In the client's CMake lists, figures dot c c goes into the client's core, and figures test, which we're going to write once we've seen them, goes into the client's tests.
29:17 arthur is a person now, with a skin and hair, and no clothes yet, as an item's look has no parts so far. The figure walks with the bot's walk, as each part moves with its bone, and the sword is in the right hand, as the hand's bone is still there. Let's switch cel shading on, in the settings, and the figure gets its outline. Up close, the outline is broken, in dots of dark round the head and along the limbs. The outline pushes each corner out along its normal, and our corners have their faces' normals, so that each triangle is lit flat. So where two faces meet, their corners go out two ways, and the faces come apart.
29:59 Before we fix it, the figure's test, in figures test dot c c, pins down how dress binds the parts. We take memory, string and vector, abseil's status or for the window, our characters header for the characters' folder, the window, the content's figures header to parse the parts, and GoogleTest. A small bone named finds a bone by its full name, for what we check. The test needs the characters, which Mixamo's terms keep out of the repo, so without them it skips, as the characters' own do, and says where the readme is that explains how to get them. It opens a hidden window, sixty-four pixels square, as raylib needs a GPU context to upload a mesh, and loads X Bot from the characters' folder. An assert stops the test at a failure, as nothing after it would mean anything without the window. The figure comes in protobuf's text format, in a raw string, like the characters' tests, which keeps its quotes as they are. Each part is a body entry, with its shape, its bone and its sizes, in metres. It's an arm, from the shoulder to the elbow, a little thinner at the elbow, then a head, and a tail, which Mixamo's rig doesn't have, so the tail has no bone to go on. We dress the model in them, with a metre of one, as X Bot is in metres. After that, the model has a single mesh, the parts'. A limb is eight sides of six rings, which is sixty-four triangles, and a ball is the icosahedron's twenty faces split in four, eighty. The tail adds none, so a part on a bone that isn't there is left out. Then each corner of the arm's triangles is bound to the left arm's bone, and each of the head's to the head's, with a weight of one, and none for the next. And we unload the model, along with its pose and its bones' matrices, which raylib six's unload model leaves behind.
31:47 We build the client at this step, and run its tests on a virtual screen from x v f b run, as the container has none. The figure's test passes, along with the characters' four, one of which still finds both bots and their clips.
The outline
32:02 To fix it, the outline gets a mesh of its own, a hull. In figures dot h, hull gives the figure's parts again for the outline, with each corner's normal its shape's smooth one, so the corners where faces meet move out together, where with each face's own normal they'd come apart. In figures dot c c, the builder gets a second way to work, for the outline, where each corner has the shape's smooth normal in place of its face's. Its constructor takes whether to, and it's explicit, so a bool can't turn into a builder by accident. When two corners swap, their smooth normals swap with them now, and each corner goes in paired with its smooth normal, which it gets when the builder is smooth, normalised, or the flat one when it isn't. The builder keeps which it is in a member, which the constructor sets once. Build takes a bool, smooth, which says which normals its builder gives. For the hull, it leaves out a part that's set into a bigger one, which is a field we're about to give the parts. Dress builds the flat mesh, as before, and so does parts, and hull builds the smooth one, for the outline. In figures dot proto, a part can be flush, in a bigger part's surface, such as an eye in the head. Pushed out four centimetres, an eye's hull would be much wider than the eye, and would ring it with dark on the face, so the outline leaves it out. In figures dot text p b, the eyes are flush. They're the two flush parts so far, and the hair has none. A garment gets a hull too, in the header that keeps them, and so does each character, whose hull is what its outline draws: the model, or a figure's hull, on the model's bones. A character that isn't dressed keeps its model as its hull, so the code that draws the outline doesn't need to ask which it has. In characters dot c c, a character's hull is its model to start with, and the body is unfolded once now, for its mesh and its hull. Once a model is dressed, its hull is a copy of the model, with the hull for its mesh. The copy shares the model's bones and their matrices, which the clips set each frame, so it's posed with the figure. Each way to wear the hair has its parts unfolded once too, and its garment is two copies, with the hair's mesh and its hull. The hair's hull is built from its unfolded parts like its mesh, with hull in place of parts. The destructor unloads each hair's hull, and a dressed character's, before the model. The lambda takes a hull now, for the outline, the character's for the figure, and the hair's for the hair. So the hair gets an outline of its own, round the outside of the hair, where it isn't against the head. In renderer dot c c, the outline goes in to four centimetres from five, as a figure's arms are about twelve centimetres across, so it's a third of that.
34:47 With cel shading on again, the outline goes round each of arthur's parts in one piece, and round the hair, a little thinner than before, with the eyes left clear. And it follows the figure as it walks, as the hull is posed by the bones that pose the rest of it.
What players wear
35:02 Before clothes can show on anyone, each client has to know what each player wears. So far, the world's message has had each player's weapon, for the sword in their hand. So each player gets wearing too, a list of what else they wear, from the off hand to the feet, by the items' ids, so that everyone sees them in it. In the world's header, next to main hand of, worn of gives what else the player wears, in each slot from the off hand to the feet that has something in it. In world dot c c, it goes through the four slots in that order, and keeps the item in each that has one, with an if that declares the stack it checks, so the stack's name lives inside the if. The item's id goes on the list, so a player who wears nothing but the sword sends an empty list. In server dot c c, the player we send gets each of those, after their weapon. In the world's tests, a new test makes a world of the test gear, and two players join it, one wearing the cap, and one with it in their bag. The first wears the cap, and the second wears nothing, until they put it on. Then the second moves the cap from their bag, where it's the first stack, onto their head, with move item, the world's move, the one a client's drag asks for, so the test goes through what the game does. After the move, the second wears the cap as well. Join, too, is what the server calls when a client logs in. In the test gear, the cap is item four, worn on the head, and a helper of these tests makes a player who has one item, worn or in their bag. On the client, a figure gets what else a player wears, and the animator copies it from each player in the world's message, as it does the weapon.
36:43 The world's tests run without the database too, so we build the server's tests at this step and run those. The new test passes, with the rest of the world's.
Clothes as items
36:53 Items dot proto imports figures dot proto, as an item's look is going to say what it looks like on a player, and a look gets parts, which go over the figure's body, and move with its bones. In items dot c c, we take the figures header, and check each part of an item's look with check part, the check the figures file uses on its own. When a part is wrong, so is the item, and the message names the item's look, then what's wrong with the part. In the content's tests, in the test that refuses items that don't add up, a cap with a ball on the head is accepted first, so a look with parts parses. Then that cap with a ball of no size is refused, so the refusal is down to the size. Its message has the cap's name, then the ball's bone. Each is a whole items file in a string, with one item, so the check runs on the look alone. In the items file, the leather cap gets two parts. The cap is a ball over the top of the head and the hair, a little wider than both, and flatter. It's eight and a half centimetres above the head's middle, so that it covers the top, and the eyes stay clear. A flat ball at the front is its peak, above the eyes, in a darker brown. Both are on the head's bone, so the cap turns with the head. Neither part is flush, so with cel shading on, the outline goes round the cap and round its peak. The tunic covers the body, a little bigger than it, with the chest from the middle of the spine to ten centimetres under the neck, where the body's chest goes. The belly goes from just above the hips up to the middle of the spine, where the chest starts, a little narrower than it, and about as flat. A hem goes from the hips down, which widens out over the tops of the legs. The hem goes down twelve centimetres from the hips' joint, and is flatter from front to back, like the chest. The chest and the belly are on the bones that the body's chest and belly are on, so the tunic bends at the waist with the figure. The hem is on the bone of the hips, so it stays with the hips while the legs swing under it. A sleeve goes from the shoulder down the upper arm, short of the elbow, and mirrored, so there's one each side. It starts three centimetres in from the shoulder's joint, so it covers the round end of the arm. Nothing of the tunic is on the forearms' bones, so the forearms stay bare under its short sleeves. Last, a belt round the waist, a flat ball a little wider than the belly, in dark brown, with a small gold buckle at the front, which is flush with the belt, so the outline leaves it out.
39:23 On the client, what's worn is going to be drawn the way the hair is, on the figure's bones, and over it. In characters dot h, wear makes an item's parts, for each character to draw on the players who wear it. Draw now draws the figure with what it wears, too. Wear takes the item's id and its look, the two things the renderer has for each item, as hold does for a weapon. A garment is something over the figure, such as the hair, or an item that's worn. The hairs are kept by their place in the list, and what's worn by the item's id, as the rest of the game knows items by their ids. So each character keeps a garment for each item it can wear. In characters dot c c, the destructor unloads each garment's mesh and hull, along with their arrays. Wear is called once for each item, as the renderer is made, so the meshes are ready before anyone needs them. Wear leaves an item with no parts alone, such as a potion, and unfolds an item's parts. Then for each character, it works out a metre in the model's units, as the constructor does before it dresses a model. A garment is two copies of the model, like a way to wear the hair, with the item's parts for meshes, so they move with its bones. The copies share the model's arrays, its bones, its matrices and its materials, so a garment costs its two meshes and nothing more. The model gets the parts' flat mesh, and the hull the smooth one, for the outline. And the character keeps the garment under the item's id. Draw draws the figure and its hair, then what it wears, which has colours of its own, over the figure's. It finds the garment for each item, and skips an item the character has no garment for. Each goes through the draw lambda, its outline first, so the garment draws over its back faces, and then it's drawn at the figure's place, its turn and its scale, in white, so it keeps its own colours. In renderer dot c c, each item in the content is held and worn, so the characters make the parts of each that has them.
41:26 In the characters' tests, a test draws a player in what they wear. It needs the characters, so without them it skips, with a message that points to the readme on getting them. It opens a hidden window, sixty-four pixels square, which raylib needs before it can draw. It draws into an offscreen target of two hundred and fifty-six pixels square, as a pixel from the game's camera at sixty-four is about twenty centimetres, wider than a limb. An offscreen target is our render texture class, which the renderer's tests draw into. Its figure is just a body, thick enough to cover a few pixels from the game's camera, and the figures list one skin, green. Its tunic is item five, worn on the body, with a look of one part, a blue ball round the chest, on the bone of the upper spine. The content is made in the test, with the figures and the items parsed from text, so the test doesn't depend on what the game's files say. The renderer is made with the test's content, so it dresses the bots in the test's figure. It clears to black, so nothing behind the figure can pass for its colours. We find the chest on the screen, from the game's camera, and draw player one, who's green, in their skin, so the chest is more green than blue, when we read its pixel back from the target. Then player one in the tunic, and the chest is more blue than green, so the tunic is drawn over the figure, in its own colour.
42:46 arthur has the leather cap and the tunic in the bag. The cap goes onto the head, and it's on the figure in the character window, then the tunic onto the body. With the windows closed, arthur is in both, in their own colours, over the skin. The server says what each player wears, so the other players see arthur in them too.
43:05 We build the client at this step, and run the characters' tests on a virtual screen, as the new one draws a figure, and drawing needs a window. The new test passes, along with the characters' other four.
Clothes to start in
43:17 Now for the clothes a new player starts in. A player is going to wear trousers, which go on the legs, between the body and the feet, so items dot proto gets a slot for them. It goes at the end of the enum, as a slot's number is what goes over the wire, so the ones we have keep theirs. And a look can be dyed: worn, it's drawn in the colour of the player who wears it, which its white parts take on. That's how a shirt is going to come in each player's colour, as one item. In items dot c c, the legs' slot is called legs, which is how the bag window names it. It's also how a player's row in the database says where an item is, so the trousers are saved with no migration, as the rest are. In the items file, the kit, which a new player is given once, gets the clothes, the linen shirt, the trousers and the shoes, which the world puts on them, as each has a slot that's free. The sword comes first, so it goes in the hand, and the potions last, which go in the bag. The linen shirt is item eight, worn on the body, and it adds to no stat, and it's dyed, with Font Awesome's shirt for its icon in the bag, in the colour of linen. A dyed item's tint is its icon's, as the bag doesn't know whose colour the shirt is going to take. It covers the chest, from the middle of the spine to ten centimetres under the neck, a little bigger than the body's, and the belly, from just above the hips. A sleeve goes from the shoulder to the elbow, and on to the wrist, on both sides, so the shirt has long sleeves. Each part is white, so it takes on the colour of whoever wears it. The sleeves are a little thicker than the arms under them, so no skin shows through. The cloth trousers are item nine, worn on the legs, with a person walking for an icon, as Font Awesome has no trousers. They go over the hips, a ball a little bigger than the body's, and down each leg, over the thigh, and the shin, to the ankle, a little bigger than the legs, in brown. A belt goes round the waist, in a darker brown, over the shirt, so the shirt looks tucked in. The belt is a little wider than the shirt's belly at the waist, so it shows all the way round. And the plain shoes are item ten, worn on the feet, with shoe prints for an icon. Their icon's tint is a light grey, so it shows against the bag's blue, where the shoes' own colour wouldn't. A shoe goes round the ankle, and moves with the foot. It's a short one, from eight centimetres above the ankle's joint down to the joint. Both of the shoe's parts are on the foot's bone, so the shoe turns with the foot as it steps. The shoe goes round the foot too, from the heel to the end of the toes, flat underneath, in a dark grey that's nearly black. The trousers end inside the shoe's top, so no ankle shows between them.
46:11 On the server, worn of gets one more slot to go through, so its list is split over three lines now. The new one is the legs, between the body and the feet, so the trousers go to each client with the rest. In the character window, the legs get a slot in the row of what's worn, between the body and the feet. In bag window dot c c, which draws the item slots for the character window too, an empty legs slot shows the person walking, greyed out, the way each empty slot shows what goes in it. In characters dot h, a garment gets dyed, for drawing it in the player's colour, not its own, and draw draws what's dyed in the colour it's given. In characters dot c c, wear keeps whether the item's look is dyed with its garment, and draw draws a dyed garment in the colour it's given, and the rest in white, which keeps their own colours. The renderer gives a player's figure their colour, as it has all along, which went unused when the figures took their skin, so now a dyed shirt comes out in it.
47:10 In the characters' test of what a player wears, the test's items get a shirt, item eight, worn on the body and dyed, with a white ball round the chest. Its ball is the tunic's size, on the bone of the upper spine, so the pixel we read is on it. Then come the first two players, one at a time, in the shirt. These checks come at the end of the test, after the tunic's, and use its camera. Player one goes first, and once they're drawn, we read the chest's pixel back. It's orange, more red than green, and more green than blue, as player one's colour is orange. That's the colour the renderer gives each player, which the shirt takes. Player two in it is blue, more blue than green, and more green than red. The figures' one skin is green, so neither colour can be the skin's, and the shirt takes on each player's own.
47:59 arthur is a new player here, so on joining, arthur gets the kit, the sword and three potions, and the clothes, worn from the start. The shirt is in the player's colour, over brown trousers and dark shoes. The character window has six slots of what's worn now, with the trousers on the legs, between the shirt and the shoes. Each new player gets a shirt in their own colour, so from far away, we can still tell them apart.
48:26 We build the client at this step, and run the characters' tests on a virtual screen again, as the test of what a player wears draws its figures, and drawing needs a window. They pass, with the shirt's checks in the test of what a player wears.
Boots and a buckler
48:41 Boots and a buckler are content too, so they need no code. The ash buckler, which the Ash and Crypt skeletons drop, gets a look. It's a round shield on the back of the left forearm, a flat ball of dark iron, fourteen centimetres along the arm from the elbow. It's above the arm in the T-pose, where the back of the forearm faces up, clear of the sleeve. Then a smaller ball at its middle, in the ember orange of its icon. Both are on the forearm's bone, so the shield turns with the arm as it swings. The leather boots are a new item, number eleven, worn on the feet in place of the shoes, with one armour, like the cap. Their icon is the shoes' shoe prints, in the leather's tint. That's the cap's and the tunic's tint, so the three icons match in the bag. Each boot goes up the shin from the ankle, seventeen centimetres, over the trousers, and moves with the shin's bone, though it starts at the foot's joint. The content allows that, as a part can start at any joint, and move with another bone. A cuff goes round its top, in a lighter brown. The foot of the boot goes round the figure's foot, from the heel to the end of the toes, a little bigger than a shoe, and moves with the foot's bone. Each part is mirrored, for the right boot. The boots go in the feet slot, like the shoes, so putting the boots on takes the shoes off, and the shoes go back to the bag. And in the monsters file, the plain skeleton, which drops the cap and the tunic, drops a pair of boots one time in ten too.
50:12 arthur has the boots and the buckler in the bag, and the shoes on. The boots go on the feet, and the shoes to the bag in their place, and the buckler goes in the off hand. So now arthur has all of it on, and the boots and the buckler move with the shins, the feet and the forearm as arthur walks.
Shadows
50:30 Nothing in our world casts a shadow, so a figure looks as if it stands a little above the ground, which the lab's shadows fixed. In renderer dot h, the renderer takes optional, and keeps the land, for the shadows to lie on, as an optional, as it has no land until it's given one. In renderer dot c c, we take c math, for the circle. A shadow reaches forty-two centimetres out from a figure's middle, about as far as its shoulders are wide, and its middle is black at about a third of the way to opaque, so the ground shows through it. Draw shadow draws a soft dark patch under a figure, dark in the middle and fading out at its edge, in sixteen slices round. A corner goes a distance out from the figure, at a number of sixteenths of the way round. The cos and the sin of the turn give the corner's way round, times the distance, from the figure's place. It's black, with the alpha it's given, and on the ground's height there, so a shadow lies along a slope. It's three centimetres above the ground, so that the ground's own triangles don't cover it. Then we hand the triangles to rlgl, which we've used before, as there are few, and they move with each figure. Between its begin and its end, it takes corners one at a time, each with the colour set before it, and draws them with raylib's default shader. For each of the sixteen slices, the middle is a triangle from the figure's place out to two corners sixty percent of the way, all three as dark, so it's as dark all over. Then a ring goes out to the edge, two triangles, which fades from dark to clear, as the GPU blends the alpha across each triangle. Set terrain keeps a copy of the land's terrain, its grid of heights, for the shadows to read. In draw, the shadows wait until the renderer has a land. They go after the figures, with the depth mask off, so they don't hide what's drawn after them, and blended by their alpha. An NPC's ball gets a shadow a little wider than it, and players and monsters get the forty-two centimetres.
52:26 arthur has a soft shadow underneath now, as dark as a third of the way to black. It moves with arthur, with each corner at the ground's height under it, so arthur stands on the grass, where before the figure looked as if it floated a little above it.
Wrap-up
52:41 So, today we looked at what the lab drew, people, sticks, a skeleton and a spider, and kept the people. A person is parts in the content now, limbs with round ends and low-poly balls, each on a bone of Mixamo's rig, with a skin and hair that each player's id picks. The client builds them where the bots' bones rest, and binds each corner to its part's bone, so the bots' clips pose them. The outline has a hull with smooth normals, so it stays in one piece. The server sends what each player wears, and an item's look has parts over the figure. So the clothes show on whoever wears them, from a new player's shirt in their colour to the boots and the buckler. And each figure has a shadow under it, on the ground.
Playing it
54:06 Let me know in the comments what you'd like to see next. And if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.