OK, so, I've had a chance to mess with Direct 3D more. As soon as you figure out basically how to draw 1 thing on the screen and you want to experiment, its best to create some kind of framework. It would be nice to draw a scene, isolate your experiment to one class, and make it easy to add/remove/alter a particular aspect of the scene you are drawing. A dynamic scene could be rendered given the following metaphors:
An Entity class that represents any draw-able item. The Entity is responsible for loading its vertices, points, textures, etc. A non-staic light source could also be an entity in which case the Entity would load its color information etc. The Entity could be an abstract base class. Entities are added to a World which represents all the dynamic items that could be rendered. While Entities might not be visible at all times (View Coordinates, World Coordinates, Painter's Algorithm discussions in future posts) they might be thinking/acting elsewhere so they need periodic chances to do something even if they are not visible. A simple DirectX program/Render loop might go like this:
All in all, the render loop for a simple game/simulation might be pretty simple. The internet could use a lot more sample code for Direct3D, at least I've been unable to find much on how various things are typically done. For example, when an Entity changes position in the World is it customary to rebuild it or to Transform its vertices to reflect the new position? From what I can see, Transformations are used to model the observer changing its view position from one place to another, yet, rebuilding a vertex list seems very inefficient. Consider this partial snippet for drawing a triangle with 3 vertices and some color:
...class MovingTriangle : Entity{private CustomVertex.TransformedColored[] _verts;private VertexBuffer _vertBuffer;private float _topX;...public override void Render(Microsoft.DirectX.Direct3D.Device dev){ dev.SetStreamSource(0, _vertBuffer, 0);dev.VertexFormat = CustomVertex.TransformedColored.Format;dev.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); }public override void DeviceAquire(){_vertBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored), 3, _dev, 0, CustomVertex.TransformedColored.Format, Pool.Default); _vertBuffer.Created += new EventHandler(_vertBuffer_Created);_vertBuffer_Created(null, EventArgs.Empty);}void _vertBuffer_Created(object sender, EventArgs e){DoCreateBuffer();}private void DoCreateBuffer(){GraphicsStream stream = _vertBuffer.Lock(0, 0, 0);_verts = new CustomVertex.TransformedColored[3];_verts[0].X = _topX;_verts[0].Y = 50;_verts[0].Z = -0;_verts[0].Rhw = 1;_verts[0].Color = System.Drawing.Color.Red.ToArgb();// _verts[1].X = _topX + 100;_verts[1].Y = 250;_verts[1].Z = -0;_verts[1].Rhw = 1;_verts[1].Color = System.Drawing.Color.Red.ToArgb();// _verts[2].X = _topX - 100;_verts[2].Y = 250;_verts[2].Z = -0;_verts[2].Rhw = 1;_verts[2].Color = System.Drawing.Color.Red.ToArgb();stream.Write(_verts);_vertBuffer.Unlock();}}...
It seems as though vertex recreation would be horribly inefficient. I just ordered a pile of DirectX books from my favorite online store to shed some light on common D3D practices. As soon as I get through the next chapter in my Physics book I can share the nature of my ridiculous DirectX test project.