Introduction to Direct 3D

Page 3 of 3
  1. Introduction
  2. Project Generation
  3. Defining the Direct3D class

Defining the Direct3D class

The constructor initializes the direct3D interfaces to NULL. It creates a Matrix Stack variable. Matrix Stacks allow the developer to create hierarchial transformation trees
by using push and pop methods. For the current matrix the developer call apply local transformations which rotate, scale, or translate from the objects local coordinate axis or the developer can elect to select tranformations relative from the parent matrix. All matrix multiplication is accumulative, so parent tranformations apply to the children.

  • The BuildSceneGraph,TraverseSceneGraph, and CleanupSceneGraph are used to build a hierarchial tree, traverse it and run mapped functions (DrawObject1,DrawObject2,DrawObject3), and cleanup the scenegraph when the base class is destroyed.
  • The InitializeEngine creates the Direct3d object, gets the current desktop display mode, create the direct3d device which allows control of video card resources.
  • The InitializeGeometry, InitializeGeometry2, and LoadTigerModel create data structures that contain vertex information. The vertex information is streamed to one of two vertex buffers used to by DrawObject1, DrawObject2, or DrawObject3.
  • The SetEnvironment method trues on Z buffering, enables Back Face Removal, and calls InitializeGeometry, InitializeGeometry2, and LoadTigerModel.
  • The Render method clears both the backbuffer and zbuffer; begins the scene; sets up the world, view, and project matrixes, and traverses the scenegraph; and ends the scene.

struct CUSTOMVERTEX
{
   D3DXVECTOR3 position; // The 3D position for the vertex
   D3DXVECTOR3 normal;   // The surface normal for the vertex
};

typedef struct CAMERA_STRUCT
{
   float x;
   float y;
   float z;
   float radians;
   float mStep;
}CAMERA_STRUCT_TYPE;

typedef struct TREENODE_STRUCT
{
   float m[16];
   char parent_key[30];
   char node_key[30];
   //void (*f)();
   //void (*engine)();
   struct TREENODE_STRUCT *sibling;
   struct TREENODE_STRUCT *child;
   struct TREENODE_STRUCT *most_recent_child;
}TREENODE_STRUCT_TYPE;


// Our custom FVF, which describes our custom vertex structure
//#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)

class D3DEngine
{

public:

   LPDIRECT3D8             g_pD3D; // Used to create the D3DDevice
   LPDIRECT3DDEVICE8       g_pd3dDevice; // Our rendering device
   LPDIRECT3DVERTEXBUFFER8 g_pVB; // Buffer to hold vertices
   LPDIRECT3DVERTEXBUFFER8 g_pVB2; // Buffer to hold vertices

   float m_swing;
   CAMERA_STRUCT_TYPE mCamera;
   TREENODE_STRUCT_TYPE* root;
   ID3DXMatrixStack* ppStack;


   LPD3DXMESH              g_pMesh;
   D3DMATERIAL8*           g_pMeshMaterials;
   LPDIRECT3DTEXTURE8*     g_pMeshTextures;
   DWORD                   g_dwNumMaterials;

public:
   D3DEngine::D3DEngine();
   D3DEngine::~D3DEngine();
   HRESULT InitializeEngine(HWND hWnd);
   void SetLightState(BOOL pState);
   void SetProjection();
   HRESULT InitializeGeometry();
   HRESULT InitializeGeometry2();
   void Render();
   void SetZBuffering(BOOL bFlag);
   void BackFaceRemoval(BOOL bFlag);
   void ReduceToUnit(D3DXVECTOR3 *vector);
   void CalculateNormal(D3DXVECTOR3 *vertex1, D3DXVECTOR3 *vertex2,D3DXVECTOR3 *vertex3,D3DXVECTOR3 *normal);
   void SetupLights();
   void PositionCamera();
   void CameraLeft();
   void CameraRight();
   void MoveCamera(float dStep);
   void AdvanceCamera();
   void RetreatCamera();
   //BOOL CreateCube(double x, double y, double z);
   void TraverseSceneGraph(TREENODE_STRUCT_TYPE *root);
   TREENODE_STRUCT_TYPE* Node(char *node_key);
   void AddChild(TREENODE_STRUCT_TYPE *parentNode, TREENODE_STRUCT_TYPE *childNode);
   void CleanSceneGraph(TREENODE_STRUCT_TYPE *root);
   void D3DEngine::BuildSceneGraph();
   HRESULT LoadTigerModel();
   void SetAmbientState(BOOL mFlag);
   void EnvironmentSetup();
   void CopperMaterial();
   void ChromeMaterial();
   void DrawObject1();
   void DrawObject2();
   void DrawObject3();
};

You might also like...

Comments

About the author

David Nishimoto United States

NishiSoft provides Part I of the Information Technology Project collaboration. Sign up and list your IT project tasks, assign task too friends, and get percent complete task.

Part will ...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”