std::vector<CUSTOMVERTEX>vertices(totalVerts);
std::vector<short>indices(totalIndices);
There's nothing immediately wrong-looking here to me. If you're saying that the version without vectors works and that doesn't, there's something else going on in code that you're not showing.
One thing possibly worth noting with vectors - there's a difference between _size_ and _capacity_. Those constructors are setting a _size_. You indicate that's what is desired, but you didn't share the code that would validate it's correct.
same filling algorithm, except using ".at()"
Don't use .at(). Like, ever. It's just a slower version of [] that throws exceptions on out-of-bounds access. All that using .at() accomplishes is making all of your access slower (due to the bounds checks) and shoves C++ exceptions into your code (and you might prefer compiling with exceptions disabled).
Also, don't do this:
memcpy(pVoid, &indices[0], sizeof(short) * indices.size());
That has two problems. First, what happens if you change the type of indices? You'll possibly forget to update that sizeof(short). Use sizeof(indices[0]) or the like instead. Second don't use the &indices[0] idiom, as that can trigger problems for zero-sized vectors (the sizeof is okay because it's an unevaluated context). Use the indices.data() member instead, which is a fast and simple equivalent to (!indices.empty() ? &indices[0] : nullptr). This applies to the vertices vector and all other vectors too, of course.
I do see you using both variables like totalVerts and the .size() access in your buffer setup, and it's not clear why. That could be part of the problem if you're mixing those elsewhere where it matters.
Alright, first of all, I really appreciate your taking your time to help. Here's the overview.
My "Terrain" class has the sole responsibility of generating the triangle-based terrain in an x,y arrangement so that every "cell" (x2 triangles) can be indexed for terrain-collision, shadows, etc. In order to generate the terrain size during runtime, I can't use a "set-at-compile-time" array; it has to be done dynamically using passed parameters.
First, the Vertex list is generated, followed by the Indice list, which sets the triangles. This works just fine.
As is the custom, both the Vertex, then the Indice buffers are filled with those lists.
To make sure the generating algorithm works, at first I used fixed-arrays. Worked just fine. Then I went on to use something that could be dynamically set during runtime, so that the "size" of the terrain (in length, width, and cells per dimension) could be set.
After some help (see a few posts back) I managed to get the dynamic-array ("new") to work; to be honest, I wasn't comfortable with this either. Here's the full "Terrain member-function" as it stands, with an attempt at VECTOR:
<<CLASS PRIVATE VARIABLES>>
LPDIRECT3DVERTEXBUFFER9 terrainV_buffer = NULL;
LPDIRECT3DINDEXBUFFER9 terrainI_buffer = NULL;
D3DXVECTOR3 minBounds = D3DXVECTOR3(-16.0f, 0.0f, -16.0f);
D3DXVECTOR3 maxBounds = D3DXVECTOR3(16.0f, 0.0f, 16.0f);
int sumVerts;
int totalVerts;
int totalPolys;
int totalIndices;
VOID* pVoid;
<<In CLASS DEFINITION>>
void Terrain::InitTerrain()
{
int numCellsWide{ 128 };
int numCellsHigh{ 128 };
int numVertsX = numCellsWide + 1;
int numVertsZ = numCellsHigh + 1;
float stepX = (maxBounds.x - minBounds.x) / numCellsWide;
float stepZ = (maxBounds.z - minBounds.z) / numCellsHigh;
totalVerts = numVertsX * numVertsZ;
totalPolys = numCellsHigh * (numCellsWide * 2);
totalIndices = 6 * (numCellsWide * numCellsHigh);
std::vector<CUSTOMVERTEX>vertices(totalVerts);
std::vector<short>indices(totalIndices);
D3DXVECTOR3 pos = D3DXVECTOR3(minBounds.x, 0.0f, minBounds.z);
int count = 0;
for (int z = 0; z < numVertsZ; z++)
{
pos.x = minBounds.x;
for (int x = 0; x < numVertsX; x++)
{
vertices.at(x).pos = pos;
vertices.at(x).nor = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
pos.x += stepX;
count++;
}
pos.z += stepZ;
}
count = 0;
int vIndex = 0;
for (int z = 0; z < numCellsHigh; z++)
{
for (int x = 0; x < numCellsWide; x++)
{
indices.at(count++) = vIndex;
indices.at(count++) = vIndex + numVertsX;
indices.at(count++) = vIndex + numVertsX + 1;
indices.at(count++) = vIndex;
indices.at(count++) = vIndex + numVertsX + 1;
indices.at(count++) = vIndex + 1;
vIndex++;
}
vIndex++;
}
d3ddev->CreateVertexBuffer(totalVerts * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &terrainV_buffer, NULL);
terrainV_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, &vertices[0], sizeof(CUSTOMVERTEX) * vertices.size());
terrainV_buffer->Unlock();
d3ddev->CreateIndexBuffer(totalIndices * sizeof(short), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &terrainI_buffer, NULL);
terrainI_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, &indices[0], sizeof(short) * indices.size());
terrainI_buffer->Unlock();
}
I'm at a loss as to how to implement the vector properly here, specifically as to the buffer-loading calls. As usual, the answer is probably right in front of me, but I can't quite figure this one out.
As for the different types of indices, I'm not exactly sure what you mean. (short) works, and I can't see a reason to change it.
Any further help is more than appreciated. Cheers!
[ETA] edited for spelling.