Thursday, September 22, 2022
HomeGame Developmentopengl - The right way to permit objects in a listing to...

opengl – The right way to permit objects in a listing to entry the thing that owns the record?


I am attempting to make a voxel sport whereas studying about OpenGL. The world is made up of many chunks of voxels. Subsequently there’s a Chunk class, containing an array of parts (which for now simply are ints indicating if a voxel is strong (1) or empty(0) ).

There’s a single World object that shops an std::vector<chunk> chunks member record. world is accountable for creating and initializing the entire chunks inside. Nevertheless, an vital step in rendering the precise voxels is for the chunks to create a mesh primarily based on which voxels are empty and that are full. To do that, every chunk iterates over its aspect array. If a voxel is strong, then it checks the orthogonally adjoining voxels and creates a quad (a category that encapsulates two triangles formatted for OpenGL) face on every uncovered facet.

A difficulty arises when the examined voxel is on the sting of its chunk. To verify adjoining voxels previous the sting, it must know information from a neighboring chunk about them. I’ve already tried storing a listing Chunk* neighbors[6] of neighboring chunks in every chunk, and assigning them through the world earlier than producing the mesh. Nevertheless this nonetheless leaves holes the place quads ought to have been generated alongside some borders, and inside partitions of quads alongside others the place none ought to have been generated.

View of the world; Terrain generated by noise, colors provided generously by the sin() function. Note the regular pattern of internal walls inside the structure and the gaps along borders near the top. This is what I'm trying to solve.

To generalize the method and make it extra accessible to entry details about any voxel on this planet (which may clearly be helpful afterward for gameplay causes), I then determined to implement a PositionInWorldIsSolid(x, y, z) perform within the World class. The concept is that every chunk solely must retailer a pointer to the world that owns all of them, and it may then name this perform to entry any chunk on this planet if it wants info (on this early case, nearly solidity). Nevertheless, right here I run into an issue.

The World class definition in World.h already contains the Chunk class #embody "Chunk.h" so it may retailer the record of chunks, since typically the world needs to be appearing on them and never the opposite means round. When I attempt to go backward and let the chunks entry details about the opposite chunks by way of the world, they should retailer a World* world referring to the only world object that owns them. To permit the chunks to make use of the kind World I must #embody "World.h" within the Chunk.h file. That is clearly an issue; now I am defining two courses by one another. Is there anyway I can get round this and permit every chunk to entry the perform in world or the record?

For context, that is in all probability the biggest undertaking I’ve ever tried and I am positively not used to working with this many shifting components, however the entire level is to achieve expertise. That mentioned, the undertaking was an extension of a learnOpenGL tutorial undertaking in visible studio; for no matter cause the tutorial makes use of header information for all class definitions, and solely makes use of a .cpp file for the file containing predominant(). I have been following suite since I have never typically had to make use of each ever earlier than, however apparently there’s a means to do that by ahead declaring the courses every of their .h information and together with every of their .cpp information (or maybe vice-versa). I have never tried this in case it is quite a lot of work to create .cpp‘s for each header concerned, but when that is the advisable answer I want to know.

Please keep in mind I’m actively attempting to be taught and get this to work; I welcome recommendation together with criticism, but when your reply is to name me silly for attempting or say I should not even be making an attempt this but you might be neither serving to me be taught nor serving to the programming neighborhood to develop.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments