仅使用一个平移矩阵时,多个网格物体在屏幕上会出现重叠。使用DirectX/C++在不同位置呈现多个网格物体的高效方法
我试过的解决方案是创建多个平移矩阵来为每个网格设置不同的初始xyz坐标。它可以工作,但是就使用的代码行数而言,这种方法效率很低。 (最终的项目可能会包含20多个网格,所以我希望不需要使用基本相同的代码段来创建20个不同的翻译矩阵)。
我非常感谢任何关于以最有效的代码使用方式呈现多个网格的最佳方式(即最少重复次数最少的指令)。
这只是一个小型的图形演示,因此获得高帧速率并不是优先考虑的事情,但是实现最有效使用代码的结果是。
下面的代码是如何我目前呈现多个网格,在不同位置的样本...
// 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);
//////////////////////////////////////////////////////////////////////
嗨jd, 你在那里写得很好,我在原文中转述了我的问题。谢谢。 – Vault13
编辑回答您的编码问题......当您使用循环和函数调用拆分此问题时,您会非常高兴! –
谢谢jd - 我会努力将其分解为基于函数的方法。如果我得到这个工作,我会在这里添加一个小例子供其他人参考。 – Vault13