Engineering Two Assignment 7

Assignment 7 Download!

So this week, we moved our meshes over to use LUA to make it easier to create human-readable files, for debugging and various other things. So…lucky for me, I already had my mesh moved over to LUA from a previous assignment. Whoo! Saved myself a lot of time this week trying to figure out reading in LUA. Then we created a system to change the file from LUA to binary. Here’s the before:

return
{
vertexBuffer =
{
{
vertex = { -0.5, -0.5, -0.5 },
color = { 255, 0, 0 },
},

–This goes on for each vertex

{
vertex = { -0.5, 0.5, -0.5 },
color = { 0, 255, 0 }
},
},

indexBuffer =
{
–Front
0, 1, 2,
1, 3, 2,

–Back
7, 6, 5,
5, 4, 7,

–Left
4, 5, 0,
1, 0, 5,

–Right
2, 3, 6,
6, 7, 2,

–Top
1, 5, 6,
6, 3, 1,

–Bottom
2, 7, 0,
0, 7, 4
}
}

Here’s afterwards:
BinaryJunk
I had to take a screenshot, or else it looks like this:
$¿¿¿ÿÿ¿?¿ÿÿ?¿¿ÿÿ??¿¯ÿÿ¿¿?¯ÿÿ¿??ÿ¯ÿ???¯ÿÿ?¿?ÿ¯ÿ
Fancy! So how does it work?

When reading in from the initial LUA file, it worked the same way as it did in the original read in. I just needed to copy it over to a builder. After reading it in, the builder determines how many vertices and indices are in the file. So when writing it to the binary file, I did

NumberOfVertices NumberOfIndices BIGPACKOFDATAFORVERTICES BIGPACKOFINDICES

It’s pseudocode, however, thinking back, I might change my variable names to the above to make it sound more like He-Man coded it.
On the game side of things, you read in the binary file. There are a couple of ways you can do this. The easiest way, which I chose to do, was to do four reads. One for vertex count, one for index count, one for the big chunk of vertices, and another for the big chunk of indices. Granted, it does feel super dirty. The other option involves doing one big read and then just putting pointers in the appropriate places. For example, you know the vertex count is going to be first and you know the index count is immediately after it so you can just shift the pointer over. Then, since you have the count of the vertices, you can just move the pointer down again equal to the number of vertices times the size of your structure for the vertex.

So I ran in to a couple problems. First was that I just threw the color in as a float immediately after the point. So what’s the problem with that? Well, on the game side of things, it’s only expecting to use 4 bytes for color – represented as an integer. So…not three floats. Instead of just throwing a big list of floats into the binary, I had to create the struct for the vertex on the builder side of things so it knew how to throw it together. So that was problem one. After that, reading it in was fantastic.

The next problem was on the game side of things reading in the binary file. Oh, reading in was a piece of cake. It was basically the opposite of the writing process. And so two things happened on this side of things. Firstly, I didn’t realize that reading in to allocated memory wasn’t a copy: I assumed it was. But I remember talking about it in class, so it didn’t take too long to figure out to create a temporary variable for reading in the values and then copying them over. And then I tried to just set the buffer equal to the location of the copied values. So that didn’t work either, since DirectX gives you a memory location, and you need to copy your junk in to that, so it was a bit of a messy process copying everything and then reiterating through the values. I tried to take out the temporary variable, because that seemed like it was sound reasoning, but it stopped rendering. So that’s that.

Time spent:
Moving Read Over (also setting up debugging for the builder): 30 Minutes
First Pass on Writing: 1 Hour
First Pass on Read: 1 Hour
Debugging and Fixing Both Sides of Read/Write: 1 Hour
Writeup: 30 Minutes

Total: 4 Hours

Leave a Reply