|
|

楼主 |
发表于 2008-12-28 00:06:00
|
显示全部楼层
Re:一个再简单不过的D3D9的小程序,为什么我的 MESH 变形
fuiqy0202 兄弟啊。。。
我还没有学到骨骼动画那里去啊。。。
这个立方体,是直接用 D3DXCreateMeshFVF 创建的。。。。
就是通过自己创建若干个顶点来实现的一个基本的MESH,骨骼动画完全没有涉及到。。
这是创建立方体的代码:
- bool Setup()
- {
- HRESULT hr = 0;
- //
- // We are going to fill the empty mesh with the geometry of a box,
- // so we need 12 triangles and 24 vetices.
- //
- hr = D3DXCreateMeshFVF(
- 12,
- 24,
- D3DXMESH_MANAGED,
- Vertex::FVF,
- Device,
- &Mesh);
- if(FAILED(hr))
- {
- ::MessageBox(0, "D3DXCreateMeshFVF() - FAILED", 0, 0);
- return false;
- }
- //
- // Fill in vertices of a box
- //
- Vertex* v = 0;
- Mesh->LockVertexBuffer(0, (void**)&v);
- // fill in the front face vertex data
- v[0] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
- v[1] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
- v[2] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
- v[3] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
- // fill in the back face vertex data
- v[4] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
- v[5] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
- v[6] = Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
- v[7] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
- // fill in the top face vertex data
- v[8] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
- v[9] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
- v[10] = Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
- v[11] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
- // fill in the bottom face vertex data
- v[12] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);
- v[13] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f);
- v[14] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f);
- v[15] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
- // fill in the left face vertex data
- v[16] = Vertex(-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
- v[17] = Vertex(-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
- v[18] = Vertex(-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
- v[19] = Vertex(-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
- // fill in the right face vertex data
- v[20] = Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
- v[21] = Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
- v[22] = Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
- v[23] = Vertex( 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
- Mesh->UnlockVertexBuffer();
- //
- // Define the triangles of the box
- //
- WORD* i = 0;
- Mesh->LockIndexBuffer(0, (void**)&i);
- // fill in the front face index data
- i[0] = 0; i[1] = 1; i[2] = 2;
- i[3] = 0; i[4] = 2; i[5] = 3;
- // fill in the back face index data
- i[6] = 4; i[7] = 5; i[8] = 6;
- i[9] = 4; i[10] = 6; i[11] = 7;
- // fill in the top face index data
- i[12] = 8; i[13] = 9; i[14] = 10;
- i[15] = 8; i[16] = 10; i[17] = 11;
- // fill in the bottom face index data
- i[18] = 12; i[19] = 13; i[20] = 14;
- i[21] = 12; i[22] = 14; i[23] = 15;
- // fill in the left face index data
- i[24] = 16; i[25] = 17; i[26] = 18;
- i[27] = 16; i[28] = 18; i[29] = 19;
- // fill in the right face index data
- i[30] = 20; i[31] = 21; i[32] = 22;
- i[33] = 20; i[34] = 22; i[35] = 23;
- Mesh->UnlockIndexBuffer();
- //
- // Specify the subset each triangle belongs to, in this example
- // we will use three subsets, the first two faces of the cube specified
- // will be in subset 0, the next two faces will be in subset 1 and
- // the the last two faces will be in subset 2.
- //
- DWORD* attributeBuffer = 0;
- Mesh->LockAttributeBuffer(0, &attributeBuffer);
- for(int a = 0; a < 4; a++)
- attributeBuffer[a] = 0;
- for(int b = 4; b < 8; b++)
- attributeBuffer[b] = 1;
- for(int c = 8; c < 12; c++)
- attributeBuffer[c] = 2;
- Mesh->UnlockAttributeBuffer();
- //
- // Optimize the mesh to generate an attribute table.
- //
- std::vector<DWORD> adjacencyBuffer(Mesh->GetNumFaces() * 3);
- Mesh->GenerateAdjacency(0.0f, &adjacencyBuffer[0]);
- hr = Mesh->OptimizeInplace(
- D3DXMESHOPT_ATTRSORT |
- D3DXMESHOPT_COMPACT |
- D3DXMESHOPT_VERTEXCACHE,
- &adjacencyBuffer[0],
- 0, 0, 0);
- //
- // Load the textures and set filters.
- //
- D3DXCreateTextureFromFile(
- Device,
- "brick0.jpg",
- &Textures[0]);
- D3DXCreateTextureFromFile(
- Device,
- "brick1.jpg",
- &Textures[1]);
- D3DXCreateTextureFromFile(
- Device,
- "checker.jpg",
- &Textures[2]);
- Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
- //
- // Disable lighting.
- //
- Device->SetRenderState(D3DRS_LIGHTING, false);
- //
- // Set camera.
- //
- D3DXVECTOR3 pos(0.0f, 0.f, -4.0f);
- D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
- D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
- D3DXMATRIX V;
- D3DXMatrixLookAtLH(
- &V,
- &pos,
- &target,
- &up);
- Device->SetTransform(D3DTS_VIEW, &V);
- //
- // Set projection matrix.
- //
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI * 0.5f, // 90 - degree
- (float)Width / (float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION, &proj);
- return true;
- }
复制代码 |
|