Since my car has been broken for the last two days, I’ve taken off work and have been working on my Masters degree, since part of my Masters involves building a small “game engine” for AI testing, I’ve been doing some more DX10 work, so its convenient for me to quickly slap together a few more tutorials.
I covered the basics of indexed buffers and the depth testing in the last tutorial, in this short tut, I’m going to cover the basics of directX meshes. A mesh is a data structure that contains all the vertex and index buffers needed to draw an object. It’s a neater method of drawing objects as we’ll see.
Mesh Basics
There are four steps to using meshes:
- Create the mesh
- Fill the Mesh with the index and vertex data necessary
- Commit the mesh to the device
- Draw the mesh
So let’s create a new mesh, first things first, we’ll define an ID3DX10Mesh* pointer called pMesh.
<pre>//create mesh if ( FAILED( D3DX10CreateMesh( pD3DDevice, vertexInputLayout, 2, "POSITION", 8, 12, D3DX10_MESH_32_BIT, &pMesh) ) ) return fatalError("Could not create mesh!"); //vertices for a cube vertex v[8]; v[0] = vertex( D3DXVECTOR3(-1,1,-1), D3DXVECTOR4(1,0,0,1) ); //front top left v[1] = vertex( D3DXVECTOR3(1,1,-1), D3DXVECTOR4(0,1,0,1) ); //front top right v[2] = vertex( D3DXVECTOR3(-1,-1,-1), D3DXVECTOR4(0,0,1,1) ); //front bottom left v[3] = vertex( D3DXVECTOR3(1,-1,-1), D3DXVECTOR4(1,1,0,1) ); //front bottom right v[4] = vertex( D3DXVECTOR3(-1,1,1), D3DXVECTOR4(1,0,0,1) ); //back top left v[5] = vertex( D3DXVECTOR3(1,1,1), D3DXVECTOR4(0,1,0,1) ); //back top right v[6] = vertex( D3DXVECTOR3(-1,-1,1), D3DXVECTOR4(0,0,1,1) ); //back bottom left v[7] = vertex( D3DXVECTOR3(1,-1,1), D3DXVECTOR4(1,1,0,1) ); //back bottom right //create indexes for a cube unsigned int i[36] = { 2,0,3,3,1,0, 3,1,7,7,5,1, 6,4,2,2,0,4, 7,5,6,6,4,5, 0,4,1,1,5,4, 6,2,7,7,3,2 }; //insert data into mesh and commit changes pMesh->SetVertexData(0, v); pMesh->SetIndexData(i, 36); pMesh->CommitToDevice();
Now we use D3DX10CreateMesh to create the mesh, the parameters are the d3d device, the vertex input layout, number of elements in the vertex layout, the name of the element that stores the vertex position, number of vertices, number of faces, mesh flag and finally the output mesh pointer.
There is a little trick here, remember how before we had triangle strips? Here we don’t, we have to specify every single triangle (referred to as a face in the mesh) by hand. So our index list looks a bit different from before.
To add the vertex data and index data, we simply use the mesh SET methods, a mesh can have multiple vertex buffers and so when you set a vertex buffer, you need to specify in which slot you wish to store it. The index data SET method simply takes the index array and the number of indexes in it.
The final step is to commit the mesh, every time you make a change to a mesh you need to commit it to the device before the changes will be taken into effect.
Drawing the Mesh
So how do we draw a mesh?
for( UINT p = 0; p < techDesc.Passes; p++ ) { //apply technique pBasicTechnique->GetPassByIndex( p )->Apply( 0 ); pMesh->DrawSubset(0); }
How simple is that? We use the draw subset method of the mesh interface to draw the mesh, the value given to the method specifies with attribute group to draw, attribute groups for meshes will be covered in a later tutorial, for now just specify 0. And that’s how to create and draw a basic mesh.
I’ve updated the draw code to render a bunch of spinning cubes just for fun:
Source Code
Source code: GitHub
First blog I read after wakeup from sleep today!
—————————-
Are you tension? panic?
Hi, thanks for your tutorials, they are really cool.
Coming from OpenGL where drawing a quad is simple as calling the function glRectf, i have a simple question for DirectX. What is a shortest way to draw :
– a quad on the screen?
– multiple quads on the screen?
Thanks!
there is no “easy” way, to draw a quad you need to draw two triangles, just like in tutorial 2.
for multiple quads look at tutorial 4, in directx 10 there are no easy helper functions like in openGL, its more of a developer API and not a hobbyist one. the glRectf function draws two triangles, so in dx10 you have to do that step yourself.
I’m following your tutorials and I’ve to say I’m learning DirectX thru’ ’em, thanks!
Well, I couldn’t stop myself after reading your comment, but to reply. I guess even Open GL isn’t a hobbyist library, fixed function pipeline was the initial way of doing things (like DX), but even Open GL is completely shader based now and many production level world applications/games are written using Open GL like Left 4 Dead 2 (http://blogs.valvesoftware.com/linux/faster-zombies/), I don’t know what you meant by developer API, since all the games we see on platforms other than Windows (Mac, iOS, Android, Linux, ) are written on Open GL.
Btw, I’m learning both Open GL and DirectX, so I’m not taking sides here, just pointing out what’s factual.
Ok, this is good to know. Thanks Bobby!
Hey, try out my mesh class, includes both exporting and loading.
you can find it at
http://www.rawgengine.darkbb.com
please stop by and give me improvments tips as well as new ideas.
Bobby i think this questions asked before but how do i load x model without DXUT ?
can you provide sample loader for us ? or any suggestion .
Thanks !
there is no easy way to load X models in DX10, you will need to write your own model loader. That’s why I’m currently working on a collada loader, to allow guys to easily be able to load in models.
if you want to just load in static models without animation, the OBJ format is very simple and easy to load in!
Hi Bobby,
Just wanted to say thanks for all these nice tutorials. !!Cheers!!
Stupid commentators, funny comments, lol
Stupid “professional”, funny comment, lol
Hi Bobby, fantastic site and tutorials, really helping me learn DX10 for my uni course. One thing I noticed when trying to apply texturing to a square created using your mesh method, was that if you add another variable to the ‘vertex’ structure, the graphics go very strange. I was wondering how you get around this? Thanks for answering!
when you add another variable to the vertex structure, you also need to modify the input layout accordingly, as well as change you vertex shader input structure as well…
the input layout on the code side informs the API of the memory layout of a single vertex, so that the api can split the big chunk of memory into the seperated vertices once it gets to the vertex shader.
Thanks for the reply. I did change the input layout, it looks like this:
const D3D10_INPUT_ELEMENT_DESC vertexInputLayout[] =
{
{ “POSITION”, 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ “COLOR”, 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ “TEXTURE”, 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0 }
};
Trouble is I don’t know what to change or what to change them to…
well what does your vertex structure look like in your c++ code and your HLSL code? the two definitions need to be the same and the input layout just describes the data boundaries and the data types stored within a single vertex.
Well I’m basically copying code from your 3rd tutorial on textures. I pasted my code over here: http://www.gamedev.net/community/forums/topic.asp?topic_id=589523&whichpage=1�
Would be fantastic if you could check it out, really need to get texturing working for this project. Really appreciate your help.
The TNM Staging System is one of the most commonly used staging systems. This system was developed and is maintained by the American Joint Committee on Cancer (AJCC) and the International Union Against Cancer (UICC). The TNM clstiaficasion system was developed as a tool for doctors to stage different types of cancer based on certain standard criteria.
tenho 24anos desde os mesus 15 anos sou apaixonado por HONDA-CIVIC porisso hohe trabalho pra compra esse carro qe e´o meu sonho de possuir
There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article thanks and we want more! Added to FeedBurner as well
This has to be the most vague, universal, and English-raped comment in the history of blog comments.