AssimpRenderer: V:/ponx/dev/Visual Studio/AssimpRenderer/include/assethelper.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List

# V:/ponx/dev/Visual Studio/AssimpRenderer/include/assethelper.h

    00001 /*
    00002 ---------------------------------------------------------------------------
    00003 Direct3D AssimpRenderer by ponx
    00004 ---------------------------------------------------------------------------
    00005
    00006 This code is heavily based on the "AssimpView" project of the Open Asset Import Library (ASSIMP).
    00007 http://assimp.sourceforge.net
    00008
    00009 /*
    00010 ---------------------------------------------------------------------------
    00011 Open Asset Import Library (ASSIMP)
    00012 ---------------------------------------------------------------------------
    00013
    00014 Copyright (c) 2006-2008, ASSIMP Development Team
    00015
    00016 All rights reserved.
    00017
    00018 Redistribution and use of this software in source and binary forms,
    00019 with or without modification, are permitted provided that the following
    00020 conditions are met:
    00021
    00022 * Redistributions of source code must retain the above
    00023 copyright notice, this list of conditions and the
    00024 following disclaimer.
    00025
    00026 * Redistributions in binary form must reproduce the above
    00027 copyright notice, this list of conditions and the
    00028 following disclaimer in the documentation and/or other
    00029 materials provided with the distribution.
    00030
    00031 * Neither the name of the ASSIMP team, nor the names of its
    00032 contributors may be used to endorse or promote products
    00033 derived from this software without specific prior
    00034 written permission of the ASSIMP Development Team.
    00035
    00036 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    00037 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    00038 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    00039 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    00040 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00041 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00042 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    00043 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    00044 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00045 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00046 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00047 ---------------------------------------------------------------------------
    00048 */
    00049
    00050
    00051
    00052
    00053 #pragma once
    00054
    00055 class SceneAnimator;
    00056
    00057 #include <stdlib.h>
    00058 #include <aiScene.h>
    00059
    00060 #define NO_OF_DIRECTIONAL_LIGHTS        1
    00061
    00062 namespace boost
    00063 {
    00064         namespace serialization
    00065         {
    00066                 class access;
    00067         }
    00068 }
    00069
    00070 namespace AssimpRendererD3d
    00071 {
    00072         class AssetHelper;
    00073 }
    00074
    00075 //{
    00077
    00080 class AssimpRendererD3d::AssetHelper
    00081         {
    00082                 friend class boost::serialization::access;
    00083                 friend class AssimpRenderer;
    00084                 friend class CMaterialManager;
    00085                 friend class CMeshRenderer;
    00086
    00087         public:
    00088                         bool SetAmbientLight(D3DCOLOR ambientLight);
    00089                         bool SetDirectionalLight(unsigned int lightIndex, D3DCOLOR lightColor, const aiVector3D& lightDir);
    00090                         bool SetDirectionalLightDirection(unsigned int lightIndex, const aiVector3D& lightDir);
    00091                         bool SetDirectionalLightColor(unsigned int lightIndex, D3DCOLOR lightColor);
    00092                         bool SetSpecularShininessModificator(float modificator);
    00093                         bool SetSpecularStrengthModificator(float modificator);
    00094
    00095                         bool operator == (const AssetHelper& otherAsset);
    00096                         bool operator != (const AssetHelper& otherAsset);
    00097
    00098         private:
    00099                         enum
    00100                         {
    00101                                 // the original normal set will be used
    00102                                 ORIGINAL = 0x0u,
    00103
    00104                                 // a smoothed normal set will be used
    00105                                 SMOOTH = 0x1u,
    00106
    00107                                 // a hard normal set will be used
    00108                                 HARD = 0x2u,
    00109                         };
    00110
    00111                         // default constructor
    00112                         AssetHelper()
    00113                         {
    00114                                 mAnimator = NULL;
    00115                                 apcMeshes = NULL;
    00116                                 pcScene = NULL;
    00117                                 numMeshes = 0;
    00118
    00119                                 // PNX:
    00120                                 lightDirs[0] = aiVector3D(1.0f, 1.0f, 1.0f);
    00121                                 lightDirs[1] = aiVector3D(1.0f, 1.0f, 1.0f);
    00122                                 lightColors[0] = D3DCOLOR(D3DCOLOR_XRGB(255, 255, 255));
    00123                                 lightColors[1] = D3DCOLOR(D3DCOLOR_XRGB(255, 255, 255));
    00124                                 lightDirIntensity = 1.0f;
    00125                                 lightAmbient =  D3DCOLOR_XRGB(127, 127, 127);
    00126                                 lightShininessModificator = 1.0f;
    00127                                 lightSpecularStrengthModificator = 1.0f;
    00128                         }
    00129
    00130                         template<class Archive>
    00131                         void serialize(Archive & ar, const unsigned int version);
    00132
    00133                         //---------------------------------------------------------------
    00134                         // default vertex data structure
    00135                         // (even if tangents, bitangents or normals aren't
    00136                         // required by the shader they will be committed to the GPU)
    00137                         //---------------------------------------------------------------
    00138                         struct Vertex
    00139                         {
    00140                                 aiVector3D vPosition;
    00141                                 aiVector3D vNormal;
    00142
    00143                                 D3DCOLOR dColorDiffuse;
    00144                                 aiVector3D vTangent;
    00145                                 aiVector3D vBitangent;
    00146                                 aiVector2D vTextureUV;
    00147                                 aiVector2D vTextureUV2;
    00148                                 unsigned char mBoneIndices[4];
    00149                                 unsigned char mBoneWeights[4]; // last Weight not used, calculated inside the vertex shader
    00150
    00152                                 static D3DVERTEXELEMENT9* GetDeclarationElements()
    00153                                 {
    00154                                         static D3DVERTEXELEMENT9 decl[] =
    00155                                         {
    00156                                                 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
    00157                                                 { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
    00158                                                 { 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
    00159                                                 { 0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
    00160                                                 { 0, 40, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
    00161                                                 { 0, 52, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
    00162                                                 { 0, 60, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
    00163                                                 { 0, 68, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 },
    00164                                                 { 0, 72, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 },
    00165                                                 D3DDECL_END()
    00166                                         };
    00167
    00168                                         return decl;
    00169                                 }
    00170
    00171                                 Vertex ()
    00172                                         :
    00173                                         vPosition       (0.0f, 0.0f, 0.0f),
    00174                                         vNormal         (0.0f, 0.0f, 0.0f),
    00175                                         dColorDiffuse   (0),
    00176                                         vTangent        (0.0f, 0.0f, 0.0f),
    00177                                         vBitangent      (0.0f, 0.0f, 0.0f),
    00178                                         vTextureUV      (0.0f, 0.0f),
    00179                                         vTextureUV2     (0.0f, 0.0f)
    00180                                         {}
    00181
    00182                                         template<class Archive>
    00183                                         void serialize(Archive & ar, const unsigned int version);
    00184                         };
    00185
    00186
    00187                         //---------------------------------------------------------------
    00188                         // Helper class to store GPU related resources created for
    00189                         // a given aiMesh
    00190                         //---------------------------------------------------------------
    00191                         class MeshHelper
    00192                                 {
    00193                                 public:
    00194
    00195                                         MeshHelper ()
    00196                                                 :
    00197                                                 piVB                            (NULL),
    00198                                                 piIB                            (NULL),
    00199                                                 vertexBuffer        (NULL),
    00200                                                 indexBuffer         (NULL),
    00201                                                 piEffect                        (NULL),
    00202                                                 piDiffuseTexture        (NULL),
    00203                                                 piDiffuseTexture2       (NULL),
    00204                                                 piSpecularTexture       (NULL),
    00205                                                 piAmbientTexture        (NULL),
    00206                                                 piNormalTexture         (NULL),
    00207                                                 piEmissiveTexture       (NULL),
    00208                                                 piOpacityTexture        (NULL),
    00209                                                 piShininessTexture      (NULL),
    00210                                                 pvOriginalNormals       (NULL),
    00211                                                 bSharedFX           (false),
    00212                                                 numVertices         (0),
    00213                                                 primitiveTypes          (0),
    00214                                                 bHM                                     (false)
    00215                                                 {}
    00216
    00217                                         ~MeshHelper ()
    00218                                                 {
    00219                                                 // NOTE: This is done in DeleteAssetData()
    00220                                                 // TODO: Make this a proper d'tor
    00221                                                 }
    00222
    00223                                         template<class Archive>
    00224                                         void serialize(Archive & ar, const unsigned int version);
    00225
    00226                                         bool operator == (const AssetHelper::MeshHelper& otherMesh);
    00227                                         bool operator != (const AssetHelper::MeshHelper& otherMesh);
    00228
    00229                                         // shading mode to use. Either Lambert or otherwise phong
    00230                                         // will be used in every case
    00231                                         aiShadingMode eShadingMode;
    00232
    00233                                         // vertex buffer
    00234                                         IDirect3DVertexBuffer9* piVB;
    00235
    00236                                         // index buffer. For partially transparent meshes
    00237                                         // created with dynamic usage to be able to update
    00238                                         // the buffer contents quickly
    00239                                         IDirect3DIndexBuffer9* piIB;
    00240
    00241                                         // PNX: copy of the vertexBuffer piVB, used for serialization
    00242                                         AssetHelper::Vertex* vertexBuffer;
    00243
    00244                                         // PNX: copy of the indexBuffer piIB, used for serialization
    00245                                         unsigned int* indexBuffer;
    00246
    00247
    00248                                         // shader to be used
    00249                                         ID3DXEffect* piEffect;
    00250                                         bool bSharedFX;
    00251
    00252
    00253                                         // PNX: damit ich später LoadTexture() ohne das aiMaterial Object aufrufen kann,
    00254                                         // werden auch im MeshHelper die pfade zu den texturen gespeichert
    00255                                         aiString texturePathDiffuse;
    00256                                         aiString texturePathDiffuse2;
    00257                                         aiString texturePathSpecular;
    00258                                         aiString texturePathAmbient;
    00259                                         aiString texturePathEmissive;
    00260                                         aiString texturePathNormal;
    00261                                         aiString texturePathOpacity;
    00262                                         aiString texturePathShininess;
    00263
    00264                                         // PNX: das flag wird gesetzt, wenn statt einer NormalMap eine HeightMap benutzt wird
    00265                                         bool bHM;
    00266
    00267                                         //PNX: übernommen aus dem aiMesh
    00271                                         unsigned int numFaces;
    00272
    00273                                         // material textures
    00274                                         IDirect3DTexture9* piDiffuseTexture;
    00275                                         IDirect3DTexture9* piDiffuseTexture2;
    00276                                         IDirect3DTexture9* piSpecularTexture;
    00277                                         IDirect3DTexture9* piAmbientTexture;
    00278                                         IDirect3DTexture9* piEmissiveTexture;
    00279                                         IDirect3DTexture9* piNormalTexture;
    00280                                         IDirect3DTexture9* piOpacityTexture;
    00281                                         IDirect3DTexture9* piShininessTexture;
    00282
    00283                                         // material colors
    00284                                         D3DXVECTOR4 vDiffuseColor;
    00285                                         D3DXVECTOR4 vSpecularColor;
    00286                                         D3DXVECTOR4 vAmbientColor;
    00287                                         D3DXVECTOR4 vEmissiveColor;
    00288
    00289                                         // opacity for the material
    00290                                         float fOpacity;
    00291
    00292                                         // shininess for the material
    00293                                         float fShininess;
    00294
    00295                                         // strength of the specular highlight
    00296                                         float fSpecularStrength;
    00297
    00298                                         // Stores a pointer to the original normal set of the asset
    00299                                         aiVector3D* pvOriginalNormals;
    00300
    00301                                         // the number of vertices in the mesh
    00302                                         unsigned int numVertices;
    00303
    00304                                         unsigned int indexBufferLength;
    00305
    00306                                         // PNX: übermommen aus aiMesh
    00307                                         unsigned int primitiveTypes;
    00308
    00309                                 };  // class MeshHelper
    00310
    00311                 // One instance per aiMesh in the globally loaded asset
    00312                 MeshHelper** apcMeshes;
    00313
    00314                 // Scene wrapper instance
    00315                 aiScene* pcScene;
    00316
    00317                 // Animation player to animate the scene if necessary
    00318                 SceneAnimator* mAnimator;
    00319
    00320                 // PNX: filename of the asset
    00321                 aiString g_szFileName;
    00322
    00323                 //PNX: hatte ich aus der aiScene hier mit reingenommen, weil ich dachte, ich müsste die pcScene dann nicht serialisieren.
    00324                 // lass ich aber erstmal hier drin, weil es so irgendwie übersichtlicher ist.
    00325                 unsigned int numMeshes;
    00326
    00327                 // PNX:
    00328                 aiVector3D lightDirs[NO_OF_DIRECTIONAL_LIGHTS];                                 // directional light
    00329                 D3DCOLOR   lightColors[NO_OF_DIRECTIONAL_LIGHTS];                               // directional light colors
    00330                 float      lightDirIntensity;                                                                   // intensity of all directional lights
    00331                 D3DCOLOR   lightAmbient;
    00332
    00333                 float           lightShininessModificator;                                                              // modify the shininess of the whole asset
    00334                 float           lightSpecularStrengthModificator;                                               // modify the specular strength of the whole asset
    00335
    00336         }; // class AssetHelper
    00337
    00338
    00339
    00340 //} // namespace
    00341
    00342
    00343 namespace AssimpRendererD3d
    00344 {
    00345         template<class Archive>
    00346         void AssetHelper::serialize(Archive & ar, const unsigned int version)
    00347         {
    00348                 //                      DxWrapper* dxw = DxWrapper::Instance();
    00349                 //                      dxw->LogWrite("serializing assethelper");
    00350
    00351
    00352                 ar & numMeshes;
    00353
    00354                 //if (!assetHelper.pcScene)
    00355                 //      assetHelper.pcScene = new aiScene();
    00356
    00357                 ar & pcScene;
    00358
    00359                 if (!apcMeshes)
    00360                 {
    00361                         apcMeshes = new AssimpRendererD3d::AssetHelper::MeshHelper*[numMeshes];
    00362                         for (unsigned int i=0; i<numMeshes; ++i)
    00363                         {
    00364                                 apcMeshes[i] = 0;
    00365                         }
    00366                 }
    00367
    00368                 for (unsigned int i=0; i<numMeshes; ++i)
    00369                 {
    00370                         if (apcMeshes[i] == 0)
    00371                                 apcMeshes[i] = new AssimpRendererD3d::AssetHelper::MeshHelper();
    00372
    00373                         ar & *apcMeshes[i];
    00374                 }
    00375
    00376         }
    00377
    00378         template<class Archive>
    00379         void AssetHelper::Vertex::serialize(Archive & ar, const unsigned int version)
    00380         {
    00381                 ar & vPosition;
    00382                 ar & vNormal;
    00383
    00384                 ar & dColorDiffuse;
    00385                 ar & vTangent;
    00386                 ar & vBitangent;
    00387                 ar & vTextureUV;
    00388                 ar & vTextureUV2;
    00389
    00390                 ar & mBoneIndices[0];
    00391                 ar & mBoneIndices[1];
    00392                 ar & mBoneIndices[2];
    00393                 ar & mBoneIndices[3];
    00394
    00395                 ar & mBoneWeights[0]; // last Weight not used, calculated inside the vertex shader
    00396                 ar & mBoneWeights[1];
    00397                 ar & mBoneWeights[2];
    00398                 ar & mBoneWeights[3];
    00399         }
    00400
    00401         //                      DxWrapper* dxw = DxWrapper::Instance();
    00402         //                      dxw->LogWrite("serializing MeshHelper");
    00403         template<class Archive>
    00404         void AssetHelper::MeshHelper::serialize(Archive & ar, const unsigned int version)
    00405         {
    00406
    00407                 ar & texturePathDiffuse;
    00408                 ar & texturePathDiffuse2;
    00409                 ar & texturePathSpecular;
    00410                 ar & texturePathAmbient;
    00411                 ar & texturePathEmissive;
    00412                 ar & texturePathNormal;
    00413                 ar & texturePathOpacity;
    00414                 ar & texturePathShininess;
    00415
    00416                 ar & bHM;
    00417                 ar & fShininess;
    00418                 ar & fSpecularStrength;
    00419                 ar & fOpacity;
    00420                 ar & bSharedFX;
    00421                 ar & eShadingMode;
    00422
    00423                 ar & vDiffuseColor;
    00424                 ar & vSpecularColor;
    00425                 ar & vEmissiveColor;
    00426                 ar & vAmbientColor;
    00427
    00428                 ar & numFaces;
    00429                 ar & numVertices;
    00430                 ar & primitiveTypes;
    00431
    00432                 // create the vertexBuffer upon deserialization
    00433                 if (vertexBuffer == 0)
    00434                         vertexBuffer = new AssimpRendererD3d::AssetHelper::Vertex[numVertices];
    00435
    00436                 // serialize the vertexBuffer
    00437                 AssimpRendererD3d::AssetHelper::Vertex* pVertex =  vertexBuffer;
    00438                 for (unsigned int i=0; i < numVertices; ++i)
    00439                 {
    00440                         ar & *pVertex;
    00441                         pVertex++;
    00442                 }
    00443
    00444                 // (de)serialize the indexBuffer
    00445                 ar & indexBufferLength;
    00446
    00447                 if (indexBuffer == 0)
    00448                 {
    00449                         indexBuffer = new unsigned int[indexBufferLength];
    00450                 }
    00451                 unsigned int* pIndex = indexBuffer;
    00452                 for (unsigned int i=0; i < indexBufferLength; ++i)
    00453                 {
    00454                         ar & *pIndex;
    00455                         pIndex++;
    00456                 }
    00457         }
    00458
    00459 }