Code Generated Cylinder


Today I got the "mountain" mesh added. The reason for why I wanted this mesh was simply that the grid alone looked quite odd / not so professional I guess. I will say that with just the new mountain mesh added, I personally enjoy the look of the game more than I did before so that's an improvement :)

Anyways, I am here for a tech discussion where I am now going to describe how this mesh is code generated.

Just kidding (for one paragraph).
So usually you have yourself or some person make a model in some software that you then need to choose a file format to then either write an importer, or actually use some library (but god forbid you even touch the FBX sdk because I did and OMG AUTODESK WHY DID YOU MAKE THAT HELL TO USE?!?!?). The other option I think most people do is just write a script to output the software's data to your own custom loadable format which sounds quite nice (but since I don't have Autodesk Maya, I'd imagine that costs money or it atleast costs you the cost of that software so yea). Basically, I have written file loaders for collada, fbx, objs and am now quite eh on the whole thing unless  model editing software is required.

Soooooooooooo because i'm this way, it gives me a perfect excuse to just write code that fully generates the mesh I want! And so I did (again). The code can be simply thought of like this. You first generate a cylinder mesh and then add cap(s) to it.
So in my code, this looks like a simple "what's the radius and height" question. With just those two things alone, you can represent a cylinder.
But, since this cylinder is going to be rendered, I needed to not only know those two values, but actually generate vertex and index data to render it (index obvs optional but you do save on atleast memory long term as you're not having to store duplicates of vertex data and can just use N bits 3 times for 1 triangle). My method was to just set Z to radius and just rotate it over N iterations where N is the subdivision amount of the cylinder >= 3 (2 would just make a quad with extra lines). On each iteration of N, you not only generate just the simple x = 0, y = 0, Z = radius but rotated vector, but you then drop in the Y value your height to make a second vertex alongside the first. So on the next iteration (ie iteration starts at 0 so iteration 1), you start filling the index buffer as you now should have 4 vertexes in your vertex buffer. So you just wind your triangles how you want so something like
A---C
|        |
B---D

Indexbuffer = A, B, C | B, D, C assuming counter clockwise winding. You now have 4 vertexes and 6 indicies.
So literally just write this in a for loop where the index buffer starts filling only after the 0th iteration and you're good to go! Or I mean, for the cylinder part of the cylinder.

For the caps, you can simply just put a single vertex in teh center and have it connect to all edge points across the cylinder.

The top would be the newly added top center vertex and you just run a for loop that makes triangles where teh vertexes referenced by the index buffer will be all even numbers in the scheme that I just wrote up. So something like

New Center Vertex, 0, 2 | New Center Vertex,  4, 6 | ....

The bottom (just -height on your up axis) will be odd numbers so

Bottom New Center Vertex, 1, 3 | Bottom New Center Vertex, 5, 7 | ....

And that's it! Now in my case, I wanted to have the grid be in the center of the cylinder. I did originally just make the simple cylinder described here, but since the grid moves up and down, you could see that it would clip with the cylinder's top. So I had to make it so the top of the cylinder had a "cutout" for the grid. This turned out to be a much harder problem than I originally though as I tend to forget how to intersect a line with a line to find where the two lines intersected (so where your new vertex would be). Once you learn how to write line intersection code, this isn't too hard as you just test to see what edge is closest to your current outside vertex and line intersect against that. Do note that this procedure actually kinda mimics the first in that you don't start the index buffer writing until after the 0th iteration so you can get a quad and create 2 triangles in it.
New Point Intersected By Edge A -- N_P_I_B_E_B
|                                               |
0--------------------------------------------2
So the index looks something like
0, New Point Intersected By Edge B, New Point Intersected By Edge A | 0, 2, New Point Intersected By Edge B

Sorry for how scattery this post is as I just wrote it. If this picks up interest, i'm more than happy to clarify / edit it to be a little more "open ended" in the sense of me not just using 0 and 2 for the indicies in the example buffer above, but instead using some variable name for ease of understanding (if ya'll think that is easier to understand).

And that's it! It's quite simple when you sit down to actually code it. To be honest, I would just reccomend that you start by thinking about the problem from the simple "I need a radius and a height value" and that's it as you'll hopefully converge on some better solution that I haven't ever though of to make this kind of code better. 

I'm going to go eat now, but please let me know your thoughts on the game! Thank you for reading and have an awesome day :)

Files

Funmi_Zip.7z 97 kB
Oct 16, 2021
Funmi_Windows.exe 234 kB
Oct 16, 2021
ChallengeList.txt 3.1 kB
Oct 03, 2021
English_Controls.txt 2.7 kB
Oct 03, 2021
日本語_Controls.txt 2 kB
Oct 03, 2021
日本語_README.txt 935 bytes
Sep 16, 2021
English_README.txt 1.2 kB
Sep 16, 2021

Get Funmi

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.