[1808] | 1 | /**
|
---|
| 2 | @author dr. Szirmay-Kalos Lázsló DirectX functionality added by Barsi Attila
|
---|
| 3 | @copyright BMGE Department of Control Engineering and Informatics
|
---|
| 4 | @brief Simple Maya .obj file loader.
|
---|
| 5 | */
|
---|
| 6 | #ifndef OBJECT_H
|
---|
| 7 | #define OBJECT_H
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | Tringle structure. It is composed of:
|
---|
| 12 | Vertex indexes.
|
---|
| 13 | Texture indexes.
|
---|
| 14 | Per face center, normal and up vectors.
|
---|
| 15 | BRDF color and emission.
|
---|
| 16 | Triangle area, and triangle texture area.
|
---|
| 17 | */
|
---|
| 18 | struct Triangle {
|
---|
| 19 | int v[3];
|
---|
| 20 | int t[3];
|
---|
| 21 | Vector center, normal, up;
|
---|
| 22 | Color BRDF, emission;
|
---|
| 23 | float area, texarea;
|
---|
| 24 | public:
|
---|
| 25 | /**
|
---|
| 26 | Constructor.
|
---|
| 27 | */
|
---|
| 28 | Triangle( ) { }
|
---|
| 29 | /**
|
---|
| 30 | Constructor.
|
---|
| 31 | @param i10 vertex index one
|
---|
| 32 | @param i20 vertex index two
|
---|
| 33 | @param i30 vertex index three
|
---|
| 34 | @param t10 texture coordinate index one
|
---|
| 35 | @param t20 texture coordinate index two
|
---|
| 36 | @param t30 texture coordinate index three
|
---|
| 37 | */
|
---|
| 38 | Triangle(int i10, int t10, int i20, int t20, int i30, int t30 ) {
|
---|
| 39 | v[0] = i10; t[0] = t10;
|
---|
| 40 | v[1] = i20; t[1] = t20;
|
---|
| 41 | v[2] = i30; t[2] = t30;
|
---|
| 42 | }
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | //Maximum vertex count.
|
---|
| 46 | #define MAXVERTICES 100000
|
---|
| 47 | //Maximum triangle count.
|
---|
| 48 | #define MAXTRIANGLES 100000
|
---|
| 49 |
|
---|
| 50 | //Red converter
|
---|
| 51 | #define RED(i) ((i) >> 16)
|
---|
| 52 | //Green converter
|
---|
| 53 | #define GREEN(i) (((i) >> 8) & 255)
|
---|
| 54 | //Blue converter
|
---|
| 55 | #define BLUE(i) ((i) & 255)
|
---|
| 56 |
|
---|
| 57 | class ObjectModel {
|
---|
| 58 | public:
|
---|
| 59 | /**Array of all vertices.
|
---|
| 60 | */
|
---|
| 61 | Vector vertices[MAXVERTICES];
|
---|
| 62 | /**
|
---|
| 63 | Array of all texture coordinates.
|
---|
| 64 | */
|
---|
| 65 | Vector texcoords[MAXVERTICES];
|
---|
| 66 | /**
|
---|
| 67 | Array of all faces.
|
---|
| 68 | */
|
---|
| 69 | Triangle triangles[MAXTRIANGLES];
|
---|
| 70 | /**
|
---|
| 71 | Number of vertices, number of texture coordinates, number of triangles.
|
---|
| 72 | */
|
---|
| 73 | int nvertices, ntexcoords, ntriangles;
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | Total surface area, total texture surface area.
|
---|
| 78 | */
|
---|
| 79 | float surface_area, texsurface_area;
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | A pointer to the log file.
|
---|
| 83 | */
|
---|
| 84 | FILE* logFile;
|
---|
| 85 | /**
|
---|
| 86 | A boolean indicating load success/faliliure.
|
---|
| 87 | */
|
---|
| 88 | bool loaded;
|
---|
| 89 | /**
|
---|
| 90 | Constructor.
|
---|
| 91 | */
|
---|
| 92 | ObjectModel();
|
---|
| 93 | /**
|
---|
| 94 | Constructor.
|
---|
| 95 | @param offset in the stream.
|
---|
| 96 | */
|
---|
| 97 | ObjectModel(int offset);
|
---|
| 98 | /**
|
---|
| 99 | Destructor.
|
---|
| 100 | */
|
---|
| 101 | ~ObjectModel( );
|
---|
| 102 | /**
|
---|
| 103 | Adds a vertex to the array of 3D vertices.
|
---|
| 104 | @param x the x coordinate.
|
---|
| 105 | */
|
---|
| 106 | void AddVertex( float x, float y, float z );
|
---|
| 107 | /**
|
---|
| 108 | Adds a vertex to the array of 2D texture coordinates.
|
---|
| 109 | */
|
---|
| 110 | void AddTexCoord( float u, float v );
|
---|
| 111 | /**
|
---|
| 112 | Adds a triangle to the array of triangles.
|
---|
| 113 | */
|
---|
| 114 | void AddTriangle( int i1, int t1, int i2, int t2, int i3, int t3 );
|
---|
| 115 | /**
|
---|
| 116 | This must be called while executing the OnInitDevice callback of the D3D utility.
|
---|
| 117 | @return Error code.
|
---|
| 118 | */
|
---|
| 119 | HRESULT initDeviceObjects(LPDIRECT3DDEVICE9 device);
|
---|
| 120 | /**
|
---|
| 121 | This must be called while executing the OnResetDevice callback of the D3D utility.
|
---|
| 122 | @return Error code.
|
---|
| 123 | */
|
---|
| 124 | HRESULT resetDeviceObjects(LPDIRECT3DDEVICE9 device);
|
---|
| 125 | /**
|
---|
| 126 | Returns all model vertices.
|
---|
| 127 | @return An array of model vertices.
|
---|
| 128 | */
|
---|
| 129 | D3DVERTEX_MODEL* getModelVertices();
|
---|
| 130 | /**
|
---|
| 131 | Tells if the vertex rotation is CW or CCW
|
---|
| 132 | @return The vertex rotation.
|
---|
| 133 | */
|
---|
| 134 | bool isVertexRotationClockwise();
|
---|
| 135 | /**
|
---|
| 136 | Returns the loaded boolean.
|
---|
| 137 | @return The loaded flag.
|
---|
| 138 | */
|
---|
| 139 | bool isLoaded();
|
---|
| 140 | /**
|
---|
| 141 | Creates D3D vertices from the arrays.
|
---|
| 142 | */
|
---|
| 143 | void CreateD3DVertices();
|
---|
| 144 | /**
|
---|
| 145 | Loads an ALIAS Maya .obj file.
|
---|
| 146 | @param fname File name.
|
---|
| 147 | @param ccw counterclockwise? false/true:CW/CCW.
|
---|
| 148 | @param cw Direct3D vertices are clockwise ? false/true:CW/CCW.
|
---|
| 149 | @param isLight is the object a light source?
|
---|
| 150 | @param su u coordinate scale
|
---|
| 151 | @param sv v coordinate scale
|
---|
| 152 | @param ou u coordinate offset
|
---|
| 153 | @param ov v coordinate offset
|
---|
| 154 |
|
---|
| 155 | */
|
---|
| 156 | void Load( char * fname, bool ccw,bool cw, bool islight, float su, float sv, float ou, float ov ) ;
|
---|
| 157 | /**
|
---|
| 158 | Renders all loaded models.
|
---|
| 159 | @param renderToAtlas Renders all loaded in render-to-atlas mode.
|
---|
| 160 | */
|
---|
| 161 | HRESULT Render(bool renderToAtlas);
|
---|
| 162 | /**
|
---|
| 163 | Renders all loaded models with the given D3DXEffect, modelview and modelviewprojection matrices.
|
---|
| 164 | @param m_pEffect Pointer to the D3DXEffect.
|
---|
| 165 | @param renderToAtlas Renders all loaded in render-to-atlas mode.
|
---|
| 166 | @param modelview the Model-View matrix.
|
---|
| 167 | @param modelview the Model-View-Projection matrix.
|
---|
| 168 | @return Error code.
|
---|
| 169 | */
|
---|
| 170 | HRESULT RenderWithFX(LPD3DXEFFECT m_pEffect,bool renderToAtlas,D3DXMATRIX modelview,D3DXMATRIX modelviewproj);
|
---|
| 171 | /**
|
---|
| 172 | Returns center, normal and up vectors of the 'i'th triangle.
|
---|
| 173 | @param i the index of the triangle.
|
---|
| 174 | @param center the center of the triangle.
|
---|
| 175 | @param normal the normal of the triangle.
|
---|
| 176 | @param up the up vector of the triangle.
|
---|
| 177 | @return Error code.
|
---|
| 178 | */
|
---|
| 179 | void GetCenterTriangle( int i, Vector& center, Vector& normal, Vector& up );
|
---|
| 180 | /**
|
---|
| 181 | Sets the current offset for the next model. You should not call this directly!
|
---|
| 182 | @param offset (the sum of the face count of all previously loaded objects).
|
---|
| 183 | */
|
---|
| 184 | void setOffset(int offset){this->offset=offset;}
|
---|
| 185 | /**
|
---|
| 186 | Returns the current offset (the sum of the face count of all previously loaded objects).
|
---|
| 187 | @return The current offset.
|
---|
| 188 | */
|
---|
| 189 | int getOffset(){return offset;}
|
---|
| 190 | /**
|
---|
| 191 | Returns the vertex declaration for the model.
|
---|
| 192 | @return The vertex declaration.
|
---|
| 193 | */
|
---|
| 194 | IDirect3DVertexDeclaration9* getVertexDeclaration(){return decl;}
|
---|
| 195 | /**
|
---|
| 196 | Returns the world transform matrix of the model.
|
---|
| 197 | @return The transform matrix.
|
---|
| 198 | */
|
---|
| 199 | D3DXMATRIX getTransformMatrix(){return transformMatrix;}
|
---|
| 200 | /**
|
---|
| 201 | Sets the world transform matrix of the model.
|
---|
| 202 | */
|
---|
| 203 | void setTransformMatrix(D3DXMATRIX transformMatrix){this->transformMatrix=transformMatrix;}
|
---|
| 204 | /**
|
---|
| 205 | Returns the texture name of the model.
|
---|
| 206 | @return The texture name.
|
---|
| 207 | */
|
---|
| 208 | LPCTSTR getTextureName(){return textureName;};
|
---|
| 209 | /**
|
---|
| 210 | Forces a texture name.
|
---|
| 211 | @param textureName The name of the texture.
|
---|
| 212 | */
|
---|
| 213 | void setTextureName(LPCTSTR textureName){this->textureName=textureName;};
|
---|
| 214 | /**
|
---|
| 215 | Returns the shininess parameter.
|
---|
| 216 | @return The shininess.
|
---|
| 217 | */
|
---|
| 218 | float getShininess(){return shininess;}
|
---|
| 219 | /**
|
---|
| 220 | Sets the shininess parameter.
|
---|
| 221 | @param shininess The shininess.
|
---|
| 222 | */
|
---|
| 223 | void setShininess(float shininess){this->shininess=shininess;}
|
---|
| 224 |
|
---|
| 225 | private:
|
---|
| 226 | /**
|
---|
| 227 | An array of model vertices. Corresponding to the required FVF.
|
---|
| 228 | */
|
---|
| 229 | D3DVERTEX_MODEL* modelVertices;
|
---|
| 230 | /**
|
---|
| 231 | D3D vertex rotation clockwise.
|
---|
| 232 | */
|
---|
| 233 | bool vertexRotationClockwise;
|
---|
| 234 | /**
|
---|
| 235 | Vertex declaration created.
|
---|
| 236 | */
|
---|
| 237 | bool vertexDeclarationCreated;
|
---|
| 238 | /**
|
---|
| 239 | The d3d device.
|
---|
| 240 | */
|
---|
| 241 | LPDIRECT3DDEVICE9 device;
|
---|
| 242 | /**
|
---|
| 243 | The D3D vertex buffer for the model.
|
---|
| 244 | */
|
---|
| 245 | LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
|
---|
| 246 | /**
|
---|
| 247 | The triangle offset of the model.
|
---|
| 248 | */
|
---|
| 249 | int offset;
|
---|
| 250 | /**
|
---|
| 251 |
|
---|
| 252 | The shininess parameter of the model.
|
---|
| 253 | */
|
---|
| 254 | float shininess;
|
---|
| 255 | /**
|
---|
| 256 | The texture name of the model.
|
---|
| 257 | */
|
---|
| 258 | LPCTSTR textureName;
|
---|
| 259 | /**
|
---|
| 260 | The D3D vertex declaration of the model.
|
---|
| 261 | */
|
---|
| 262 | LPDIRECT3DVERTEXDECLARATION9 decl;
|
---|
| 263 | /**
|
---|
| 264 | The world transform matrix of the model.
|
---|
| 265 | */
|
---|
| 266 | D3DXMATRIX transformMatrix;
|
---|
| 267 | };
|
---|
| 268 |
|
---|
| 269 | #endif |
---|