Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- VertexType* vertices;
- unsigned long* indices;
- int index, i, j;
- float positionX, positionZ, u, v, increment;
- D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
- D3D11_SUBRESOURCE_DATA vertexData, indexData;
- // Calculate the number of vertices in the terrain mesh.
- vertexCount = (resolution - 1) * (resolution - 1) * 6;
- indexCount = vertexCount;
- vertices = new VertexType[vertexCount];
- indices = new unsigned long[indexCount];
- index = 0;
- // UV coords.
- u = 0;
- v = 0;
- increment = 1.0f / (resolution - 1);
- for (j = 0; j < (resolution - 1); j++)
- {
- for (i = 0; i < (resolution - 1); i++)
- {
- // Upper right.
- positionX = (float)(i + 1);
- positionZ = (float)(j + 1);
- vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
- vertices[index].texture = XMFLOAT2(u + increment, v + increment);
- vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
- indices[index] = index;
- index++;
- // lower right
- positionX = (float)(i);
- positionZ = (float)(j + 1);
- vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
- vertices[index].texture = XMFLOAT2(u, v + increment);
- vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
- indices[index] = index;
- index++;
- // Bottom left
- positionX = (float)(i);
- positionZ = (float)(j);
- vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
- vertices[index].texture = XMFLOAT2(u, v);
- vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
- indices[index] = index;
- index++;
- // Upper left.
- positionX = (float)(i + 1);
- positionZ = (float)(j);
- vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);
- vertices[index].texture = XMFLOAT2(u + increment, v);
- vertices[index].normal = XMFLOAT3(0.0, 1.0, 0.0);
- indices[index] = index;
- index++;
- u += increment;
- }
- u = 0;
- v += increment;
- }
- // Set up the description of the static vertex buffer.
- vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- vertexBufferDesc.ByteWidth = sizeof(VertexType) * vertexCount;
- vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- vertexBufferDesc.CPUAccessFlags = 0;
- vertexBufferDesc.MiscFlags = 0;
- vertexBufferDesc.StructureByteStride = 0;
- // Give the subresource structure a pointer to the vertex data.
- vertexData.pSysMem = vertices;
- vertexData.SysMemPitch = 0;
- vertexData.SysMemSlicePitch = 0;
- // Now create the vertex buffer.
- device->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer);
- // Set up the description of the static index buffer.
- indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- indexBufferDesc.ByteWidth = sizeof(unsigned long) * indexCount;
- indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
- indexBufferDesc.CPUAccessFlags = 0;
- indexBufferDesc.MiscFlags = 0;
- indexBufferDesc.StructureByteStride = 0;
- // Give the subresource structure a pointer to the index data.
- indexData.pSysMem = indices;
- indexData.SysMemPitch = 0;
- indexData.SysMemSlicePitch = 0;
- // Create the index buffer.
- device->CreateBuffer(&indexBufferDesc, &indexData, &indexBuffer);
- // Release the arrays now that the buffers have been created and loaded.
- delete[] vertices;
- vertices = 0;
- delete[] indices;
- indices = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement