void TriGridDemo::buildGeoBuffers() { std::vector<D3DXVECTOR3> verts; std::vector<DWORD> indices; GenTriGrid(100, 100, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices); mNumVertices = 100*100; mNumTriangles = 99*99*2; HR(gd3dDevice->CreateVertexBuffer(mNumVertices * sizeof(VertexPos), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0)); VertexPos *v = 0; HR(mVB->Lock(0, 0, (void**)&v, 0)); for (DWORD i = 0; i < mNumVertices; ++i) v[i] = verts[i]; HR(mVB->Unlock()); HR(gd3dDevice->CreateIndexBuffer(mNumTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0)); WORD *k = 0; HR(mIB->Lock(0, 0, (void**)&k, 0)); for (DWORD i = 0; i < mNumTriangles*3; ++i) k[i] = (WORD)indices[i]; HR(mIB->Unlock()); }
void MultiTexDemo::buildGridGeometry() { std::vector<D3DXVECTOR3> verts; std::vector<DWORD> indices; GenTriGrid(100, 100, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices); // Save vertex count and triangle count for DrawIndexedPrimitive arguments. mNumGridVertices = 100*100; mNumGridTriangles = 99*99*2; // Obtain a pointer to a new vertex buffer. HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPNT), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mGridVB, 0)); // Now lock it to obtain a pointer to its internal data, and write the // grid's vertex data. VertexPNT* v = 0; HR(mGridVB->Lock(0, 0, (void**)&v, 0)); float w = 99.0f; float d = 99.0f; for(int i = 0; i < 100; ++i) { for(int j = 0; j < 100; ++j) { DWORD index = i * 100 + j; v[index].pos = verts[index]; v[index].normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f); v[index].tex0.x = (v[index].pos.x + (0.5f*w)) / w; v[index].tex0.y = (v[index].pos.z - (0.5f*d)) / -d; } } HR(mGridVB->Unlock()); // Obtain a pointer to a new index buffer. HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mGridIB, 0)); // Now lock it to obtain a pointer to its internal data, and write the // grid's index data. WORD* k = 0; HR(mGridIB->Lock(0, 0, (void**)&k, 0)); for(DWORD i = 0; i < mNumGridTriangles*3; ++i) k[i] = (WORD)indices[i]; HR(mGridIB->Unlock()); }
void SpotlightDemo::buildGeoBuffers() { std::vector<D3DXVECTOR3> verts; std::vector<DWORD> indices; GenTriGrid(100, 100, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices); // Save vertex count and triangle count for DrawIndexedPrimitive arguments. mNumGridVertices = 100*100; mNumGridTriangles = 99*99*2; // Obtain a pointer to a new vertex buffer. HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPN), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0)); // Now lock it to obtain a pointer to its internal data, and write the // grid's vertex data. VertexPN* v = 0; HR(mVB->Lock(0, 0, (void**)&v, 0)); for(DWORD i = 0; i < mNumGridVertices; ++i) { v[i].pos = verts[i]; v[i].normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f); } HR(mVB->Unlock()); // Obtain a pointer to a new index buffer. HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0)); // Now lock it to obtain a pointer to its internal data, and write the // grid's index data. WORD* k = 0; HR(mIB->Lock(0, 0, (void**)&k, 0)); for(DWORD i = 0; i < mNumGridTriangles*3; ++i) k[i] = (WORD)indices[i]; HR(mIB->Unlock()); }
PondWater::PondWater(InitInfo& initInfo) { mInitInfo = initInfo; mWidth = (initInfo.vertCols-1)*initInfo.dx; mDepth = (initInfo.vertRows-1)*initInfo.dz; mWaveMapOffset0 = D3DXVECTOR2(0.0f, 0.0f); mWaveMapOffset1 = D3DXVECTOR2(0.0f, 0.0f); DWORD numTris = (initInfo.vertRows-1)*(initInfo.vertCols-1)*2; DWORD numVerts = initInfo.vertRows*initInfo.vertCols; //=============================================================== // Allocate the mesh. D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE]; UINT numElems = 0; HR(VertexPT::Decl->GetDeclaration(elems, &numElems)); HR(D3DXCreateMesh(numTris, numVerts, D3DXMESH_MANAGED, elems, gd3dDevice, &mMesh)); //=============================================================== // Write the grid vertices and triangles to the mesh. VertexPT* v = 0; HR(mMesh->LockVertexBuffer(0, (void**)&v)); std::vector<D3DXVECTOR3> verts; std::vector<DWORD> indices; GenTriGrid(mInitInfo.vertRows, mInitInfo.vertCols, mInitInfo.dx, mInitInfo.dz, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices); for(int i = 0; i < mInitInfo.vertRows; ++i) { for(int j = 0; j < mInitInfo.vertCols; ++j) { DWORD index = i * mInitInfo.vertCols + j; v[index].pos = verts[index]; v[index].tex0 = D3DXVECTOR2((float)j/mInitInfo.vertCols, (float)i/mInitInfo.vertRows) * initInfo.texScale; } } HR(mMesh->UnlockVertexBuffer()); //=============================================================== // Write triangle data so we can compute normals. WORD* indexBuffPtr = 0; HR(mMesh->LockIndexBuffer(0, (void**)&indexBuffPtr)); DWORD* attBuff = 0; HR(mMesh->LockAttributeBuffer(0, &attBuff)); for(UINT i = 0; i < mMesh->GetNumFaces(); ++i) { indexBuffPtr[i*3+0] = (WORD)indices[i*3+0]; indexBuffPtr[i*3+1] = (WORD)indices[i*3+1]; indexBuffPtr[i*3+2] = (WORD)indices[i*3+2]; attBuff[i] = 0; // All in subset 0. } HR(mMesh->UnlockIndexBuffer()); HR(mMesh->UnlockAttributeBuffer()); //=============================================================== // Optimize for the vertex cache and build attribute table. DWORD* adj = new DWORD[mMesh->GetNumFaces()*3]; HR(mMesh->GenerateAdjacency(EPSILON, adj)); HR(mMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE|D3DXMESHOPT_ATTRSORT, adj, 0, 0, 0)); delete[] adj; //=============================================================== // Create textures/effect. HR(D3DXCreateTextureFromFile(gd3dDevice, initInfo.waveMapFilename0.c_str(), &mWaveMap0)); HR(D3DXCreateTextureFromFile(gd3dDevice, initInfo.waveMapFilename1.c_str(), &mWaveMap1)); D3DVIEWPORT9 vp = {0, 0, 512, 512, 0.0f, 1.0f}; mRefractMap = new DrawableTex2D(512, 512, 0, D3DFMT_X8R8G8B8, true, D3DFMT_D24X8, vp, true); mReflectMap = new DrawableTex2D(512, 512, 0, D3DFMT_X8R8G8B8, true, D3DFMT_D24X8, vp, true); buildFX(); }
void Terrain::buildSubGridMesh(RECT& R, VertexPNT* gridVerts) { //=============================================================== // Create the subgrid mesh. ID3DXMesh* subMesh = 0; D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE]; UINT numElems = 0; HR(VertexPNT::Decl->GetDeclaration(elems, &numElems)); HR(D3DXCreateMesh(SubGrid::NUM_TRIS, SubGrid::NUM_VERTS, D3DXMESH_MANAGED, elems, gd3dDevice, &subMesh)); //=============================================================== // Build Vertex Buffer. Copy rectangle of vertices from the // grid into the subgrid structure. VertexPNT* v = 0; HR(subMesh->LockVertexBuffer(0, (void**)&v)); int k = 0; for(int i = R.top; i <= R.bottom; ++i) { for(int j = R.left; j <= R.right; ++j) { v[k++] = gridVerts[i*mVertCols+j]; } } //=============================================================== // Compute the bounding box before unlocking the vertex buffer. AABB bndBox; HR(D3DXComputeBoundingBox((D3DXVECTOR3*)v, subMesh->GetNumVertices(), sizeof(VertexPNT), &bndBox.minPt, &bndBox.maxPt)); HR(subMesh->UnlockVertexBuffer()); //=============================================================== // Build Index and Attribute Buffer. // Get indices for subgrid (we don't use the verts here--the verts // are given by the parameter gridVerts). std::vector<D3DXVECTOR3> tempVerts; std::vector<DWORD> tempIndices; GenTriGrid(SubGrid::NUM_ROWS, SubGrid::NUM_COLS, mDX, mDZ, D3DXVECTOR3(0.0f, 0.0f, 0.0f), tempVerts, tempIndices); WORD* indices = 0; DWORD* attBuff = 0; HR(subMesh->LockIndexBuffer(0, (void**)&indices)); HR(subMesh->LockAttributeBuffer(0, &attBuff)); for(int i = 0; i < SubGrid::NUM_TRIS; ++i) { indices[i*3+0] = (WORD)tempIndices[i*3+0]; indices[i*3+1] = (WORD)tempIndices[i*3+1]; indices[i*3+2] = (WORD)tempIndices[i*3+2]; attBuff[i] = 0; // All in subset 0. } HR(subMesh->UnlockIndexBuffer()); HR(subMesh->UnlockAttributeBuffer()); //=============================================================== // Optimize for the vertex cache and build attribute table. DWORD* adj = new DWORD[subMesh->GetNumFaces()*3]; HR(subMesh->GenerateAdjacency(EPSILON, adj)); HR(subMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE|D3DXMESHOPT_ATTRSORT, adj, 0, 0, 0)); delete[] adj; //=============================================================== // Save the mesh and bounding box. mSubGridMeshes.push_back(subMesh); mSubGridBndBoxes.push_back(bndBox); }
void Terrain::buildGeometry() { //=============================================================== // Create one large mesh for the grid in system memory. DWORD numTris = (mVertRows-1)*(mVertCols-1)*2; DWORD numVerts = mVertRows*mVertCols; ID3DXMesh* mesh = 0; D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE]; UINT numElems = 0; HR(VertexPNT::Decl->GetDeclaration(elems, &numElems)); HR(D3DXCreateMesh(numTris, numVerts, D3DXMESH_SYSTEMMEM|D3DXMESH_32BIT, elems, gd3dDevice, &mesh)); //=============================================================== // Write the grid vertices and triangles to the mesh. VertexPNT* v = 0; HR(mesh->LockVertexBuffer(0, (void**)&v)); std::vector<D3DXVECTOR3> verts; std::vector<DWORD> indices; GenTriGrid(mVertRows, mVertCols, mDX, mDZ, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices); float w = mWidth; float d = mDepth; for(UINT i = 0; i < mesh->GetNumVertices(); ++i) { // We store the grid vertices in a linear array, but we can // convert the linear array index to an (r, c) matrix index. int r = i / mVertCols; int c = i % mVertCols; v[i].pos = verts[i]; v[i].pos.y = mHeightmap(r, c); v[i].tex0.x = (v[i].pos.x + (0.5f*w)) / w; v[i].tex0.y = (v[i].pos.z - (0.5f*d)) / -d; } // Write triangle data so we can compute normals. DWORD* indexBuffPtr = 0; HR(mesh->LockIndexBuffer(0, (void**)&indexBuffPtr)); for(UINT i = 0; i < mesh->GetNumFaces(); ++i) { indexBuffPtr[i*3+0] = indices[i*3+0]; indexBuffPtr[i*3+1] = indices[i*3+1]; indexBuffPtr[i*3+2] = indices[i*3+2]; } HR(mesh->UnlockIndexBuffer()); // Compute Vertex Normals. HR(D3DXComputeNormals(mesh, 0)); //=============================================================== // Now break the grid up into subgrid meshes. // Find out the number of subgrids we'll have. For example, if // m = 513, n = 257, SUBGRID_VERT_ROWS = SUBGRID_VERT_COLS = 33, // then subGridRows = 512/32 = 16 and sibGridCols = 256/32 = 8. int subGridRows = (mVertRows-1) / (SubGrid::NUM_ROWS-1); int subGridCols = (mVertCols-1) / (SubGrid::NUM_COLS-1); for(int r = 0; r < subGridRows; ++r) { for(int c = 0; c < subGridCols; ++c) { // Rectangle that indicates (via matrix indices ij) the // portion of global grid vertices to use for this subgrid. RECT R = { c * (SubGrid::NUM_COLS-1), r * (SubGrid::NUM_ROWS-1), (c+1) * (SubGrid::NUM_COLS-1), (r+1) * (SubGrid::NUM_ROWS-1) }; buildSubGridMesh(R, v); } } HR(mesh->UnlockVertexBuffer()); ReleaseCOM(mesh); // Done with global mesh. }