0
\$\begingroup\$

My map loading and rendering process is slow when using very large maps, so I'm looking for ways to make it more efficient.

My current solution works like this:

  • My map loader uses for loops for loading text based files and reading the map.

    • It parses the objects in the map and converts them to an array

    • The map is a text file that stores game map contents as a list of object information.

    • The input data looks like (x,y,z),material,blocktype (x,y,z),material,blocktype...

      Scale does not need stored in the file because it's not implemented .

  • My rendering for loop traverses the array with a for loop

    • It checks if the entry in the array contains a pyramid

    • If so, then it calls the pyramid object's rendering method, taking other parameters

The issue is simply slow rendering due this for loop in my OpenGL rendering code used for calling objects by checking values on arrays. It becomes slow with big objects. How can I make this faster?

I use different map loaders one for networking (receiving player object sending info). Here's one for loading maps:

      void MapLoad(string filelocation)
      {

            string[] map_;
            
            string line = File.ReadAllText(filelocation);//
            map_ = line.Split(new char[] { ',', ';', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);


            for (int xj = 0; xj < map_.Length; xj += 5)
            {
                // Here xj is an index of a X coordinate, xj + 1 is an index of a Y coordinate, xj + 2 is an index of a Z coordinate

    //object for storing the data
                    Block_ chunk_block = new Block_();
    //coordinates set
                    chunk_block.set_v1(new Vector3(Int32.Parse(map_[xj]), Int32.Parse(map_[xj + 1]), Int32.Parse(map_[xj + 2])));
    //material set
                    chunk_block.set_v1_material(Int32.Parse(map_[xj + 3]));//1
    //block type set 
                    chunk_block.set_v1_blocktype(Int32.Parse(map_[xj + 4]));
                    chunk_block.set_v1_scale(new Vector3(1, 1, 1));
                    stuff__.block_file.Add(chunk_block);
                    stuff__.collision.Add(chunk_block.get_v1());
               
            }

        }

    //version for position
    void MapLoad(string filelocation,Vector3 position)
    {
    
            string[] map_;
            
            string line = File.ReadAllText(filelocation);//
            map_ = line.Split(new char[] { ',', ';', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
    
            
            for (int xj = 0; xj < map_.Length; xj += 5)
            {
                // Here xj is an index of a X coordinate, xj + 1 is an index of a Y coordinate, xj + 2 is an index of a Z coordinate
    
                Block_ chunk_block = new Block_();
    //position
                chunk_block.set_v1(new Vector3(Int32.Parse(map_[xj])+position.X, Int32.Parse(map_[xj + 1]) + position.Y, Int32.Parse(map_[xj + 2]) + position.Z));
    //mat
                chunk_block.set_v1_material(Int32.Parse(map_[xj + 3]));//1
    //blocktype
                chunk_block.set_v1_blocktype(Int32.Parse(map_[xj + 4]));
                chunk_block.set_v1_scale(new Vector3(1, 1, 1));
                stuff__.block_file.Add(chunk_block);
                stuff__.collision.Add(new Vector3(chunk_block.get_v1().X, chunk_block.get_v1().Y+1, chunk_block.get_v1().Z));
                
            }
    
        }

And here is my rendering loop:

for (int i = 0; i < stuff__.block_file.Count; i++)
{
        //block data given to block model object that takes material for textures etc but thats not about problem
        block(new Vector3(stuff__.block_file[i].get_v1().X, stuff__.block_file[i].get_v1().Y-2 + walking__, stuff__.block_file[i].get_v1().Z), stuff__.block_file[i], BeginMode.Quads, stuff__.block_file[i].get_v1_scale());        
}

\$\endgroup\$
5
  • \$\begingroup\$ What's that "map" ? Wild guess: maybe you want to look into LOD techniques and spacial datastructures ? Anyway, pls. describe the problem in more detail, and be as specific as possible, pls. \$\endgroup\$ Commented Jan 14, 2021 at 15:23
  • 1
    \$\begingroup\$ @TRGamerTR It might help to read the Stack Exchange reference How to create a Minimal, Reproducible Example. Also, rather than mentioning the language in the question body, it's better to use the appropriate tags - this will automatically highlight your question to users with an interest in that tag. \$\endgroup\$ Commented Jan 14, 2021 at 16:30
  • \$\begingroup\$ @Pikalek i have issues with back ticks they make text included as code what to do then including language in code probablly better , its most simplest one not sure even it going to compile beacuse of risk of missing paranthesis theres not a issue either i asked about alternative for that for loop in rendering \$\endgroup\$ Commented Jan 14, 2021 at 16:38
  • 1
    \$\begingroup\$ Do I understand that you're trying to render a Minecraft-like voxel world? If so, we have lots of previous Q&A demonstrating how to make various optimizations for that kind of rendering problem. Drawing each cube one by one as you're doing will not give good performance — try searching for "chunk" rendering for better approaches you can use. \$\endgroup\$ Commented Jan 14, 2021 at 17:14
  • \$\begingroup\$ @TRGamerTR I'm not sure I understand your response. I was recommending 1) understanding Minimal Reproducible Examples because they are the preferred norm for Stack Exchange & 2) next time you include the C# tag on your post instead of saying "this is c sharp code" in the question text. \$\endgroup\$ Commented Jan 14, 2021 at 18:51

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.