Efficient way to render multiple mesh objects in different positions using DirectX / C++

StackOverflow https://stackoverflow.com/questions/21106205

  •  27-09-2022
  •  | 
  •  

When using only one translation matrix, multiple meshes appear overlapping onscreen.

The solution I tried was to create multiple translation matrices to set different initial xyz coordinates for each mesh. It works, but this method seems pretty inefficient in terms of the number of lines of code used. (The final project will probably incorporate 20+ meshes so I was hoping I would not need to create 20 different translation matrices using basically the same section of code).

I'd greatly appreciate any suggestions as to the best way of rendering multiple meshes with the most efficient use of code (i.e fewest instructions with least amount of repetition).

This is only a small graphical demo so getting a high framerate is not the priority, but achieving the result with the most efficient use of code is.

Below Code is a sample of how i'm currently rendering multiple meshes in different positions...

 // initial locations for each mesh
    D3DXVECTOR3 Translate1(-30.0f, 0.0f, 0.0f);                     
    D3DXVECTOR3 Translate2(-30.0f, 10.0f, 0.0f);            
    D3DXVECTOR3 Translate3(0.0f, 0.0f, 0.0f);               
    D3DXVECTOR3 Translate4(0.0f, 10.0f, 0.0f);              

    //set scaling on all x y and z axis
    D3DXVECTOR3 Scaling(g_ScaleX, g_ScaleY, g_ScaleZ);



    /////////////////////////////////////////////////////////////////////////

    // create first transformation matrix
    D3DXMATRIX world1; 

    // create first translation matrix
    D3DXMATRIX matrixTranslate;     

    D3DXMatrixTransformation(&world1, &ScalingCentre, &ScalingRotation, &Scaling, &RotationCentre, &Rotation, &Translate1);

    //D3DXMatrixIdentity(&world1);  // set world1 as current transformation matrix

    // set world1 as current transformation matrix for future meshes
    g_pd3dDevice -> SetTransform(D3DTS_WORLD, &world1);

    // recompute normals
    g_pd3dDevice -> SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

    // render first mesh
    mesh1.Render(g_pd3dDevice);

    /////////////////////////////////////////////////////////////////////////

    D3DXMATRIX world2;

    D3DXMATRIX matrixTranslate2;


    D3DXMatrixTransformation(&world2, &ScalingCentre, &ScalingRotation, &Scaling, &RotationCentre, &Rotation, &Translate2);


    // set world2 as current transformation matrix for future meshes
    g_pd3dDevice -> SetTransform(D3DTS_WORLD, &world2);

    // render second mesh
    mesh2.Render(g_pd3dDevice);


    ////////////////////////////////////////////////////////////////////////

    D3DXMATRIX world3;

    D3DXMATRIX matrixTranslate3;

    D3DXMatrixTransformation(&world3, &ScalingCentre, &ScalingRotation, &Scaling, &RotationCentre, &Rotation, &Translate3);


    // set world2 as current transformation matrix for future meshes
    g_pd3dDevice -> SetTransform(D3DTS_WORLD, &world3);

     //render thirdmesh
    mesh3.Render(g_pd3dDevice);

    //////////////////////////////////////////////////////////////////////
有帮助吗?

解决方案

Edit: I see by efficient you meant 'compact code' (it usually means 'less cpu usage' :) In that case, yes, I noticed that you copy and paste essentially the same code. Why don't you use a function that takes parameters, including a transform and a mesh? That way, you can write one function that draws a mesh, and call it for each mesh. Better yet, also store the meshes in an array, then iterate over the array, calling the draw function for each element. I think you should read up on basic tutorials for C/C++. Have fun!

http://www.cplusplus.com/doc/tutorial/

Original comment:

The cost of calculating and setting a transform is much smaller than the cost of rendering a mesh, so i think there is no problem here we can help you solve. For example, is your framerate low?

When thinking about performance (in computer graphics or otherwise), try to express your problem as a measurable statement, rather than guesses based on feel. Start by describing your purpose (e.g. drawing multiple meshes at a good frame rate), then describe what isn't working, then develop theories as to why, which you can then test.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top