1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2021, assimp team
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
13 
14 * Redistributions of source code must retain the above
15   copyright notice, this list of conditions and the
16   following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19   copyright notice, this list of conditions and the
20   following disclaimer in the documentation and/or other
21   materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24   contributors may be used to endorse or promote products
25   derived from this software without specific prior
26   written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
41 
42 #if (!defined AV_ASSET_HELPER_H_INCLUDED)
43 #define AV_ASSET_HELPER_H_INCLUDED
44 
45 #include <d3d9.h>
46 #include <d3dx9.h>
47 #include <d3dx9mesh.h>
48 
49 #include <assimp/scene.h>
50 
51 namespace AssimpView {
52 
53 class SceneAnimator;
54 
55 //-------------------------------------------------------------------------------
56 /** \brief Class to wrap ASSIMP's asset output structures
57     */
58 //-------------------------------------------------------------------------------
59 class AssetHelper {
60 public:
61     enum {
62         // the original normal set will be used
63         ORIGINAL = 0x0u,
64 
65         // a smoothed normal set will be used
66         SMOOTH = 0x1u,
67 
68         // a hard normal set will be used
69         HARD = 0x2u,
70     };
71 
72     // default constructor
AssetHelper()73     AssetHelper() :
74             iNormalSet(ORIGINAL) {
75         mAnimator = NULL;
76         apcMeshes = NULL;
77         pcScene = NULL;
78     }
79 
80     //---------------------------------------------------------------
81     // default vertex data structure
82     // (even if tangents, bitangents or normals aren't
83     // required by the shader they will be committed to the GPU)
84     //---------------------------------------------------------------
85     struct Vertex {
86         aiVector3D vPosition;
87         aiVector3D vNormal;
88 
89         D3DCOLOR dColorDiffuse;
90         aiVector3D vTangent;
91         aiVector3D vBitangent;
92         aiVector2D vTextureUV;
93         aiVector2D vTextureUV2;
94         unsigned char mBoneIndices[4];
95         unsigned char mBoneWeights[4]; // last Weight not used, calculated inside the vertex shader
96 
97         /** Returns the vertex declaration elements to create a declaration from. */
GetDeclarationElementsVertex98         static D3DVERTEXELEMENT9 *GetDeclarationElements() {
99             static D3DVERTEXELEMENT9 decl[] = {
100                 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
101                 { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
102                 { 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
103                 { 0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
104                 { 0, 40, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
105                 { 0, 52, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
106                 { 0, 60, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
107                 { 0, 68, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 },
108                 { 0, 72, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 },
109                 D3DDECL_END()
110             };
111 
112             return decl;
113         }
114     };
115 
116     //---------------------------------------------------------------
117     // FVF vertex structure used for normals
118     //---------------------------------------------------------------
119     struct LineVertex {
120         aiVector3D vPosition;
121         DWORD dColorDiffuse;
122 
123         // retrieves the FVF code of the vertex type
GetFVFLineVertex124         static DWORD GetFVF() {
125             return D3DFVF_DIFFUSE | D3DFVF_XYZ;
126         }
127     };
128 
129     //---------------------------------------------------------------
130     // Helper class to store GPU related resources created for
131     // a given aiMesh
132     //---------------------------------------------------------------
133     class MeshHelper {
134     public:
MeshHelper()135         MeshHelper() :
136                 eShadingMode(),
137                 piVB(NULL),
138                 piIB(NULL),
139                 piVBNormals(NULL),
140                 piEffect(NULL),
141                 bSharedFX(false),
142                 piDiffuseTexture(NULL),
143                 piSpecularTexture(NULL),
144                 piAmbientTexture(NULL),
145                 piEmissiveTexture(NULL),
146                 piNormalTexture(NULL),
147                 piOpacityTexture(NULL),
148                 piShininessTexture(NULL),
149                 piLightmapTexture(NULL),
150                 fOpacity(),
151                 fShininess(),
152                 fSpecularStrength(),
153                 twosided(false),
154                 pvOriginalNormals(NULL) {}
155 
~MeshHelper()156         ~MeshHelper() {
157             // NOTE: This is done in DeleteAssetData()
158             // TODO: Make this a proper d'tor
159         }
160 
161         // shading mode to use. Either Lambert or otherwise phong
162         // will be used in every case
163         aiShadingMode eShadingMode;
164 
165         // vertex buffer
166         IDirect3DVertexBuffer9 *piVB;
167 
168         // index buffer. For partially transparent meshes
169         // created with dynamic usage to be able to update
170         // the buffer contents quickly
171         IDirect3DIndexBuffer9 *piIB;
172 
173         // vertex buffer to be used to draw vertex normals
174         // (vertex normals are generated in every case)
175         IDirect3DVertexBuffer9 *piVBNormals;
176 
177         // shader to be used
178         ID3DXEffect *piEffect;
179         bool bSharedFX;
180 
181         // material textures
182         IDirect3DTexture9 *piDiffuseTexture;
183         IDirect3DTexture9 *piSpecularTexture;
184         IDirect3DTexture9 *piAmbientTexture;
185         IDirect3DTexture9 *piEmissiveTexture;
186         IDirect3DTexture9 *piNormalTexture;
187         IDirect3DTexture9 *piOpacityTexture;
188         IDirect3DTexture9 *piShininessTexture;
189         IDirect3DTexture9 *piLightmapTexture;
190 
191         // material colors
192         D3DXVECTOR4 vDiffuseColor;
193         D3DXVECTOR4 vSpecularColor;
194         D3DXVECTOR4 vAmbientColor;
195         D3DXVECTOR4 vEmissiveColor;
196 
197         // opacity for the material
198         float fOpacity;
199 
200         // shininess for the material
201         float fShininess;
202 
203         // strength of the specular highlight
204         float fSpecularStrength;
205 
206         // two-sided?
207         bool twosided;
208 
209         // Stores a pointer to the original normal set of the asset
210         aiVector3D *pvOriginalNormals;
211     };
212 
213     // One instance per aiMesh in the globally loaded asset
214     MeshHelper **apcMeshes;
215 
216     // Scene wrapper instance
217     aiScene *pcScene;
218 
219     // Animation player to animate the scene if necessary
220     SceneAnimator *mAnimator;
221 
222     // Specifies the normal set to be used
223     unsigned int iNormalSet;
224 
225     // ------------------------------------------------------------------
226     // set the normal set to be used
227     void SetNormalSet(unsigned int iSet);
228 
229     // ------------------------------------------------------------------
230     // flip all normal vectors
231     void FlipNormals();
232     void FlipNormalsInt();
233 };
234 } // namespace AssimpView
235 
236 #endif // !! IG
237