
I started work on a small project to help me learn to program 3D using OpenGL. I am making a 3D map with a city style landscape, office buildings, roads and the like. I'm not sure if I want you driving around in a car for some reason or a tank. For now I have the movement set up so you move like you would in a tank. It's just easier this way and allows a little more freedom in movement. You can also fly up into the air to look down over the area but in the finished program there wouldn't be any flying as this is meant to be more of a land vehicle style game.
So far it is running rather well, but there was an unforeseen problem. I done a simple in game grid map with square spots like a 2D game, each being grass, road, building or whatever in a square grid. Then, the game creates a cube style building, textured etc, square flat piece for grass etc. This is temporary as I plan to load in 3D Models later on. I actually have a seperate program I worked on to learn to load in 3D Studio Max .ASC (text) models, and I have it working. Now my problem I have is when i started researching collision detection in 3D games. There's a lot more to it than one might think. In a 2D game, it is fairly straight forward, you check your position on the map and compare it to the location of buildings or other in game objects. In 3D it requires more preparation ahead of time. You want the program to test your position against ALL the polygons in the game... this would be time consuming, but... even in this simple case of checking all polygons, you need to have all polygons that will be used in the game in a list of some sort so you can check them. Creating on the fly objects in the game simply won't due. This means I have to design some sort of level editor and then a method to add polygons from any imported objects to the list as well... or just have the level editor import objects then merge them with the level I s'pose. So, I have to redesign the game, almost from scratch. Learning is a pain at times.
Once you get a level editor made and a level loaded into a game, you can then work on optimizing things for speed. You don't want to have to check every polgon in the game everytime you move after all! Well, with a list of polygons you can then use something called a Quad Tree... which basically sorts your polygon list into quadrants so you narrow down only a small number of polygons that are in the same quadrant as you are, then you search them for collision = faster. This also means you can speed things up when drawing your scene by only drawing polygons that fall within your field of view... also called a "frustum", which is just that area you see in a 3D game and excludes all polygons that fall outside that. This speeds things up a lot... you use your quad tree to discover which zones fall within your field of view then you only render the polygons in those quads. You can then take this a step further and only draw the polygons that actually face the viewer, no sense drawing polygons that aren't facing you after all right? This is called occlusion culling.
By now you might be thinking, "why should I go through all this trouble to speed things up, what's a couple extra polygons? My Video card is a super duper extra fast polgon rendering machine!", you've got some nerve asking such a question! :) You would be right though, IF all those polygons could be sent directly to the card without any problems I would agree, although there are still good reasons to speed things up. The faster it runs, the more detail you can add and the better looking your scene will be. There's another good reason. All those polygons need to be sent to the 3D Card, the slowdown comes when the data is actually sent to the card. This is a slow process, and the less data you send to the card, the faster it will all be rendered. You would be surprised at how much this improves things. I once created a 3D program that drew a simple landscape with trees, each tree consists of a simple 6 sided trunk with two polygons making up the branches, FAR LESS detailed than anything from modern games... when I ran the program with a higher detailed landscape and lots of trees I got 1 FPS!!! :) No optimizations.
Another optimization I want to do is called Level of Detail (LoD), this is where the farther an object is away from the viewer, the less detail (less polygons) are rendered. Some objects require a lower resolution version. Trees can be rendered as a single polgyon image at a far distance that always faces the viewer and is switched out for a higher and higher detailed model as it gets closer. This is done in most games that have lots of trees in them. You don't notice the difference, not at large distances, but you sure notice the speed.
Anyhow, this is a langthy description of what I wish to do. It's a long road but I really hope to get most of this done and have a good understanding of how to program a decent 3D game. I'll try to post my progress here, it may help someone else and will help keep me focused.

No comments:
Post a Comment