1. Learn how to draw line.
2. Create a demo scene to show the engine.
Direct3D has a build-in primitive type “D3DPT_LINELIST”. When calling DrawPrimitive using this type, Direct3D will consider the vertices stored in vertex buffer are a list of isolated line segments.
Before calling DrawPrimitive, I need to modify the shaders:
- Vertex Shader: since I am passing world coordinates, I don’t need do a model-to-world transformation.
- Fragment Shader: the only thing I need to do is to output vertex color I stored in vertex buffer.
Most of my code resides in Engine or Game folder. Everything in a scene is an object which is inherited from interface IObject. Each object can have one controller that control the behavior of the object. All the concrete controllers are inherited from IController and located in game folder. If you are familiar with Unity, you can think a controller is like the script we created in Unity.
I have a ContentManager class that manages all the resources (mesh, shader, texture, etc.) So for every resource there will be only one copy in the memory.
Previously I used ID3DXConstantTable to set shader constants. Now I’ve changed my shader code to use registers. This allows me to set all constants in one call.
Here is a shader I used in my game:
You can see I bind g_transform_worldToView to register c0, g_transform_viewToScreen to register c4, g_transform_modelToWorld to register c8. Every register can hold 4 floats and each of my constant is a 4×4 matrix so it will take 4 registers to store one matrix.
In my code I created a struct to hold all constants:
Now when I want to draw my object, I set constants like this:
When I draw debug line I only need first 2 constants, in that case I just need to change the number from 12 to 8.
Here is a screenshot of my demo:
W, S to control the aim of the cannon.
Space to fire a bullet.
The number on the top right is the life of the box, when it comes to zero and player wins.
Fire has a 3s of cool down time.