So I can rotate objects around their local coordinates and can properly move models around and have independent rotations. There is one small problem with it right now. Well, two problems really. When I first made my cube model (vertex by vertex painstakingly and foolishly hard coded) I made some temporary x,y,z values to help me with creating the vertices. So something like “x=32″ and then I also had a variable called size, so my first “face” or vertices was a vector of Vector(x,y,z), Vector(x+size, y, z), Vector( x+size, y+size, z) etc. When I created my secondary level, I added size to the z value. So the model basically had an offset. I somehow got it in my head the OpenGL would ignore the offset and draw the object at the origin anyways, as in, drawing the origin of the model at the origin. This created the whole rotation problem issue – which led to great frustration. So models need to placed around origin of Vector(0,0,0) when they are created. Which leads to the next problem.
Complex models might be a bad idea. And by complex models I mean anything that isn’t a Sphere or Cube. Last time I checked (although that was before I fixed it all completely), Rectanges didn’t even work properly with regards to rotation. So, I want the option to create an origin for my models so if a model has an origin at their head, I want the option of changing that origin to their feet or center of mass/volume. I saw a couple of posts about this – it involves taking the average of the vertices or something – at least for the “true” center. I’ve tried the “true” center and it didn’t seem to work… whats more, I’m not really sure how to pull what I want off. To set the origin, I have to translate to the origin minus the average for all vertices and then I would have to do my rotation and then translate to the actual location of the model. The whole model origin override will be a per-class thing so the actual model won’t be touched.
There is another huge problem though. I can color my cubes just fine but texturing them has some issues. I noticed these issues awhile ago and I didn’t really want to investigate, but I had a good idea of what my problem was. I assumed Texture Coordinates were per-vertex, that is, because my triangles all share from the same pool of vertices, I figured it wouldn’t be a bad idea to save even more space by just storing the texture coordinates for a vertex with the vertex. Turns out, I was pretty wrong about the whole thing and now I’m paying dearly for it. Because I stored my texture coordinates with my vertices, it was merely 8 extra lines for me to add. I actually have two cubes right now, which are exactly the same in every way but rotation and location in the game world, but I had plans for the second cube, maybe to mess around with it and turn it into a rectangle to test stuff and what not.
My first cube was put into a class called TestModel where it had a unique variable called pModel. I inherrited from my BaseObject, but at the time I didn’t have Models implemented in there yet (now it is.) Because I wanted to later change my vertices, I just created another class called TestModelB and basically did a copy and paste. So back to the texture coordinates, because I have 2 cubes to fix (the second cube isn’t quite the same as the first anymore so I can’t just do a copy and paste again) I need to add a grand total of 72 lines of tedious code. So, here comes my next goal:
I need to get a Model Importer up and running. I don’t care what format, hand-crafting a model is becoming WAY too tedious for my liking. Its in my belief that burning through long and plentiful lines of code, rather than copying and pasting, builds character, but this is… SUICIDE. After I get the texture coordinates all fixed up (And I’ll be discarding the second cube me thinks… I think one triangle is broken anyways) I’ll look into getting multiple mesh support up and then from there, looking at uber easy model format. I’ll be making my own model format I think, one that will be optimized for the engine, but that’ll come after I start working on the tools, ie. Level Editor, Modeller/UV Mapper, and whatever else I figure I’ll need.
EDIT( 4 hours later ):
So I gave up on the whole texture coordinates thing. I could only get 2-3 faces to be texture correctly and that was after I tried to calculate S+T myself and after I let OpenGL itself have a swing at it via some function that didn’t work. Anyways, this is a good thing really. I got so fed up that I started working on an importer for models. Currently I’ve just exported a Cube… yes just a cube, into the “simple model format” (Text option from the export menu). It had some crap in it I could have done without, so I trimmed it down to just vertices, triangles and then I added some color information. So here are the results.
*face palm*
Need I say more? Something is definitely up.
EDIT #2: Yay! Don’t you love when things just work?
Because I checked over my code again and again and found nothing wrong, I assumed it was something wrong with MilkShakes exporter. I tend to do that a lot. “It can’t POSSIBLY be MY code! It looks like it is working in THEORY.” Oh… if I had a quarter for every time I thought that. So I typed out my known-to-be-working cube model that is hard coded into the new format to see if maybe it really was just the exported file or perhaps my understanding of the extremely simple (hence why its called “Simple Model Format” *face palm*). Unfortunately, it was neither, and my cube didn’t work any better. In fact, it worked less so (which is kind of funny, no?) After some tinkering, I found out the problem was actually with the way I was grabbing data from the file stream. After working with C/C++ for this short amount of time, I have fallen in love with it. Its become to me like Lua or Java, where pretty much anything is possible. Unfortunately, it is extremely lacking. Because C/C++ is so old, I don’t think anyone is really updating the standard anymore.
With things like Lua and Java, they’re constantly being updated, new features added, bugs fixed, etc. One “feature” that I feel should be built-in (and by built-in, I mean not having to go to lousy extended libraries) and not require one to do messy, ugly work arounds (like for example, when you are required to use a const char * when you only have a const *, at which time you can either pack up the bags or do an incredibly ugly maneuver which involves creating a stream and “streaming” the char* over to a const char*, rather than typecasting it.) And thats where I’m going with this really. I’ve never really understand the need to have const char* and char*. Why not have just one, or provide built-in, beautiful facilities to turn one into the other. Shouldn’t it be as simple as taking data at the end of one pointer, converting it, then placing it at the end of another pointer?
Anyways, so was using getline(…) to grab data from a fstream. Eventually I got to the point where I got a char* and I had to use the ATOI method to turn that char into an integer as well as ATOF for the vertices. A given line is like the following: “-10 10 -10″. Me in my infinite wisdom though the c_str() method of a string returned every character in a char*. So when i used ATOI, I skipped over the whitespace index. Long story short, everything was wrong about what I was doing. It all appeared fine until it decided it was going to pull values from elsewhere in the char*. So, after looking at some code, I moved over to fopen and fscanf which is the best fracking thing ever. It is of course, deprecated, but there is the alternative of fopen_s which returns an error status rather than the file handle, so its more secure. I’ll look into that later though, I’m happy with the progress I’ve made so far. After maybe a couple of modeling tests, I may start work on a smalllll level designer.