Hi fellow devs,
Years ago, around the time of DirectX7, back when DirectDraw was separate to Direct3D, I made an RPG game engine (FF clone). It didn't have sound, but it did most of the cool stuff like tilemaps and I even implemented a rudimentary scripting language and characters talking etc. I never made a game out of it because I lost interest, partially because the project got too big for 17 year old me.
These past few months I've started work on a new game that I need to use 3D for. It's in space and all it really needs to be is pretty spheres and a UI. I thought that should be fairly simple.
So, I've been reading all about OpenGL and SDL2 and I've started to get some cubemapped spheres being displayed with some lighting. I'm working on shaders now for lens flare or something to make the sun look cool. Anyway, I'm already starting to see that my code is getting difficult to manage.
Does anyone have some generic advice on how to structure a 3D engine like this? I've got a need for objects to use different materials and different shaders; I've got the possibility of millions of objects in total. I've got a need for screen-wide post processing, probably in multiple passes.
It's all working well now, but I have only two objects. I'm concerned that as scene-complexity grows that it becomes unmanageable. Just looking for generic advice.
Hey, I've just finished building an engine for a 3d top-down shooter supporting large maps and
a lot of enemies and objects. You're going to have to use an ide that supports virtual folders.
They help with organizing your source files under the correct categories. Example:
[Workspace]My_Engine:
[Folder]Display:
- display_init.cpp
- opengl.cpp
- directx.cpp
- shaders.cpp
[Folder]Map_Components:
- actors.cpp
- objects.cpp
- effectors.cpp
- particle_emitters.cpp
- particles.cpp
- projectiles.cpp
- map.cpp
[Folder]Sound:
- sound_init.cpp
- soundfx.cpp
- streams.cpp
[Folder]Controllers:
- keyboard.cpp
- mouse.cpp
- controller.cpp
[Folder]GUI:
- hud.cpp
- menu.cpp
[Folder]Backend:
- main.cpp
- file_loader.cpp
- events.cpp
- maths.cpp
- memory.cpp
[Folder]Headers:
- myengine_typedefs.h
- myengine_functions.h
- myengine_global_variables.h
This is just if you feel the need to organize your source files and if you ever decide to add more things to
your game. 3D scenes aren't that easy to manage. So, I hope you find my knowledge useful.
To manage a lot of objects in memory, I would use array variables as they're easier to sort and access.
You have to limit them though, but you can still allocate memory to an enemy array of say 1000000 elements.
This is also better than using linked lists which are the worst bits of code to work with.
In c/c++ it would look like this: ENEMY *enemy_array = (ENEMY **)malloc(sizeof(ENEMY *)*1000000);
But, you must be careful to allocate memory to each element that you use before adjusting enemy's properties. This
technique applies to scene objects as well.
Also, if you can use structures instead of classes, it will be easier to manage. I don't know what
language you're programming with, but you can always google up derivatives between c/c++ and whatever you're using.
The last thing that is VERY important with managing a scene and displaying it is limiting what you display based
on distance or visibility within your screen. You will have to create an array that will contain visible nodes
and all your nodes.
(Nodes represent every object on your map - enemies, your character, objects, static soundfx, etc.)
You will attach each class or structure (enemy, projectile, object) to a node so that it can
be sorted into what's visible and what's not. Whatever gets put into your visible nodes array
will be the ones that you draw, the rest will be skipped.
You can use this technique and modify it for other derivatives. Anyway, I hope you find this useful because
you're gonna struggle to get find generic 3d scene info for your game development. Otherwise, you should look
into using an engine like Unity. Most of the work's been done and a lot of guys use it to build all kinds
of indie games.