1 /*RWH moved to a seperate file*/
2 
3 
4 #include "3dc.h"
5 #include "module.h"
6 #include "inline.h"
7 
8 #include "gameplat.h"
9 #include "gamedef.h"
10 
11 
12 #include "dynblock.h"
13 #include "dynamics.h"
14 #define UseLocalAssert No
15 #include "ourasert.h"
16 
17 
18 /*						   *
19 						   *
20 						   *
21 						*  *  *
22 						 * * *
23 						  ***
24                            *
25 */
26 /*KJL***********************************************
27 * Polygon Access Functions V1.0, 18:12:27 11/07/96 *
28 ***********************************************KJL*/
29 
30 int SetupPolygonAccess(DISPLAYBLOCK *objectPtr);
31 void AccessNextPolygon(void);
32 void GetPolygonVertices(struct ColPolyTag *polyPtr);
33 void GetPolygonNormal(struct ColPolyTag *polyPtr);
34 int SetupPolygonAccessFromShapeIndex(int shapeIndex);
35 
36 
37 /* the following are needed for morphing support */
38 #if SupportMorphing
39 extern MORPHDISPLAY MorphDisplay;
40 extern VECTORCH MorphedPts[];
41 #endif
42 
43 VECTORCH *ShapePointsPtr;
44 int *ShapeNormalsPtr;
45 int *Shape2NormalsPtr;
46 char ShapeIsMorphed;
47 int **ItemArrayPtr;
48 POLYHEADER *PolyheaderPtr;
49 
SetupPolygonAccess(DISPLAYBLOCK * objectPtr)50 int SetupPolygonAccess(DISPLAYBLOCK *objectPtr)
51 {
52 	SHAPEHEADER *shape1Ptr;
53 
54 	#if SupportMorphing
55   	if (objectPtr->ObMorphCtrl) /* morphable object? */
56 	{
57 		VECTORCH *shape1PointsPtr;
58 		VECTORCH *shape2PointsPtr;
59 
60 		/* Set up the morph data */
61 		GetMorphDisplay(&MorphDisplay, objectPtr);
62 
63 		shape1Ptr = MorphDisplay.md_sptr1;
64 
65 		if(MorphDisplay.md_lerp == 0x0000)
66 		{
67 
68 			ShapePointsPtr = (VECTORCH *)*shape1Ptr->points;
69 			ShapeNormalsPtr = (int *) *(shape1Ptr->sh_normals);
70 			ShapeIsMorphed=0;
71 
72 		}
73 		else if(MorphDisplay.md_lerp == 0xffff)
74 		{
75 			SHAPEHEADER *shape2Ptr;
76 			shape2Ptr = MorphDisplay.md_sptr2;
77 
78 			ShapePointsPtr = (VECTORCH *)*shape2Ptr->points;
79 			ShapeNormalsPtr = (int *) *(shape2Ptr->sh_normals);
80 			ShapeIsMorphed=0;
81 		}
82 		else
83 		{
84 			SHAPEHEADER *shape2Ptr;
85 			shape2Ptr = MorphDisplay.md_sptr2;
86 
87 			shape1PointsPtr = (VECTORCH *)(*shape1Ptr->points);
88 			shape2PointsPtr = (VECTORCH *)(*shape2Ptr->points);
89 
90 			/* you're going to need all the points so you might as well morph them all at once now */
91 			{
92 		    	int numberOfPoints = shape1Ptr->numpoints;
93 				VECTORCH *morphedPointsPtr = (VECTORCH *) MorphedPts;
94 
95 				while(numberOfPoints--)
96 				{
97 				   	VECTORCH vertex1 = *shape1PointsPtr;
98 				   	VECTORCH vertex2 = *shape2PointsPtr;
99 
100 					if( (vertex1.vx == vertex2.vx && vertex1.vy == vertex2.vy && vertex1.vz == vertex2.vz) )
101 					{
102 						*morphedPointsPtr = vertex1;
103 					}
104 					else
105 					{
106 						/* KJL 15:27:20 05/22/97 - I've changed this to speed things up, If a vertex
107 						component has a magnitude greater than 32768 things will go wrong. */
108 						morphedPointsPtr->vx = vertex1.vx + (((vertex2.vx-vertex1.vx)*MorphDisplay.md_lerp)>>16);
109 						morphedPointsPtr->vy = vertex1.vy + (((vertex2.vy-vertex1.vy)*MorphDisplay.md_lerp)>>16);
110 						morphedPointsPtr->vz = vertex1.vz + (((vertex2.vz-vertex1.vz)*MorphDisplay.md_lerp)>>16);
111 					}
112 
113 		            shape1PointsPtr++;
114 		            shape2PointsPtr++;
115 					morphedPointsPtr++;
116 				}
117 			}
118 
119 		    ShapePointsPtr = (VECTORCH *)MorphedPts;
120 			ShapeNormalsPtr = (int *) *(shape1Ptr->sh_normals);
121 			Shape2NormalsPtr = (int *) *(shape2Ptr->sh_normals);
122 			ShapeIsMorphed=1;
123 		}
124 		ItemArrayPtr = (int **)shape1Ptr->items;
125 	}
126   	else /* not a morphing object */
127   	#endif
128   	{
129 		shape1Ptr = GetShapeData(objectPtr->ObShape);
130 
131 		ShapePointsPtr  = (VECTORCH *)(*shape1Ptr->points);
132 		ShapeNormalsPtr = (int *)(*shape1Ptr->sh_normals);
133 		ItemArrayPtr = (int **)shape1Ptr->items;
134         ShapeIsMorphed=0;
135 	}
136 
137 	{
138 		int *itemPtr = *ItemArrayPtr;
139 		PolyheaderPtr = (POLYHEADER *) itemPtr;
140 	}
141 
142     return shape1Ptr->numitems;
143 }
AccessNextPolygon(void)144 void AccessNextPolygon(void)
145 {
146 	int *itemPtr = *(ItemArrayPtr++);
147 	PolyheaderPtr = (POLYHEADER *) itemPtr;
148     return;
149 }
150 
GetPolygonVertices(struct ColPolyTag * polyPtr)151 void GetPolygonVertices(struct ColPolyTag *polyPtr)
152 {
153 	int *vertexNumberPtr = &PolyheaderPtr->Poly1stPt;
154 
155   	polyPtr->PolyPoint[0] = *(ShapePointsPtr + *vertexNumberPtr++);
156     polyPtr->PolyPoint[1] = *(ShapePointsPtr + *vertexNumberPtr++);
157     polyPtr->PolyPoint[2] = *(ShapePointsPtr + *vertexNumberPtr++);
158 
159 	if (*vertexNumberPtr != Term)
160 	{
161 	    polyPtr->PolyPoint[3] = *(ShapePointsPtr + *vertexNumberPtr);
162 	   	polyPtr->NumberOfVertices=4;
163 	}
164 	else
165 	{
166 	   	polyPtr->NumberOfVertices=3;
167 	}
168 
169     return;
170 }
GetPolygonNormal(struct ColPolyTag * polyPtr)171 void GetPolygonNormal(struct ColPolyTag *polyPtr)
172 {
173 	if (ShapeIsMorphed)
174 	{
175 		VECTORCH n1Ptr = *(VECTORCH*)(ShapeNormalsPtr + PolyheaderPtr->PolyNormalIndex);
176 		VECTORCH n2Ptr = *(VECTORCH*)(Shape2NormalsPtr + PolyheaderPtr->PolyNormalIndex);
177 
178 		if( ((n1Ptr.vx == n2Ptr.vx)
179 		  && (n1Ptr.vy == n2Ptr.vy)
180 		  && (n1Ptr.vz == n2Ptr.vz))
181 		  || (MorphDisplay.md_lerp == 0) )
182 		{
183 			polyPtr->PolyNormal = n1Ptr;
184 		}
185 		else if(MorphDisplay.md_lerp == 0xffff)
186 		{
187 			polyPtr->PolyNormal = n2Ptr;
188 		}
189 		else
190 		{
191 			VECTORCH *pointPtr[3];
192  			int *vertexNumPtr = &PolyheaderPtr->Poly1stPt;
193 
194 			pointPtr[0] = (ShapePointsPtr + *vertexNumPtr++);
195 			pointPtr[1] = (ShapePointsPtr + *vertexNumPtr++);
196 			pointPtr[2] = (ShapePointsPtr + *vertexNumPtr);
197 
198 			MakeNormal
199 			(
200 				pointPtr[0],
201 				pointPtr[1],
202 				pointPtr[2],
203 				&polyPtr->PolyNormal
204 			);
205 		}
206 	}
207     else /* not morphed */
208     {
209      	polyPtr->PolyNormal = *(VECTORCH*)(ShapeNormalsPtr + PolyheaderPtr->PolyNormalIndex);
210 
211     	/* KJL 20:55:36 05/14/97 - turned off for alpha */
212     	#if 0
213     	if(	(polyPtr->PolyNormal.vx==0)
214 		  &&(polyPtr->PolyNormal.vy==0)
215 		  &&(polyPtr->PolyNormal.vz==0) )
216 		{
217 			textprint("shape data has zero normal\n");
218 		}
219 		#endif
220 
221     }
222     return;
223 }
224 
225 /*-----------------------Patrick 1/12/96--------------------------
226   I have added this function to initialise polygon access for a
227   module based on the shape index specified in its mapblock....
228   Specifically, this is for setting up location data for modules
229   during game initialisation, and so doesn't support morphing.
230   ----------------------------------------------------------------*/
SetupPolygonAccessFromShapeIndex(int shapeIndex)231 int SetupPolygonAccessFromShapeIndex(int shapeIndex)
232 {
233 	SHAPEHEADER *shape1Ptr;
234 
235 	shape1Ptr = GetShapeData(shapeIndex);
236 	ShapePointsPtr  = (VECTORCH *)(*shape1Ptr->points);
237 	ShapeNormalsPtr = (int *)(*shape1Ptr->sh_normals);
238 	ItemArrayPtr = (int **)shape1Ptr->items;
239     ShapeIsMorphed=0;
240 
241 	{
242 		int *itemPtr = *ItemArrayPtr;
243 		PolyheaderPtr = (POLYHEADER *) itemPtr;
244 	}
245 
246     return shape1Ptr->numitems;
247 }
248 
249 /*--------------------Patrick 17/12/96----------------------------
250   I have added some more shape data access functions......
251   ----------------------------------------------------------------*/
252 
253 static VECTORCH patPointData;
254 static int patPolyVertexIndices[4];
255 static VECTORCH *patShapePointsPtr;
256 
SetupPointAccessFromShapeIndex(int shapeIndex)257 int SetupPointAccessFromShapeIndex(int shapeIndex)
258 {
259 	SHAPEHEADER *shapePtr;
260 
261 	shapePtr = GetShapeData(shapeIndex);
262 	patShapePointsPtr  = (VECTORCH *)(*shapePtr->points);
263 
264     return shapePtr->numpoints;
265 }
266 
267 
AccessNextPoint(void)268 VECTORCH* AccessNextPoint(void)
269 {
270 	patPointData = *patShapePointsPtr++;
271 	return &patPointData;
272 }
273 
AccessPointFromIndex(int index)274 VECTORCH* AccessPointFromIndex(int index)
275 {
276 	patPointData = patShapePointsPtr[index];
277 	return &patPointData;
278 }
279 
280 /* KJL 18:51:08 21/11/98 - similiar function for polys */
AccessPolyFromIndex(int index)281 POLYHEADER *AccessPolyFromIndex(int index)
282 {
283 	int *itemPtr = *(ItemArrayPtr+index);
284 	PolyheaderPtr = (POLYHEADER *) itemPtr;
285 	return PolyheaderPtr;
286 }
287 
DestroyPolygon(int shapeIndex,int polyIndex)288 void DestroyPolygon(int shapeIndex,int polyIndex)
289 {
290 	SHAPEHEADER *shapePtr = GetShapeData(shapeIndex);
291 	shapePtr->numitems--;
292 	*(ItemArrayPtr+polyIndex) = *(ItemArrayPtr+shapePtr->numitems);
293 }
294 
ReplaceVertexInPolygon(int polyIndex,int oldVertex,int newVertex)295 void ReplaceVertexInPolygon(int polyIndex, int oldVertex, int newVertex)
296 {
297 	int *vertexNumberPtr;
298 	int *itemPtr = *(ItemArrayPtr+polyIndex);
299 	PolyheaderPtr = (POLYHEADER *) itemPtr;
300 
301 	vertexNumberPtr = &PolyheaderPtr->Poly1stPt;
302 
303     while(*vertexNumberPtr != Term)
304 	{
305     	if (*vertexNumberPtr == oldVertex)
306 		{
307 			*vertexNumberPtr = newVertex;
308 		}
309 		vertexNumberPtr++;
310 	}
311 
312 	{
313 		VECTORCH newNormal;
314 		VECTORCH *pointPtr[3];
315 		int *vertexNumPtr = &PolyheaderPtr->Poly1stPt;
316 		pointPtr[0] = (ShapePointsPtr + *vertexNumPtr++);
317 		pointPtr[1] = (ShapePointsPtr + *vertexNumPtr++);
318 		pointPtr[2] = (ShapePointsPtr + *vertexNumPtr);
319 		MakeNormal
320 		(
321 			pointPtr[0],
322 			pointPtr[1],
323 			pointPtr[2],
324 			&newNormal
325 		);
326 	   	*(VECTORCH*)(ShapeNormalsPtr + PolyheaderPtr->PolyNormalIndex)=newNormal;
327 	}
328 
329 
330 }
GetPolygonNormalFromIndex(void)331 VECTORCH *GetPolygonNormalFromIndex(void)
332 {
333 	return (VECTORCH*)(ShapeNormalsPtr + PolyheaderPtr->PolyNormalIndex);
334 }
335 
GetPolygonVertexIndices(void)336 int *GetPolygonVertexIndices(void)
337 {
338 	int *vertexNumberPtr = &PolyheaderPtr->Poly1stPt;
339     int numberOfVertices=0;
340 
341     patPolyVertexIndices[3]	= -1;
342 
343     while(*vertexNumberPtr != Term)
344 	{
345     	patPolyVertexIndices[numberOfVertices++] = (*vertexNumberPtr);
346 		vertexNumberPtr++;
347 	}
348 
349     return &patPolyVertexIndices[0];
350 }
351 
352 
353 /*--------------------Roxby 3/7/97----------------------------
354   I have added some more shape data access functions......
355   ----------------------------------------------------------------*/
356 
357 
SetupPolygonFlagAccessForShape(SHAPEHEADER * shape)358 void SetupPolygonFlagAccessForShape(SHAPEHEADER *shape)
359 {
360 }
361 
362 
Request_PolyFlags(void * polygon)363 int Request_PolyFlags(void *polygon)
364 {
365 	POLYHEADER *poly = (POLYHEADER*)polygon;
366 	return poly->PolyFlags;
367 }
368