Hello Gamedev.net I've been lurking these forums for ages and trying to learn from others but now it is time for me to ask my own question.
I'm using C++ together with DirectX11 and have been following Rasterteks guides and I am using the framework he explains in his guides. I got his terrain example to work without any issues. Now the time has come to create something else and go my own path(still using his framework tho).
What I am trying to do is to create a icosphere. I've been using http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html and http://www.gamedev.net/topic/623118-icosphere-generation-code-not-working-properlyhelp/ to implement it in my c++ code. I got the vertices, the indices all is in their correct place but it won't render on the screen. My latest try was to move it to a vertex array instead of a vector of vertices and the situation is the same.
Here comes my code that doesn't work:
XMFLOAT4 color;
unsigned long *indices;VertexType *vertices;D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;D3D11_SUBRESOURCE_DATA vertexData, indexData;HRESULT result;int index;index = 0;color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);float t = (float)((1.0f + sqrt(5.0f)) / 2.0f);VertexType tempVertex;tempVertex.position = XMFLOAT3(-1.0f, t, 0.0f);tempVertex.color = color;AddVertex(tempVertex);tempVertex.position = XMFLOAT3(1.0f, t, 0.0f);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(-1.0f, -t, 0);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(1.0f, -t, 0.0f);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(0.0f, -1.0f, t);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(0.0f, 1.0f, t);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(0.0f, -1.0f, -t);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(0.0f, 1.0f, -t);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(t, 0.0f, -1.0f);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(t, 0.0f, 1.0f);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(-t, 0.0f, -1.0f);AddVertex(tempVertex);tempVertex.position = XMFLOAT3(-t, 0.0f, 1.0f);AddVertex(tempVertex);// 5 faces around vertex 0AddTriangle(0, 11, 5);AddTriangle(0, 5, 1);AddTriangle(0, 1, 7);AddTriangle(0, 7, 10);AddTriangle(0, 10, 11);vertices = new VertexType[mPlanetMesh.vertices.size()];if (!vertices){return false;}for (int i = 0; i < mPlanetMesh.vertices.size(); i++){vertices.position = mPlanetMesh.vertices.position;vertices.color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);}mIndexCount = mPlanetMesh.indices.size();result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer);if (FAILED(result)){return false;}for (trianglesIT = mTriangles.begin(); trianglesIT != mTriangles.end(); ++trianglesIT){mPlanetMesh.indices.push_back((*trianglesIT).vertexIndex3);mPlanetMesh.indices.push_back((*trianglesIT).vertexIndex2);mPlanetMesh.indices.push_back((*trianglesIT).vertexIndex1);}vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;vertexBufferDesc.ByteWidth = sizeof(VertexType) * mPlanetMesh.vertices.size();vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;vertexBufferDesc.CPUAccessFlags = 0;vertexBufferDesc.MiscFlags = 0;vertexBufferDesc.StructureByteStride = 0;vertexData.pSysMem = vertices;// &mPlanetMesh.vertices;vertexData.SysMemPitch = 0;vertexData.SysMemSlicePitch = 0;indices = new unsigned long[mPlanetMesh.indices.size()];if (!indices){return false;}for (int i = 0; i < mPlanetMesh.indices.size(); i++){indices[0] = mPlanetMesh.indices;}indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;indexBufferDesc.ByteWidth = sizeof(unsigned long) * mPlanetMesh.indices.size();indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;indexBufferDesc.CPUAccessFlags = 0;indexBufferDesc.MiscFlags = 0;indexBufferDesc.StructureByteStride = 0;indexData.pSysMem = indices;// &mPlanetMesh.indices;//&mTriangles;indexData.SysMemPitch = 0;indexData.SysMemSlicePitch = 0;result = device->CreateBuffer(&indexBufferDesc, &indexData, &mIndexBuffer);if (FAILED(result)){return false;}delete[] vertices;vertices = 0;delete[] indices;indices = 0;return true;
void TerrainClass::RenderBuffers(ID3D11DeviceContext *deviceContext)
{unsigned int stride;unsigned int offset;stride = sizeof(VertexType);offset = 0;deviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset);deviceContext->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R32_UINT, 0);deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);return;}
The code above is the one that is not rendering correctly.
The code below is the one that is working
VertexType *vertices;
unsigned long *indices;D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;D3D11_SUBRESOURCE_DATA vertexData, indexData;HRESULT result;int i, j, terrainWidth, terrainHeight, index;XMFLOAT4 color;float positionX, positionZ;terrainHeight = 256;terrainWidth = 256;color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);mVertexCount = (terrainWidth - 1) * (terrainHeight - 1) * 8;mIndexCount = mVertexCount;vertices = new VertexType[mVertexCount];if (!vertices){return false;}indices = new unsigned long[mIndexCount];if (!indices){return false;}index = 0;for (j = 0; j < (terrainHeight - 1); j++){for (i = 0; i < (terrainWidth - 1); i++){// Line 1 - Upper left.positionX = (float)i;positionZ = (float)(j + 1);vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;// Line 1 - Upper right.positionX = (float)(i + 1);positionZ = (float)(j + 1);vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;// Line 2 - Upper rightpositionX = (float)(i + 1);positionZ = (float)(j + 1);vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;// Line 2 - Bottom right.positionX = (float)(i + 1);positionZ = (float)j;vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;// Line 3 - Bottom right.positionX = (float)(i + 1);positionZ = (float)j;vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;// Line 3 - Bottom left.positionX = (float)i;positionZ = (float)j;// vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);// vertices[index].color = color;// indices[index] = index;// index++;// Line 4 - Bottom left.positionX = (float)i;positionZ = (float)j;vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;// Line 4 - Upper left.positionX = (float)i;positionZ = (float)(j + 1);vertices[index].position = XMFLOAT3(positionX, 0.0f, positionZ);vertices[index].color = color;indices[index] = index;index++;}}vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;vertexBufferDesc.ByteWidth = sizeof(VertexType) * mVertexCount;vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;vertexBufferDesc.CPUAccessFlags = 0;vertexBufferDesc.MiscFlags = 0;vertexBufferDesc.StructureByteStride = 0;vertexData.pSysMem = vertices;vertexData.SysMemPitch = 0;vertexData.SysMemSlicePitch = 0;result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer);if (FAILED(result)){return false;}indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;indexBufferDesc.ByteWidth = sizeof(unsigned long) * mIndexCount;indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;indexBufferDesc.CPUAccessFlags = 0;indexBufferDesc.MiscFlags = 0;indexBufferDesc.StructureByteStride = 0;indexData.pSysMem = indices;indexData.SysMemPitch = 0;indexData.SysMemSlicePitch = 0;result = device->CreateBuffer(&indexBufferDesc, &indexData, &mIndexBuffer);if (FAILED(result)){return false;}delete[] vertices;vertices = 0;delete[] indices;indices = 0;return true;
and below is the rendering code for the working code(Note that the only difference is the linestrip part in topology)
void TerrainClass::RenderBuffers(ID3D11DeviceContext *deviceContext)
{unsigned int stride;unsigned int offset;stride = sizeof(VertexType);offset = 0;deviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset);deviceContext->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R32_UINT, 0);deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_LINESLIST);return;}
Anyone got any idea what my issue could be?
Thanks in advance
//Toastmastern