1 | /*----------------------------------------------------------------------
|
---|
2 | This Software and Related Documentation are Proprietary to Ageia
|
---|
3 | Technologies, Inc.
|
---|
4 |
|
---|
5 | Copyright 2005 Ageia Technologies, Inc. St. Louis, MO
|
---|
6 | Unpublished -
|
---|
7 | All Rights Reserved Under the Copyright Laws of the United States.
|
---|
8 |
|
---|
9 | Restricted Rights Legend: Use, Duplication, or Disclosure by
|
---|
10 | the Government is Subject to Restrictions as Set Forth in
|
---|
11 | Paragraph (c)(1)(ii) of the Rights in Technical Data and
|
---|
12 | Computer Software Clause at DFARS 252.227-7013. Ageia
|
---|
13 | Technologies Inc.
|
---|
14 | -----------------------------------------------------------------------*/
|
---|
15 |
|
---|
16 | #ifndef FW_MESH_COOKER_H
|
---|
17 | #define FW_MESH_COOKER_H 1
|
---|
18 |
|
---|
19 | #include "NxPhysics.h"
|
---|
20 |
|
---|
21 | class ConvexMesh;
|
---|
22 | void makeFwConvex(NxU8 *addr, const ConvexMesh *mesh);
|
---|
23 |
|
---|
24 | #ifdef DEFINE_CONVEX_COOKER
|
---|
25 | //#include "PhysicsSDK.h"
|
---|
26 | #include "Physics.h"
|
---|
27 | #include "TriangleMesh.h"
|
---|
28 | #include "TriangleMeshShape.h"
|
---|
29 | #include "ConvexMesh.h"
|
---|
30 | #include "ConvexShape.h"
|
---|
31 | #include "Scene.h"
|
---|
32 | // #include "Stream.h"
|
---|
33 | #include "ConvexHull.h"
|
---|
34 | #include "ConvexMesh.h"
|
---|
35 | #include "fw-mesh-utils.h"
|
---|
36 | #include "fw-convex.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | // convex is required to be at least MAX_FWCONVEX_SIZE
|
---|
40 |
|
---|
41 | void makeFwConvex(NxU8 *convex, const ConvexMesh *mesh)
|
---|
42 | {
|
---|
43 | NxU32 i,j;
|
---|
44 |
|
---|
45 | NxU32 nVerts = ((ConvexMeshRuntime *)mesh)->GetNbVerts();
|
---|
46 | NxU32 nFaces = ((ConvexMeshRuntime *)mesh)->GetNbPolygons();
|
---|
47 | NxU32 nEdges = nVerts + nFaces - 2;
|
---|
48 |
|
---|
49 | ASSERT(nVerts <= FW_MAX_CONVEX_VERT);
|
---|
50 | ASSERT(nFaces <= FW_MAX_CONVEX_FACE);
|
---|
51 |
|
---|
52 | // Allocate memory
|
---|
53 |
|
---|
54 | fw_convex_shape *shape = (fw_convex_shape *) convex;
|
---|
55 | shape->numVerts = nVerts;
|
---|
56 | shape->numFaces = nFaces;
|
---|
57 |
|
---|
58 | shape->centroid = FwV3(0,0,0);
|
---|
59 | FwV3 bbMin = FwV3(FLT_MAX,FLT_MAX,FLT_MAX);
|
---|
60 | FwV3 bbMax = FwV3(-FLT_MAX,-FLT_MAX,-FLT_MAX);
|
---|
61 |
|
---|
62 | // Copy verts
|
---|
63 |
|
---|
64 | FwV3 *verts = getConvexVerts(convex);
|
---|
65 | for (i = 0; i < nVerts; i++)
|
---|
66 | {
|
---|
67 | verts[i] = *(FwV3 *)&(((ConvexMeshRuntime *)mesh)->GetVerts()[i]);
|
---|
68 | shape->centroid += verts[i];
|
---|
69 | bbMin = bbMin.min(verts[i]);
|
---|
70 | bbMax = bbMax.max(verts[i]);
|
---|
71 | }
|
---|
72 |
|
---|
73 | shape->centroid /= (NxReal)nVerts;
|
---|
74 | shape->bbCentre = (bbMin + bbMax)/2;
|
---|
75 | shape->bbRadius = (bbMax - bbMin)/2;
|
---|
76 |
|
---|
77 | fw_convex_face *faces = getConvexFaces(convex);
|
---|
78 | fw_convex_loop *loops = getConvexLoops(convex);
|
---|
79 |
|
---|
80 | // Copy faces
|
---|
81 |
|
---|
82 | NxU32 loopIndex = 0;
|
---|
83 | for (i = 0; i < nFaces; i++)
|
---|
84 | {
|
---|
85 | const HullPolygon & Poly = ((ConvexMeshRuntime *)mesh)->GetPolygon(i);
|
---|
86 |
|
---|
87 | faces[i].plane.n = *(FwV3 *)&Poly.mPlane.n;
|
---|
88 | faces[i].plane.d = Poly.mPlane.d;
|
---|
89 | faces[i].loopIndex = loopIndex;
|
---|
90 | faces[i].numVerts = Poly.mNbVerts;
|
---|
91 |
|
---|
92 | FwV3 fn = (verts[Poly.mVRef[1]]-verts[Poly.mVRef[0]]).cross(verts[Poly.mVRef[2]]-verts[Poly.mVRef[1]]);
|
---|
93 |
|
---|
94 |
|
---|
95 | // Copy loops such that cross product of successive faces is always outward facing
|
---|
96 |
|
---|
97 | if(fn.dot(faces[i].plane.n)>0)
|
---|
98 | {
|
---|
99 | for (j = 0; j < Poly.mNbVerts; j++)
|
---|
100 | {
|
---|
101 | loops[loopIndex].vIndex = Poly.mVRef[j];
|
---|
102 | loops[loopIndex].eIndex = Poly.mERef[j]; // TODO: cull parallel edges
|
---|
103 | loopIndex++;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | for (j = 0; j < Poly.mNbVerts; j++)
|
---|
109 | {
|
---|
110 | loops[loopIndex].vIndex = Poly.mVRef[Poly.mNbVerts-1-j];
|
---|
111 | loops[loopIndex].eIndex = Poly.mERef[Poly.mNbVerts-1-j]; // TODO: cull parallel edges
|
---|
112 | loopIndex++;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | NxReal normErr = 0;
|
---|
118 | for(i=0;i<nFaces;i++)
|
---|
119 | {
|
---|
120 | fw_convex_loop *loop = loops + faces[i].loopIndex;
|
---|
121 | NxU8 v0 = loop[faces[i].numVerts-1].vIndex;
|
---|
122 | NxU8 v1;
|
---|
123 |
|
---|
124 | for(j=0; j<faces[i].numVerts; j++, v0=v1)
|
---|
125 | {
|
---|
126 | v1 = loop[j].vIndex;
|
---|
127 | FwV3 e = (verts[v1]-verts[v0]).unit();
|
---|
128 | NxReal k = fabs(faces[i].plane.n.dot(e));
|
---|
129 | if(k>normErr)
|
---|
130 | normErr = k;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | // printf("********** Normal error %f\n",normErr);
|
---|
135 |
|
---|
136 |
|
---|
137 | /* fill in per-edge face info - somewhat clunky */
|
---|
138 |
|
---|
139 | NxU8 *edgeTable = (NxU8 *) _alloca(nEdges * 2 * 3);
|
---|
140 | NxU8 *edgePtr = edgeTable;
|
---|
141 |
|
---|
142 | for(i=0;i<nFaces;i++)
|
---|
143 | {
|
---|
144 | fw_convex_loop *loop = loops + faces[i].loopIndex;
|
---|
145 | NxU8 v0 = loop[faces[i].numVerts-1].vIndex;
|
---|
146 | NxU8 v1;
|
---|
147 |
|
---|
148 | for(j=0; j<faces[i].numVerts; j++, v0=v1)
|
---|
149 | {
|
---|
150 | v1 = loop[j].vIndex;
|
---|
151 | *edgePtr++ = v0;
|
---|
152 | *edgePtr++ = v1;
|
---|
153 | *edgePtr++ = i;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | for(i=0;i<nFaces;i++)
|
---|
159 | {
|
---|
160 | fw_convex_loop *loop = loops + faces[i].loopIndex;
|
---|
161 | NxU8 v0 = loop[faces[i].numVerts-1].vIndex;
|
---|
162 | NxU8 v1;
|
---|
163 |
|
---|
164 | for(j=0; j<faces[i].numVerts; j++, v0=v1)
|
---|
165 | {
|
---|
166 | NxU32 k;
|
---|
167 | v1 = loop[j].vIndex;
|
---|
168 | for(k=0;k<nEdges*2 && !(edgeTable[k*3]==v1 && edgeTable[k*3+1]==v0); k++);
|
---|
169 | assert(k!=nEdges*2);
|
---|
170 | loop[j].adjFace = edgeTable[k*3+2];
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | {
|
---|
175 | shape->ofsLoops = (sizeof(fw_convex_shape) + sizeof(fw_convex_face) * nFaces) >> 2;
|
---|
176 | shape->ofsVerts = shape->ofsLoops +
|
---|
177 | ((sizeof(fw_convex_loop) * nEdges * 2) >> 2);
|
---|
178 | shape->cSize = shape->ofsVerts + ((12 * shape->numVerts) >> 2);
|
---|
179 | shape->cSize = (shape->cSize + 7) & ~7;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | #endif
|
---|
184 | #endif
|
---|
185 |
|
---|
186 |
|
---|
187 | //AGCOPYRIGHTBEGIN
|
---|
188 | ///////////////////////////////////////////////////////////////////////////
|
---|
189 | // Copyright © 2005 AGEIA Technologies.
|
---|
190 | // All rights reserved. www.ageia.com
|
---|
191 | ///////////////////////////////////////////////////////////////////////////
|
---|
192 | //AGCOPYRIGHTEND
|
---|
193 |
|
---|