• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
VitaliBR

Assimp and Direct3D 9

6 posts in this topic

Hi guys!

I'm trying to render a model obj uploaded by assimp.
I doing as follows:

variables
[CODE]LPDIRECT3DVERTEXBUFFER9 m_pVB;
LPDIRECT3DINDEXBUFFER9 m_pIB;
UINT numVertices;
UINT numFaces;
UINT inx_vertex;
UINT inx_faces;
UINT startIndex;
VERTEX *vertices;
USHORT *indices;[/CODE]

load the model
[CODE]
#define MeshFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)


// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_CalcTangentSpace |
aiProcess_JoinIdenticalVertices |
aiProcess_GenSmoothNormals |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_OptimizeMeshes);

// If the import failed, report it
if( !scene)
{
std::string s = "Error in load the " + pFile;
MessageBoxA (NULL, s.c_str(), "ERROR", MB_OK);
return false;
}
aiMesh** meshes = scene->mMeshes;
numVertices = 0;
numFaces = 0;
inx_vertex = 0;
inx_faces = 0;
startIndex = 0;

for(int nMeshes = 0; nMeshes < scene->mNumMeshes; nMeshes++)
{
numVertices += meshes[nMeshes]->mNumVertices;
numFaces += meshes[nMeshes]->mNumFaces;
}
vertices = new VERTEX[numVertices];
indices = new USHORT[numFaces*3];
for(int nMeshes = 0; nMeshes < scene->mNumMeshes; nMeshes++)
{
startIndex = inx_vertex;
for(int nVertex = 0; nVertex < meshes[nMeshes]->mNumVertices; nVertex++)
{
vertices[inx_vertex].X = meshes[nMeshes]->mVertices[nVertex].x;
vertices[inx_vertex].Y = meshes[nMeshes]->mVertices[nVertex].y;
vertices[inx_vertex].Z = meshes[nMeshes]->mVertices[nVertex].z;
vertices[inx_vertex].Nx = meshes[nMeshes]->mNormals[nVertex].x;
vertices[inx_vertex].Ny = meshes[nMeshes]->mNormals[nVertex].y;
vertices[inx_vertex].Nz = meshes[nMeshes]->mNormals[nVertex].z;
vertices[inx_vertex].Color = D3DCOLOR_XRGB(255,255,255);
inx_vertex++;
}
for(int nFaces = 0; nFaces < meshes[nMeshes]->mNumFaces; nFaces++)
{
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[0] + startIndex;
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[1] + startIndex;
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[2] + startIndex;
}
}

// create a vertex buffer interface called v_buffer
devices.d3ddev->CreateVertexBuffer(8*sizeof(SDX_CUSTOMVERTEX),
0,
MeshFVF,
D3DPOOL_MANAGED,
&m_pVB,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
m_pVB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, 8*sizeof(SDX_CUSTOMVERTEX));
m_pVB->Unlock();
// create an index buffer interface called i_buffer
devices.d3ddev->CreateIndexBuffer(36*sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pIB,
NULL);
// lock i_buffer and load the indices into it
m_pIB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
m_pIB->Unlock();
[/CODE]

render the model
[CODE]devices.d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);
devices.d3ddev->SetFVF(MeshFVF);
devices.d3ddev->SetStreamSource(0, m_pVB, 0, sizeof(VERTEX));
devices.d3ddev->SetIndices(m_pIB);
devices.d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVertices, 0, numFaces);
devices.d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);[/CODE]

