1 //**************************************************************************
2 //**
3 //**	##   ##    ##    ##   ##   ####     ####   ###     ###
4 //**	##   ##  ##  ##  ##   ##  ##  ##   ##  ##  ####   ####
5 //**	 ## ##  ##    ##  ## ##  ##    ## ##    ## ## ## ## ##
6 //**	 ## ##  ########  ## ##  ##    ## ##    ## ##  ###  ##
7 //**	  ###   ##    ##   ###    ##  ##   ##  ##  ##       ##
8 //**	   #    ##    ##    #      ####     ####   ##       ##
9 //**
10 //**	$Id: d3d_local.h 4349 2010-12-17 15:07:11Z dj_jl $
11 //**
12 //**	Copyright (C) 1999-2006 Jānis Legzdiņš
13 //**
14 //**	This program is free software; you can redistribute it and/or
15 //**  modify it under the terms of the GNU General Public License
16 //**  as published by the Free Software Foundation; either version 2
17 //**  of the License, or (at your option) any later version.
18 //**
19 //**	This program is distributed in the hope that it will be useful,
20 //**  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //**  GNU General Public License for more details.
23 //**
24 //**************************************************************************
25 //**
26 //**	Local header for Direct3D drawer
27 //**
28 //**************************************************************************
29 
30 #ifndef _D3D_LOCAL_H
31 #define _D3D_LOCAL_H
32 
33 // HEADER FILES ------------------------------------------------------------
34 
35 #include "winlocal.h"
36 #include "gamedefs.h"
37 #include <d3d9.h>
38 #include "cl_local.h"
39 #include "r_shared.h"
40 
41 // MACROS ------------------------------------------------------------------
42 
43 // TYPES -------------------------------------------------------------------
44 
45 struct MyD3DVertex
46 {
47 	float		x;			// Homogeneous coordinates
48 	float		y;
49 	float		z;
50 	vuint32		colour;		// Vertex colour
51 	float		texs;		// Texture coordinates
52 	float		text;
53 	float		lights;		// Lightmap coordinates
54 	float		lightt;
55 
MyD3DVertexMyD3DVertex56 	MyD3DVertex() { }
MyD3DVertexMyD3DVertex57 	MyD3DVertex(const TVec& v, vuint32 _colour, float _s, float _t)
58 	{
59 		x = v.x;
60 		y = v.y;
61 		z = v.z;
62 		colour = _colour;
63 		texs = _s;
64 		text = _t;
65 		lights = 0.0;
66 		lightt = 0.0;
67 	}
MyD3DVertexMyD3DVertex68 	MyD3DVertex(const TVec& v, vuint32 _colour, float _s, float _t,
69 		float _ls, float _lt)
70 	{
71 		x = v.x;
72 		y = v.y;
73 		z = v.z;
74 		colour = _colour;
75 		texs = _s;
76 		text = _t;
77 		lights = _ls;
78 		lightt = _lt;
79 	}
MyD3DVertexMyD3DVertex80 	MyD3DVertex(float _x, float _y, vuint32 _colour, float _s, float _t)
81 	{
82 		x = _x;
83 		y = _y;
84 		z = 0.0;
85 		colour = _colour;
86 		texs = _s;
87 		text = _t;
88 		lights = 0.0;
89 		lightt = 0.0;
90 	}
91 };
92 
93 #define MYD3D_VERTEX_FORMAT		(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2)
94 
95 struct MyD3DMatrix : public D3DMATRIX
96 {
97 public:
MyD3DMatrixMyD3DMatrix98 	MyD3DMatrix()
99 	{
100 	}
MyD3DMatrixMyD3DMatrix101 	MyD3DMatrix(float f11, float f12, float f13, float f14,
102 				float f21, float f22, float f23, float f24,
103 				float f31, float f32, float f33, float f34,
104 				float f41, float f42, float f43, float f44 )
105 	{
106 		_11 = f11; _12 = f12; _13 = f13; _14 = f14;
107 		_21 = f21; _22 = f22; _23 = f23; _24 = f24;
108 		_31 = f31; _32 = f32; _33 = f33; _34 = f34;
109 		_41 = f41; _42 = f42; _43 = f43; _44 = f44;
110 	}
111 
112 	// access grants
operatorMyD3DMatrix113 	float& operator () ( UINT iRow, UINT iCol )
114 	{
115 		return m[iRow][iCol];
116 	}
operatorMyD3DMatrix117 	float operator () ( UINT iRow, UINT iCol ) const
118 	{
119 		return m[iRow][iCol];
120 	}
121 
122 	friend void MatrixMultiply(MyD3DMatrix &out, const MyD3DMatrix& a,
123 		const MyD3DMatrix& b);
124 	MyD3DMatrix operator * (const MyD3DMatrix& mat) const
125 	{
126 		MyD3DMatrix matT;
127 		MatrixMultiply(matT, *this, mat);
128 		return matT;
129 	}
130 };
131 
132 class VDirect3DDrawer : public VDrawer
133 {
134 public:
135 	VDirect3DDrawer();
136 	void Init();
137 	bool SetResolution(int, int, int, bool);
138 	void InitResolution();
139 	void StartUpdate();
140 	void Update();
141 	void BeginDirectUpdate();
142 	void EndDirectUpdate();
143 	void Shutdown();
144 	void* ReadScreen(int*, bool*);
145 	void ReadBackScreen(int, int, rgba_t*);
146 
147 	//	Rendering stuff
148 	void SetupView(VRenderLevelDrawer*, const refdef_t*);
149 	void SetupViewOrg();
150 	void WorldDrawing();
151 	void EndView();
152 
153 	//	Texture stuff
154 	void PrecacheTexture(VTexture*);
155 
156 	//	Polygon drawing
157 	void DrawSkyPolygon(surface_t*, bool, VTexture*, float, VTexture*, float,
158 		int);
159 	void DrawMaskedPolygon(surface_t*, float, bool);
160 	void DrawSpritePolygon(TVec*, VTexture*, float, bool, VTextureTranslation*,
161 		int, vuint32, vuint32, const TVec&, float, const TVec&, const TVec&,
162 		const TVec&);
163 	void DrawAliasModel(const TVec&, const TAVec&, const TVec&, const TVec&,
164 		VMeshModel*, int, int, VTexture*, VTextureTranslation*, int, vuint32,
165 		vuint32, float, bool, bool, float, bool);
166 	bool StartPortal(VPortal*, bool);
167 	void EndPortal(VPortal*, bool);
168 
169 	//	Particles
170 	void StartParticles();
171 	void DrawParticle(particle_t *);
172 	void EndParticles();
173 
174 	//	Drawing
175 	void DrawPic(float, float, float, float, float, float, float, float,
176 		VTexture*, VTextureTranslation*, float);
177 	void DrawPicShadow(float, float, float, float, float, float, float,
178 		float, VTexture*, float);
179 	void FillRectWithFlat(float, float, float, float, float, float, float,
180 		float, VTexture*);
181 	void FillRect(float, float, float, float, vuint32);
182 	void ShadeRect(int, int, int, int, float);
183 	void DrawConsoleBackground(int);
184 	void DrawSpriteLump(float, float, float, float, VTexture*,
185 		VTextureTranslation*, bool);
186 
187 	//	Automap
188 	void StartAutomap();
189 	void DrawLine(int, int, vuint32, int, int, vuint32);
190 	void EndAutomap();
191 
192 	//	Advanced drawing.
193 	bool SupportsAdvancedRendering();
194 	void DrawWorldAmbientPass();
195 	void BeginShadowVolumesPass();
196 	void BeginLightShadowVolumes();
197 	void RenderSurfaceShadowVolume(surface_t*, TVec&, float);
198 	void BeginLightPass(TVec&, float, vuint32);
199 	void DrawSurfaceLight(surface_t*);
200 	void DrawWorldTexturesPass();
201 	void DrawWorldFogPass();
202 	void EndFogPass();
203 	void DrawAliasModelAmbient(const TVec&, const TAVec&, const TVec&,
204 		const TVec&, VMeshModel*, int, int, VTexture*, vuint32, float, bool);
205 	void DrawAliasModelTextures(const TVec&, const TAVec&, const TVec&,
206 		const TVec&, VMeshModel*, int, int, VTexture*, VTextureTranslation*, int,
207 		float, bool);
208 	void BeginModelsLightPass(TVec&, float, vuint32);
209 	void DrawAliasModelLight(const TVec&, const TAVec&, const TVec&,
210 		const TVec&, VMeshModel*, int, int, VTexture*, float, bool);
211 	void BeginModelsShadowsPass(TVec&, float);
212 	void DrawAliasModelShadow(const TVec&, const TAVec&, const TVec&,
213 		const TVec&, VMeshModel*, int, int, float, bool, const TVec&, float);
214 	void DrawAliasModelFog(const TVec&, const TAVec&, const TVec&,
215 		const TVec&, VMeshModel*, int, int, VTexture*, vuint32, float, bool);
216 
217 private:
218 	bool Reset();
219 	void Setup2D();
220 	void FlushTextures();
221 	void ReleaseTextures();
222 	void FlushTexture(VTexture*);
223 	LPDIRECT3DTEXTURE9 CreateSurface(int, int, int, bool);
224 	void SetTexture(VTexture*, int);
225 	void SetSpriteLump(VTexture*, VTextureTranslation*, int);
226 	void SetPic(VTexture*, VTextureTranslation*, int);
227 	void GenerateTexture(VTexture*, void**, VTextureTranslation*, int);
228 	void UploadTextureImage(LPDIRECT3DTEXTURE9, int, int, int, const rgba_t*);
229 	LPDIRECT3DTEXTURE9 UploadTexture8(int, int, const vuint8*, const rgba_t*);
230 	LPDIRECT3DTEXTURE9 UploadTexture(int, int, const rgba_t*);
231 
232 	void SetFade(vuint32 NewFade);
233 
234 	void DoHorizonPolygon(surface_t*);
235 	void DrawPortalArea(VPortal*);
236 
MakeCol16(vuint8 r,vuint8 g,vuint8 b,vuint8 a)237 	vuint16 MakeCol16(vuint8 r, vuint8 g, vuint8 b, vuint8 a)
238 	{
239 		return vuint16(((a & 0x80) << 8) |
240 			((r & 0xf8) << 7) |
241 			((g & 0xf8) << 2) |
242 			(b >> 3));
243 	}
MakeCol32(vuint8 r,vuint8 g,vuint8 b,vuint8 a)244 	vuint32 MakeCol32(vuint8 r, vuint8 g, vuint8 b, vuint8 a)
245 	{
246 		return (a << 24) | (r << 16) | (g << 8) | b;
247 	}
248 
249 	bool						Windowed;
250 
251 	HMODULE						DLLHandle;
252 
253 	//	Direct3D interfaces
254 	LPDIRECT3D9					Direct3D;
255 	LPDIRECT3DDEVICE9			RenderDevice;
256 
257 
258 	D3DVIEWPORT9				viewData;
259 	MyD3DMatrix					IdentityMatrix;
260 	MyD3DMatrix					matProj;
261 	MyD3DMatrix					matView;
262 	vuint32						SurfaceMemFlag;
263 	bool						square_textures;
264 	int							maxTexSize;
265 	int							TexStage;
266 
267 	float						tex_iw;
268 	float						tex_ih;
269 
270 	int							lastgamma;
271 	vuint32						CurrentFade;
272 
273 	//	Texture filters.
274 	D3DTEXTUREFILTERTYPE		magfilter;
275 	D3DTEXTUREFILTERTYPE		minfilter;
276 	D3DTEXTUREFILTERTYPE		mipfilter;
277 
278 	//	Textures.
279 	LPDIRECT3DTEXTURE9			particle_texture;
280     int                         tscount;
281 
282 	//	Lightmaps.
283 	LPDIRECT3DTEXTURE9			light_surf[NUM_BLOCK_SURFS];
284 
285 	//	Specular lightmaps.
286 	LPDIRECT3DTEXTURE9			add_surf[NUM_BLOCK_SURFS];
287 
288 	IDirect3DSurface9			*DXBlockSurface[2];
289 	BYTE dblock;
290 
291 	static VCvarI device;
292 	static VCvarI clear;
293 	static VCvarI tex_linear;
294 	static VCvarI dither;
295 	static VCvarI blend_sprites;
296 	static VCvarF maxdist;
297 	static VCvarI model_lighting;
298 	static VCvarI specular_highlights;
299 	static VCvarI avoid_input_lag;
300 };
301 
302 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
303 
304 // PUBLIC DATA DECLARATIONS ------------------------------------------------
305 
306 #endif
307