NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
GRID.H
Go to the documentation of this file.
00001 
00011 #pragma once
00012 
00013 #include <d3d9.h>
00014 #include <d3dx9math.h>
00015 #include <d3dx9tex.h>
00016 #include <d3dx9mesh.h>
00017 
00018 #ifndef PI
00019 #define PI 3.14159265f
00020 #endif
00021 
00022 struct GRIDVERTEX {
00023   D3DXVECTOR3 position; // The untransformed, 3D position for the vertex
00024   D3DXVECTOR3 normal; // normalized normal
00025   DWORD color; // color for this vertex
00026   float u, v; // the texture coordinates
00027 };
00028 
00029 #define D3DFVF_GRIDVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)
00030 
00031 struct MAP {
00032   unsigned int width, height;
00033   float* array;
00034 };
00035 
00036 class Grid {
00037 private:
00038   int widthVertices; // width in vertices
00039   int heightVertices; // height in vertices
00040   int widthFaces; // width in faces
00041   int heightFaces; // height in faces
00042   unsigned long numVertices;
00043   int numFaces;
00044   D3DXVECTOR3 v0, v1, v2, v3; // in D3D face order
00045   D3DXVECTOR3 dv; // delta v from 0 to 2
00046   GRIDVERTEX *vertices;
00047   WORD *indexData;
00048   LPD3DXMESH mesh;
00049   float scale;
00050   float uOffset, vOffset;
00051   public:
00052   Grid(
00053       const MAP& map,
00054       D3DXVECTOR3 *v0P,
00055       D3DXVECTOR3 *v1P,
00056       D3DXVECTOR3 *v2P,
00057       D3DXVECTOR3 *v3P
00058       );
00059 
00060   ~Grid() {
00061     delete vertices;
00062     delete indexData;
00063   }
00064   ;
00065 
00066   void Startup();
00067 
00068   void GetGridPoint(
00069       D3DXVECTOR3 *pt,
00070       int i, // row (from bottom to top)
00071       int j // column (from left to right)
00072       );
00073 
00074   void GetGridNormal(
00075       D3DXVECTOR3 *n,
00076       int i, // row (from bottom to top)
00077       int j // column (from left to right)
00078       );
00079 
00080   void UpdateUVs();
00081 
00082   void CreateDirectXMesh(
00083       LPDIRECT3DDEVICE9 pd3dDevice
00084       );
00085 
00086   void DrawDirectXMesh(
00087       LPDIRECT3DDEVICE9 pd3dDevice
00088       );
00089 
00090   float getTexScale() {
00091     return scale;
00092   }
00093 
00094   void setTexScale(float value) {
00095     scale = value;
00096     UpdateUVs();
00097   }
00098 
00099   float getTexOffsetU() {
00100     return uOffset;
00101   }
00102 
00103   void setTexOffsetU(float value) {
00104     uOffset = value;
00105     UpdateUVs();
00106   }
00107 
00108   float getTexOffsetV() {
00109     return vOffset;
00110   }
00111 
00112   void setTexOffsetV(float value) {
00113     vOffset = value;
00114     UpdateUVs();
00115   }
00116 
00117 };