1 extern "C" {
2 
3 #include "3dc.h"
4 #include "inline.h"
5 #include "module.h"
6 #include "gamedef.h"
7 #include "stratdef.h"
8 #include "vramtime.h"
9 
10 #include "dxlog.h"
11 
12 #include "d3_func.h"
13 #include "d3dmacs.h"
14 
15 #include "string.h"
16 
17 #include "kshape.h"
18 #include "frustrum.h"
19 
20 #include "d3d_hud.h"
21 #include "gamedef.h"
22 
23 #include "particle.h"
24 
25 #define UseLocalAssert No
26 #include "ourasert.h"
27 extern "C++"{
28 #include "r2base.h"
29 #include <math.h> // for sqrt
30 };
31 #include "HUD_layout.h"
32 #define HAVE_VISION_H 1
33 #include "vision.h"
34 #include "lighting.h"
35 #include "showcmds.h"
36 #include "frustrum.h"
37 #include "smacker.h"
38 #include "d3d_render.h"
39 #include "avp_userprofile.h"
40 #include "bh_types.h"
41 
42 #include <string.h>
43 
44 
45 #define RGBLIGHT_MAKE(r,g,b) RGB_MAKE(r,g,b)
46 #define RGBALIGHT_MAKE(r,g,b,a) RGBA_MAKE(r,g,b,a)
47 
48 
49 // Set to Yes to make default texture filter bilinear averaging rather
50 // than nearest
51 extern BOOL BilinearTextureFilter;
52 
53 
54 
55 #define FMV_ON 0
56 #define FMV_EVERYWHERE 0
57 #define WIBBLY_FMV_ON 0
58 #define FMV_SIZE 128
59 VECTORCH FmvPosition = {42985-3500,2765-5000,-35457};
60 
61 #define FOG_ON 0
62 #if 1
63 
64 #define FOG_COLOUR 0x7f7f7f //0x404040
65 #define FOG_SCALE 512
66 #else
67 #define FOG_COLOUR 0x7f7f7f//0xd282828 //0x404040
68 #define FOG_SCALE 256
69 #endif
70 
71 #define TriangleVertices   3
72 
73 #define LineVertices 2
74 
75 #define DefaultVertexIntensity 200
76 #define TransparentAlphaValue 128
77 
78 #define DefaultColour (RGBLIGHT_MAKE(DefaultVertexIntensity,DefaultVertexIntensity,DefaultVertexIntensity))
79 #define DefaultAlphaColour (RGBALIGHT_MAKE(DefaultVertexIntensity,DefaultVertexIntensity,DefaultVertexIntensity,TransparentAlphaValue))
80 
81 
82 #if 0
83 #define TxIntensityLShift 4
84 #else
85 #define TxIntensityLShift 0
86 #endif
87 
88 #define TxIntensityLimit 255
89 
90 #define MaxVerticesInExecuteBuffer (MaxD3DVertices)
91 // Colour is a 24-bit RGB colour, standard engine
92 // internal format.
93 // a is an 8 bit alpha value.
94 #define MAKE_ALPHACOLOUR(colour, a)   ((D3DCOLOR) (((a) << 24) | (colour)))
95 
96 extern int SpecialFXImageNumber;
97 extern int SmokyImageNumber;
98 extern int ChromeImageNumber;
99 extern int HUDFontsImageNumber;
100 extern int BurningImageNumber;
101 extern int PredatorVisionChangeImageNumber;
102 extern int PredatorNumbersImageNumber;
103 extern int StaticImageNumber;
104 extern int AAFontImageNumber;
105 extern int WaterShaftImageNumber;
106 
107 D3DTEXTUREHANDLE FMVTextureHandle[4];
108 D3DTEXTUREHANDLE NoiseTextureHandle;
109 
110 int LightIntensityAtPoint(VECTORCH *pointPtr);
111 
112 // Externs
113 
114 extern int VideoMode;
115 extern int WindowMode;
116 extern int ScanDrawMode;
117 extern int DXMemoryMode;
118 extern int ZBufferRequestMode;
119 extern int RasterisationRequestMode;
120 extern int SoftwareScanDrawRequestMode;
121 extern SCREENDESCRIPTORBLOCK ScreenDescriptorBlock;
122 extern VIEWDESCRIPTORBLOCK* Global_VDB_Ptr;
123 
124 
125 extern IMAGEHEADER ImageHeaderArray[];
126 extern int NumImagesArray[];
127 
128 extern LPDIRECTDRAW lpDD;
129 extern LPDIRECTDRAWSURFACE lpDDSPrimary;
130 extern BOOL MMXAvailable;
131 extern int NormalFrameTime;
132 int WireFrameMode;
133 
134 extern int HUDScaleFactor;
135 
136 //Globals
137 
138 int D3DDriverMode;
139 int NumVertices;
140 
141 static LPVOID ExecuteBufferDataArea;
142 static LPVOID ExecuteBufferInstArea;
143 static LPVOID ExecBufDataPtr;
144 static LPVOID ExecBufInstPtr;
145 static LPVOID ExecBufStartInstPtr;
146 #if SupportZBuffering
147 #endif
148 
149 
150 static unsigned char D3DShadingMode;
151 static unsigned char D3DTexturePerspective;
152 static unsigned char D3DTBlendMode;
153 static unsigned char D3DAlphaMode;
154 static D3DTEXTUREHANDLE CurrTextureHandle;
155 static unsigned char DefaultD3DTextureFilterMin;
156 static unsigned char DefaultD3DTextureFilterMax;
157 // Globals for frame by frame definition of
158 // coloured materials for D3D rendering interface
159 
160 
161 
162 
163 static int NumberOfRenderedTriangles=0;
164 int NumberOfLandscapePolygons;
165 RENDERSTATES CurrentRenderStates;
166 extern HRESULT LastError;
167 
168 
169 void ChangeTranslucencyMode(enum TRANSLUCENCY_TYPE translucencyRequired);
170 void ChangeFilteringMode(enum FILTERING_MODE_ID filteringRequired);
171 
172 #define CheckTranslucencyModeIsCorrect(x) \
173 if (CurrentRenderStates.TranslucencyMode!=(x)) \
174 	ChangeTranslucencyMode((x));
175 
176 #define CheckFilteringModeIsCorrect(x) \
177 if (CurrentRenderStates.FilteringMode!=(x)) \
178 	ChangeFilteringMode((x));
179 
180 
181 
182 extern D3DINFO d3d;
183 
184 extern int ExBufSize;
185 extern LPDIRECT3DEXECUTEBUFFER lpD3DExecCmdBuf;
186 
187 
188 
189 
190 
191 /* KJL 15:12:02 10/05/98 - FMV stuff */
192 static PALETTEENTRY FMVPalette[4][256];
193 
194 void D3D_DrawFMVOnWater(int xOrigin, int yOrigin, int zOrigin);
195 static void UpdateFMVTextures(int maxTextureNumberToUpdate);
196 extern int NextFMVFrame(void*bufferPtr, int x, int y, int w, int h, int fmvNumber);
197 extern void UpdateFMVPalette(PALETTEENTRY *FMVPalette, int fmvNumber);
198 
199 void D3D_DrawFMV(int xOrigin, int yOrigin, int zOrigin);
200 
201 VECTORCH FMVParticleAbsPosition[450];
202 VECTORCH FMVParticlePosition[450];
203 int FMVParticleColour;
204 
205 void D3D_DrawSwirlyFMV(int xOrigin, int yOrigin, int zOrigin);
206 void D3D_FMVParticle_Output(RENDERVERTEX *renderVerticesPtr);
207 
208 
209 void DrawFBM(void);
210 
211 
212 
213 void D3D_DrawCable(VECTORCH *centrePtr, MATRIXCH *orientationPtr);
214 
215 
216 int WaterXOrigin;
217 int WaterZOrigin;
218 float WaterUScale;
219 float WaterVScale;
220 
SetExecuteBufferDefaults(void)221 BOOL SetExecuteBufferDefaults(void)
222 
223 {
224     D3DEXECUTEBUFFERDESC d3dexDesc;
225     D3DEXECUTEDATA d3dExecData;
226 
227     memset(&d3dexDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
228     d3dexDesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
229 
230 	// Acquire lock on execute buffer
231 	LastError = lpD3DExecCmdBuf->Lock(&d3dexDesc);
232 	LOGDXERR(LastError);
233 
234 	if (LastError != D3D_OK)
235 	  return FALSE;
236 
237     // Zero buffer and acquire pointers to start of areas
238     memset(d3dexDesc.lpData, 0, ExBufSize);
239 
240     // Initialise pointers to data areas within buffer
241     ExecuteBufferDataArea = d3dexDesc.lpData;
242 	ExecuteBufferInstArea = (LPVOID)
243 	   ((int) ExecuteBufferDataArea
244 	   + (int) (sizeof(D3DTLVERTEX) * MaxVerticesInExecuteBuffer));
245 
246 	ExecBufDataPtr = ExecuteBufferDataArea;
247 	ExecBufInstPtr = ExecuteBufferInstArea;
248 
249     NumVertices = 0;
250 
251 // If we can, we want to turn off culling at the level of the rasterisation
252 // module.  Note that Microsoft's software renderers do not support this,
253 // unfortunately.
254    #if 1
255    if (ScanDrawMode == ScanDrawD3DHardwareRGB)
256      {
257       OP_STATE_RENDER(1, ExecBufInstPtr);
258         STATE_DATA(D3DRENDERSTATE_CULLMODE, D3DCULL_NONE, ExecBufInstPtr);
259 	 }
260 	#endif
261 
262     #if SupportZBuffering
263     OP_STATE_RENDER(1, ExecBufInstPtr);
264     STATE_DATA(D3DRENDERSTATE_ZENABLE, TRUE, ExecBufInstPtr);
265 
266 
267     // Certainly necess for new mixed HSR model!!!
268     OP_STATE_RENDER(1, ExecBufInstPtr);
269     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
270 
271     OP_STATE_RENDER(1, ExecBufInstPtr);
272     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
273 
274     #endif
275 
276     // Set default alpha modes with blending off
277     OP_STATE_RENDER(1, ExecBufInstPtr);
278        STATE_DATA(D3DRENDERSTATE_SRCBLEND,
279        D3DBLEND_SRCALPHA, ExecBufInstPtr);
280 
281     OP_STATE_RENDER(1, ExecBufInstPtr);
282        STATE_DATA(D3DRENDERSTATE_DESTBLEND,
283        D3DBLEND_INVSRCALPHA, ExecBufInstPtr);
284 
285     OP_STATE_RENDER(1, ExecBufInstPtr);
286       STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,
287              FALSE, ExecBufInstPtr);
288 
289 	#if FOG_ON
290 	OP_STATE_RENDER(1, ExecBufInstPtr);
291 	STATE_DATA(D3DRENDERSTATE_FOGENABLE, TRUE, ExecBufInstPtr);
292 
293 	OP_STATE_RENDER(1, ExecBufInstPtr);
294 	STATE_DATA(D3DRENDERSTATE_FOGCOLOR, FOG_COLOUR, ExecBufInstPtr);
295 	#endif
296 	#if 0
297 	{
298 		float fogStart = 2000.0f; // these values work on a Voodoo2, but not a G200. Hmm.
299 		float fogEnd = 32000.0f;
300 		OP_STATE_RENDER(2, ExecBufInstPtr);
301 		STATE_DATA(D3DRENDERSTATE_FOGENABLE, TRUE, ExecBufInstPtr);
302 		STATE_DATA(D3DRENDERSTATE_FOGCOLOR, 0x7f7f7f, ExecBufInstPtr);
303 
304 		#if 1
305 		OP_STATE_RENDER(3, ExecBufInstPtr);
306 		STATE_DATA(D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_LINEAR, ExecBufInstPtr);
307 		STATE_DATA(D3DRENDERSTATE_FOGTABLESTART, *(DWORD*)(&fogStart), ExecBufInstPtr);
308 		STATE_DATA(D3DRENDERSTATE_FOGTABLEEND, *(DWORD*)(&fogEnd), ExecBufInstPtr);
309 		#endif
310 //		STATE_DATA(D3DRENDERSTATE_SPECULARENABLE, FALSE, ExecBufInstPtr);
311 	}
312 	#endif
313 
314 	OP_STATE_RENDER(1, ExecBufInstPtr);
315 	STATE_DATA(D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATE, ExecBufInstPtr);
316 
317     OP_EXIT(ExecBufInstPtr);
318 
319     // Unlock buffer
320     LastError = lpD3DExecCmdBuf->Unlock();
321 	LOGDXERR(LastError);
322 
323 	if (LastError != D3D_OK)
324 	  return FALSE;
325 
326     // Setup for execution
327     memset(&d3dExecData, 0, sizeof(D3DEXECUTEDATA));
328     d3dExecData.dwSize = sizeof(D3DEXECUTEDATA);
329 	// Vertices start at beginning of buffer, i.e.
330 	// at ExecuteBufferDataArea, so there is no
331 	// Vertex Offset.
332     d3dExecData.dwVertexCount = NumVertices;
333 
334     d3dExecData.dwInstructionOffset = (ULONG) ((char*) ExecuteBufferInstArea
335                                     - (char*) ExecuteBufferDataArea);
336     d3dExecData.dwInstructionLength = (ULONG) ((char*)ExecBufInstPtr
337                                     - (char*) ExecuteBufferInstArea);
338 
339     // Do the actual set up
340     LastError = lpD3DExecCmdBuf->SetExecuteData(&d3dExecData);
341 	LOGDXERR(LastError);
342 
343 	if (LastError != D3D_OK)
344 	  return FALSE;
345 
346 	LastError = d3d.lpD3DDevice->BeginScene();
347 	LOGDXERR(LastError);
348 
349 	if (LastError != D3D_OK)
350 	  return FALSE;
351 
352 	// Execute buffer
353 	// We will specify no clipping since the engine
354 	// already clips to the VDB.  Eventually we might
355 	// try for no culling as well...if we're feeling
356 	// really daring...
357 	LastError = d3d.lpD3DDevice->Execute(lpD3DExecCmdBuf,
358 	          d3d.lpD3DViewport, D3DEXECUTE_UNCLIPPED);
359 	LOGDXERR(LastError);
360 
361 	if (LastError != D3D_OK)
362 	  return FALSE;
363 
364 	// End scene
365     LastError = d3d.lpD3DDevice->EndScene();
366 	LOGDXERR(LastError);
367 
368 	if (LastError != D3D_OK)
369  	  return FALSE;
370 
371     return TRUE;
372 }
373 static int NeedToClearExecuteBuffer=1;
LockExecuteBuffer(void)374 BOOL LockExecuteBuffer(void)
375 
376 {
377     D3DEXECUTEBUFFERDESC d3dexDesc;
378 
379     memset(&d3dexDesc, 0, sizeof(D3DEXECUTEBUFFERDESC));
380     d3dexDesc.dwSize = sizeof(D3DEXECUTEBUFFERDESC);
381 
382 	// Acquire lock on execute buffer
383 	LastError = lpD3DExecCmdBuf->Lock(&d3dexDesc);
384 	LOGDXERR(LastError);
385 
386 	if (LastError != D3D_OK)
387 	  return FALSE;
388 
389     // Zero buffer and acquire pointers to start of areas
390 //    memset(d3dexDesc.lpData, 0, ExBufSize);
391   //	if(NeedToClearExecuteBuffer)
392 	{
393 	    memset(d3dexDesc.lpData, 0, ExBufSize);
394   //		NeedToClearExecuteBuffer=0;
395 	}
396 	//textprint("ExBufSize %d %d %d %d\n",ExBufSize,sizeof(D3DINSTRUCTION),sizeof(D3DTRIANGLE),sizeof(D3DTLVERTEX) );
397     // Two lines below should really be in init routine,
398 	// but are here for the moment in case locking has
399 	// weird side effects.
400 	// FIXME!!!!
401     // Initialise pointers to data areas within buffer
402     ExecuteBufferDataArea = d3dexDesc.lpData;
403 	// Hmmm....
404 	ExecuteBufferInstArea = (LPVOID)
405 	   ((int) ExecuteBufferDataArea
406 	   + (int) (sizeof(D3DTLVERTEX) * MaxVerticesInExecuteBuffer));
407 
408 	ExecBufDataPtr = ExecuteBufferDataArea;
409 	ExecBufInstPtr = ExecuteBufferInstArea;
410 	NumVertices = 0;
411 
412 // Force initialisation of render states during
413 // first call to Write functions
414 	D3DShadingMode = 0xff;
415 	D3DTexturePerspective = 0xff;
416 	D3DAlphaMode = 0xff;
417 
418 //  Other initialisations
419 	CurrTextureHandle = (D3DTEXTUREHANDLE) 0xff;
420 
421 
422 // Save pointer to execute buffer and
423 // wind that pointer on by the
424 // amount of a process vertices data opcode and
425 // associated data
426     ExecBufStartInstPtr = ExecBufInstPtr;
427     ExecBufInstPtr = (void*)(((LPD3DINSTRUCTION) ExecBufInstPtr) + 1);
428     ExecBufInstPtr = (void*)(((LPD3DPROCESSVERTICES) ExecBufInstPtr) + 1);
429 
430 	#if 0
431     // Set default texture filter for this frame
432 	// NOTE THAT BILINEAR BEHAVIOUR MIGHT EVENTUALLY BE BEST
433 	// MOVED TO AN ITEM FLAG, NOT A COMPILE OPTION
434 	// Note at present no support for averaging between
435 	// mip-maps!!! May well not exist on all (or most) accelerators
436 	// anyway!!!
437 	if (BilinearTextureFilter)
438 	{
439 		if (ScreenDescriptorBlock.SDB_Flags & SDB_Flag_MIP)
440 		  {
441 		   DefaultD3DTextureFilterMin = D3DFILTER_MIPLINEAR;
442 		   DefaultD3DTextureFilterMax = D3DFILTER_MIPLINEAR;
443 		  }
444 		else
445 		  {
446 		   DefaultD3DTextureFilterMin = D3DFILTER_LINEAR;
447 		   DefaultD3DTextureFilterMax = D3DFILTER_LINEAR;
448 		  }
449 	}
450 	else
451 	{
452 		if (ScreenDescriptorBlock.SDB_Flags & SDB_Flag_MIP)
453 		  {
454 		   DefaultD3DTextureFilterMin = D3DFILTER_MIPNEAREST;
455 		   DefaultD3DTextureFilterMax = D3DFILTER_MIPNEAREST;
456 		  }
457 		else
458 		  {
459 		   DefaultD3DTextureFilterMin = D3DFILTER_NEAREST;
460 		   DefaultD3DTextureFilterMax = D3DFILTER_NEAREST;
461 		  }
462 	}
463     // Actually set default texture filters
464 
465     OP_STATE_RENDER(2, ExecBufInstPtr);
466     STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, DefaultD3DTextureFilterMax, ExecBufInstPtr);
467     STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, DefaultD3DTextureFilterMin, ExecBufInstPtr);
468 	#endif
469     return TRUE;
470 }
471 
472 
UnlockExecuteBufferAndPrepareForUse(void)473 BOOL UnlockExecuteBufferAndPrepareForUse(void)
474 
475 {
476     D3DEXECUTEDATA d3dExecData;
477 
478     // Unlock buffer
479     LastError = lpD3DExecCmdBuf->Unlock();
480 	LOGDXERR(LastError);
481 
482 	if (LastError != D3D_OK)
483 	  return FALSE;
484 
485     // Setup for execution
486     memset(&d3dExecData, 0, sizeof(D3DEXECUTEDATA));
487     d3dExecData.dwSize = sizeof(D3DEXECUTEDATA);
488 	// Vertices start at beginning of buffer, i.e.
489 	// at ExecuteBufferDataArea, so there is no
490 	// Vertex Offset.
491     d3dExecData.dwVertexCount = NumVertices;
492 
493     d3dExecData.dwInstructionOffset = (ULONG) ((char*) ExecuteBufferInstArea
494                                     - (char*) ExecuteBufferDataArea);
495     d3dExecData.dwInstructionLength = (ULONG) ((char*)ExecBufInstPtr
496                                     - (char*) ExecuteBufferInstArea);
497 
498     // Do the actual set up
499     LastError = lpD3DExecCmdBuf->SetExecuteData(&d3dExecData);
500 	LOGDXERR(LastError);
501 
502 	if (LastError != D3D_OK)
503 	  return FALSE;
504 
505     return TRUE;
506 }
507 
508 // Test for multiple execute buffers!!!!
BeginD3DScene(void)509 BOOL BeginD3DScene(void)
510 
511 {
512 #if SOFTWARE_RENDERER
513     LockSurfaceAndGetBufferPointer();
514 
515 	return TRUE;
516 #else
517 	NumberOfRenderedTriangles=0;
518 	LastError = d3d.lpD3DDevice->BeginScene();
519 	LOGDXERR(LastError);
520 
521 	if (LastError != D3D_OK)
522 	  return FALSE;
523 	else
524 	  return TRUE;
525 #endif
526 }
D3D_SetupSceneDefaults(void)527 void D3D_SetupSceneDefaults(void)
528 {
529 	/* force translucency state to be reset */
530 	CurrentRenderStates.TranslucencyMode = TRANSLUCENCY_NOT_SET;
531 	CurrentRenderStates.FilteringMode = FILTERING_NOT_SET;
532 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
533 	CheckWireFrameMode(0);
534 	OP_STATE_RENDER(1, ExecBufInstPtr);
535 	STATE_DATA(D3DRENDERSTATE_DITHERENABLE, TRUE, ExecBufInstPtr);
536 }
EndD3DScene(void)537 BOOL EndD3DScene(void)
538 
539 {
540 #if SOFTWARE_RENDERER
541 	UnlockSurface();
542 	return TRUE;
543 #else
544 	extern int NormalFrameTime;
545 
546     LastError = d3d.lpD3DDevice->EndScene();
547 	LOGDXERR(LastError);
548 	if (ShowDebuggingText.PolyCount)
549 	{
550 		ReleasePrintDebuggingText("NumberOfLandscapePolygons: %d\n",NumberOfLandscapePolygons);
551 		ReleasePrintDebuggingText("NumberOfRenderedTriangles: %d\n",NumberOfRenderedTriangles);
552 	}
553 //	textprint ("NumberOfRenderedTrianglesPerSecond: %d\n",DIV_FIXED(NumberOfRenderedTriangles,NormalFrameTime));
554 	NumberOfLandscapePolygons=0;
555 
556 	if (LastError != D3D_OK)
557 	  return FALSE;
558 	else
559 	  return TRUE;
560 #endif
561 
562 }
WriteEndCodeToExecuteBuffer(void)563 void WriteEndCodeToExecuteBuffer(void)
564 {
565 	// Initialise execute buffer by specifying
566 	// that all operations will be rasterisation
567 	// only (this optimises the immediate mode interface
568 	// behaviour, since it knows that it will not
569 	// be asked to perform geometry transformations
570 	// or lighting operations).
571 	// Write process vertices instruction
572 	// to the START of the execute buffer,
573 	// now that we know how many vertices
574 	// there are.
575 
576 	OP_PROCESS_VERTICES(1, ExecBufStartInstPtr);
577 	PROCESSVERTICES_DATA(D3DPROCESSVERTICES_COPY, 0, NumVertices, ExecBufStartInstPtr);
578 
579 	// Write single termination opcode to buffer
580 	OP_EXIT(ExecBufInstPtr);
581 }
582 
583 #include "pentime.h"
ExecuteBuffer(void)584 BOOL ExecuteBuffer(void)
585 
586 {
587 #if SOFTWARE_RENDERER
588 	return TRUE;
589 #else
590 
591  //	textprint("%d\n",sizeof(D3DINSTRUCTION) + sizeof(D3DTRIANGLE));
592 //	textprint("NumVertices %d\n Instructions %d",NumVertices,(char*)ExecBufInstPtr-(char*)ExecBufStartInstPtr);
593 //ProfileStart();
594 	LastError = d3d.lpD3DDevice->Execute(lpD3DExecCmdBuf,
595 	          d3d.lpD3DViewport, D3DEXECUTE_UNCLIPPED);
596 	LOGDXERR(LastError);
597 //ProfileStop("execute buffer");
598 	if (LastError != D3D_OK)
599 	  return FALSE;
600     else
601 	  return TRUE;
602 #endif
603 }
604 
CheckRenderStatesForModule(MODULE * modulePtr)605 void CheckRenderStatesForModule(MODULE *modulePtr)
606 {
607 //  	textprint("fog %d\n",CurrentRenderStates.FogIsOn);
608 
609 	if (modulePtr->m_flags & MODULEFLAG_FOG)
610 	{
611 //		CurrentRenderStates.FogIsOn = 1;
612 	}
613 	else
614 	{
615 //		CurrentRenderStates.FogIsOn = 0;
616 	}
617 }
618 
SetFogDistance(int fogDistance)619 void SetFogDistance(int fogDistance)
620 {
621 	if (fogDistance>10000) fogDistance = 10000;
622 	fogDistance+=5000;
623 	fogDistance=2000;
624 	CurrentRenderStates.FogDistance = fogDistance;
625 //	textprint("fog distance %d\n",fogDistance);
626 
627 }
628 
ChangeTranslucencyMode(enum TRANSLUCENCY_TYPE translucencyRequired)629 void ChangeTranslucencyMode(enum TRANSLUCENCY_TYPE translucencyRequired)
630 {
631 
632 //	OP_STATE_RENDER(1, ExecBufInstPtr);
633 //	STATE_DATA(D3DRENDERSTATE_STIPPLEDALPHA,FALSE, ExecBufInstPtr);
634 //	OP_STATE_RENDER(1, ExecBufInstPtr);
635 //	STATE_DATA(D3DRENDERSTATE_STIPPLEENABLE,FALSE, ExecBufInstPtr);
636 //	if (CurrentRenderStates.TranslucencyMode!=(translucencyRequired))
637 	{
638 		CurrentRenderStates.TranslucencyMode=translucencyRequired;
639 		switch(CurrentRenderStates.TranslucencyMode)
640 		{
641 		 	case TRANSLUCENCY_OFF:
642 			{
643 				if (TRIPTASTIC_CHEATMODE||MOTIONBLUR_CHEATMODE)
644 				{
645 					if (D3DAlphaMode != Yes)
646 					{
647 						D3DAlphaMode = Yes;
648 						OP_STATE_RENDER(1, ExecBufInstPtr);
649 						STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
650 					}
651 					OP_STATE_RENDER(2, ExecBufInstPtr);
652 					STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_INVSRCALPHA, ExecBufInstPtr);
653 					STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_SRCALPHA, ExecBufInstPtr);
654 				}
655 				else
656 				{
657 					if (D3DAlphaMode != No)
658 					{
659 						D3DAlphaMode = No;
660 						OP_STATE_RENDER(1, ExecBufInstPtr);
661 						STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	FALSE, ExecBufInstPtr);
662 					}
663 					OP_STATE_RENDER(2, ExecBufInstPtr);
664 					STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_ONE, ExecBufInstPtr);
665 					STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_ZERO, ExecBufInstPtr);
666 				}
667 
668 				break;
669 			}
670 		 	case TRANSLUCENCY_NORMAL:
671 			{
672 				if (D3DAlphaMode != Yes)
673 				{
674 					D3DAlphaMode = Yes;
675 					OP_STATE_RENDER(1, ExecBufInstPtr);
676 					STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
677 				}
678 				OP_STATE_RENDER(2, ExecBufInstPtr);
679 				STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_SRCALPHA, ExecBufInstPtr);
680 				STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_INVSRCALPHA, ExecBufInstPtr);
681 
682 
683 
684 				break;
685 			}
686 		 	case TRANSLUCENCY_COLOUR:
687 			{
688 				if (D3DAlphaMode != Yes)
689 				{
690 					D3DAlphaMode = Yes;
691 					OP_STATE_RENDER(1, ExecBufInstPtr);
692 					STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
693 				}
694 				OP_STATE_RENDER(2, ExecBufInstPtr);
695 				STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_ZERO, ExecBufInstPtr);
696 				STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_SRCCOLOR, ExecBufInstPtr);
697 				break;
698 			}
699 		 	case TRANSLUCENCY_INVCOLOUR:
700 			{
701 				if (D3DAlphaMode != Yes)
702 				{
703 					D3DAlphaMode = Yes;
704 					OP_STATE_RENDER(1, ExecBufInstPtr);
705 					STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
706 				}
707 				OP_STATE_RENDER(2, ExecBufInstPtr);
708 				STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_ZERO, ExecBufInstPtr);
709 				STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_INVSRCCOLOR, ExecBufInstPtr);
710 				break;
711 			}
712 	  		case TRANSLUCENCY_GLOWING:
713 			{
714 				if (D3DAlphaMode != Yes)
715 				{
716 					D3DAlphaMode = Yes;
717 					OP_STATE_RENDER(1, ExecBufInstPtr);
718 					STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
719 				}
720 				OP_STATE_RENDER(2, ExecBufInstPtr);
721 				STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_SRCALPHA, ExecBufInstPtr);
722 				STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_ONE, ExecBufInstPtr);
723 				break;
724 			}
725 	  		case TRANSLUCENCY_DARKENINGCOLOUR:
726 			{
727 				if (D3DAlphaMode != Yes)
728 				{
729 					D3DAlphaMode = Yes;
730 					OP_STATE_RENDER(1, ExecBufInstPtr);
731 					STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
732 				}
733 				OP_STATE_RENDER(2, ExecBufInstPtr);
734 				STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_INVDESTCOLOR, ExecBufInstPtr);
735 				STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_ZERO, ExecBufInstPtr);
736 				break;
737 			}
738 			case TRANSLUCENCY_JUSTSETZ:
739 			{
740 				if (D3DAlphaMode != Yes)
741 				{
742 					D3DAlphaMode = Yes;
743 					OP_STATE_RENDER(1, ExecBufInstPtr);
744 					STATE_DATA(D3DRENDERSTATE_ALPHABLENDENABLE,	TRUE, ExecBufInstPtr);
745 				}
746 				OP_STATE_RENDER(2, ExecBufInstPtr);
747 				STATE_DATA(D3DRENDERSTATE_SRCBLEND,D3DBLEND_ZERO, ExecBufInstPtr);
748 				STATE_DATA(D3DRENDERSTATE_DESTBLEND,D3DBLEND_ONE, ExecBufInstPtr);
749 			}
750 
751 			default:
752 				break;
753 		}
754 	}
755 }
756 
ChangeFilteringMode(enum FILTERING_MODE_ID filteringRequired)757 void ChangeFilteringMode(enum FILTERING_MODE_ID filteringRequired)
758 {
759 	CurrentRenderStates.FilteringMode = filteringRequired;
760 
761 	switch(CurrentRenderStates.FilteringMode)
762 	{
763 		case FILTERING_BILINEAR_OFF:
764 		{
765 		    OP_STATE_RENDER(2, ExecBufInstPtr);
766 		    STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, D3DFILTER_NEAREST, ExecBufInstPtr);
767 		    STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, D3DFILTER_NEAREST, ExecBufInstPtr);
768 			break;
769 		}
770 		case FILTERING_BILINEAR_ON:
771 		{
772 		    OP_STATE_RENDER(2, ExecBufInstPtr);
773 		    STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, D3DFILTER_LINEAR, ExecBufInstPtr);
774 		    STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, D3DFILTER_LINEAR, ExecBufInstPtr);
775 			break;
776 		}
777 		default:
778 		{
779 			LOCALASSERT("Unrecognized filtering mode"==0);
780 			break;
781 		}
782 	}
783 }
784 
CheckWireFrameMode(int shouldBeOn)785 extern void CheckWireFrameMode(int shouldBeOn)
786 {
787 	if (shouldBeOn) shouldBeOn=1;
788 	if(CurrentRenderStates.WireFrameModeIsOn!=shouldBeOn)
789 	{
790 		CurrentRenderStates.WireFrameModeIsOn=shouldBeOn;
791 	   if (shouldBeOn)
792 	   {
793 			OP_STATE_RENDER(1, ExecBufInstPtr);
794 			STATE_DATA(D3DRENDERSTATE_FILLMODE,	D3DFILL_WIREFRAME, ExecBufInstPtr);
795 	   }
796 	   else
797 	   {
798 			OP_STATE_RENDER(1, ExecBufInstPtr);
799 			STATE_DATA(D3DRENDERSTATE_FILLMODE,	D3DFILL_SOLID, ExecBufInstPtr);
800 	   }
801 
802 	}
803 
804 }
D3D_Line(VECTOR2D * LineStart,VECTOR2D * LineEnd,int LineColour)805 void D3D_Line(VECTOR2D* LineStart, VECTOR2D* LineEnd, int LineColour)
806 {
807 	if (d3d.ThisDriver.dpcLineCaps.dwShadeCaps & D3DPSHADECAPS_COLORFLATRGB)
808 	{
809 		int x1, x2, y1, y2;
810 
811 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
812 
813 	    // Test!!!
814 		// LineColour = 0xffffffff;
815 		vertexPtr->sx = LineStart->vx;
816 		vertexPtr->sy = LineStart->vy;
817 		vertexPtr[1].sx = LineEnd->vx;
818 		vertexPtr[1].sy = LineEnd->vy;
819 
820 		NumVertices+=2;
821 
822     	vertexPtr->color = LineColour;
823 		vertexPtr[1].color = LineColour;
824 
825 	    // Always turn off translucency for the moment
826 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
827 
828 	    // Turn OFF texturing if it is on...
829 	    if (CurrTextureHandle != NULL)
830 		  {
831 	       OP_STATE_RENDER(1, ExecBufInstPtr);
832 	          STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
833 		   CurrTextureHandle = NULL;
834 		  }
835 
836 		OP_LINE_LIST(1, ExecBufInstPtr);
837 	        ((LPD3DLINE)ExecBufInstPtr)->v1 = (NumVertices-2); // vertex index
838 	        ((LPD3DLINE)ExecBufInstPtr)->v2 = (NumVertices-1); // vertex index
839 	        ExecBufInstPtr = ((char*)ExecBufInstPtr) + sizeof(D3DLINE);
840 	}
841 
842 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
843 	{
844 	   WriteEndCodeToExecuteBuffer();
845   	   UnlockExecuteBufferAndPrepareForUse();
846 	   ExecuteBuffer();
847   	   LockExecuteBuffer();
848 	}
849 }
850 
851 static void D3D_OutputTriangles(void);
D3D_BackdropPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)852 void D3D_BackdropPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
853 {
854 	int flags;
855 	int texoffset;
856 
857 	D3DTEXTUREHANDLE TextureHandle;
858 
859 	float ZNear;
860 	float RecipW, RecipH;
861 
862     // Get ZNear
863 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
864 
865 
866 	// Take header information
867 	flags = inputPolyPtr->PolyFlags;
868 
869 	// We assume bit 15 (TxLocal) HAS been
870 	// properly cleared this time...
871 	texoffset = (inputPolyPtr->PolyColour & ClrTxDefn);
872 
873 	TextureHandle = (D3DTEXTUREHANDLE)
874 	          ImageHeaderArray[texoffset].D3DHandle;
875 
876     // Check for textures that have not loaded
877 	// properly
878 
879     if (TextureHandle == (D3DTEXTUREHANDLE) 0)
880 	  return;
881 
882 	if(ImageHeaderArray[texoffset].ImageWidth==128)
883 	{
884 		RecipW = 1.0 /128.0;
885 	}
886 	else
887 	{
888 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
889 		RecipW = (1.0 / width);
890 	}
891 	if(ImageHeaderArray[texoffset].ImageHeight==128)
892 	{
893 		RecipH = 1.0 / 128.0;
894 	}
895 	else
896 	{
897 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
898 		RecipH = (1.0 / height);
899 	}
900 
901 
902 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
903 	{
904 		int i = RenderPolygon.NumberOfVertices;
905 		RENDERVERTEX *vertices = renderVerticesPtr;
906 
907 		do
908 		{
909 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
910 		  	float oneOverZ;
911 		  	oneOverZ = (1.0)/vertices->Z;
912 			float zvalue;
913 
914 			{
915 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
916 
917 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
918 				{
919 					x=Global_VDB_Ptr->VDB_ClipLeft;
920 				}
921 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
922 				{
923 					x=Global_VDB_Ptr->VDB_ClipRight;
924 				}
925 
926 				vertexPtr->sx=x;
927 			}
928 			{
929 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
930 
931 				if (y<Global_VDB_Ptr->VDB_ClipUp)
932 				{
933 					y=Global_VDB_Ptr->VDB_ClipUp;
934 				}
935 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
936 				{
937 					y=Global_VDB_Ptr->VDB_ClipDown;
938 				}
939 				vertexPtr->sy=y;
940 
941 			}
942 			vertexPtr->tu = ((float)(vertices->U>>16)+.5) * RecipW;
943 			vertexPtr->tv = ((float)(vertices->V>>16)+.5) * RecipH;
944 			vertexPtr->rhw = oneOverZ;
945 
946 			vertexPtr->color = RGBLIGHT_MAKE(vertices->R,vertices->G,vertices->B);
947 			vertexPtr->sz = 1.0;
948 
949 			vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,255);
950 
951 			vertices++;
952 			NumVertices++;
953 		}
954 	  	while(--i);
955 	}
956 
957  	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
958 
959 	// Insert state change for shading model if required
960     if (D3DShadingMode != D3DSHADE_GOURAUD)
961 	  {
962    	   D3DShadingMode = D3DSHADE_GOURAUD;
963 	   OP_STATE_RENDER(1, ExecBufInstPtr);
964 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
965 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
966 	  }
967 
968 // Insert state change for texturing perspective value
969 // Note that drawtx3das2d options have ONLY been allowed for here,
970 // not when the rhw values are generated.  This is a deliberate choice,
971 // based on the assumption that drawtx3das2d will not be used very often
972 // and the extra branching at the top of this function will impose a
973 // greater cost than the (rare) savings in floating pt divisions are worth.
974 // Or so I claim...
975 
976     if (D3DTexturePerspective != Yes)
977     {
978 		D3DTexturePerspective = Yes;
979 		OP_STATE_RENDER(1, ExecBufInstPtr);
980 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
981 	}
982 
983     if (TextureHandle != CurrTextureHandle)
984 	{
985     	OP_STATE_RENDER(1, ExecBufInstPtr);
986         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
987 	   	CurrTextureHandle = TextureHandle;
988 	}
989 
990 
991 	D3D_OutputTriangles();
992 }
993 
D3D_ZBufferedGouraudTexturedPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)994 void D3D_ZBufferedGouraudTexturedPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
995 {
996 	int flags;
997 	int texoffset;
998 
999 	D3DTEXTUREHANDLE TextureHandle;
1000 
1001 	float ZNear;
1002 	float RecipW, RecipH;
1003 
1004     // Get ZNear
1005 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1006 
1007 
1008 	// Take header information
1009 	flags = inputPolyPtr->PolyFlags;
1010 
1011 	// We assume bit 15 (TxLocal) HAS been
1012 	// properly cleared this time...
1013 	texoffset = (inputPolyPtr->PolyColour & ClrTxDefn);
1014 	if (texoffset)
1015 	{
1016 		TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[texoffset].D3DHandle;
1017 	}
1018 	else
1019 	{
1020 		TextureHandle = CurrTextureHandle;
1021 	}
1022 
1023     // Check for textures that have not loaded
1024 	// properly
1025 
1026 //    if (TextureHandle == (D3DTEXTUREHANDLE) 0)
1027  //	  return;
1028 
1029 	#ifdef AVP_DEBUG_VERSION
1030 	if(ImageHeaderArray[texoffset].ImageWidth==128)
1031 	{
1032 		RecipW = (1.0f /128.0f)/65536.0f;
1033 	}
1034 	else
1035 	{
1036 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
1037 		RecipW = (1.0f / width)/65536.0f;
1038 	}
1039 	if(ImageHeaderArray[texoffset].ImageHeight==128)
1040 	{
1041 		RecipH = (1.0f / 128.0f)/65536.0f;
1042 	}
1043 	else
1044 	{
1045 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
1046 		RecipH = (1.0f / height)/65536.0f;
1047 	}
1048 	#else
1049 	RecipW = (1.0f/65536.0f)/128.0f;
1050 	RecipH = (1.0f/65536.0f)/128.0f;
1051 	#endif
1052 //	RecipW = (1.0/65536.0)/(float) ImageHeaderArray[texoffset].ImageWidth;
1053 //	RecipH = (1.0/65536.0)/(float) ImageHeaderArray[texoffset].ImageHeight;
1054 
1055 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1056 	{
1057 		int i = RenderPolygon.NumberOfVertices;
1058 		RENDERVERTEX *vertices = renderVerticesPtr;
1059 
1060 		do
1061 		{
1062 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1063 		  	float oneOverZ;
1064 		  	oneOverZ = (1.0f)/(vertices->Z);
1065 			float zvalue;
1066 
1067 			vertexPtr->tu = ((float)vertices->U) * RecipW + (1.0f/256.0f);
1068 			vertexPtr->tv = ((float)vertices->V) * RecipH + (1.0f/256.0f);
1069 			vertexPtr->rhw = oneOverZ;
1070 
1071 			{
1072 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1073 	   //			zvalue /= 65536.0;
1074 	   		   	zvalue = 1.0f - ZNear/zvalue;
1075 			}
1076 
1077 			{
1078 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1079 
1080 				#if 1
1081 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1082 				{
1083 					x=Global_VDB_Ptr->VDB_ClipLeft;
1084 				}
1085 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1086 				{
1087 					x=Global_VDB_Ptr->VDB_ClipRight;
1088 				}
1089 				#endif
1090 				vertexPtr->sx=x;
1091 			}
1092 			{
1093 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1094 
1095 				#if 1
1096 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1097 				{
1098 					y=Global_VDB_Ptr->VDB_ClipUp;
1099 				}
1100 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1101 				{
1102 					y=Global_VDB_Ptr->VDB_ClipDown;
1103 				}
1104 				#endif
1105 				vertexPtr->sy=y;
1106 
1107 			}
1108 //			vertexPtr->tu = ((float)(vertices->U>>16)+0.5) * RecipW;
1109 //			vertexPtr->tv = ((float)(vertices->V>>16)+0.5) * RecipH;
1110 //			vertexPtr->rhw = oneOverZ;
1111 
1112 			{
1113 				extern unsigned char GammaValues[256];
1114 		  		vertexPtr->color = RGBALIGHT_MAKE(GammaValues[vertices->R],GammaValues[vertices->G],GammaValues[vertices->B],vertices->A);
1115 			}
1116 
1117 			vertexPtr->sz = zvalue;
1118 			#if 0//FOG_ON
1119 			{
1120 				extern unsigned char GammaValues[256];
1121 				int r = GammaValues[vertices->SpecularR];
1122 				int g = GammaValues[vertices->SpecularG];
1123 				int b = GammaValues[vertices->SpecularB];
1124 
1125 				if(CurrentRenderStates.FogIsOn && vertices->Z>5000)//CurrentRenderStates.FogDistance)
1126 				{
1127 //					extern int CloakingPhase;
1128 					int brightness = (vertices->R+vertices->G+vertices->B)*86*3;
1129 
1130 					int fog = ((vertices->Z-5000/*-CurrentRenderStates.FogDistance*/)*4);
1131 					if (fog<0) fog=0;
1132 				 	if (fog>brightness) fog = brightness;
1133 					if (fog>ONE_FIXED) fog=ONE_FIXED;
1134 
1135 					{
1136 						extern int SkyColour_R;
1137 						int rfog = MUL_FIXED(fog,SkyColour_R);
1138 				 		if (r<rfog) r=rfog;
1139 					}
1140 					{
1141 						extern int SkyColour_G;
1142 						int gfog = MUL_FIXED(fog,SkyColour_G);
1143 				 		if (g<gfog) g=gfog;
1144 					}
1145 					{
1146 						extern int SkyColour_B;
1147 						int bfog = MUL_FIXED(fog,SkyColour_B);
1148 				 		if (b<bfog) b=bfog;
1149 					}
1150 				}
1151 			   	vertexPtr->specular=RGBALIGHT_MAKE(r,g,b,255);
1152 			}
1153 			#else
1154 			#if 0
1155 		   	if (RenderPolygon.TranslucencyMode != TRANSLUCENCY_OFF)
1156 		    {
1157 				vertexPtr->specular=0;
1158 			}
1159 			else
1160 			#endif
1161 			{
1162 				extern unsigned char GammaValues[256];
1163 				vertexPtr->specular=RGBALIGHT_MAKE(GammaValues[vertices->SpecularR],GammaValues[vertices->SpecularG],GammaValues[vertices->SpecularB],255);
1164 			}
1165 			#endif
1166 
1167 			vertices++;
1168 			NumVertices++;
1169 		}
1170 	  	while(--i);
1171 	}
1172 	CheckTranslucencyModeIsCorrect(RenderPolygon.TranslucencyMode);
1173 
1174 	// Insert state change for shading model if required
1175     if (D3DShadingMode != D3DSHADE_GOURAUD)
1176 	  {
1177    	   D3DShadingMode = D3DSHADE_GOURAUD;
1178 	   OP_STATE_RENDER(1, ExecBufInstPtr);
1179 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
1180 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
1181 	  }
1182 
1183 // Insert state change for texturing perspective value
1184 // Note that drawtx3das2d options have ONLY been allowed for here,
1185 // not when the rhw values are generated.  This is a deliberate choice,
1186 // based on the assumption that drawtx3das2d will not be used very often
1187 // and the extra branching at the top of this function will impose a
1188 // greater cost than the (rare) savings in floating pt divisions are worth.
1189 // Or so I claim...
1190 
1191     if (D3DTexturePerspective != Yes)
1192     {
1193 		D3DTexturePerspective = Yes;
1194 		OP_STATE_RENDER(1, ExecBufInstPtr);
1195 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
1196 	}
1197 
1198 	#if FMV_EVERYWHERE
1199 	TextureHandle = FMVTextureHandle[0];
1200 	#endif
1201     if (TextureHandle != CurrTextureHandle)
1202 	{
1203     	OP_STATE_RENDER(1, ExecBufInstPtr);
1204         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
1205 	   	CurrTextureHandle = TextureHandle;
1206 	}
1207 
1208 
1209 	D3D_OutputTriangles();
1210 }
D3D_ZBufferedGouraudPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)1211 void D3D_ZBufferedGouraudPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
1212 {
1213 	int flags;
1214 
1215 	float ZNear;
1216 
1217     // Get ZNear
1218 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1219 
1220 	// Take header information
1221 	flags = inputPolyPtr->PolyFlags;
1222 
1223 
1224 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1225 	{
1226 		int i = RenderPolygon.NumberOfVertices;
1227 		RENDERVERTEX *vertices = renderVerticesPtr;
1228 
1229 		do
1230 		{
1231 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1232 		  	float oneOverZ;
1233 		  	oneOverZ = (1.0)/vertices->Z;
1234 			float zvalue;
1235 
1236 			{
1237 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1238 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1239 				{
1240 					x=Global_VDB_Ptr->VDB_ClipLeft;
1241 				}
1242 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1243 				{
1244 					x=Global_VDB_Ptr->VDB_ClipRight;
1245 				}
1246 
1247 				vertexPtr->sx=x;
1248 			}
1249 			{
1250 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1251 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1252 				{
1253 					y=Global_VDB_Ptr->VDB_ClipUp;
1254 				}
1255 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1256 				{
1257 					y=Global_VDB_Ptr->VDB_ClipDown;
1258 				}
1259 				vertexPtr->sy=y;
1260 			}
1261 			{
1262 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1263 				zvalue = ((zvalue-ZNear)/zvalue);
1264 			}
1265 
1266 			{
1267 				if (flags & iflag_transparent)
1268 				{
1269 			  		vertexPtr->color = RGBALIGHT_MAKE(vertices->R,vertices->G,vertices->B, vertices->A);
1270 				}
1271 				else
1272 				{
1273 					vertexPtr->color = RGBLIGHT_MAKE(vertices->R,vertices->G,vertices->B);
1274 				}
1275 			}
1276 			vertexPtr->sz = zvalue;
1277 			vertices++;
1278 			NumVertices++;
1279 		}
1280 	  	while(--i);
1281 	}
1282 	CheckTranslucencyModeIsCorrect(RenderPolygon.TranslucencyMode);
1283 
1284 	// Insert state change for shading model if required
1285     if (D3DShadingMode != D3DSHADE_GOURAUD)
1286 	  {
1287    	   D3DShadingMode = D3DSHADE_GOURAUD;
1288 	   OP_STATE_RENDER(1, ExecBufInstPtr);
1289 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
1290 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
1291 	  }
1292 
1293     // Turn OFF texturing if it is on...
1294     if (CurrTextureHandle != NULL)
1295 	  {
1296        OP_STATE_RENDER(1, ExecBufInstPtr);
1297           STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
1298 	   CurrTextureHandle = NULL;
1299 	  }
1300 
1301 	D3D_OutputTriangles();
1302 }
D3D_PredatorThermalVisionPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)1303 void D3D_PredatorThermalVisionPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
1304 {
1305 	float ZNear;
1306 
1307     // Get ZNear
1308 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1309 
1310 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1311 	{
1312 		int i = RenderPolygon.NumberOfVertices;
1313 		RENDERVERTEX *vertices = renderVerticesPtr;
1314 
1315 		do
1316 		{
1317 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1318 		  	float oneOverZ;
1319 		  	oneOverZ = (1.0)/vertices->Z;
1320 			float zvalue;
1321 
1322 			{
1323 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1324 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1325 				{
1326 					x=Global_VDB_Ptr->VDB_ClipLeft;
1327 				}
1328 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1329 				{
1330 					x=Global_VDB_Ptr->VDB_ClipRight;
1331 				}
1332 
1333 				vertexPtr->sx=x;
1334 			}
1335 			{
1336 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1337 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1338 				{
1339 					y=Global_VDB_Ptr->VDB_ClipUp;
1340 				}
1341 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1342 				{
1343 					y=Global_VDB_Ptr->VDB_ClipDown;
1344 				}
1345 				vertexPtr->sy=y;
1346 			}
1347 			{
1348 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1349 				zvalue = ((zvalue-ZNear)/zvalue);
1350 			}
1351 
1352 			vertexPtr->color = RGBALIGHT_MAKE(vertices->R,vertices->G,vertices->B,vertices->A);//RGBALIGHT_MAKE(255,255,255,255);
1353 			vertexPtr->sz = zvalue;
1354 			vertexPtr->rhw = zvalue;
1355 			vertices++;
1356 			NumVertices++;
1357 		}
1358 	  	while(--i);
1359 	}
1360 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
1361 
1362 	// Insert state change for shading model if required
1363     if (D3DShadingMode != D3DSHADE_GOURAUD)
1364 	  {
1365    	   D3DShadingMode = D3DSHADE_GOURAUD;
1366 	   OP_STATE_RENDER(1, ExecBufInstPtr);
1367 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
1368 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
1369 	  }
1370 
1371     // Turn OFF texturing if it is on...
1372     if (CurrTextureHandle != NULL)
1373 	{
1374        OP_STATE_RENDER(1, ExecBufInstPtr);
1375        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
1376 	   CurrTextureHandle = NULL;
1377 	}
1378 	D3D_OutputTriangles();
1379 }
1380 
D3D_PredatorSeeAliensVisionPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)1381 void D3D_PredatorSeeAliensVisionPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
1382 {
1383 	float ZNear;
1384 
1385     // Get ZNear
1386 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1387 
1388 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1389 	{
1390 		int i = RenderPolygon.NumberOfVertices;
1391 		RENDERVERTEX *vertices = renderVerticesPtr;
1392 
1393 		do
1394 		{
1395 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1396 		  	float oneOverZ;
1397 		  	oneOverZ = (1.0)/vertices->Z;
1398 			float zvalue;
1399 
1400 			{
1401 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1402 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1403 				{
1404 					x=Global_VDB_Ptr->VDB_ClipLeft;
1405 				}
1406 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1407 				{
1408 					x=Global_VDB_Ptr->VDB_ClipRight;
1409 				}
1410 
1411 				vertexPtr->sx=x;
1412 			}
1413 			{
1414 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1415 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1416 				{
1417 					y=Global_VDB_Ptr->VDB_ClipUp;
1418 				}
1419 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1420 				{
1421 					y=Global_VDB_Ptr->VDB_ClipDown;
1422 				}
1423 				vertexPtr->sy=y;
1424 			}
1425 			{
1426 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1427 				zvalue = ((zvalue-ZNear)/zvalue);
1428 			}
1429 
1430 			vertexPtr->color = RGBALIGHT_MAKE(255,0,0,255);//255,255,255,255);
1431 			vertexPtr->sz = zvalue;
1432 			vertexPtr->rhw = zvalue;
1433 			vertices++;
1434 			NumVertices++;
1435 		}
1436 	  	while(--i);
1437 	}
1438 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING)//JUSTSETZ);
1439 
1440 
1441     // Turn OFF texturing if it is on...
1442     if (CurrTextureHandle != NULL)
1443 	{
1444        OP_STATE_RENDER(1, ExecBufInstPtr);
1445        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
1446 	   CurrTextureHandle = NULL;
1447 	}
1448 
1449 	D3D_OutputTriangles();
1450 }
D3D_ZBufferedCloakedPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)1451 void D3D_ZBufferedCloakedPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
1452 {
1453 	extern int CloakingMode;
1454 	extern char CloakedPredatorIsMoving;
1455 	int uOffset = FastRandom()&255;
1456 	int vOffset = FastRandom()&255;
1457 
1458 	int flags;
1459 	int texoffset;
1460 
1461 	D3DTEXTUREHANDLE TextureHandle;
1462 
1463 	float ZNear;
1464 	float RecipW, RecipH;
1465 
1466     // Get ZNear
1467 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1468 
1469 
1470 	// Take header information
1471 	flags = inputPolyPtr->PolyFlags;
1472 
1473 	// We assume bit 15 (TxLocal) HAS been
1474 	// properly cleared this time...
1475 	texoffset = (inputPolyPtr->PolyColour & ClrTxDefn);
1476 
1477 	TextureHandle = (D3DTEXTUREHANDLE)
1478 	          ImageHeaderArray[texoffset].D3DHandle;
1479     // Check for textures that have not loaded
1480 	// properly
1481 
1482     if (TextureHandle == (D3DTEXTUREHANDLE) 0)
1483 	  return;
1484 
1485 
1486 	if(ImageHeaderArray[texoffset].ImageWidth==128)
1487 	{
1488 		RecipW = 1.0 /128.0;
1489 	}
1490 	else
1491 	{
1492 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
1493 		RecipW = (1.0 / width);
1494 	}
1495 	if(ImageHeaderArray[texoffset].ImageHeight==128)
1496 	{
1497 		RecipH = 1.0 / 128.0;
1498 	}
1499 	else
1500 	{
1501 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
1502 		RecipH = (1.0 / height);
1503 	}
1504 
1505 
1506 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1507 	{
1508 		int i = RenderPolygon.NumberOfVertices;
1509 		RENDERVERTEX *vertices = renderVerticesPtr;
1510 
1511 		do
1512 		{
1513 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1514 		  	float oneOverZ;
1515 		  	oneOverZ = (1.0)/vertices->Z;
1516 			float zvalue;
1517 
1518 			{
1519 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1520 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1521 				{
1522 					x=Global_VDB_Ptr->VDB_ClipLeft;
1523 				}
1524 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1525 				{
1526 					x=Global_VDB_Ptr->VDB_ClipRight;
1527 				}
1528 
1529 				vertexPtr->sx=x;
1530 			}
1531 			{
1532 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1533 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1534 				{
1535 					y=Global_VDB_Ptr->VDB_ClipUp;
1536 				}
1537 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1538 				{
1539 					y=Global_VDB_Ptr->VDB_ClipDown;
1540 				}
1541 				vertexPtr->sy=y;
1542 			}
1543 
1544 			vertexPtr->tu = ((float)(vertices->U>>16)+0.5) * RecipW;
1545 			vertexPtr->tv = ((float)(vertices->V>>16)+0.5) * RecipH;
1546 
1547 	   		if (CloakedPredatorIsMoving)
1548 			{
1549 		   		vertexPtr->color = RGBALIGHT_MAKE(vertices->R,vertices->G,vertices->B,vertices->A);
1550 			}
1551 			else
1552 			{
1553 		   		vertexPtr->color = RGBALIGHT_MAKE(vertices->R,vertices->G,vertices->B,vertices->A);
1554 			}
1555 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
1556 
1557 			vertexPtr->rhw = oneOverZ;
1558 
1559 			{
1560 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1561 				zvalue = ((zvalue-ZNear)/zvalue);
1562 			}
1563 
1564 			vertexPtr->sz = zvalue;
1565 
1566 //		   	vertexPtr->specular=RGBALIGHT_MAKE(vertices->SpecularR,vertices->SpecularG,vertices->SpecularB,255);
1567 			vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,255);
1568 
1569 			vertices++;
1570 			NumVertices++;
1571 		}
1572 	  	while(--i);
1573 	}
1574 
1575 	// Insert state change for shading model if required
1576     if (D3DShadingMode != D3DSHADE_GOURAUD)
1577 	  {
1578    	   D3DShadingMode = D3DSHADE_GOURAUD;
1579 	   OP_STATE_RENDER(1, ExecBufInstPtr);
1580 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
1581 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
1582 	  }
1583 
1584 // Insert state change for texturing perspective value
1585 // Note that drawtx3das2d options have ONLY been allowed for here,
1586 // not when the rhw values are generated.  This is a deliberate choice,
1587 // based on the assumption that drawtx3das2d will not be used very often
1588 // and the extra branching at the top of this function will impose a
1589 // greater cost than the (rare) savings in floating pt divisions are worth.
1590 // Or so I claim...
1591 
1592     if (D3DTexturePerspective != Yes)
1593     {
1594 		D3DTexturePerspective = Yes;
1595 		OP_STATE_RENDER(1, ExecBufInstPtr);
1596 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
1597 	}
1598     if (TextureHandle != CurrTextureHandle)
1599 	{
1600        OP_STATE_RENDER(1, ExecBufInstPtr);
1601        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
1602 	   CurrTextureHandle = TextureHandle;
1603 	}
1604     /*
1605     if (TextureHandle != NULL)
1606 	{
1607     	OP_STATE_RENDER(1, ExecBufInstPtr);
1608     	STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
1609 		CurrTextureHandle = NULL;
1610 	}
1611 	*/
1612 
1613 	D3D_OutputTriangles();
1614 }
D3D_CloakedPredatorPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)1615 void D3D_CloakedPredatorPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
1616 {
1617 	float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1618 
1619 	int texoffset = SpecialFXImageNumber;
1620 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[texoffset].D3DHandle;
1621 	float RecipW, RecipH;
1622 
1623 	if(ImageHeaderArray[texoffset].ImageWidth==256)
1624 	{
1625 		RecipW = 1.0 /256.0;
1626 	}
1627 	else
1628 	{
1629 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
1630 		RecipW = (1.0 / width);
1631 	}
1632 	if(ImageHeaderArray[texoffset].ImageHeight==256)
1633 	{
1634 		RecipH = 1.0 / 256.0;
1635 	}
1636 	else
1637 	{
1638 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
1639 		RecipH = (1.0 / height);
1640 	}
1641 
1642 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1643 	{
1644 		int i = RenderPolygon.NumberOfVertices;
1645 		RENDERVERTEX *vertices = renderVerticesPtr;
1646 
1647 		do
1648 		{
1649 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1650 		  	float oneOverZ;
1651 		  	oneOverZ = (1.0)/(vertices->Z);
1652 			float zvalue;
1653 
1654 			vertexPtr->tu = ((float)vertices->U) * RecipW;
1655 			vertexPtr->tv = ((float)vertices->V) * RecipH;
1656 			vertexPtr->rhw = oneOverZ;
1657 
1658 			{
1659 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1660 	   //			zvalue /= 65536.0;
1661 	   		   	zvalue = 1.0 - ZNear/zvalue;
1662 			}
1663 
1664 			{
1665 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1666 
1667 				#if 1
1668 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1669 				{
1670 					x=Global_VDB_Ptr->VDB_ClipLeft;
1671 				}
1672 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1673 				{
1674 					x=Global_VDB_Ptr->VDB_ClipRight;
1675 				}
1676 				#endif
1677 				vertexPtr->sx=x;
1678 			}
1679 			{
1680 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1681 
1682 				#if 1
1683 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1684 				{
1685 					y=Global_VDB_Ptr->VDB_ClipUp;
1686 				}
1687 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1688 				{
1689 					y=Global_VDB_Ptr->VDB_ClipDown;
1690 				}
1691 				#endif
1692 				vertexPtr->sy=y;
1693 
1694 			}
1695 			vertexPtr->color = RGBALIGHT_MAKE(255,255,255,32);
1696 			vertexPtr->sz = zvalue;
1697 
1698 			vertices++;
1699 			NumVertices++;
1700 		}
1701 	  	while(--i);
1702 	}
1703 
1704 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
1705 
1706 	// Insert state change for shading model if required
1707     if (D3DShadingMode != D3DSHADE_GOURAUD)
1708 	  {
1709    	   D3DShadingMode = D3DSHADE_GOURAUD;
1710 	   OP_STATE_RENDER(1, ExecBufInstPtr);
1711 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
1712 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
1713 	  }
1714 
1715 // Insert state change for texturing perspective value
1716 // Note that drawtx3das2d options have ONLY been allowed for here,
1717 // not when the rhw values are generated.  This is a deliberate choice,
1718 // based on the assumption that drawtx3das2d will not be used very often
1719 // and the extra branching at the top of this function will impose a
1720 // greater cost than the (rare) savings in floating pt divisions are worth.
1721 // Or so I claim...
1722 
1723     if (D3DTexturePerspective != Yes)
1724     {
1725 		D3DTexturePerspective = Yes;
1726 		OP_STATE_RENDER(1, ExecBufInstPtr);
1727 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
1728 	}
1729 
1730 	#if 1
1731     if (TextureHandle != CurrTextureHandle)
1732 	{
1733     	OP_STATE_RENDER(1, ExecBufInstPtr);
1734         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
1735 	   	CurrTextureHandle = TextureHandle;
1736 	}
1737 	#else
1738     if (CurrTextureHandle != NoiseTextureHandle)
1739 	{
1740     	OP_STATE_RENDER(1, ExecBufInstPtr);
1741         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NoiseTextureHandle, ExecBufInstPtr);
1742         CurrTextureHandle = NoiseTextureHandle;
1743 	}
1744 	#endif
1745 
1746 	D3D_OutputTriangles();
1747 }
1748 
D3D_ZBufferedTexturedPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)1749 void D3D_ZBufferedTexturedPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
1750 {
1751 	int flags;
1752 	int texoffset;
1753 	int intensity;
1754 
1755 	D3DTEXTUREHANDLE TextureHandle;
1756 
1757 	float ZNear;
1758 	float RecipW, RecipH;
1759 
1760     // Get ZNear
1761 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
1762 
1763 
1764 	// Take header information
1765 	flags = inputPolyPtr->PolyFlags;
1766 
1767 	intensity = (inputPolyPtr->PolyColour >> TxDefn);
1768 	intensity >>= TxDefn;
1769 
1770 	// We assume bit 15 (TxLocal) HAS been
1771 	// properly cleared this time...
1772 	texoffset = (inputPolyPtr->PolyColour & ClrTxDefn);
1773 
1774 	TextureHandle = (D3DTEXTUREHANDLE)
1775 	          ImageHeaderArray[texoffset].D3DHandle;
1776 
1777     // Check for textures that have not loaded
1778 	// properly
1779 
1780     if (TextureHandle == (D3DTEXTUREHANDLE) 0)
1781 	  return;
1782 
1783 	if(ImageHeaderArray[texoffset].ImageWidth==128)
1784 	{
1785 		RecipW = 1.0 /128.0;
1786 	}
1787 	else
1788 	{
1789 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
1790 		RecipW = (1.0 / width);
1791 	}
1792 	if(ImageHeaderArray[texoffset].ImageHeight==128)
1793 	{
1794 		RecipH = 1.0 / 128.0;
1795 	}
1796 	else
1797 	{
1798 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
1799 		RecipH = (1.0 / height);
1800 	}
1801 
1802 
1803 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
1804 	{
1805 		int i = RenderPolygon.NumberOfVertices;
1806 		RENDERVERTEX *vertices = renderVerticesPtr;
1807 
1808 		do
1809 		{
1810 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
1811 		  	float oneOverZ;
1812 		  	oneOverZ = (1.0)/vertices->Z;
1813 			float zvalue;
1814 
1815 			{
1816 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
1817 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
1818 				{
1819 					x=Global_VDB_Ptr->VDB_ClipLeft;
1820 				}
1821 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
1822 				{
1823 					x=Global_VDB_Ptr->VDB_ClipRight;
1824 				}
1825 
1826 				vertexPtr->sx=x;
1827 			}
1828 			{
1829 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
1830 				if (y<Global_VDB_Ptr->VDB_ClipUp)
1831 				{
1832 					y=Global_VDB_Ptr->VDB_ClipUp;
1833 				}
1834 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
1835 				{
1836 					y=Global_VDB_Ptr->VDB_ClipDown;
1837 				}
1838 				vertexPtr->sy=y;
1839 			}
1840 			vertexPtr->tu = ((float)(vertices->U>>16)+0.5) * RecipW;
1841 			vertexPtr->tv = ((float)(vertices->V>>16)+0.5) * RecipH;
1842 			vertexPtr->rhw = oneOverZ;
1843 
1844 			{
1845 				zvalue = vertices->Z+HeadUpDisplayZOffset;
1846 				zvalue = ((zvalue-ZNear)/zvalue);
1847 			}
1848 
1849 		  	 // Different behaviour for different driver modes
1850 			switch (D3DDriverMode)
1851 			{
1852 				case D3DSoftwareRGBDriver:
1853 				{
1854 					if (flags & iflag_nolight)
1855 					{
1856 						vertexPtr->color = DefaultColour;
1857 					}
1858 					else
1859 					{
1860 						int i1shift = intensity;
1861 					  	vertexPtr->color = RGBLIGHT_MAKE(i1shift, i1shift,i1shift);
1862 					}
1863 					break;
1864 				}
1865 
1866 				case D3DSoftwareRampDriver:
1867 				{
1868 					// Blue component only valid for ramp driver
1869 					if (flags & iflag_nolight)
1870 					{
1871 						vertexPtr->color = RGB_MAKE(0,0,DefaultVertexIntensity);
1872 					}
1873 					else
1874 					{
1875 						vertexPtr->color = RGB_MAKE(0,0,intensity);
1876 					}
1877 
1878 					break;
1879 				}
1880 
1881 				case D3DHardwareRGBDriver:
1882 				{
1883 					if (flags & iflag_transparent)
1884 					{
1885 						if (flags & iflag_nolight)
1886 						{
1887 							vertexPtr->color = DefaultAlphaColour;
1888 						}
1889 						else
1890 						{
1891 					  		vertexPtr->color = RGBALIGHT_MAKE(intensity,intensity,intensity, TransparentAlphaValue);
1892 						}
1893 					}
1894 					else
1895 					{
1896 						if (flags & iflag_nolight)
1897 						{
1898 							vertexPtr->color = DefaultColour;
1899 						}
1900 						else
1901 						{
1902 							vertexPtr->color = RGBLIGHT_MAKE(intensity,intensity,intensity);
1903 						}
1904 					}
1905 					break;
1906 				}
1907 
1908 
1909 				default:
1910 				{
1911 					vertexPtr->color = 0;
1912 					break;
1913 				}
1914 			}
1915 			vertexPtr->sz = zvalue;
1916 			vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,255);
1917 			vertices++;
1918 			NumVertices++;
1919 		}
1920 	  	while(--i);
1921 	}
1922 
1923 	if (flags & iflag_transparent)
1924 	{
1925 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
1926 	}
1927 	else
1928 	{
1929 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
1930 	}
1931 
1932 	// Insert state change for shading model if required
1933     if (D3DShadingMode != D3DSHADE_GOURAUD)
1934 	  {
1935    	   D3DShadingMode = D3DSHADE_GOURAUD;
1936 	   OP_STATE_RENDER(1, ExecBufInstPtr);
1937 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
1938 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
1939 	  }
1940 
1941 // Insert state change for texturing perspective value
1942 // Note that drawtx3das2d options have ONLY been allowed for here,
1943 // not when the rhw values are generated.  This is a deliberate choice,
1944 // based on the assumption that drawtx3das2d will not be used very often
1945 // and the extra branching at the top of this function will impose a
1946 // greater cost than the (rare) savings in floating pt divisions are worth.
1947 // Or so I claim...
1948 
1949     if ((flags & iflag_drawtx3das2d) ||
1950 	   (Global_VDB_Ptr->VDB_Flags & ViewDB_Flag_drawtx3das2d))
1951 	  {
1952        if (D3DTexturePerspective != No)
1953 	     {
1954 	      D3DTexturePerspective = No;
1955           OP_STATE_RENDER(1, ExecBufInstPtr);
1956 	        STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE,
1957 	           FALSE, ExecBufInstPtr);
1958 	     }
1959 	  }
1960 	else
1961 	  {
1962        if (D3DTexturePerspective != Yes)
1963 	     {
1964 	      D3DTexturePerspective = Yes;
1965           OP_STATE_RENDER(1, ExecBufInstPtr);
1966 	        STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE,
1967 	           TRUE, ExecBufInstPtr);
1968 	     }
1969 	  }
1970 
1971     if (TextureHandle != CurrTextureHandle)
1972 	  {
1973        OP_STATE_RENDER(1, ExecBufInstPtr);
1974           STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
1975 	   CurrTextureHandle = TextureHandle;
1976 	  }
1977 	D3D_OutputTriangles();
1978 }
1979 #define OUTPUT_TRIANGLE(a,b,c,n) \
1980 ((LPD3DTRIANGLE)ExecBufInstPtr)->v1 = (NumVertices+(a)-(n)); \
1981 ((LPD3DTRIANGLE)ExecBufInstPtr)->v2 = (NumVertices+(b)-(n)); \
1982 ((LPD3DTRIANGLE)ExecBufInstPtr)->v3 = (NumVertices+(c)-(n)); \
1983 ((LPD3DTRIANGLE)ExecBufInstPtr)->wFlags = D3DTRIFLAG_EDGEENABLETRIANGLE; \
1984 ExecBufInstPtr = ((char*)ExecBufInstPtr) + sizeof(D3DTRIANGLE); \
1985 NumberOfRenderedTriangles++;
1986 
D3D_OutputTriangles(void)1987 static void D3D_OutputTriangles(void)
1988 {
1989 	#if 0
1990     OP_STATE_RENDER(1, ExecBufInstPtr);
1991     STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
1992 	#endif
1993 
1994 	#if 0
1995 	/* KJL 18:17:54 24/02/98 - doesn't seem to make any difference! */
1996 	if (QWORD_ALIGNED(ExecBufInstPtr))
1997     {
1998         OP_NOP(ExecBufInstPtr);
1999 	}
2000 	#endif
2001 
2002 	switch(RenderPolygon.NumberOfVertices)
2003 	{
2004 		default:
2005 		case 3:
2006 		{
2007 			OP_TRIANGLE_LIST(1, ExecBufInstPtr);
2008 			OUTPUT_TRIANGLE(0,2,1, 3);
2009 			break;
2010 		}
2011 		case 4:
2012 		{
2013 			OP_TRIANGLE_LIST(2, ExecBufInstPtr);
2014 			OUTPUT_TRIANGLE(0,1,2, 4);
2015 			OUTPUT_TRIANGLE(0,2,3, 4);
2016 			break;
2017 		}
2018 		case 5:
2019 		{
2020 			OP_TRIANGLE_LIST(3, ExecBufInstPtr);
2021 		    OUTPUT_TRIANGLE(0,1,4, 5);
2022 		    OUTPUT_TRIANGLE(1,3,4, 5);
2023 		    OUTPUT_TRIANGLE(1,2,3, 5);
2024 			break;
2025 		}
2026 		case 6:
2027 		{
2028 			OP_TRIANGLE_LIST(4, ExecBufInstPtr);
2029 		    OUTPUT_TRIANGLE(0,4,5, 6);
2030 		    OUTPUT_TRIANGLE(0,3,4, 6);
2031 		    OUTPUT_TRIANGLE(0,2,3, 6);
2032 		    OUTPUT_TRIANGLE(0,1,2, 6);
2033 			break;
2034 		}
2035 		case 7:
2036 		{
2037 			OP_TRIANGLE_LIST(5, ExecBufInstPtr);
2038 		    OUTPUT_TRIANGLE(0,5,6, 7);
2039 		    OUTPUT_TRIANGLE(0,4,5, 7);
2040 		    OUTPUT_TRIANGLE(0,3,4, 7);
2041 		    OUTPUT_TRIANGLE(0,2,3, 7);
2042 		    OUTPUT_TRIANGLE(0,1,2, 7);
2043 			break;
2044 		}
2045 		case 8:
2046 		{
2047 			OP_TRIANGLE_LIST(6, ExecBufInstPtr);
2048 		    OUTPUT_TRIANGLE(0,6,7, 8);
2049 		    OUTPUT_TRIANGLE(0,5,6, 8);
2050 		    OUTPUT_TRIANGLE(0,4,5, 8);
2051 		    OUTPUT_TRIANGLE(0,3,4, 8);
2052 		    OUTPUT_TRIANGLE(0,2,3, 8);
2053 		    OUTPUT_TRIANGLE(0,1,2, 8);
2054 			break;
2055 		}
2056 
2057 
2058 	}
2059 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
2060 	{
2061 	   WriteEndCodeToExecuteBuffer();
2062   	   UnlockExecuteBufferAndPrepareForUse();
2063 	   ExecuteBuffer();
2064   	   LockExecuteBuffer();
2065 	}
2066 }
2067 
D3D_HUD_Setup(void)2068 void D3D_HUD_Setup(void)
2069 {
2070 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
2071 
2072 	// turn off perspective drawing
2073     if (D3DTexturePerspective != No)
2074 	{
2075 		D3DTexturePerspective = No;
2076 		OP_STATE_RENDER(1, ExecBufInstPtr);
2077 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, FALSE, ExecBufInstPtr);
2078 	}
2079 
2080 /*
2081     OP_STATE_RENDER(2, ExecBufInstPtr);
2082 	STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, D3DFILTER_NEAREST, ExecBufInstPtr);
2083 	STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, D3DFILTER_NEAREST, ExecBufInstPtr);
2084     OP_STATE_RENDER(2, ExecBufInstPtr);
2085     STATE_DATA(D3DRENDERSTATE_TEXTUREMAG, DefaultD3DTextureFilterMax, ExecBufInstPtr);
2086     STATE_DATA(D3DRENDERSTATE_TEXTUREMIN, DefaultD3DTextureFilterMin, ExecBufInstPtr);
2087 */
2088 	OP_STATE_RENDER(1, ExecBufInstPtr);
2089     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
2090 
2091 }
2092 
2093 
D3D_HUDQuad_Output(int imageNumber,struct VertexTag * quadVerticesPtr,unsigned int colour)2094 void D3D_HUDQuad_Output(int imageNumber,struct VertexTag *quadVerticesPtr, unsigned int colour)
2095 {
2096 	float RecipW, RecipH;
2097 
2098 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[imageNumber].D3DHandle;
2099 
2100     // Check for textures that have not loaded properly
2101     LOCALASSERT(TextureHandle != (D3DTEXTUREHANDLE) 0);
2102 
2103 	if(ImageHeaderArray[imageNumber].ImageWidth==128)
2104 	{
2105 		RecipW = 1.0f / 128.0f;
2106 	}
2107 	else
2108 	{
2109 		float width = (float) ImageHeaderArray[imageNumber].ImageWidth - 0.0f;
2110 		RecipW = (1.0f / width);
2111 	}
2112 	if(ImageHeaderArray[imageNumber].ImageHeight==128)
2113 	{
2114 		RecipH = 1.0f / 128.0f;
2115 	}
2116 	else
2117 	{
2118 		float height = (float) ImageHeaderArray[imageNumber].ImageHeight - 0.0f;
2119 		RecipH = (1.0f / height);
2120 	}
2121 
2122 
2123 	/* OUTPUT quadVerticesPtr TO EXECUTE BUFFER */
2124 	{
2125 		int i = 4;
2126 		do
2127 		{
2128 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2129 
2130 //			textprint("x %d, y %d, u %d, v %d\n",quadVerticesPtr->X,quadVerticesPtr->Y,quadVerticesPtr->U,quadVerticesPtr->V);
2131 			vertexPtr->sx=quadVerticesPtr->X;
2132 			vertexPtr->sy=quadVerticesPtr->Y;
2133 			vertexPtr->tu = ((float)(quadVerticesPtr->U)) * RecipW;
2134 			vertexPtr->tv = ((float)(quadVerticesPtr->V)) * RecipH;
2135 			vertexPtr->rhw = 1.0f;
2136 	  		vertexPtr->color = colour;
2137 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
2138 
2139 			quadVerticesPtr++;
2140 			NumVertices++;
2141 		}
2142 	  	while(--i);
2143 	}
2144 
2145 	// set correct texture handle
2146     if (TextureHandle != CurrTextureHandle)
2147 	{
2148        OP_STATE_RENDER(1, ExecBufInstPtr);
2149        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2150 	   CurrTextureHandle = TextureHandle;
2151 	}
2152 
2153 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
2154 
2155 
2156 	/* output triangles to execute buffer */
2157 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
2158 	OUTPUT_TRIANGLE(0,1,3, 4);
2159 	OUTPUT_TRIANGLE(1,2,3, 4);
2160 
2161 	/* check to see if buffer is getting full */
2162 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
2163 	{
2164 	   WriteEndCodeToExecuteBuffer();
2165   	   UnlockExecuteBufferAndPrepareForUse();
2166 	   ExecuteBuffer();
2167   	   LockExecuteBuffer();
2168 	}
2169 }
2170 
2171 
D3D_DrawRebellionLogo(unsigned int alpha)2172 void D3D_DrawRebellionLogo(unsigned int alpha)
2173 {
2174 	extern int RebellionLogoImageNumber;
2175 	extern int FoxLogoImageNumber;
2176 	float scale = ScreenDescriptorBlock.SDB_Width/640.0f;
2177 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[FoxLogoImageNumber].D3DHandle;
2178 	int colour = 0xffffff+((alpha>>8)<<24);
2179     // Check for textures that have not loaded properly
2180     LOCALASSERT(TextureHandle != (D3DTEXTUREHANDLE) 0);
2181 
2182 	/* OUTPUT quadVerticesPtr TO EXECUTE BUFFER */
2183 	{
2184 		float x[] = {0.0f,1.0f,1.0f,0.0f};
2185 		float y[] = {0.0f,0.0f,1.0f,1.0f};
2186 		int i;
2187 		for (i=0; i<4; i++)
2188 		{
2189 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2190 			vertexPtr->sx = (x[i]*63.0f+64.0f)*scale;
2191 			vertexPtr->sy = (y[i]*63.0f+4.0f)*scale;
2192 			vertexPtr->sz = 0.0f;
2193 			vertexPtr->tu = x[i];
2194 			vertexPtr->tv = y[i];
2195 			vertexPtr->rhw = 1.0;
2196 	  		vertexPtr->color = colour;
2197 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
2198 
2199 			NumVertices++;
2200 		}
2201 	}
2202 	// set correct texture handle
2203     if (TextureHandle != CurrTextureHandle)
2204 	{
2205        OP_STATE_RENDER(1, ExecBufInstPtr);
2206        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2207 	   CurrTextureHandle = TextureHandle;
2208 	}
2209 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
2210 
2211 
2212 	/* output triangles to execute buffer */
2213 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
2214 	OUTPUT_TRIANGLE(0,1,3, 4);
2215 	OUTPUT_TRIANGLE(1,2,3, 4);
2216 
2217 	/* check to see if buffer is getting full */
2218 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
2219 	{
2220 	   WriteEndCodeToExecuteBuffer();
2221   	   UnlockExecuteBufferAndPrepareForUse();
2222 	   ExecuteBuffer();
2223   	   LockExecuteBuffer();
2224 	}
2225 
2226 	TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[RebellionLogoImageNumber].D3DHandle;
2227 
2228 	{
2229 		float x[] = {0.0f,1.0f,1.0f,0.0f};
2230 		float y[] = {0.0f,0.0f,1.0f,1.0f};
2231 		int i;
2232 		for (i=0; i<4; i++)
2233 		{
2234 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2235 			vertexPtr->sx = (x[i]*127.0f+4.0f+130.0f)*scale;
2236 			vertexPtr->sy = (y[i]*63.0f+4.0f)*scale;
2237 			vertexPtr->sz = 0.0f;
2238 			vertexPtr->tu = x[i];
2239 			vertexPtr->tv = y[i];
2240 			vertexPtr->rhw = 1.0;
2241 	  		vertexPtr->color = colour;
2242 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
2243 
2244 			NumVertices++;
2245 		}
2246 	}
2247 
2248 	// set correct texture handle
2249     if (TextureHandle != CurrTextureHandle)
2250 	{
2251        OP_STATE_RENDER(1, ExecBufInstPtr);
2252        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2253 	   CurrTextureHandle = TextureHandle;
2254 	}
2255 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
2256 
2257 
2258 	/* output triangles to execute buffer */
2259 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
2260 	OUTPUT_TRIANGLE(0,1,3, 4);
2261 	OUTPUT_TRIANGLE(1,2,3, 4);
2262 
2263 	/* check to see if buffer is getting full */
2264 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
2265 	{
2266 	   WriteEndCodeToExecuteBuffer();
2267   	   UnlockExecuteBufferAndPrepareForUse();
2268 	   ExecuteBuffer();
2269   	   LockExecuteBuffer();
2270 	}
2271 }
2272 
2273 
D3D_DrawParticle_Rain(PARTICLE * particlePtr,VECTORCH * prevPositionPtr)2274 void D3D_DrawParticle_Rain(PARTICLE *particlePtr,VECTORCH *prevPositionPtr)
2275 {
2276 	VECTORCH vertices[3];
2277 	vertices[0] = *prevPositionPtr;
2278 
2279 	/* translate second vertex into view space */
2280 	TranslatePointIntoViewspace(&vertices[0]);
2281 
2282 	/* is particle within normal view frustrum ? */
2283 	if((-vertices[0].vx <= vertices[0].vz)
2284 	&&(vertices[0].vx <= vertices[0].vz)
2285 	&&(-vertices[0].vy <= vertices[0].vz)
2286 	&&(vertices[0].vy <= vertices[0].vz))
2287 	{
2288 
2289 		vertices[1] = particlePtr->Position;
2290 		vertices[2] = particlePtr->Position;
2291 		vertices[1].vx += particlePtr->Offset.vx;
2292 		vertices[2].vx -= particlePtr->Offset.vx;
2293 		vertices[1].vz += particlePtr->Offset.vz;
2294 		vertices[2].vz -= particlePtr->Offset.vz;
2295 
2296 		/* translate particle into view space */
2297 		TranslatePointIntoViewspace(&vertices[1]);
2298 		TranslatePointIntoViewspace(&vertices[2]);
2299 
2300 		float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
2301 
2302 
2303 
2304 		/* OUTPUT VERTICES TO EXECUTE BUFFER */
2305 		{
2306 			int i = 3;
2307 			VECTORCH *verticesPtr = vertices;
2308 			do
2309 			{
2310 				D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2311 				int x = (verticesPtr->vx*(Global_VDB_Ptr->VDB_ProjX))/verticesPtr->vz+Global_VDB_Ptr->VDB_CentreX;
2312 				int y = (verticesPtr->vy*(Global_VDB_Ptr->VDB_ProjY))/verticesPtr->vz+Global_VDB_Ptr->VDB_CentreY;
2313 				{
2314 					if (x<Global_VDB_Ptr->VDB_ClipLeft)
2315 					{
2316 						x=Global_VDB_Ptr->VDB_ClipLeft;
2317 					}
2318 					else if (x>Global_VDB_Ptr->VDB_ClipRight)
2319 					{
2320 						x=Global_VDB_Ptr->VDB_ClipRight;
2321 					}
2322 
2323 					vertexPtr->sx=x;
2324 				}
2325 				{
2326 					if (y<Global_VDB_Ptr->VDB_ClipUp)
2327 					{
2328 						y=Global_VDB_Ptr->VDB_ClipUp;
2329 					}
2330 					else if (y>Global_VDB_Ptr->VDB_ClipDown)
2331 					{
2332 						y=Global_VDB_Ptr->VDB_ClipDown;
2333 					}
2334 					vertexPtr->sy=y;
2335 				}
2336 
2337 			  	float oneOverZ = ((float)verticesPtr->vz-ZNear)/(float)verticesPtr->vz;
2338 
2339 				if (i==3) vertexPtr->color = RGBALIGHT_MAKE(0,255,255,32);
2340 				else vertexPtr->color = RGBALIGHT_MAKE(255,255,255,32);
2341 
2342 				vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
2343 				vertexPtr->sz = oneOverZ;
2344 				NumVertices++;
2345 				verticesPtr++;
2346 			}
2347 		  	while(--i);
2348 		}
2349 		if (CurrTextureHandle != NULL)
2350 		{
2351 			OP_STATE_RENDER(1, ExecBufInstPtr);
2352 			STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
2353 			CurrTextureHandle = NULL;
2354 	  	}
2355 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
2356 
2357 
2358 		OP_TRIANGLE_LIST(1, ExecBufInstPtr);
2359 		OUTPUT_TRIANGLE(0,2,1, 3);
2360 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
2361 		{
2362 		   WriteEndCodeToExecuteBuffer();
2363 	  	   UnlockExecuteBufferAndPrepareForUse();
2364 		   ExecuteBuffer();
2365 	  	   LockExecuteBuffer();
2366 		}
2367 	}
2368 
2369 }
2370 
D3D_DrawParticle_Smoke(PARTICLE * particlePtr)2371 void D3D_DrawParticle_Smoke(PARTICLE *particlePtr)
2372 {
2373 	VECTORCH vertices[3];
2374 	vertices[0] = particlePtr->Position;
2375 
2376 	/* translate second vertex into view space */
2377 	TranslatePointIntoViewspace(&vertices[0]);
2378 
2379 	/* is particle within normal view frustrum ? */
2380 	int inView = 0;
2381 
2382 	if(AvP.PlayerType == I_Alien)
2383 	{
2384 		if((-vertices[0].vx <= vertices[0].vz*2)
2385 		&&(vertices[0].vx <= vertices[0].vz*2)
2386 		&&(-vertices[0].vy <= vertices[0].vz*2)
2387 		&&(vertices[0].vy <= vertices[0].vz*2))
2388 		{
2389 			inView = 1;
2390 		}
2391 	}
2392 	else
2393 	{
2394 		if((-vertices[0].vx <= vertices[0].vz)
2395 		&&(vertices[0].vx <= vertices[0].vz)
2396 		&&(-vertices[0].vy <= vertices[0].vz)
2397 		&&(vertices[0].vy <= vertices[0].vz))
2398 		{
2399 			inView = 1;
2400 		}
2401 	}
2402 
2403 	if (inView)
2404 	{
2405 
2406 		vertices[1] = particlePtr->Position;
2407 		vertices[2] = particlePtr->Position;
2408 		vertices[1].vx += ((FastRandom()&15)-8)*2;
2409 		vertices[1].vy += ((FastRandom()&15)-8)*2;
2410 		vertices[1].vz += ((FastRandom()&15)-8)*2;
2411 		vertices[2].vx += ((FastRandom()&15)-8)*2;
2412 		vertices[2].vy += ((FastRandom()&15)-8)*2;
2413 		vertices[2].vz += ((FastRandom()&15)-8)*2;
2414 
2415 		/* translate particle into view space */
2416 		TranslatePointIntoViewspace(&vertices[1]);
2417 		TranslatePointIntoViewspace(&vertices[2]);
2418 
2419 		float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
2420 
2421 
2422 
2423 		/* OUTPUT VERTICES TO EXECUTE BUFFER */
2424 		{
2425 			int i = 3;
2426 			VECTORCH *verticesPtr = vertices;
2427 			do
2428 			{
2429 				D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2430 				int x = (verticesPtr->vx*(Global_VDB_Ptr->VDB_ProjX))/verticesPtr->vz+Global_VDB_Ptr->VDB_CentreX;
2431 				int y = (verticesPtr->vy*(Global_VDB_Ptr->VDB_ProjY))/verticesPtr->vz+Global_VDB_Ptr->VDB_CentreY;
2432 				{
2433 					if (x<Global_VDB_Ptr->VDB_ClipLeft)
2434 					{
2435 						x=Global_VDB_Ptr->VDB_ClipLeft;
2436 					}
2437 					else if (x>Global_VDB_Ptr->VDB_ClipRight)
2438 					{
2439 						x=Global_VDB_Ptr->VDB_ClipRight;
2440 					}
2441 
2442 					vertexPtr->sx=x;
2443 				}
2444 				{
2445 					if (y<Global_VDB_Ptr->VDB_ClipUp)
2446 					{
2447 						y=Global_VDB_Ptr->VDB_ClipUp;
2448 					}
2449 					else if (y>Global_VDB_Ptr->VDB_ClipDown)
2450 					{
2451 						y=Global_VDB_Ptr->VDB_ClipDown;
2452 					}
2453 					vertexPtr->sy=y;
2454 				}
2455 
2456 				float zvalue = (vertices->vz)+HeadUpDisplayZOffset;
2457 				zvalue = ((zvalue-ZNear)/zvalue);
2458 
2459 			  	 // Different behaviour for different driver modes
2460 				switch (D3DDriverMode)
2461 				{
2462  					default:
2463 					case D3DSoftwareRGBDriver:
2464 					case D3DSoftwareRampDriver:
2465 					break;
2466 					case D3DHardwareRGBDriver:
2467 					{
2468 						vertexPtr->color = RGBALIGHT_MAKE((particlePtr->LifeTime>>8),(particlePtr->LifeTime>>8),0,(particlePtr->LifeTime>>7)+64);
2469 						break;
2470 					}
2471 				}
2472 				vertexPtr->sz = zvalue;
2473 				NumVertices++;
2474 				verticesPtr++;
2475 			}
2476 		  	while(--i);
2477 		}
2478 		OP_TRIANGLE_LIST(1, ExecBufInstPtr);
2479 		OUTPUT_TRIANGLE(0,2,1, 3);
2480 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
2481 		{
2482 		   WriteEndCodeToExecuteBuffer();
2483 	  	   UnlockExecuteBufferAndPrepareForUse();
2484 		   ExecuteBuffer();
2485 	  	   LockExecuteBuffer();
2486 		}
2487 	}
2488 
2489 }
2490 
2491 
D3D_DecalSystem_Setup(void)2492 void D3D_DecalSystem_Setup(void)
2493 {
2494 #if 1
2495 	OP_STATE_RENDER(1, ExecBufInstPtr);
2496 //	STATE_DATA(D3DRENDERSTATE_DITHERENABLE, FALSE, ExecBufInstPtr);
2497     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
2498 #endif
2499 }
D3D_DecalSystem_End(void)2500 void D3D_DecalSystem_End(void)
2501 {
2502 #if 1
2503 	OP_STATE_RENDER(1, ExecBufInstPtr);
2504 //	STATE_DATA(D3DRENDERSTATE_DITHERENABLE, TRUE, ExecBufInstPtr);
2505     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
2506 #endif
2507 }
2508 
D3D_Decal_Output(DECAL * decalPtr,RENDERVERTEX * renderVerticesPtr)2509 void D3D_Decal_Output(DECAL *decalPtr,RENDERVERTEX *renderVerticesPtr)
2510 {
2511 	DECAL_DESC *decalDescPtr = &DecalDescription[decalPtr->DecalID];
2512 
2513 	int texoffset;
2514 	D3DTEXTUREHANDLE TextureHandle;
2515 
2516 	float ZNear;
2517 	float RecipW, RecipH;
2518 	int colour;
2519 	int specular=RGBALIGHT_MAKE(0,0,0,0);//255);
2520 
2521     // Get ZNear
2522 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
2523 
2524 	if (decalPtr->DecalID == DECAL_FMV)
2525 	{
2526 		#if !FMV_ON
2527 		return;
2528 		#endif
2529 		TextureHandle=FMVTextureHandle[decalPtr->Centre.vx];
2530 		RecipW = 1.0 /128.0;
2531 		RecipH = 1.0 /128.0;
2532 	    if (TextureHandle != CurrTextureHandle)
2533 		{
2534 	    	OP_STATE_RENDER(1, ExecBufInstPtr);
2535 	        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2536 		   	CurrTextureHandle = TextureHandle;
2537 		}
2538 	}
2539 	else if (decalPtr->DecalID == DECAL_SHAFTOFLIGHT||decalPtr->DecalID == DECAL_SHAFTOFLIGHT_OUTER)
2540 	{
2541 	    if (NULL != CurrTextureHandle)
2542 		{
2543 		   	CurrTextureHandle = NULL;
2544 	    	OP_STATE_RENDER(1, ExecBufInstPtr);
2545 	        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
2546 		}
2547 	}
2548 	else
2549 	{
2550 		texoffset = SpecialFXImageNumber;
2551 		TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[texoffset].D3DHandle;
2552 
2553 	    // Check for textures that have not loaded
2554 		// properly
2555 
2556 	    if (TextureHandle == (D3DTEXTUREHANDLE) 0)
2557 		  return;
2558 
2559 		if(ImageHeaderArray[texoffset].ImageWidth==256)
2560 		{
2561 			RecipW = 1.0 /256.0;
2562 		}
2563 		else
2564 		{
2565 			float width = (float) ImageHeaderArray[texoffset].ImageWidth;
2566 			RecipW = (1.0 / width);
2567 		}
2568 		if(ImageHeaderArray[texoffset].ImageHeight==256)
2569 		{
2570 			RecipH = 1.0 / 256.0;
2571 		}
2572 		else
2573 		{
2574 			float height = (float) ImageHeaderArray[texoffset].ImageHeight;
2575 			RecipH = (1.0 / height);
2576 		}
2577 	    if (TextureHandle != CurrTextureHandle)
2578 		{
2579 	    	OP_STATE_RENDER(1, ExecBufInstPtr);
2580 	        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2581 		   	CurrTextureHandle = TextureHandle;
2582 		}
2583 	}
2584 
2585 	if (decalDescPtr->IsLit)
2586 	{
2587 		int intensity = LightIntensityAtPoint(decalPtr->Vertices);
2588 		colour = RGBALIGHT_MAKE
2589 	  		  	(
2590 	  		   		MUL_FIXED(intensity,decalDescPtr->RedScale[CurrentVisionMode]),
2591 	  		   		MUL_FIXED(intensity,decalDescPtr->GreenScale[CurrentVisionMode]),
2592 	  		   		MUL_FIXED(intensity,decalDescPtr->BlueScale[CurrentVisionMode]),
2593 	  		   		decalDescPtr->Alpha
2594 	  		   	);
2595 	}
2596 	else
2597 	{
2598 		colour = RGBALIGHT_MAKE
2599 			  	(
2600 			   		decalDescPtr->RedScale[CurrentVisionMode],
2601 			   		decalDescPtr->GreenScale[CurrentVisionMode],
2602 			   		decalDescPtr->BlueScale[CurrentVisionMode],
2603 			   		decalDescPtr->Alpha
2604 			   	);
2605 	}
2606 
2607 	if (RAINBOWBLOOD_CHEATMODE)
2608 	{
2609 		colour = RGBALIGHT_MAKE
2610 							  (
2611 							  	FastRandom()&255,
2612 							  	FastRandom()&255,
2613 							  	FastRandom()&255,
2614 							  	decalDescPtr->Alpha
2615 							  );
2616 	}
2617 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
2618 	{
2619 		int i = RenderPolygon.NumberOfVertices;
2620 		RENDERVERTEX *vertices = renderVerticesPtr;
2621 
2622 		do
2623 		{
2624 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2625 		  	float oneOverZ;
2626 		  	oneOverZ = (1.0)/vertices->Z;
2627 			float zvalue;
2628 
2629 			{
2630 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
2631 
2632 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
2633 				{
2634 					x=Global_VDB_Ptr->VDB_ClipLeft;
2635 				}
2636 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
2637 				{
2638 					x=Global_VDB_Ptr->VDB_ClipRight;
2639 				}
2640 
2641 				vertexPtr->sx=x;
2642 			}
2643 			{
2644 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
2645 
2646 				if (y<Global_VDB_Ptr->VDB_ClipUp)
2647 				{
2648 					y=Global_VDB_Ptr->VDB_ClipUp;
2649 				}
2650 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
2651 				{
2652 					y=Global_VDB_Ptr->VDB_ClipDown;
2653 				}
2654 				vertexPtr->sy=y;
2655 
2656 			}
2657 			vertexPtr->tu = ((float)(vertices->U>>16)+.5) * RecipW;
2658 			vertexPtr->tv = ((float)(vertices->V>>16)+.5) * RecipH;
2659 			vertexPtr->rhw = oneOverZ;
2660 
2661 			{
2662 				zvalue = (vertices->Z)+HeadUpDisplayZOffset-50;
2663 			   	zvalue = ((zvalue-ZNear)/zvalue);
2664 			}
2665 
2666 			vertexPtr->color = colour;
2667 
2668 			vertexPtr->sz = zvalue;
2669 
2670 		   	vertexPtr->specular= specular;//RGBALIGHT_MAKE(vertices->SpecularR,vertices->SpecularG,vertices->SpecularB,fog);
2671 
2672 
2673 			vertices++;
2674 			NumVertices++;
2675 		}
2676 	  	while(--i);
2677 	}
2678 
2679 	/* Check translucency mode */
2680 	CheckTranslucencyModeIsCorrect(decalDescPtr->TranslucencyType);
2681 
2682 
2683     if (D3DTexturePerspective != Yes)
2684     {
2685 		D3DTexturePerspective = Yes;
2686 		OP_STATE_RENDER(1, ExecBufInstPtr);
2687 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
2688 	}
2689 
2690 
2691 
2692 	D3D_OutputTriangles();
2693 }
D3D_Particle_Output(PARTICLE * particlePtr,RENDERVERTEX * renderVerticesPtr)2694 void D3D_Particle_Output(PARTICLE *particlePtr,RENDERVERTEX *renderVerticesPtr)
2695 {
2696 	#if 1
2697 	PARTICLE_DESC *particleDescPtr = &ParticleDescription[particlePtr->ParticleID];
2698 
2699 	int texoffset = SpecialFXImageNumber;
2700 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[texoffset].D3DHandle;
2701 	float RecipW, RecipH;
2702 
2703 
2704     // Check for textures that have not loaded
2705 	// properly
2706 
2707     if (TextureHandle == (D3DTEXTUREHANDLE) 0)
2708 	  return;
2709 
2710 	if(ImageHeaderArray[texoffset].ImageWidth==256)
2711 	{
2712 		RecipW = 1.0 /256.0;
2713 	}
2714 	else
2715 	{
2716 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
2717 		RecipW = (1.0 / width);
2718 	}
2719 	if(ImageHeaderArray[texoffset].ImageHeight==256)
2720 	{
2721 		RecipH = 1.0 / 256.0;
2722 	}
2723 	else
2724 	{
2725 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
2726 		RecipH = (1.0 / height);
2727 	}
2728 
2729 	int colour;
2730 
2731 	if (particleDescPtr->IsLit && !(particlePtr->ParticleID==PARTICLE_ALIEN_BLOOD && CurrentVisionMode==VISION_MODE_PRED_SEEALIENS) )
2732 	{
2733 		int intensity = LightIntensityAtPoint(&particlePtr->Position);
2734 		if (particlePtr->ParticleID==PARTICLE_SMOKECLOUD || particlePtr->ParticleID==PARTICLE_ANDROID_BLOOD)
2735 		{
2736 			colour = RGBALIGHT_MAKE
2737 				  	(
2738 				   		MUL_FIXED(intensity,particlePtr->ColourComponents.Red),
2739 				   		MUL_FIXED(intensity,particlePtr->ColourComponents.Green),
2740 				   		MUL_FIXED(intensity,particlePtr->ColourComponents.Blue),
2741 				   		particlePtr->ColourComponents.Alpha
2742 				   	);
2743 
2744 		}
2745 		else
2746 		{
2747 			colour = RGBALIGHT_MAKE
2748 				  	(
2749 				   		MUL_FIXED(intensity,particleDescPtr->RedScale[CurrentVisionMode]),
2750 				   		MUL_FIXED(intensity,particleDescPtr->GreenScale[CurrentVisionMode]),
2751 				   		MUL_FIXED(intensity,particleDescPtr->BlueScale[CurrentVisionMode]),
2752 				   		particleDescPtr->Alpha
2753 				   	);
2754 		}
2755 	}
2756 	else
2757 	{
2758 		colour = particlePtr->Colour;
2759 	}
2760 	if (RAINBOWBLOOD_CHEATMODE)
2761 	{
2762 		colour = RGBALIGHT_MAKE
2763 							  (
2764 							  	FastRandom()&255,
2765 							  	FastRandom()&255,
2766 							  	FastRandom()&255,
2767 							  	particleDescPtr->Alpha
2768 							  );
2769 	}
2770 
2771 
2772 	{
2773 		float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
2774 
2775 
2776 		{
2777 			int i = RenderPolygon.NumberOfVertices;
2778 			RENDERVERTEX *vertices = renderVerticesPtr;
2779 
2780 			do
2781 			{
2782 				D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2783 			  	float oneOverZ = (1.0)/vertices->Z;
2784 				float zvalue;
2785 
2786 				{
2787 					int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
2788 
2789 					if (x<Global_VDB_Ptr->VDB_ClipLeft)
2790 					{
2791 						x=Global_VDB_Ptr->VDB_ClipLeft;
2792 					}
2793 					else if (x>Global_VDB_Ptr->VDB_ClipRight)
2794 					{
2795 						x=Global_VDB_Ptr->VDB_ClipRight;
2796 					}
2797 
2798 					vertexPtr->sx=x;
2799 				}
2800 				{
2801 					int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
2802 
2803 					if (y<Global_VDB_Ptr->VDB_ClipUp)
2804 					{
2805 						y=Global_VDB_Ptr->VDB_ClipUp;
2806 					}
2807 					else if (y>Global_VDB_Ptr->VDB_ClipDown)
2808 					{
2809 						y=Global_VDB_Ptr->VDB_ClipDown;
2810 					}
2811 					vertexPtr->sy=y;
2812 
2813 				}
2814 				vertexPtr->tu = ((float)(vertices->U>>16)+.5) * RecipW;
2815 				vertexPtr->tv = ((float)(vertices->V>>16)+.5) * RecipH;
2816 				vertexPtr->rhw = oneOverZ;
2817 				if (particleDescPtr->IsDrawnInFront)
2818 				{
2819 					zvalue = 0.0f;
2820 				}
2821 				else if (particleDescPtr->IsDrawnAtBack)
2822 				{
2823 					zvalue = 1.0f;
2824 				}
2825 				else
2826 				{
2827 					zvalue = 1.0f - ZNear*oneOverZ;
2828 				}
2829 
2830 				vertexPtr->color = colour;
2831 				vertexPtr->sz = zvalue;
2832 	 		   	vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,255);//RGBALIGHT_MAKE(vertices->SpecularR,vertices->SpecularG,vertices->SpecularB,fog);
2833 
2834 	 			NumVertices++;
2835 				vertices++;
2836 			}
2837 		  	while(--i);
2838 		}
2839 
2840 		// set correct texture handle
2841 	    if (TextureHandle != CurrTextureHandle)
2842 		{
2843 	       OP_STATE_RENDER(1, ExecBufInstPtr);
2844 	       STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2845 		   CurrTextureHandle = TextureHandle;
2846 		}
2847 
2848 		CheckTranslucencyModeIsCorrect(particleDescPtr->TranslucencyType);
2849 
2850 	    if (D3DTexturePerspective != Yes)
2851 	    {
2852 			D3DTexturePerspective = Yes;
2853 			OP_STATE_RENDER(1, ExecBufInstPtr);
2854 			STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
2855 		}
2856 
2857 
2858 
2859 		D3D_OutputTriangles();
2860 
2861 	}
2862 	#endif
2863 }
D3D_FMVParticle_Output(RENDERVERTEX * renderVerticesPtr)2864 void D3D_FMVParticle_Output(RENDERVERTEX *renderVerticesPtr)
2865 {
2866 	D3DTEXTUREHANDLE TextureHandle = FMVTextureHandle[0];
2867 	float RecipW, RecipH;
2868 
2869 	RecipW = 1.0 /128.0;
2870 	RecipH = 1.0 /128.0;
2871 
2872 	int colour = FMVParticleColour&0xffffff;
2873 
2874 	{
2875 		float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
2876 
2877 
2878 		{
2879 			int i = RenderPolygon.NumberOfVertices;
2880 			RENDERVERTEX *vertices = renderVerticesPtr;
2881 
2882 			do
2883 			{
2884 				D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
2885 			  	float oneOverZ = (1.0)/vertices->Z;
2886 				float zvalue;
2887 
2888 				{
2889 					int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
2890 
2891 					if (x<Global_VDB_Ptr->VDB_ClipLeft)
2892 					{
2893 						x=Global_VDB_Ptr->VDB_ClipLeft;
2894 					}
2895 					else if (x>Global_VDB_Ptr->VDB_ClipRight)
2896 					{
2897 						x=Global_VDB_Ptr->VDB_ClipRight;
2898 					}
2899 
2900 					vertexPtr->sx=x;
2901 				}
2902 				{
2903 					int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
2904 
2905 					if (y<Global_VDB_Ptr->VDB_ClipUp)
2906 					{
2907 						y=Global_VDB_Ptr->VDB_ClipUp;
2908 					}
2909 					else if (y>Global_VDB_Ptr->VDB_ClipDown)
2910 					{
2911 						y=Global_VDB_Ptr->VDB_ClipDown;
2912 					}
2913 					vertexPtr->sy=y;
2914 
2915 				}
2916 				vertexPtr->tu = ((float)(vertices->U>>16)) * RecipW;
2917 				vertexPtr->tv = ((float)(vertices->V>>16)) * RecipH;
2918 				vertexPtr->rhw = oneOverZ;
2919 				zvalue = 1.0 - ZNear*oneOverZ;
2920 
2921 //				vertexPtr->color = colour;
2922 				vertexPtr->color = (colour)+(vertices->A<<24);
2923 				vertexPtr->sz = zvalue;
2924 	 		   	vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,255);//RGBALIGHT_MAKE(vertices->SpecularR,vertices->SpecularG,vertices->SpecularB,fog);
2925 
2926 	 			NumVertices++;
2927 				vertices++;
2928 			}
2929 		  	while(--i);
2930 		}
2931 
2932 		// set correct texture handle
2933 	    if (TextureHandle != CurrTextureHandle)
2934 		{
2935 	       OP_STATE_RENDER(1, ExecBufInstPtr);
2936 	       STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
2937 		   CurrTextureHandle = TextureHandle;
2938 		}
2939 
2940 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
2941 
2942 	    if (D3DTexturePerspective != Yes)
2943 	    {
2944 			D3DTexturePerspective = Yes;
2945 			OP_STATE_RENDER(1, ExecBufInstPtr);
2946 			STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
2947 		}
2948 
2949 
2950 
2951 		D3D_OutputTriangles();
2952 
2953 	}
2954 }
2955 
2956 
2957 extern int CloakingPhase;
2958 extern int NumActiveBlocks;
2959 extern DISPLAYBLOCK *ActiveBlockList[];
2960 extern int GlobalAmbience;
2961 extern unsigned char *ScreenBuffer;
2962 extern long BackBufferPitch;
2963 
2964 unsigned short FlameFunction(int x, int y);
2965 void InitRandomArrays(void);
2966 int Turbulence(int x, int y, int t);
2967 
2968 void UpdateForceField(void);
2969 void D3D_DrawForceField(int xOrigin, int yOrigin, int zOrigin, int fieldType);
2970 
2971 void UpdateWaterFall(void);
2972 void D3D_DrawWaterFall(int xOrigin, int yOrigin, int zOrigin);
2973 void D3D_DrawPowerFence(int xOrigin, int yOrigin, int zOrigin, int xScale, int yScale, int zScale);
2974 void D3D_DrawExplosion(int xOrigin, int yOrigin, int zOrigin, int size);
2975 
2976 void D3D_DrawWaterPatch(int xOrigin, int yOrigin, int zOrigin);
2977 
2978 void D3D_DrawWaterOctagonPatch(int xOrigin, int yOrigin, int zOrigin, int xOffset, int zOffset);
2979 
2980 int LightSourceWaterPoint(VECTORCH *pointPtr,int offset);
2981 void D3D_DrawWaterMesh_Unclipped(void);
2982 void D3D_DrawWaterMesh_Clipped(void);
2983 
2984 
2985 void D3D_DrawMoltenMetal(int xOrigin, int yOrigin, int zOrigin);
2986 void D3D_DrawMoltenMetalMesh_Unclipped(void);
2987 void D3D_DrawMoltenMetalMesh_Clipped(void);
2988 
2989 
2990 //#define WATER_POLY_SCALE 256
2991 int MeshXScale;
2992 int MeshZScale;
2993 int WaterFallBase;
PostLandscapeRendering(void)2994 void PostLandscapeRendering(void)
2995 {
2996 	extern int NumOnScreenBlocks;
2997 	extern DISPLAYBLOCK *OnScreenBlockList[];
2998 	int numOfObjects = NumOnScreenBlocks;
2999 
3000 	extern char LevelName[];
3001 
3002   	CurrentRenderStates.FogIsOn = 1;
3003 
3004 	if (!strcmp(LevelName,"fall")||!strcmp(LevelName,"fall_m"))
3005 	{
3006 		char drawWaterFall = 0;
3007 		char drawStream = 0;
3008 
3009 		while(numOfObjects)
3010 		{
3011 			DISPLAYBLOCK *objectPtr = OnScreenBlockList[--numOfObjects];
3012 			MODULE *modulePtr = objectPtr->ObMyModule;
3013 
3014 			/* if it's a module, which isn't inside another module */
3015 			if (modulePtr && modulePtr->name)
3016 			{
3017 				if( (!strcmp(modulePtr->name,"fall01"))
3018 				  ||(!strcmp(modulePtr->name,"well01"))
3019 				  ||(!strcmp(modulePtr->name,"well02"))
3020 				  ||(!strcmp(modulePtr->name,"well03"))
3021 				  ||(!strcmp(modulePtr->name,"well04"))
3022 				  ||(!strcmp(modulePtr->name,"well05"))
3023 				  ||(!strcmp(modulePtr->name,"well06"))
3024 				  ||(!strcmp(modulePtr->name,"well07"))
3025 				  ||(!strcmp(modulePtr->name,"well08"))
3026 				  ||(!strcmp(modulePtr->name,"well")))
3027 				{
3028 					drawWaterFall = 1;
3029 				}
3030 				else if( (!strcmp(modulePtr->name,"stream02"))
3031 				       ||(!strcmp(modulePtr->name,"stream03"))
3032 				       ||(!strcmp(modulePtr->name,"watergate")))
3033 				{
3034 		   			drawStream = 1;
3035 				}
3036 			}
3037 		}
3038 
3039 		if (drawWaterFall)
3040 		{
3041 			// Turn OFF texturing if it is on...
3042 			if (CurrTextureHandle != NULL)
3043 			{
3044 				OP_STATE_RENDER(1, ExecBufInstPtr);
3045 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
3046 				CurrTextureHandle = NULL;
3047 			}
3048 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3049 			if (NumVertices)
3050 			{
3051 			   WriteEndCodeToExecuteBuffer();
3052 		  	   UnlockExecuteBufferAndPrepareForUse();
3053 			   ExecuteBuffer();
3054 		  	   LockExecuteBuffer();
3055 			}
3056 			OP_STATE_RENDER(1, ExecBufInstPtr);
3057 		    //STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_ALWAYS, ExecBufInstPtr);
3058 			STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
3059 
3060 	   		//UpdateWaterFall();
3061 			WaterFallBase = 109952;
3062 
3063 			MeshZScale = (66572-51026)/15;
3064 			MeshXScale = (109952+3039)/45;
3065 
3066 	   		D3D_DrawWaterFall(175545,-3039,51026);
3067 //			MeshZScale = -(538490-392169);
3068 //			MeshXScale = 55000;
3069 	//		D3D_DrawWaterPatch(-100000, WaterFallBase, 538490);
3070 
3071 			OP_STATE_RENDER(1, ExecBufInstPtr);
3072 		    //STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
3073 		    STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
3074 		}
3075 		if (drawStream)
3076 		{
3077 			int x = 68581;
3078 			int y = 12925;
3079 			int z = 93696;
3080 			MeshXScale = (87869-68581);
3081 			MeshZScale = (105385-93696);
3082 			{
3083 				extern void CheckForObjectsInWater(int minX, int maxX, int minZ, int maxZ, int averageY);
3084 				CheckForObjectsInWater(x, x+MeshXScale, z, z+MeshZScale, y);
3085 			}
3086 
3087 			WaterXOrigin=x;
3088 			WaterZOrigin=z;
3089 			WaterUScale = 4.0f/(float)MeshXScale;
3090 			WaterVScale = 4.0f/(float)MeshZScale;
3091 		 	MeshXScale/=4;
3092 		 	MeshZScale/=2;
3093 
3094 			// Turn OFF texturing if it is on...
3095 			D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[ChromeImageNumber].D3DHandle;
3096 			if (CurrTextureHandle != TextureHandle)
3097 			{
3098 				OP_STATE_RENDER(1, ExecBufInstPtr);
3099 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
3100 				CurrTextureHandle = TextureHandle;
3101 			}
3102 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3103 			if (NumVertices)
3104 			{
3105 			   WriteEndCodeToExecuteBuffer();
3106 		  	   UnlockExecuteBufferAndPrepareForUse();
3107 			   ExecuteBuffer();
3108 		  	   LockExecuteBuffer();
3109 			}
3110 		 	D3D_DrawWaterPatch(x, y, z);
3111 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z);
3112 		 	D3D_DrawWaterPatch(x+MeshXScale*2, y, z);
3113 		 	D3D_DrawWaterPatch(x+MeshXScale*3, y, z);
3114 		 	D3D_DrawWaterPatch(x, y, z+MeshZScale);
3115 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale);
3116 		 	D3D_DrawWaterPatch(x+MeshXScale*2, y, z+MeshZScale);
3117 		 	D3D_DrawWaterPatch(x+MeshXScale*3, y, z+MeshZScale);
3118 		}
3119 	}
3120 	#if 0
3121 	else if ( (!__stricmp(LevelName,"e3demo")) || (!__stricmp(LevelName,"e3demosp")) )
3122 	{
3123 		int drawOctagonPool = -1;
3124 		int drawFMV = -1;
3125 		int drawPredatorFMV = -1;
3126 		int drawSwirlyFMV = -1;
3127 		int drawSwirlyFMV2 = -1;
3128 		int drawSwirlyFMV3 = -1;
3129 		while(numOfObjects)
3130 		{
3131 			DISPLAYBLOCK *objectPtr = OnScreenBlockList[--numOfObjects];
3132 			MODULE *modulePtr = objectPtr->ObMyModule;
3133 
3134 			/* if it's a module, which isn't inside another module */
3135 			if (modulePtr && modulePtr->name)
3136 			{
3137 				if(!__stricmp(modulePtr->name,"water1"))
3138 				{
3139 					drawOctagonPool = modulePtr->m_index;
3140 				}
3141 				else if(!__stricmp(modulePtr->name,"marine01b"))
3142 				{
3143 					drawFMV = modulePtr->m_index;
3144 				}
3145 				else if(!_stricmp(modulePtr->name,"predator01"))
3146 				{
3147 					drawPredatorFMV = modulePtr->m_index;
3148 				}
3149 				else if(!_stricmp(modulePtr->name,"toptopgr01"))
3150 				{
3151 					drawSwirlyFMV = modulePtr->m_index;
3152 				}
3153 				else if(!_stricmp(modulePtr->name,"grille04"))
3154 				{
3155 					drawSwirlyFMV2 = modulePtr->m_index;
3156 				}
3157 				#if 0
3158 				else if(!_stricmp(modulePtr->name,"marine05"))
3159 				{
3160 					drawSwirlyFMV3 = modulePtr->m_index;
3161 				}
3162 				#endif
3163 			}
3164 		}
3165 		#if FMV_ON
3166 //		UpdateFMVTextures(3);
3167 
3168 
3169 		if (drawFMV!=-1)
3170 		{
3171 			DECAL fmvDecal =
3172 			{
3173 				DECAL_FMV,
3174 			};
3175 			fmvDecal.ModuleIndex = drawFMV;
3176 			fmvDecal.UOffset = 0;
3177 
3178 			UpdateFMVTextures(4);
3179 
3180 			for (int z=0; z<6; z++)
3181 			{
3182 				for (int y=0; y<3; y++)
3183 				{
3184 					fmvDecal.Vertices[0].vx = -149;
3185 					fmvDecal.Vertices[1].vx = -149;
3186 					fmvDecal.Vertices[2].vx = -149;
3187 					fmvDecal.Vertices[3].vx = -149;
3188 
3189 					fmvDecal.Vertices[0].vy = -3254+y*744;
3190 					fmvDecal.Vertices[1].vy = -3254+y*744;
3191 					fmvDecal.Vertices[2].vy = -3254+y*744+744;
3192 					fmvDecal.Vertices[3].vy = -3254+y*744+744;
3193 
3194 					fmvDecal.Vertices[0].vz = 49440+z*993;
3195 					fmvDecal.Vertices[1].vz = 49440+z*993+993;
3196 					fmvDecal.Vertices[2].vz = 49440+z*993+993;
3197 					fmvDecal.Vertices[3].vz = 49440+z*993;
3198 					fmvDecal.Centre.vx = ((z+y)%3)+1;
3199 					RenderDecal(&fmvDecal);
3200 				}
3201 			}
3202 		}
3203 		if (drawPredatorFMV!=-1)
3204 		{
3205 			DECAL fmvDecal =
3206 			{
3207 				DECAL_FMV,
3208 			};
3209 			fmvDecal.ModuleIndex = drawPredatorFMV;
3210 			fmvDecal.UOffset = 0;
3211 
3212 			UpdateFMVTextures(4);
3213 
3214 			for (int z=0; z<12; z++)
3215 			{
3216 				for (int y=0; y<7; y++)
3217 				{
3218 					fmvDecal.Vertices[0].vx = -7164;
3219 					fmvDecal.Vertices[1].vx = -7164;
3220 					fmvDecal.Vertices[2].vx = -7164;
3221 					fmvDecal.Vertices[3].vx = -7164;
3222 
3223 					fmvDecal.Vertices[0].vy = -20360+y*362;
3224 					fmvDecal.Vertices[1].vy = -20360+y*362;
3225 					fmvDecal.Vertices[2].vy = -20360+y*362+362;
3226 					fmvDecal.Vertices[3].vy = -20360+y*362+362;
3227 
3228 					fmvDecal.Vertices[0].vz = 1271+z*483+483;
3229 					fmvDecal.Vertices[1].vz = 1271+z*483;
3230 					fmvDecal.Vertices[2].vz = 1271+z*483;
3231 					fmvDecal.Vertices[3].vz = 1271+z*483+483;
3232 					fmvDecal.Centre.vx = (z+y)%3;
3233 					RenderDecal(&fmvDecal);
3234 				}
3235 			}
3236 		}
3237 
3238 		#endif
3239 
3240 		if (drawSwirlyFMV!=-1)
3241 		{
3242 			UpdateFMVTextures(1);
3243 			D3D_DrawSwirlyFMV(30000,-12500,0);
3244 		}
3245 		if (drawSwirlyFMV2!=-1)
3246 		{
3247 			UpdateFMVTextures(1);
3248 			D3D_DrawSwirlyFMV(2605,-6267-2000,17394-3200);
3249 		}
3250 
3251 		if (drawSwirlyFMV3!=-1)
3252 		{
3253 //			UpdateFMVTextures(1);
3254 			D3D_DrawSwirlyFMV(5117,3456-3000,52710-2000);
3255 		}
3256 		if (drawOctagonPool!=-1)
3257 		{
3258 			#if FMV_ON
3259 			UpdateFMVTextures(1);
3260 
3261 			MeshXScale = (3000);
3262 			MeshZScale = (4000);
3263 			D3D_DrawFMVOnWater(-1000,3400,22000);
3264 			{
3265 				DECAL fmvDecal =
3266 				{
3267 					DECAL_FMV,
3268 					{
3269 					{0,-2500,29000},
3270 					{2000,-2500,29000},
3271 					{2000,-2500+750*2,29000},
3272 					{0,-2500+750*2,29000}
3273 					},
3274 					0
3275 				};
3276 				fmvDecal.ModuleIndex = drawOctagonPool;
3277 				fmvDecal.Centre.vx = 0;
3278 				fmvDecal.UOffset = 0;
3279 
3280 				RenderDecal(&fmvDecal);
3281 			}
3282 			#endif
3283 
3284 			int highDetailRequired = 1;
3285 			int x = 1023;
3286 			int y = 3400;
3287 			int z = 27536;
3288 
3289 			{
3290 				int dx = Player->ObWorld.vx - x;
3291 				if (dx< -8000 || dx > 8000)
3292 				{
3293 					highDetailRequired = 0;
3294 				}
3295 				else
3296 				{
3297 					int dz = Player->ObWorld.vz - z;
3298 					if (dz< -8000 || dz > 8000)
3299 					{
3300 						highDetailRequired = 0;
3301 					}
3302 				}
3303 			}
3304 			MeshXScale = 7700;
3305 			MeshZScale = 7700;
3306 			{
3307 				extern void CheckForObjectsInWater(int minX, int maxX, int minZ, int maxZ, int averageY);
3308 				CheckForObjectsInWater(x-MeshXScale, x+MeshXScale, z-MeshZScale, z+MeshZScale, y);
3309 			}
3310 
3311 			MeshXScale /=15;
3312 			MeshZScale /=15;
3313 
3314 			// Turn OFF texturing if it is on...
3315 			D3DTEXTUREHANDLE TextureHandle = NULL;
3316 			if (CurrTextureHandle != TextureHandle)
3317 			{
3318 				OP_STATE_RENDER(1, ExecBufInstPtr);
3319 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
3320 				CurrTextureHandle = TextureHandle;
3321 			}
3322 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3323 			if (NumVertices)
3324 			{
3325 			   WriteEndCodeToExecuteBuffer();
3326 		  	   UnlockExecuteBufferAndPrepareForUse();
3327 			   ExecuteBuffer();
3328 		  	   LockExecuteBuffer();
3329 			}
3330 			if (highDetailRequired)
3331 			{
3332 				MeshXScale /= 2;
3333 				MeshZScale /= 2;
3334 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3335 				D3D_DrawWaterOctagonPatch(x,y,z,15,0);
3336 				D3D_DrawWaterOctagonPatch(x,y,z,0,15);
3337 				D3D_DrawWaterOctagonPatch(x,y,z,15,15);
3338 				MeshXScale = -MeshXScale;
3339 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3340 				D3D_DrawWaterOctagonPatch(x,y,z,15,0);
3341 				D3D_DrawWaterOctagonPatch(x,y,z,0,15);
3342 				D3D_DrawWaterOctagonPatch(x,y,z,15,15);
3343 				MeshZScale = -MeshZScale;
3344 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3345 				D3D_DrawWaterOctagonPatch(x,y,z,15,0);
3346 				D3D_DrawWaterOctagonPatch(x,y,z,0,15);
3347 				D3D_DrawWaterOctagonPatch(x,y,z,15,15);
3348 				MeshXScale = -MeshXScale;
3349 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3350 				D3D_DrawWaterOctagonPatch(x,y,z,15,0);
3351 				D3D_DrawWaterOctagonPatch(x,y,z,0,15);
3352 				D3D_DrawWaterOctagonPatch(x,y,z,15,15);
3353 			}
3354 			else
3355 			{
3356 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3357 				MeshXScale = -MeshXScale;
3358 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3359 				MeshZScale = -MeshZScale;
3360 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3361 				MeshXScale = -MeshXScale;
3362 				D3D_DrawWaterOctagonPatch(x,y,z,0,0);
3363 			}
3364 
3365 		}
3366 	}
3367 	#endif
3368 	else if (!_stricmp(LevelName,"hangar"))
3369 	{
3370 	   	#if FMV_ON
3371 		#if WIBBLY_FMV_ON
3372 		UpdateFMVTextures(1);
3373 	   	D3D_DrawFMV(FmvPosition.vx,FmvPosition.vy,FmvPosition.vz);
3374 		#endif
3375 		#endif
3376 		#if 0
3377 		{
3378 			VECTORCH v = {49937,-4000,-37709};		// hangar
3379 			D3D_DrawCable(&v);
3380 		}
3381 		#endif
3382 	}
3383 	else if (!_stricmp(LevelName,"invasion_a"))
3384 	{
3385 		char drawWater = 0;
3386 		char drawEndWater = 0;
3387 
3388 		while(numOfObjects)
3389 		{
3390 			DISPLAYBLOCK *objectPtr = OnScreenBlockList[--numOfObjects];
3391 			MODULE *modulePtr = objectPtr->ObMyModule;
3392 
3393 			/* if it's a module, which isn't inside another module */
3394 			if (modulePtr && modulePtr->name)
3395 			{
3396 				if( (!strcmp(modulePtr->name,"hivepool"))
3397 				  ||(!strcmp(modulePtr->name,"hivepool04")))
3398 				{
3399 					drawWater = 1;
3400 					break;
3401 				}
3402 				else
3403 				{
3404 					if(!strcmp(modulePtr->name,"shaftbot"))
3405 					{
3406 						drawEndWater = 1;
3407 					}
3408 					if((!_stricmp(modulePtr->name,"shaft01"))
3409 					 ||(!_stricmp(modulePtr->name,"shaft02"))
3410 					 ||(!_stricmp(modulePtr->name,"shaft03"))
3411 					 ||(!_stricmp(modulePtr->name,"shaft04"))
3412 					 ||(!_stricmp(modulePtr->name,"shaft05"))
3413 					 ||(!_stricmp(modulePtr->name,"shaft06")))
3414 					{
3415 						extern void HandleRainShaft(MODULE *modulePtr, int bottomY, int topY, int numberOfRaindrops);
3416 						HandleRainShaft(modulePtr, -11726,-107080,10);
3417 						drawEndWater = 1;
3418 						break;
3419 					}
3420 				}
3421 			}
3422 
3423 		}
3424 
3425 		if (drawWater)
3426 		{
3427 			int x = 20767;
3428 			int y = -36000+200;
3429 			int z = 30238;
3430 			MeshXScale = (36353-20767);
3431 			MeshZScale = (41927-30238);
3432 			{
3433 				extern void CheckForObjectsInWater(int minX, int maxX, int minZ, int maxZ, int averageY);
3434 				CheckForObjectsInWater(x, x+MeshXScale, z, z+MeshZScale, y);
3435 			}
3436 
3437 			WaterXOrigin=x;
3438 			WaterZOrigin=z;
3439 			WaterUScale = 4.0f/(float)MeshXScale;
3440 			WaterVScale = 4.0f/(float)MeshZScale;
3441 		 	MeshXScale/=4;
3442 		 	MeshZScale/=2;
3443 
3444 			// Turn OFF texturing if it is on...
3445 			D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[ChromeImageNumber].D3DHandle;
3446 			if (CurrTextureHandle != TextureHandle)
3447 			{
3448 				OP_STATE_RENDER(1, ExecBufInstPtr);
3449 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
3450 				CurrTextureHandle = TextureHandle;
3451 			}
3452 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3453 			if (NumVertices)
3454 			{
3455 			   WriteEndCodeToExecuteBuffer();
3456 		  	   UnlockExecuteBufferAndPrepareForUse();
3457 			   ExecuteBuffer();
3458 		  	   LockExecuteBuffer();
3459 			}
3460 		 	D3D_DrawWaterPatch(x, y, z);
3461 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z);
3462 		 	D3D_DrawWaterPatch(x+MeshXScale*2, y, z);
3463 		 	D3D_DrawWaterPatch(x+MeshXScale*3, y, z);
3464 		 	D3D_DrawWaterPatch(x, y, z+MeshZScale);
3465 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale);
3466 		 	D3D_DrawWaterPatch(x+MeshXScale*2, y, z+MeshZScale);
3467 		 	D3D_DrawWaterPatch(x+MeshXScale*3, y, z+MeshZScale);
3468 		}
3469 		else if (drawEndWater)
3470 		{
3471 			int x = -15471;
3472 			int y = -11720-500;
3473 			int z = -55875;
3474 			MeshXScale = (15471-1800);
3475 			MeshZScale = (55875-36392);
3476 			{
3477 				extern void CheckForObjectsInWater(int minX, int maxX, int minZ, int maxZ, int averageY);
3478 				CheckForObjectsInWater(x, x+MeshXScale, z, z+MeshZScale, y);
3479 			}
3480 			WaterXOrigin=x;
3481 			WaterZOrigin=z;
3482 			WaterUScale = 4.0f/(float)(MeshXScale+1800-3782);
3483 			WaterVScale = 4.0f/(float)MeshZScale;
3484 		 	MeshXScale/=4;
3485 		 	MeshZScale/=2;
3486 
3487 			// Turn OFF texturing if it is on...
3488 			D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[WaterShaftImageNumber].D3DHandle;
3489 			if (CurrTextureHandle != TextureHandle)
3490 			{
3491 				OP_STATE_RENDER(1, ExecBufInstPtr);
3492 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
3493 				CurrTextureHandle = TextureHandle;
3494 			}
3495 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3496 			if (NumVertices)
3497 			{
3498 			   WriteEndCodeToExecuteBuffer();
3499 		  	   UnlockExecuteBufferAndPrepareForUse();
3500 			   ExecuteBuffer();
3501 		  	   LockExecuteBuffer();
3502 			}
3503 		 	D3D_DrawWaterPatch(x, y, z);
3504 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z);
3505 		 	D3D_DrawWaterPatch(x+MeshXScale*2, y, z);
3506 		 	D3D_DrawWaterPatch(x+MeshXScale*3, y, z);
3507 		 	D3D_DrawWaterPatch(x, y, z+MeshZScale);
3508 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale);
3509 		 	D3D_DrawWaterPatch(x+MeshXScale*2, y, z+MeshZScale);
3510 		 	D3D_DrawWaterPatch(x+MeshXScale*3, y, z+MeshZScale);
3511 		}
3512 	}
3513 	#if 1
3514 	else if (!_stricmp(LevelName,"derelict"))
3515 	{
3516 		char drawMirrorSurfaces = 0;
3517 		char drawWater = 0;
3518 
3519 		while(numOfObjects)
3520 		{
3521 			DISPLAYBLOCK *objectPtr = OnScreenBlockList[--numOfObjects];
3522 			MODULE *modulePtr = objectPtr->ObMyModule;
3523 
3524 			/* if it's a module, which isn't inside another module */
3525 			if (modulePtr && modulePtr->name)
3526 			{
3527 			  	if( (!_stricmp(modulePtr->name,"start-en01"))
3528 			  	  ||(!_stricmp(modulePtr->name,"start")))
3529 				{
3530 					drawMirrorSurfaces = 1;
3531 				}
3532 				else if (!_stricmp(modulePtr->name,"water-01"))
3533 				{
3534 					extern void HandleRainShaft(MODULE *modulePtr, int bottomY, int topY, int numberOfRaindrops);
3535 					drawWater = 1;
3536 					HandleRainShaft(modulePtr, 32000, 0, 16);
3537 				}
3538 			}
3539 		}
3540 
3541 		if (drawMirrorSurfaces)
3542 		{
3543 			extern void RenderMirrorSurface(void);
3544 			extern void RenderMirrorSurface2(void);
3545 			extern void RenderParticlesInMirror(void);
3546 			RenderParticlesInMirror();
3547 			RenderMirrorSurface();
3548 			RenderMirrorSurface2();
3549 		}
3550 		if (drawWater)
3551 		{
3552 			int x = -102799;
3553 			int y = 32000;
3554 			int z = -200964;
3555 			MeshXScale = (102799-87216);
3556 			MeshZScale = (200964-180986);
3557 			{
3558 				extern void CheckForObjectsInWater(int minX, int maxX, int minZ, int maxZ, int averageY);
3559 				CheckForObjectsInWater(x, x+MeshXScale, z, z+MeshZScale, y);
3560 			}
3561 
3562 			WaterXOrigin=x;
3563 			WaterZOrigin=z;
3564 			WaterUScale = 4.0f/(float)MeshXScale;
3565 			WaterVScale = 4.0f/(float)MeshZScale;
3566 		 	MeshXScale/=2;
3567 		 	MeshZScale/=2;
3568 
3569 			// Turn OFF texturing if it is on...
3570 			D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[ChromeImageNumber].D3DHandle;
3571 			if (CurrTextureHandle != TextureHandle)
3572 			{
3573 				OP_STATE_RENDER(1, ExecBufInstPtr);
3574 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
3575 				CurrTextureHandle = TextureHandle;
3576 			}
3577 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3578 			if (NumVertices)
3579 			{
3580 			   WriteEndCodeToExecuteBuffer();
3581 		  	   UnlockExecuteBufferAndPrepareForUse();
3582 			   ExecuteBuffer();
3583 		  	   LockExecuteBuffer();
3584 			}
3585 		 	D3D_DrawWaterPatch(x, y, z);
3586 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z);
3587 		 	D3D_DrawWaterPatch(x, y, z+MeshZScale);
3588 		 	D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale);
3589 		}
3590 
3591 	}
3592 	#endif
3593 	else if (!_stricmp(LevelName,"genshd1"))
3594 	{
3595 		char drawWater = 0;
3596 
3597 		while(numOfObjects)
3598 		{
3599 			DISPLAYBLOCK *objectPtr = OnScreenBlockList[--numOfObjects];
3600 			MODULE *modulePtr = objectPtr->ObMyModule;
3601 
3602 			/* if it's a module, which isn't inside another module */
3603 			if (modulePtr && modulePtr->name)
3604 			{
3605 				if( (!_stricmp(modulePtr->name,"largespace"))
3606 				  ||(!_stricmp(modulePtr->name,"proc13"))
3607 				  ||(!_stricmp(modulePtr->name,"trench01"))
3608 				  ||(!_stricmp(modulePtr->name,"trench02"))
3609 				  ||(!_stricmp(modulePtr->name,"trench03"))
3610 				  ||(!_stricmp(modulePtr->name,"trench04"))
3611 				  ||(!_stricmp(modulePtr->name,"trench05"))
3612 				  ||(!_stricmp(modulePtr->name,"trench06"))
3613 				  ||(!_stricmp(modulePtr->name,"trench07"))
3614 				  ||(!_stricmp(modulePtr->name,"trench08"))
3615 				  ||(!_stricmp(modulePtr->name,"trench09")))
3616 				{
3617 					extern void HandleRain(int numberOfRaindrops);
3618 					HandleRain(999);
3619 					break;
3620 				}
3621 			}
3622 
3623 		}
3624 	}
3625 }
3626 
D3D_DrawWaterTest(MODULE * testModulePtr)3627 void D3D_DrawWaterTest(MODULE *testModulePtr)
3628 {
3629 	extern char LevelName[];
3630 	if (!strcmp(LevelName,"genshd1"))
3631 	{
3632 		extern DISPLAYBLOCK *Player;
3633 
3634 //		DISPLAYBLOCK *objectPtr = OnScreenBlockList[numOfObjects];
3635 		MODULE *modulePtr = testModulePtr;//objectPtr->ObMyModule;
3636 #if 0
3637 		if (testModulePtr && testModulePtr->name)
3638 		if(!strcmp(testModulePtr->name,"LargeSpace"))
3639 		{
3640 			extern void HandleRain(int numberOfRaindrops);
3641 			HandleRain(999);
3642 		}
3643 #endif
3644 		if (modulePtr && modulePtr->name)
3645 		{
3646 			if (!strcmp(modulePtr->name,"05"))
3647 			{
3648 				int y = modulePtr->m_maxy+modulePtr->m_world.vy-500;
3649 		   		int x = modulePtr->m_minx+modulePtr->m_world.vx;
3650 		   		int z = modulePtr->m_minz+modulePtr->m_world.vz;
3651 				MeshXScale = (7791 - -7794);
3652 				MeshZScale = (23378 - 7793);
3653 				{
3654 					extern void CheckForObjectsInWater(int minX, int maxX, int minZ, int maxZ, int averageY);
3655 					CheckForObjectsInWater(x, x+MeshXScale, z, z+MeshZScale, y);
3656 				}
3657 				D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[WaterShaftImageNumber].D3DHandle;
3658 				if (CurrTextureHandle != TextureHandle)
3659 				{
3660 					OP_STATE_RENDER(1, ExecBufInstPtr);
3661 					STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
3662 					CurrTextureHandle = TextureHandle;
3663 				}
3664 				CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
3665 				if (NumVertices)
3666 				{
3667 				   WriteEndCodeToExecuteBuffer();
3668 			  	   UnlockExecuteBufferAndPrepareForUse();
3669 				   ExecuteBuffer();
3670 			  	   LockExecuteBuffer();
3671 				}
3672 				WaterXOrigin=x;
3673 				WaterZOrigin=z;
3674 				WaterUScale = 4.0f/(float)(MeshXScale);
3675 				WaterVScale = 4.0f/(float)MeshZScale;
3676 				#if 1
3677 				MeshXScale/=2;
3678 				MeshZScale/=2;
3679 				D3D_DrawWaterPatch(x, y, z);
3680 				D3D_DrawWaterPatch(x+MeshXScale, y, z);
3681 				D3D_DrawWaterPatch(x, y, z+MeshZScale);
3682 				D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale);
3683 
3684 				extern void HandleRainShaft(MODULE *modulePtr, int bottomY, int topY, int numberOfRaindrops);
3685 				HandleRainShaft(modulePtr, y,-21000,1);
3686 				#else
3687 				MeshXScale/=4;
3688 				MeshZScale/=4;
3689 				D3D_DrawWaterPatch(x, y, z);
3690 				D3D_DrawWaterPatch(x, y, z+MeshZScale);
3691 				D3D_DrawWaterPatch(x, y, z+MeshZScale*2);
3692 				D3D_DrawWaterPatch(x, y, z+MeshZScale*3);
3693 				D3D_DrawWaterPatch(x+MeshXScale, y, z);
3694 				D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale);
3695 				D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale*2);
3696 				D3D_DrawWaterPatch(x+MeshXScale, y, z+MeshZScale*3);
3697 				D3D_DrawWaterPatch(x+MeshXScale*2, y, z);
3698 				D3D_DrawWaterPatch(x+MeshXScale*2, y, z+MeshZScale);
3699 				D3D_DrawWaterPatch(x+MeshXScale*2, y, z+MeshZScale*2);
3700 				D3D_DrawWaterPatch(x+MeshXScale*2, y, z+MeshZScale*3);
3701 				D3D_DrawWaterPatch(x+MeshXScale*3, y, z);
3702 				D3D_DrawWaterPatch(x+MeshXScale*3, y, z+MeshZScale);
3703 				D3D_DrawWaterPatch(x+MeshXScale*3, y, z+MeshZScale*2);
3704 				D3D_DrawWaterPatch(x+MeshXScale*3, y, z+MeshZScale*3);
3705 				HandleRainDrops(modulePtr,2);
3706 				#endif
3707 			}
3708 		}
3709 	}
3710 	#if 0
3711 	else if ( (!_stricmp(LevelName,"e3demo")) || (!_stricmp(LevelName,"e3demosp")) )
3712 	{
3713 		if (testModulePtr && testModulePtr->name)
3714 		{
3715 			#if 0
3716 			if(!_stricmp(testModulePtr->name,"watermid"))
3717 			{
3718 				DECAL fmvDecal =
3719 				{
3720 					DECAL_FMV,
3721 					{
3722 					{0,-2500,29000},
3723 					{2000,-2500,29000},
3724 					{2000,-2500+750*2,29000},
3725 					{0,-2500+750*2,29000}
3726 					},
3727 					0
3728 				};
3729 				fmvDecal.ModuleIndex = testModulePtr->m_index;
3730 				fmvDecal.Centre.vx = 0;
3731 				fmvDecal.UOffset = 0;
3732 
3733 				RenderDecal(&fmvDecal);
3734 			}
3735 			#endif
3736 			if(!_stricmp(testModulePtr->name,"lowlowlo03"))
3737 			{
3738 				VECTORCH position = {6894,469,-13203};
3739 				VECTORCH disp = position;
3740 				int i,d;
3741 
3742 				disp.vx -= Player->ObWorld.vx;
3743 				disp.vy -= Player->ObWorld.vy;
3744 				disp.vz -= Player->ObWorld.vz;
3745 				d = ONE_FIXED - Approximate3dMagnitude(&disp)*2;
3746 				if (d<0) d = 0;
3747 
3748 				i = MUL_FIXED(10,d);
3749 				while(i--)
3750 				{
3751 					VECTORCH velocity;
3752 					velocity.vx = ((FastRandom()&1023) - 512);
3753 					velocity.vy = ((FastRandom()&1023) - 512)+2000;
3754 					velocity.vz = (1000+(FastRandom()&255))*2;
3755 					MakeParticle(&(position),&(velocity),PARTICLE_STEAM);
3756 				}
3757 			}
3758 		}
3759 	}
3760 	#endif
3761 
3762 }
3763 
3764 VECTORCH MeshVertex[256];
3765 #define TEXTURE_WATER 0
3766 
3767 VECTORCH MeshWorldVertex[256];
3768 unsigned int MeshVertexColour[256];
3769 unsigned int MeshVertexSpecular[256];
3770 char MeshVertexOutcode[256];
D3D_DrawWaterPatch(int xOrigin,int yOrigin,int zOrigin)3771 void D3D_DrawWaterPatch(int xOrigin, int yOrigin, int zOrigin)
3772 {
3773 	int i=0;
3774 	int x;
3775 	for (x=0; x<16; x++)
3776 	{
3777 		int z;
3778 		for(z=0; z<16; z++)
3779 		{
3780 			VECTORCH *point = &MeshVertex[i];
3781 
3782 			point->vx = xOrigin+(x*MeshXScale)/15;
3783 			point->vz = zOrigin+(z*MeshZScale)/15;
3784 
3785 
3786 			int offset=0;
3787 
3788 		 #if 1
3789 			/* basic noise ripples */
3790 //		 	offset = MUL_FIXED(32,GetSin(  (point->vx+point->vz+CloakingPhase)&4095 ) );
3791 //		 	offset += MUL_FIXED(16,GetSin(  (point->vx-point->vz*2+CloakingPhase/2)&4095 ) );
3792 
3793 			{
3794  				offset += EffectOfRipples(point);
3795 			}
3796 		#endif
3797 	//		if (offset>450) offset = 450;
3798 	//		if (offset<-450) offset = -450;
3799 			point->vy = yOrigin+offset;
3800 
3801 			#if 0
3802 			MeshVertexColour[i] = LightSourceWaterPoint(point,offset);
3803 			#else
3804 			{
3805 				int alpha = 128-offset/4;
3806 		//		if (alpha>255) alpha = 255;
3807 		//		if (alpha<128) alpha = 128;
3808 				switch (CurrentVisionMode)
3809 				{
3810 					default:
3811 					case VISION_MODE_NORMAL:
3812 					{
3813 //						MeshVertexColour[i] = RGBALIGHT_MAKE(10,51,28,alpha);
3814 						MeshVertexColour[i] = RGBALIGHT_MAKE(255,255,255,alpha);
3815 						#if 0
3816 						#if 1
3817 						VECTORCH pos = {24087,yOrigin,39165};
3818 						int c = (8191-VectorDistance(&pos,point));
3819 						if (c<0) c=0;
3820 						else
3821 						{
3822 							int s = GetSin((CloakingPhase/2)&4095);
3823 							s = MUL_FIXED(s,s)/64;
3824 							c = MUL_FIXED(s,c);
3825 						}
3826 						MeshVertexSpecular[i] = (c<<16)+(((c/4)<<8)&0xff00) + (c/4);
3827 						#else
3828 						if (!(FastRandom()&1023))
3829 						{
3830 							MeshVertexSpecular[i] = 0xc04040;
3831 						}
3832 						else
3833 						{
3834 							MeshVertexSpecular[i] = 0;
3835 						}
3836 						#endif
3837 						#endif
3838 						break;
3839 					}
3840 					case VISION_MODE_IMAGEINTENSIFIER:
3841 					{
3842 						MeshVertexColour[i] = RGBALIGHT_MAKE(0,51,0,alpha);
3843 						break;
3844 					}
3845 					case VISION_MODE_PRED_THERMAL:
3846 					case VISION_MODE_PRED_SEEALIENS:
3847 					case VISION_MODE_PRED_SEEPREDTECH:
3848 					{
3849 						MeshVertexColour[i] = RGBALIGHT_MAKE(0,0,28,alpha);
3850 					  	break;
3851 					}
3852 				}
3853 
3854 			}
3855 			#endif
3856 
3857 			#if 1
3858 			MeshWorldVertex[i].vx = ((point->vx-WaterXOrigin)/4+MUL_FIXED(GetSin((point->vy*16)&4095),128));
3859 			MeshWorldVertex[i].vy = ((point->vz-WaterZOrigin)/4+MUL_FIXED(GetSin((point->vy*16+200)&4095),128));
3860 			#endif
3861 
3862 			#if 1
3863 			TranslatePointIntoViewspace(point);
3864 			#else
3865 			point->vx -= Global_VDB_Ptr->VDB_World.vx;
3866 			point->vy -= Global_VDB_Ptr->VDB_World.vy;
3867 			point->vz -= Global_VDB_Ptr->VDB_World.vz;
3868 			RotateVector(point,&(Global_VDB_Ptr->VDB_Mat));
3869 			point->vy = MUL_FIXED(point->vy,87381);
3870 
3871 			#endif
3872 			/* is particle within normal view frustrum ? */
3873 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
3874 			{
3875 				if(( (-point->vx <= point->vz*2)
3876 		   			&&(point->vx <= point->vz*2)
3877 					&&(-point->vy <= point->vz*2)
3878 					&&(point->vy <= point->vz*2) ))
3879 				{
3880 					MeshVertexOutcode[i]=1;
3881 				}
3882 				else
3883 				{
3884 					MeshVertexOutcode[i]=0;
3885 				}
3886 			}
3887 			else
3888 			{
3889 				if(( (-point->vx <= point->vz)
3890 		   			&&(point->vx <= point->vz)
3891 					&&(-point->vy <= point->vz)
3892 					&&(point->vy <= point->vz) ))
3893 				{
3894 					MeshVertexOutcode[i]=1;
3895 				}
3896 				else
3897 				{
3898 					MeshVertexOutcode[i]=0;
3899 				}
3900 			}
3901 
3902 			i++;
3903 		}
3904 	}
3905 
3906 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
3907 	{
3908 		D3D_DrawMoltenMetalMesh_Unclipped();
3909 //		D3D_DrawWaterMesh_Unclipped();
3910 	}
3911 	else
3912 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
3913 	{
3914 		D3D_DrawMoltenMetalMesh_Clipped();
3915 //		D3D_DrawWaterMesh_Clipped();
3916 	}
3917 
3918 
3919 }
3920 
D3D_DrawWaterMesh_Unclipped(void)3921 void D3D_DrawWaterMesh_Unclipped(void)
3922 {
3923 	float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
3924 
3925 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
3926 	{
3927 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
3928 		VECTORCH *point = MeshVertex;
3929 		#if TEXTURE_WATER
3930 		VECTORCH *pointWS = MeshWorldVertex;
3931 		#endif
3932 		int i;
3933 		for (i=0; i<256; i++)
3934 		{
3935 
3936 			if (point->vz<=1) point->vz = 1;
3937 			int x = (point->vx*(Global_VDB_Ptr->VDB_ProjX))/point->vz+Global_VDB_Ptr->VDB_CentreX;
3938 			int y = (point->vy*(Global_VDB_Ptr->VDB_ProjY))/point->vz+Global_VDB_Ptr->VDB_CentreY;
3939   //			textprint("%d, %d\n",x,y);
3940 			#if 1
3941 			{
3942 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
3943 				{
3944 					x=Global_VDB_Ptr->VDB_ClipLeft;
3945 				}
3946 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
3947 				{
3948 					x=Global_VDB_Ptr->VDB_ClipRight;
3949 				}
3950 
3951 				vertexPtr->sx=x;
3952 			}
3953 			{
3954 				if (y<Global_VDB_Ptr->VDB_ClipUp)
3955 				{
3956 					y=Global_VDB_Ptr->VDB_ClipUp;
3957 				}
3958 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
3959 				{
3960 					y=Global_VDB_Ptr->VDB_ClipDown;
3961 				}
3962 				vertexPtr->sy=y;
3963 			}
3964 			#else
3965 			vertexPtr->sx=x;
3966 			vertexPtr->sy=y;
3967 			#endif
3968 			#if FOG_ON
3969 			{
3970 				int fog = (point->vz)/FOG_SCALE;
3971 				if (fog<0) fog=0;
3972 			 	if (fog>254) fog=254;
3973 				fog=255-fog;
3974 			   	vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,fog);
3975 			}
3976 			#endif
3977 			point->vz+=HeadUpDisplayZOffset;
3978 		  	float oneOverZ = ((float)(point->vz)-ZNear)/(float)(point->vz);
3979 		  //vertexPtr->color = RGBALIGHT_MAKE(66,70,0,127+(FastRandom()&63));
3980 			vertexPtr->color = MeshVertexColour[i];
3981 			vertexPtr->sz = oneOverZ;
3982 			#if TEXTURE_WATER
3983 			vertexPtr->tu = pointWS->vx/128.0;
3984 			vertexPtr->tv =	pointWS->vz/128.0;
3985 			#endif
3986 
3987 
3988 			NumVertices++;
3989 			vertexPtr++;
3990 			point++;
3991 			#if TEXTURE_WATER
3992 			pointWS++;
3993 			#endif
3994 		}
3995 	}
3996  //	textprint("numvertices %d\n",NumVertices);
3997 
3998 
3999     /*
4000      * Make sure that the triangle data (not OP) will be QWORD aligned
4001      */
4002 	if (QWORD_ALIGNED(ExecBufInstPtr))
4003     {
4004         OP_NOP(ExecBufInstPtr);
4005     }
4006 
4007   	OP_TRIANGLE_LIST(450, ExecBufInstPtr);
4008 	/* CONSTRUCT POLYS */
4009 	{
4010 		int x;
4011 		for (x=0; x<15; x++)
4012 		{
4013 			int y;
4014 			for(y=0; y<15; y++)
4015 			{
4016 				OUTPUT_TRIANGLE(0+x+(16*y),1+x+(16*y),16+x+(16*y), 256);
4017 				OUTPUT_TRIANGLE(1+x+(16*y),17+x+(16*y),16+x+(16*y), 256);
4018 			}
4019 		}
4020 	}
4021 	#if 1
4022 	{
4023 	   WriteEndCodeToExecuteBuffer();
4024   	   UnlockExecuteBufferAndPrepareForUse();
4025 	   ExecuteBuffer();
4026   	   LockExecuteBuffer();
4027 	}
4028 	#endif
4029 }
D3D_DrawWaterMesh_Clipped(void)4030 void D3D_DrawWaterMesh_Clipped(void)
4031 {
4032 	float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
4033 
4034 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
4035 	{
4036 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
4037 		VECTORCH *point = MeshVertex;
4038 		#if TEXTURE_WATER
4039 		VECTORCH *pointWS = MeshWorldVertex;
4040 		#endif
4041 		int i;
4042 		for (i=0; i<256; i++)
4043 		{
4044 			{
4045 				if (point->vz<=1) point->vz = 1;
4046 				int x = (point->vx*(Global_VDB_Ptr->VDB_ProjX))/point->vz+Global_VDB_Ptr->VDB_CentreX;
4047 				int y = (point->vy*(Global_VDB_Ptr->VDB_ProjY))/point->vz+Global_VDB_Ptr->VDB_CentreY;
4048 				#if 1
4049 				{
4050 					if (x<Global_VDB_Ptr->VDB_ClipLeft)
4051 					{
4052 						x=Global_VDB_Ptr->VDB_ClipLeft;
4053 					}
4054 					else if (x>Global_VDB_Ptr->VDB_ClipRight)
4055 					{
4056 						x=Global_VDB_Ptr->VDB_ClipRight;
4057 					}
4058 
4059 					vertexPtr->sx=x;
4060 				}
4061 				{
4062 					if (y<Global_VDB_Ptr->VDB_ClipUp)
4063 					{
4064 						y=Global_VDB_Ptr->VDB_ClipUp;
4065 					}
4066 					else if (y>Global_VDB_Ptr->VDB_ClipDown)
4067 					{
4068 						y=Global_VDB_Ptr->VDB_ClipDown;
4069 					}
4070 					vertexPtr->sy=y;
4071 				}
4072 				#else
4073 				vertexPtr->sx=x;
4074 				vertexPtr->sy=y;
4075 				#endif
4076 				#if FOG_ON
4077 				{
4078 					int fog = ((point->vz)/FOG_SCALE);
4079 					if (fog<0) fog=0;
4080 				 	if (fog>254) fog=254;
4081 					fog=255-fog;
4082 				   	vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,fog);
4083 				}
4084 				#endif
4085 				#if TEXTURE_WATER
4086 				vertexPtr->tu = pointWS->vx/128.0;
4087 				vertexPtr->tv =	pointWS->vz/128.0;
4088 				#endif
4089 				point->vz+=HeadUpDisplayZOffset;
4090 			  	float oneOverZ = ((float)(point->vz)-ZNear)/(float)(point->vz);
4091 			  //	vertexPtr->color = RGBALIGHT_MAKE(66,70,0,127+(FastRandom()&63));
4092 				vertexPtr->color = MeshVertexColour[i];
4093 				vertexPtr->sz = oneOverZ;
4094 			}
4095 			NumVertices++;
4096 			vertexPtr++;
4097 			point++;
4098 			#if TEXTURE_WATER
4099 			pointWS++;
4100 			#endif
4101 		}
4102 	}
4103 //	textprint("numvertices %d\n",NumVertices);
4104 	/* CONSTRUCT POLYS */
4105 	{
4106 		int x;
4107 		for (x=0; x<15; x++)
4108 		{
4109 			int y;
4110 			for(y=0; y<15; y++)
4111 			{
4112 				#if 1
4113 				int p1 = 0+x+(16*y);
4114 				int p2 = 1+x+(16*y);
4115 				int p3 = 16+x+(16*y);
4116 				int p4 = 17+x+(16*y);
4117 
4118 				if (MeshVertexOutcode[p1]||MeshVertexOutcode[p2]||MeshVertexOutcode[p3])
4119 				{
4120 					OP_TRIANGLE_LIST(1, ExecBufInstPtr);
4121 					OUTPUT_TRIANGLE(p1,p2,p3, 256);
4122 				}
4123 				if (MeshVertexOutcode[p2]||MeshVertexOutcode[p3]||MeshVertexOutcode[p4])
4124 				{
4125 					OP_TRIANGLE_LIST(1, ExecBufInstPtr);
4126 					OUTPUT_TRIANGLE(p2,p4,p3, 256);
4127 				}
4128 				#else
4129 				int p2 = 1+x+(16*y);
4130 				int p3 = 16+x+(16*y);
4131 
4132 				if (MeshVertexOutcode[p2]&&MeshVertexOutcode[p3])
4133 				{
4134 					int p1 = 0+x+(16*y);
4135 					int p4 = 17+x+(16*y);
4136 					if (MeshVertexOutcode[p1])
4137 					{
4138 						OP_TRIANGLE_LIST(1, ExecBufInstPtr);
4139 						OUTPUT_TRIANGLE(p1,p2,p3, 256);
4140 					}
4141 					if (MeshVertexOutcode[p4])
4142 					{
4143 						OP_TRIANGLE_LIST(1, ExecBufInstPtr);
4144 						OUTPUT_TRIANGLE(p2,p4,p3, 256);
4145 					}
4146 				}
4147 				#endif
4148 			}
4149 		}
4150 	}
4151 	#if 1
4152 	{
4153 	   WriteEndCodeToExecuteBuffer();
4154   	   UnlockExecuteBufferAndPrepareForUse();
4155 	   ExecuteBuffer();
4156   	   LockExecuteBuffer();
4157 	}
4158 	#endif
4159 }
4160 
4161 
LightSourceWaterPoint(VECTORCH * pointPtr,int offset)4162 int LightSourceWaterPoint(VECTORCH *pointPtr,int offset)
4163 {
4164 	// this needs a rewrite...
4165 	// make a list of lights which will affect some part of the mesh
4166 	// and go throught that for each point.
4167 
4168 //	int intensity=0;
4169    	int redI=0,greenI=0,blueI=0;
4170 
4171 	#if 0
4172 	int alpha = 207-offset;
4173 	if (alpha>255) alpha = 255;
4174 	if (alpha<160) alpha = 160;
4175 
4176 	return RGBALIGHT_MAKE(128,128,255,alpha);
4177 	#else
4178 
4179 	DISPLAYBLOCK **activeBlockListPtr = ActiveBlockList;
4180 	for(int i = NumActiveBlocks; i!=0; i--)
4181 	{
4182 		DISPLAYBLOCK *dispPtr = *activeBlockListPtr++;
4183 
4184 		if(dispPtr->ObNumLights)
4185 		{
4186 			for(int j = 0; j < dispPtr->ObNumLights; j++)
4187 			{
4188 				LIGHTBLOCK *lptr = dispPtr->ObLights[j];
4189 
4190 				VECTORCH disp = lptr->LightWorld;
4191 				disp.vx -= pointPtr->vx;
4192 				disp.vy -= pointPtr->vy;
4193 				disp.vz -= pointPtr->vz;
4194 
4195 				int dist = Approximate3dMagnitude(&disp);
4196 
4197 				if (dist<lptr->LightRange)
4198 				{
4199 					int brightness = MUL_FIXED(lptr->BrightnessOverRange,lptr->LightRange-dist);
4200 					redI += MUL_FIXED(brightness,lptr->RedScale);
4201 					greenI += MUL_FIXED(brightness,lptr->GreenScale);
4202 					blueI += MUL_FIXED(brightness,lptr->BlueScale);
4203 				}
4204 			}
4205 		}
4206 	}
4207 	if (redI>ONE_FIXED) redI=ONE_FIXED;
4208 	else if (redI<GlobalAmbience) redI=GlobalAmbience;
4209 	if (greenI>ONE_FIXED) greenI=ONE_FIXED;
4210 	else if (greenI<GlobalAmbience) greenI=GlobalAmbience;
4211  	if (blueI>ONE_FIXED) blueI=ONE_FIXED;
4212 	else if (blueI<GlobalAmbience) blueI=GlobalAmbience;
4213 
4214 	int alpha = 192-offset/4;
4215 	if (alpha>255) alpha = 255;
4216 	if (alpha<128) alpha = 128;
4217 
4218 //	return RGBALIGHT_MAKE(MUL_FIXED(64+(offset&128),redI),MUL_FIXED(64+(offset&128),greenI),MUL_FIXED(64+(offset&128),blueI),alpha);
4219 //	return RGBALIGHT_MAKE(MUL_FIXED(50,redI),MUL_FIXED(255,greenI),MUL_FIXED(140,blueI),alpha);
4220 	return RGBALIGHT_MAKE(MUL_FIXED(10,redI),MUL_FIXED(51,greenI),MUL_FIXED(28,blueI),alpha);
4221 //	return RGBALIGHT_MAKE(MUL_FIXED(128,redI),MUL_FIXED(128,greenI),MUL_FIXED(255,blueI),alpha);
4222 	#endif
4223 }
LightIntensityAtPoint(VECTORCH * pointPtr)4224 int LightIntensityAtPoint(VECTORCH *pointPtr)
4225 {
4226 	int intensity=0;
4227 
4228 	DISPLAYBLOCK **activeBlockListPtr = ActiveBlockList;
4229 	for(int i = NumActiveBlocks; i!=0; i--)
4230 	{
4231 		DISPLAYBLOCK *dispPtr = *activeBlockListPtr++;
4232 
4233 		if(dispPtr->ObNumLights)
4234 		{
4235 			for(int j = 0; j < dispPtr->ObNumLights; j++)
4236 			{
4237 				LIGHTBLOCK *lptr = dispPtr->ObLights[j];
4238 
4239 				VECTORCH disp = lptr->LightWorld;
4240 				disp.vx -= pointPtr->vx;
4241 				disp.vy -= pointPtr->vy;
4242 				disp.vz -= pointPtr->vz;
4243 
4244 				int dist = Approximate3dMagnitude(&disp);
4245 
4246 				if (dist<lptr->LightRange)
4247 				{
4248 					intensity += WideMulNarrowDiv(lptr->LightBright,lptr->LightRange-dist,lptr->LightRange);
4249 				}
4250 			}
4251 		}
4252 	}
4253 	if (intensity>ONE_FIXED) intensity=ONE_FIXED;
4254 	else if (intensity<GlobalAmbience) intensity=GlobalAmbience;
4255 
4256 	/* KJL 20:31:39 12/1/97 - limit how dark things can be so blood doesn't go green */
4257 	if (intensity<10*256) intensity = 10*256;
4258 
4259 	return intensity;
4260 }
4261 signed int ForceFieldPointDisplacement[15*3+1][16];
4262 signed int ForceFieldPointDisplacement2[15*3+1][16];
4263 signed int ForceFieldPointVelocity[15*3+1][16];
4264 unsigned char ForceFieldPointColour1[15*3+1][16];
4265 unsigned char ForceFieldPointColour2[15*3+1][16];
4266 
4267 int Phase=0;
4268 int ForceFieldPhase=0;
InitForceField(void)4269 void InitForceField(void)
4270 {
4271 	for (int x=0; x<15*3+1; x++)
4272 		for (int y=0; y<16; y++)
4273 		{
4274 			ForceFieldPointDisplacement[x][y]=0;
4275 			ForceFieldPointDisplacement2[x][y]=0;
4276 			ForceFieldPointVelocity[x][y]=0;
4277 		}
4278 	ForceFieldPhase=0;
4279 }
4280 #if 1
4281 
UpdateForceField(void)4282 void UpdateForceField(void)
4283 {
4284 	#if 1
4285 	Phase+=NormalFrameTime>>6;
4286 	ForceFieldPhase+=NormalFrameTime>>5;
4287 	int x;
4288 	for (x=1; x<15*3; x++)
4289 	{
4290 		int y;
4291 		for (y=1; y<15; y++)
4292 		{
4293 
4294 			int acceleration =32*(-8*ForceFieldPointDisplacement[x][y]
4295 								+ForceFieldPointDisplacement[x-1][y-1]
4296 								+ForceFieldPointDisplacement[x-1][y]
4297 								+ForceFieldPointDisplacement[x-1][y+1]
4298 								+ForceFieldPointDisplacement[x][y-1]
4299 								+ForceFieldPointDisplacement[x][y+1]
4300 #if 0
4301 								)
4302 #else
4303 
4304 								+ForceFieldPointDisplacement[x+1][y-1]
4305 								+ForceFieldPointDisplacement[x+1][y]
4306 								+ForceFieldPointDisplacement[x+1][y+1])
4307 #endif
4308 								-(ForceFieldPointVelocity[x][y]*5);
4309 
4310 			ForceFieldPointVelocity[x][y] += MUL_FIXED(acceleration,NormalFrameTime);
4311 			ForceFieldPointDisplacement2[x][y] += MUL_FIXED(ForceFieldPointVelocity[x][y],NormalFrameTime);
4312 #if 1
4313 			if(ForceFieldPointDisplacement2[x][y]>200) ForceFieldPointDisplacement2[x][y]=200;
4314 			if(ForceFieldPointDisplacement2[x][y]<-200) ForceFieldPointDisplacement2[x][y]=-200;
4315 #else
4316 			if(ForceFieldPointDisplacement2[x][y]>512) ForceFieldPointDisplacement2[x][y]=512;
4317 			if(ForceFieldPointDisplacement2[x][y]<-512) ForceFieldPointDisplacement2[x][y]=-512;
4318 
4319 #endif
4320 			{
4321 				int offset = ForceFieldPointDisplacement2[x][y];
4322 				int colour = ForceFieldPointVelocity[x][y]/4;
4323 
4324 				if (offset<0) offset =-offset;
4325 				if (colour<0) colour =-colour;
4326 				colour=(colour+offset)/2;
4327 
4328 				if(colour>255) colour=255;
4329 				colour++;
4330 
4331 				ForceFieldPointColour1[x][y]=FastRandom()%colour;
4332 				ForceFieldPointColour2[x][y]=FastRandom()%colour;
4333 			}
4334 		}
4335 
4336 	}
4337 	for (x=1; x<15*3; x++)
4338 	{
4339 		int y;
4340 		for (y=1; y<15; y++)
4341 		{
4342 			ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement2[x][y];
4343 		}
4344 	}
4345 	{
4346 		#if 1
4347 	  	if(ForceFieldPhase>1000)
4348 		{
4349 			ForceFieldPhase=0;
4350 			int x = 1+(FastRandom()%(15*3-2));
4351 			int y = 1+(FastRandom()%13);
4352 			ForceFieldPointVelocity[x][y] = 10000;
4353 			ForceFieldPointVelocity[x][y+1] = 10000;
4354 			ForceFieldPointVelocity[x+1][y] = 10000;
4355 			ForceFieldPointVelocity[x+1][y+1] = 10000;
4356 		}
4357 		#else
4358 	   //	if(ForceFieldPhase>1000)
4359 		{
4360 			ForceFieldPhase=0;
4361 			int x = 1+(FastRandom()%(15*3-2));
4362 			int y = 1+(FastRandom()%13);
4363 			ForceFieldPointVelocity[x][y] = (FastRandom()&16383)+8192;
4364 		}
4365 		#endif
4366 	}
4367 	#else
4368 	int x;
4369 	int y;
4370 	for (y=0; y<=15; y++)
4371 	{
4372 		ForceFieldPointDisplacement[0][y] += (FastRandom()&127)-64;
4373 		if(ForceFieldPointDisplacement[0][y]>512) ForceFieldPointDisplacement[0][y]=512;
4374 		if(ForceFieldPointDisplacement[0][y]<-512) ForceFieldPointDisplacement[0][y]=-512;
4375 		ForceFieldPointVelocity[0][y] = (FastRandom()&16383)-8192;
4376 	}
4377 	for (x=15*3-1; x>0; x--)
4378 	{
4379 		for (y=0; y<=15; y++)
4380 		{
4381 			ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement[x-1][y];
4382 			ForceFieldPointVelocity[x][y] = ForceFieldPointVelocity[x-1][y];
4383 		}
4384 
4385 	}
4386 	for (x=15*3-1; x>1; x--)
4387 	{
4388 		y = FastRandom()&15;
4389 	 	ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement[x-1][y];
4390 		y = (FastRandom()&15)-1;
4391 	 	ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement[x-1][y];
4392 	}
4393 	#endif
4394 }
UpdateWaterFall(void)4395 void UpdateWaterFall(void)
4396 {
4397 	int x;
4398 	int y;
4399 	for (y=0; y<=15; y++)
4400 	{
4401 		ForceFieldPointDisplacement[0][y] += (FastRandom()&127)-64;
4402 		if(ForceFieldPointDisplacement[0][y]>512) ForceFieldPointDisplacement[0][y]=512;
4403 		if(ForceFieldPointDisplacement[0][y]<-512) ForceFieldPointDisplacement[0][y]=-512;
4404 		ForceFieldPointVelocity[0][y] = (FastRandom()&16383)-8192;
4405 	}
4406 	for (x=15*3-1; x>0; x--)
4407 	{
4408 		for (y=0; y<=15; y++)
4409 		{
4410 			ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement[x-1][y];
4411 			ForceFieldPointVelocity[x][y] = ForceFieldPointVelocity[x-1][y];
4412 		}
4413 
4414 	}
4415 	for (x=15*3-1; x>1; x--)
4416 	{
4417 		y = FastRandom()&15;
4418 	 	ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement[x-1][y];
4419 		y = (FastRandom()&15)-1;
4420 	 	ForceFieldPointDisplacement[x][y] = ForceFieldPointDisplacement[x-1][y];
4421 	}
4422 }
4423 
4424 #endif
D3D_DrawForceField(int xOrigin,int yOrigin,int zOrigin,int fieldType)4425 void D3D_DrawForceField(int xOrigin, int yOrigin, int zOrigin, int fieldType)
4426 {
4427 	MeshXScale = 4096/16;
4428 	MeshZScale = 4096/16;
4429 
4430 	for (int field=0; field<3; field++)
4431 	{
4432 	int i=0;
4433 	int x;
4434 	for (x=(0+field*15); x<(16+field*15); x++)
4435 	{
4436 		int z;
4437 		for(z=0; z<16; z++)
4438 		{
4439 			VECTORCH *point = &MeshVertex[i];
4440 			int offset = ForceFieldPointDisplacement[x][z];
4441 
4442 			switch(fieldType)
4443 			{
4444 				case 0:
4445 				{
4446 				 	point->vx = xOrigin+(x*MeshXScale);
4447 				 	point->vy = yOrigin+(z*MeshZScale);
4448 				 	point->vz = zOrigin+offset;
4449 					break;
4450 				}
4451 				case 1:
4452 				{
4453 
4454 					int theta = (z*4095)/15;
4455 					int u = (x*65536)/45;
4456 
4457 					int b = MUL_FIXED(2*u,(65536-u));
4458 					int c = MUL_FIXED(u,u);
4459 					int phi = (Phase&4095);
4460 					int x3 = (GetSin(phi))/64;
4461 					int y3 = 5000-(GetCos((phi*3+1000)&4095)/128);
4462 					int z3 = (GetSin((3*phi+1324)&4095))/32;
4463 					int x2 = -x3/2;
4464 					int y2 = 3000;
4465 					int z2 = -z3/4;
4466 					int innerRadius = 100;//GetSin(u/32)/16+offset;
4467 
4468 					point->vx = xOrigin+(b*x2+c*x3)/65536+MUL_FIXED(innerRadius,GetSin(theta));
4469 					point->vy = yOrigin-5000+(b*y2+c*y3)/65536;
4470 					point->vz = zOrigin+(b*z2+c*z3)/65536+MUL_FIXED(innerRadius,GetCos(theta));
4471 					break;
4472 				}
4473 				case 2:
4474 				{
4475 					int theta = (z*4095)/15;
4476 					int phi = (x*4095)/45;
4477 					int innerRadius = 1000+offset;
4478 					int outerRadius = 4000;
4479 
4480 
4481 					point->vx = xOrigin+MUL_FIXED(outerRadius-MUL_FIXED(innerRadius,GetSin(theta)),GetCos(phi));
4482 					point->vy = yOrigin+MUL_FIXED(innerRadius,GetCos(theta));
4483 					point->vz = zOrigin+MUL_FIXED(outerRadius-MUL_FIXED(innerRadius,GetSin(theta)),GetSin(phi));
4484 					break;
4485 				}
4486 				case 3:
4487 				{
4488 
4489 					int theta = (x*4095)/45;
4490 					int radius = offset+2000;
4491 					point->vx = xOrigin+MUL_FIXED(radius,GetCos(theta));
4492 					point->vy = yOrigin+(z*MeshZScale);
4493 					point->vz = zOrigin+MUL_FIXED(radius,GetSin(theta));
4494 					break;
4495 				}
4496 			}
4497 
4498 			if (offset<0) offset =-offset;
4499 			offset+=16;
4500 
4501 //			offset-=32;
4502 //			if (offset<0) offset = 0;
4503 
4504 			if(offset>255) offset=255;
4505 
4506 			MeshVertexColour[i] = RGBALIGHT_MAKE(ForceFieldPointColour1[x][z],ForceFieldPointColour2[x][z],255,offset);
4507 			#if TEXTURE_WATER
4508 			MeshWorldVertex[i].vx = point->vx;
4509 			MeshWorldVertex[i].vz = point->vz;
4510 			#endif
4511 
4512 			TranslatePointIntoViewspace(point);
4513 
4514 			/* is particle within normal view frustrum ? */
4515 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
4516 			{
4517 				if(( (-point->vx <= point->vz*2)
4518 		   			&&(point->vx <= point->vz*2)
4519 					&&(-point->vy <= point->vz*2)
4520 					&&(point->vy <= point->vz*2) ))
4521 				{
4522 					MeshVertexOutcode[i]=1;
4523 				}
4524 				else
4525 				{
4526 					MeshVertexOutcode[i]=0;
4527 				}
4528 			}
4529 			else
4530 			{
4531 				if(( (-point->vx <= point->vz)
4532 		   			&&(point->vx <= point->vz)
4533 					&&(-point->vy <= point->vz)
4534 					&&(point->vy <= point->vz) ))
4535 				{
4536 					MeshVertexOutcode[i]=1;
4537 				}
4538 				else
4539 				{
4540 					MeshVertexOutcode[i]=0;
4541 				}
4542 			}
4543 
4544 			i++;
4545 		}
4546 	}
4547 	//textprint("\n");
4548 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
4549 	{
4550 		D3D_DrawWaterMesh_Unclipped();
4551 	}
4552 	else
4553 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
4554 	{
4555 		D3D_DrawWaterMesh_Clipped();
4556 	}
4557 	}
4558 }
4559 
4560 
D3D_DrawPowerFence(int xOrigin,int yOrigin,int zOrigin,int xScale,int yScale,int zScale)4561 void D3D_DrawPowerFence(int xOrigin, int yOrigin, int zOrigin, int xScale, int yScale, int zScale)
4562 {
4563 	for (int field=0; field<3; field++)
4564 	{
4565 	int i=0;
4566 	int x;
4567 	for (x=(0+field*15); x<(16+field*15); x++)
4568 	{
4569 		int z;
4570 		for(z=0; z<16; z++)
4571 		{
4572 			VECTORCH *point = &MeshVertex[i];
4573 			int offset = ForceFieldPointDisplacement[x][z];
4574 
4575 		 	point->vx = xOrigin+(x*xScale);
4576 		 	point->vy = yOrigin+(z*yScale);
4577 		 	point->vz = zOrigin+(x*zScale);
4578 
4579 			if (offset<0) offset =-offset;
4580 			offset+=16;
4581 
4582 			if(offset>255) offset=255;
4583 
4584 			MeshVertexColour[i] = RGBALIGHT_MAKE(ForceFieldPointColour1[x][z],ForceFieldPointColour2[x][z],255,offset);
4585 
4586 			/* translate particle into view space */
4587 			TranslatePointIntoViewspace(point);
4588 
4589 			/* is particle within normal view frustrum ? */
4590 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
4591 			{
4592 				if(( (-point->vx <= point->vz*2)
4593 		   			&&(point->vx <= point->vz*2)
4594 					&&(-point->vy <= point->vz*2)
4595 					&&(point->vy <= point->vz*2) ))
4596 				{
4597 					MeshVertexOutcode[i]=1;
4598 				}
4599 				else
4600 				{
4601 					MeshVertexOutcode[i]=0;
4602 				}
4603 			}
4604 			else
4605 			{
4606 				if(( (-point->vx <= point->vz)
4607 		   			&&(point->vx <= point->vz)
4608 					&&(-point->vy <= point->vz)
4609 					&&(point->vy <= point->vz) ))
4610 				{
4611 					MeshVertexOutcode[i]=1;
4612 				}
4613 				else
4614 				{
4615 					MeshVertexOutcode[i]=0;
4616 				}
4617 			}
4618 
4619 			i++;
4620 		}
4621 	}
4622 	//textprint("\n");
4623 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
4624 	{
4625 		D3D_DrawWaterMesh_Unclipped();
4626 	}
4627 	else
4628 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
4629 	{
4630 		D3D_DrawWaterMesh_Clipped();
4631 	}
4632 	}
4633 }
4634 
D3D_DrawWaterFall(int xOrigin,int yOrigin,int zOrigin)4635 void D3D_DrawWaterFall(int xOrigin, int yOrigin, int zOrigin)
4636 {
4637 	{
4638 		int noRequired = MUL_FIXED(250,NormalFrameTime);
4639 		for (int i=0; i<noRequired; i++)
4640 		{
4641 			VECTORCH velocity;
4642 			VECTORCH position;
4643 			position.vx = xOrigin;
4644 			position.vy = yOrigin-(FastRandom()&511);//+45*MeshXScale;
4645 			position.vz = zOrigin+(FastRandom()%(15*MeshZScale));
4646 
4647 			velocity.vy = (FastRandom()&511)+512;//-((FastRandom()&1023)+2048)*8;
4648 			velocity.vx = ((FastRandom()&511)+256)*2;
4649 			velocity.vz = 0;//-((FastRandom()&511))*8;
4650 			MakeParticle(&(position), &velocity, PARTICLE_WATERFALLSPRAY);
4651 		}
4652 		#if 0
4653 		noRequired = MUL_FIXED(200,NormalFrameTime);
4654 		for (i=0; i<noRequired; i++)
4655 		{
4656 			VECTORCH velocity;
4657 			VECTORCH position;
4658 			position.vx = xOrigin+(FastRandom()%(15*MeshZScale));
4659 			position.vy = yOrigin+45*MeshXScale;
4660 			position.vz = zOrigin;
4661 
4662 			velocity.vy = -((FastRandom()&16383)+4096);
4663 			velocity.vx = ((FastRandom()&4095)-2048);
4664 			velocity.vz = -((FastRandom()&2047)+1048);
4665 			MakeParticle(&(position), &velocity, PARTICLE_WATERFALLSPRAY);
4666 		}
4667 		#endif
4668 	}
4669 	{
4670 		extern void RenderWaterFall(int xOrigin, int yOrigin, int zOrigin);
4671 		//RenderWaterFall(xOrigin, yOrigin-500, zOrigin+50);
4672 	}
4673    	return;
4674 	for (int field=0; field<3; field++)
4675 	{
4676 	int i=0;
4677 	int x;
4678 	for (x=(0+field*15); x<(16+field*15); x++)
4679 	{
4680 		int z;
4681 		for(z=0; z<16; z++)
4682 		{
4683 			VECTORCH *point = &MeshVertex[i];
4684 			int offset = ForceFieldPointDisplacement[x][z];
4685 
4686 		#if 1
4687 			int u = (x*65536)/45;
4688 
4689 			int b = MUL_FIXED(2*u,(65536-u));
4690 			int c = MUL_FIXED(u,u);
4691 			int y3 = 45*MeshXScale;
4692 			int x3 = 5000;
4693 			int y2 = 1*MeshXScale;
4694 			int x2 = GetSin(CloakingPhase&4095)+GetCos((CloakingPhase*3+399)&4095);
4695 			x2 = MUL_FIXED(x2,x2)/128;
4696 
4697 			if (offset<0) offset =-offset;
4698 			point->vx = xOrigin+MUL_FIXED(b,x2)+MUL_FIXED(c,x3)+offset;
4699 			point->vy = yOrigin+MUL_FIXED(b,y2)+MUL_FIXED(c,y3);
4700 			point->vz = zOrigin+(z*MeshZScale);
4701 
4702 			if (point->vy>4742)
4703 			{
4704 				if (z<=4)
4705 				{
4706 					point->vy-=MeshXScale;
4707 					if (point->vy<4742) point->vy=4742;
4708 					if (point->vx<179427) point->vx=179427;
4709 				}
4710 				else if (z<=8)
4711 				{
4712 					point->vx+=(8-z)*1000;
4713 				}
4714 			}
4715 
4716 			#else
4717 			if (offset<0) offset =-offset;
4718 		 	point->vx = xOrigin-offset;
4719 		 	point->vy = yOrigin+(x*MeshXScale);
4720 		 	point->vz = zOrigin+(z*MeshZScale);
4721 			#endif
4722 
4723 
4724 
4725 
4726 			offset= (offset/4)+127;
4727 
4728 //			offset-=32;
4729 //			if (offset<0) offset = 0;
4730 
4731 			if(offset>255) offset=255;
4732 
4733 			MeshVertexColour[i] = RGBALIGHT_MAKE(offset,offset,255,offset/2);
4734 			#if TEXTURE_WATER
4735 			MeshWorldVertex[i].vx = point->vx;
4736 			MeshWorldVertex[i].vz = point->vz;
4737 			#endif
4738 
4739 			/* translate particle into view space */
4740 			TranslatePointIntoViewspace(point);
4741 
4742 			/* is particle within normal view frustrum ? */
4743 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
4744 			{
4745 				if(( (-point->vx <= point->vz*2)
4746 		   			&&(point->vx <= point->vz*2)
4747 					&&(-point->vy <= point->vz*2)
4748 					&&(point->vy <= point->vz*2) ))
4749 				{
4750 					MeshVertexOutcode[i]=1;
4751 				}
4752 				else
4753 				{
4754 					MeshVertexOutcode[i]=0;
4755 				}
4756 			}
4757 			else
4758 			{
4759 				if(( (-point->vx <= point->vz)
4760 		   			&&(point->vx <= point->vz)
4761 					&&(-point->vy <= point->vz)
4762 					&&(point->vy <= point->vz) ))
4763 				{
4764 					MeshVertexOutcode[i]=1;
4765 				}
4766 				else
4767 				{
4768 					MeshVertexOutcode[i]=0;
4769 				}
4770 			}
4771 
4772 			i++;
4773 		}
4774 	}
4775 	//textprint("\n");
4776 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
4777 	{
4778 		D3D_DrawWaterMesh_Unclipped();
4779 	}
4780 	else
4781 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
4782 	{
4783 		D3D_DrawWaterMesh_Clipped();
4784 	}
4785 	}
4786 }
4787 
D3D_DrawParticleBeam(DISPLAYBLOCK * muzzlePtr,VECTORCH * targetPositionPtr)4788 void D3D_DrawParticleBeam(DISPLAYBLOCK *muzzlePtr, VECTORCH *targetPositionPtr)
4789 {
4790 	VECTORCH vertices[6];
4791 	int beamRadius = 45+(FastRandom()&15);
4792 	vertices[0] = *targetPositionPtr;
4793 	vertices[1] = *targetPositionPtr;
4794 	vertices[2] = *targetPositionPtr;
4795 
4796 	vertices[3] = muzzlePtr->ObWorld;
4797 	vertices[4] = muzzlePtr->ObWorld;
4798 	vertices[5] = muzzlePtr->ObWorld;
4799 
4800 	vertices[1].vy += beamRadius;
4801 	vertices[2].vy -= beamRadius;
4802 	vertices[4].vy += beamRadius;
4803 	vertices[5].vy -= beamRadius;
4804 
4805 	/* translate vertices into vxew space */
4806 	TranslatePointIntoViewspace(&vertices[0]);
4807 	TranslatePointIntoViewspace(&vertices[1]);
4808 	TranslatePointIntoViewspace(&vertices[2]);
4809 	TranslatePointIntoViewspace(&vertices[3]);
4810 	TranslatePointIntoViewspace(&vertices[4]);
4811 	TranslatePointIntoViewspace(&vertices[5]);
4812 
4813 	float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
4814 
4815 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
4816 	{
4817 		int i = 6;
4818 		VECTORCH *verticesPtr = vertices;
4819 		do
4820 		{
4821 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
4822 			int x = (verticesPtr->vx*(Global_VDB_Ptr->VDB_ProjX))/verticesPtr->vz+Global_VDB_Ptr->VDB_CentreX;
4823 			int y = (verticesPtr->vy*(Global_VDB_Ptr->VDB_ProjY))/verticesPtr->vz+Global_VDB_Ptr->VDB_CentreY;
4824 			{
4825 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
4826 				{
4827 					x=Global_VDB_Ptr->VDB_ClipLeft;
4828 				}
4829 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
4830 				{
4831 					x=Global_VDB_Ptr->VDB_ClipRight;
4832 				}
4833 
4834 				vertexPtr->sx=x;
4835 			}
4836 			{
4837 				if (y<Global_VDB_Ptr->VDB_ClipUp)
4838 				{
4839 					y=Global_VDB_Ptr->VDB_ClipUp;
4840 				}
4841 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
4842 				{
4843 					y=Global_VDB_Ptr->VDB_ClipDown;
4844 				}
4845 				vertexPtr->sy=y;
4846 			}
4847 
4848 		  	float oneOverZ = ((float)verticesPtr->vz-ZNear)/(float)verticesPtr->vz;
4849 
4850 		  	 // Different behavxour for different driver modes
4851 			switch (D3DDriverMode)
4852 			{
4853 				default:
4854 				case D3DSoftwareRGBDriver:
4855 				case D3DSoftwareRampDriver:
4856 				break;
4857 				case D3DHardwareRGBDriver:
4858 				{
4859 					if (i==6 || i==3) vertexPtr->color = RGBALIGHT_MAKE(255,255,255,255);
4860 					else vertexPtr->color = RGBALIGHT_MAKE(0,0,255,32);
4861 
4862 					break;
4863 				}
4864 			}
4865 			vertexPtr->sz = oneOverZ;
4866 			NumVertices++;
4867 			verticesPtr++;
4868 		}
4869 	  	while(--i);
4870 	}
4871 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
4872 
4873 	OP_TRIANGLE_LIST(4, ExecBufInstPtr);
4874 	OUTPUT_TRIANGLE(0,3,5, 6);
4875 	OUTPUT_TRIANGLE(0,3,4, 6);
4876 	OUTPUT_TRIANGLE(0,2,5, 6);
4877 	OUTPUT_TRIANGLE(0,1,4, 6);
4878 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
4879 	{
4880 	   WriteEndCodeToExecuteBuffer();
4881   	   UnlockExecuteBufferAndPrepareForUse();
4882 	   ExecuteBuffer();
4883   	   LockExecuteBuffer();
4884 	}
4885 }
D3D_DrawExplosion(int xOrigin,int yOrigin,int zOrigin,int size)4886 void D3D_DrawExplosion(int xOrigin, int yOrigin, int zOrigin, int size)
4887 {
4888 			if (NumVertices)
4889 			{
4890 			   WriteEndCodeToExecuteBuffer();
4891 		  	   UnlockExecuteBufferAndPrepareForUse();
4892 			   ExecuteBuffer();
4893 		  	   LockExecuteBuffer();
4894 			}
4895 
4896 			// Turn OFF texturing if it is on...
4897 			if (CurrTextureHandle != NULL)
4898 			{
4899 				OP_STATE_RENDER(1, ExecBufInstPtr);
4900 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
4901 				CurrTextureHandle = NULL;
4902 			}
4903 
4904 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
4905 
4906 
4907 		 	OP_STATE_RENDER(1, ExecBufInstPtr);
4908 		 	STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
4909 	MeshXScale = 4096/16;
4910 	MeshZScale = 4096/16;
4911 
4912 	int i=0;
4913 	int x;
4914 	for (x=0; x<16; x++)
4915 	{
4916 		int z;
4917 		for(z=0; z<16; z++)
4918 		{
4919 			VECTORCH *point = &MeshVertex[i];
4920 			int offset;// = MUL_FIXED((FastRandom()&1023)-512,CloakingPhase&65535);//MUL_FIXED(255,GetSin((point->vx+point->vy+point->vz+CloakingPhase)&4095));
4921 			int sd;
4922 			{
4923 				int theta = ((z*2048)/15)&4095;
4924 				int phi = ((x*4096)/15)&4095;
4925 				if (theta==0) sd = 0;
4926 				else sd = phi+theta;
4927 				if (phi==0 || phi==2048) sd = 0;
4928 				else sd = phi+theta;
4929 
4930 				int outerRadius = MUL_FIXED(4000,size);
4931 
4932 				if (sd)
4933 				offset = MUL_FIXED((FastRandom()&511)-255,size);
4934 
4935 				offset=0;
4936 
4937 				point->vx = xOrigin+MUL_FIXED(outerRadius+offset,MUL_FIXED(GetSin(theta),GetCos(phi)));
4938 				point->vy = yOrigin+MUL_FIXED(outerRadius+offset,MUL_FIXED(GetSin(theta),GetSin(phi)));
4939 				point->vz = zOrigin+MUL_FIXED(outerRadius+offset,GetCos(theta));
4940 			}
4941 
4942 			if(offset<0) offset = -offset;
4943 			MeshVertexColour[i] = RGBALIGHT_MAKE(255,GetSin((sd+CloakingPhase)&4095)&255,0,32);
4944 
4945 			TranslatePointIntoViewspace(point);
4946 
4947 			/* is particle within normal view frustrum ? */
4948 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
4949 			{
4950 				if(( (-point->vx <= point->vz*2)
4951 		   			&&(point->vx <= point->vz*2)
4952 					&&(-point->vy <= point->vz*2)
4953 					&&(point->vy <= point->vz*2) ))
4954 				{
4955 					MeshVertexOutcode[i]=1;
4956 				}
4957 				else
4958 				{
4959 					MeshVertexOutcode[i]=0;
4960 				}
4961 			}
4962 			else
4963 			{
4964 				if(( (-point->vx <= point->vz)
4965 		   			&&(point->vx <= point->vz)
4966 					&&(-point->vy <= point->vz)
4967 					&&(point->vy <= point->vz) ))
4968 				{
4969 					MeshVertexOutcode[i]=1;
4970 				}
4971 				else
4972 				{
4973 					MeshVertexOutcode[i]=0;
4974 				}
4975 			}
4976 
4977 			i++;
4978 		}
4979 	}
4980 	textprint("drawing explosion\n");
4981 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
4982 	{
4983 		D3D_DrawWaterMesh_Unclipped();
4984 	}
4985 	else
4986 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
4987 	{
4988 		D3D_DrawWaterMesh_Clipped();
4989 	}
4990 			OP_STATE_RENDER(1, ExecBufInstPtr);
4991 			STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
4992 
4993 }
4994 
4995 
DrawFrameRateBar(void)4996 void DrawFrameRateBar(void)
4997 {
4998 	extern int NormalFrameTime;
4999 	#if 1
5000 	{
5001 		int width = DIV_FIXED(ScreenDescriptorBlock.SDB_Width/120,NormalFrameTime);
5002 		if (width>ScreenDescriptorBlock.SDB_Width) width=ScreenDescriptorBlock.SDB_Width;
5003 
5004 		r2rect rectangle
5005 		(
5006 			0,0,
5007 			width,
5008 			24
5009 		);
5010 		textprint("width %d\n",width);
5011 
5012 		rectangle . AlphaFill
5013 		(
5014 			0xff, // unsigned char R,
5015 			0x00,// unsigned char G,
5016 			0x00,// unsigned char B,
5017 		   	128 // unsigned char translucency
5018 		);
5019 	}
5020 	#endif
5021 }
D3D_DrawAlienRedBlipIndicatingJawAttack(void)5022 void D3D_DrawAlienRedBlipIndicatingJawAttack(void)
5023 {
5024 	r2rect rectangle
5025 	(
5026 		16,16,
5027 		32,
5028 		32
5029 	);
5030 
5031 	rectangle . AlphaFill
5032 	(
5033 		0xff, // unsigned char R,
5034 		0x00,// unsigned char G,
5035 		0x00,// unsigned char B,
5036 	   	128 // unsigned char translucency
5037 	);
5038 }
D3D_DrawBackdrop(void)5039 void D3D_DrawBackdrop(void)
5040 {
5041 	extern char LevelName[];
5042 
5043 	if (TRIPTASTIC_CHEATMODE||MOTIONBLUR_CHEATMODE) return;
5044 
5045 	if(WireFrameMode)
5046 	{
5047    		ColourFillBackBuffer(0);
5048 		return;
5049 	}
5050 	else if(ShowDebuggingText.Tears)
5051 	{
5052 		ColourFillBackBuffer((63<<5));
5053 		return;
5054 	}
5055 
5056 	{
5057 		int needToDrawBackdrop=0;
5058 		extern int NumActiveBlocks;
5059 		extern DISPLAYBLOCK *ActiveBlockList[];
5060 
5061 		int numOfObjects = NumActiveBlocks;
5062 		while(numOfObjects--)
5063 		{
5064 			DISPLAYBLOCK *objectPtr = ActiveBlockList[numOfObjects];
5065 			MODULE *modulePtr = objectPtr->ObMyModule;
5066 
5067 
5068 			if (modulePtr && (ModuleCurrVisArray[modulePtr->m_index] == 2) &&modulePtr->m_flags&MODULEFLAG_SKY)
5069 			{
5070 				needToDrawBackdrop=1;
5071 				break;
5072 			}
5073 		}
5074 		if(needToDrawBackdrop)
5075 		{
5076 			extern BOOL LevelHasStars;
5077 			extern void RenderSky(void);
5078 
5079 			ColourFillBackBuffer(0);
5080 
5081 			if (LevelHasStars)
5082 			{
5083 				extern void RenderStarfield(void);
5084 				RenderStarfield();
5085 			}
5086 			else
5087 			{
5088 		  		RenderSky();
5089 			}
5090 			return;
5091 		}
5092 	}
5093 
5094 
5095 	/* if the player is outside the environment, clear the screen! */
5096 	{
5097 		extern MODULE *playerPherModule;
5098  		if (!playerPherModule)
5099  		{
5100  			ColourFillBackBuffer(0);
5101 			return;
5102 		}
5103 	}
5104 	{
5105 		PLAYER_STATUS *playerStatusPtr= (PLAYER_STATUS *) (Player->ObStrategyBlock->SBdataptr);
5106 
5107 		if (!playerStatusPtr->IsAlive || FREEFALL_CHEATMODE)
5108 		{
5109 			// minimise effects of camera glitches
5110 			ColourFillBackBuffer(0);
5111 			return;
5112 		}
5113 	}
5114 }
5115 
MakeNoiseTexture(void)5116 void MakeNoiseTexture(void)
5117 {
5118 // 	return;
5119 	DDSURFACEDESC ddsd;
5120 	LPDIRECTDRAWSURFACE tempSurface;
5121 	LPDIRECT3DTEXTURE tempTexture;
5122 
5123 	LPDIRECTDRAWSURFACE destSurface;
5124 	LPDIRECT3DTEXTURE destTexture;
5125 
5126 
5127 	memcpy(&ddsd, &(d3d.TextureFormat[d3d.CurrentTextureFormat].ddsd), sizeof(ddsd));
5128 
5129 	ddsd.dwSize = sizeof(ddsd);
5130 
5131 	ddsd.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
5132 	ddsd.ddsCaps.dwCaps = (DDSCAPS_SYSTEMMEMORY|DDSCAPS_TEXTURE);
5133 
5134 	ddsd.dwHeight = 256;
5135 	ddsd.dwWidth = 256;
5136 
5137 	LastError = lpDD->CreateSurface(&ddsd, &tempSurface, NULL);
5138 	LOGDXERR(LastError);
5139 
5140 
5141 
5142 	LastError = tempSurface->QueryInterface(IID_IDirect3DTexture, (LPVOID*) &tempTexture);
5143 	LOGDXERR(LastError);
5144 
5145 	// Query destination surface for a texture interface.
5146 	memset(&ddsd, 0, sizeof(DDSURFACEDESC));
5147 	ddsd.dwSize = sizeof(DDSURFACEDESC);
5148 
5149 	LastError = tempSurface->GetSurfaceDesc(&ddsd);
5150 	LOGDXERR(LastError);
5151 
5152 	ddsd.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
5153 	ddsd.ddsCaps.dwCaps = (DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD );
5154 
5155 	LastError = lpDD->CreateSurface(&ddsd, &destSurface, NULL);
5156 	LOGDXERR(LastError);
5157 
5158 	/* KJL 11:59:21 09/02/98 - check for palettised modes */
5159 	{
5160 		int PalCaps;
5161 
5162 		if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
5163 		{
5164 			PalCaps = (DDPCAPS_8BIT | DDPCAPS_ALLOW256);
5165 		}
5166 		#if 0
5167 		else if (ddsd.ddpfPixelFormat.dwFlags &	DDPF_PALETTEINDEXED4)
5168 		{
5169 			PalCaps = DDPCAPS_4BIT;
5170 		}
5171 		else
5172 		#endif
5173 		{
5174 			PalCaps = 0;
5175 		}
5176 
5177 		if (PalCaps)
5178 		{
5179 			LPDIRECTDRAWPALETTE destPalette = NULL;
5180 			PALETTEENTRY palette[256];
5181 			memset(palette, 0, sizeof(PALETTEENTRY) * 256);
5182 			for(int i=0;i<256;i++)
5183 			{
5184 				palette[i].peRed = i;
5185 				palette[i].peGreen = i;
5186 				palette[i].peBlue = i;
5187 			}
5188 
5189 			LastError = lpDD->CreatePalette(PalCaps, palette, &destPalette, NULL);
5190 			LOGDXERR(LastError);
5191 
5192 			LastError = destSurface->SetPalette(destPalette);
5193 			LastError = tempSurface->SetPalette(destPalette);
5194 			LOGDXERR(LastError);
5195 			{
5196 		   		memset(&ddsd, 0, sizeof(DDSURFACEDESC));
5197 				ddsd.dwSize = sizeof(DDSURFACEDESC);
5198 				LastError = tempSurface->Lock(NULL, &ddsd, 0, NULL);
5199 				LOGDXERR(LastError);
5200 
5201 
5202 				unsigned char *dst = (unsigned char *)ddsd.lpSurface;
5203 				LOCALASSERT(dst);
5204 			 	for(int i = 0; i < 256; i ++)
5205 			 	{
5206 		 			for(int j = 0; j < 256; j ++)
5207 			 		{
5208 						int c = FastRandom()&255;
5209 						*dst++ = (c);
5210 					}
5211 			 		/* move the pitch to width difference */
5212 			 		//dst += ddsd.lPitch - 256;
5213 			 	}
5214 
5215 				LastError = tempSurface->Unlock(NULL);
5216 				LOGDXERR(LastError);
5217 			}
5218 		}
5219 		else
5220 		{
5221 			{
5222 		   		memset(&ddsd, 0, sizeof(DDSURFACEDESC));
5223 				ddsd.dwSize = sizeof(DDSURFACEDESC);
5224 				LastError = tempSurface->Lock(NULL, &ddsd, 0, NULL);
5225 				LOGDXERR(LastError);
5226 
5227 
5228 				unsigned short *dst = (unsigned short *)ddsd.lpSurface;
5229 				LOCALASSERT(dst);
5230 			 	for(int i = 0; i < 256; i ++)
5231 			 	{
5232 					int density = FastRandom()&31;
5233 					{
5234 			 			for(int j = 0; j < 256; j ++)
5235 				 		{
5236 							int c = FastRandom()&31;
5237 							*dst++ = (c)+(c<<6)+(c<<11);
5238 				 		}
5239 					}
5240 			 		/* move the pitch to width difference */
5241 			 		//dst += ddsd.lPitch - 256;
5242 			 	}
5243 
5244 				LastError = tempSurface->Unlock(NULL);
5245 				LOGDXERR(LastError);
5246 			}
5247 		}
5248 	}
5249 
5250 	LastError = destSurface->QueryInterface(IID_IDirect3DTexture,(LPVOID*) &destTexture);
5251 	LOGDXERR(LastError);
5252 
5253 	LastError = destTexture->Load(tempTexture);
5254  	LOGDXERR(LastError);
5255 
5256 	// Clean up surfaces etc
5257 //	RELEASE(SrcTexture);
5258 	LastError = destTexture->GetHandle(d3d.lpD3DDevice, &NoiseTextureHandle);
5259 	RELEASE(tempSurface);
5260 	RELEASE(tempTexture);
5261 
5262 }
DrawNoiseOverlay(int t)5263 void DrawNoiseOverlay(int t)
5264 {
5265 //	return;
5266 	{
5267 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5268 
5269 		extern float CameraZoomScale;
5270 		float u = FastRandom()&255;
5271 		float v = FastRandom()&255;
5272 		int c = 255;
5273 		int size = 256;//*CameraZoomScale;
5274 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5275 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5276 		vertexPtr->sz = 1.0f;
5277 		vertexPtr->rhw = 1.0f;
5278 		vertexPtr->tu = u/256.0;
5279 		vertexPtr->tv = v/256.0;
5280 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5281 		vertexPtr++;
5282 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5283 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5284 		vertexPtr->sz = 1.0f;
5285 		vertexPtr->rhw = 1.0f;
5286 		vertexPtr->tu = (u+size)/256.0;
5287 		vertexPtr->tv = v/256.0;
5288 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5289 		vertexPtr++;
5290 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5291 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5292 		vertexPtr->sz = 1.0f;
5293 		vertexPtr->rhw = 1.0f;
5294 		vertexPtr->tu = (u+size)/256.0;
5295 		vertexPtr->tv = (v+size)/256.0;
5296 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5297 		vertexPtr++;
5298 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5299 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5300 		vertexPtr->sz = 1.0f;
5301 		vertexPtr->rhw = 1.0f;
5302 		vertexPtr->tu = u/256.0;
5303 		vertexPtr->tv = (v+size)/256.0;
5304 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5305 
5306 		NumVertices+=4;
5307 	}
5308 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
5309 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
5310     OP_STATE_RENDER(1, ExecBufInstPtr);
5311     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_ALWAYS, ExecBufInstPtr);
5312 
5313  	NoiseTextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[StaticImageNumber].D3DHandle;
5314     if (CurrTextureHandle != NoiseTextureHandle)
5315 	{
5316     	OP_STATE_RENDER(1, ExecBufInstPtr);
5317         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NoiseTextureHandle, ExecBufInstPtr);
5318         CurrTextureHandle = NoiseTextureHandle;
5319 	}
5320     if (D3DTexturePerspective != No)
5321 	{
5322 		D3DTexturePerspective = No;
5323 		OP_STATE_RENDER(1, ExecBufInstPtr);
5324 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, FALSE, ExecBufInstPtr);
5325 	}
5326 
5327 
5328 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
5329 	OUTPUT_TRIANGLE(0,1,3, 4);
5330 	OUTPUT_TRIANGLE(1,2,3, 4);
5331 
5332     OP_STATE_RENDER(1, ExecBufInstPtr);
5333     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
5334 
5335 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
5336 	{
5337 	   WriteEndCodeToExecuteBuffer();
5338   	   UnlockExecuteBufferAndPrepareForUse();
5339 	   ExecuteBuffer();
5340   	   LockExecuteBuffer();
5341 	}
5342 
5343 }
DrawScanlinesOverlay(float level)5344 void DrawScanlinesOverlay(float level)
5345 {
5346 //	return;
5347 	{
5348 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5349 
5350 		float u = 0.0f;//FastRandom()&255;
5351 		float v = 128.0f;//FastRandom()&255;
5352 		int c = 255;
5353 		extern float CameraZoomScale;
5354 		int t;
5355 	   	f2i(t,64.0f+level*64.0f);
5356 
5357 		float size = 128.0f*(1.0f-level*0.8f);//*CameraZoomScale;
5358 
5359 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5360 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5361 		vertexPtr->sz = 1.0f;
5362 		vertexPtr->rhw = 1.0f;
5363 		vertexPtr->tu = (v-size)/256.0f;
5364 		vertexPtr->tv = 1.0f;
5365 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5366 		vertexPtr++;
5367 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5368 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5369 		vertexPtr->sz = 1.0f;
5370 		vertexPtr->rhw = 1.0f;
5371 		vertexPtr->tu = (v-size)/256.0f;
5372 		vertexPtr->tv = 1.0f;
5373 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5374 		vertexPtr++;
5375 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5376 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5377 		vertexPtr->sz = 1.0f;
5378 		vertexPtr->rhw = 1.0f;
5379 		vertexPtr->tu = (v+size)/256.0f;
5380 		vertexPtr->tv = 1.0f;
5381 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5382 		vertexPtr++;
5383 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5384 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5385 		vertexPtr->sz = 1.0f;
5386 		vertexPtr->rhw = 1.0f;
5387 		vertexPtr->tu = (v+size)/256.0f;
5388 		vertexPtr->tv = 1.0f;
5389 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,t);
5390 
5391 		NumVertices+=4;
5392 	}
5393 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
5394 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
5395     OP_STATE_RENDER(1, ExecBufInstPtr);
5396     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_ALWAYS, ExecBufInstPtr);
5397 
5398  	NoiseTextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[PredatorNumbersImageNumber].D3DHandle;
5399     if (CurrTextureHandle != NoiseTextureHandle)
5400 	{
5401     	OP_STATE_RENDER(1, ExecBufInstPtr);
5402         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NoiseTextureHandle, ExecBufInstPtr);
5403         CurrTextureHandle = NoiseTextureHandle;
5404 	}
5405     if (D3DTexturePerspective != No)
5406 	{
5407 		D3DTexturePerspective = No;
5408 		OP_STATE_RENDER(1, ExecBufInstPtr);
5409 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, FALSE, ExecBufInstPtr);
5410 	}
5411 
5412 
5413 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
5414 	OUTPUT_TRIANGLE(0,1,3, 4);
5415 	OUTPUT_TRIANGLE(1,2,3, 4);
5416 
5417     OP_STATE_RENDER(1, ExecBufInstPtr);
5418     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
5419 
5420 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
5421 	{
5422 	   WriteEndCodeToExecuteBuffer();
5423   	   UnlockExecuteBufferAndPrepareForUse();
5424 	   ExecuteBuffer();
5425   	   LockExecuteBuffer();
5426 	}
5427 
5428 	if (level==1.0f) DrawNoiseOverlay(128);
5429 }
DrawPredatorVisionChangeOverlay(int time)5430 void DrawPredatorVisionChangeOverlay(int time)
5431 {
5432 //	return;
5433 	{
5434 	if (time>ONE_FIXED)
5435 	for(int i=0; i<8; i++)
5436 	{
5437 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5438 		int t = (time-ONE_FIXED-((i+3)&7)*2048)*2;
5439 		if (t<0) t = 0;
5440 
5441 		float rightU;
5442 		float leftU;
5443 		if (t>ONE_FIXED)
5444 		{
5445 			leftU = 0.0f;
5446 			rightU = 0.5f;
5447 		}
5448 		else if (t>ONE_FIXED/2)
5449 		{
5450 			leftU = 1.0f - ((float)(t))/65536.0f;
5451 			rightU = 1.0f - ((float)(t-ONE_FIXED/2))/65536.0f;
5452 		}
5453 		else
5454 		{
5455 			leftU = 1.0f - ((float)(t))/65536.0f;
5456 			rightU = 1.0f;
5457 		}
5458 
5459 		float topV = 44.0f/128.0f;
5460 		float bottomV = 119.0f/128.0f;
5461 		if (leftU<0) leftU=0;
5462 		PrintDebuggingText("%f %f\n",leftU,rightU);
5463 		int rightX = MUL_FIXED((Global_VDB_Ptr->VDB_ClipRight-Global_VDB_Ptr->VDB_ClipLeft),t)+Global_VDB_Ptr->VDB_ClipLeft;
5464 		if (rightX>Global_VDB_Ptr->VDB_ClipRight) rightX = Global_VDB_Ptr->VDB_ClipRight;
5465 		int y = i*60;
5466 		int h = 60;//+MUL_FIXED(10,time-ONE_FIXED);
5467 		int c = 255;
5468 
5469 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5470 	  	vertexPtr->sy =	y;
5471 		vertexPtr->sz = 0;
5472 		vertexPtr->rhw = 1.0f;
5473 		vertexPtr->tu = leftU;
5474 		vertexPtr->tv = topV;
5475 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5476 		vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5477 		vertexPtr++;
5478 	  	vertexPtr->sx =	rightX;
5479 	  	vertexPtr->sy =	y;
5480 		vertexPtr->sz = 0;
5481 		vertexPtr->rhw = 1.0f;
5482 		vertexPtr->tu = rightU;
5483 		vertexPtr->tv = topV;
5484 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5485 		vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5486 		vertexPtr++;
5487 	  	vertexPtr->sx =	rightX;
5488 	  	vertexPtr->sy = y+h;
5489 		vertexPtr->sz = 0;
5490 		vertexPtr->rhw = 1.0f;
5491 		vertexPtr->tu = rightU;
5492 		vertexPtr->tv = bottomV;
5493 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5494 		vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5495 		vertexPtr++;
5496 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5497 	  	vertexPtr->sy =	y+h;
5498 		vertexPtr->sz = 0;
5499 		vertexPtr->rhw = 1.0f;
5500 		vertexPtr->tu = leftU;
5501 		vertexPtr->tv = bottomV;
5502 		vertexPtr->color = RGBALIGHT_MAKE(255,255,255,255);
5503 		vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5504 
5505 		NumVertices+=4;
5506 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_COLOUR);
5507 		CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
5508 
5509 	 //	NoiseTextureHandle = 0;
5510 		D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[PredatorVisionChangeImageNumber].D3DHandle;
5511 	    if (CurrTextureHandle != TextureHandle)
5512 		{
5513 	    	OP_STATE_RENDER(1, ExecBufInstPtr);
5514 	        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
5515 	        CurrTextureHandle = TextureHandle;
5516 		}
5517 
5518 		OP_TRIANGLE_LIST(2, ExecBufInstPtr);
5519 		OUTPUT_TRIANGLE(0,1,3, 4);
5520 		OUTPUT_TRIANGLE(1,2,3, 4);
5521 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
5522 		{
5523 		   WriteEndCodeToExecuteBuffer();
5524 	  	   UnlockExecuteBufferAndPrepareForUse();
5525 		   ExecuteBuffer();
5526 	  	   LockExecuteBuffer();
5527 		}
5528 	}
5529 	}
5530 
5531 	{
5532 	if (time>ONE_FIXED+8192) time = ONE_FIXED+8192;
5533 	{
5534 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5535 
5536 
5537 		float rightU = 1.0f;
5538 		float leftU = 1.0f - ((float)time)/65536.0f;
5539 		float v = 0;
5540 		if (leftU<0) leftU=0;
5541 		PrintDebuggingText("%f %f\n",leftU,rightU);
5542 		int rightX = MUL_FIXED((Global_VDB_Ptr->VDB_ClipRight-Global_VDB_Ptr->VDB_ClipLeft),time)+Global_VDB_Ptr->VDB_ClipLeft;
5543 		int c=128;
5544 
5545 		int size = 256/4;
5546 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5547 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5548 		vertexPtr->sz = 0.001;
5549 		vertexPtr->rhw = 1.0f;
5550 		vertexPtr->tu = leftU;
5551 		vertexPtr->tv = v/256.0;
5552 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5553 		vertexPtr->specular = 0;
5554 		vertexPtr++;
5555 	  	vertexPtr->sx =	rightX;
5556 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5557 		vertexPtr->sz = 0.001;
5558 		vertexPtr->rhw = 1.0f;
5559 		vertexPtr->tu = rightU;
5560 		vertexPtr->tv = v/256.0;
5561 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5562 		vertexPtr->specular = 0;
5563 		vertexPtr++;
5564 	  	vertexPtr->sx =	rightX;
5565 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5566 		vertexPtr->sz = 0.001;
5567 		vertexPtr->rhw = 1.0f;
5568 		vertexPtr->tu = rightU;
5569 		vertexPtr->tv = (v+size)/256.0;
5570 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5571 		vertexPtr->specular = 0;
5572 		vertexPtr++;
5573 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5574 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5575 		vertexPtr->sz = 0.001;
5576 		vertexPtr->rhw = 1.0f;
5577 		vertexPtr->tu = leftU;
5578 		vertexPtr->tv = (v+size)/256.0;
5579 		vertexPtr->color = RGBALIGHT_MAKE(c,c,c,255);
5580 		vertexPtr->specular = 0;
5581 
5582 		NumVertices+=4;
5583 	}
5584 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
5585 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
5586 
5587  //	NoiseTextureHandle = 0;
5588 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[PredatorVisionChangeImageNumber].D3DHandle;
5589     if (CurrTextureHandle != TextureHandle)
5590 	{
5591     	OP_STATE_RENDER(1, ExecBufInstPtr);
5592         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
5593         CurrTextureHandle = TextureHandle;
5594 	}
5595 
5596 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
5597 	OUTPUT_TRIANGLE(0,1,3, 4);
5598 	OUTPUT_TRIANGLE(1,2,3, 4);
5599 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
5600 	{
5601 	   WriteEndCodeToExecuteBuffer();
5602   	   UnlockExecuteBufferAndPrepareForUse();
5603 	   ExecuteBuffer();
5604   	   LockExecuteBuffer();
5605 	}
5606 	}
5607 
5608 }
5609 #if 0
5610 #define OCTAVES 1
5611 float u[OCTAVES];
5612 float v[OCTAVES];
5613 float du[OCTAVES];
5614 float dv[OCTAVES];
5615 int setup=0;
5616 void DrawFBM(void)
5617 {
5618 	extern int CloudyImageNumber;
5619 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[CloudyImageNumber].D3DHandle;
5620 //	return;		   1
5621 	int i;
5622 	int size = 256;
5623 //	float u = FastRandom()&255;
5624 //	float v = FastRandom()&255;
5625 	int t =255;
5626 	if(!setup)
5627 	{
5628 		setup=1;
5629 		for(i=OCTAVES-1;i>=0;i--)
5630 		{
5631 			du[i] = ( (float)((FastRandom()&65535)-32768)*(i+1) )/16384.0;
5632 			dv[i] = ( (float)((FastRandom()&65535)-32768)*(i+1) )/16384.0;
5633 		}
5634 	}
5635 
5636 	float timeScale = ((float)NormalFrameTime)/65536.0;
5637 	for(i=0; i<OCTAVES; i++)
5638 	{
5639 		u[i]+=du[i]*timeScale;
5640 		v[i]+=dv[i]*timeScale;
5641 		#if 1
5642 		{
5643 			MATRIXCH mat = (Global_VDB_Ptr->VDB_Mat);
5644 			VECTORCH v[4]=
5645 			{
5646 				{-ONE_FIXED,-ONE_FIXED,ONE_FIXED},
5647 				{ ONE_FIXED,-ONE_FIXED,ONE_FIXED},
5648 				{ ONE_FIXED, 0,	ONE_FIXED},
5649 				{-ONE_FIXED, 0,	ONE_FIXED},
5650 			};
5651 			TransposeMatrixCH(&mat);
5652 			RotateVector(&v[0],&mat);
5653 			RotateVector(&v[1],&mat);
5654 			RotateVector(&v[2],&mat);
5655 			RotateVector(&v[3],&mat);
5656 			if (v[0].vy=0) v[0].vy=-1;
5657 			if (v[1].vy=0) v[1].vy=-1;
5658 			if (v[2].vy=0) v[2].vy=-1;
5659 			if (v[3].vy=0) v[3].vy=-1;
5660 			if (v[0].vy>0) v[0].vy=-v[0].vy;
5661 			if (v[1].vy>0) v[1].vy=-v[1].vy;
5662 			if (v[2].vy>0) v[2].vy=-v[2].vy;
5663 			if (v[3].vy>0) v[3].vy=-v[3].vy;
5664 
5665 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5666 
5667 			vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5668 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5669 			vertexPtr->sz = 1;
5670 			vertexPtr->tu = -((float)v[0].vx)/((float)v[0].vy)/1.0;
5671 			vertexPtr->tv = -((float)v[0].vz)/((float)v[0].vy)/1.0;
5672 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5673 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5674 			vertexPtr++;
5675 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5676 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5677 			vertexPtr->sz = 1;
5678 			vertexPtr->tu = -((float)v[1].vx)/((float)v[1].vy)/1.0;
5679 			vertexPtr->tv = -((float)v[1].vz)/((float)v[1].vy)/1.0;
5680 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5681 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5682 			vertexPtr++;
5683 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5684 		  	vertexPtr->sy =	(Global_VDB_Ptr->VDB_ClipUp+Global_VDB_Ptr->VDB_ClipDown)/2;
5685 			vertexPtr->sz = 1;
5686 			vertexPtr->tu = -((float)v[2].vx)/((float)v[2].vy)/1.0;
5687 			vertexPtr->tv = -((float)v[2].vz)/((float)v[2].vy)/1.0;
5688 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5689 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5690 			vertexPtr++;
5691 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5692 		  	vertexPtr->sy =	(Global_VDB_Ptr->VDB_ClipUp+Global_VDB_Ptr->VDB_ClipDown)/2;
5693 			vertexPtr->sz = 1;
5694 			vertexPtr->tu = -((float)v[3].vx)/((float)v[3].vy)/1.0;
5695 			vertexPtr->tv = -((float)v[3].vz)/((float)v[3].vy)/1.0;
5696 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5697 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5698 
5699 			NumVertices+=4;
5700 		}
5701 
5702 		#else
5703 		{
5704 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5705 
5706 			vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5707 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5708 			vertexPtr->sz = 1;
5709 			vertexPtr->tu = u[i]/256.0;
5710 			vertexPtr->tv = v[i]/256.0;
5711 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5712 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5713 			vertexPtr++;
5714 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5715 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
5716 			vertexPtr->sz = 1;
5717 			vertexPtr->tu = (u[i]+size)/256.0;
5718 			vertexPtr->tv = v[i]/256.0;
5719 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5720 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5721 			vertexPtr++;
5722 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
5723 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5724 			vertexPtr->sz = 1;
5725 			vertexPtr->tu = (u[i]+size)/256.0;
5726 			vertexPtr->tv = (v[i]+size)/256.0;
5727 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5728 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5729 			vertexPtr++;
5730 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
5731 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
5732 			vertexPtr->sz = 1;
5733 			vertexPtr->tu = u[i]/256.0;
5734 			vertexPtr->tv = (v[i]+size)/256.0;
5735 			vertexPtr->color = RGBALIGHT_MAKE(200,255,255,t);
5736 			vertexPtr->specular = RGBALIGHT_MAKE(0,0,0,255);
5737 
5738 			NumVertices+=4;
5739 		}
5740 		#endif
5741 		size*=2;
5742 		t/=2;
5743 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
5744 		CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
5745 
5746 	 //	NoiseTextureHandle = 0;
5747 	    if (CurrTextureHandle != TextureHandle)
5748 		{
5749 	    	OP_STATE_RENDER(1, ExecBufInstPtr);
5750 	        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
5751 	        CurrTextureHandle = NoiseTextureHandle;
5752 		}
5753 	    if (D3DTexturePerspective != No)
5754 		{
5755 			D3DTexturePerspective = No;
5756 			OP_STATE_RENDER(1, ExecBufInstPtr);
5757 			STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, FALSE, ExecBufInstPtr);
5758 		}
5759 
5760 
5761 		OP_TRIANGLE_LIST(2, ExecBufInstPtr);
5762 		OUTPUT_TRIANGLE(0,1,3, 4);
5763 		OUTPUT_TRIANGLE(1,2,3, 4);
5764 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
5765 		{
5766 		   WriteEndCodeToExecuteBuffer();
5767 	  	   UnlockExecuteBufferAndPrepareForUse();
5768 		   ExecuteBuffer();
5769 	  	   LockExecuteBuffer();
5770 		}
5771 	}
5772 
5773 }
5774 #endif
D3D_SkyPolygon_Output(POLYHEADER * inputPolyPtr,RENDERVERTEX * renderVerticesPtr)5775 void D3D_SkyPolygon_Output(POLYHEADER *inputPolyPtr,RENDERVERTEX *renderVerticesPtr)
5776 {
5777 	int flags;
5778 	int texoffset;
5779 
5780 	D3DTEXTUREHANDLE TextureHandle;
5781 
5782 	float ZNear;
5783 	float RecipW, RecipH;
5784 
5785     // Get ZNear
5786 	ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
5787 
5788 
5789 	// Take header information
5790 	flags = inputPolyPtr->PolyFlags;
5791 
5792 	// We assume bit 15 (TxLocal) HAS been
5793 	// properly cleared this time...
5794 	texoffset = (inputPolyPtr->PolyColour & ClrTxDefn);
5795 
5796 	TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[texoffset].D3DHandle;
5797 
5798     // Check for textures that have not loaded
5799 	// properly
5800 
5801 //    if (TextureHandle == (D3DTEXTUREHANDLE) 0)
5802  //	  return;
5803 
5804 	#ifdef AVP_DEBUG_VERSION
5805 	if(ImageHeaderArray[texoffset].ImageWidth==128)
5806 	{
5807 		RecipW = (1.0 /128.0)/65536.0f;
5808 	}
5809 	else
5810 	{
5811 		float width = (float) ImageHeaderArray[texoffset].ImageWidth;
5812 		RecipW = (1.0 / width)/65536.0f;
5813 	}
5814 	if(ImageHeaderArray[texoffset].ImageHeight==128)
5815 	{
5816 		RecipH = (1.0 / 128.0)/65536.0f;
5817 	}
5818 	else
5819 	{
5820 		float height = (float) ImageHeaderArray[texoffset].ImageHeight;
5821 		RecipH = (1.0 / height)/65536.0f;
5822 	}
5823 	#else
5824 	RecipW = (1.0/65536.0)/128.0;
5825 	RecipH = (1.0/65536.0)/128.0;
5826 	#endif
5827 //	RecipW = (1.0/65536.0)/(float) ImageHeaderArray[texoffset].ImageWidth;
5828 //	RecipH = (1.0/65536.0)/(float) ImageHeaderArray[texoffset].ImageHeight;
5829 
5830 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
5831 	{
5832 		int i = RenderPolygon.NumberOfVertices;
5833 		RENDERVERTEX *vertices = renderVerticesPtr;
5834 
5835 		do
5836 		{
5837 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
5838 		  	float oneOverZ;
5839 		  	oneOverZ = (1.0)/(vertices->Z);
5840 			float zvalue;
5841 
5842 			vertexPtr->tu = ((float)vertices->U) * RecipW + (1.0/256.0);
5843 			vertexPtr->tv = ((float)vertices->V) * RecipH + (1.0/256.0);
5844 			vertexPtr->rhw = oneOverZ;
5845 
5846 			{
5847 				zvalue = vertices->Z+HeadUpDisplayZOffset;
5848 	   //			zvalue /= 65536.0;
5849 	   		   	zvalue = 1.0 - ZNear/zvalue;
5850 			}
5851 
5852 			{
5853 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
5854 
5855 				#if 1
5856 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
5857 				{
5858 					x=Global_VDB_Ptr->VDB_ClipLeft;
5859 				}
5860 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
5861 				{
5862 					x=Global_VDB_Ptr->VDB_ClipRight;
5863 				}
5864 				#endif
5865 				vertexPtr->sx=x;
5866 			}
5867 			{
5868 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
5869 
5870 				#if 1
5871 				if (y<Global_VDB_Ptr->VDB_ClipUp)
5872 				{
5873 					y=Global_VDB_Ptr->VDB_ClipUp;
5874 				}
5875 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
5876 				{
5877 					y=Global_VDB_Ptr->VDB_ClipDown;
5878 				}
5879 				#endif
5880 				vertexPtr->sy=y;
5881 
5882 			}
5883 //			vertexPtr->tu = ((float)(vertices->U>>16)+0.5) * RecipW;
5884 //			vertexPtr->tv = ((float)(vertices->V>>16)+0.5) * RecipH;
5885 //			vertexPtr->rhw = oneOverZ;
5886 
5887 
5888 	  		vertexPtr->color = RGBALIGHT_MAKE(vertices->R,vertices->G,vertices->B,vertices->A);
5889 
5890 			vertexPtr->sz = 1.0;
5891 			vertexPtr->specular=RGBALIGHT_MAKE(0,0,0,255);
5892 			vertices++;
5893 			NumVertices++;
5894 		}
5895 	  	while(--i);
5896 	}
5897 
5898 	CheckTranslucencyModeIsCorrect(RenderPolygon.TranslucencyMode);
5899 	// Insert state change for shading model if required
5900     if (D3DShadingMode != D3DSHADE_GOURAUD)
5901 	  {
5902    	   D3DShadingMode = D3DSHADE_GOURAUD;
5903 	   OP_STATE_RENDER(1, ExecBufInstPtr);
5904 	     STATE_DATA(D3DRENDERSTATE_SHADEMODE,
5905 		       D3DSHADE_GOURAUD, ExecBufInstPtr);
5906 	  }
5907 
5908 // Insert state change for texturing perspective value
5909 // Note that drawtx3das2d options have ONLY been allowed for here,
5910 // not when the rhw values are generated.  This is a deliberate choice,
5911 // based on the assumption that drawtx3das2d will not be used very often
5912 // and the extra branching at the top of this function will impose a
5913 // greater cost than the (rare) savings in floating pt divisions are worth.
5914 // Or so I claim...
5915 
5916     if (D3DTexturePerspective != Yes)
5917     {
5918 		D3DTexturePerspective = Yes;
5919 		OP_STATE_RENDER(1, ExecBufInstPtr);
5920 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
5921 	}
5922 
5923 	#if FMV_EVERYWHERE
5924 	TextureHandle = FMVTextureHandle[0];
5925 	#endif
5926 
5927     if (TextureHandle != CurrTextureHandle)
5928 	{
5929     	OP_STATE_RENDER(1, ExecBufInstPtr);
5930         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
5931 	   	CurrTextureHandle = TextureHandle;
5932 	}
5933 
5934 
5935 	D3D_OutputTriangles();
5936 }
5937 
D3D_DrawFMVOnWater(int xOrigin,int yOrigin,int zOrigin)5938 void D3D_DrawFMVOnWater(int xOrigin, int yOrigin, int zOrigin)
5939 {
5940 	int colour;
5941 	switch (CurrentVisionMode)
5942 	{
5943 		default:
5944 		case VISION_MODE_NORMAL:
5945 		{
5946 			colour = RGBLIGHT_MAKE(128,128,128);
5947 			break;
5948 		}
5949 		case VISION_MODE_IMAGEINTENSIFIER:
5950 		{
5951 			colour = RGBLIGHT_MAKE(0,255,0);
5952 			break;
5953 		}
5954 		case VISION_MODE_PRED_THERMAL:
5955 		case VISION_MODE_PRED_SEEALIENS:
5956 		case VISION_MODE_PRED_SEEPREDTECH:
5957 		{
5958 			colour = RGBLIGHT_MAKE(128,0,128);
5959 		  	break;
5960 		}
5961 	}
5962 	int i=0;
5963 	int x;
5964 	for (x=0; x<16; x++)
5965 	{
5966 		int z;
5967 		for(z=0; z<16; z++)
5968 		{
5969 			VECTORCH *point = &MeshVertex[i];
5970 
5971 			point->vz = zOrigin + (z*MeshZScale)/15;
5972 			point->vz += (FastRandom()&31);
5973 
5974 			point->vx = (x*MeshXScale)/15;
5975 			point->vx -= MeshXScale/2;
5976 			point->vx = (point->vx * (32-z))/16 + (FastRandom()&31);
5977 			point->vx += xOrigin + MeshXScale/2;
5978 
5979 			int offset = EffectOfRipples(point);
5980 	 		point->vy = yOrigin+offset;
5981 
5982 
5983 			{
5984 				int alpha = 192-offset*4;
5985 				if (alpha>255) alpha = 255;
5986 				if (alpha<128) alpha = 128;
5987 				MeshVertexColour[i] = colour + (alpha<<24);
5988 			}
5989 
5990 			#if 1
5991 			TranslatePointIntoViewspace(point);
5992 			#else
5993 			point->vx -= Global_VDB_Ptr->VDB_World.vx;
5994 			point->vy -= Global_VDB_Ptr->VDB_World.vy;
5995 			point->vz -= Global_VDB_Ptr->VDB_World.vz;
5996 			MeshWorldVertex[i] = *point;
5997 			RotateVector(point,&(Global_VDB_Ptr->VDB_Mat));
5998 			point->vy = MUL_FIXED(point->vy,87381);
5999 
6000 			#endif
6001 			/* is particle within normal view frustrum ? */
6002 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
6003 			{
6004 				if(( (-point->vx <= point->vz*2)
6005 		   			&&(point->vx <= point->vz*2)
6006 					&&(-point->vy <= point->vz*2)
6007 					&&(point->vy <= point->vz*2) ))
6008 				{
6009 					MeshVertexOutcode[i]=1;
6010 				}
6011 				else
6012 				{
6013 					MeshVertexOutcode[i]=0;
6014 				}
6015 			}
6016 			else
6017 			{
6018 				if(( (-point->vx <= point->vz)
6019 		   			&&(point->vx <= point->vz)
6020 					&&(-point->vy <= point->vz)
6021 					&&(point->vy <= point->vz) ))
6022 				{
6023 					MeshVertexOutcode[i]=1;
6024 				}
6025 				else
6026 				{
6027 					MeshVertexOutcode[i]=0;
6028 				}
6029 			}
6030 
6031 			{
6032 				// u
6033 				MeshWorldVertex[i].vx = (x*4096)/15;
6034 				// v
6035 				MeshWorldVertex[i].vy = (z*2048)/20;
6036 
6037 			}
6038 		   	i++;
6039 		}
6040 	}
6041 
6042 	D3DTEXTUREHANDLE TextureHandle = FMVTextureHandle[0];
6043 	if (CurrTextureHandle != TextureHandle)
6044 	{
6045 		OP_STATE_RENDER(1, ExecBufInstPtr);
6046 		STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
6047 		CurrTextureHandle = TextureHandle;
6048 	}
6049 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
6050 	if (NumVertices)
6051 	{
6052 	   WriteEndCodeToExecuteBuffer();
6053   	   UnlockExecuteBufferAndPrepareForUse();
6054 	   ExecuteBuffer();
6055   	   LockExecuteBuffer();
6056 	}
6057 	OP_STATE_RENDER(1, ExecBufInstPtr);
6058     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
6059 
6060 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
6061 	{
6062 		D3D_DrawMoltenMetalMesh_Unclipped();
6063 	}
6064 	else
6065 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
6066 	{
6067 		D3D_DrawMoltenMetalMesh_Clipped();
6068 	}
6069 
6070 	OP_STATE_RENDER(1, ExecBufInstPtr);
6071     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
6072 
6073 }
6074 
D3D_DrawMoltenMetal(int xOrigin,int yOrigin,int zOrigin)6075 void D3D_DrawMoltenMetal(int xOrigin, int yOrigin, int zOrigin)
6076 {
6077 	int i=0;
6078 	int x;
6079 	for (x=0; x<16; x++)
6080 	{
6081 		int z;
6082 		for(z=0; z<16; z++)
6083 		{
6084 			VECTORCH *point = &MeshVertex[i];
6085 
6086 			point->vx = xOrigin+(x*MeshXScale)/15;
6087 			point->vz = zOrigin+(z*MeshZScale)/15;
6088 		 #if 0
6089 
6090 			int offset=0;
6091 
6092 		 	offset = MUL_FIXED(32,GetSin(  (point->vx+point->vz+CloakingPhase)&4095 ) );
6093 		 	offset += MUL_FIXED(16,GetSin(  (point->vx-point->vz*2+CloakingPhase/2)&4095 ) );
6094 			{
6095 				float dx=point->vx-22704;
6096 				float dz=point->vz+20652;
6097 				float a = dx*dx+dz*dz;
6098 				a=sqrt(a);
6099 
6100 				offset+= MUL_FIXED(200,GetSin( (((int)a-CloakingPhase)&4095)  ));
6101 			}
6102 		#endif
6103 		 #if 1
6104 			int offset=0;
6105 
6106 			/* basic noise ripples */
6107 		 	offset = MUL_FIXED(128,GetSin(  ((point->vx+point->vz)/16+CloakingPhase)&4095 ) );
6108 		 	offset += MUL_FIXED(64,GetSin(  ((point->vx-point->vz*2)/4+CloakingPhase/2)&4095 ) );
6109 		 	offset += MUL_FIXED(64,GetSin(  ((point->vx*5-point->vz)/32+CloakingPhase/5)&4095 ) );
6110 
6111 		#endif
6112 			if (offset>450) offset = 450;
6113 			if (offset<-1000) offset = -1000;
6114 			point->vy = yOrigin+offset;
6115 
6116 			{
6117 				int shade = 191+(offset+256)/8;
6118 				MeshVertexColour[i] = RGBLIGHT_MAKE(shade,shade,shade);
6119 			}
6120 
6121 			#if 1
6122 			TranslatePointIntoViewspace(point);
6123 			#else
6124 			point->vx -= Global_VDB_Ptr->VDB_World.vx;
6125 			point->vy -= Global_VDB_Ptr->VDB_World.vy;
6126 			point->vz -= Global_VDB_Ptr->VDB_World.vz;
6127 			MeshWorldVertex[i] = *point;
6128 			RotateVector(point,&(Global_VDB_Ptr->VDB_Mat));
6129 			point->vy = MUL_FIXED(point->vy,87381);
6130 
6131 			#endif
6132 			/* is particle within normal view frustrum ? */
6133 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
6134 			{
6135 				if(( (-point->vx <= point->vz*2)
6136 		   			&&(point->vx <= point->vz*2)
6137 					&&(-point->vy <= point->vz*2)
6138 					&&(point->vy <= point->vz*2) ))
6139 				{
6140 					MeshVertexOutcode[i]=1;
6141 				}
6142 				else
6143 				{
6144 					MeshVertexOutcode[i]=0;
6145 				}
6146 			}
6147 			else
6148 			{
6149 				if(( (-point->vx <= point->vz)
6150 		   			&&(point->vx <= point->vz)
6151 					&&(-point->vy <= point->vz)
6152 					&&(point->vy <= point->vz) ))
6153 				{
6154 					MeshVertexOutcode[i]=1;
6155 				}
6156 				else
6157 				{
6158 					MeshVertexOutcode[i]=0;
6159 				}
6160 			}
6161 
6162 			#if 0
6163 			{
6164 				// v
6165 				MeshWorldVertex[i].vy = (offset+256)*4;
6166 				// u
6167 				MeshWorldVertex[i].vx = ((MeshWorldVertex[i].vx)&4095);
6168 
6169 			}
6170 			#else
6171 			{
6172 				Normalise(&MeshWorldVertex[i]);
6173 				// v
6174 				int theta = (MeshWorldVertex[i].vy+offset);
6175 				if (theta<0) theta=0;
6176 				if (theta>ONE_FIXED) theta=ONE_FIXED;
6177 
6178 				// u
6179 				int arctan = ((atan2((double)MeshWorldVertex[i].vx,(double)MeshWorldVertex[i].vz)/ 6.28318530718))*4095;
6180 				MeshWorldVertex[i].vx = (arctan+offset)&4095;
6181 
6182 				MeshWorldVertex[i].vy = ArcCos(theta);
6183 
6184 			}
6185 			#endif
6186 
6187 
6188 			i++;
6189 		}
6190 	}
6191 
6192 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[StaticImageNumber].D3DHandle;
6193 	if (CurrTextureHandle != TextureHandle)
6194 	{
6195 		OP_STATE_RENDER(1, ExecBufInstPtr);
6196 		STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
6197 		CurrTextureHandle = TextureHandle;
6198 	}
6199 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
6200 	if (NumVertices)
6201 	{
6202 	   WriteEndCodeToExecuteBuffer();
6203   	   UnlockExecuteBufferAndPrepareForUse();
6204 	   ExecuteBuffer();
6205   	   LockExecuteBuffer();
6206 	}
6207 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
6208 	{
6209 		D3D_DrawMoltenMetalMesh_Unclipped();
6210 	}
6211 	else
6212 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
6213 	{
6214 		D3D_DrawMoltenMetalMesh_Clipped();
6215 	}
6216 
6217 
6218 }
6219 
D3D_DrawMoltenMetalMesh_Unclipped(void)6220 void D3D_DrawMoltenMetalMesh_Unclipped(void)
6221 {
6222 	float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
6223 
6224 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
6225 	{
6226 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
6227 		VECTORCH *point = MeshVertex;
6228 
6229 		VECTORCH *pointWS = MeshWorldVertex;
6230 
6231 		int i;
6232 		for (i=0; i<256; i++)
6233 		{
6234 
6235 			if (point->vz<=1) point->vz = 1;
6236 			int x = (point->vx*(Global_VDB_Ptr->VDB_ProjX+1))/point->vz+Global_VDB_Ptr->VDB_CentreX;
6237 			int y = (point->vy*(Global_VDB_Ptr->VDB_ProjY+1))/point->vz+Global_VDB_Ptr->VDB_CentreY;
6238   //			textprint("%d, %d\n",x,y);
6239 			#if 1
6240 			{
6241 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
6242 				{
6243 					x=Global_VDB_Ptr->VDB_ClipLeft;
6244 				}
6245 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
6246 				{
6247 					x=Global_VDB_Ptr->VDB_ClipRight;
6248 				}
6249 
6250 				vertexPtr->sx=x;
6251 			}
6252 			{
6253 				if (y<Global_VDB_Ptr->VDB_ClipUp)
6254 				{
6255 					y=Global_VDB_Ptr->VDB_ClipUp;
6256 				}
6257 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
6258 				{
6259 					y=Global_VDB_Ptr->VDB_ClipDown;
6260 				}
6261 				vertexPtr->sy=y;
6262 			}
6263 			#else
6264 			vertexPtr->sx=x;
6265 			vertexPtr->sy=y;
6266 			#endif
6267 
6268 
6269 			point->vz+=HeadUpDisplayZOffset;
6270 		  	float oneOverZ = ((float)(point->vz)-ZNear)/(float)(point->vz);
6271 		   	vertexPtr->color = MeshVertexColour[i];
6272 			vertexPtr->specular = 0;
6273 			vertexPtr->sz = oneOverZ;
6274 
6275 			vertexPtr->tu = pointWS->vx*WaterUScale+(1.0f/256.0f);
6276 			vertexPtr->tv =	pointWS->vy*WaterVScale+(1.0f/256.0f);
6277 			vertexPtr->rhw = 1.0/point->vz;
6278 
6279 
6280 			NumVertices++;
6281 			vertexPtr++;
6282 			point++;
6283 
6284 			pointWS++;
6285 		}
6286 	}
6287  //	textprint("numvertices %d\n",NumVertices);
6288 
6289 
6290     /*
6291      * Make sure that the triangle data (not OP) will be QWORD aligned
6292      */
6293 	if (QWORD_ALIGNED(ExecBufInstPtr))
6294     {
6295         OP_NOP(ExecBufInstPtr);
6296     }
6297 
6298   	OP_TRIANGLE_LIST(450, ExecBufInstPtr);
6299 	/* CONSTRUCT POLYS */
6300 	{
6301 		int x;
6302 		for (x=0; x<15; x++)
6303 		{
6304 			int y;
6305 			for(y=0; y<15; y++)
6306 			{
6307 				OUTPUT_TRIANGLE(0+x+(16*y),1+x+(16*y),16+x+(16*y), 256);
6308 				OUTPUT_TRIANGLE(1+x+(16*y),17+x+(16*y),16+x+(16*y), 256);
6309 			}
6310 		}
6311 	}
6312 	#if 1
6313 	{
6314 	   WriteEndCodeToExecuteBuffer();
6315   	   UnlockExecuteBufferAndPrepareForUse();
6316 	   ExecuteBuffer();
6317   	   LockExecuteBuffer();
6318 	}
6319 	#endif
6320 }
D3D_DrawMoltenMetalMesh_Clipped(void)6321 void D3D_DrawMoltenMetalMesh_Clipped(void)
6322 {
6323 	float ZNear = (float) (Global_VDB_Ptr->VDB_ClipZ * GlobalScale);
6324 
6325 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
6326 	{
6327 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
6328 		VECTORCH *point = MeshVertex;
6329 
6330 		VECTORCH *pointWS = MeshWorldVertex;
6331 
6332 		int i;
6333 		for (i=0; i<256; i++)
6334 		{
6335 			{
6336 				int z = point->vz;
6337 				if (z<=0) z = 1;
6338 				int x = (point->vx*(Global_VDB_Ptr->VDB_ProjX+1))/z+Global_VDB_Ptr->VDB_CentreX;
6339 				int y = (point->vy*(Global_VDB_Ptr->VDB_ProjY+1))/z+Global_VDB_Ptr->VDB_CentreY;
6340 				#if 1
6341 				{
6342 					if (x<Global_VDB_Ptr->VDB_ClipLeft)
6343 					{
6344 						x=Global_VDB_Ptr->VDB_ClipLeft;
6345 					}
6346 					else if (x>Global_VDB_Ptr->VDB_ClipRight)
6347 					{
6348 						x=Global_VDB_Ptr->VDB_ClipRight;
6349 					}
6350 
6351 					vertexPtr->sx=x;
6352 				}
6353 				{
6354 					if (y<Global_VDB_Ptr->VDB_ClipUp)
6355 					{
6356 						y=Global_VDB_Ptr->VDB_ClipUp;
6357 					}
6358 					else if (y>Global_VDB_Ptr->VDB_ClipDown)
6359 					{
6360 						y=Global_VDB_Ptr->VDB_ClipDown;
6361 					}
6362 					vertexPtr->sy=y;
6363 				}
6364 				#else
6365 				vertexPtr->sx=x;
6366 				vertexPtr->sy=y;
6367 				#endif
6368 
6369 				vertexPtr->tu = pointWS->vx*WaterUScale+(1.0f/256.0f);
6370 				vertexPtr->tv =	pointWS->vy*WaterVScale+(1.0f/256.0f);
6371 
6372 				point->vz+=HeadUpDisplayZOffset;
6373 			  	float oneOverZ = ((float)(z)-ZNear)/(float)(z);
6374 				vertexPtr->color = MeshVertexColour[i];
6375 				vertexPtr->specular = 0;
6376 				vertexPtr->sz = oneOverZ;
6377 				vertexPtr->rhw = 1.0f/(float)z;
6378 
6379 			}
6380 			NumVertices++;
6381 			vertexPtr++;
6382 			point++;
6383 
6384 			pointWS++;
6385 		}
6386 	}
6387 //	textprint("numvertices %d\n",NumVertices);
6388 	/* CONSTRUCT POLYS */
6389 	{
6390 //		OP_TRIANGLE_LIST(450, ExecBufInstPtr);
6391 		int x;
6392 		for (x=0; x<15; x++)
6393 		{
6394 			int y;
6395 			for(y=0; y<15; y++)
6396 			{
6397 				int p1 = 0+x+(16*y);
6398 				int p2 = 1+x+(16*y);
6399 				int p3 = 16+x+(16*y);
6400 				int p4 = 17+x+(16*y);
6401 
6402 				#if 0
6403 				if (MeshVertexOutcode[p1]&&MeshVertexOutcode[p2]&&MeshVertexOutcode[p3])
6404 				{
6405 					OP_TRIANGLE_LIST(1, ExecBufInstPtr);
6406 					OUTPUT_TRIANGLE(p1,p2,p3, 256);
6407 				}
6408 				if (MeshVertexOutcode[p2]&&MeshVertexOutcode[p3]&&MeshVertexOutcode[p4])
6409 				{
6410 					OP_TRIANGLE_LIST(1, ExecBufInstPtr);
6411 					OUTPUT_TRIANGLE(p2,p4,p3, 256);
6412 				}
6413 				#else
6414 				if (MeshVertexOutcode[p1]&&MeshVertexOutcode[p2]&&MeshVertexOutcode[p3]&&MeshVertexOutcode[p4])
6415 				{
6416 					OP_TRIANGLE_LIST(2, ExecBufInstPtr);
6417 					OUTPUT_TRIANGLE(p1,p2,p3, 256);
6418 					OUTPUT_TRIANGLE(p2,p4,p3, 256);
6419 				}
6420 
6421 				#endif
6422 			}
6423 		}
6424 	}
6425 	#if 1
6426 	{
6427 	   WriteEndCodeToExecuteBuffer();
6428   	   UnlockExecuteBufferAndPrepareForUse();
6429 	   ExecuteBuffer();
6430   	   LockExecuteBuffer();
6431 	}
6432 	#endif
6433 	#if 1
6434 	{
6435 //		OP_TRIANGLE_LIST(450, ExecBufInstPtr);
6436 		POLYHEADER fakeHeader;
6437 
6438 		fakeHeader.PolyFlags = 0;
6439 		fakeHeader.PolyColour = 0;
6440 		RenderPolygon.TranslucencyMode = TRANSLUCENCY_NORMAL;
6441 		int x;
6442 		for (x=0; x<15; x++)
6443 		{
6444 			int y;
6445 			for(y=0; y<15; y++)
6446 			{
6447 				int p[4];
6448 				p[0] = 0+x+(16*y);
6449 				p[1] = 1+x+(16*y);
6450 				p[2] = 17+x+(16*y);
6451 				p[3] = 16+x+(16*y);
6452 
6453 				if (!(MeshVertexOutcode[p[0]]&&MeshVertexOutcode[p[1]]&&MeshVertexOutcode[p[2]]&&MeshVertexOutcode[p[3]]))
6454 				{
6455 				   {
6456 						int i;
6457 						for (i=0; i<4; i++)
6458 						{
6459 							VerticesBuffer[i].X	= MeshVertex[p[i]].vx;
6460 							VerticesBuffer[i].Y	= MeshVertex[p[i]].vy;
6461 							VerticesBuffer[i].Z	= MeshVertex[p[i]].vz;
6462 							VerticesBuffer[i].U = MeshWorldVertex[p[i]].vx*(WaterUScale*128.0f*65536.0f);
6463 							VerticesBuffer[i].V = MeshWorldVertex[p[i]].vy*(WaterVScale*128.0f*65536.0f);
6464 
6465 							VerticesBuffer[i].A = (MeshVertexColour[p[i]]&0xff000000)>>24;
6466 							VerticesBuffer[i].R = (MeshVertexColour[p[i]]&0x00ff0000)>>16;
6467 							VerticesBuffer[i].G	= (MeshVertexColour[p[i]]&0x0000ff00)>>8;
6468 							VerticesBuffer[i].B = MeshVertexColour[p[i]]&0x000000ff;
6469 							VerticesBuffer[i].SpecularR = 0;
6470 							VerticesBuffer[i].SpecularG = 0;
6471 							VerticesBuffer[i].SpecularB = 0;
6472 						}
6473 						RenderPolygon.NumberOfVertices=4;
6474 					}
6475 					{
6476 						int outcode = QuadWithinFrustrum();
6477 
6478 						if (outcode)
6479 						{
6480 							GouraudTexturedPolygon_ClipWithZ();
6481 							if(RenderPolygon.NumberOfVertices<3) continue;
6482 							GouraudTexturedPolygon_ClipWithNegativeX();
6483 							if(RenderPolygon.NumberOfVertices<3) continue;
6484 							GouraudTexturedPolygon_ClipWithPositiveY();
6485 							if(RenderPolygon.NumberOfVertices<3) continue;
6486 							GouraudTexturedPolygon_ClipWithNegativeY();
6487 							if(RenderPolygon.NumberOfVertices<3) continue;
6488 							GouraudTexturedPolygon_ClipWithPositiveX();
6489 							if(RenderPolygon.NumberOfVertices<3) continue;
6490 					   //	D3D_ZBufferedGouraudPolygon_Output(&fakeHeader,RenderPolygon.Vertices);
6491 					   		D3D_ZBufferedGouraudTexturedPolygon_Output(&fakeHeader,RenderPolygon.Vertices);
6492 						}
6493 					}
6494 				}
6495 			}
6496 		}
6497 	}
6498 	#endif
6499 	#if 1
6500 	{
6501 	   WriteEndCodeToExecuteBuffer();
6502   	   UnlockExecuteBufferAndPrepareForUse();
6503 	   ExecuteBuffer();
6504   	   LockExecuteBuffer();
6505 	}
6506 	#endif
6507 }
6508 
6509 void *DynamicImagePtr;
6510 
6511 #define NUMBER_OF_SMACK_SURFACES 4
6512 LPDIRECTDRAWSURFACE SrcDDSurface[NUMBER_OF_SMACK_SURFACES];
6513 LPDIRECT3DTEXTURE SrcTexture[NUMBER_OF_SMACK_SURFACES];
6514 void *SrcSurfacePtr[NUMBER_OF_SMACK_SURFACES];
6515 
6516 LPDIRECTDRAWSURFACE DstDDSurface[NUMBER_OF_SMACK_SURFACES]={0,0,0};
6517 LPDIRECT3DTEXTURE DstTexture[NUMBER_OF_SMACK_SURFACES]={0,0,0};
6518 int CurrentSurface;
6519 
InitDrawTest(void)6520 void InitDrawTest(void)
6521 {
6522 #if FMV_ON
6523 	DDSURFACEDESC ddsd;
6524 	int surface = NUMBER_OF_SMACK_SURFACES;
6525 
6526 	while(surface--)
6527 	{
6528 		memcpy(&ddsd, &(d3d.TextureFormat[d3d.CurrentTextureFormat].ddsd), sizeof(ddsd));
6529 
6530 		ddsd.dwSize = sizeof(ddsd);
6531 
6532 		ddsd.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
6533 			ddsd.ddsCaps.dwCaps = (DDSCAPS_SYSTEMMEMORY|DDSCAPS_TEXTURE);
6534 
6535 		ddsd.dwHeight = FMV_SIZE;
6536 		ddsd.dwWidth = FMV_SIZE;
6537 
6538 		LastError = lpDD->CreateSurface(&ddsd, &SrcDDSurface[surface], NULL);
6539 		LOGDXERR(LastError);
6540 
6541 		DDBLTFX ddbltfx;
6542 		memset(&ddbltfx, 0, sizeof(ddbltfx));
6543 		ddbltfx.dwSize = sizeof(ddbltfx);
6544 		ddbltfx.dwFillColor	= 0;// (2<<11)+(26<<5)+8;
6545 		LastError=SrcDDSurface[surface]->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
6546 		LOGDXERR(LastError);
6547 
6548 		LastError = SrcDDSurface[surface]->QueryInterface(IID_IDirect3DTexture, (LPVOID*) &SrcTexture[surface]);
6549 		LOGDXERR(LastError);
6550 
6551 
6552 		{
6553 			int PalCaps;
6554 
6555 			if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
6556 			{
6557 				PalCaps = (DDPCAPS_8BIT | DDPCAPS_ALLOW256);
6558 			}
6559 			else if (ddsd.ddpfPixelFormat.dwFlags &	DDPF_PALETTEINDEXED4)
6560 			{
6561 				PalCaps = DDPCAPS_4BIT;
6562 			}
6563 			else
6564 			{
6565 				PalCaps = 0;
6566 			}
6567 
6568 			if (PalCaps)
6569 			{
6570 				LPDIRECTDRAWPALETTE destPalette = NULL;
6571 				LastError = lpDD->CreatePalette(PalCaps, FMVPalette[surface], &destPalette, NULL);
6572 				LOGDXERR(LastError);
6573 
6574 				LastError = SrcDDSurface[surface]->SetPalette(destPalette);
6575 //				LastError = tempSurface->SetPalette(destPalette);
6576 				LOGDXERR(LastError);
6577 			}
6578 		}
6579 
6580 
6581 
6582 		memset(&ddsd, 0, sizeof(DDSURFACEDESC));
6583 		ddsd.dwSize = sizeof(DDSURFACEDESC);
6584 		LastError = SrcDDSurface[surface]->Lock(NULL,&ddsd,0,NULL);
6585 		LOGDXERR(LastError);
6586 
6587 		SrcSurfacePtr[surface] = (void*)ddsd.lpSurface;
6588 
6589 	   	LastError = SrcDDSurface[surface]->Unlock(NULL);
6590 		LOGDXERR(LastError);
6591 	}
6592 	CurrentSurface=0;
6593 
6594 	{
6595 		extern void InitFMV(void);
6596 		InitFMV();
6597 	}
6598 
6599 	FMVTextureHandle[0]=0;
6600 	FMVTextureHandle[1]=0;
6601 	FMVTextureHandle[2]=0;
6602 #endif
6603 
6604 }
6605 
D3D_DrawFMV(int xOrigin,int yOrigin,int zOrigin)6606 void D3D_DrawFMV(int xOrigin, int yOrigin, int zOrigin)
6607 {
6608 	MeshXScale = 4096/8/3;
6609 	MeshZScale = 4096/8/3;
6610 	OP_STATE_RENDER(1, ExecBufInstPtr);
6611     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
6612 
6613 	for (int field=0; field<3; field++)
6614 	{
6615 	for (int zfield=0; zfield<3; zfield++)
6616 	{
6617 	int i=0;
6618 	int x;
6619 
6620 	for (x=(0+field*15); x<(16+field*15); x++)
6621 	{
6622 		int z;
6623 		for(z=0+zfield*15; z<16+zfield*15; z++)
6624 		{
6625 			VECTORCH *point = &MeshVertex[i];
6626 
6627 		 	point->vx = xOrigin+(x*MeshXScale);
6628 		 	point->vy = yOrigin+(z*MeshZScale);
6629 
6630 			int offset;
6631 		 	offset = MUL_FIXED(128,GetSin(  ((point->vx+point->vy)+CloakingPhase)&4095 ) );
6632 		 	offset += MUL_FIXED(64,GetSin(  ((point->vx-point->vy*2)+CloakingPhase/2)&4095 ) );
6633 		 	offset += MUL_FIXED(64,GetSin(  ((point->vx*5-point->vy)/2+CloakingPhase/5)&4095 ) );
6634 
6635 		 	point->vz = zOrigin+offset;
6636 
6637 			{
6638 				int shade = 127+(offset+256)/4;
6639 				MeshVertexColour[i] = RGBALIGHT_MAKE(shade,shade,shade,192);
6640 			}
6641 			#if 0
6642 			{
6643 				int sd;
6644 				int size=65536;
6645 				int theta = ((z*4096)/45)&4095;
6646 				int phi = ((x*2048)/45)&4095;
6647 				if (theta==0) sd = 0;
6648 				else sd = phi+theta;
6649 				if (phi==0 || phi==2048) sd = 0;
6650 				else sd = phi+theta;
6651 
6652 				int outerRadius = MUL_FIXED(4000+offset,size);
6653 
6654 				if (sd)
6655 				offset = MUL_FIXED((FastRandom()&511)-255,size);
6656 
6657 				offset=0;
6658 
6659 				point->vx = xOrigin+2000+MUL_FIXED(outerRadius+offset,MUL_FIXED(GetSin(theta),GetCos(phi)));
6660 				point->vy = yOrigin+MUL_FIXED(outerRadius+offset,MUL_FIXED(GetSin(theta),GetSin(phi)));
6661 				point->vz = zOrigin+MUL_FIXED(outerRadius+offset,GetCos(theta));
6662 			}
6663 	  		#endif
6664 			MeshWorldVertex[i].vx = (x*4096/45);
6665 			MeshWorldVertex[i].vy = (z*2048/45);
6666 
6667 			TranslatePointIntoViewspace(point);
6668 
6669 			/* is particle within normal view frustrum ? */
6670 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
6671 			{
6672 				if(( (-point->vx <= point->vz*2)
6673 		   			&&(point->vx <= point->vz*2)
6674 					&&(-point->vy <= point->vz*2)
6675 					&&(point->vy <= point->vz*2) ))
6676 				{
6677 					MeshVertexOutcode[i]=1;
6678 				}
6679 				else
6680 				{
6681 					MeshVertexOutcode[i]=0;
6682 				}
6683 			}
6684 			else
6685 			{
6686 				if(( (-point->vx <= point->vz)
6687 		   			&&(point->vx <= point->vz)
6688 					&&(-point->vy <= point->vz)
6689 					&&(point->vy <= point->vz) ))
6690 				{
6691 					MeshVertexOutcode[i]=1;
6692 				}
6693 				else
6694 				{
6695 					MeshVertexOutcode[i]=0;
6696 				}
6697 			}
6698 
6699 			i++;
6700 		}
6701 	}
6702 	//textprint("\n");
6703 	D3DTEXTUREHANDLE TextureHandle = FMVTextureHandle[0];
6704 	if (CurrTextureHandle != TextureHandle)
6705 	{
6706 		OP_STATE_RENDER(1, ExecBufInstPtr);
6707 		STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
6708 		CurrTextureHandle = TextureHandle;
6709 	}
6710 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);//GLOWING);
6711 	if (NumVertices)
6712 	{
6713 	   WriteEndCodeToExecuteBuffer();
6714   	   UnlockExecuteBufferAndPrepareForUse();
6715 	   ExecuteBuffer();
6716   	   LockExecuteBuffer();
6717 	}
6718 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
6719 	{
6720 		D3D_DrawMoltenMetalMesh_Unclipped();
6721 	}
6722 	else
6723 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
6724 	{
6725 		D3D_DrawMoltenMetalMesh_Clipped();
6726 	}
6727 }	}
6728 
6729 	OP_STATE_RENDER(1, ExecBufInstPtr);
6730     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
6731 }
6732 
6733 
RenderFMVParticle(int t,int o,VECTORCH * offsetPtr)6734 void RenderFMVParticle(int t, int o, VECTORCH *offsetPtr)
6735 {
6736 	{
6737 		VECTORCH translatedPosition = MeshVertex[o+t];
6738 		translatedPosition.vx += offsetPtr->vx;
6739 		translatedPosition.vy += offsetPtr->vy;
6740 		translatedPosition.vz += offsetPtr->vz;
6741 		TranslatePointIntoViewspace(&translatedPosition);
6742 		VerticesBuffer[0].X = translatedPosition.vx;
6743 		VerticesBuffer[0].Y = translatedPosition.vy;
6744 		VerticesBuffer[0].Z = translatedPosition.vz;
6745 	}
6746 	{
6747 		VECTORCH translatedPosition = MeshVertex[1+t];
6748 		translatedPosition.vx += offsetPtr->vx;
6749 		translatedPosition.vy += offsetPtr->vy;
6750 		translatedPosition.vz += offsetPtr->vz;
6751 		TranslatePointIntoViewspace(&translatedPosition);
6752 		VerticesBuffer[1].X = translatedPosition.vx;
6753 		VerticesBuffer[1].Y = translatedPosition.vy;
6754 		VerticesBuffer[1].Z = translatedPosition.vz;
6755 	}
6756 	{
6757 		VECTORCH translatedPosition = MeshVertex[16+t];
6758 		translatedPosition.vx += offsetPtr->vx;
6759 		translatedPosition.vy += offsetPtr->vy;
6760 		translatedPosition.vz += offsetPtr->vz;
6761 		TranslatePointIntoViewspace(&translatedPosition);
6762 		VerticesBuffer[2].X = translatedPosition.vx;
6763 		VerticesBuffer[2].Y = translatedPosition.vy;
6764 		VerticesBuffer[2].Z = translatedPosition.vz;
6765 	}
6766 	{
6767 		int outcode = TriangleWithinFrustrum();
6768 
6769 		if (outcode)
6770 		{
6771 			/* setup */
6772 			RenderPolygon.NumberOfVertices=3;
6773 
6774 			VerticesBuffer[0].U = MeshWorldVertex[o+t].vx;
6775 			VerticesBuffer[0].V = MeshWorldVertex[o+t].vy;
6776 			VerticesBuffer[0].A = 192;
6777 
6778 			VerticesBuffer[1].U = MeshWorldVertex[1+t].vx;
6779 			VerticesBuffer[1].V = MeshWorldVertex[1+t].vy;
6780 			VerticesBuffer[1].A = 192;
6781 
6782 			VerticesBuffer[2].U = MeshWorldVertex[16+t].vx;
6783 			VerticesBuffer[2].V = MeshWorldVertex[16+t].vy;
6784 			VerticesBuffer[2].A = 192;
6785 
6786 
6787 			if (outcode!=2)
6788 			{
6789 				TexturedPolygon_ClipWithZ();
6790 				if(RenderPolygon.NumberOfVertices<3) return;
6791 				TexturedPolygon_ClipWithNegativeX();
6792 				if(RenderPolygon.NumberOfVertices<3) return;
6793 				TexturedPolygon_ClipWithPositiveY();
6794 				if(RenderPolygon.NumberOfVertices<3) return;
6795 				TexturedPolygon_ClipWithNegativeY();
6796 				if(RenderPolygon.NumberOfVertices<3) return;
6797 				TexturedPolygon_ClipWithPositiveX();
6798 				if(RenderPolygon.NumberOfVertices<3) return;
6799 				D3D_FMVParticle_Output(RenderPolygon.Vertices);
6800   			}
6801 			else D3D_FMVParticle_Output(VerticesBuffer);
6802 		}
6803 	}
6804 }
6805 
6806 
D3D_DrawSwirlyFMV(int xOrigin,int yOrigin,int zOrigin)6807 void D3D_DrawSwirlyFMV(int xOrigin, int yOrigin, int zOrigin)
6808 {
6809 	MeshXScale = 2048/8;
6810 	MeshZScale = (2048*3)/8/4;
6811 	OP_STATE_RENDER(1, ExecBufInstPtr);
6812     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
6813 
6814 	int i=0;
6815 	int x;
6816 
6817 	for (x=0; x<16; x++)
6818 	{
6819 		int z;
6820 		for(z=0; z<16; z++)
6821 		{
6822 			VECTORCH *point = &MeshVertex[i];
6823 
6824 		 	point->vz = zOrigin+(x*MeshXScale);
6825 		 	point->vy = yOrigin+(z*MeshZScale);
6826 
6827 			int offset;
6828 		 	offset = MUL_FIXED(128,GetSin(  ((point->vz+point->vy)+CloakingPhase)&4095 ) );
6829 		 	offset += MUL_FIXED(64,GetSin(  ((point->vz-point->vy*2)+CloakingPhase/2)&4095 ) );
6830 		 	offset += MUL_FIXED(64,GetSin(  ((point->vz*5-point->vy)/2+CloakingPhase/5)&4095 ) );
6831 
6832 		 	point->vx = xOrigin+offset;
6833 
6834 	   		MeshWorldVertex[i].vx = ((15-x)*65536*128)/15;
6835 			MeshWorldVertex[i].vy = (z*49152*128)/15;
6836 
6837 
6838    			i++;
6839 		}
6840 	}
6841 
6842 	for (x=0; x<15; x++)
6843 	{
6844 		int y;
6845 		for(y=0; y<15; y++)
6846 		{
6847 			int a = (x+y*15)*2;
6848 			int b = 1;
6849 			int o = x+y*16;
6850 			int t = 0;
6851 			do
6852 			{
6853 				int p = a+b;
6854 
6855 		   		#if 1
6856 		   		RenderFMVParticle(o,t,&FMVParticlePosition[p]);
6857 				#else
6858 				{
6859 					PARTICLE particle;
6860 					particle.ParticleID = PARTICLE_NONCOLLIDINGFLAME;
6861 					particle.Position = FMVParticleAbsPosition[p];
6862 					particle.Colour = RGBA_MAKE(255,255,255,16);
6863 					particle.Size = 3000;
6864 					RenderParticle(&particle);
6865 				}
6866 				#endif
6867 
6868 				FMVParticleAbsPosition[p] = MeshVertex[o+t];
6869 				FMVParticleAbsPosition[p].vx += FMVParticlePosition[p].vx;
6870 				FMVParticleAbsPosition[p].vy += FMVParticlePosition[p].vy;
6871 				FMVParticleAbsPosition[p].vz += FMVParticlePosition[p].vz;
6872 
6873    				t=17;
6874 			}
6875 			while(b--);
6876 		}
6877 	}
6878 	for(int p=0; p<450; p++)
6879 	{
6880 		#if 0
6881 		if ((CloakingPhase&32767) > 16383)
6882 		{
6883 			int n=p+52;
6884 			int o=p+76;
6885 			if (n>449) n-=449;
6886 			if (o>449) o-=449;
6887 
6888 			FMVParticlePosition[p].vx -= MUL_FIXED(FMVParticleAbsPosition[p].vx - FMVParticleAbsPosition[n].vx,NormalFrameTime*8);
6889 			FMVParticlePosition[p].vy -= MUL_FIXED(FMVParticleAbsPosition[p].vy - FMVParticleAbsPosition[n].vy,NormalFrameTime*8);
6890 			FMVParticlePosition[p].vz -= MUL_FIXED(FMVParticleAbsPosition[p].vz - FMVParticleAbsPosition[n].vz,NormalFrameTime*8);
6891 			FMVParticlePosition[p].vx += MUL_FIXED(FMVParticleAbsPosition[p].vx - FMVParticleAbsPosition[o].vx,NormalFrameTime/4);
6892 			FMVParticlePosition[p].vy += MUL_FIXED(FMVParticleAbsPosition[p].vy - FMVParticleAbsPosition[o].vy,NormalFrameTime/4);
6893 			FMVParticlePosition[p].vz += MUL_FIXED(FMVParticleAbsPosition[p].vz - FMVParticleAbsPosition[o].vz,NormalFrameTime/4);
6894 
6895 		}
6896 		else
6897 		{
6898 
6899 			FMVParticlePosition[p].vx -= MUL_FIXED(FMVParticlePosition[p].vx,NormalFrameTime*4);
6900 			FMVParticlePosition[p].vy -= MUL_FIXED(FMVParticlePosition[p].vy,NormalFrameTime*4);
6901 			FMVParticlePosition[p].vz -= MUL_FIXED(FMVParticlePosition[p].vz,NormalFrameTime*4);
6902 
6903 			int m = Approximate3dMagnitude(&FMVParticlePosition[p]);
6904 
6905 			if (m<=20)
6906 			{
6907 				FMVParticlePosition[p].vx = 0;
6908 				FMVParticlePosition[p].vy = 0;
6909 				FMVParticlePosition[p].vz = 0;
6910 			}
6911 
6912 		}
6913 		#else
6914 				FMVParticlePosition[p].vx = 0;
6915 				FMVParticlePosition[p].vy = 0;
6916 				FMVParticlePosition[p].vz = 0;
6917 		#endif
6918 	}
6919 
6920 	OP_STATE_RENDER(1, ExecBufInstPtr);
6921     STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
6922 }
6923 
UpdateFMVTextures(int maxTextureNumberToUpdate)6924 static void UpdateFMVTextures(int maxTextureNumberToUpdate)
6925 {
6926 #if FMV_ON
6927 	int surfaceNumber=maxTextureNumberToUpdate;
6928 	while(surfaceNumber--)
6929 	{
6930 		DDSURFACEDESC ddsd;
6931 
6932 
6933 		memset(&ddsd, 0, sizeof(DDSURFACEDESC));
6934 		ddsd.dwSize = sizeof(DDSURFACEDESC);
6935 		LastError = SrcDDSurface[surfaceNumber]->Lock(NULL,&ddsd,DDLOCK_WAIT,NULL);
6936 		LOGDXERR(LastError);
6937 
6938 		{
6939 			if (!NextFMVFrame(SrcSurfacePtr[surfaceNumber],0,0,128,96, surfaceNumber))
6940 			{
6941 		    	LastError = SrcDDSurface[surfaceNumber]->Unlock(NULL);
6942 				LOGDXERR(LastError);
6943 			 	return;
6944 			}
6945 	  	}
6946 
6947 	    LastError = SrcDDSurface[surfaceNumber]->Unlock(NULL);
6948 		LOGDXERR(LastError);
6949 
6950 		if (DstDDSurface[surfaceNumber]) ReleaseDDSurface(DstDDSurface[surfaceNumber]);
6951 		if (DstTexture[surfaceNumber]) ReleaseD3DTexture(DstTexture[surfaceNumber]);
6952 
6953 		// Query destination surface for a texture interface.
6954 		memset(&ddsd, 0, sizeof(DDSURFACEDESC));
6955 		ddsd.dwSize = sizeof(DDSURFACEDESC);
6956 
6957 		LastError = SrcDDSurface[surfaceNumber]->GetSurfaceDesc(&ddsd);
6958 		LOGDXERR(LastError);
6959 
6960 		ddsd.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
6961 		ddsd.ddsCaps.dwCaps = (DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD );
6962 
6963 		LastError = lpDD->CreateSurface(&ddsd, &DstDDSurface[surfaceNumber], NULL);
6964 		LOGDXERR(LastError);
6965 		{
6966 			int PalCaps;
6967 
6968 			if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
6969 			{
6970 				PalCaps = (DDPCAPS_8BIT | DDPCAPS_ALLOW256);
6971 			}
6972 			else if (ddsd.ddpfPixelFormat.dwFlags &	DDPF_PALETTEINDEXED4)
6973 			{
6974 				PalCaps = DDPCAPS_4BIT;
6975 			}
6976 			else
6977 			{
6978 				PalCaps = 0;
6979 			}
6980 
6981 			if (PalCaps)
6982 			{
6983 				LPDIRECTDRAWPALETTE destPalette = NULL;
6984 
6985 				LastError = lpDD->CreatePalette(PalCaps, FMVPalette[surfaceNumber], &destPalette, NULL);
6986 				LOGDXERR(LastError);
6987 
6988 				UpdateFMVPalette(FMVPalette[surfaceNumber],surfaceNumber);
6989 				LastError = DstDDSurface[surfaceNumber]->SetPalette(destPalette);
6990 				LOGDXERR(LastError);
6991 				LastError = SrcDDSurface[surfaceNumber]->SetPalette(destPalette);
6992 				LOGDXERR(LastError);
6993 
6994 			}
6995 		}
6996 		LastError = DstDDSurface[surfaceNumber]->QueryInterface(IID_IDirect3DTexture,(LPVOID*) &DstTexture[surfaceNumber]);
6997 		LOGDXERR(LastError);
6998 
6999 		LastError = DstTexture[surfaceNumber]->Load(SrcTexture[surfaceNumber]);
7000 	 	LOGDXERR(LastError);
7001 
7002 		LastError = DstTexture[surfaceNumber]->GetHandle(d3d.lpD3DDevice, &FMVTextureHandle[surfaceNumber]);
7003 		LOGDXERR(LastError);
7004 	}
7005 #endif
7006 }
7007 
7008 
KillFMVTexture(void)7009 void KillFMVTexture(void)
7010 {
7011 	int surface = NUMBER_OF_SMACK_SURFACES;
7012 
7013 	while(surface--)
7014 	{
7015 		RELEASE(SrcDDSurface[surface]);
7016 		RELEASE(SrcTexture[surface]);
7017 		RELEASE(DstDDSurface[surface]);
7018 		RELEASE(DstTexture[surface]);
7019 	}
7020 
7021 }
7022 
7023 
ThisFramesRenderingHasBegun(void)7024 void ThisFramesRenderingHasBegun(void)
7025 {
7026 		if (ScanDrawMode != ScanDrawDirectDraw)
7027 		{
7028 			BeginD3DScene();
7029 			LockExecuteBuffer();
7030 			D3D_SetupSceneDefaults();
7031 		}
7032 		else
7033 		{
7034 			LockSurfaceAndGetBufferPointer();
7035 		}
7036 }
7037 
ThisFramesRenderingHasFinished(void)7038 void ThisFramesRenderingHasFinished(void)
7039 {
7040 	if (ScanDrawMode != ScanDrawDirectDraw)
7041 	{
7042 		WriteEndCodeToExecuteBuffer();
7043 		UnlockExecuteBufferAndPrepareForUse();
7044 		ExecuteBuffer();
7045 		EndD3DScene();
7046 	}
7047 
7048  	/* KJL 11:46:56 01/16/97 - kill off any lights which are fated to be removed */
7049 	LightBlockDeallocation();
7050 
7051 }
D3D_DrawWaterOctagonPatch(int xOrigin,int yOrigin,int zOrigin,int xOffset,int zOffset)7052 void D3D_DrawWaterOctagonPatch(int xOrigin, int yOrigin, int zOrigin, int xOffset, int zOffset)
7053 {
7054 	float grad = 2.414213562373;
7055 	int i=0;
7056 	int x;
7057 	for (x=xOffset; x<16+xOffset; x++)
7058 	{
7059 		int z;
7060 		for(z=zOffset; z<16+zOffset; z++)
7061 		{
7062 			VECTORCH *point = &MeshVertex[i];
7063 
7064 		  	if (x>z)
7065 			{
7066 				float m,xs;
7067 				if (x!=0)
7068 				{
7069 					m = (float)(z)/(float)(x);
7070 					xs = grad/(grad+m);
7071 				}
7072 				else
7073 				{
7074 					xs = 0;
7075 				}
7076 				#if 1
7077 				f2i(point->vx , xs*x*MeshXScale);
7078 				f2i(point->vz , (grad-grad*xs)*x*MeshZScale);
7079 				#else
7080 				point->vx = xs*x*MeshXScale;
7081 				point->vz = (grad-grad*xs)*x*MeshZScale;
7082 				#endif
7083 			}
7084 			else
7085 			{
7086 				float m,xs;
7087 				if (z!=0)
7088 				{
7089 					m = (float)(x)/(float)(z);
7090 					xs = grad/(grad+m);
7091 				}
7092 				else
7093 				{
7094 					xs = 0;
7095 				}
7096 				#if 1
7097 				f2i(point->vz ,	xs*z*MeshZScale);
7098 				f2i(point->vx ,	(grad-grad*xs)*z*MeshXScale);
7099 				#else
7100 				point->vz =	xs*z*MeshZScale;
7101 				point->vx =	(grad-grad*xs)*z*MeshXScale;
7102 				#endif
7103 			}
7104 
7105 			point->vx += xOrigin;
7106 			point->vz += zOrigin;
7107 
7108 			int offset = EffectOfRipples(point);
7109 
7110 			point->vy = yOrigin+offset;
7111 
7112 			#if 0
7113 			MeshVertexColour[i] = LightSourceWaterPoint(point,offset);
7114 			#else
7115 			{
7116 				int alpha = 128-offset/4;
7117 		//		if (alpha>255) alpha = 255;
7118 		//		if (alpha<128) alpha = 128;
7119 				switch (CurrentVisionMode)
7120 				{
7121 					default:
7122 					case VISION_MODE_NORMAL:
7123 					{
7124 						MeshVertexColour[i] = RGBALIGHT_MAKE(10,51,28,alpha);
7125 						break;
7126 					}
7127 					case VISION_MODE_IMAGEINTENSIFIER:
7128 					{
7129 						MeshVertexColour[i] = RGBALIGHT_MAKE(0,51,0,alpha);
7130 						break;
7131 					}
7132 					case VISION_MODE_PRED_THERMAL:
7133 					case VISION_MODE_PRED_SEEALIENS:
7134 					case VISION_MODE_PRED_SEEPREDTECH:
7135 					{
7136 						MeshVertexColour[i] = RGBALIGHT_MAKE(0,0,28,alpha);
7137 					  	break;
7138 					}
7139 				}
7140 
7141 			}
7142 			#endif
7143 			TranslatePointIntoViewspace(point);
7144 			/* is particle within normal view frustrum ? */
7145 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
7146 			{
7147 				if(( (-point->vx <= point->vz*2)
7148 		   			&&(point->vx <= point->vz*2)
7149 					&&(-point->vy <= point->vz*2)
7150 					&&(point->vy <= point->vz*2) ))
7151 				{
7152 					MeshVertexOutcode[i]=1;
7153 				}
7154 				else
7155 				{
7156 					MeshVertexOutcode[i]=0;
7157 				}
7158 			}
7159 			else
7160 			{
7161 				if(( (-point->vx <= point->vz)
7162 		   			&&(point->vx <= point->vz)
7163 					&&(-point->vy <= point->vz)
7164 					&&(point->vy <= point->vz) ))
7165 				{
7166 					MeshVertexOutcode[i]=1;
7167 				}
7168 				else
7169 				{
7170 					MeshVertexOutcode[i]=0;
7171 				}
7172 			}
7173 
7174 			i++;
7175 		}
7176 	}
7177 
7178 	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
7179 	{
7180 		D3D_DrawWaterMesh_Unclipped();
7181 	}
7182 	else
7183 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
7184 	{
7185 		D3D_DrawWaterMesh_Clipped();
7186 	}
7187 
7188 
7189 }
7190 
7191 
D3D_DrawSliderBar(int x,int y,int alpha)7192 extern void D3D_DrawSliderBar(int x, int y, int alpha)
7193 {
7194 	struct VertexTag quadVertices[4];
7195 	int sliderHeight = 11;
7196 	unsigned int colour = alpha>>8;
7197 
7198 	if (colour>255) colour = 255;
7199 	colour = (colour<<24)+0xffffff;
7200 
7201 	quadVertices[0].Y = y;
7202 	quadVertices[1].Y = y;
7203 	quadVertices[2].Y = y + sliderHeight;
7204 	quadVertices[3].Y = y + sliderHeight;
7205 
7206 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
7207 	{
7208 		int topLeftU = 1;
7209 		int topLeftV = 68;
7210 
7211 		quadVertices[0].U = topLeftU;
7212 		quadVertices[0].V = topLeftV;
7213 		quadVertices[1].U = topLeftU + 2;
7214 		quadVertices[1].V = topLeftV;
7215 		quadVertices[2].U = topLeftU + 2;
7216 		quadVertices[2].V = topLeftV + sliderHeight;
7217 		quadVertices[3].U = topLeftU;
7218 		quadVertices[3].V = topLeftV + sliderHeight;
7219 
7220 		quadVertices[0].X = x;
7221 		quadVertices[3].X = x;
7222 		quadVertices[1].X = x + 2;
7223 		quadVertices[2].X = x + 2;
7224 
7225 		D3D_HUDQuad_Output
7226 		(
7227 			HUDFontsImageNumber,
7228 			quadVertices,
7229 			colour
7230 		);
7231 	}
7232 	{
7233 		int topLeftU = 7;
7234 		int topLeftV = 68;
7235 
7236 		quadVertices[0].U = topLeftU;
7237 		quadVertices[0].V = topLeftV;
7238 		quadVertices[1].U = topLeftU + 2;
7239 		quadVertices[1].V = topLeftV;
7240 		quadVertices[2].U = topLeftU + 2;
7241 		quadVertices[2].V = topLeftV + sliderHeight;
7242 		quadVertices[3].U = topLeftU;
7243 		quadVertices[3].V = topLeftV + sliderHeight;
7244 
7245 		quadVertices[0].X = x+213+2;
7246 		quadVertices[3].X = x+213+2;
7247 		quadVertices[1].X = x+2 +213+2;
7248 		quadVertices[2].X = x+2 +213+2;
7249 
7250 		D3D_HUDQuad_Output
7251 		(
7252 			HUDFontsImageNumber,
7253 			quadVertices,
7254 			colour
7255 		);
7256 	}
7257 	quadVertices[2].Y = y + 2;
7258 	quadVertices[3].Y = y + 2;
7259 
7260 	{
7261 		int topLeftU = 5;
7262 		int topLeftV = 77;
7263 
7264 		quadVertices[0].U = topLeftU;
7265 		quadVertices[0].V = topLeftV;
7266 		quadVertices[1].U = topLeftU;
7267 		quadVertices[1].V = topLeftV;
7268 		quadVertices[2].U = topLeftU;
7269 		quadVertices[2].V = topLeftV + 2;
7270 		quadVertices[3].U = topLeftU;
7271 		quadVertices[3].V = topLeftV + 2;
7272 
7273 		quadVertices[0].X = x + 2;
7274 		quadVertices[3].X = x + 2;
7275 		quadVertices[1].X = x + 215;
7276 		quadVertices[2].X = x + 215;
7277 
7278 		D3D_HUDQuad_Output
7279 		(
7280 			HUDFontsImageNumber,
7281 			quadVertices,
7282 			colour
7283 		);
7284 	}
7285 	quadVertices[0].Y = y + 9;
7286 	quadVertices[1].Y = y + 9;
7287 	quadVertices[2].Y = y + 11;
7288 	quadVertices[3].Y = y + 11;
7289 
7290 	{
7291 		int topLeftU = 5;
7292 		int topLeftV = 77;
7293 
7294 		quadVertices[0].U = topLeftU;
7295 		quadVertices[0].V = topLeftV;
7296 		quadVertices[1].U = topLeftU;
7297 		quadVertices[1].V = topLeftV;
7298 		quadVertices[2].U = topLeftU;
7299 		quadVertices[2].V = topLeftV + 2;
7300 		quadVertices[3].U = topLeftU;
7301 		quadVertices[3].V = topLeftV + 2;
7302 
7303 		quadVertices[0].X = x + 2;
7304 		quadVertices[3].X = x + 2;
7305 		quadVertices[1].X = x + 215;
7306 		quadVertices[2].X = x + 215;
7307 
7308 		D3D_HUDQuad_Output
7309 		(
7310 			HUDFontsImageNumber,
7311 			quadVertices,
7312 			colour
7313 		);
7314 	}
7315 
7316 
7317 
7318 }
7319 
D3D_DrawSlider(int x,int y,int alpha)7320 extern void D3D_DrawSlider(int x, int y, int alpha)
7321 {
7322 	struct VertexTag quadVertices[4];
7323 	int sliderHeight = 5;
7324 	unsigned int colour = alpha>>8;
7325 
7326 	if (colour>255) colour = 255;
7327 	colour = (colour<<24)+0xffffff;
7328 
7329 	quadVertices[0].Y = y;
7330 	quadVertices[1].Y = y;
7331 	quadVertices[2].Y = y + sliderHeight;
7332 	quadVertices[3].Y = y + sliderHeight;
7333 
7334 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
7335 	{
7336 		int topLeftU = 11;
7337 		int topLeftV = 74;
7338 
7339 		quadVertices[0].U = topLeftU;
7340 		quadVertices[0].V = topLeftV;
7341 		quadVertices[1].U = topLeftU + 9;
7342 		quadVertices[1].V = topLeftV;
7343 		quadVertices[2].U = topLeftU + 9;
7344 		quadVertices[2].V = topLeftV + sliderHeight;
7345 		quadVertices[3].U = topLeftU;
7346 		quadVertices[3].V = topLeftV + sliderHeight;
7347 
7348 		quadVertices[0].X = x;
7349 		quadVertices[3].X = x;
7350 		quadVertices[1].X = x + 9;
7351 		quadVertices[2].X = x + 9;
7352 
7353 		D3D_HUDQuad_Output
7354 		(
7355 			HUDFontsImageNumber,
7356 			quadVertices,
7357 			colour
7358 		);
7359 	}
7360 }
7361 
DrawFontTest(void)7362 void DrawFontTest(void)
7363 {
7364 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
7365 
7366 	struct VertexTag quadVertices[4];
7367 
7368 	quadVertices[0].Y = 0;
7369 	quadVertices[1].Y = 0;
7370 	quadVertices[2].Y = 256;
7371 	quadVertices[3].Y = 256;
7372 
7373 	quadVertices[0].U = 0;
7374 	quadVertices[0].V = 0;
7375 	quadVertices[1].U = 256;
7376 	quadVertices[1].V = 0;
7377 	quadVertices[2].U = 256;
7378 	quadVertices[2].V = 256;
7379 	quadVertices[3].U = 0;
7380 	quadVertices[3].V = 256;
7381 
7382 	quadVertices[0].X = 0;
7383 	quadVertices[3].X = 0;
7384 	quadVertices[1].X = 256;
7385 	quadVertices[2].X = 256;
7386 
7387 	D3D_HUDQuad_Output
7388 	(
7389 		AAFontImageNumber,
7390 		quadVertices,
7391 		0xffffffff
7392 	);
7393 }
D3D_DrawRectangle(int x,int y,int w,int h,int alpha)7394 extern void D3D_DrawRectangle(int x, int y, int w, int h, int alpha)
7395 {
7396 	struct VertexTag quadVertices[4];
7397 	int sliderHeight = 11;
7398 	unsigned int colour = alpha>>8;
7399 
7400 	if (colour>255) colour = 255;
7401 	colour = (colour<<24)+0xffffff;
7402 
7403 	quadVertices[0].Y = y;
7404 	quadVertices[1].Y = y;
7405 	quadVertices[2].Y = y + 6;
7406 	quadVertices[3].Y = y + 6;
7407 
7408 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
7409 	/* top left corner */
7410 	{
7411 		int topLeftU = 1;
7412 		int topLeftV = 238;
7413 
7414 		quadVertices[0].U = topLeftU;
7415 		quadVertices[0].V = topLeftV;
7416 		quadVertices[1].U = topLeftU + 6;
7417 		quadVertices[1].V = topLeftV;
7418 		quadVertices[2].U = topLeftU + 6;
7419 		quadVertices[2].V = topLeftV + 6;
7420 		quadVertices[3].U = topLeftU;
7421 		quadVertices[3].V = topLeftV + 6;
7422 
7423 		quadVertices[0].X = x;
7424 		quadVertices[3].X = x;
7425 		quadVertices[1].X = x + 6;
7426 		quadVertices[2].X = x + 6;
7427 
7428 		D3D_HUDQuad_Output
7429 		(
7430 			AAFontImageNumber,
7431 			quadVertices,
7432 			colour
7433 		);
7434 	}
7435 	/* top */
7436 	{
7437 		int topLeftU = 9;
7438 		int topLeftV = 238;
7439 
7440 		quadVertices[0].U = topLeftU;
7441 		quadVertices[0].V = topLeftV;
7442 		quadVertices[1].U = topLeftU;
7443 		quadVertices[1].V = topLeftV;
7444 		quadVertices[2].U = topLeftU;
7445 		quadVertices[2].V = topLeftV + 6;
7446 		quadVertices[3].U = topLeftU;
7447 		quadVertices[3].V = topLeftV + 6;
7448 
7449 		quadVertices[0].X = x+6;
7450 		quadVertices[3].X = x+6;
7451 		quadVertices[1].X = x+6 + w-12;
7452 		quadVertices[2].X = x+6 + w-12;
7453 
7454 		D3D_HUDQuad_Output
7455 		(
7456 			AAFontImageNumber,
7457 			quadVertices,
7458 			colour
7459 		);
7460 	}
7461 	/* top right corner */
7462 	{
7463 		int topLeftU = 11;
7464 		int topLeftV = 238;
7465 
7466 		quadVertices[0].U = topLeftU;
7467 		quadVertices[0].V = topLeftV;
7468 		quadVertices[1].U = topLeftU + 6;
7469 		quadVertices[1].V = topLeftV;
7470 		quadVertices[2].U = topLeftU + 6;
7471 		quadVertices[2].V = topLeftV + 6;
7472 		quadVertices[3].U = topLeftU;
7473 		quadVertices[3].V = topLeftV + 6;
7474 
7475 		quadVertices[0].X = x + w - 6;
7476 		quadVertices[3].X = x + w - 6;
7477 		quadVertices[1].X = x + w;
7478 		quadVertices[2].X = x + w;
7479 
7480 		D3D_HUDQuad_Output
7481 		(
7482 			AAFontImageNumber,
7483 			quadVertices,
7484 			colour
7485 		);
7486 	}
7487 	quadVertices[0].Y = y + 6;
7488 	quadVertices[1].Y = y + 6;
7489 	quadVertices[2].Y = y + h - 6;
7490 	quadVertices[3].Y = y + h - 6;
7491 	/* right */
7492 	{
7493 		int topLeftU = 1;
7494 		int topLeftV = 246;
7495 
7496 		quadVertices[0].U = topLeftU;
7497 		quadVertices[0].V = topLeftV;
7498 		quadVertices[1].U = topLeftU + 6;
7499 		quadVertices[1].V = topLeftV;
7500 		quadVertices[2].U = topLeftU + 6;
7501 		quadVertices[2].V = topLeftV;
7502 		quadVertices[3].U = topLeftU;
7503 		quadVertices[3].V = topLeftV;
7504 
7505 		D3D_HUDQuad_Output
7506 		(
7507 			AAFontImageNumber,
7508 			quadVertices,
7509 			colour
7510 		);
7511 	}
7512 	/* left */
7513 	{
7514 		int topLeftU = 1;
7515 		int topLeftV = 246;
7516 
7517 		quadVertices[0].U = topLeftU;
7518 		quadVertices[0].V = topLeftV;
7519 		quadVertices[1].U = topLeftU + 6;
7520 		quadVertices[1].V = topLeftV;
7521 		quadVertices[2].U = topLeftU + 6;
7522 		quadVertices[2].V = topLeftV;
7523 		quadVertices[3].U = topLeftU;
7524 		quadVertices[3].V = topLeftV;
7525 
7526 		quadVertices[0].X = x;
7527 		quadVertices[3].X = x;
7528 		quadVertices[1].X = x + 6;
7529 		quadVertices[2].X = x + 6;
7530 
7531 		D3D_HUDQuad_Output
7532 		(
7533 			AAFontImageNumber,
7534 			quadVertices,
7535 			colour
7536 		);
7537 	}
7538 	quadVertices[0].Y = y + h - 6;
7539 	quadVertices[1].Y = y + h - 6;
7540 	quadVertices[2].Y = y + h;
7541 	quadVertices[3].Y = y + h;
7542 	/* bottom left corner */
7543 	{
7544 		int topLeftU = 1;
7545 		int topLeftV = 248;
7546 
7547 		quadVertices[0].U = topLeftU;
7548 		quadVertices[0].V = topLeftV;
7549 		quadVertices[1].U = topLeftU + 6;
7550 		quadVertices[1].V = topLeftV;
7551 		quadVertices[2].U = topLeftU + 6;
7552 		quadVertices[2].V = topLeftV + 6;
7553 		quadVertices[3].U = topLeftU;
7554 		quadVertices[3].V = topLeftV + 6;
7555 
7556 		quadVertices[0].X = x;
7557 		quadVertices[3].X = x;
7558 		quadVertices[1].X = x + 6;
7559 		quadVertices[2].X = x + 6;
7560 
7561 		D3D_HUDQuad_Output
7562 		(
7563 			AAFontImageNumber,
7564 			quadVertices,
7565 			colour
7566 		);
7567 	}
7568 	/* bottom */
7569 	{
7570 		int topLeftU = 9;
7571 		int topLeftV = 238;
7572 
7573 		quadVertices[0].U = topLeftU;
7574 		quadVertices[0].V = topLeftV;
7575 		quadVertices[1].U = topLeftU;
7576 		quadVertices[1].V = topLeftV;
7577 		quadVertices[2].U = topLeftU;
7578 		quadVertices[2].V = topLeftV + 6;
7579 		quadVertices[3].U = topLeftU;
7580 		quadVertices[3].V = topLeftV + 6;
7581 
7582 		quadVertices[0].X = x+6;
7583 		quadVertices[3].X = x+6;
7584 		quadVertices[1].X = x+6 + w-12;
7585 		quadVertices[2].X = x+6 + w-12;
7586 
7587 		D3D_HUDQuad_Output
7588 		(
7589 			AAFontImageNumber,
7590 			quadVertices,
7591 			colour
7592 		);
7593 	}
7594 	/* bottom right corner */
7595 	{
7596 		int topLeftU = 11;
7597 		int topLeftV = 248;
7598 
7599 		quadVertices[0].U = topLeftU;
7600 		quadVertices[0].V = topLeftV;
7601 		quadVertices[1].U = topLeftU + 6;
7602 		quadVertices[1].V = topLeftV;
7603 		quadVertices[2].U = topLeftU + 6;
7604 		quadVertices[2].V = topLeftV + 6;
7605 		quadVertices[3].U = topLeftU;
7606 		quadVertices[3].V = topLeftV + 6;
7607 
7608 		quadVertices[0].X = x + w - 6;
7609 		quadVertices[3].X = x + w - 6;
7610 		quadVertices[1].X = x + w;
7611 		quadVertices[2].X = x + w;
7612 
7613 		D3D_HUDQuad_Output
7614 		(
7615 			AAFontImageNumber,
7616 			quadVertices,
7617 			colour
7618 		);
7619 	}
7620 
7621 
7622 }
D3D_DrawColourBar(int yTop,int yBottom,int rScale,int gScale,int bScale)7623 extern void D3D_DrawColourBar(int yTop, int yBottom, int rScale, int gScale, int bScale)
7624 {
7625 	extern unsigned char GammaValues[256];
7626 
7627 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_OFF);
7628     if (CurrTextureHandle)
7629 	{
7630     	OP_STATE_RENDER(1, ExecBufInstPtr);
7631         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
7632         CurrTextureHandle = NULL;
7633 	}
7634 
7635 	for (int i=0; i<255; )
7636 	{
7637 		{
7638 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
7639 			unsigned int colour;
7640 			unsigned int c;
7641 
7642 			c = GammaValues[i];
7643 			colour = RGBA_MAKE(MUL_FIXED(c,rScale),MUL_FIXED(c,gScale),MUL_FIXED(c,bScale),0);
7644 		  	vertexPtr->sx =	(Global_VDB_Ptr->VDB_ClipRight*i)/255;
7645 		  	vertexPtr->sy =	yTop;
7646 			vertexPtr->sz = 0;
7647 			vertexPtr->color = colour;
7648 			vertexPtr++;
7649 		  	vertexPtr->sx =	(Global_VDB_Ptr->VDB_ClipRight*i)/255;
7650 		  	vertexPtr->sy =	yBottom;
7651 			vertexPtr->sz = 0;
7652 			vertexPtr->color = colour;
7653 			vertexPtr++;
7654 
7655 			i++;
7656 			c = GammaValues[i];
7657 			colour = RGBA_MAKE(MUL_FIXED(c,rScale),MUL_FIXED(c,gScale),MUL_FIXED(c,bScale),0);
7658 		  	vertexPtr->sx =	(Global_VDB_Ptr->VDB_ClipRight*i)/255;
7659 		  	vertexPtr->sy =	yBottom;
7660 			vertexPtr->sz = 0;
7661 			vertexPtr->color = colour;
7662 			vertexPtr++;
7663 		  	vertexPtr->sx =	(Global_VDB_Ptr->VDB_ClipRight*i)/255;
7664 		  	vertexPtr->sy =	yTop;
7665 			vertexPtr->sz = 0;
7666 			vertexPtr->color = colour;
7667 			vertexPtr++;
7668 
7669 			NumVertices+=4;
7670 		}
7671 
7672 
7673 		OP_TRIANGLE_LIST(2, ExecBufInstPtr);
7674 		OUTPUT_TRIANGLE(0,1,3, 4);
7675 		OUTPUT_TRIANGLE(1,2,3, 4);
7676 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
7677 		{
7678 		   WriteEndCodeToExecuteBuffer();
7679 	  	   UnlockExecuteBufferAndPrepareForUse();
7680 		   ExecuteBuffer();
7681 	  	   LockExecuteBuffer();
7682 		}
7683 	}
7684 }
7685 
7686 
D3D_FadeDownScreen(int brightness,int colour)7687 extern void D3D_FadeDownScreen(int brightness, int colour)
7688 {
7689 	{
7690 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
7691 		int t = 255 - (brightness>>8);
7692 		if (t<0) t = 0;
7693 
7694  	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7695 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7696 		vertexPtr->sz = 0;
7697 		vertexPtr->color = (t<<24)+colour;
7698 		vertexPtr++;
7699 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7700 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7701 		vertexPtr->sz = 0;
7702 		vertexPtr->color = (t<<24)+colour;
7703 		vertexPtr++;
7704 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7705 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7706 		vertexPtr->sz = 0;
7707 		vertexPtr->color = (t<<24)+colour;
7708 		vertexPtr++;
7709 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7710 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7711 		vertexPtr->sz = 0;
7712 		vertexPtr->color = (t<<24)+colour;
7713 
7714 		NumVertices+=4;
7715 	}
7716 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_NORMAL);
7717 //	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
7718 
7719  //	NoiseTextureHandle = 0;
7720     if (CurrTextureHandle)
7721 	{
7722     	OP_STATE_RENDER(1, ExecBufInstPtr);
7723         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
7724         CurrTextureHandle = NULL;
7725 	}
7726 
7727 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
7728 	OUTPUT_TRIANGLE(0,1,3, 4);
7729 	OUTPUT_TRIANGLE(1,2,3, 4);
7730 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
7731 	{
7732 	   WriteEndCodeToExecuteBuffer();
7733   	   UnlockExecuteBufferAndPrepareForUse();
7734 	   ExecuteBuffer();
7735   	   LockExecuteBuffer();
7736 	}
7737 }
ClearZBufferWithPolygon(void)7738 extern void ClearZBufferWithPolygon(void)
7739 {
7740 	{
7741 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
7742 
7743  	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7744 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7745 		vertexPtr->sz = 1.0f;
7746 		vertexPtr->rhw = 1;
7747 		vertexPtr->color = 0;
7748 		vertexPtr++;
7749 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7750 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7751 		vertexPtr->sz = 1.0f;
7752 		vertexPtr->rhw = 1;
7753 		vertexPtr->color = 0;
7754 		vertexPtr++;
7755 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7756 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7757 		vertexPtr->sz = 1.0f;
7758 		vertexPtr->rhw = 1;
7759 		vertexPtr->color = 0;
7760 		vertexPtr++;
7761 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7762 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7763 		vertexPtr->sz = 1.0f;
7764 		vertexPtr->rhw = 1;
7765 		vertexPtr->color = 0;
7766 
7767 		NumVertices+=4;
7768 	}
7769 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_JUSTSETZ);
7770     OP_STATE_RENDER(1, ExecBufInstPtr);
7771     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_ALWAYS, ExecBufInstPtr);
7772 
7773 	D3DTEXTUREHANDLE TextureHandle = 0;
7774 	if (CurrTextureHandle != TextureHandle)
7775 	{
7776 		OP_STATE_RENDER(1, ExecBufInstPtr);
7777 		STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
7778 		CurrTextureHandle = TextureHandle;
7779 	}
7780 
7781 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
7782 	OUTPUT_TRIANGLE(0,1,3, 4);
7783 	OUTPUT_TRIANGLE(1,2,3, 4);
7784 
7785     OP_STATE_RENDER(1, ExecBufInstPtr);
7786     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
7787 
7788 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
7789 	{
7790 	   WriteEndCodeToExecuteBuffer();
7791   	   UnlockExecuteBufferAndPrepareForUse();
7792 	   ExecuteBuffer();
7793   	   LockExecuteBuffer();
7794 	}
7795 }
7796 
D3D_PlayerOnFireOverlay(void)7797 extern void D3D_PlayerOnFireOverlay(void)
7798 {
7799 	{
7800 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
7801 		int t = 128;
7802 		int colour = (FMVParticleColour&0xffffff)+(t<<24);
7803 
7804 		float u = (FastRandom()&255)/256.0f;
7805 		float v = (FastRandom()&255)/256.0f;
7806 
7807  	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7808 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7809 		vertexPtr->sz = 0;
7810 		vertexPtr->rhw = 1;
7811 		vertexPtr->tu = u;
7812 		vertexPtr->tv = v;
7813 		vertexPtr->color = colour;
7814 		vertexPtr++;
7815 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7816 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7817 		vertexPtr->sz = 0;
7818 		vertexPtr->rhw = 1;
7819 		vertexPtr->tu = u+1.0f;
7820 		vertexPtr->tv = v;
7821 		vertexPtr->color = colour;
7822 		vertexPtr++;
7823 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7824 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7825 		vertexPtr->sz = 0;
7826 		vertexPtr->rhw = 1;
7827 		vertexPtr->tu = u+1.0f;
7828 		vertexPtr->tv = v+1.0f;
7829 		vertexPtr->color = colour;
7830 		vertexPtr++;
7831 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7832 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7833 		vertexPtr->sz = 0;
7834 		vertexPtr->rhw = 1;
7835 		vertexPtr->tu = u;
7836 		vertexPtr->tv = v+1.0f;
7837 		vertexPtr->color = colour;
7838 
7839 		NumVertices+=4;
7840 	}
7841 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
7842 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
7843 
7844 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[BurningImageNumber].D3DHandle;
7845 	if (CurrTextureHandle != TextureHandle)
7846 	{
7847 		OP_STATE_RENDER(1, ExecBufInstPtr);
7848 		STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
7849 		CurrTextureHandle = TextureHandle;
7850 	}
7851 
7852 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
7853 	OUTPUT_TRIANGLE(0,1,3, 4);
7854 	OUTPUT_TRIANGLE(1,2,3, 4);
7855 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
7856 	{
7857 	   WriteEndCodeToExecuteBuffer();
7858   	   UnlockExecuteBufferAndPrepareForUse();
7859 	   ExecuteBuffer();
7860   	   LockExecuteBuffer();
7861 	}
7862 }
D3D_ScreenInversionOverlay(void)7863 extern void D3D_ScreenInversionOverlay(void)
7864 {
7865 	int theta[2];
7866 	int colour = 0xffffffff;
7867 	int i;
7868 
7869 	theta[0] = (CloakingPhase/8)&4095;
7870 	theta[1] = (800-CloakingPhase/8)&4095;
7871 
7872 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_DARKENINGCOLOUR);
7873 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
7874 
7875 	for (i=0; i<2; i++)
7876 	{
7877 		float sin = (GetSin(theta[i]))/65536.0f/16.0f;
7878 		float cos = (GetCos(theta[i]))/65536.0f/16.0f;
7879 		{
7880 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
7881 	 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7882 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7883 			vertexPtr->sz = 0;
7884 			vertexPtr->rhw = 1;
7885 			vertexPtr->tu = 0.375 + (cos*(-1) - sin*(-1));
7886 			vertexPtr->tv = 0.375 + (sin*(-1) + cos*(-1));
7887 			vertexPtr->color = colour;
7888 			vertexPtr++;
7889 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7890 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7891 			vertexPtr->sz = 0;
7892 			vertexPtr->rhw = 1;
7893 			vertexPtr->tu = .375 + (cos*(+1) - sin*(-1));
7894 			vertexPtr->tv = .375 + (sin*(+1) + cos*(-1));
7895 			vertexPtr->color = colour;
7896 			vertexPtr++;
7897 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7898 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7899 			vertexPtr->sz = 0;
7900 			vertexPtr->rhw = 1;
7901 			vertexPtr->tu = .375 + (cos*(+1) - sin*(+1));
7902 			vertexPtr->tv = .375 + (sin*(+1) + cos*(+1));
7903 			vertexPtr->color = colour;
7904 			vertexPtr++;
7905 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7906 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7907 			vertexPtr->sz = 0;
7908 			vertexPtr->rhw = 1;
7909 			vertexPtr->tu = .375 + (cos*(-1) - sin*(+1));
7910 			vertexPtr->tv = .375 + (sin*(-1) + cos*(+1));
7911 			vertexPtr->color = colour;
7912 
7913 			NumVertices+=4;
7914 		}
7915 
7916 	//	D3DTEXTUREHANDLE TextureHandle = NULL;
7917 		D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[SpecialFXImageNumber].D3DHandle;
7918 		if (CurrTextureHandle != TextureHandle)
7919 		{
7920 			OP_STATE_RENDER(1, ExecBufInstPtr);
7921 			STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
7922 			CurrTextureHandle = TextureHandle;
7923 		}
7924 
7925 		OP_TRIANGLE_LIST(2, ExecBufInstPtr);
7926 		OUTPUT_TRIANGLE(0,1,3, 4);
7927 		OUTPUT_TRIANGLE(1,2,3, 4);
7928 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
7929 		{
7930 		   WriteEndCodeToExecuteBuffer();
7931 	  	   UnlockExecuteBufferAndPrepareForUse();
7932 		   ExecuteBuffer();
7933 	  	   LockExecuteBuffer();
7934 		}
7935 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_COLOUR);
7936 	}
7937 }
D3D_PredatorScreenInversionOverlay(void)7938 extern void D3D_PredatorScreenInversionOverlay(void)
7939 {
7940 	int colour = 0xffffffff;
7941 	int i;
7942 	{
7943 		D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
7944 
7945 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7946 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7947 		vertexPtr->sz = 1.0f;
7948 		vertexPtr->rhw = 1.0f;
7949 		vertexPtr->color = colour;
7950 		vertexPtr++;
7951 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7952 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
7953 		vertexPtr->sz = 1.0f;
7954 		vertexPtr->rhw = 1.0f;
7955 		vertexPtr->color = colour;
7956 		vertexPtr++;
7957 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
7958 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7959 		vertexPtr->sz = 1.0f;
7960 		vertexPtr->rhw = 1.0f;
7961 		vertexPtr->color = colour;
7962 		vertexPtr++;
7963 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
7964 	  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
7965 		vertexPtr->sz = 1.0f;
7966 		vertexPtr->rhw = 1.0f;
7967 		vertexPtr->color = colour;
7968 
7969 		NumVertices+=4;
7970 	}
7971 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_DARKENINGCOLOUR);
7972 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
7973     OP_STATE_RENDER(1, ExecBufInstPtr);
7974     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_ALWAYS, ExecBufInstPtr);
7975 
7976 // 	D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[StaticImageNumber].D3DHandle;
7977 	D3DTEXTUREHANDLE TextureHandle = NULL;
7978 //		D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[SpecialFXImageNumber].D3DHandle;
7979 	if (CurrTextureHandle != TextureHandle)
7980 	{
7981 		OP_STATE_RENDER(1, ExecBufInstPtr);
7982 		STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
7983 		CurrTextureHandle = TextureHandle;
7984 	}
7985 
7986 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
7987 	OUTPUT_TRIANGLE(0,1,3, 4);
7988 	OUTPUT_TRIANGLE(1,2,3, 4);
7989 
7990     OP_STATE_RENDER(1, ExecBufInstPtr);
7991     STATE_DATA(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL, ExecBufInstPtr);
7992 
7993 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
7994 	{
7995 	   WriteEndCodeToExecuteBuffer();
7996   	   UnlockExecuteBufferAndPrepareForUse();
7997 	   ExecuteBuffer();
7998   	   LockExecuteBuffer();
7999 	}
8000 }
D3D_PlayerDamagedOverlay(int intensity)8001 extern void D3D_PlayerDamagedOverlay(int intensity)
8002 {
8003 	int theta[2];
8004 //	int colour = 0x00ffff + (intensity<<24);
8005 	int colour,baseColour;
8006 	int i;
8007 
8008 	theta[0] = (CloakingPhase/8)&4095;
8009 	theta[1] = (800-CloakingPhase/8)&4095;
8010 
8011 	switch(AvP.PlayerType)
8012 	{
8013 		default:
8014 			LOCALASSERT(0);
8015 			/* if no debug then fall through to marine */
8016 		case I_Marine:
8017 			baseColour = 0xff0000;
8018 			break;
8019 
8020 		case I_Alien:
8021 			baseColour = 0xffff00;
8022 			break;
8023 
8024 		case I_Predator:
8025 			baseColour = 0x00ff00;
8026 			break;
8027 	}
8028 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_INVCOLOUR);
8029 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_ON);
8030 	colour = 0xffffff - baseColour + (intensity<<24);
8031 	for(i=0; i<=1; i++)
8032 	{
8033 		float sin = (GetSin(theta[i]))/65536.0f/16.0f;
8034 		float cos = (GetCos(theta[i]))/65536.0f/16.0f;
8035 		{
8036 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
8037 	 	  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
8038 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
8039 			vertexPtr->sz = 0;
8040 			vertexPtr->rhw = 1;
8041 			vertexPtr->tu = 0.875 + (cos*(-1) - sin*(-1));
8042 			vertexPtr->tv = 0.375 + (sin*(-1) + cos*(-1));
8043 			vertexPtr->color = colour;
8044 			vertexPtr++;
8045 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
8046 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipUp;
8047 			vertexPtr->sz = 0;
8048 			vertexPtr->rhw = 1;
8049 			vertexPtr->tu = .875 + (cos*(+1) - sin*(-1));
8050 			vertexPtr->tv = .375 + (sin*(+1) + cos*(-1));
8051 			vertexPtr->color = colour;
8052 			vertexPtr++;
8053 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipRight;
8054 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
8055 			vertexPtr->sz = 0;
8056 			vertexPtr->rhw = 1;
8057 			vertexPtr->tu = .875 + (cos*(+1) - sin*(+1));
8058 			vertexPtr->tv = .375 + (sin*(+1) + cos*(+1));
8059 			vertexPtr->color = colour;
8060 			vertexPtr++;
8061 		  	vertexPtr->sx =	Global_VDB_Ptr->VDB_ClipLeft;
8062 		  	vertexPtr->sy =	Global_VDB_Ptr->VDB_ClipDown;
8063 			vertexPtr->sz = 0;
8064 			vertexPtr->rhw = 1;
8065 			vertexPtr->tu = .875 + (cos*(-1) - sin*(+1));
8066 			vertexPtr->tv = .375 + (sin*(-1) + cos*(+1));
8067 			vertexPtr->color = colour;
8068 
8069 			NumVertices+=4;
8070 		}
8071 
8072 	//	D3DTEXTUREHANDLE TextureHandle = NULL;
8073 		D3DTEXTUREHANDLE TextureHandle = (D3DTEXTUREHANDLE)ImageHeaderArray[SpecialFXImageNumber].D3DHandle;
8074 		if (CurrTextureHandle != TextureHandle)
8075 		{
8076 			OP_STATE_RENDER(1, ExecBufInstPtr);
8077 			STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
8078 			CurrTextureHandle = TextureHandle;
8079 		}
8080 
8081 		OP_TRIANGLE_LIST(2, ExecBufInstPtr);
8082 		OUTPUT_TRIANGLE(0,1,3, 4);
8083 		OUTPUT_TRIANGLE(1,2,3, 4);
8084 		if (NumVertices > (MaxVerticesInExecuteBuffer-12))
8085 		{
8086 		   WriteEndCodeToExecuteBuffer();
8087 	  	   UnlockExecuteBufferAndPrepareForUse();
8088 		   ExecuteBuffer();
8089 	  	   LockExecuteBuffer();
8090 		}
8091 
8092 //		colour = 0xff0000+(intensity<<24);
8093 		colour = baseColour +(intensity<<24);
8094 		CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
8095 	}
8096 }
8097 
8098 #define NUMBER_OF_CABLE_SEGMENTS 25
8099 int CableTheta[NUMBER_OF_CABLE_SEGMENTS];
8100 int CablePhi[NUMBER_OF_CABLE_SEGMENTS];
8101 
8102 int CableThetaVelocity[NUMBER_OF_CABLE_SEGMENTS];
8103 int CablePhiVelocity[NUMBER_OF_CABLE_SEGMENTS];
8104 
8105 VECTORCH CableForce[NUMBER_OF_CABLE_SEGMENTS];
8106 VECTORCH CableDirection[NUMBER_OF_CABLE_SEGMENTS];
8107 VECTORCH CableThetaDirection[NUMBER_OF_CABLE_SEGMENTS];
8108 VECTORCH CablePhiDirection[NUMBER_OF_CABLE_SEGMENTS];
8109 
8110 extern void MakeMatrixFromDirection(VECTORCH *directionPtr, MATRIXCH *matrixPtr);
8111 
D3D_DrawCable(VECTORCH * centrePtr,MATRIXCH * orientationPtr)8112 void D3D_DrawCable(VECTORCH *centrePtr, MATRIXCH *orientationPtr)
8113 {
8114 	{
8115 			// Turn OFF texturing if it is on...
8116 			if (CurrTextureHandle != NULL)
8117 			{
8118 				OP_STATE_RENDER(1, ExecBufInstPtr);
8119 				STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, NULL, ExecBufInstPtr);
8120 				CurrTextureHandle = NULL;
8121 			}
8122 
8123 			CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
8124 
8125 			if (NumVertices)
8126 			{
8127 			   WriteEndCodeToExecuteBuffer();
8128 		  	   UnlockExecuteBufferAndPrepareForUse();
8129 			   ExecuteBuffer();
8130 		  	   LockExecuteBuffer();
8131 			}
8132 
8133 			OP_STATE_RENDER(1, ExecBufInstPtr);
8134 			STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, FALSE, ExecBufInstPtr);
8135 
8136 	}
8137 	MeshXScale = 4096/16;
8138 	MeshZScale = 4096/16;
8139 
8140 	for (int field=0; field<3; field++)
8141 	{
8142 	int i=0;
8143 	int x;
8144 	for (x=(0+field*15); x<(16+field*15); x++)
8145 	{
8146 		int z;
8147 		for(z=0; z<16; z++)
8148 		{
8149 			VECTORCH *point = &MeshVertex[i];
8150 			{
8151 				int innerRadius = 20;
8152 				VECTORCH radius;
8153 				int theta = ((4096*z)/15)&4095;
8154 				int rOffset = GetSin((x*64+theta/32-CloakingPhase)&4095);
8155 				rOffset = MUL_FIXED(rOffset,rOffset)/512;
8156 
8157 
8158 				radius.vx = MUL_FIXED(innerRadius+rOffset/8,GetSin(theta));
8159 				radius.vy = MUL_FIXED(innerRadius+rOffset/8,GetCos(theta));
8160 				radius.vz = 0;
8161 
8162 				RotateVector(&radius,orientationPtr);
8163 
8164 				point->vx = centrePtr[x].vx+radius.vx;
8165 				point->vy = centrePtr[x].vy+radius.vy;
8166 				point->vz = centrePtr[x].vz+radius.vz;
8167 
8168 				MeshVertexColour[i] = RGBALIGHT_MAKE(0,rOffset,255,128);
8169 
8170 			}
8171 
8172 			TranslatePointIntoViewspace(point);
8173 
8174 			/* is particle within normal view frustrum ? */
8175 			if(AvP.PlayerType==I_Alien)	/* wide frustrum */
8176 			{
8177 				if(( (-point->vx <= point->vz*2)
8178 		   			&&(point->vx <= point->vz*2)
8179 					&&(-point->vy <= point->vz*2)
8180 					&&(point->vy <= point->vz*2) ))
8181 				{
8182 					MeshVertexOutcode[i]=1;
8183 				}
8184 				else
8185 				{
8186 					MeshVertexOutcode[i]=0;
8187 				}
8188 			}
8189 			else
8190 			{
8191 				if(( (-point->vx <= point->vz)
8192 		   			&&(point->vx <= point->vz)
8193 					&&(-point->vy <= point->vz)
8194 					&&(point->vy <= point->vz) ))
8195 				{
8196 					MeshVertexOutcode[i]=1;
8197 				}
8198 				else
8199 				{
8200 					MeshVertexOutcode[i]=0;
8201 				}
8202 			}
8203 
8204 			i++;
8205 		}
8206 	}
8207 	//textprint("\n");
8208    	if ((MeshVertexOutcode[0]&&MeshVertexOutcode[15]&&MeshVertexOutcode[240]&&MeshVertexOutcode[255]))
8209 	{
8210 		D3D_DrawMoltenMetalMesh_Unclipped();
8211 	   //	D3D_DrawWaterMesh_Unclipped();
8212 	}
8213 	else
8214 //	else if (MeshVertexOutcode[0]||MeshVertexOutcode[15]||MeshVertexOutcode[240]||MeshVertexOutcode[255])
8215 	{
8216 		D3D_DrawMoltenMetalMesh_Clipped();
8217   	   //	D3D_DrawWaterMesh_Clipped();
8218 	}
8219 	}
8220 			OP_STATE_RENDER(1, ExecBufInstPtr);
8221 			STATE_DATA(D3DRENDERSTATE_ZWRITEENABLE, TRUE, ExecBufInstPtr);
8222 }
8223 
8224 
8225 
SetupFMVTexture(FMVTEXTURE * ftPtr)8226 void SetupFMVTexture(FMVTEXTURE *ftPtr)
8227 {
8228 	DDSURFACEDESC ddsd;
8229 	memcpy(&ddsd, &(d3d.TextureFormat[d3d.CurrentTextureFormat].ddsd), sizeof(ddsd));
8230 
8231 	ddsd.dwSize = sizeof(ddsd);
8232 
8233 	ddsd.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
8234 		ddsd.ddsCaps.dwCaps = (DDSCAPS_SYSTEMMEMORY|DDSCAPS_TEXTURE);
8235 
8236 	ddsd.dwHeight = FMV_SIZE;
8237 	ddsd.dwWidth = FMV_SIZE;
8238 
8239 	LastError = lpDD->CreateSurface(&ddsd, &(ftPtr->SrcSurface), NULL);
8240 	LOGDXERR(LastError);
8241 
8242 	DDBLTFX ddbltfx;
8243 	memset(&ddbltfx, 0, sizeof(ddbltfx));
8244 	ddbltfx.dwSize = sizeof(ddbltfx);
8245 	ddbltfx.dwFillColor	= 0;// (2<<11)+(26<<5)+8;
8246 	LastError=(ftPtr->SrcSurface)->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
8247 	LOGDXERR(LastError);
8248 
8249 	LastError = (ftPtr->SrcSurface)->QueryInterface(IID_IDirect3DTexture, (LPVOID*) &(ftPtr->SrcTexture));
8250 	LOGDXERR(LastError);
8251 
8252 
8253 	{
8254 		int PalCaps;
8255 
8256 		if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
8257 		{
8258 			PalCaps = (DDPCAPS_8BIT | DDPCAPS_ALLOW256);
8259 		}
8260 		else if (ddsd.ddpfPixelFormat.dwFlags &	DDPF_PALETTEINDEXED4)
8261 		{
8262 			PalCaps = DDPCAPS_4BIT;
8263 		}
8264 		else
8265 		{
8266 			PalCaps = 0;
8267 		}
8268 
8269 		if (PalCaps)
8270 		{
8271 			LPDIRECTDRAWPALETTE destPalette = NULL;
8272 			LastError = lpDD->CreatePalette(PalCaps, ftPtr->SrcPalette, &destPalette, NULL);
8273 			LOGDXERR(LastError);
8274 
8275 			LastError = (ftPtr->SrcSurface)->SetPalette(destPalette);
8276 //				LastError = tempSurface->SetPalette(destPalette);
8277 			LOGDXERR(LastError);
8278 		}
8279 	}
8280 	ftPtr->DestTexture = 0;
8281 	ftPtr->SoundVolume = 0;
8282 }
8283 
8284 
8285 
UpdateFMVTexture(FMVTEXTURE * ftPtr)8286 void UpdateFMVTexture(FMVTEXTURE *ftPtr)
8287 {
8288 	LPDIRECTDRAWSURFACE destSurface = NULL;
8289 	LOCALASSERT(ftPtr);
8290 	LOCALASSERT(ftPtr->ImagePtr);
8291 	LPDIRECTDRAWSURFACE srcSurface = ftPtr->SrcSurface;
8292 	LPDIRECT3DTEXTURE srcTexture = ftPtr->SrcTexture;
8293 
8294 	LOCALASSERT(srcSurface);
8295 
8296 	DDSURFACEDESC ddsd;
8297 	memset(&ddsd, 0, sizeof(DDSURFACEDESC));
8298 	ddsd.dwSize = sizeof(DDSURFACEDESC);
8299 
8300 	LastError = srcSurface->Lock(NULL,&ddsd,DDLOCK_WAIT,NULL);
8301 
8302 	// check for success
8303 	{
8304 		if (!NextFMVTextureFrame(ftPtr,(void*)ddsd.lpSurface))
8305 		{
8306 	    	LastError = srcSurface->Unlock(NULL);
8307 			LOGDXERR(LastError);
8308 		 	return;
8309 		}
8310   	}
8311 
8312     LastError = srcSurface->Unlock(NULL);
8313 	LOGDXERR(LastError);
8314 
8315 	if (ftPtr->DestTexture)
8316 	{
8317 		ReleaseD3DTexture(ftPtr->DestTexture);
8318 		ftPtr->DestTexture = 0;
8319 	}
8320 
8321 	// Query destination surface for a texture interface.
8322 	memset(&ddsd, 0, sizeof(DDSURFACEDESC));
8323 	ddsd.dwSize = sizeof(DDSURFACEDESC);
8324 
8325 	LastError = srcSurface->GetSurfaceDesc(&ddsd);
8326 	LOGDXERR(LastError);
8327 
8328 	ddsd.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
8329 	ddsd.ddsCaps.dwCaps = (DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD );
8330 
8331 	LastError = lpDD->CreateSurface(&ddsd, &destSurface, NULL);
8332 	LOGDXERR(LastError);
8333 	{
8334 		int PalCaps;
8335 
8336 		if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
8337 		{
8338 			PalCaps = (DDPCAPS_8BIT | DDPCAPS_ALLOW256);
8339 		}
8340 		else if (ddsd.ddpfPixelFormat.dwFlags &	DDPF_PALETTEINDEXED4)
8341 		{
8342 			PalCaps = DDPCAPS_4BIT;
8343 		}
8344 		else
8345 		{
8346 			PalCaps = 0;
8347 		}
8348 
8349 		#if 1
8350 		if (PalCaps)
8351 		{
8352 			LPDIRECTDRAWPALETTE destPalette = NULL;
8353 
8354 			LastError = lpDD->CreatePalette(PalCaps, ftPtr->SrcPalette, &destPalette, NULL);
8355 			LOGDXERR(LastError);
8356 			UpdateFMVTexturePalette(ftPtr);
8357 			LastError = destSurface->SetPalette(destPalette);
8358 			LOGDXERR(LastError);
8359 			LastError = srcSurface->SetPalette(destPalette);
8360 			LOGDXERR(LastError);
8361 
8362 			destPalette->Release();
8363 		}
8364 		#endif
8365 	}
8366 	LastError = destSurface->QueryInterface(IID_IDirect3DTexture,(LPVOID*) &(ftPtr->DestTexture));
8367 	LOGDXERR(LastError);
8368 
8369 	LastError = (ftPtr->DestTexture)->Load(srcTexture);
8370  	LOGDXERR(LastError);
8371 
8372 	LastError = (ftPtr->DestTexture)->GetHandle(d3d.lpD3DDevice, &(ftPtr->ImagePtr->D3DHandle));
8373 	LOGDXERR(LastError);
8374 
8375   //	ftPtr->ImagePtr->DDSurface = destSurface;
8376 //	ftPtr->ImagePtr->D3DTexture = (ftPtr->DestTexture);
8377 	if (destSurface) ReleaseDDSurface(destSurface);
8378 }
8379 #if 0
8380 static int GammaSetting;
8381 void UpdateGammaSettings(int g, int forceUpdate)
8382 {
8383 	LPDIRECTDRAWGAMMACONTROL handle;
8384 	DDGAMMARAMP gammaValues;
8385 
8386 	if (g==GammaSetting && !forceUpdate) return;
8387 
8388 	lpDDSPrimary->QueryInterface(IID_IDirectDrawGammaControl,(LPVOID*)&handle);
8389 
8390 //	handle->GetGammaRamp(0,&gammaValues);
8391 	for (int i=0; i<=255; i++)
8392 	{
8393 		int u = ((i*65536)/255);
8394 		int m = MUL_FIXED(u,u);
8395 		int l = MUL_FIXED(2*u,ONE_FIXED-u);
8396 
8397 		int a;
8398 
8399 		a = m/256+MUL_FIXED(g,l);
8400 		if (a<0) a=0;
8401 		if (a>255) a=255;
8402 
8403 		gammaValues.red[i]=a*256;
8404 		gammaValues.green[i]=a*256;
8405 		gammaValues.blue[i]=a*256;
8406 	}
8407 //	handle->SetGammaRamp(0,&gammaValues);
8408 	handle->SetGammaRamp(DDSGR_CALIBRATE,&gammaValues);
8409 	GammaSetting=g;
8410 //	handle->SetGammaRamp(DDSGR_CALIBRATE,&gammaValues);
8411 	//handle->GetGammaRamp(0,&gammaValues);
8412 	RELEASE(handle);
8413 }
8414 #endif
8415 
8416 
8417 
8418 // For extern "C"
8419 
8420 };
8421 
8422 
8423 
8424 
8425 
8426 
8427 
8428 
8429 
8430 
8431 
8432 
8433 
8434 
8435 
8436 
8437 
8438 
8439 
8440 
8441 
8442 
8443 
8444 
8445 
8446 
8447 #if 0
8448 void D3D_Polygon_Output(RENDERVERTEX *renderVerticesPtr)
8449 {
8450 	D3DTEXTUREHANDLE TextureHandle;
8451 
8452 	float ZNear;
8453 	float RecipW, RecipH;
8454 
8455 
8456 	if(RenderPolygon.IsTextured)
8457 	{
8458 		int texoffset = RenderPolygon.ImageIndex;
8459 		TextureHandle = (D3DTEXTUREHANDLE) ImageHeaderArray[texoffset].D3DHandle;
8460 		if(ImageHeaderArray[texoffset].ImageWidth==128)
8461 		{
8462 			RecipW = 1.0 /128.0;
8463 		}
8464 		else
8465 		{
8466 			float width = (float) ImageHeaderArray[texoffset].ImageWidth;
8467 			RecipW = (1.0 / width);
8468 		}
8469 		if(ImageHeaderArray[texoffset].ImageHeight==128)
8470 		{
8471 			RecipH = 1.0 / 128.0;
8472 		}
8473 		else
8474 		{
8475 			float height = (float) ImageHeaderArray[texoffset].ImageHeight;
8476 			RecipH = (1.0 / height);
8477 		}
8478 	}
8479 
8480 
8481 	/* OUTPUT VERTICES TO EXECUTE BUFFER */
8482 	{
8483 		int i = RenderPolygon.NumberOfVertices;
8484 		RENDERVERTEX *vertices = renderVerticesPtr;
8485 
8486 		do
8487 		{
8488 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
8489 		  	float oneOverZ;
8490 		  	oneOverZ = (1.0)/vertices->Z;
8491 			float zvalue;
8492 
8493 			{
8494 				int x = (vertices->X*(Global_VDB_Ptr->VDB_ProjX+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreX;
8495 
8496 				if (x<Global_VDB_Ptr->VDB_ClipLeft)
8497 				{
8498 					x=Global_VDB_Ptr->VDB_ClipLeft;
8499 				}
8500 				else if (x>Global_VDB_Ptr->VDB_ClipRight)
8501 				{
8502 					x=Global_VDB_Ptr->VDB_ClipRight;
8503 				}
8504 
8505 				vertexPtr->sx=x;
8506 			}
8507 			{
8508 				int y = (vertices->Y*(Global_VDB_Ptr->VDB_ProjY+1))/vertices->Z+Global_VDB_Ptr->VDB_CentreY;
8509 
8510 				if (y<Global_VDB_Ptr->VDB_ClipUp)
8511 				{
8512 					y=Global_VDB_Ptr->VDB_ClipUp;
8513 				}
8514 				else if (y>Global_VDB_Ptr->VDB_ClipDown)
8515 				{
8516 					y=Global_VDB_Ptr->VDB_ClipDown;
8517 				}
8518 				vertexPtr->sy=y;
8519 
8520 			}
8521 
8522 			if (RenderPolygon.IsTextured)
8523 			{
8524 				vertexPtr->tu = ((float)(vertices->U>>16)+0.5) * RecipW;
8525 				vertexPtr->tv = ((float)(vertices->V>>16)+0.5) * RecipH;
8526 				vertexPtr->rhw = oneOverZ;
8527 			}
8528 
8529 			{
8530 				zvalue = vertices->Z+HeadUpDisplayZOffset;
8531 	   //			zvalue /= 65536.0;
8532 	   		   	zvalue = ((zvalue-ZNear)/zvalue);
8533 			}
8534 
8535  			if (RenderPolygon.TranslucencyMode!=TRANSLUCENCY_OFF)
8536 			{
8537 		  		vertexPtr->color = RGBALIGHT_MAKE(vertices->R,vertices->G,vertices->B, vertices->A);
8538 			}
8539 			else
8540 			{
8541 				vertexPtr->color = RGBLIGHT_MAKE(vertices->R,vertices->G,vertices->B);
8542 			}
8543 
8544  			if (RenderPolygon.IsSpecularLit)
8545 			{
8546 		  		vertexPtr->specular = RGBALIGHT_MAKE(vertices->R/2,vertices->G/2,vertices->B/2, 0);
8547 			}
8548 			else
8549 			{
8550 				vertexPtr->specular=0;
8551 			}
8552 
8553 			vertexPtr->sz = zvalue;
8554 
8555 			#if FOG_ON
8556 			if(CurrentRenderStates.FogIsOn)
8557 			{
8558 				if (vertices->Z>CurrentRenderStates.FogDistance)
8559 				{
8560 					int fog = ((vertices->Z-CurrentRenderStates.FogDistance)/FOG_SCALE);
8561 					if (fog<0) fog=0;
8562 				 	if (fog>254) fog=254;
8563 				  	fog=255-fog;
8564 				   	vertexPtr->specular|=RGBALIGHT_MAKE(0,0,0,fog);
8565 				}
8566 				else
8567 				{
8568 				   	vertexPtr->specular|=RGBALIGHT_MAKE(0,0,0,255);
8569 				}
8570 			}
8571 			#endif
8572 
8573 			vertices++;
8574 			NumVertices++;
8575 		}
8576 	  	while(--i);
8577 	}
8578 
8579 	CheckTranslucencyModeIsCorrect(RenderPolygon.TranslucencyMode);
8580 
8581     if (D3DTexturePerspective != Yes)
8582     {
8583 		D3DTexturePerspective = Yes;
8584 		OP_STATE_RENDER(1, ExecBufInstPtr);
8585 		STATE_DATA(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE, ExecBufInstPtr);
8586 	}
8587 
8588     if (TextureHandle != CurrTextureHandle)
8589 	{
8590     	OP_STATE_RENDER(1, ExecBufInstPtr);
8591         STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, TextureHandle, ExecBufInstPtr);
8592 	   	CurrTextureHandle = TextureHandle;
8593 	}
8594 
8595 
8596 	D3D_OutputTriangles();
8597 }
8598 
8599 #endif
8600 
8601 
8602 
8603 
8604 
8605 
AlphaFill(unsigned char R,unsigned char G,unsigned char B,unsigned char translucency) const8606 void r2rect :: AlphaFill
8607 (
8608 	unsigned char R,
8609 	unsigned char G,
8610 	unsigned char B,
8611 	unsigned char translucency
8612 ) const
8613 {
8614 	GLOBALASSERT
8615 	(
8616 		bValidPhys()
8617 	);
8618 	if (y1<=y0) return;
8619 	#if 1
8620 	/* OUTPUT quadVerticesPtr TO EXECUTE BUFFER */
8621 	{
8622 		D3DCOLOR Colour;
8623 
8624   		Colour = RGBALIGHT_MAKE(R,G,B,translucency);
8625 
8626 		{
8627 			D3DTLVERTEX *vertexPtr = &((LPD3DTLVERTEX)ExecuteBufferDataArea)[NumVertices];
8628 
8629 			/* Vertex 0 = Top left */
8630 			vertexPtr->sx= x0;
8631 			vertexPtr->sy= y0;
8632 			vertexPtr->color = Colour;
8633 
8634 			NumVertices++;
8635 			vertexPtr++;
8636 
8637 			/* Vertex 1 = Top right */
8638 			vertexPtr->sx=( x1 - 1);
8639 			vertexPtr->sy=( y0 );
8640 			vertexPtr->color = Colour;
8641 
8642 			NumVertices++;
8643 			vertexPtr++;
8644 
8645 			/* Vertex 2 = Bottom right */
8646 			vertexPtr->sx=( x1 - 1);
8647 			vertexPtr->sy=( y1 - 1);
8648 			vertexPtr->color = Colour;
8649 
8650 			NumVertices++;
8651 			vertexPtr++;
8652 
8653 			/* Vertex 3 = Bottom left */
8654 			vertexPtr->sx=x0;
8655 			vertexPtr->sy=( y1 - 1);
8656 			vertexPtr->color = Colour;
8657 
8658 			NumVertices++;
8659 		}
8660 	}
8661 	// set correct texture handle
8662     if (0 != CurrTextureHandle)
8663 	{
8664        OP_STATE_RENDER(1, ExecBufInstPtr);
8665        STATE_DATA(D3DRENDERSTATE_TEXTUREHANDLE, 0, ExecBufInstPtr);
8666 	   CurrTextureHandle = 0;
8667 	}
8668 
8669 	CheckTranslucencyModeIsCorrect(TRANSLUCENCY_GLOWING);
8670 	/* output triangles to execute buffer */
8671 	OP_TRIANGLE_LIST(2, ExecBufInstPtr);
8672 	OUTPUT_TRIANGLE(0,1,3, 4);
8673 	OUTPUT_TRIANGLE(1,2,3, 4);
8674 
8675 	/* check to see if buffer is getting full */
8676 	if (NumVertices > (MaxVerticesInExecuteBuffer-12))
8677 	{
8678 	   WriteEndCodeToExecuteBuffer();
8679   	   UnlockExecuteBufferAndPrepareForUse();
8680 	   ExecuteBuffer();
8681   	   LockExecuteBuffer();
8682 	}
8683 	#endif
8684 }
8685 extern void D3D_RenderHUDNumber_Centred(unsigned int number,int x,int y,int colour);
8686 extern void D3D_RenderHUDString(char *stringPtr,int x,int y,int colour);
8687 extern void D3D_RenderHUDString_Clipped(char *stringPtr,int x,int y,int colour);
8688 extern void D3D_RenderHUDString_Centred(char *stringPtr, int centreX, int y, int colour);
8689 
D3D_RenderHUDNumber_Centred(unsigned int number,int x,int y,int colour)8690 extern void D3D_RenderHUDNumber_Centred(unsigned int number,int x,int y,int colour)
8691 {
8692 	struct VertexTag quadVertices[4];
8693 	int noOfDigits=3;
8694 	int h = MUL_FIXED(HUDScaleFactor,HUD_DIGITAL_NUMBERS_HEIGHT);
8695 	int w = MUL_FIXED(HUDScaleFactor,HUD_DIGITAL_NUMBERS_WIDTH);
8696 
8697 	quadVertices[0].Y = y;
8698 	quadVertices[1].Y = y;
8699 	quadVertices[2].Y = y + h;
8700 	quadVertices[3].Y = y + h;
8701 
8702 	x += (3*w)/2;
8703 
8704 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
8705 
8706 	do
8707 	{
8708 		int digit = number%10;
8709 		number/=10;
8710 		{
8711 			int topLeftU;
8712 			int topLeftV;
8713 			if (digit<8)
8714 			{
8715 				topLeftU = 1+(digit)*16;
8716 				topLeftV = 1;
8717 			}
8718 			else
8719 			{
8720 				topLeftU = 1+(digit-8)*16;
8721 				topLeftV = 1+24;
8722 			}
8723 			if (AvP.PlayerType == I_Marine) topLeftV+=80;
8724 
8725 			quadVertices[0].U = topLeftU;
8726 			quadVertices[0].V = topLeftV;
8727 			quadVertices[1].U = topLeftU + HUD_DIGITAL_NUMBERS_WIDTH;
8728 			quadVertices[1].V = topLeftV;
8729 			quadVertices[2].U = topLeftU + HUD_DIGITAL_NUMBERS_WIDTH;
8730 			quadVertices[2].V = topLeftV + HUD_DIGITAL_NUMBERS_HEIGHT;
8731 			quadVertices[3].U = topLeftU;
8732 			quadVertices[3].V = topLeftV + HUD_DIGITAL_NUMBERS_HEIGHT;
8733 
8734 			x -= 1+w;
8735 			quadVertices[0].X = x;
8736 			quadVertices[3].X = x;
8737 			quadVertices[1].X = x + w;
8738 			quadVertices[2].X = x + w;
8739 
8740 			D3D_HUDQuad_Output
8741 			(
8742 				HUDFontsImageNumber,
8743 				quadVertices,
8744 				colour
8745 			);
8746 		}
8747 	}
8748 	while(--noOfDigits);
8749 
8750 }
8751 
8752 
D3D_RenderHUDString(char * stringPtr,int x,int y,int colour)8753 extern void D3D_RenderHUDString(char *stringPtr,int x,int y,int colour)
8754 {
8755 	struct VertexTag quadVertices[4];
8756 
8757 	quadVertices[0].Y = y-1;
8758 	quadVertices[1].Y = y-1;
8759 	quadVertices[2].Y = y + HUD_FONT_HEIGHT + 1;
8760 	quadVertices[3].Y = y + HUD_FONT_HEIGHT + 1;
8761 
8762 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
8763 	while( *stringPtr )
8764 	{
8765 		char c = *stringPtr++;
8766 
8767 		{
8768 			int topLeftU = 1+((c-32)&15)*16;
8769 			int topLeftV = 1+((c-32)>>4)*16;
8770 
8771 			quadVertices[0].U = topLeftU - 1;
8772 			quadVertices[0].V = topLeftV - 1;
8773 			quadVertices[1].U = topLeftU + HUD_FONT_WIDTH + 1;
8774 			quadVertices[1].V = topLeftV - 1;
8775 			quadVertices[2].U = topLeftU + HUD_FONT_WIDTH + 1;
8776 			quadVertices[2].V = topLeftV + HUD_FONT_HEIGHT + 1;
8777 			quadVertices[3].U = topLeftU - 1;
8778 			quadVertices[3].V = topLeftV + HUD_FONT_HEIGHT + 1;
8779 
8780 			quadVertices[0].X = x - 1;
8781 			quadVertices[3].X = x - 1;
8782 			quadVertices[1].X = x + HUD_FONT_WIDTH + 1;
8783 			quadVertices[2].X = x + HUD_FONT_WIDTH + 1;
8784 
8785 			D3D_HUDQuad_Output
8786 			(
8787 				AAFontImageNumber,
8788 				quadVertices,
8789 				colour
8790 			);
8791 		}
8792 		x += AAFontWidths[c];
8793 	}
8794 }
D3D_RenderHUDString_Clipped(char * stringPtr,int x,int y,int colour)8795 extern void D3D_RenderHUDString_Clipped(char *stringPtr,int x,int y,int colour)
8796 {
8797 	struct VertexTag quadVertices[4];
8798 
8799  	LOCALASSERT(y<=0);
8800 
8801 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
8802 
8803 	quadVertices[2].Y = y + HUD_FONT_HEIGHT + 1;
8804 	quadVertices[3].Y = y + HUD_FONT_HEIGHT + 1;
8805 
8806 	quadVertices[0].Y = 0;
8807 	quadVertices[1].Y = 0;
8808 
8809 	while ( *stringPtr )
8810 	{
8811 		char c = *stringPtr++;
8812 
8813 		{
8814 			int topLeftU = 1+((c-32)&15)*16;
8815 			int topLeftV = 1+((c-32)>>4)*16;
8816 
8817 			quadVertices[0].U = topLeftU - 1;
8818 			quadVertices[0].V = topLeftV - y;
8819 			quadVertices[1].U = topLeftU + HUD_FONT_WIDTH+1;
8820 			quadVertices[1].V = topLeftV - y;
8821 			quadVertices[2].U = topLeftU + HUD_FONT_WIDTH+1;
8822 			quadVertices[2].V = topLeftV + HUD_FONT_HEIGHT+1;
8823 			quadVertices[3].U = topLeftU - 1;
8824 			quadVertices[3].V = topLeftV + HUD_FONT_HEIGHT+1;
8825 
8826 			quadVertices[0].X = x - 1;
8827 			quadVertices[3].X = x - 1;
8828 			quadVertices[1].X = x + HUD_FONT_WIDTH + 1;
8829 			quadVertices[2].X = x + HUD_FONT_WIDTH + 1;
8830 
8831 			D3D_HUDQuad_Output
8832 			(
8833 				AAFontImageNumber,
8834 				quadVertices,
8835 				colour
8836 			);
8837 		}
8838 		x += AAFontWidths[c];
8839 	}
8840 }
8841 
D3D_RenderHUDString_Centred(char * stringPtr,int centreX,int y,int colour)8842 void D3D_RenderHUDString_Centred(char *stringPtr, int centreX, int y, int colour)
8843 {
8844 	int length = 0;
8845 	char *ptr = stringPtr;
8846 
8847 	while(*ptr)
8848 	{
8849 		length+=AAFontWidths[*ptr++];
8850 	}
8851 	length = MUL_FIXED(HUDScaleFactor,length);
8852 // 	D3D_RenderHUDString(stringPtr,centreX-length/2,y,colour);
8853 
8854 	int x = centreX-length/2;
8855 {
8856 	struct VertexTag quadVertices[4];
8857 
8858 	quadVertices[0].Y = y-MUL_FIXED(HUDScaleFactor,1);
8859 	quadVertices[1].Y = y-MUL_FIXED(HUDScaleFactor,1);
8860 	quadVertices[2].Y = y + MUL_FIXED(HUDScaleFactor,HUD_FONT_HEIGHT + 1);
8861 	quadVertices[3].Y = y + MUL_FIXED(HUDScaleFactor,HUD_FONT_HEIGHT + 1);
8862 
8863 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
8864 	while( *stringPtr )
8865 	{
8866 		char c = *stringPtr++;
8867 
8868 		{
8869 			int topLeftU = 1+((c-32)&15)*16;
8870 			int topLeftV = 1+((c-32)>>4)*16;
8871 			#if 1
8872 			quadVertices[0].U = topLeftU - 1;
8873 			quadVertices[0].V = topLeftV - 1;
8874 			quadVertices[1].U = topLeftU + HUD_FONT_WIDTH + 1;
8875 			quadVertices[1].V = topLeftV - 1;
8876 			quadVertices[2].U = topLeftU + HUD_FONT_WIDTH + 1;
8877 			quadVertices[2].V = topLeftV + HUD_FONT_HEIGHT + 1;
8878 			quadVertices[3].U = topLeftU - 1;
8879 			quadVertices[3].V = topLeftV + HUD_FONT_HEIGHT + 1;
8880 			#else
8881 			quadVertices[0].U = topLeftU ;
8882 			quadVertices[0].V = topLeftV ;
8883 			quadVertices[1].U = topLeftU + HUD_FONT_WIDTH ;
8884 			quadVertices[1].V = topLeftV ;
8885 			quadVertices[2].U = topLeftU + HUD_FONT_WIDTH ;
8886 			quadVertices[2].V = topLeftV + HUD_FONT_HEIGHT ;
8887 			quadVertices[3].U = topLeftU ;
8888 			quadVertices[3].V = topLeftV + HUD_FONT_HEIGHT ;
8889 			#endif
8890 			quadVertices[0].X = x - MUL_FIXED(HUDScaleFactor,1);
8891 			quadVertices[3].X = x - MUL_FIXED(HUDScaleFactor,1);
8892 			quadVertices[1].X = x + MUL_FIXED(HUDScaleFactor,HUD_FONT_WIDTH + 1);
8893 			quadVertices[2].X = x + MUL_FIXED(HUDScaleFactor,HUD_FONT_WIDTH + 1);
8894 
8895 			D3D_HUDQuad_Output
8896 			(
8897 				AAFontImageNumber,
8898 				quadVertices,
8899 				colour
8900 			);
8901 		}
8902 		x += MUL_FIXED(HUDScaleFactor,AAFontWidths[c]);
8903 	}
8904 }
8905 }
8906 
8907 extern "C"
8908 {
8909 
RenderString(char * stringPtr,int x,int y,int colour)8910 extern void RenderString(char *stringPtr, int x, int y, int colour)
8911 {
8912 	D3D_RenderHUDString(stringPtr,x,y,colour);
8913 }
8914 
RenderStringCentred(char * stringPtr,int centreX,int y,int colour)8915 extern void RenderStringCentred(char *stringPtr, int centreX, int y, int colour)
8916 {
8917 	int length = 0;
8918 	char *ptr = stringPtr;
8919 
8920 	while(*ptr)
8921 	{
8922 		length+=AAFontWidths[*ptr++];
8923 	}
8924 	D3D_RenderHUDString(stringPtr,centreX-length/2,y,colour);
8925 }
8926 
RenderStringVertically(char * stringPtr,int centreX,int bottomY,int colour)8927 extern void RenderStringVertically(char *stringPtr, int centreX, int bottomY, int colour)
8928 {
8929 	struct VertexTag quadVertices[4];
8930 	int y = bottomY;
8931 
8932 	quadVertices[0].X = centreX - (HUD_FONT_HEIGHT/2) - 1;
8933 	quadVertices[1].X = quadVertices[0].X;
8934 	quadVertices[2].X = quadVertices[0].X+2+HUD_FONT_HEIGHT*1;
8935 	quadVertices[3].X = quadVertices[2].X;
8936 
8937 	CheckFilteringModeIsCorrect(FILTERING_BILINEAR_OFF);
8938 	while( *stringPtr )
8939 	{
8940 		char c = *stringPtr++;
8941 
8942 		{
8943 			int topLeftU = 1+((c-32)&15)*16;
8944 			int topLeftV = 1+((c-32)>>4)*16;
8945 
8946 			quadVertices[0].U = topLeftU - 1;
8947 			quadVertices[0].V = topLeftV - 1;
8948 			quadVertices[1].U = topLeftU + HUD_FONT_WIDTH;
8949 			quadVertices[1].V = topLeftV - 1;
8950 			quadVertices[2].U = topLeftU + HUD_FONT_WIDTH;
8951 			quadVertices[2].V = topLeftV + HUD_FONT_HEIGHT + 1;
8952 			quadVertices[3].U = topLeftU - 1;
8953 			quadVertices[3].V = topLeftV + HUD_FONT_HEIGHT + 1;
8954 
8955 			quadVertices[0].Y = y ;
8956 			quadVertices[1].Y = y - HUD_FONT_WIDTH*1 -1;
8957 			quadVertices[2].Y = y - HUD_FONT_WIDTH*1 -1;
8958 			quadVertices[3].Y = y ;
8959 
8960 			D3D_HUDQuad_Output
8961 			(
8962 				AAFontImageNumber,
8963 				quadVertices,
8964 				colour
8965 			);
8966 		}
8967 	   	y -= AAFontWidths[c];
8968 	}
8969 }
8970 
8971 };
8972 
8973 
8974