06-28-2005, 01:43 PM
I'm a little confused about creating more than one triangle, i mean i can do it if i add every single triangle into an array called tris(), but i get the feeling this isnt sensible and theres an official way of doing things, my guess at a solution is to have each instance of my class to have a
Box(5) As TransformedColoured
but that would mean each time i want to render everything i have to call BeginScene, and have a render routine which looks like this in each instance of the class:
Public Sub Redraw()
Dim vBuff As VertexBuffer
vBuff = New VertexBuffer(GetType(CustomVertex.TransformedColored), Box.Length, device, Usage.None, CustomVertex.TransformedColored.Format, Pool.Default)
Dim stm As GraphicsStream = vBuff.Lock(0, 0, 0)
device.SetStreamSource(0, vBuff, 0)
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2)
and surely i dont want to do that inside every instance of every class which has a Box in (a box being two triangles) every single frame loop? please tell me am i thinking about this the wrong way?
06-28-2005, 05:45 PM
You can draw each triangle one at a time but it's really slow. When I first started my editor for a 2d game, I had a box class which stored 6 vertices and had a redraw sub like you mentioned. So each box would render one at a time. However, I did not recreate the vertices every time the box was drawn because there's no need to unless the vertex data needs to be changed (color or position is changing) in which case I'd just use drawuserprimitives.
Anywho, this is still basically wrong, unless you want a slow engine. The idea is to use batches, where you place all triangles with the same texture in your scene into one vertex buffer then render this larger vertex buffer in one draw call which speed things up quite a bit.
It might seem confusing, so let me explain what the next step I did with my editor was. There was a collection which held each box class. You could edit a box individually, which had its own 6 vertices, color, texture, etc. But when an edit was made, the editor then did a sort, which went through each box, found all the unique textures used, created a vertex buffer for each texture, then plopped all boxes with x texture into x vertex buffer. Then the editor rendered those vertex buffers instead of every box one by one.
06-29-2005, 08:52 AM
thanks, since i'm using vertexes without textures i can just use them all in the one array right?
well what i have been doing so far is using an index from the tris array, so everything is sent in at once, and when i draw i just use an offset for the two triangles that belong to the box, because after this i need to draw all the text above it, and then the next object may overlay that text and have its own text above itself.
Sorry if that was confusing but basically objects are on top of each other, and this wasnt slowing my computer down at all no matter how many objects were there, but the problem was when i quit the program, the longer the progam was left on, the longer it took to quit, and sometimes if i'd left it on for ages it would crash the computer when trying to quit. i thought this could only be either the creating each vertex every frame, or calling them all from one buffer.
07-04-2005, 07:57 PM
You could use zbuffer instead of drawing in layers like that, but I'm not sure what kind of text you need there. If the text was always the same, create some textures for it or something and draw it as a transparent quad.
If all your vertices use the same vertex format and don't need to be changed dynamically then I guess you could put them all in one array.
Somewhere in the back of the book "Managed DirectX 9" it mentions that when a vertex buffer is created it hooks to DeviceLost, DeviceReset and Disposing events of the device. Garbage collection won't clean it up because it's still attached to those events. Device objects supposedly all work like this.
"Since you never maintained a reference yourself to this vertex buffer, once it has lost scope, you can never remove it. In a short time you would run out of memory, and be puzzling to figure out why."
"Another side effect of this behavior is shutdown time." When you create the vertex buffer each frame it gets "orphaned" and thus when you shut down the program it signals the device to go through "event unwinding" which takes longer the more orphaned objects there are.
Maybe that's your problem...I'm pretty sure I'm having a problem with this as well.
07-07-2005, 09:07 AM
excellent thanks this could be it, sounds a lot like what i've got wrong, does it tell you about a solution to this problem?
EDIT: unfortunately the text will be different each time, sometimes the user types it in themselves, but i dont think thats affecting the problem since each engine has to redraw all its objects anyway.
07-12-2005, 07:52 PM
it seems like you gave me the solution, i just had to dispose of the vertex buffer each loop after the scene had been ended and displayed, i also disposed the stream just incase, i still have to test it in longer conditions but first results are promising! thanks for all your help! :)