1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
void CDirectXFramework::DefineGrid(int Rows, int Cols, float dx, float dz, const D3DXVECTOR3& center) {
/* precalculations */
int CellRows = Rows - 1;
int CellCols = Cols - 1;
float width = (float)CellCols * dx;
float depth = (float)CellRows * dz;
//offsets to translate grid to center of coordinate system
float xOffset = -width * 0.5f;
float zOffset = depth * 0.5f;
/* build vertices */
vertices3 = 0;
VertexBuffer3->Lock(0, 0, (void**)&vertices3, 0);
int k = 0;
for(float i = 0; i < Rows; ++i) {
for(float j = 0; j < Cols; ++j) {
//negate the depth coordinates to put in Quadrant 4, then offset about center of coordinate system
vertices3[k].position = D3DXVECTOR3(j * dx + xOffset, 0.0f, -i * dz + zOffset); /* this line in particular is where the error occurs once i = 2, j = 58, and k = 258 */
//translate center of grid to the specified center
D3DXMATRIX T;
D3DXMatrixTranslation(&T, center.x, center.y, center.z);
D3DXVec3TransformCoord(&vertices3[k].position, &vertices3[k].position, &T);
D3DXVec3Normalize(&vertices3[k].normal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
vertices3[k].uv = D3DXVECTOR2((vertices3[k].position.x + (0.5f * width)) / width, (vertices3[k].position.z - (0.5f * depth)) / -depth);
++k; //next vertex
}
}
VertexBuffer3->Unlock();
/* build indices */
indices3 = 0;
IndexBuffer3->Lock(0, 0, (void**)&indices3, 0);
//generate indices for each quad
k = 0;
for(DWORD i = 0; i < (DWORD)Rows; ++i) {
for(DWORD j = 0; j < (DWORD)Cols; ++j) {
//1st triangle
indices3[k] = i * Cols + j;
indices3[k + 1] = i * Cols + j + 1;
indices3[k + 2] = (i + 1) * Cols + j;
//2nd triangle
indices3[k + 3] = (i + 1) * Cols + j;
indices3[k + 4] = i * Cols + j + 1;
indices3[k + 5] = (i + 1) * Cols + j + 1;
k += 6; //next quad
}
}
IndexBuffer3->Unlock();
}
|