We also want this object to be updatable and have all the goodies we have in our RC3DBaseObject, so we will also derive from that class, you RC3DBaseDrawableObject.h file should look like this now
Before we create this game class, we will quickly create a keyboard manager class, this will help manage user input, it works just the same as in the earlier posts, but we can control it from within our game class.
Create a class called RCKeyboardStateManager with no base class. We will put in the header all the elements we need to manage the keyboard
We are now going to define a number of methods that we will use in our game loop (this will still live out side of the Game class for this post), we also need a graphics device, and a keyboard manager. Our RCGame.h file should now look like this
Game InitializeAnd now to the function bodies, lets take a look at the Initialize method first
We now have a game class we can use to update and render objects in the world, so why don’t we create an object class that can then be actually rendered.
I am going to take some more from the SDK tutorial to build this class, as we have not looked at loading mesh’s in yet, we will define our own geometry and use that to render a cube, we will now create a TestCube class that will derive from RC3DBaseDrawableObject to do this.
We also want to include the the RCGame.h and Interfaces.h in here as we will be using them. We are also going to create a number of member variables that we can then use to set up and render our cube. We will also add an Initialize method and implement the derived Draw call. Your TestCube.h file should look something like this
We then have references to the vertex and pixel shaders we are going to use to render this object.
We then have our vertex and index buffer, again will be defined later.
We then have a number of Buffers, these are new to me as they are part of the DirectX 11 API (remember I am coming from DirectX 9c!) and I will explain my understanding of them later.
Lastly we have a texture and a sampler reference for it.
We now have a definition, as ever, onto the function bodies, but wait, we still need some data definitions, the input layout, the vertex and index buffers, all these I have put at the top of the TestCube.cpp file, this makes them global to the class and so can be accessed across methods. Really I should have not been lazy and set them up right in the class, but I am itching to get something rendered :P
So, at the top of the TestCube.cpp file we have this lump of variables
SimpleVertex is the vertex structure we will use for the cube’s verts
CBChangeOnResize is a structure that is reloaded on screen resize
CBChangesEveryFrame describes what will be passed to the shader every frame
vertices and index are what will fill the vertex and index buffers to render the cube.
Our TestCube.cpp now looks like this after we have populated the member functions
Our Draw call is again pretty straightforward, sets the device up with vertex and index buffers, sets the parameters in the buffers, then makes the draw call.
We now have something we can render, what we need now is a WinMain, a loop and some objects to render…
Just as before, create a main.cpp file in the solution
In here we will add all theeaders we need, create a WinProc call back as before and a WinMain function, we will also set up the game loop as before, but use our new game class to build the window and device.
Lets add our camera, set up a list of IDrawables and I Updateables and call the game update and draw calls. Under RC3DGame game, add the following lines (don’t forget to add the header for the camera!)
After we have initialized the graphics devic, we can initialize our camera and add it to the updateable list with in WinMain
Include the TestCube.h file. and add the following after the IDrawable list creation
and call it Shaders, then copy any dds texture you like into the project folder and call it seafloor.
In the Shaders filter/folder create anew file called Tutorial07.fx and paste the following into it
So, we can run this now right? Nope, if you do you will get this error
We need to right click the shader file, and tell the IDE not to try and build it as we will do this at run time, change the ItemType from “HLSL Compiler” to “Does not participate in build”
If we run the project now, we will get this
AT LAST! We have rendered our cube in DirectX 11 using a basic C++ framework.
That’s all well and good, but that’s just a square of texture, how do I know it’s a cube? Well you will need to move around it, so lets wire up some keyboard controls like we have in previous posts, but the game class is already getting us the keyboard state, so we just need to react to what’s pressed.
After our WinProc callback method we can create a HandleKeyboardInput method like this
Or, as shown in a clip a few posts ago, this :)
So, your main.cpp would look something like this
I hope you have found this of some use, please let me know what you think in the comments.
Source code is available here.