/
gl_draw.c
413 lines (350 loc) · 9.47 KB
/
gl_draw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//**************************************************************************
//**
//** GL_DRAW.C
//**
//** High level DGL drawing functions.
//**
//**************************************************************************
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include "de_base.h"
#include "de_graphics.h"
#include "de_refresh.h"
#include "de_render.h"
// MACROS ------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static int curfilter = 0; // The current filter (0 = none).
static boolean usePatchOffset = true; // A bit of a hack...
// CODE --------------------------------------------------------------------
void GL_UsePatchOffset(boolean enable)
{
usePatchOffset = enable;
}
void GL_DrawRawScreen_CS
(int lump, float offx, float offy, float scalex, float scaley)
{
boolean isTwoPart;
int pixelBorder = 0;
float tcb = 0;
if(lump < 0 || lump >= numlumps) return;
gl.MatrixMode(DGL_MODELVIEW);
gl.PushMatrix();
gl.LoadIdentity();
// Setup offset and scale.
// Scale the offsets to match the resolution.
gl.Translatef(offx * screenWidth/320.0f, offy * screenHeight/200.0f, 0);
gl.Scalef(scalex, scaley, 1);
gl.MatrixMode(DGL_PROJECTION);
gl.PushMatrix();
gl.LoadIdentity();
gl.Ortho(0, 0, screenWidth, screenHeight, -1, 1);
GL_SetRawImage(lump, 1);
isTwoPart = (lumptexinfo[lump].tex[1] != 0);
if(isTwoPart)
{
tcb = lumptexinfo[lump].height / 256.0f;
}
else
{
// Bottom texture coordinate.
tcb = 1;
}
pixelBorder = lumptexinfo[lump].width[0] * screenWidth / 320;
// The first part is rendered in any case.
gl.Begin(DGL_QUADS);
gl.TexCoord2f(0, 0);
gl.Vertex2f(0, 0);
gl.TexCoord2f(1, 0);
gl.Vertex2f(pixelBorder, 0);
gl.TexCoord2f(1, tcb);
gl.Vertex2f(pixelBorder, screenHeight);
gl.TexCoord2f(0, tcb);
gl.Vertex2f(0, screenHeight);
gl.End();
if(isTwoPart)
{
// And the other part.
GL_SetRawImage(lump, 2);
gl.Begin(DGL_QUADS);
gl.TexCoord2f(0, 0);
gl.Vertex2f(pixelBorder - 1, 0);
gl.TexCoord2f(1, 0);
gl.Vertex2f(screenWidth, 0);
gl.TexCoord2f(1, tcb);
gl.Vertex2f(screenWidth, screenHeight);
gl.TexCoord2f(0, tcb);
gl.Vertex2f(pixelBorder - 1, screenHeight);
gl.End();
}
// Restore the old projection matrix.
gl.PopMatrix();
gl.MatrixMode(DGL_MODELVIEW);
gl.PopMatrix();
}
//===========================================================================
// GL_DrawRawScreen
// Raw screens are 320 x 200.
//===========================================================================
void GL_DrawRawScreen(int lump, float offx, float offy)
{
gl.Color3f(1, 1, 1);
GL_DrawRawScreen_CS(lump, offx, offy, 1, 1);
}
//===========================================================================
// GL_DrawPatch_CS
// Drawing with the Current State.
//===========================================================================
void GL_DrawPatch_CS(int x, int y, int lumpnum)
{
int w, h;
// Set the texture.
GL_SetPatch(lumpnum);
w = lumptexinfo[lumpnum].width[0];
h = lumptexinfo[lumpnum].height;
if(usePatchOffset)
{
x += lumptexinfo[lumpnum].offx;
y += lumptexinfo[lumpnum].offy;
}
gl.Begin(DGL_QUADS);
gl.TexCoord2f(0, 0);
gl.Vertex2f(x, y);
gl.TexCoord2f(1, 0);
gl.Vertex2f(x+w, y);
gl.TexCoord2f(1, 1);
gl.Vertex2f(x+w, y+h);
gl.TexCoord2f(0, 1);
gl.Vertex2f(x, y+h);
gl.End();
// Is there a second part?
if(GL_GetOtherPart(lumpnum))
{
x += w;
GL_BindTexture(GL_GetOtherPart(lumpnum));
w = lumptexinfo[lumpnum].width[1];
gl.Begin(DGL_QUADS);
gl.TexCoord2f(0, 0);
gl.Vertex2f(x, y);
gl.TexCoord2f(1, 0);
gl.Vertex2f(x+w, y);
gl.TexCoord2f(1, 1);
gl.Vertex2f(x+w, y+h);
gl.TexCoord2f(0, 1);
gl.Vertex2f(x, y+h);
gl.End();
}
}
void GL_DrawPatchLitAlpha(int x, int y, float light, float alpha, int lumpnum)
{
gl.Color4f(light, light, light, alpha);
GL_DrawPatch_CS(x, y, lumpnum);
}
void GL_DrawPatch(int x, int y, int lumpnum)
{
if(lumpnum < 0) return;
GL_DrawPatchLitAlpha(x, y, 1, 1, lumpnum);
}
void GL_DrawFuzzPatch(int x, int y, int lumpnum)
{
if(lumpnum < 0) return;
GL_DrawPatchLitAlpha(x, y, 1, .333f, lumpnum);
}
void GL_DrawAltFuzzPatch(int x, int y, int lumpnum)
{
if(lumpnum < 0) return;
GL_DrawPatchLitAlpha(x, y, 1, .666f, lumpnum);
}
void GL_DrawShadowedPatch(int x, int y, int lumpnum)
{
if(lumpnum < 0) return;
GL_DrawPatchLitAlpha(x+2, y+2, 0, .4f, lumpnum);
GL_DrawPatchLitAlpha(x, y, 1, 1, lumpnum);
}
void GL_DrawRect(float x, float y, float w, float h, float r, float g, float b, float a)
{
gl.Color4f(r, g, b, a);
gl.Begin(DGL_QUADS);
gl.TexCoord2f(0, 0);
gl.Vertex2f(x, y);
gl.TexCoord2f(1, 0);
gl.Vertex2f(x+w, y);
gl.TexCoord2f(1, 1);
gl.Vertex2f(x+w, y+h);
gl.TexCoord2f(0, 1);
gl.Vertex2f(x, y+h);
gl.End();
}
void GL_DrawRectTiled(int x, int y, int w, int h, int tw, int th)
{
// Make sure the current texture will be tiled.
gl.TexParameter(DGL_WRAP_S, DGL_REPEAT);
gl.TexParameter(DGL_WRAP_T, DGL_REPEAT);
gl.Begin(DGL_QUADS);
gl.TexCoord2f(0, 0);
gl.Vertex2f(x, y);
gl.TexCoord2f(w/(float)tw, 0);
gl.Vertex2f(x+w, y);
gl.TexCoord2f(w/(float)tw, h/(float)th);
gl.Vertex2f(x+w, y+h);
gl.TexCoord2f(0, h/(float)th);
gl.Vertex2f(x, y+h);
gl.End();
}
// The cut rectangle must be inside the other one.
void GL_DrawCutRectTiled(int x, int y, int w, int h, int tw, int th,
int cx, int cy, int cw, int ch)
{
float ftw = tw, fth = th;
// We'll draw at max four rectangles.
int toph = cy-y, bottomh = y+h-(cy+ch), sideh = h-toph-bottomh,
lefth = cx-x, righth = x+w-(cx+cw);
gl.Begin(DGL_QUADS);
if(toph > 0)
{
// The top rectangle.
gl.TexCoord2f(0, 0);
gl.Vertex2f(x, y);
gl.TexCoord2f(w/ftw, 0);
gl.Vertex2f(x+w, y);
gl.TexCoord2f(w/ftw, toph/fth);
gl.Vertex2f(x+w, y+toph);
gl.TexCoord2f(0, toph/fth);
gl.Vertex2f(x, y+toph);
}
if(lefth > 0 && sideh > 0)
{
float yoff = toph/fth;
// The left rectangle.
gl.TexCoord2f(0, yoff);
gl.Vertex2f(x, y+toph);
gl.TexCoord2f(lefth/ftw, yoff);
gl.Vertex2f(x+lefth, y+toph);
gl.TexCoord2f(lefth/ftw, yoff+sideh/fth);
gl.Vertex2f(x+lefth, y+toph+sideh);
gl.TexCoord2f(0, yoff+sideh/fth);
gl.Vertex2f(x, y+toph+sideh);
}
if(righth > 0 && sideh > 0)
{
int ox = x+lefth+cw;
float xoff = (lefth+cw)/ftw;
float yoff = toph/fth;
// The left rectangle.
gl.TexCoord2f(xoff, yoff);
gl.Vertex2f(ox, y+toph);
gl.TexCoord2f(xoff+righth/ftw, yoff);
gl.Vertex2f(ox+righth, y+toph);
gl.TexCoord2f(xoff+righth/ftw, yoff+sideh/fth);
gl.Vertex2f(ox+righth, y+toph+sideh);
gl.TexCoord2f(xoff, yoff+sideh/fth);
gl.Vertex2f(ox, y+toph+sideh);
}
if(bottomh > 0)
{
int oy = y+toph+sideh;
float yoff = (toph+sideh)/fth;
gl.TexCoord2f(0, yoff);
gl.Vertex2f(x, oy);
gl.TexCoord2f(w/ftw, yoff);
gl.Vertex2f(x+w, oy);
gl.TexCoord2f(w/ftw, yoff+bottomh/fth);
gl.Vertex2f(x+w, oy+bottomh);
gl.TexCoord2f(0, yoff+bottomh/fth);
gl.Vertex2f(x, oy+bottomh);
}
gl.End();
}
// Totally inefficient for a large number of lines.
void GL_DrawLine(float x1, float y1, float x2, float y2,
float r, float g, float b, float a)
{
gl.Color4f(r, g, b, a);
gl.Begin(DGL_LINES);
gl.Vertex2f(x1, y1);
gl.Vertex2f(x2, y2);
gl.End();
}
void GL_SetColor(int palidx)
{
GL_SetColor2(palidx, 1);
}
void GL_SetColor2(int palidx, float alpha)
{
byte rgb[4];
if(palidx == -1) // Invisible?
{
gl.Color4f(0, 0, 0, 0);
}
else
{
PalIdxToRGB(W_CacheLumpNum(pallump, PU_CACHE), palidx, rgb);
if(alpha < 0) alpha = 0;
if(alpha > 1) alpha = 1;
rgb[3] = alpha * 255;
gl.Color4ubv(rgb);
}
}
void GL_SetColorAndAlpha(float r, float g, float b, float a)
{
gl.Color4f(r, g, b, a);
}
void GL_SetFilter(int filterRGBA)
{
curfilter = filterRGBA;
}
// Returns nonzero if the filter was drawn.
int GL_DrawFilter()
{
if(!curfilter) return 0; // No filter needed.
// No texture, please.
gl.Disable(DGL_TEXTURING);
gl.Color4ub(curfilter&0xff, (curfilter>>8)&0xff, (curfilter>>16)&0xff,
(curfilter>>24)&0xff);
gl.Begin(DGL_QUADS);
gl.Vertex2f(0, 0);
gl.Vertex2f(320, 0);
gl.Vertex2f(320, 200);
gl.Vertex2f(0, 200);
gl.End();
gl.Enable(DGL_TEXTURING);
return 1;
}
//===========================================================================
// GL_DrawPSprite
//===========================================================================
void GL_DrawPSprite(float x, float y, float scale, int flip, int lump)
{
int w, h;
int w2, h2;
float s, t;
int pSprMode = 1;
spritelump_t *slump = spritelumps + lump;
if(flip) flip = 1; // Make sure it's zero or one.
GL_SetSprite(lump, pSprMode);
w = slump->width;
h = slump->height;
w2 = CeilPow2(w);
h2 = CeilPow2(h);
// Let's calculate texture coordinates.
// To remove a possible edge artifact, move the corner a bit up/left.
s = slump->tc[pSprMode][VX] - 0.4f/w2;
t = slump->tc[pSprMode][VY] - 0.4f/h2;
gl.Begin(DGL_QUADS);
gl.TexCoord2f(flip * s, 0);
gl.Vertex2f(x, y);
gl.TexCoord2f(!flip * s, 0);
gl.Vertex2f(x + w*scale, y);
gl.TexCoord2f(!flip * s, t);
gl.Vertex2f(x + w*scale, y + h*scale);
gl.TexCoord2f(flip * s, t);
gl.Vertex2f(x, y+h*scale);
gl.End();
}