Jump to content

Mesh generation lag (c++)


Slastraf
 Share

Go to solution Solved by Slastraf,

Recommended Posts

void generateTest(){
    //const siv::PerlinNoise perlin(seed);
    Surface* surface = NULL;
    Model* model = NULL;
    for(int i = 0; i<1000; i++)
    {
            model = Model::Create();
            model->SetColor(1.0, 0.0, 1.0);
            surface = model->AddSurface(); 
            surface->AddVertex(0.5, 0, 0.5, 0, 0, 1);
            surface->AddVertex(0.5, 0, -0.5, 0, 0, 1);
            surface->AddVertex(-0.5, 0, 0.5, 0, 0, 1);
            surface->AddVertex(-0.5, 0, -0.5, 0, 0, 1);
            surface->AddTriangle(0, 1, 2);
            surface->AddTriangle(1, 2, 3);
            surface->Update();  
            model->UpdateAABB(Entity::LocalAABB | Entity::GlobalAABB);
    }
}

Hello
 

after I wanted to make a mesh generation algorithm I found out that I get a lot of lag when trying to have a small amount of vertex (<10k) in the scene
If you run the test example above, it will make the game run at 2 fps or less.
It does not max out my ram / gpu at all. I guess it has to do with memory leaks because I did not delete surface and model pointer.
Then I tried to do the latter but it made different errors.

Link to comment
Share on other sites

Your code above is creating 1000 unique model entities. I would expect that to result in pretty slow rendering. The number of entities, and especially the number of unique meshes, if often more important than the vertex count.

If you created one and then made 999 instances of it, it would run much faster.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • Solution
2 minutes ago, Josh said:

Your code above is creating 1000 new model entities. I would expect that to be a fairly slow process.

That pointed me in the right direction. I made everything into one model, it still lagged, then I also made only one surface and then I got acceptable performance. Still it should not lag in the first place. The code looks like this and now I can proceed. 
 

void generateTest(){

 //   std::random_device rd; 
 //   std::uint32_t seed = rd();
	//const siv::PerlinNoise perlin(seed);
    Surface* surface = NULL;
    Model* model = NULL;
    model = Model::Create();
    model->SetColor(1.0, 0.0, 1.0);
    surface = model->AddSurface(); 
    for(int i = 0; i<1000; i++)
    {
            surface->AddVertex(0.5, 0, 0.5, 0, 0, 1);
            surface->AddVertex(0.5, 0, -0.5, 0, 0, 1);
            surface->AddVertex(-0.5, 0, 0.5, 0, 0, 1);
            surface->AddVertex(-0.5, 0, -0.5, 0, 0, 1);
            surface->AddTriangle(0, 1, 2);
            surface->AddTriangle(1, 2, 3);
            surface->Update();  
            model->UpdateAABB(Entity::LocalAABB | Entity::GlobalAABB);
    }
}

 

Link to comment
Share on other sites

4 hours ago, Slastraf said:

surface->Update();

model->UpdateAABB(Entity::LocalAABB | Entity::GlobalAABB);

This should probably outside your loop as well.  It won't effect render performance but it will generate the mesh faster as updateAABB() I think loops through all vertices in the model and finds the extents of the mesh for the AABB.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...