 Welcome to catch from scratch episode 8 This video won't be nearly as long as the previous one that one was really bad So today we're gonna talk about updating the draw function to handle our new data structures So that should be pretty pretty simple so We really have to do primarily Let me show what we're trying to do here in our Earl draw .c file we had this draw routine. I want to copy this and Make it not have to have as inputs num nodes and triangles and then arrays for numbers and number of nodes and triangles So the plan first would be to define some new functions in gum that see that will be useful for other things in this in this software, so a few things we have to do I want to have a a count nodes function and also a count basis function and this will hopefully be enough to give us our num nodes and num triangles for The the new draw routine. So we'll pass in as inputs just the body We're just gonna pass in as a as a body to count the nodes So what we do is I struck node We'll iterate through the the nodes Set a counter We'll make a simple loop This will just say what while the the node is not in all Includes our counter and we'll say it or equals it or Next and then we will return the num That should be pretty much it for this this function here. I want to copy this though and also have Count faces should be a similar function. Just instead of having a node. It will be a face Everything else, I think you keep the same we get a pretty good job making this modular And then the last thing I want to do is I want to create a function just for our test case I want to be able to make a body from a face and pass in a face and The reason for that is because if you recall yesterday in the previous video We had this make polygon face function that we use as a test case for our triangulation It returns a face I want to be able to return an actual body from a given face. So just to you know make that simpler I'll make a function for that. It's not gonna be very challenging. We're just going to Define body and we're gonna pass in faces and nodes from that from that input parameter face We'll say How do you do this? We do struct body Then we should say body face. So the first face is gonna point to the input face Then the first node is gonna point to the face node array zero So super super simple Basically, we're just going to you know Take the face and the node array from the face and put that into a body and return it So that's honestly, that's it. I'm gonna put this these declarations in the header file I'll I'll be right back Okay, I put in these declarations and now we're done with all the geometry type stuff. I imagine So we'll close out of those now I want to In the draw.h. I want to make a new function. I Want to keep the old one just for nostalgia's sake, but then I want to make a draw body Function with the same parameter just step for the last four of these instead of having all those weird things I just want to pass in a struct body Pointer that's the plan. So I'm done with this one Now in our draw.c. I want to copy and just you know replicate this entire draw routine Again and call it draw body Just a couple small changes here. Nothing too massive So what do we have to do? Well, if you recall The biggest thing was you know, since we're passing in like a set of you know nodes and triangles We had to convert that into like a one-dimensional array for our open GL calls right here And that was done, you know pretty Peculiarly, it's not exactly a you know, very elegant. So I want to make this so we can simply pass in a Body structure and take that Sorry, my cat's having a seizure. So we're just gonna ignore that. Anyway, I want to replace these lines here with something new and improved So what's the plan? Well, we can use our functions We just made to identify the number of nodes and the number of faces. So it would just count nodes body number of faces Was count faces body Then we will have to define these these things here again, but the vertices array will be Size three dumb nodes and then the emissives will be the obviously num faces We're gonna assume that everything passed in here is going to be a triangle. If not, we'll pass some some some error handling back But right now We'll have to define a couple variables So the algorithm I was I was thinking to implementing Well, first we're gonna need some some some iterators here, so You need a face iterator Just find that right now as body face Iterator to go through Leave that as null for the time being and Also, we're gonna loop through we're gonna actually go through and increment the index for these two arrays manually As we go, so I'm just going to define them up front K and L. So what's the plan here? The plan will be to Let's just say to go there is a loop over the faces in the body To populate the index array or indices array I should call it indices vector to be honest then in that loop If the face is a triangle We we go on if not we'll pass some error. I hate error handling, but we're just gonna print something out print error then we'll loop over all the points in the face and then We'll loop through the nodes in the body and until we have the ID of that given node basically we're just trying to figure out which IDs In our in our data structure for the the body for each node Correspond with the nodes on the face and write that in the vector. So nothing too complicated I'm going to write up this off off recording and get back to you in a minute or two Okay, so here's that function basically has said we will loop over all the faces in the body Then if the face is a triangle We will you know if has three nodes in the on the face will loop over all the points. So zero one and two Then we're gonna loop through the nodes in the body Until the node in the body that we're iterating through matches the node in the array and when the two match we know that the triangle is including the correct node index and we'll just store that in the indices vector and then break out of the loop and complete the You know like the loop here with these two iterators and then in case we don't have to have three nodes Like we have four nodes or five or six Then we'll print this error oops non-triangular face in draw body I'm not gonna you know, you know return some kind of weird error and you know in the program I'm sure it won't actually break anything. I just it's gonna give you bogus drawing So there's no reason to you know Break everything for no reason at this point. We've got the indices Vector completed we still have to get this vertices Vector the actual coordinates of each of the nodes. So to do that It's very simple we're just gonna take those values straight from the The node list for the body. So we already have that sort of existing in our structure We need to just convert it to a single one-dimensional float vector Let's do that. We'll use our iterator node thing We will loop through so we'll say while iter node does not equal null We'll just constantly index this vertex Vector at a different location with this l plus plus Because l starts at zero, but we're gonna, you know increase that every time we go through And we'll set that to iter node node x And then the next two are going to be y and z And then lastly very simple. We're just going to iterate our Variable here Oops, this is bad next That should be that We can I think kill all our old Stuff for that I'm not sure if we have to go back and And Change any of these like values like I'm not sure we reference any of those those variables anywhere else in the program I'll take a look and and I'll see there has to be something somewhere like numb trunks or something Let me just see numb Triangles yeah right here. So this should be numb faces Is this the right function? Yeah, that's the right function. I think let's say numb faces I think we're pretty much done with If assuming this works, it may not even work if it works. We're done with this File the last file here is the main file So in this case, I I want to stop printing out this all this garbage Just for the sample that we're making today We will get rid of that. What else will we do? Face make polygon So we'll say come on struct body Hexagon equals and we'll use our make body from face function That we just created And we'll get rid of this thing and this thing We'll change this for triangulate body because we made it a body. Oops And now we can call our our draw routine now the draw routine requires some stuff It requires a look at Size three and a look from Um Size three Uh, I'm just gonna put some random numbers here. I don't really know what's going on. We're gonna do we'll look at 1 1 0 that seems to be pretty much in the center and uh We'll look from far away I don't know 0 0 10 or 5. What do you think? And then we'll just something call our draw body function draw body Um, I think we had 800 800 as our previous Window size And I'm pretty sure it's look at then look from And then our actual body. So we'll try the prism first And then We'll do the same thing I'll comment it out for now, but we'll try it on the hexagon as well See this compiles. It probably won't what did I do wrong this time? Num faces All right, I'll go back and check where this is. This is in our draw. See I'm gonna type it over there I'll be right back Okay, I I fixed those problems just a couple typos and I had my includes in the wrong order. So No worries everything is fixed Yeah, so I run the function I run the code I should say here is our prism. So you can see it It works. We have all our triangles on the screen. Everything is correct As you would expect Now, let's see if our Our other test case works. This should give us that weird looking Hexagon thing from yesterday in the previous video Yeah, you can see our weird looking thing. It was like this if you recall So, yeah, it's pretty much spot on what I would expect So, yeah, that's it for this video. Next video we will I'm not sure. I guess I'll figure it out. Probably we're gonna update our Our export functions to handle our new data structures Anyway, thanks for watching