kbz
kbz

Reputation: 1006

How to correctly draw a flat grid of triangles for terrain direct x 11 C++

I'm trying to draw a grid of triangles for the first part of my terrain mapping but there is a slight error and I'm not sure where I'm going wrong. Could someone please point out my error or explain how to correctly construct this grid.

To clarify, look at the right side of the grid, that shouldn't happen.

Vertices:

vector<Vertex> Terrain::generateVertices(int width, int height) {
    vector<Vertex> vertices;
    Vertex v;
    float du = 1.0f / (width - 1);
    float dv = 1.0f / (height - 1);
    for (int r = 0; r < width; ++r) {
        for (int c = 0; c < height; ++c) {
            v.Pos = XMFLOAT3((float)c, (float)r, 0.0f);
            v.Normal = XMFLOAT3(0.0f, 0.0f, 0.0f);
            v.TextureCoordinate = XMFLOAT2(c * du, r * dv);
            vertices.push_back(v);
        }
    }
    return vertices;
}

Indices:

vector<WORD> Terrain::generateIndices(int width, int height) {
    vector<WORD> indices;
    for (int r = 0; r < width; ++r) {
        for (int c = 0; c < height; ++c) {
            indices.push_back(r * height + c);
            indices.push_back(r * height + (c + 1));
            indices.push_back((r + 1)*height + c);

            indices.push_back((r + 1) * height + c);
            indices.push_back((r * height + (c + 1)));
            indices.push_back((r + 1) * height + (c + 1));
        }
    }
    return indices;
}

Outcome: enter image description here

When I draw a small grid, for example, 10x10, it comes out exactly how it should

enter image description here

When I draw a larger grid, 512x512 to cover my terrain, that's when the issues occur with the indices/vertices

Edit: I believe I have found the problem, but I'm not sure how to solve it. I can draw a grid up to 256x256, but anything over that will give me these graphical issues.

Upvotes: 1

Views: 1629

Answers (1)

WORD is a 16-bit unsigned integer with a maximum value of 65535. With a 300x300 grid you have 90,000 vertices, so you can't index them all with a WORD.

Upvotes: 3

Related Questions