W3DGranny.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DGranny.cpp ////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: W3DGranny.cpp
  36. //
  37. // Created: Mark Wilczynski, Sep 2001
  38. //
  39. // Desc: Methods for using Granny models within the W3D engine.
  40. //-----------------------------------------------------------------------------
  41. #ifdef INCLUDE_GRANNY_IN_BUILD
  42. #include "W3DDevice/GameClient/W3DGranny.h"
  43. #include "common/GlobalData.h"
  44. #include "texture.h"
  45. #include "colmath.h"
  46. #include "coltest.h"
  47. #include "rinfo.h"
  48. #include "camera.h"
  49. #include "assetmgr.h"
  50. #include "WW3D2/DX8Wrapper.h"
  51. #include "WW3D2/Scene.h"
  52. #pragma comment( lib, "granny2" )
  53. static granny_pnt332_vertex g_blendingBuffer[4096]; ///<temporary workspace for granny (all models < 4096 verts).
  54. /** Local function used to find animation matching the given model */
  55. static int FindTrackGroupFor(granny_animation *Animation, granny_model_instance *ModelInstance)
  56. {
  57. if(Animation && ModelInstance)
  58. {
  59. {for(Int TrackGroupIndex = 0;
  60. TrackGroupIndex < Animation->TrackGroupCount;
  61. ++TrackGroupIndex)
  62. {
  63. if(strcmp(Animation->TrackGroups[TrackGroupIndex]->Name,
  64. GrannyGetSourceModel(ModelInstance)->Name) == 0)
  65. {
  66. return(TrackGroupIndex);
  67. }
  68. }}
  69. }
  70. return(-1);
  71. }
  72. /** Local function used to modify the original granny data - flip coordintes, scale, etc. */
  73. static void TransformFile(granny_file_info *FileInfo)
  74. {
  75. float Origin[] = {0, 0, 0};
  76. float RightVector[] = {1, 0, 0}; //x
  77. float UpVector[] = {0, 0, 1}; //y
  78. float BackVector[] = {0, -1, 0};//z
  79. return;
  80. float Affine3[3];
  81. float Linear3x3[3][3];
  82. float InverseLinear3x3[3][3];
  83. GrannyAutoComputeBasisConversion(FileInfo, 1.0f,
  84. Origin,
  85. RightVector,
  86. UpVector,
  87. BackVector,
  88. Affine3, (float *)Linear3x3,
  89. (float *)InverseLinear3x3);
  90. GrannyAutoTransformFile(FileInfo, Affine3,
  91. (float *)Linear3x3,
  92. (float *)InverseLinear3x3);
  93. }
  94. //=============================================================================
  95. // GrannyRenderObjClass::~GrannyRenderObjClass
  96. //=============================================================================
  97. /** Destructor. Releases w3d/granny assets. */
  98. //=============================================================================
  99. GrannyRenderObjClass::~GrannyRenderObjClass(void)
  100. {
  101. if (m_animationControl)
  102. GrannyFreeControl(m_animationControl);
  103. if (m_modelInstance)
  104. GrannyFreeModelInstance(m_modelInstance);
  105. freeResources();
  106. }
  107. //=============================================================================
  108. // GrannyRenderObjClass::GrannyRenderObjClass
  109. //=============================================================================
  110. /** Constructor. Creates an instance of the prototype. */
  111. //=============================================================================
  112. GrannyRenderObjClass::GrannyRenderObjClass(const GrannyPrototypeClass &proto)
  113. :m_prototype(proto)
  114. {
  115. granny_file_info *fileInfo = GrannyGetFileInfo((granny_file *)proto.m_file);
  116. m_boundingBox = proto.m_boundingBox;
  117. m_boundingSphere = proto.m_boundingSphere;
  118. m_vertexCount = proto.m_vertexCount;
  119. m_animationControl = NULL; //no animation playing by default
  120. if (fileInfo)
  121. {
  122. //Find the skinned model
  123. for (Int modelIndex=0; modelIndex<fileInfo->ModelCount; modelIndex++)
  124. {
  125. granny_model *sourceModel = fileInfo->Models[modelIndex];
  126. //ignore bounding boxes since they are never rendered
  127. if (stricmp(sourceModel->Name,"AABOX") != 0)
  128. m_modelInstance = GrannyInstantiateModel(fileInfo->Models[modelIndex]);
  129. }
  130. if(m_modelInstance)
  131. {
  132. //assign textures to the model
  133. GrannyAutoBindModel(m_modelInstance, _GrannyLoader.Get_Material_Library(), 1);
  134. // GrannyAddModelToScene(Global.Scene, ModelInstance); ///@todo: Create a granny scene for quicker updates.
  135. float *RootTransform = GrannyGetModelRootTransform(m_modelInstance);
  136. RootTransform[12] = 0;//x
  137. RootTransform[13] = 0;//y
  138. RootTransform[14] = 0;//z
  139. }//modelinstance*/
  140. }
  141. }
  142. /** Set scaling factor applied to prototype during rendering */
  143. void GrannyRenderObjClass::Set_ObjectScale(float scale)
  144. {
  145. ObjectScale=scale;
  146. //adjust bounding volumes we copied from non-scaled prototype.
  147. m_boundingBox.Center *= scale;
  148. m_boundingBox.Extent *= scale;
  149. ///@todo: Remove earlier hacks in W3D to get instance matrix scaling!
  150. ///After the W3D hacks are gone, we should also scale the radius here.
  151. m_boundingSphere.Center *= scale;
  152. }
  153. //=============================================================================
  154. // GrannyRenderObjClass::Get_Obj_Space_Bounding_Sphere
  155. //=============================================================================
  156. /** WW3D method that returns object bounding sphere used in frustum culling*/
  157. //=============================================================================
  158. void GrannyRenderObjClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  159. { sphere=m_boundingSphere;
  160. }
  161. //=============================================================================
  162. // GrannyRenderObjClass::Get_Obj_Space_Bounding_Box
  163. //=============================================================================
  164. /** WW3D method that returns object bounding box used in collision detection*/
  165. //=============================================================================
  166. void GrannyRenderObjClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  167. {
  168. box=m_boundingBox;
  169. }
  170. //=============================================================================
  171. // GrannyRenderObjClass::Cast_Ray
  172. //=============================================================================
  173. /** WW3D method that returns intersection point between ray and model.
  174. We will only return results of a simple 'pick box' - not full triangle level
  175. detail.
  176. */
  177. //=============================================================================
  178. Bool GrannyRenderObjClass::Cast_Ray(RayCollisionTestClass & raytest)
  179. {
  180. if ((Get_Collision_Type() & raytest.CollisionType) == 0) return false;
  181. AABoxClass hbox=m_boundingBox;
  182. hbox.Transform(Get_Transform());
  183. if (CollisionMath::Collide(raytest.Ray,hbox,raytest.Result)) {
  184. raytest.CollidedRenderObj = this;
  185. return true;
  186. }
  187. return false;
  188. }
  189. //=============================================================================
  190. // GrannyRenderObjClass::Class_ID
  191. //=============================================================================
  192. /** returns the class id, so the scene can tell what kind of render object it has. */
  193. //=============================================================================
  194. Int GrannyRenderObjClass::Class_ID(void) const
  195. {
  196. return RenderObjClass::CLASSID_UNKNOWN;
  197. }
  198. //=============================================================================
  199. // GrannyRenderObjClass::Clone
  200. //=============================================================================
  201. /** Not used, but required virtual method. */
  202. //=============================================================================
  203. RenderObjClass * GrannyRenderObjClass::Clone(void) const
  204. {
  205. assert(false);
  206. return NULL;
  207. }
  208. //=============================================================================
  209. // GrannyRenderObjClass::freeResources
  210. //=============================================================================
  211. /** Free any W3D resources associated with this object */
  212. //=============================================================================
  213. Int GrannyRenderObjClass::freeResources(void)
  214. {
  215. // REF_PTR_RELEASE(m_stageZeroTexture);
  216. return 0;
  217. }
  218. /** W3D Method used to control the animation assocated with this render object. */
  219. void GrannyRenderObjClass::Set_Animation( HAnimClass * motion, float frame, int anim_mode)
  220. {
  221. GrannyAnimClass *gAnim=(GrannyAnimClass*)motion;
  222. granny_animation *Animation=gAnim->Animation;
  223. if(Animation)
  224. {
  225. Int trackIndex=-1;
  226. //Check if this animation works with this model
  227. trackIndex=FindTrackGroupFor(Animation,m_modelInstance);
  228. if(trackIndex != -1)
  229. {
  230. //stop previous animations
  231. if (m_animationControl)
  232. { GrannyFreeControl(m_animationControl);
  233. m_animationControl=NULL;
  234. }
  235. m_animationControl = GrannyPlayControlledAnimation(frame / gAnim->FrameRate, Animation, trackIndex, m_modelInstance, 0);
  236. if (m_animationControl)
  237. {
  238. GrannySetControlSpeed(m_animationControl, 1.0f); //play at normal speed
  239. if (anim_mode == ANIM_MODE_LOOP)
  240. GrannySetControlLooping(m_animationControl, true);
  241. else
  242. GrannySetControlLooping(m_animationControl, false);
  243. }
  244. }
  245. }
  246. }
  247. //=============================================================================
  248. // GrannyRenderObjClass::Render
  249. //=============================================================================
  250. /** Draws this render object to the screen.
  251. */
  252. //=============================================================================
  253. void GrannyRenderObjClass::Render(RenderInfoClass & rinfo)
  254. {
  255. TheGrannyRenderObjSystem->AddRenderObject(rinfo, this);
  256. return;
  257. //update the model
  258. GrannySetModelClock(m_modelInstance, LastSyncTime);
  259. LastSyncTime = WW3D::Get_Sync_Time()*0.001f; //convert to seconds
  260. GrannySampleModelAnimations(m_modelInstance, 0, GrannyGetModelBoneCount(m_modelInstance),
  261. GrannyGetModelLocalPose(m_modelInstance));
  262. GrannyBuildModelWorldTransforms(m_modelInstance, 0,
  263. GrannyGetModelBoneCount(m_modelInstance),
  264. GrannyGetModelLocalPose(m_modelInstance),
  265. GrannyGetModelRootTransform(m_modelInstance),
  266. GrannyGetModelWorldPose(m_modelInstance));
  267. Real *RootTransform = GrannyGetModelRootTransform(m_modelInstance);
  268. RootTransform[12] = 0; //X
  269. RootTransform[13] = 0; //Y
  270. RootTransform[13] = 0; //Z
  271. RootTransform[0] = ObjectScale;
  272. RootTransform[5] = ObjectScale;
  273. RootTransform[10] = ObjectScale;
  274. Int MeshCount = GrannyGetModelMeshCount(m_modelInstance);
  275. for(Int MeshIndex = 0; MeshIndex < MeshCount; ++MeshIndex)
  276. {
  277. granny_mesh_instance *Mesh = (granny_mesh_instance *)GrannyGetModelMeshCookie(m_modelInstance, MeshIndex);
  278. granny_mesh_model_binding *Binding = GrannyGetMeshModelBinding(Mesh);
  279. granny_pnt332_vertex *Vertices = 0;
  280. if(GrannyMeshIsRigid(Mesh))
  281. { assert(0); //have not coded support for rigid meshes yet - only soft skinned supported.
  282. // granny_matrix_4x4 TempBuffer;
  283. // glMultMatrixf(GrannyGetRigidMeshTransform(
  284. // Binding, GrannyGetModelWorldPose(m_modelInstance),
  285. // (granny_real32 *)TempBuffer));
  286. Vertices = (granny_pnt332_vertex *)GrannyGetMeshVertices(Mesh);
  287. }
  288. else
  289. {
  290. GrannyDeformMesh(Binding, GrannyGetModelWorldPose(m_modelInstance),
  291. 0, g_blendingBuffer);
  292. Vertices = g_blendingBuffer;
  293. }
  294. int GroupCount = GrannyGetMeshTriangleGroupCount(Mesh);
  295. {for(Int GroupIndex = 0;
  296. GroupIndex < GroupCount;
  297. ++GroupIndex)
  298. {
  299. granny_material_instance *Material = (granny_material_instance *)
  300. GrannyGetMeshMaterialCookie(
  301. Mesh, GrannyGetMeshTriangleGroupMaterialIndex(
  302. Mesh, GroupIndex));
  303. if(Material)
  304. {
  305. Int TextureIndex;
  306. if(GrannyGetMaterialTextureIndexByType(
  307. Material, GrannyDiffuseColorTexture, &TextureIndex))
  308. {
  309. granny_texture_instance *TextureInstance =
  310. (granny_texture_instance *)GrannyGetMaterialTextureCookie(
  311. Material, TextureIndex);
  312. if(TextureInstance)
  313. {
  314. DX8Wrapper::Set_Texture(0, (TextureClass *)GrannyGetTextureCookie(TextureInstance));
  315. }
  316. else
  317. DX8Wrapper::Set_Texture(0, NULL);
  318. }
  319. }
  320. DynamicVBAccessClass vb_access(BUFFER_TYPE_DYNAMIC_DX8,dynamic_fvf_type,m_vertexCount);
  321. {
  322. DynamicVBAccessClass::WriteLockClass lock(&vb_access);
  323. VertexFormatXYZNDUV2* vb=lock.Get_Formatted_Vertex_Array();
  324. if(vb)
  325. {
  326. for (Int i=0; i<m_vertexCount; i++)
  327. {
  328. vb->x=Vertices->Position[0];
  329. vb->y=Vertices->Position[1];
  330. vb->z=Vertices->Position[2];
  331. vb->nx=Vertices->Normal[0];
  332. vb->ny=Vertices->Normal[1];
  333. vb->nz=Vertices->Normal[2];
  334. vb->diffuse=0xffffffff;
  335. vb->u1=Vertices->UV[0];
  336. vb->v1=Vertices->UV[1];
  337. vb++;
  338. Vertices++;
  339. }
  340. }
  341. }
  342. Int indexCount=GrannyGetMeshTriangleGroupIndexCount(Mesh, GroupIndex);
  343. UnsignedInt *indices=(UnsignedInt*)GrannyGetMeshTriangleGroupIndices(Mesh, GroupIndex);
  344. DynamicIBAccessClass ib_access(BUFFER_TYPE_DYNAMIC_DX8,indexCount);
  345. {
  346. DynamicIBAccessClass::WriteLockClass lock(&ib_access);
  347. unsigned short* ib=lock.Get_Index_Array();
  348. if(ib)
  349. {
  350. for (Int i=0; i<indexCount; i++)
  351. ib[i]=(UnsignedShort)indices[i];
  352. }
  353. }
  354. Matrix3D tm(Transform);
  355. DX8Wrapper::Set_Light_Environment(rinfo.light_environment);
  356. DX8Wrapper::Set_Material(m_prototype.m_vertexMaterial);
  357. DX8Wrapper::Set_Shader(ShaderClass::_PresetOpaqueShader);
  358. DX8Wrapper::Set_Transform(D3DTS_WORLD,tm);
  359. DX8Wrapper::Set_Index_Buffer(ib_access,0);
  360. DX8Wrapper::Set_Vertex_Buffer(vb_access);
  361. DX8Wrapper::Draw_Triangles( 0,indexCount/3, 0, m_vertexCount); //draw a quad, 2 triangles, 4 verts
  362. }}
  363. }
  364. }
  365. /**
  366. GrannyLoaderClass::Load -- reads in a granny model and creates a prototype for it
  367. */
  368. PrototypeClass * GrannyLoaderClass::Load_W3D(const char *filename)
  369. {
  370. char drive[_MAX_DRIVE],dir[_MAX_DIR],fname[_MAX_FNAME],ext[_MAX_EXT];
  371. granny_file *File = GrannyReadEntireFile(filename);
  372. granny_file_info *fileInfo;
  373. AABoxClass box;
  374. Int vertexCount=0;
  375. Int indexCount=0;
  376. Int meshCount=0;
  377. GrannyPrototypeClass::grannyMeshDesc meshData[GRANNY_MAX_MESHES_PER_MODEL];
  378. if(File)
  379. {
  380. fileInfo = GrannyGetFileInfo(File);
  381. if (fileInfo)
  382. {
  383. TransformFile(fileInfo);
  384. for(int TextureIndex = 0; TextureIndex < fileInfo->TextureCount; ++TextureIndex)
  385. {
  386. _splitpath(fileInfo->Textures[TextureIndex]->FromFileName, drive, dir, fname, ext );
  387. TextureClass *TextureHandle=WW3DAssetManager::Get_Instance()->Get_Texture(strncat(fname,".tga",4));
  388. granny_texture_instance *TextureInstance = GrannyInstantiateTexture(fileInfo->Textures[TextureIndex]);
  389. GrannySetTextureCookie(TextureInstance, (granny_uint32)TextureHandle);
  390. GrannyAddTextureToLibrary(TextureLibrary,TextureInstance);
  391. }
  392. //Calculate bounding box and find maximum number of vertices needed to render mesh.
  393. for (Int modelIndex=0; modelIndex<fileInfo->ModelCount; modelIndex++)
  394. {
  395. granny_model *sourceModel = fileInfo->Models[modelIndex];
  396. if (stricmp(sourceModel->Name,"AABOX") == 0)
  397. { //found a collision box, copy out data
  398. int MeshCount = sourceModel->MeshBindingCount;
  399. if (MeshCount==1)
  400. {
  401. granny_mesh *sourceMesh = sourceModel->MeshBindings[0].Mesh;
  402. granny_pn33_vertex *Vertices = (granny_pn33_vertex *)sourceMesh->PrimaryVertexData->Vertices;
  403. Vector3 points[24];
  404. assert (sourceMesh->PrimaryVertexData->VertexCount <= 24);
  405. for (Int boxVertex=0; boxVertex<sourceMesh->PrimaryVertexData->VertexCount; boxVertex++)
  406. { points[boxVertex].Set(Vertices[boxVertex].Position[0],
  407. Vertices[boxVertex].Position[1],
  408. Vertices[boxVertex].Position[2]);
  409. }
  410. box.Init(points,sourceMesh->PrimaryVertexData->VertexCount);
  411. }
  412. }
  413. else
  414. { //mesh is part of model
  415. meshCount = sourceModel->MeshBindingCount;
  416. for (Int meshIndex=0; meshIndex<meshCount; meshIndex++)
  417. {
  418. granny_mesh *sourceMesh = sourceModel->MeshBindings[meshIndex].Mesh;
  419. meshData[meshIndex].vertex=NULL;
  420. meshData[meshIndex].vertexCount=0;
  421. if (sourceMesh->PrimaryVertexData)
  422. { vertexCount+=sourceMesh->PrimaryVertexData->VertexCount;
  423. meshData[meshIndex].vertex=(granny_pnt332_vertex *)sourceMesh->PrimaryVertexData->Vertices;
  424. meshData[meshIndex].vertexCount=sourceMesh->PrimaryVertexData->VertexCount;
  425. }
  426. meshData[meshIndex].index=NULL;
  427. meshData[meshIndex].indexCount=NULL;
  428. if (sourceMesh->PrimaryTopology)
  429. {
  430. assert (sourceMesh->PrimaryTopology->GroupCount == 1); //should only have 1 material per mesh!
  431. granny_tri_material_group &sourceGroup = sourceMesh->PrimaryTopology->Groups[0];
  432. // This is the texture index (relative to the array we just
  433. // built) for this group of triangles
  434. // SourceGroup.MaterialIndex;
  435. // This is the number of indices
  436. if(sourceMesh->PrimaryTopology->IndexCount)
  437. {
  438. meshData[meshIndex].index=(const UnsignedInt*)&sourceMesh->PrimaryTopology->Indices[3*sourceGroup.TriFirst];
  439. // These are the indices for this group
  440. meshData[meshIndex].indexCount=3*sourceGroup.TriCount;
  441. indexCount += meshData[meshIndex].indexCount; //keep track of total indices in this model
  442. }
  443. }
  444. }
  445. }
  446. }
  447. ///@todo: Convert granny materials into W3D/D3D materials - now using default.
  448. // for(int MaterialIndex = 0; MaterialIndex < fileInfo->MaterialCount; ++MaterialIndex)
  449. // {
  450. // granny_material_instance *MaterialInstance = GrannyInstantiateMaterial(fileInfo->Materials[MaterialIndex]);
  451. // GrannyAutoBindAllMaterialTextures(MaterialInstance,TextureLibrary);
  452. // GrannyAddMaterialToLibrary(MaterialLibrary,MaterialInstance);
  453. // }
  454. GrannyAutoBindAllMaterials(MaterialLibrary, fileInfo,TextureLibrary);
  455. } //fileInfo
  456. if (fileInfo == NULL) {
  457. return NULL;
  458. }
  459. // ok, accept this model!
  460. GrannyPrototypeClass * hproto = NEW GrannyPrototypeClass(File);
  461. _splitpath(filename, drive, dir, fname, ext );
  462. hproto->Set_Name(strcat(fname,ext));
  463. hproto->setBoundingBox(box);
  464. hproto->setBoundingSphere(SphereClass(box.Center,box.Extent.Length()));
  465. hproto->setVertexCount(vertexCount);
  466. hproto->setIndexCount(indexCount);
  467. hproto->setMeshCount(meshCount);
  468. for (Int i=0; i<meshCount; i++)
  469. hproto->setMeshData(meshData[i],i);
  470. return hproto;
  471. }//FILE
  472. return NULL;
  473. }
  474. GrannyLoaderClass::GrannyLoaderClass(void)
  475. {
  476. TextureLibrary = GrannyNewTextureLibrary(1 << 12);
  477. MaterialLibrary = GrannyNewMaterialLibrary(1 << 12);
  478. }
  479. GrannyLoaderClass::~GrannyLoaderClass(void)
  480. {
  481. GrannyFreeTextureLibrary(TextureLibrary);
  482. GrannyFreeMaterialLibrary(MaterialLibrary);
  483. TextureLibrary=NULL;
  484. MaterialLibrary=NULL;
  485. }
  486. GrannyAnimManagerClass::GrannyAnimManagerClass(void)
  487. {
  488. // Create the hash tables
  489. AnimPtrTable = NEW HashTableClass( 2048 );
  490. MissingAnimTable = NEW HashTableClass( 2048 );
  491. }
  492. GrannyAnimManagerClass::~GrannyAnimManagerClass(void)
  493. {
  494. Free_All_Anims();
  495. delete AnimPtrTable;
  496. AnimPtrTable = NULL;
  497. delete MissingAnimTable;
  498. MissingAnimTable = NULL;
  499. }
  500. /** Release all loaded animations */
  501. void GrannyAnimManagerClass::Free_All_Anims(void)
  502. {
  503. // Make an iterator, and release all ptrs
  504. GrannyAnimManagerIterator it( *this );
  505. for( it.First(); !it.Is_Done(); it.Next() ) {
  506. GrannyAnimClass *anim = it.Get_Current_Anim();
  507. anim->Release_Ref();
  508. }
  509. // Then clear the table
  510. AnimPtrTable->Reset();
  511. }
  512. /** Find animation in cache */
  513. GrannyAnimClass * GrannyAnimManagerClass::Peek_Anim(const char * name)
  514. {
  515. return (GrannyAnimClass*)AnimPtrTable->Find( name );
  516. }
  517. /** Get animation from cache and increment its reference count */
  518. GrannyAnimClass * GrannyAnimManagerClass::Get_Anim(const char * name)
  519. {
  520. GrannyAnimClass * anim = Peek_Anim( name );
  521. if ( anim != NULL ) {
  522. anim->Add_Ref();
  523. }
  524. return anim;
  525. }
  526. /** Add animation to cache */
  527. Bool GrannyAnimManagerClass::Add_Anim(GrannyAnimClass *new_anim)
  528. {
  529. WWASSERT (new_anim != NULL);
  530. // Increment the refcount on the new animation and add it to our table.
  531. new_anim->Add_Ref ();
  532. AnimPtrTable->Add( new_anim );
  533. return true;
  534. }
  535. /*
  536. ** Missing Anims
  537. **
  538. ** The idea here, allow the system to register which anims are determined to be missing
  539. ** so that if they are asked for again, we can quickly return NULL, without searching the
  540. ** disk again.
  541. */
  542. void GrannyAnimManagerClass::Register_Missing( const char * name )
  543. {
  544. MissingAnimTable->Add( NEW MissingAnimClass( name ) );
  545. }
  546. Bool GrannyAnimManagerClass::Is_Missing( const char * name )
  547. {
  548. return ( MissingAnimTable->Find( name ) != NULL );
  549. }
  550. /** Load an animation from disk and add to cache */
  551. int GrannyAnimManagerClass::Load_Anim(const char * name)
  552. {
  553. GrannyAnimClass * newanim = NEW GrannyAnimClass;
  554. if (newanim == NULL) {
  555. goto Error;
  556. }
  557. SET_REF_OWNER( newanim );
  558. if (newanim->Load_W3D(name) != GrannyAnimClass::OK)
  559. { // load failed!
  560. newanim->Release_Ref();
  561. goto Error;
  562. } else if (Peek_Anim(newanim->Get_Name()) != NULL)
  563. { // duplicate exists!
  564. newanim->Release_Ref(); // Release the one we just loaded
  565. goto Error;
  566. } else
  567. { Add_Anim( newanim );
  568. newanim->Release_Ref();
  569. }
  570. return 0;
  571. Error:
  572. return 1;
  573. }
  574. /*
  575. ** Iterator converter from HashableClass to GrannyAnimClass
  576. */
  577. GrannyAnimClass * GrannyAnimManagerIterator::Get_Current_Anim( void )
  578. {
  579. return (GrannyAnimClass *)Get_Current();
  580. }
  581. GrannyAnimClass::GrannyAnimClass(void) :
  582. NumFrames(0),
  583. FrameRate(0),
  584. File(0),
  585. Animation(0)
  586. {
  587. Name[0]='\0';
  588. }
  589. /** GrannyAnimClass::~GrannyAnimClass -- Destructor */
  590. GrannyAnimClass::~GrannyAnimClass(void)
  591. {
  592. GrannyFreeFile(File);
  593. }
  594. /** Read granny data from disk */
  595. int GrannyAnimClass::Load_W3D(const char *name)
  596. {
  597. granny_file *file=NULL;
  598. file = GrannyReadEntireFile(name);
  599. if (file)
  600. {
  601. granny_file_info *fileInfo;
  602. fileInfo = GrannyGetFileInfo(file);
  603. if (fileInfo && fileInfo->AnimationCount)
  604. { Animation = fileInfo->Animations[0];
  605. File = file;
  606. strcpy(Name,name);
  607. FrameRate = 1.0f/Animation->TimeStep;
  608. NumFrames = Animation->Duration / Animation->TimeStep;
  609. return OK;
  610. }
  611. else
  612. GrannyFreeFile(File); //no animations found
  613. }
  614. return LOAD_ERROR;
  615. }
  616. GrannyLoaderClass _GrannyLoader;
  617. GrannyRenderObjSystem *TheGrannyRenderObjSystem=NULL;
  618. /** Adds render object to a queue with other objects using the same material. These queues will be
  619. flushed at the end of the frame.
  620. */
  621. void GrannyRenderObjSystem::AddRenderObject(RenderInfoClass & rinfo, GrannyRenderObjClass *robj)
  622. {
  623. if (m_renderObjectCount >= MAX_VISIBLE_GRANNY_MODELS)
  624. { assert (m_renderObjectCount < MAX_VISIBLE_GRANNY_MODELS);
  625. return; //can't add any more granny models this frame.
  626. }
  627. for (Int i=0; i<m_renderStateCount; i++)
  628. { //search for other objects with same render state
  629. //right now we can just sort by prototype since prototypes have fixed materials.
  630. if (m_renderStateModelList[i].list->m_prototype.m_file == robj->m_prototype.m_file)
  631. { //found model with same prototype. Add new model to same list.
  632. robj->m_nextSystem=m_renderStateModelList[i].list;
  633. m_renderStateModelList[i].list=robj;
  634. if (rinfo.light_environment)
  635. m_renderLocalLightEnv[m_renderObjectCount]=*rinfo.light_environment;
  636. m_renderObjectCount++;
  637. return;
  638. }
  639. }
  640. //Found a new render state
  641. assert (m_renderStateCount < MAX_GRANNY_RENDERSTATES);
  642. if (m_renderStateCount > MAX_GRANNY_RENDERSTATES)
  643. return; //reached maximum number of render states
  644. //store this objects lighting information for use later during drawing.
  645. if (rinfo.light_environment)
  646. m_renderLocalLightEnv[m_renderObjectCount]=*rinfo.light_environment;
  647. m_renderStateModelList[m_renderStateCount++].list=robj;
  648. robj->m_nextSystem=NULL;
  649. m_renderObjectCount++;
  650. }
  651. /** Draws all the granny render objects that were rendered in the last frame.
  652. Drawing is done in render state order to reduce overhead.
  653. */
  654. void GrannyRenderObjSystem::Flush(void)
  655. {
  656. GrannyRenderObjClass *robj;
  657. granny_model_instance *modelInstance;
  658. Bool setMaterial;
  659. Int modelCount=0;
  660. Int indexCount; //per model index count
  661. for (Int i=0; i<m_renderStateCount; i++)
  662. {
  663. robj=m_renderStateModelList[i].list; //set to start of objects using this material
  664. setMaterial=true; //force rendering code to apply material
  665. while (robj)
  666. {
  667. modelInstance=robj->m_modelInstance;
  668. //update the model
  669. GrannySetModelClock(modelInstance,robj->LastSyncTime);
  670. robj->LastSyncTime = WW3D::Get_Sync_Time()*0.001f; //convert to seconds
  671. GrannySampleModelAnimations(modelInstance, 0, GrannyGetModelBoneCount(modelInstance),
  672. GrannyGetModelLocalPose(modelInstance));
  673. GrannyBuildModelWorldTransforms(modelInstance, 0,
  674. GrannyGetModelBoneCount(modelInstance),
  675. GrannyGetModelLocalPose(modelInstance),
  676. GrannyGetModelRootTransform(modelInstance),
  677. GrannyGetModelWorldPose(modelInstance));
  678. Real *RootTransform = GrannyGetModelRootTransform(modelInstance);
  679. RootTransform[12] = 0; //X
  680. RootTransform[13] = 0; //Y
  681. RootTransform[13] = 0; //Z
  682. RootTransform[0] = robj->ObjectScale;
  683. RootTransform[5] = robj->ObjectScale;
  684. RootTransform[10] = robj->ObjectScale;
  685. Int MeshCount = GrannyGetModelMeshCount(modelInstance);
  686. for(Int MeshIndex = 0; MeshIndex < MeshCount; ++MeshIndex)
  687. {
  688. granny_mesh_instance *Mesh = (granny_mesh_instance *)GrannyGetModelMeshCookie(modelInstance, MeshIndex);
  689. granny_mesh_model_binding *Binding = GrannyGetMeshModelBinding(Mesh);
  690. granny_pnt332_vertex *Vertices = 0;
  691. if(GrannyMeshIsRigid(Mesh))
  692. { assert(0); //have not coded support for rigid meshes yet - only soft skinned supported.
  693. // granny_matrix_4x4 TempBuffer;
  694. // glMultMatrixf(GrannyGetRigidMeshTransform(
  695. // Binding, GrannyGetModelWorldPose(modelInstance),
  696. // (granny_real32 *)TempBuffer));
  697. Vertices = (granny_pnt332_vertex *)GrannyGetMeshVertices(Mesh);
  698. }
  699. else
  700. {
  701. GrannyDeformMesh(Binding, GrannyGetModelWorldPose(modelInstance),
  702. 0, g_blendingBuffer);
  703. Vertices = g_blendingBuffer;
  704. }
  705. int GroupCount = GrannyGetMeshTriangleGroupCount(Mesh);
  706. for(Int GroupIndex = 0; GroupIndex < GroupCount; ++GroupIndex)
  707. {
  708. granny_material_instance *Material = (granny_material_instance *)
  709. GrannyGetMeshMaterialCookie(
  710. Mesh, GrannyGetMeshTriangleGroupMaterialIndex(
  711. Mesh, GroupIndex));
  712. if(setMaterial && Material)
  713. {
  714. Int TextureIndex;
  715. if(GrannyGetMaterialTextureIndexByType(
  716. Material, GrannyDiffuseColorTexture, &TextureIndex))
  717. {
  718. granny_texture_instance *TextureInstance =
  719. (granny_texture_instance *)GrannyGetMaterialTextureCookie(
  720. Material, TextureIndex);
  721. if(TextureInstance)
  722. {
  723. DX8Wrapper::Set_Texture(0, (TextureClass *)GrannyGetTextureCookie(TextureInstance));
  724. }
  725. else
  726. DX8Wrapper::Set_Texture(0, NULL);
  727. }
  728. }
  729. DynamicVBAccessClass vb_access(BUFFER_TYPE_DYNAMIC_DX8,dynamic_fvf_type,robj->m_vertexCount);
  730. {
  731. DynamicVBAccessClass::WriteLockClass lock(&vb_access);
  732. VertexFormatXYZNDUV2* vb=lock.Get_Formatted_Vertex_Array();
  733. if(vb)
  734. {
  735. for (Int i=0; i<robj->m_vertexCount; i++)
  736. {
  737. vb->x=Vertices->Position[0];
  738. vb->y=Vertices->Position[1];
  739. vb->z=Vertices->Position[2];
  740. vb->nx=Vertices->Normal[0];
  741. vb->ny=Vertices->Normal[1];
  742. vb->nz=Vertices->Normal[2];
  743. vb->diffuse=0xffffffff;
  744. vb->u1=Vertices->UV[0];
  745. vb->v1=Vertices->UV[1];
  746. vb++;
  747. Vertices++;
  748. }
  749. }
  750. }
  751. if (setMaterial)
  752. { //we started using a new material queue - really a new model prototype
  753. //so reset all relevant data.
  754. indexCount=GrannyGetMeshTriangleGroupIndexCount(Mesh, GroupIndex);
  755. UnsignedInt *indices=(UnsignedInt*)GrannyGetMeshTriangleGroupIndices(Mesh, GroupIndex);
  756. DynamicIBAccessClass ib_access(BUFFER_TYPE_DYNAMIC_DX8,indexCount);
  757. {
  758. DynamicIBAccessClass::WriteLockClass lock(&ib_access);
  759. unsigned short* ib=lock.Get_Index_Array();
  760. if(ib)
  761. {
  762. for (Int i=0; i<indexCount; i++)
  763. ib[i]=(UnsignedShort)indices[i];
  764. }
  765. }
  766. DX8Wrapper::Set_Material(robj->m_prototype.m_vertexMaterial);
  767. DX8Wrapper::Set_Shader(ShaderClass::_PresetOpaqueShader);
  768. setMaterial=false; //don't set material again unless it changes.
  769. DX8Wrapper::Set_Index_Buffer(ib_access,0);
  770. }
  771. Matrix3D tm(robj->Transform);
  772. DX8Wrapper::Set_Light_Environment(&m_renderLocalLightEnv[modelCount]);
  773. DX8Wrapper::Set_Transform(D3DTS_WORLD,tm);
  774. DX8Wrapper::Set_Vertex_Buffer(vb_access);
  775. DX8Wrapper::Draw_Triangles( 0,indexCount/3, 0, robj->m_vertexCount); //draw a quad, 2 triangles, 4 verts
  776. }
  777. }
  778. robj=robj->m_nextSystem; //move to next object using this material
  779. modelCount++;
  780. }//while
  781. }
  782. //reset for next frame
  783. m_renderObjectCount=0;
  784. m_renderStateCount=0;
  785. }
  786. #if 0 ///@todo: Will have to implement an optimized granny rendering system
  787. //=============================================================================
  788. // GrannyRenderObjClassSystem::GrannyRenderObjClassSystem
  789. //=============================================================================
  790. /** Constructor. Just nulls out some variables. */
  791. //=============================================================================
  792. GrannyRenderObjClassSystem::GrannyRenderObjClassSystem()
  793. {
  794. m_usedModules = NULL;
  795. m_freeModules = NULL;
  796. m_indexBuffer = NULL;
  797. m_vertexMaterialClass = NULL;
  798. m_vertexBuffer = NULL;
  799. }
  800. //=============================================================================
  801. // GrannyRenderObjClassSystem::~GrannyRenderObjClassSystem
  802. //=============================================================================
  803. /** Destructor. Free all pre-allocated track laying render objects*/
  804. //=============================================================================
  805. GrannyRenderObjClassSystem::~GrannyRenderObjClassSystem( void )
  806. {
  807. // free all data
  808. shutdown();
  809. m_vertexMaterialClass=NULL;
  810. }
  811. //=============================================================================
  812. // GrannyRenderObjClassSystem::ReAcquireResources
  813. //=============================================================================
  814. /** (Re)allocates all W3D assets after a reset.. */
  815. //=============================================================================
  816. void GrannyRenderObjClassSystem::ReAcquireResources(void)
  817. {
  818. // just for paranoia's sake.
  819. REF_PTR_RELEASE(m_indexBuffer);
  820. REF_PTR_RELEASE(m_vertexBuffer);
  821. //Create static index buffers. These will index the vertex buffers holding the map.
  822. m_indexBuffer=NEW_REF(DX8IndexBufferClass,(32*6));
  823. // Fill up the IB
  824. {
  825. DX8IndexBufferClass::WriteLockClass lockIdxBuffer(m_indexBuffer);
  826. UnsignedShort *ib=lockIdxBuffer.Get_Index_Array();
  827. }
  828. ///@todo: Allocating double sized buffer than really needed... but things go bad otherwise. Figure out why!
  829. m_vertexBuffer=NEW_REF(DX8VertexBufferClass,(DX8_FVF_XYZDUV1,1*2,DX8VertexBufferClass::USAGE_DYNAMIC));
  830. }
  831. //=============================================================================
  832. // GrannyRenderObjClassSystem::ReleaseResources
  833. //=============================================================================
  834. /** (Re)allocates all W3D assets after a reset.. */
  835. //=============================================================================
  836. void GrannyRenderObjClassSystem::ReleaseResources(void)
  837. {
  838. REF_PTR_RELEASE(m_indexBuffer);
  839. REF_PTR_RELEASE(m_vertexBuffer);
  840. // Note - it is ok to not release the material, as it is a w3d object that
  841. // has no dx8 resources. jba.
  842. }
  843. //=============================================================================
  844. // GrannyRenderObjClassSystem::init
  845. //=============================================================================
  846. /** initialize the system, allocate all the render objects we will need */
  847. //=============================================================================
  848. void GrannyRenderObjClassSystem::init()
  849. {
  850. const Int numModules=TheGlobalData->m_maxTerrainTracks;
  851. Int i;
  852. GrannyRenderObjClass *mod;
  853. ReAcquireResources();
  854. //go with a preset material for now.
  855. m_vertexMaterialClass=VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_DIFFUSE);
  856. //use a multi-texture shader: (text1*diffuse)*text2.
  857. m_shaderClass = ShaderClass::_PresetAlphaShader;//_PresetATestSpriteShader;//_PresetOpaqueShader;
  858. // we cannot initialize a system that is already initialized
  859. if( m_freeModules || m_usedModules )
  860. {
  861. // system already online!
  862. assert( 0 );
  863. return;
  864. } // end if
  865. // allocate our modules for this system
  866. for( i = 0; i < numModules; i++ )
  867. {
  868. mod = NEW_REF( GrannyRenderObjClass, () );
  869. if( mod == NULL )
  870. {
  871. // unable to allocate modules needed
  872. assert( 0 );
  873. return;
  874. } // end if
  875. mod->m_prevSystem = NULL;
  876. mod->m_nextSystem = m_freeModules;
  877. if( m_freeModules )
  878. m_freeModules->m_prevSystem = mod;
  879. m_freeModules = mod;
  880. } // end for i
  881. } // end init
  882. //=============================================================================
  883. // GrannyRenderObjClassSystem::shutdown
  884. //=============================================================================
  885. /** Shutdown and free all memory for this system */
  886. //=============================================================================
  887. void GrannyRenderObjClassSystem::shutdown( void )
  888. {
  889. REF_PTR_RELEASE(m_indexBuffer);
  890. REF_PTR_RELEASE(m_vertexMaterialClass);
  891. REF_PTR_RELEASE(m_vertexBuffer);
  892. } // end shutdown
  893. //=============================================================================
  894. // GrannyRenderObjClassSystem::update
  895. //=============================================================================
  896. /** Update the state of all active track marks - fade, expire, etc. */
  897. //=============================================================================
  898. void GrannyRenderObjClassSystem::update()
  899. {
  900. Int iTime=timeGetTime();
  901. }
  902. //=============================================================================
  903. // GrannyRenderObjClassSystem::flush
  904. //=============================================================================
  905. /** Draw all active track marks for this frame */
  906. //=============================================================================
  907. void GrannyRenderObjClassSystem::flush()
  908. {
  909. Int diffuseLight;
  910. GrannyRenderObjClass *mod=0;
  911. // adjust shading for time of day.
  912. Real shadeR, shadeG, shadeB;
  913. shadeR = TheGlobalData->m_terrainAmbientRed;
  914. shadeG = TheGlobalData->m_terrainAmbientGreen;
  915. shadeB = TheGlobalData->m_terrainAmbientBlue;
  916. shadeR += TheGlobalData->m_terrainDiffuseRed/2;
  917. shadeG += TheGlobalData->m_terrainDiffuseGreen/2;
  918. shadeB += TheGlobalData->m_terrainDiffuseBlue/2;
  919. shadeR*=255.0f;
  920. shadeG*=255.0f;
  921. shadeB*=255.0f;
  922. diffuseLight=(int)shadeB | ((int)shadeG << 8) | ((int)shadeR << 16);
  923. //check if there is anything to draw and fill vertex buffer
  924. {
  925. DX8VertexBufferClass::WriteLockClass lockVtxBuffer(m_vertexBuffer);
  926. VertexFormatXYZDUV1 *verts = (VertexFormatXYZDUV1*)lockVtxBuffer.Get_Vertex_Array();
  927. }//edges to flush
  928. //draw the filled vertex buffers
  929. {
  930. ShaderClass::Invalidate();
  931. DX8Wrapper::Set_Material(m_vertexMaterialClass);
  932. DX8Wrapper::Set_Shader(m_shaderClass);
  933. DX8Wrapper::Set_Index_Buffer(m_indexBuffer,0);
  934. DX8Wrapper::Set_Vertex_Buffer(m_vertexBuffer);
  935. Matrix3D tm(mod->Transform);
  936. DX8Wrapper::Set_Transform(D3DTS_WORLD,tm);
  937. DX8Wrapper::Set_Index_Buffer_Index_Offset(0);
  938. DX8Wrapper::Draw_Triangles( 0,2, 0, 1*2);
  939. } //there are some edges to render in pool.
  940. }
  941. //=============================================================================
  942. // GrannyRenderObjClassSystem::createRenderObj
  943. //=============================================================================
  944. /** Creates an instance of a W3D render object using the specified granny */
  945. /* model. If the model doesn't exist yet, it will be loaded from disk. */
  946. //=============================================================================
  947. RenderObjClass *GrannyRenderObjClassSystem::createRenderObj(const char * name)
  948. {
  949. return NULL;
  950. }
  951. GrannyRenderObjClassSystem *TheGrannyRenderObjClassSystem=NULL; ///< singleton for track drawing system.
  952. #endif //end of GrannyRenderObjClassSystem
  953. #endif //INCLUDE_GRANNY_IN_BUILD