topical media & game development
graphic-directx-game-10-DiffuseDemo-d3dUtil.cpp / cpp
//=============================================================================
// d3dUtil.cpp by Frank Luna (C) 2005 All Rights Reserved.
//=============================================================================
include <d3dUtil.h>
void GenTriGrid(int numVertRows, int numVertCols,
float dx, float dz,
const D3DXVECTOR3& center,
std::vector<D3DXVECTOR3>& verts,
std::vector<DWORD>& indices)
{
int numVertices = numVertRows*numVertCols;
int numCellRows = numVertRows-1;
int numCellCols = numVertCols-1;
int numTris = numCellRows*numCellCols*2;
float width = (float)numCellCols * dx;
float depth = (float)numCellRows * dz;
//===========================================
// Build vertices.
// We first build the grid geometry centered about the origin and on
// the xz-plane, row-by-row and in a top-down fashion. We then translate
// the grid vertices so that they are centered about the specified
// parameter 'center'.
verts.resize( numVertices );
// Offsets to translate grid from quadrant 4 to center of
// coordinate system.
float xOffset = -width * 0.5f;
float zOffset = depth * 0.5f;
int k = 0;
for(float i = 0; i < numVertRows; ++i)
{
for(float j = 0; j < numVertCols; ++j)
{
// Negate the depth coordinate to put in quadrant four.
// Then offset to center about coordinate system.
verts[k].x = j * dx + xOffset;
verts[k].z = -i * dz + zOffset;
verts[k].y = 0.0f;
// Translate so that the center of the grid is at the
// specified 'center' parameter.
D3DXMATRIX T;
D3DXMatrixTranslation(&T, center.x, center.y, center.z);
D3DXVec3TransformCoord(&verts[k], &verts[k], &T);
++k; // Next vertex
}
}
//===========================================
// Build indices.
indices.resize(numTris * 3);
// Generate indices for each quad.
k = 0;
for(DWORD i = 0; i < (DWORD)numCellRows; ++i)
{
for(DWORD j = 0; j < (DWORD)numCellCols; ++j)
{
indices[k] = i * numVertCols + j;
indices[k + 1] = i * numVertCols + j + 1;
indices[k + 2] = (i+1) * numVertCols + j;
indices[k + 3] = (i+1) * numVertCols + j;
indices[k + 4] = i * numVertCols + j + 1;
indices[k + 5] = (i+1) * numVertCols + j + 1;
// next quad
k += 6;
}
}
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.