But the model is rendered completely wrong, is formless, very bizarre.
Where am I wrong? makes three days I have been analyzing and trying to find the error. :(
0

Share this post


Link to post
Share on other sites
About that memcpy call for the indices, it looks like you are passing the size of the pointer (which will be 4 or 8) rather than the size of the array. Edited by beans222
2

Share this post


Link to post
Share on other sites
[quote name='beans222' timestamp='1343446266' post='4963849']
About that memcpy call for the indices, it looks like you are passing the size of the pointer (which will be 4 or 8) rather than the size of the array.
[/quote]
I changed the memcpy (I think)

But now it shows nothing on the screen. :(

Loader:
[CODE]// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_CalcTangentSpace |
aiProcess_JoinIdenticalVertices |
aiProcess_GenSmoothNormals |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_OptimizeMeshes);

// If the import failed, report it
if( !scene)
{
std::string s = "Error in load the " + pFile;
MessageBoxA (NULL, s.c_str(), "Error mesh", MB_OK);
return false;
}
aiMesh** meshes = scene->mMeshes;
numVertices = 0;
numFaces = 0;
inx_vertex = 0;
inx_faces = 0;
startIndex = 0;

for(int nMeshes = 0; nMeshes < scene->mNumMeshes; nMeshes++)
{
numVertices += meshes[nMeshes]->mNumVertices;
numFaces += meshes[nMeshes]->mNumFaces;
}
vertices = new VERTEX[numVertices];
indices = new USHORT[numFaces*3];
for(int nMeshes = 0; nMeshes < scene->mNumMeshes; nMeshes++)
{
startIndex = inx_vertex;
for(int nVertex = 0; nVertex < meshes[nMeshes]->mNumVertices; nVertex++)
{
vertices[inx_vertex].X = meshes[nMeshes]->mVertices[nVertex].x;
vertices[inx_vertex].Y = meshes[nMeshes]->mVertices[nVertex].y;
vertices[inx_vertex].Z = meshes[nMeshes]->mVertices[nVertex].z;
vertices[inx_vertex].Nx = meshes[nMeshes]->mNormals[nVertex].x;
vertices[inx_vertex].Ny = meshes[nMeshes]->mNormals[nVertex].y;
vertices[inx_vertex].Nz = meshes[nMeshes]->mNormals[nVertex].z;
vertices[inx_vertex].Color = D3DCOLOR_XRGB(255,255,255);
inx_vertex++;
}
for(int nFaces = 0; nFaces < meshes[nMeshes]->mNumFaces; nFaces++)
{
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[0] + startIndex;
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[1] + startIndex;
indices[inx_faces++] = meshes[nMeshes]->mFaces[nFaces].mIndices[2] + startIndex;
}
}

// create a vertex buffer interface called v_buffer
if(FAILED(devices.d3ddev->CreateVertexBuffer(numVertices * sizeof(SDX_CUSTOMVERTEX),
0,
MeshFVF,
D3DPOOL_MANAGED,
&m_pVB,
NULL)))
{
MessageBoxA(NULL, "Error in CreateVertexBuffer", "Error mesh", MB_OK | MB_ICONEXCLAMATION);
return false;
}
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
m_pVB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(SDX_CUSTOMVERTEX));
m_pVB->Unlock();
// create an index buffer interface called i_buffer
if(FAILED(devices.d3ddev->CreateIndexBuffer(numFaces * 3 * sizeof(unsigned int),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_pIB,
NULL)))
{
MessageBoxA(NULL, "Error in CreateIndexBuffer", "Error mesh", MB_OK | MB_ICONEXCLAMATION);
return false;
}
// lock i_buffer and load the indices into it
m_pIB->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(unsigned int));
m_pIB->Unlock();
return true;[/CODE]

Render:
[CODE]devices.d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);
devices.d3ddev->SetFVF(MeshFVF);
devices.d3ddev->SetStreamSource(0, m_pVB, 0, sizeof(VERTEX));
devices.d3ddev->SetIndices(m_pIB);
if(FAILED(devices.d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numVertices, 0, numFaces)))
{
MessageBoxA(NULL, "Error in render model", "Error mesh", MB_OK | MB_ICONEXCLAMATION);
return;
}
devices.d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
devices.d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);[/CODE]
0

Share this post


