loading meshes

Started by
7 comments, last by macmoy 15 years, 9 months ago
any model would do.. i just wanna ask if the vertices must be saved inside vertexbuffer?? u mean, include that inside it?? or you just put 3 vertices inside vertexbuffer then change the value of each vertex every loop.. waaaaaa!! im confused..
Advertisement

Me too [smile]

First of all it would be useful to know which API you're using (DirectX, XNA, SlimDX...). All of these have functions to load a model into vertex-/indexbuffer pair, so yes vertices are typically saved in a vertexbuffer. This helps managing the memory the vertices use and facilitates copying them to video memory, which in turn helps performance.

For 3 or any low number of vertices, you can also use Draw functions that will upload them to videomemory each frame. This is typically less efficient, but for vertices that change every frame (so the vertex data *really* changes, to move/rotate/scale them you should use tranformation matrices), it saves you the overhead of the vertexbuffer and can actually work out better, not to mention it being more convenient. In case you feel like Googling, that function is called Drawprimitiveup in DirectX.

Hope this unconfuses you a bit [wink]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
im using directx9 as my API

i'd made an OBJ loader just right now..

i inserted all the vertices inside vertexbuffer.. my problem is, i use DrawPrimitive();

i cant manage to choose the vertex to use according to the faces needed.

e.g.

there are 8 vertices for a box

12 faces.


i inserted 8 vertices.. but i cant choose 3 vertices to use to form a face(from the 12 faces).

so what i did is:

12*3 = 36;
i inserted 36 vertices inside the vertexbuffer..

every 3 vertices forms 1 face.

so the downside is im wasting memory for repeated vertices.

how to solve this, i think DrawPrimitive is not suited for this job because it automatically get 3 consecutive vertices.


thanks for you replies!!
You can use DrawIndexedPrimitive.

In addition to the vertexbuffer, you also need an index buffer.

Each set of 3 WORDs in the index buffer specify the vertices to use to draw a face.

If the first 4 vertices in the vertex buffer are the 4 vertices on one face of the cube (arranged clockwise), the first 2 faces specified in the index buffer are:

0,1,2 // specifying the vertices in CW order
2,3,0

Then set your stream source to the vertex buffer, the index buffer to the index buffer you filled, and call DrawIndexedPrimitive().

As you pick vertex indices to form faces, make sure you specify them in CW order!

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

wow!! i get it! so the vertexbuffer stores vertices and indexbuffer stores indices. the problem is,,how to create indexbuffer? i only know how to use vertexbuffer..


thanks!!
is there a structure for index buffer?

like vertex buffer.e.g.

struct myVer{    float x,y,z;    DWORD color;};


because,,the result is not what im expecting.. it must be a cube.
there will be 12 face. per face,it will have 3 indices.. so 12*3 is 36.

d3ddev->CreateVertexBuffer(sizeof(sVer)*numver,0,MYFVF,D3DPOOL_MANAGED,&d3dbuffer,0);	d3ddev->CreateIndexBuffer(sizeof(int)*numface*3,D3DUSAGE_SOFTWAREPROCESSING,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&d3dindex,0);	void *p;	d3dbuffer->Lock(0,0,&p,0);		memcpy(p,point,sizeof(sVer)*numver);		pPoint=(sVer*)p;	d3dbuffer->Unlock();	void *p2;	d3dindex->Lock(0,0,&p2,0);		memcpy(p2,ind,sizeof(int)*numface*3);	d3dindex->Unlock();


what's wrong?

[Edited by - macmoy on July 19, 2008 12:21:33 AM]
I Got it!!! the ind data type must be WORD,,not INT
You're creating a 16-bit index buffer but then copying ints into it ... You'll either want to use shorts for your indices or create a 32-bit index buffer :)
ah!!! now i understand!! THANK YOU SO MUCH teutonicus!!!!

This topic is closed to new replies.

Advertisement