PVRTFixedPoint.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /******************************************************************************
  2. @File PVRTFixedPoint.cpp
  3. @Title PVRTFixedPoint
  4. @Version
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform Independant
  7. @Description Converts MAX exported meshes to fixed point objects for use with
  8. opengles lite.
  9. ******************************************************************************/
  10. #include <math.h>
  11. #include <string.h>
  12. #include "PVRTContext.h"
  13. #include "PVRTFixedPoint.h"
  14. /********************************************************
  15. ** Most of the code only applies to CommonLite profile **
  16. ********************************************************/
  17. #ifdef PVRT_FIXED_POINT_ENABLE
  18. /*!***************************************************************************
  19. @Function CreateFixedObjectMesh
  20. @Input mesh The mesh to create the fixed point version from
  21. @Returns A fixed point version of mesh
  22. @Description Converts model floating point data to fixed point
  23. *****************************************************************************/
  24. HeaderStruct_Fixed_Mesh *CreateFixedObjectMesh(HeaderStruct_Mesh *mesh)
  25. {
  26. HeaderStruct_Fixed_Mesh *new_mesh = new HeaderStruct_Fixed_Mesh;
  27. new_mesh->fCenter[0] = PVRTF2X(mesh->fCenter[0]);
  28. new_mesh->fCenter[1] = PVRTF2X(mesh->fCenter[1]);
  29. new_mesh->fCenter[2] = PVRTF2X(mesh->fCenter[2]);
  30. new_mesh->nNumVertex = mesh->nNumVertex;
  31. new_mesh->nNumFaces = mesh->nNumFaces;
  32. new_mesh->nNumStrips = mesh->nNumStrips;
  33. new_mesh->nMaterial = mesh->nMaterial;
  34. if(mesh->nNumVertex)
  35. {
  36. new_mesh->pVertex = new VERTTYPE[mesh->nNumVertex*3];
  37. for(unsigned int i = 0; i < mesh->nNumVertex*3; i++) // each vertex is 3 floats
  38. new_mesh->pVertex[i] = PVRTF2X(mesh->pVertex[i]);
  39. }
  40. else
  41. {
  42. new_mesh->pVertex = 0;
  43. new_mesh->nNumVertex = 0;
  44. }
  45. if(mesh->pUV)
  46. {
  47. new_mesh->pUV = new VERTTYPE[mesh->nNumVertex*2];
  48. for(unsigned int i = 0; i < mesh->nNumVertex*2; i++) // UVs come in pairs of floats
  49. new_mesh->pUV[i] = PVRTF2X(mesh->pUV[i]);
  50. }
  51. else
  52. new_mesh->pUV = 0;
  53. if(mesh->pNormals)
  54. {
  55. new_mesh->pNormals = new VERTTYPE[mesh->nNumVertex*3];
  56. for(unsigned int i = 0; i < mesh->nNumVertex*3; i++) // each normal is 3 floats
  57. new_mesh->pNormals[i] = PVRTF2X(mesh->pNormals[i]);
  58. }
  59. else
  60. {
  61. new_mesh->pNormals = 0;
  62. }
  63. /*
  64. * Format of packedVerts is
  65. * Position
  66. * Normal / Colour
  67. * UVs
  68. */
  69. #define MF_NORMALS 1
  70. #define MF_VERTEXCOLOR 2
  71. #define MF_UV 3
  72. if(mesh->pPackedVertex)
  73. {
  74. unsigned int nPackedVertSize = mesh->nNumVertex * 3 +
  75. (mesh->nFlags & MF_NORMALS ? mesh->nNumVertex * 3 : 0) +
  76. (mesh->nFlags & MF_VERTEXCOLOR ? mesh->nNumVertex * 3 : 0) +
  77. (mesh->nFlags & MF_UV ? mesh->nNumVertex * 2 : 0);
  78. new_mesh->pPackedVertex = new VERTTYPE[nPackedVertSize];
  79. for(unsigned int i = 0; i < nPackedVertSize; i++)
  80. new_mesh->pPackedVertex[i] = PVRTF2X(mesh->pPackedVertex[i]);
  81. }
  82. else
  83. new_mesh->pPackedVertex = 0;
  84. // simply copy reference to all properties which do not need conversion (indicies)
  85. new_mesh->pVertexColor = mesh->pVertexColor;
  86. new_mesh->pVertexMaterial = mesh->pVertexMaterial;
  87. new_mesh->pFaces = mesh->pFaces;
  88. new_mesh->pStrips = mesh->pStrips;
  89. new_mesh->pStripLength = mesh->pStripLength;
  90. // we're leaving the patch stuff alone
  91. new_mesh->Patch.nType = mesh->Patch.nType;
  92. new_mesh->Patch.nNumPatches = mesh->Patch.nNumPatches;
  93. new_mesh->Patch.nNumVertices = mesh->Patch.nNumVertices;
  94. new_mesh->Patch.nNumSubdivisions = mesh->Patch.nNumSubdivisions;
  95. new_mesh->Patch.pControlPoints = mesh->Patch.pControlPoints;
  96. new_mesh->Patch.pUVs = mesh->Patch.pUVs;
  97. return new_mesh;
  98. }
  99. /*!***************************************************************************
  100. @Function FreeFixedObjectMesh
  101. @Input mesh The mesh to delete
  102. @Description Release memory allocated in CreateFixedObjectMesh()
  103. *****************************************************************************/
  104. void FreeFixedObjectMesh(HeaderStruct_Fixed_Mesh* mesh)
  105. {
  106. delete[] mesh->pVertex;
  107. delete[] mesh->pUV;
  108. delete[] mesh->pNormals;
  109. delete[] mesh->pPackedVertex;
  110. delete mesh;
  111. }
  112. #endif
  113. /*!***************************************************************************
  114. @Function PVRTLoadHeaderObject
  115. @Input headerObj Pointer to object structure in the header file
  116. @Return directly usable geometry in fixed or float format as appropriate
  117. @Description Converts the data exported by MAX to fixed point when used in OpenGL
  118. ES common-lite profile.
  119. *****************************************************************************/
  120. HeaderStruct_Mesh_Type *PVRTLoadHeaderObject(const void *headerObj)
  121. {
  122. #ifdef PVRT_FIXED_POINT_ENABLE
  123. return (HeaderStruct_Mesh_Type*) CreateFixedObjectMesh((HeaderStruct_Mesh *) headerObj);
  124. #else
  125. HeaderStruct_Mesh_Type *new_mesh = new HeaderStruct_Mesh_Type;
  126. memcpy (new_mesh,headerObj,sizeof(HeaderStruct_Mesh_Type));
  127. return (HeaderStruct_Mesh_Type*) new_mesh;
  128. #endif
  129. }
  130. /*!***************************************************************************
  131. @Function PVRTUnloadHeaderObject
  132. @Input headerObj Pointer returned by LoadHeaderObject
  133. @Description Releases memory allocated by LoadHeaderObject when geometry no longer
  134. needed.
  135. *****************************************************************************/
  136. void PVRTUnloadHeaderObject(HeaderStruct_Mesh_Type* headerObj)
  137. {
  138. #ifdef PVRT_FIXED_POINT_ENABLE
  139. FreeFixedObjectMesh(headerObj);
  140. #else
  141. delete headerObj;
  142. #endif
  143. }
  144. /*****************************************************************************
  145. End of file (PVRTFixedPoint.cpp)
  146. *****************************************************************************/