Help!How to draw all the objects in only one DrawPrimitive() call?

Started by
3 comments, last by AlanWu 11 years, 4 months ago
Hello,everybody.I think i am new,i am alan,i am from China.I moved to NewZealand one year ago.
But my English still not good.Please don't mind.
I got some questions when i was programming and i don't know how to do,so i just ask some questions.
Some people said if you call DrawPrimitive for every objects it will be very slow.I understood it.
And they said we can draw all the objects in only one DrawPrimitive call.They said put all the object's vertex in a vertex buffer then call SetStreamSource and call DrawPrimitive for only once time.
First,i don't understand how to put all the objects in only one vertex buffer.
Second,i want to draw the texture in the objects,and they have the different texture,if it is only once DrawPrimitive call,how could i set the different textures for every objects?
I don't understand it at all.Could everybody help me please?
Oh,and i am making a 2D render,it doesn't need to be 3D but i hope it can be fast.
Advertisement
Ok, just to make it clear, you can do a high number of draw calls. I would say you can easily do hundreds of draw calls per frame without a problem. So depending on how complex your scene is, you can still do that.

And yes, you can combine your objects into a single buffer, you'll see methods like this one :

DeviceContext.Draw(int vertexCount, int startVertexLocation);

These methods allow you to set offsets into a buffer and draw a chosen number of vertices from that buffer. So you will need to have specific knowledge about the vertices inside your buffers, and where in that buffer each model starts and ends. That knowledge may come as a result of you constructing the buffer yourself in code, or parsing your model files (fbx, x, or whatever) and determining where one model starts and ends. The process of putting multiple objects into a buffer is the same as putting one in, you just have to keep track of what you are doing. Each model is just a collection of vertices after all. One model is essentially the same as the next, in that they are both just made of vertices.

Drawing different textures on your models is not a problem. But it might take you a little time to work it all out. You can include texture indices inside your vertex data (eg using a color channel to hold your texture index) and use a texture array to provide your textures, or load your different textures into the texture buffers. Also you might include sprite sheet coordinates in your vertex data and map into a sprite sheet.

Those ideas should get you started.

Edit : Actually I just noticed you are using DirectX 9. I don't think DX9 has texture arrays in the sense that I'm talking about (DirectX 11). So you'll have to just use the texture buffers or a sprite sheet.
thank you very much,but i have to go to bed now,i will tried it tomorrow
thank you!
Well,I don't directx9 has texture buffer or texture array.
And,DeviceContext.Draw(int vertexCount, int startVertexLocation);
This function is for directx 10,directx 9 does not has it.
Directx9 has this one "HRESULT DrawPrimitive(
[in] D3DPRIMITIVETYPE PrimitiveType,
[in] UINT StartVertex,
[in] UINT PrimitiveCount
);"
I don't understand,if i want to make a rectangle at point 0,0.And i want to make another rectangle at 10,10.How could i put all the these two vertices into only one vertex?If i just put them together they will connect each other,how to make them not connect?Could you give me a example?
I don't care texture now,solve the vertices first.
Thank you!
The first question is finished.
I found my problem,i use D3DPT_TRIANGLESTRIP to draw because some articles said D3DPT_TRIANGLESTRIP is much faster than D3DPT_TRIANGLELIST,i change to D3DPT_TRIANGLESTRIP just now,it is ok.
Thanks everybody!

This topic is closed to new replies.

Advertisement