Quake II RTX doxygen  1.0 dev
draw.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2003-2006 Andrey Nazarov
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "gl.h"
20 
22 
23 static inline void _GL_StretchPic(
24  float x, float y, float w, float h,
25  float s1, float t1, float s2, float t2,
26  uint32_t color, int texnum, int flags)
27 {
28  vec_t *dst_vert;
29  uint32_t *dst_color;
30  QGL_INDEX_TYPE *dst_indices;
31 
32  if (tess.numverts + 4 > TESS_MAX_VERTICES ||
34  (tess.numverts && tess.texnum[0] != texnum)) {
35  GL_Flush2D();
36  }
37 
38  tess.texnum[0] = texnum;
39 
40  dst_vert = tess.vertices + tess.numverts * 4;
41  Vector4Set(dst_vert, x, y, s1, t1);
42  Vector4Set(dst_vert + 4, x + w, y, s2, t1);
43  Vector4Set(dst_vert + 8, x + w, y + h, s2, t2);
44  Vector4Set(dst_vert + 12, x, y + h, s1, t2);
45 
46  dst_color = (uint32_t *)tess.colors + tess.numverts;
47  dst_color[0] = color;
48  dst_color[1] = color;
49  dst_color[2] = color;
50  dst_color[3] = color;
51 
52  dst_indices = tess.indices + tess.numindices;
53  dst_indices[0] = tess.numverts + 0;
54  dst_indices[1] = tess.numverts + 2;
55  dst_indices[2] = tess.numverts + 3;
56  dst_indices[3] = tess.numverts + 0;
57  dst_indices[4] = tess.numverts + 1;
58  dst_indices[5] = tess.numverts + 2;
59 
60  if (flags & IF_TRANSPARENT) {
61  if ((flags & IF_PALETTED) && draw.scale == 1) {
62  tess.flags |= 1;
63  } else {
64  tess.flags |= 2;
65  }
66  }
67 
68  if ((color & U32_ALPHA) != U32_ALPHA) {
69  tess.flags |= 2;
70  }
71 
72  tess.numverts += 4;
73  tess.numindices += 6;
74 }
75 
76 #define GL_StretchPic(x,y,w,h,s1,t1,s2,t2,color,image) \
77  _GL_StretchPic(x,y,w,h,s1,t1,s2,t2,color,(image)->texnum,(image)->flags)
78 
79 void GL_Blend(void)
80 {
81  color_t color;
82 
83  color.u8[0] = glr.fd.blend[0] * 255;
84  color.u8[1] = glr.fd.blend[1] * 255;
85  color.u8[2] = glr.fd.blend[2] * 255;
86  color.u8[3] = glr.fd.blend[3] * 255;
87 
88  _GL_StretchPic(glr.fd.x, glr.fd.y, glr.fd.width, glr.fd.height, 0, 0, 1, 1,
89  color.u32, TEXNUM_WHITE, 0);
90 }
91 
92 void R_ClearColor_GL(void)
93 {
94  draw.colors[0].u32 = U32_WHITE;
95  draw.colors[1].u32 = U32_WHITE;
96 }
97 
98 void R_SetAlpha_GL(float alpha)
99 {
100  draw.colors[0].u8[3] =
101  draw.colors[1].u8[3] = alpha * 255;
102 }
103 
104 void R_SetAlphaScale_GL(float alpha)
105 {
106  // nop - only used by the RTX renderer
107 }
108 
109 void R_SetColor_GL(uint32_t color)
110 {
111  draw.colors[0].u32 = color;
112  draw.colors[1].u8[3] = draw.colors[0].u8[3];
113 }
114 
115 void R_SetClipRect_GL(const clipRect_t *clip)
116 {
117  clipRect_t rc;
118  float scale;
119 
120  GL_Flush2D();
121 
122  if (!clip) {
123 clear:
124  if (draw.scissor) {
125  qglDisable(GL_SCISSOR_TEST);
126  draw.scissor = qfalse;
127  }
128  return;
129  }
130 
131  scale = 1 / draw.scale;
132 
133  rc.left = clip->left * scale;
134  rc.top = clip->top * scale;
135  rc.right = clip->right * scale;
136  rc.bottom = clip->bottom * scale;
137 
138  if (rc.left < 0)
139  rc.left = 0;
140  if (rc.top < 0)
141  rc.top = 0;
142  if (rc.right > r_config.width)
143  rc.right = r_config.width;
144  if (rc.bottom > r_config.height)
145  rc.bottom = r_config.height;
146  if (rc.right < rc.left)
147  goto clear;
148  if (rc.bottom < rc.top)
149  goto clear;
150 
151  qglEnable(GL_SCISSOR_TEST);
152  qglScissor(rc.left, r_config.height - rc.bottom,
153  rc.right - rc.left, rc.bottom - rc.top);
154  draw.scissor = qtrue;
155 }
156 
157 void R_SetScale_GL(float scale)
158 {
159  if (draw.scale == scale) {
160  return;
161  }
162 
163  GL_Flush2D();
164 
165  GL_Ortho(0, Q_rint(r_config.width * scale),
166  Q_rint(r_config.height * scale), 0, -1, 1);
167 
168  draw.scale = scale;
169 }
170 
171 void R_DrawStretchPic_GL(int x, int y, int w, int h, qhandle_t pic)
172 {
173  image_t *image = IMG_ForHandle(pic);
174 
175  GL_StretchPic(x, y, w, h, image->sl, image->tl, image->sh, image->th,
176  draw.colors[0].u32, image);
177 }
178 
179 void R_DrawPic_GL(int x, int y, qhandle_t pic)
180 {
181  image_t *image = IMG_ForHandle(pic);
182 
183  GL_StretchPic(x, y, image->width, image->height,
184  image->sl, image->tl, image->sh, image->th, draw.colors[0].u32, image);
185 }
186 
187 #define DIV64 (1.0f / 64.0f)
188 
189 void R_TileClear_GL(int x, int y, int w, int h, qhandle_t pic)
190 {
191  GL_StretchPic(x, y, w, h, x * DIV64, y * DIV64,
192  (x + w) * DIV64, (y + h) * DIV64, U32_WHITE, IMG_ForHandle(pic));
193 }
194 
195 void R_DrawFill8_GL(int x, int y, int w, int h, int c)
196 {
197  if (!w || !h)
198  return;
199  _GL_StretchPic(x, y, w, h, 0, 0, 1, 1, d_8to24table[c & 0xff], TEXNUM_WHITE, 0);
200 }
201 
202 void R_DrawFill32_GL(int x, int y, int w, int h, uint32_t color)
203 {
204  if (!w || !h)
205  return;
206  _GL_StretchPic(x, y, w, h, 0, 0, 1, 1, color, TEXNUM_WHITE, 0);
207 }
208 
209 static inline void draw_char(int x, int y, int flags, int c, image_t *image)
210 {
211  float s, t;
212 
213  if ((c & 127) == 32) {
214  return;
215  }
216 
217  if (flags & UI_ALTCOLOR) {
218  c |= 0x80;
219  }
220  if (flags & UI_XORCOLOR) {
221  c ^= 0x80;
222  }
223 
224  s = (c & 15) * 0.0625f;
225  t = (c >> 4) * 0.0625f;
226 
227  if (gl_fontshadow->integer > 0 && c != 0x83) {
228  uint32_t black = MakeColor(0, 0, 0, draw.colors[0].u8[3]);
229 
230  GL_StretchPic(x + 1, y + 1, CHAR_WIDTH, CHAR_HEIGHT, s, t,
231  s + 0.0625f, t + 0.0625f, black, image);
232 
233  if (gl_fontshadow->integer > 1)
234  GL_StretchPic(x + 2, y + 2, CHAR_WIDTH, CHAR_HEIGHT, s, t,
235  s + 0.0625f, t + 0.0625f, black, image);
236  }
237 
238  GL_StretchPic(x, y, CHAR_WIDTH, CHAR_HEIGHT, s, t,
239  s + 0.0625f, t + 0.0625f, draw.colors[c >> 7].u32, image);
240 }
241 
242 void R_DrawChar_GL(int x, int y, int flags, int c, qhandle_t font)
243 {
244  draw_char(x, y, flags, c & 255, IMG_ForHandle(font));
245 }
246 
247 int R_DrawString_GL(int x, int y, int flags, size_t maxlen, const char *s, qhandle_t font)
248 {
249  image_t *image = IMG_ForHandle(font);
250 
251  while (maxlen-- && *s) {
252  byte c = *s++;
253  draw_char(x, y, flags, c, image);
254  x += CHAR_WIDTH;
255  }
256 
257  return x;
258 }
259 
260 #ifdef _DEBUG
261 
262 image_t *r_charset;
263 
264 void Draw_Stringf(int x, int y, const char *fmt, ...)
265 {
266  va_list argptr;
267  char buffer[MAX_STRING_CHARS];
268  char *string;
269  byte c;
270  float s, t;
271 
272  va_start(argptr, fmt);
273  Q_vsnprintf(buffer, sizeof(buffer), fmt, argptr);
274  va_end(argptr);
275 
276  string = buffer;
277  while (*string) {
278  c = *string++;
279 
280  s = (c & 15) * 0.0625f;
281  t = (c >> 4) * 0.0625f;
282 
283  GL_StretchPic(x, y, CHAR_WIDTH, CHAR_HEIGHT, s, t,
284  s + 0.0625f, t + 0.0625f, U32_WHITE, r_charset);
285  x += CHAR_WIDTH;
286  }
287 }
288 
289 void Draw_Stats(void)
290 {
291  int x = 10, y = 10;
292 
293  if (!r_charset) {
294  qhandle_t tmp;
295  tmp = R_RegisterFont("conchars");
296  if (!tmp) return;
297  r_charset = IMG_ForHandle(tmp);
298  }
299 
300  Draw_Stringf(x, y, "Nodes visible : %i", c.nodesVisible); y += 10;
301  Draw_Stringf(x, y, "Nodes culled : %i", c.nodesCulled); y += 10;
302  Draw_Stringf(x, y, "Nodes drawn : %i", c.nodesDrawn); y += 10;
303  Draw_Stringf(x, y, "Leaves drawn : %i", c.leavesDrawn); y += 10;
304  Draw_Stringf(x, y, "Faces drawn : %i", c.facesDrawn); y += 10;
305  if (c.facesCulled) {
306  Draw_Stringf(x, y, "Faces culled : %i", c.facesCulled); y += 10;
307  }
308  if (c.boxesCulled) {
309  Draw_Stringf(x, y, "Boxes culled : %i", c.boxesCulled); y += 10;
310  }
311  if (c.spheresCulled) {
312  Draw_Stringf(x, y, "Spheres culled : %i", c.spheresCulled); y += 10;
313  }
314  if (c.rotatedBoxesCulled) {
315  Draw_Stringf(x, y, "RtBoxes culled : %i", c.rotatedBoxesCulled); y += 10;
316  }
317  Draw_Stringf(x, y, "Tris drawn : %i", c.trisDrawn); y += 10;
318  Draw_Stringf(x, y, "Tex switches : %i", c.texSwitches); y += 10;
319  if (c.texUploads) {
320  Draw_Stringf(x, y, "Tex uploads : %i", c.texUploads); y += 10;
321  }
322  if (c.batchesDrawn) {
323  Draw_Stringf(x, y, "Batches drawn: %i", c.batchesDrawn); y += 10;
324  Draw_Stringf(x, y, "Faces / batch: %.1f", (float)c.facesDrawn / c.batchesDrawn);
325  y += 10;
326  Draw_Stringf(x, y, "Tris / batch : %.1f", (float)c.facesTris / c.batchesDrawn);
327  y += 10;
328  }
329  Draw_Stringf(x, y, "2D batches : %i", c.batchesDrawn2D); y += 10;
330 }
331 
332 void Draw_Lightmaps(void)
333 {
334  int i, x, y;
335 
336  for (i = 0; i < lm.nummaps; i++) {
337  x = i & 1;
338  y = i >> 1;
339  _GL_StretchPic(256 * x, 256 * y, 256, 256,
340  0, 0, 1, 1, U32_WHITE, lm.texnums[i], 0);
341  }
342 }
343 
344 void Draw_Scrap(void)
345 {
346  _GL_StretchPic(0, 0, 256, 256,
347  0, 0, 1, 1, U32_WHITE, TEXNUM_SCRAP, IF_PALETTED | IF_TRANSPARENT);
348 }
349 
350 #endif
TEXNUM_SCRAP
#define TEXNUM_SCRAP
Definition: gl.h:440
R_SetAlphaScale_GL
void R_SetAlphaScale_GL(float alpha)
Definition: draw.c:104
statCounters_t::nodesCulled
int nodesCulled
Definition: gl.h:144
TESS_MAX_INDICES
#define TESS_MAX_INDICES
Definition: gl.h:460
statCounters_t::facesDrawn
int facesDrawn
Definition: gl.h:138
R_SetClipRect_GL
void R_SetClipRect_GL(const clipRect_t *clip)
Definition: draw.c:115
lm
lightmap_builder_t lm
Definition: surf.c:25
image_t
struct image_s image_t
Definition: material.h:27
statCounters_t::batchesDrawn2D
int batchesDrawn2D
Definition: gl.h:149
tesselator_t::numindices
int numindices
Definition: gl.h:468
GL_Ortho
void GL_Ortho(GLfloat xmin, GLfloat xmax, GLfloat ymin, GLfloat ymax, GLfloat znear, GLfloat zfar)
Definition: state.c:230
R_ClearColor_GL
void R_ClearColor_GL(void)
Definition: draw.c:92
R_SetScale_GL
void R_SetScale_GL(float scale)
Definition: draw.c:157
statCounters_t::trisDrawn
int trisDrawn
Definition: gl.h:142
drawStatic_t::colors
color_t colors[2]
Definition: gl.h:402
R_DrawString_GL
int R_DrawString_GL(int x, int y, int flags, size_t maxlen, const char *s, qhandle_t font)
Definition: draw.c:247
d_8to24table
uint32_t d_8to24table[256]
Definition: images.c:654
statCounters_t::nodesDrawn
int nodesDrawn
Definition: gl.h:135
Q_vsnprintf
size_t Q_vsnprintf(char *dest, size_t size, const char *fmt, va_list argptr)
Definition: shared.c:791
R_DrawPic_GL
void R_DrawPic_GL(int x, int y, qhandle_t pic)
Definition: draw.c:179
statCounters_t::boxesCulled
int boxesCulled
Definition: gl.h:146
GL_Flush2D
void GL_Flush2D(void)
Definition: tess.c:31
DIV64
#define DIV64
Definition: draw.c:187
draw
drawStatic_t draw
Definition: draw.c:21
R_DrawFill32_GL
void R_DrawFill32_GL(int x, int y, int w, int h, uint32_t color)
Definition: draw.c:202
statCounters_t::texUploads
int texUploads
Definition: gl.h:141
TESS_MAX_VERTICES
#define TESS_MAX_VERTICES
Definition: gl.h:459
R_DrawChar_GL
void R_DrawChar_GL(int x, int y, int flags, int c, qhandle_t font)
Definition: draw.c:242
GL_StretchPic
#define GL_StretchPic(x, y, w, h, s1, t1, s2, t2, color, image)
Definition: draw.c:76
qglScissor
#define qglScissor
Definition: fixed.h:99
statCounters_t::texSwitches
int texSwitches
Definition: gl.h:140
statCounters_t::leavesDrawn
int leavesDrawn
Definition: gl.h:136
R_SetAlpha_GL
void R_SetAlpha_GL(float alpha)
Definition: draw.c:98
tesselator_t::indices
QGL_INDEX_TYPE indices[TESS_MAX_INDICES]
Definition: gl.h:464
qglDisable
#define qglDisable
Definition: fixed.h:54
_GL_StretchPic
static void _GL_StretchPic(float x, float y, float w, float h, float s1, float t1, float s2, float t2, uint32_t color, int texnum, int flags)
Definition: draw.c:23
draw_char
static void draw_char(int x, int y, int flags, int c, image_t *image)
Definition: draw.c:209
R_DrawStretchPic_GL
void R_DrawStretchPic_GL(int x, int y, int w, int h, qhandle_t pic)
Definition: draw.c:171
tesselator_t::texnum
GLuint texnum[MAX_TMUS]
Definition: gl.h:466
tesselator_t::flags
int flags
Definition: gl.h:469
c
statCounters_t c
Definition: main.c:30
lightmap_builder_t::nummaps
int nummaps
Definition: gl.h:264
glr
glRefdef_t glr
Definition: main.c:27
tesselator_t::colors
GLubyte colors[4 *TESS_MAX_VERTICES]
Definition: gl.h:465
drawStatic_t
Definition: gl.h:401
R_SetColor_GL
void R_SetColor_GL(uint32_t color)
Definition: draw.c:109
tess
tesselator_t tess
Definition: tess.c:21
statCounters_t::facesTris
int facesTris
Definition: gl.h:139
R_DrawFill8_GL
void R_DrawFill8_GL(int x, int y, int w, int h, int c)
Definition: draw.c:195
TEXNUM_WHITE
#define TEXNUM_WHITE
Definition: draw.c:30
lightmap_builder_t::texnums
GLuint texnums[LM_MAX_LIGHTMAPS]
Definition: gl.h:265
gl.h
statCounters_t::batchesDrawn
int batchesDrawn
Definition: gl.h:143
statCounters_t::rotatedBoxesCulled
int rotatedBoxesCulled
Definition: gl.h:148
GL_Blend
void GL_Blend(void)
Definition: draw.c:79
color
static vec4_t color
Definition: mesh.c:33
IMG_ForHandle
image_t * IMG_ForHandle(qhandle_t h)
Definition: images.c:1156
statCounters_t::spheresCulled
int spheresCulled
Definition: gl.h:147
drawStatic_t::scale
float scale
Definition: gl.h:404
QGL_INDEX_TYPE
#define QGL_INDEX_TYPE
Definition: gl.h:48
tesselator_t::vertices
GLfloat vertices[VERTEX_SIZE *TESS_MAX_VERTICES]
Definition: gl.h:463
drawStatic_t::scissor
qboolean scissor
Definition: gl.h:403
R_TileClear_GL
void R_TileClear_GL(int x, int y, int w, int h, qhandle_t pic)
Definition: draw.c:189
gl_fontshadow
cvar_t * gl_fontshadow
Definition: main.c:54
statCounters_t::nodesVisible
int nodesVisible
Definition: gl.h:134
r_config
refcfg_t r_config
Definition: refresh.c:401
glRefdef_t::fd
refdef_t fd
Definition: gl.h:81
tesselator_t::numverts
int numverts
Definition: gl.h:467
qglEnable
#define qglEnable
Definition: fixed.h:58
statCounters_t::facesCulled
int facesCulled
Definition: gl.h:145