What type of programming structure can I use to enumerate classes?

I am creating a minecraft style voxel engine. I store the level in one 3D array at a time when each value represents a voxel type, for example. 0 = air, 1 = stone, etc. Later voxels must have certain properties, for example. some may be textured, and some actions may need to be performed when they are placed.

The first and most obvious solution that I was thinking about was to have a voxel base class with methods like OnPlace etc. that would be obtained by other types. But I'm not sure how to associate this with the map, I need a way that will be very fast (ray tracing is so slow) and flexible, but I have no idea. Any tips?

+4
source share
5 answers

I think you'll need a Dictionary . This is great to give each class an ID that you can easily add to the readonly public property. Then create a dictionary with the key being the identifier and the value being the type of the class. Thus, you can effectively maintain the level and use a dictionary (fast, but somewhat expensive in memory) to see how each block should be displayed.

You will need to use reflection and repeat all subclasses of your voxel base class when the program starts to create the dictionary.

If you plan to support mods / plugins, you will need to look for more than your own assembly (the most difficult part is to get a link to their assembly, after which it is only redesigned code), but for now, just start with your own.

+3
source

The obvious approach is to have a 3d array of objects ( class Voxel ) and have a backlink using location = {int x, int y, int z}; .

Note that it might be a good idea to completely drop the link from the voxel to the location, so you don't need to use unique objects per cell. You might consider using a flyweight pattern to share most of the date for such “voxel” objects, even if you still have to return the map link.

+3
source

I'm not quite what you are asking for, because when you say "list classes", it makes me think of iterating over the properties of objects using reflection; that this is not what you want to do.

For me, it is more like your 3D array should contain objects, not integers. If you have a Voxel base class that inherits all voxels from you, remove the need for a type specifier. Instead, you simply put the abstract methods in the base class and override them in the derived classes. After you have all this, any code dealing with an array should be able to interact with it without knowing the type of the derived class.

The application code should just view it as an array of type Voxel . Invocation methods for any given Voxel must perform behavior specific to this type of voxel. This is the basic idea of ​​inheritance and polymorphism.

0
source

Well, your performance bottleneck is not a "voxel type choice." This intersection and collision detection for literally millions of rays (at least 1 per pixel, but you are not going to get good pictures with only 1 rays per pixel), you need to track at a speed of 60 frames per second.

The only way to do such madness these days is GPU ray tracing

Therefore, your main problem is not “what data structure to use” to determine the type of voxel. This is an efficient ray intersection code on a GPU.

NVIDIA currently has a ray tracing mechanism called Optix .

0
source

Much depends on how you need to go through the data for rendering voxels . For rendering, octree might be a good solution (or maybe a Kd tree )

I suggest that you first focus on what access is needed (possibly through testing), and then optimize.

0
source

Source: https://habr.com/ru/post/1483431/


All Articles