/** @author dr. Szirmay-Kalos Lázsló DirectX functionality added by Barsi Attila @copyright BMGE Department of Control Engineering and Informatics @brief Simple Maya .obj file loader. */ #ifndef OBJECT_H #define OBJECT_H /** Tringle structure. It is composed of: Vertex indexes. Texture indexes. Per face center, normal and up vectors. BRDF color and emission. Triangle area, and triangle texture area. */ struct Triangle { int v[3]; int t[3]; Vector center, normal, up; Color BRDF, emission; float area, texarea; public: /** Constructor. */ Triangle( ) { } /** Constructor. @param i10 vertex index one @param i20 vertex index two @param i30 vertex index three @param t10 texture coordinate index one @param t20 texture coordinate index two @param t30 texture coordinate index three */ Triangle(int i10, int t10, int i20, int t20, int i30, int t30 ) { v[0] = i10; t[0] = t10; v[1] = i20; t[1] = t20; v[2] = i30; t[2] = t30; } }; //Maximum vertex count. #define MAXVERTICES 100000 //Maximum triangle count. #define MAXTRIANGLES 100000 //Red converter #define RED(i) ((i) >> 16) //Green converter #define GREEN(i) (((i) >> 8) & 255) //Blue converter #define BLUE(i) ((i) & 255) class ObjectModel { public: /**Array of all vertices. */ Vector vertices[MAXVERTICES]; /** Array of all texture coordinates. */ Vector texcoords[MAXVERTICES]; /** Array of all faces. */ Triangle triangles[MAXTRIANGLES]; /** Number of vertices, number of texture coordinates, number of triangles. */ int nvertices, ntexcoords, ntriangles; /** Total surface area, total texture surface area. */ float surface_area, texsurface_area; /** A pointer to the log file. */ FILE* logFile; /** A boolean indicating load success/faliliure. */ bool loaded; /** Constructor. */ ObjectModel(); /** Constructor. @param offset in the stream. */ ObjectModel(int offset); /** Destructor. */ ~ObjectModel( ); /** Adds a vertex to the array of 3D vertices. @param x the x coordinate. */ void AddVertex( float x, float y, float z ); /** Adds a vertex to the array of 2D texture coordinates. */ void AddTexCoord( float u, float v ); /** Adds a triangle to the array of triangles. */ void AddTriangle( int i1, int t1, int i2, int t2, int i3, int t3 ); /** This must be called while executing the OnInitDevice callback of the D3D utility. @return Error code. */ HRESULT initDeviceObjects(LPDIRECT3DDEVICE9 device); /** This must be called while executing the OnResetDevice callback of the D3D utility. @return Error code. */ HRESULT resetDeviceObjects(LPDIRECT3DDEVICE9 device); /** Returns all model vertices. @return An array of model vertices. */ D3DVERTEX_MODEL* getModelVertices(); /** Tells if the vertex rotation is CW or CCW @return The vertex rotation. */ bool isVertexRotationClockwise(); /** Returns the loaded boolean. @return The loaded flag. */ bool isLoaded(); /** Creates D3D vertices from the arrays. */ void CreateD3DVertices(); /** Loads an ALIAS Maya .obj file. @param fname File name. @param ccw counterclockwise? false/true:CW/CCW. @param cw Direct3D vertices are clockwise ? false/true:CW/CCW. @param isLight is the object a light source? @param su u coordinate scale @param sv v coordinate scale @param ou u coordinate offset @param ov v coordinate offset */ void Load( char * fname, bool ccw,bool cw, bool islight, float su, float sv, float ou, float ov ) ; /** Renders all loaded models. @param renderToAtlas Renders all loaded in render-to-atlas mode. */ HRESULT Render(bool renderToAtlas); /** Renders all loaded models with the given D3DXEffect, modelview and modelviewprojection matrices. @param m_pEffect Pointer to the D3DXEffect. @param renderToAtlas Renders all loaded in render-to-atlas mode. @param modelview the Model-View matrix. @param modelview the Model-View-Projection matrix. @return Error code. */ HRESULT RenderWithFX(LPD3DXEFFECT m_pEffect,bool renderToAtlas,D3DXMATRIX modelview,D3DXMATRIX modelviewproj); /** Returns center, normal and up vectors of the 'i'th triangle. @param i the index of the triangle. @param center the center of the triangle. @param normal the normal of the triangle. @param up the up vector of the triangle. @return Error code. */ void GetCenterTriangle( int i, Vector& center, Vector& normal, Vector& up ); /** Sets the current offset for the next model. You should not call this directly! @param offset (the sum of the face count of all previously loaded objects). */ void setOffset(int offset){this->offset=offset;} /** Returns the current offset (the sum of the face count of all previously loaded objects). @return The current offset. */ int getOffset(){return offset;} /** Returns the vertex declaration for the model. @return The vertex declaration. */ IDirect3DVertexDeclaration9* getVertexDeclaration(){return decl;} /** Returns the world transform matrix of the model. @return The transform matrix. */ D3DXMATRIX getTransformMatrix(){return transformMatrix;} /** Sets the world transform matrix of the model. */ void setTransformMatrix(D3DXMATRIX transformMatrix){this->transformMatrix=transformMatrix;} /** Returns the texture name of the model. @return The texture name. */ LPCTSTR getTextureName(){return textureName;}; /** Forces a texture name. @param textureName The name of the texture. */ void setTextureName(LPCTSTR textureName){this->textureName=textureName;}; /** Returns the shininess parameter. @return The shininess. */ float getShininess(){return shininess;} /** Sets the shininess parameter. @param shininess The shininess. */ void setShininess(float shininess){this->shininess=shininess;} private: /** An array of model vertices. Corresponding to the required FVF. */ D3DVERTEX_MODEL* modelVertices; /** D3D vertex rotation clockwise. */ bool vertexRotationClockwise; /** Vertex declaration created. */ bool vertexDeclarationCreated; /** The d3d device. */ LPDIRECT3DDEVICE9 device; /** The D3D vertex buffer for the model. */ LPDIRECT3DVERTEXBUFFER9 vertexBuffer; /** The triangle offset of the model. */ int offset; /** The shininess parameter of the model. */ float shininess; /** The texture name of the model. */ LPCTSTR textureName; /** The D3D vertex declaration of the model. */ LPDIRECT3DVERTEXDECLARATION9 decl; /** The world transform matrix of the model. */ D3DXMATRIX transformMatrix; }; #endif