Link to post
Share on other sites
The final argument to memcpy should be the same as the length passed to create the buffer (as it's the total bytes to copy), and the sizeof in the index buffer one should be short, not int. ("numFaces * 3 * sizeof(short)" "numVertices * sizeof(SDX_CUSTOMVERTEX)"). Is type VERTEX (the type of array 'vertices') the same thing as SDX_CUSTOMVERTEX?

I think there's also a problem with the counters in the code that gets the data from Assimp. The index counter is based on the number of vertices which will almost never match the number of vertices. It would be better to use completely separate counters for vertex and index data and just add the mesh's mNumVertices and mNumFaces*3 to the respective counters at the end of the outer loop.
1

Share this post


Link to post
Share on other sites
[quote name='beans222' timestamp='1343675163' post='4964580']
The final argument to memcpy should be the same as the length passed to create the buffer (as it's the total bytes to copy), and the sizeof in the index buffer one should be short, not int. ("numFaces * 3 * sizeof(short)" "numVertices * sizeof(SDX_CUSTOMVERTEX)"). Is type VERTEX (the type of array 'vertices') the same thing as SDX_CUSTOMVERTEX?

I think there's also a problem with the counters in the code that gets the data from Assimp. The index counter is based on the number of vertices which will almost never match the number of vertices. It would be better to use completely separate counters for vertex and index data and just add the mesh's mNumVertices and mNumFaces*3 to the respective counters at the end of the outer loop.
[/quote]
yes, the SDX_CUSTOMVERTEX and VERTEX is the same, VERTEX is my old structure.

I tried to redo the structure was as follows:
[CODE]// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_FlipUVs);
/*
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_CalcTangentSpace |
aiProcess_JoinIdenticalVertices |
aiProcess_GenSmoothNormals |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_OptimizeMeshes);
*/

// If the import failed, report it
if( !scene)
{
std::string s = "Error in load the " + pFile;
MessageBoxA(NULL, s.c_str(), "Error mesh", MB_OK);
return false;
}
//retrieves the meshes from the main scene;
aiMesh** meshes = scene->mMeshes;
aiMaterial** materials = scene->mMaterials;
aiNode* sceneRoot = scene->mRootNode;
aiNode** children = sceneRoot->mChildren;
aiNode* child;
UINT* meshID;
UINT materialID;
UINT numVertices = 0;
UINT numTriangles = 0;
Vertex* vertices;
DWORD* indices;
UINT* attributeBuffer;
UINT i = 0;
bool rootNode = true;
while(i < sceneRoot->mNumChildren)
{
if(rootNode)
{
child = sceneRoot;
rootNode = false;
} else
{
child = children[i];
i++;
}
if(!(child->mNumMeshes > 0))
continue;
meshID = child->mMeshes;
for(UINT x = 0; x < child->mNumMeshes; x++)
{
numVertices += meshes[meshID[x]]->mNumVertices;
numTriangles += meshes[meshID[x]]->mNumFaces;
}
vertices = new Vertex[numVertices];
indices = new DWORD[numTriangles*3];
attributeBuffer = new UINT[numTriangles];
UINT nextVertex = 0;
UINT nextIndex = 0;
for(UINT x = 0; x < child->mNumMeshes; x++)
{
UINT meshStartIndex = nextVertex;
for(UINT j = 0; j < meshes[meshID[x]]->mNumVertices; j++)
{
vertices[nextVertex].pos = aiVec3ToSDXVector3(meshes[meshID[x]]->mVertices[j]);
vertices[nextVertex].tangent = aiVec3ToSDXVector3(meshes[meshID[x]]->mTangents[j]);
vertices[nextVertex].normal = aiVec3ToSDXVector3(meshes[meshID[x]]->mNormals[j]);
vertices[nextVertex].texC = aiVec3ToSDXVector2(meshes[meshID[x]]->mTextureCoords[0][j]);
nextVertex++;
}
for(UINT j = 0; j < meshes[meshID[x]]->mNumFaces; j++)
{
UINT n = 0;
if(x != 0)
{
n = j + meshes[meshID[x-1]]->mNumFaces;
} else
{
n = j;
}
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[0] + meshStartIndex;
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[1] + meshStartIndex;
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[2] + meshStartIndex;
attributeBuffer[n] = meshes[meshID[x]]->mMaterialIndex;
}
}
}
return true;[/CODE]

but I'm still trying to find a way to create the mesh, I could use the D3DXCreateMesh?
0

Share this post


Link to post
Share on other sites
I tried this (with the help of the assimp view source):
http://pastebin.com/vz51v8E9

helpers:
http://pastebin.com/aiuzbAGa

and render model:
http://pastebin.com/M2fdH36T

but does not render anything on the screen :(

[size=4][b]But the strange thing is that my [/b][/size]
[size=4][b]// get the number of vertices/faces in the model
unsigned int iNumVert = 0;
unsigned int iNumFaces = 0;
for (unsigned int i = 0; i < scene->mNumMeshes;++i)
{
iNumVert += scene->mMeshes[i]->mNumVertices;
iNumFaces += scene->mMeshes[i]->mNumFaces;[/b][/size]
[size=4][b]}[/b][/size]

[size=4][b]is [/b][/size]

[size=4][b]iNumVert = 1217[/b][/size]
[size=4][b]iNumFaces = 1368[/b][/size]

[size=4][b]and the same model in assimp view is [/b][/size]
[size=4][b]iNumVert = 1284
iNumFaces = 1340[/b][/size]
[img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

[size=4][b]I'm using the model [/b][/size][b]Assimp/test/models/OBJ/spider.obj[/b]
0

Share this post


Link to post
Share on other sites
I did it! Missing only the textures [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by VitaliBR
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0