1 //**************************************************************************
2 //**
3 //**	##   ##    ##    ##   ##   ####     ####   ###     ###
4 //**	##   ##  ##  ##  ##   ##  ##  ##   ##  ##  ####   ####
5 //**	 ## ##  ##    ##  ## ##  ##    ## ##    ## ## ## ## ##
6 //**	 ## ##  ########  ## ##  ##    ## ##    ## ##  ###  ##
7 //**	  ###   ##    ##   ###    ##  ##   ##  ##  ##       ##
8 //**	   #    ##    ##    #      ####     ####   ##       ##
9 //**
10 //**	$Id: gl_draw.cpp 4222 2010-05-02 14:54:34Z 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 //**	Functions to draw patches (by post) directly to screen.
27 //**
28 //**************************************************************************
29 
30 // HEADER FILES ------------------------------------------------------------
31 
32 #include "gl_local.h"
33 
34 // MACROS ------------------------------------------------------------------
35 
36 // TYPES -------------------------------------------------------------------
37 
38 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
39 
40 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
41 
42 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
43 
44 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
45 
46 // PUBLIC DATA DEFINITIONS -------------------------------------------------
47 
48 // PRIVATE DATA DEFINITIONS ------------------------------------------------
49 
50 // CODE --------------------------------------------------------------------
51 
52 //==========================================================================
53 //
54 //	VOpenGLDrawer::DrawPic
55 //
56 //==========================================================================
57 
DrawPic(float x1,float y1,float x2,float y2,float s1,float t1,float s2,float t2,VTexture * Tex,VTextureTranslation * Trans,float Alpha)58 void VOpenGLDrawer::DrawPic(float x1, float y1, float x2, float y2,
59 	float s1, float t1, float s2, float t2, VTexture* Tex,
60 	VTextureTranslation* Trans, float Alpha)
61 {
62 	guard(VOpenGLDrawer::DrawPic);
63 	SetPic(Tex, Trans, CM_Default);
64 	if (HaveShaders)
65 	{
66 		p_glUseProgramObjectARB(DrawSimpleProgram);
67 		p_glUniform1iARB(DrawSimpleTextureLoc, 0);
68 		p_glUniform1fARB(DrawSimpleAlphaLoc, Alpha);
69 	}
70 	else
71 	{
72 		if (Alpha < 1.0)
73 		{
74 			glDisable(GL_ALPHA_TEST);
75 			glColor4f(1, 1, 1, Alpha);
76 		}
77 		else
78 		{
79 			glColor4f(1, 1, 1, 1);
80 		}
81 	}
82 	if (Alpha < 1.0)
83 	{
84 		glEnable(GL_BLEND);
85 	}
86 	glBegin(GL_QUADS);
87 	glTexCoord2f(s1 * tex_iw, t1 * tex_ih);
88 	glVertex2f(x1, y1);
89 	glTexCoord2f(s2 * tex_iw, t1 * tex_ih);
90 	glVertex2f(x2, y1);
91 	glTexCoord2f(s2 * tex_iw, t2 * tex_ih);
92 	glVertex2f(x2, y2);
93 	glTexCoord2f(s1 * tex_iw, t2 * tex_ih);
94 	glVertex2f(x1, y2);
95 	glEnd();
96 	if (Alpha < 1.0)
97 	{
98 		glDisable(GL_BLEND);
99 	}
100 	if (!HaveShaders)
101 	{
102 		if (Alpha < 1.0)
103 		{
104 			glEnable(GL_ALPHA_TEST);
105 		}
106 	}
107 	unguard;
108 }
109 
110 //==========================================================================
111 //
112 //	VOpenGLDrawer::DrawPicShadow
113 //
114 //==========================================================================
115 
DrawPicShadow(float x1,float y1,float x2,float y2,float s1,float t1,float s2,float t2,VTexture * Tex,float shade)116 void VOpenGLDrawer::DrawPicShadow(float x1, float y1, float x2, float y2,
117 	float s1, float t1, float s2, float t2, VTexture* Tex, float shade)
118 {
119 	guard(VOpenGLDrawer::DrawPicShadow);
120 	SetPic(Tex, NULL, CM_Default);
121 	if (HaveShaders)
122 	{
123 		p_glUseProgramObjectARB(DrawShadowProgram);
124 		p_glUniform1iARB(DrawSimpleTextureLoc, 0);
125 		p_glUniform1fARB(DrawSimpleAlphaLoc, shade);
126 	}
127 	else
128 	{
129 		glDisable(GL_ALPHA_TEST);
130 		glColor4f(0, 0, 0, shade);
131 	}
132 	glEnable(GL_BLEND);
133 	glBegin(GL_QUADS);
134 	glTexCoord2f(s1 * tex_iw, t1 * tex_ih);
135 	glVertex2f(x1, y1);
136 	glTexCoord2f(s2 * tex_iw, t1 * tex_ih);
137 	glVertex2f(x2, y1);
138 	glTexCoord2f(s2 * tex_iw, t2 * tex_ih);
139 	glVertex2f(x2, y2);
140 	glTexCoord2f(s1 * tex_iw, t2 * tex_ih);
141 	glVertex2f(x1, y2);
142 	glEnd();
143 	glDisable(GL_BLEND);
144 	if (!HaveShaders)
145 	{
146 		glEnable(GL_ALPHA_TEST);
147 	}
148 	unguard;
149 }
150 
151 //==========================================================================
152 //
153 //  VOpenGLDrawer::FillRectWithFlat
154 //
155 // 	Fills rectangle with flat.
156 //
157 //==========================================================================
158 
FillRectWithFlat(float x1,float y1,float x2,float y2,float s1,float t1,float s2,float t2,VTexture * Tex)159 void VOpenGLDrawer::FillRectWithFlat(float x1, float y1, float x2, float y2,
160 	float s1, float t1, float s2, float t2, VTexture* Tex)
161 {
162 	guard(VOpenGLDrawer::FillRectWithFlat);
163 	SetTexture(Tex, CM_Default);
164 
165 	if (HaveShaders)
166 	{
167 		p_glUseProgramObjectARB(DrawSimpleProgram);
168 		p_glUniform1iARB(DrawSimpleTextureLoc, 0);
169 		p_glUniform1fARB(DrawSimpleAlphaLoc, 1.0);
170 	}
171 	else
172 	{
173 		glColor4f(1, 1, 1, 1);
174 	}
175 	glBegin(GL_QUADS);
176 	glTexCoord2f(s1 * tex_iw, t1 * tex_ih);
177 	glVertex2f(x1, y1);
178 	glTexCoord2f(s2 * tex_iw, t1 * tex_ih);
179 	glVertex2f(x2, y1);
180 	glTexCoord2f(s2 * tex_iw, t2 * tex_ih);
181 	glVertex2f(x2, y2);
182 	glTexCoord2f(s1 * tex_iw, t2 * tex_ih);
183 	glVertex2f(x1, y2);
184 	glEnd();
185 	unguard;
186 }
187 
188 //==========================================================================
189 //
190 //  VOpenGLDrawer::FillRect
191 //
192 //==========================================================================
193 
FillRect(float x1,float y1,float x2,float y2,vuint32 colour)194 void VOpenGLDrawer::FillRect(float x1, float y1, float x2, float y2,
195 	vuint32 colour)
196 {
197 	guard(VOpenGLDrawer::FillRect);
198 	if (HaveShaders)
199 	{
200 		p_glUseProgramObjectARB(DrawFixedColProgram);
201 		p_glUniform4fARB(DrawFixedColColourLoc, ((colour >> 16) & 0xff) / 255.0,
202 			((colour >> 8) & 0xff) / 255.0, (colour & 0xff) / 255.0, 1.0);
203 	}
204 	else
205 	{
206 		SetColour(colour);
207 		glDisable(GL_TEXTURE_2D);
208 		glDisable(GL_ALPHA_TEST);
209 	}
210 	glBegin(GL_QUADS);
211 	glVertex2f(x1, y1);
212 	glVertex2f(x2, y1);
213 	glVertex2f(x2, y2);
214 	glVertex2f(x1, y2);
215 	glEnd();
216 	if (!HaveShaders)
217 	{
218 		glEnable(GL_ALPHA_TEST);
219 		glEnable(GL_TEXTURE_2D);
220 	}
221 	unguard;
222 }
223 
224 //==========================================================================
225 //
226 //	VOpenGLDrawer::ShadeRect
227 //
228 //  Fade all the screen buffer, so that the menu is more readable,
229 // especially now that we use the small hufont in the menus...
230 //
231 //==========================================================================
232 
ShadeRect(int x,int y,int w,int h,float darkening)233 void VOpenGLDrawer::ShadeRect(int x, int y, int w, int h, float darkening)
234 {
235 	guard(VOpenGLDrawer::ShadeRect);
236 	if (HaveShaders)
237 	{
238 		p_glUseProgramObjectARB(DrawFixedColProgram);
239 		p_glUniform4fARB(DrawFixedColColourLoc, 0, 0, 0, darkening);
240 	}
241 	else
242 	{
243 		glDisable(GL_ALPHA_TEST);
244 		glDisable(GL_TEXTURE_2D);
245 		glColor4f(0, 0, 0, darkening);
246 	}
247 	glEnable(GL_BLEND);
248 
249 	glBegin(GL_QUADS);
250 	glVertex2f(x, y);
251 	glVertex2f(x + w, y);
252 	glVertex2f(x + w, y + h);
253 	glVertex2f(x, y + h);
254 	glEnd();
255 
256 	glDisable(GL_BLEND);
257 	if (!HaveShaders)
258 	{
259 		glEnable(GL_ALPHA_TEST);
260 		glEnable(GL_TEXTURE_2D);
261 	}
262 	unguard;
263 }
264 
265 //==========================================================================
266 //
267 //	VOpenGLDrawer::DrawConsoleBackground
268 //
269 //==========================================================================
270 
DrawConsoleBackground(int h)271 void VOpenGLDrawer::DrawConsoleBackground(int h)
272 {
273 	guard(VOpenGLDrawer::DrawConsoleBackground);
274 	if (HaveShaders)
275 	{
276 		p_glUseProgramObjectARB(DrawFixedColProgram);
277 		p_glUniform4fARB(DrawFixedColColourLoc, 0, 0, 0.5, 0.75);
278 	}
279 	else
280 	{
281 		glDisable(GL_ALPHA_TEST);
282 		glDisable(GL_TEXTURE_2D);
283 		glColor4f(0, 0, 0.5, 0.75);
284 	}
285 	glEnable(GL_BLEND);
286 
287 	glBegin(GL_QUADS);
288 	glVertex2f(0, 0);
289 	glVertex2f(ScreenWidth, 0);
290 	glVertex2f(ScreenWidth, h);
291 	glVertex2f(0, h);
292 	glEnd();
293 
294 	glDisable(GL_BLEND);
295 	if (!HaveShaders)
296 	{
297 		glEnable(GL_ALPHA_TEST);
298 		glEnable(GL_TEXTURE_2D);
299 	}
300 	unguard;
301 }
302 
303 //==========================================================================
304 //
305 //	VOpenGLDrawer::DrawSpriteLump
306 //
307 //==========================================================================
308 
DrawSpriteLump(float x1,float y1,float x2,float y2,VTexture * Tex,VTextureTranslation * Translation,bool flip)309 void VOpenGLDrawer::DrawSpriteLump(float x1, float y1, float x2, float y2,
310 	VTexture* Tex, VTextureTranslation* Translation, bool flip)
311 {
312 	guard(VOpenGLDrawer::DrawSpriteLump);
313 	SetSpriteLump(Tex, Translation, CM_Default);
314 
315 	float s1, s2;
316 	if (flip)
317 	{
318 		s1 = Tex->GetWidth() * tex_iw;
319 		s2 = 0;
320 	}
321 	else
322 	{
323 		s1 = 0;
324 		s2 = Tex->GetWidth() * tex_iw;
325 	}
326 	float texh = Tex->GetHeight() * tex_ih;
327 
328 	if (HaveShaders)
329 	{
330 		p_glUseProgramObjectARB(DrawSimpleProgram);
331 		p_glUniform1iARB(DrawSimpleTextureLoc, 0);
332 		p_glUniform1fARB(DrawSimpleAlphaLoc, 1.0);
333 	}
334 	else
335 	{
336 		glColor4f(1, 1, 1, 1);
337 	}
338 	glBegin(GL_QUADS);
339 
340 	glTexCoord2f(s1, 0);
341 	glVertex2f(x1, y1);
342 	glTexCoord2f(s2, 0);
343 	glVertex2f(x2, y1);
344 	glTexCoord2f(s2, texh);
345 	glVertex2f(x2, y2);
346 	glTexCoord2f(s1, texh);
347 	glVertex2f(x1, y2);
348 
349 	glEnd();
350 	unguard;
351 }
352 
353 
354 //==========================================================================
355 //
356 //	VOpenGLDrawer::StartAutomap
357 //
358 //==========================================================================
359 
StartAutomap()360 void VOpenGLDrawer::StartAutomap()
361 {
362 	guard(VOpenGLDrawer::StartAutomap);
363 	if (HaveShaders)
364 	{
365 		p_glUseProgramObjectARB(DrawAutomapProgram);
366 	}
367 	else
368 	{
369 		glDisable(GL_TEXTURE_2D);
370 		glDisable(GL_ALPHA_TEST);
371 	}
372 	glEnable(GL_LINE_SMOOTH);
373 	glEnable(GL_BLEND);
374 	glBegin(GL_LINES);
375 	unguard;
376 }
377 
378 //==========================================================================
379 //
380 //	VOpenGLDrawer::DrawLine
381 //
382 //==========================================================================
383 
DrawLine(int x1,int y1,vuint32 c1,int x2,int y2,vuint32 c2)384 void VOpenGLDrawer::DrawLine(int x1, int y1, vuint32 c1, int x2, int y2,
385 	vuint32 c2)
386 {
387 	guard(VOpenGLDrawer::DrawLine);
388 	SetColour(c1);
389 	glVertex2f(x1, y1);
390 	SetColour(c2);
391 	glVertex2f(x2, y2);
392 	unguard;
393 }
394 
395 //==========================================================================
396 //
397 //	VOpenGLDrawer::EndAutomap
398 //
399 //==========================================================================
400 
EndAutomap()401 void VOpenGLDrawer::EndAutomap()
402 {
403 	guard(VOpenGLDrawer::EndAutomap);
404 	glEnd();
405 	glDisable(GL_BLEND);
406 	glDisable(GL_LINE_SMOOTH);
407 	if (!HaveShaders)
408 	{
409 		glEnable(GL_ALPHA_TEST);
410 		glEnable(GL_TEXTURE_2D);
411 	}
412 	unguard;
413 }
414