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