Jump to content
  • Advertisement
Sign in to follow this  
Toastmastern

Issues rendering

This topic is 679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 0
AddTriangle(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;
 
The render code
 

 

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 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 - 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

Share this post


Link to post
Share on other sites
Advertisement
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer);
if (FAILED(result))
{
return false;
}

... ... ... ... 

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;
 

Seems like you're creating the vertex buffer with an uninitialized vertexBufferDesc, which you fill in afterwards!

Share this post


Link to post
Share on other sites
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer);
if (FAILED(result))
{
return false;
}

... ... ... ... 

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;
 

Seems like you're creating the vertex buffer with an uninitialized vertexBufferDesc, which you fill in afterwards!

 

 

Indeed an error, fixed now but no luck with getting the triangles to render. 

 

Here is the structs in the TerrainClass.h that is used in TerrainClass.cpp which I posted earlier:

 

 

struct VertexType

{
XMFLOAT3 position;
XMFLOAT4 color;
};
 
struct MeshGeometry3D
{
vector<VertexType> vertices;
vector<unsigned long> indices;
int mVertexCount;
};
Edited by Toastmastern

Share this post


Link to post
Share on other sites

The issue turned out to be a 0 that should have been a "i" in the for-loop, so simple yet so hard to find. There went a few hours of work :)

 

The final code for anyone that might stumble across this thread in the future:

 

 

D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;

D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
XMFLOAT4 color;
 
mIndex = 0;
 
color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
 
float t = (float)((1.0f + sqrt(5.0f)) / 2.0f);
 
AddVertex(VertexType(-1.0f, t, 0.0f, color));
AddVertex(VertexType(1.0f, t, 0.0f, color));
AddVertex(VertexType(-1.0f, -t, 0.0f, color));
AddVertex(VertexType(1.0f, -t, 0.0f, color));
AddVertex(VertexType(0.0f, -1.0f, t, color));
AddVertex(VertexType(0.0f, 1.0f, t, color));
AddVertex(VertexType(0.0f, -1.0f, -t, color));
AddVertex(VertexType(0.0f, 1.0f, -t, color));
AddVertex(VertexType(t, 0.0f, -1.0f, color));
AddVertex(VertexType(t, 0.0f, 1.0f, color));
AddVertex(VertexType(-t, 0.0f, -1.0f, color));
AddVertex(VertexType(-t, 0.0f, 1.0f, color));
 
// 5 faces around vertex 0
AddTriangle(0, 11, 5);
AddTriangle(0, 5, 1);
AddTriangle(0, 1, 7);
AddTriangle(0, 7, 10);
AddTriangle(0, 10, 11);
 
// 5 adjacent faces
AddTriangle(1, 5, 9);
AddTriangle(5, 11, 4);
AddTriangle(11, 10, 2);
AddTriangle(10, 7, 6);
AddTriangle(7, 1, 8);
 
// 5 faces around point 3
AddTriangle(3, 9, 4);
AddTriangle(3, 4, 2);
AddTriangle(3, 2, 6);
AddTriangle(3, 6, 8);
AddTriangle(3, 8, 9);
 
// 5 adjacent faces
AddTriangle(4, 9, 5);
AddTriangle(2, 4, 11);
AddTriangle(6, 2, 10);
AddTriangle(8, 6, 7);
AddTriangle(9, 8, 1);
 
for (int i = 0; i < 5; i++) 
{
vector<TrianglesIndices> mTrianglesTemp;
 
for (trianglesIT = mTriangles.begin(); trianglesIT != mTriangles.end(); ++trianglesIT)
{
int a = GetMiddlePoint((*trianglesIT).vertexIndex1, (*trianglesIT).vertexIndex2);
int b = GetMiddlePoint((*trianglesIT).vertexIndex2, (*trianglesIT).vertexIndex3);
int c = GetMiddlePoint((*trianglesIT).vertexIndex3, (*trianglesIT).vertexIndex1);
 
mTrianglesTemp.push_back(TrianglesIndices((*trianglesIT).vertexIndex1, a, c));
mTrianglesTemp.push_back(TrianglesIndices((*trianglesIT).vertexIndex2, b, a));
mTrianglesTemp.push_back(TrianglesIndices((*trianglesIT).vertexIndex3, c, b));
mTrianglesTemp.push_back(TrianglesIndices(a, b, c));
}
 
mTriangles = mTrianglesTemp;
}
 
for (trianglesIT = mTriangles.begin(); trianglesIT != mTriangles.end(); ++trianglesIT)
{
mPlanetMesh.indices.push_back((*trianglesIT).vertexIndex1);
mPlanetMesh.indices.push_back((*trianglesIT).vertexIndex2);
mPlanetMesh.indices.push_back((*trianglesIT).vertexIndex3);
}
 
mIndexCount = mPlanetMesh.indices.size();
 
//Scale vertices
for (int i = 0; i < mPlanetMesh.vertices.size(); i++)
{
mPlanetMesh.vertices.position.x = mPlanetMesh.vertices.position.x * 150000.0f;
mPlanetMesh.vertices.position.y = mPlanetMesh.vertices.position.y * 150000.0f;
mPlanetMesh.vertices.position.z = mPlanetMesh.vertices.position.z * 150000.0f;
}
 
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 = &mPlanetMesh.vertices[0];
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) * mPlanetMesh.indices.size();
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
 
indexData.pSysMem = &mPlanetMesh.indices[0];
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
 
result = device->CreateBuffer(&indexBufferDesc, &indexData, &mIndexBuffer);
if (FAILED(result))
{
return false;
}
 
return true;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!