Skip to main content
GameDev.net gamedev.net
🔒 Locked

DX10 - DrawIndexed - problem

Started by dys129 Feb 27, 2009 at 5:08 AM 4 replies 2.5k views
Original Post
dys129
dys129
Hi! I'm writing my own Mesh class for Dx10 and got some problem using DrawIndexed. I wrote exporter from 3d max and in my mesh file I got something like: Submesh 1 NrVerts 340 NrInd 580 Vertex1 -3,0,4 Vertex2 ... Index1 0,1,3 Index2 ... Submesh 2 NrVerts 133 NrInd 220 Vertex1 -2,5,8 ... Index1 0,5,7 ... I'm creating VertexBuffer which holds ALL vertices (from all submeshes) and the same goes with IndexBuffer. And my drawing function looks:

UINT uiStride = sizeof(cVertex);
UINT uiOffset = 0;
Graphics()->GetDevice()->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &uiStride, &uiOffset);
Graphics()->GetDevice()->IASetIndexBuffer(m_pIndexBuffer, DXGI_FORMAT_R32_UINT, m_Submeshes[SubmeshNr].m_uiStartingIndex*sizeof(DWORD));
	Graphics()->GetDevice()->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);	Graphics()->GetDevice()->DrawIndexed(m_Submeshes[SubmeshNr].m_uiNumOfIndices*3,  m_Submeshes[SubmeshNr].m_uiStartingIndex, m_Submeshes[SubmeshNr].m_iBaseVertexLocation);

It's ok if i'm rendering only first submesh but if there are more it looks like this: http://img407.imageshack.us/my.php?image=teapot.jpg And this is how I set all paremeters which I pass to DrawIndexed: m_uiNumOfIndices = (num of faces I parse from file) m_uiStartingIndex = (num of faces of all previous submeshes) m_iBaseVertexLocation = (num of vertices of all previous submeshes) Can anybody tell me what am I doing wrong?
resle
resle
Just a quick idea, take it as nothing but a guess: I think your problem isn't in the way you call DrawIndexed, but rather the way you may have exported mesh data from 3ds max. Give us more info about this, and how you create and fill the index and vertex buffers.
..so we ate rare animals, we spent the night eating rare animals..
dys129
dys129
This is how I get Mesh using MaxScript:

function GetTotalSceneVertices = (	local vertices = 0	for o in geometry do	(		vertices+=o.numverts	)	return vertices)function GetTotalSceneTriangles = (	local triangles = 0	for o in geometry do	(		triangles+=o.numfaces	)	return triangles)WriteLong fstream (GetTotalSceneVertices())		WriteLong fstream (GetTotalSceneTriangles())for obj in geometry(num_verts = getNumVerts objnum_faces = getNumFaces objWriteLong fstream num_vertsWriteLong fstream num_facesfor v = 1 to num_verts do(vert=getVert obj vvnormal=getNormal obj vvnormal = normalize vnormal	WriteFloat fstream vert.xWriteFloat fstream vert.z --flipWriteFloat fstream vert.y)for f = 1 to num_faces do(index = getFace obj fWriteLong fstream (index.x-1)WriteLong fstream (index.y-1)WriteLong fstream (index.z-1)))


Then in my mesh class I open this file, and read all vertices and indices for each submesh and fill its struct:
MeshPart.m_uiNumOfVertices = num_verts; <--read from mesh fileMeshPart.m_uiNumOfIndices = num_faces; <--read from mesh fileMeshPart.m_uiStartingIndex = StartingIndex; MeshPart.m_uiSubmeshNumber = i;MeshPart.m_iBaseVertexLocation = StartingVertex;StartingIndex += num_faces-1;StartingVertex += num_verts;


After reading all submeshes I create VertexBuffer And IndexBuffer:
D3D10_BUFFER_DESC bufferDesc;bufferDesc.Usage = D3D10_USAGE_DEFAULT;bufferDesc.ByteWidth = sizeof(cVertex)*NumOfAllVertices;bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;bufferDesc.CPUAccessFlags = 0;bufferDesc.MiscFlags = 0;D3D10_SUBRESOURCE_DATA InitData;InitData.pSysMem = &m_Verts[0]; <--m_Verts is vector containg all verticesInitData.SysMemPitch = 0;InitData.SysMemSlicePitch = 0;Graphics()->GetDevice()->CreateBuffer(&bufferDesc, &InitData, &m_pVertexBuffer)bufferDesc.ByteWidth = sizeof(DWORD)*NumOfAllIndices*3;bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;InitData.pSysMem = &m_Indices[0];Graphics()->GetDevice()->CreateBuffer(&bufferDesc, &InitData, &m_pIndexBuffer);


If you want to know anything more please ask, because i'm stuck with it :)
ET3D
ET3D
One thing which looks wrong to me is that you're using the face count for your index offset. Shouldn't it be 3 times the number of faces?
dys129
dys129
if I multiply m_Submeshes[SubmeshNr].m_uiStartingIndex by 3 I'm just going out of range of IndexBuffer.
dys129
dys129
Ok finally I figured out what's wrong :)

IASetIndexBuffer(m_pIndexBuffer, DXGI_FORMAT_R32_UINT, m_Submeshes[SubmeshNr].m_uiStartingIndex*sizeof(cIndex));

While setting IndexBuffer 3rd argument should be StartingIndex * offset. I set offset = sizeof(DWORD) but it should be sizeof(cIndex) where

struct cIndex
{
DWORD ind1, ind2, ind3
};

And then DrawIndexed should look like this

DrawIndexed(m_Submeshes[SubmeshNr].m_uiNumOfIndices*3, 0, m_Submeshes[SubmeshNr].m_iBaseVertexLocation);

And now everything works as it suppose to :) I hope it will help someone :)
Thanks for all hints :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.