ec4694f1d66deae62495df2ca8f90779d29bf516
[xonotic/netradiant.git] / contrib / prtview / portals.cpp
1 /*
2 PrtView plugin for GtkRadiant
3 Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include "stdafx.h"
21 #include <string.h>
22 #include <stdlib.h>
23 #ifndef __APPLE__
24 #include <search.h>
25 #endif
26 #include <stdio.h>
27
28 #define LINE_BUF 1000
29
30 CPortals portals;
31 CPortalsRender render;
32
33 int compare( const void *arg1, const void *arg2 )
34 {
35
36         if(portals.portal[*((int *)arg1)].dist > portals.portal[*((int *)arg2)].dist)
37                 return -1;
38         else if(portals.portal[*((int *)arg1)].dist < portals.portal[*((int *)arg2)].dist)
39                 return 1;
40
41         return 0;
42 }
43
44
45 CBspPortal::CBspPortal()
46 {
47         memset(this, 0, sizeof(CBspPortal));
48 }
49
50 CBspPortal::~CBspPortal()
51 {
52         delete[] point;
53         delete[] inner_point;
54 }
55
56 bool CBspPortal::Build(char *def)
57 {
58         char *c = def;
59         unsigned int n;
60         int dummy1, dummy2;
61         int res_cnt, i;
62
63         if(portals.hint_flags)
64         {
65                 res_cnt = sscanf(def, "%u %d %d %d", &point_count, &dummy1, &dummy2, (int *)&hint);
66         }
67         else
68         {
69                 sscanf(def, "%u", &point_count);
70                 hint = qfalse;
71         }
72
73         if(point_count < 3 || (portals.hint_flags && res_cnt < 4))
74                 return qfalse;
75
76         point = new CBspPoint[point_count];
77         inner_point = new CBspPoint[point_count];
78
79         for(n = 0; n < point_count; n++)
80         {
81                 for(; *c != 0 && *c != '('; c++);
82
83                 if(*c == 0)
84                         return qfalse;
85
86                 c++;
87
88                 sscanf(c, "%f %f %f", point[n].p, point[n].p+1, point[n].p+2);
89
90                 center.p[0] += point[n].p[0];
91                 center.p[1] += point[n].p[1];
92                 center.p[2] += point[n].p[2];
93
94                 if(n == 0)
95                 {
96                         for(i = 0; i < 3; i++)
97                         {
98                                 min[i] = point[n].p[i];
99                                 max[i] = point[n].p[i];
100                         }
101                 }
102                 else
103                 {
104                         for(i = 0; i < 3; i++)
105                         {
106                                 if(min[i] > point[n].p[i])
107                                         min[i] = point[n].p[i];
108                                 if(max[i] < point[n].p[i])
109                                         max[i] = point[n].p[i];
110                         }
111                 }
112         }
113
114         center.p[0] /= (float)point_count;
115         center.p[1] /= (float)point_count;
116         center.p[2] /= (float)point_count;
117
118         for(n = 0; n < point_count; n++)
119         {
120                 inner_point[n].p[0] = (0.01f * center.p[0]) + (0.99f * point[n].p[0]);
121                 inner_point[n].p[1] = (0.01f * center.p[1]) + (0.99f * point[n].p[1]);
122                 inner_point[n].p[2] = (0.01f * center.p[2]) + (0.99f * point[n].p[2]);
123         }
124
125         fp_color_random[0] = (float)(rand() & 0xff) / 255.0f;
126         fp_color_random[1] = (float)(rand() & 0xff) / 255.0f;
127         fp_color_random[2] = (float)(rand() & 0xff) / 255.0f;
128         fp_color_random[3] = 1.0f;
129
130         return qtrue;
131 }
132
133 CPortals::CPortals()
134 {
135         memset(this, 0, sizeof(CPortals));
136 }
137
138 CPortals::~CPortals()
139 {
140         Purge();
141 }
142
143 void CPortals::Purge()
144 {
145         delete[] portal;
146         delete[] portal_sort;
147         portal = NULL;
148         portal_sort = NULL;
149         portal_count = 0;
150
151         /*
152         delete[] node;
153         node = NULL;
154         node_count = 0;
155         */
156 }
157
158 void CPortals::Load()
159 {
160         char buf[LINE_BUF+1];
161
162         memset(buf, 0, LINE_BUF + 1);
163         
164         Purge();
165
166         Sys_Printf(MSG_PREFIX "Loading portal file %s.\n", fn);
167
168         FILE *in;
169
170         in = fopen(fn, "rt");
171
172         if(in == NULL)
173         {
174                 Sys_Printf("  ERROR - could not open file.\n");
175
176                 return;
177         }
178
179         if(!fgets(buf, LINE_BUF, in))
180         {
181                 fclose(in);
182
183                 Sys_Printf("  ERROR - File ended prematurely.\n");
184
185                 return;
186         }
187
188         if(strncmp("PRT1", buf, 4) != 0)
189         {
190                 fclose(in);
191
192                 Sys_Printf("  ERROR - File header indicates wrong file type (should be \"PRT1\").\n");
193
194                 return;
195         }
196
197         if(!fgets(buf, LINE_BUF, in))
198         {
199                 fclose(in);
200
201                 Sys_Printf("  ERROR - File ended prematurely.\n");
202
203                 return;
204         }
205
206         sscanf(buf, "%u", &node_count);
207 /*
208         if(node_count > 0xFFFF)
209         {
210                 fclose(in);
211
212                 node_count = 0;
213
214                 Sys_Printf("  ERROR - Extreme number of nodes, aborting.\n");
215
216                 return;
217         }
218         */
219
220         if(!fgets(buf, LINE_BUF, in))
221         {
222                 fclose(in);
223
224                 node_count = 0;
225
226                 Sys_Printf("  ERROR - File ended prematurely.\n");
227
228                 return;
229         }
230
231         sscanf(buf, "%u", &portal_count);
232
233         if(portal_count > 0xFFFF)
234         {
235                 fclose(in);
236
237                 portal_count = 0;
238                 node_count = 0;
239
240                 Sys_Printf("  ERROR - Extreme number of portals, aborting.\n");
241
242                 return;
243         }
244
245         if(portal_count < 0)
246         {
247                 fclose(in);
248
249                 portal_count = 0;
250                 node_count = 0;
251
252                 Sys_Printf("  ERROR - number of portals equals 0, aborting.\n");
253
254                 return;
255         }
256
257 //      node = new CBspNode[node_count];
258         portal = new CBspPortal[portal_count];
259         portal_sort = new int[portal_count];
260
261         unsigned int n;
262         bool first = qtrue;
263         unsigned test_vals_1, test_vals_2;
264
265         hint_flags = qfalse;
266
267         for(n = 0; n < portal_count; )
268         {
269                 if(!fgets(buf, LINE_BUF, in))
270                 {
271                         fclose(in);
272
273                         Purge();
274
275                         Sys_Printf("  ERROR - Could not find information for portal number %d of %d.\n", n + 1, portal_count);
276
277                         return;
278                 }
279
280                 if(!portal[n].Build(buf))
281                 {
282                         if(first && sscanf(buf, "%d %d", &test_vals_1, &test_vals_2) == 1) // skip additional counts of later data, not needed
283                         {
284                                 // We can count on hint flags being in the file
285                                 hint_flags = qtrue;
286                                 continue;
287                         }
288
289                         first = qfalse;
290
291                         fclose(in);
292
293                         Purge();
294
295                         Sys_Printf("  ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, portal_count);
296
297                         return;
298                 }
299
300                 n++;
301         }
302
303         fclose(in);
304
305         Sys_Printf("  %u portals read in.\n", node_count, portal_count);
306 }
307
308 void CPortals::FixColors()
309 {
310         fp_color_2d[0] = (float)GetRValue(color_2d) / 255.0f;
311         fp_color_2d[1] = (float)GetGValue(color_2d) / 255.0f;
312         fp_color_2d[2] = (float)GetBValue(color_2d) / 255.0f;
313         fp_color_2d[3] = 1.0f;
314
315         fp_color_3d[0] = (float)GetRValue(color_3d) / 255.0f;
316         fp_color_3d[1] = (float)GetGValue(color_3d) / 255.0f;
317         fp_color_3d[2] = (float)GetBValue(color_3d) / 255.0f;
318         fp_color_3d[3] = 1.0f;
319
320         fp_color_fog[0] = 0.0f;//(float)GetRValue(color_fog) / 255.0f;
321         fp_color_fog[1] = 0.0f;//(float)GetGValue(color_fog) / 255.0f;
322         fp_color_fog[2] = 0.0f;//(float)GetBValue(color_fog) / 255.0f;
323         fp_color_fog[3] = 1.0f;
324 }
325
326 CPortalsRender::CPortalsRender()
327 {
328         refCount = 1;
329 }
330
331 CPortalsRender::~CPortalsRender()
332 {
333 }
334
335 void CPortalsRender::Register()
336 {
337         g_QglTable.m_pfnHookGL2DWindow( this );
338         g_QglTable.m_pfnHookGL3DWindow( this );
339 }
340
341 void CPortalsRender::Draw2D( VIEWTYPE vt )
342 {
343         if(!portals.show_2d || portals.portal_count < 1)
344                 return;
345
346         g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
347
348         if(portals.aa_2d)
349         {
350                 g_QglTable.m_pfn_qglEnable(GL_BLEND);
351                 g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);
352         }
353         else
354         {
355                 g_QglTable.m_pfn_qglDisable(GL_BLEND);
356                 g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);
357         }
358
359         switch(vt)
360         {
361         case XY:
362                 break;
363         case XZ:
364                 g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
365                 break;
366         case YZ:
367                 g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
368                 g_QglTable.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);
369                 break;
370         }
371
372         g_QglTable.m_pfn_qglLineWidth(portals.width_2d * 0.5f);
373
374         g_QglTable.m_pfn_qglColor4fv(portals.fp_color_2d);
375
376         unsigned int n, p;
377
378         for(n = 0; n < portals.portal_count; n++)
379         {
380                 g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
381
382                 for(p = 0; p < portals.portal[n].point_count; p++)
383                         g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].point[p].p);
384
385                 g_QglTable.m_pfn_qglEnd();
386         }
387
388         g_QglTable.m_pfn_qglPopAttrib();
389 }
390
391 /*
392  * Transform a point (column vector) by a 4x4 matrix.  I.e.  out = m * in
393  * Input:  m - the 4x4 matrix
394  *         in - the 4x1 vector
395  * Output:  out - the resulting 4x1 vector.
396  */
397 static void transform_point( GLdouble out[4], const GLdouble m[16],
398                              const GLdouble in[4] )
399 {
400 #define M(row,col)  m[col*4+row]
401    out[0] = M(0,0) * in[0] + M(0,1) * in[1] + M(0,2) * in[2] + M(0,3) * in[3];
402    out[1] = M(1,0) * in[0] + M(1,1) * in[1] + M(1,2) * in[2] + M(1,3) * in[3];
403    out[2] = M(2,0) * in[0] + M(2,1) * in[1] + M(2,2) * in[2] + M(2,3) * in[3];
404    out[3] = M(3,0) * in[0] + M(3,1) * in[1] + M(3,2) * in[2] + M(3,3) * in[3];
405 #undef M
406 }
407
408 #include <math.h>
409
410
411 /*
412  * Perform a 4x4 matrix multiplication  (product = a x b).
413  * Input:  a, b - matrices to multiply
414  * Output:  product - product of a and b
415  */
416 static void matmul( GLdouble *product, const GLdouble *a, const GLdouble *b )
417 {
418    /* This matmul was contributed by Thomas Malik */
419    GLdouble temp[16];
420    GLint i;
421
422 #define A(row,col)  a[(col<<2)+row]
423 #define B(row,col)  b[(col<<2)+row]
424 #define T(row,col)  temp[(col<<2)+row]
425
426    /* i-te Zeile */
427    for (i = 0; i < 4; i++)
428      {
429         T(i, 0) = A(i, 0) * B(0, 0) + A(i, 1) * B(1, 0) + A(i, 2) * B(2, 0) + A(i, 3) * B(3, 0);
430         T(i, 1) = A(i, 0) * B(0, 1) + A(i, 1) * B(1, 1) + A(i, 2) * B(2, 1) + A(i, 3) * B(3, 1);
431         T(i, 2) = A(i, 0) * B(0, 2) + A(i, 1) * B(1, 2) + A(i, 2) * B(2, 2) + A(i, 3) * B(3, 2);
432         T(i, 3) = A(i, 0) * B(0, 3) + A(i, 1) * B(1, 3) + A(i, 2) * B(2, 3) + A(i, 3) * B(3, 3);
433      }
434
435 #undef A
436 #undef B
437 #undef T
438    memcpy ( product, temp, 16*sizeof(GLdouble) );
439 }
440
441
442
443 /*
444  * Compute inverse of 4x4 transformation matrix.
445  * Code contributed by Jacques Leroy jle@star.be
446  * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
447  */
448 static GLboolean invert_matrix( const GLdouble *m, GLdouble *out )
449 {
450 /* NB. OpenGL Matrices are COLUMN major. */
451 #define SWAP_ROWS(a, b) { GLdouble *_tmp = a; (a)=(b); (b)=_tmp; }
452 #define MAT(m,r,c) (m)[(c)*4+(r)]
453
454  GLdouble wtmp[4][8];
455  GLdouble m0, m1, m2, m3, s;
456  GLdouble *r0, *r1, *r2, *r3;
457
458  r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
459
460  r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
461  r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
462  r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
463
464  r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
465  r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
466  r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
467
468  r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
469  r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
470  r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
471
472  r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
473  r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
474  r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
475
476  /* choose pivot - or die */
477  if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
478  if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
479  if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
480  if (0.0 == r0[0])  return GL_FALSE;
481
482  /* eliminate first variable     */
483  m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
484  s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
485  s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
486  s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
487  s = r0[4];
488  if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
489  s = r0[5];
490  if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
491  s = r0[6];
492  if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
493  s = r0[7];
494  if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
495
496  /* choose pivot - or die */
497  if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
498  if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
499  if (0.0 == r1[1])  return GL_FALSE;
500
501  /* eliminate second variable */
502  m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
503  r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
504  r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
505  s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
506  s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
507  s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
508  s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
509
510  /* choose pivot - or die */
511  if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
512  if (0.0 == r2[2])  return GL_FALSE;
513
514  /* eliminate third variable */
515  m3 = r3[2]/r2[2];
516  r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
517  r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
518  r3[7] -= m3 * r2[7];
519
520  /* last check */
521  if (0.0 == r3[3]) return GL_FALSE;
522
523  s = 1.0/r3[3];              /* now back substitute row 3 */
524  r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
525
526  m2 = r2[3];                 /* now back substitute row 2 */
527  s  = 1.0/r2[2];
528  r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
529  r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
530  m1 = r1[3];
531  r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
532  r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
533  m0 = r0[3];
534  r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
535  r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
536
537  m1 = r1[2];                 /* now back substitute row 1 */
538  s  = 1.0/r1[1];
539  r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
540  r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
541  m0 = r0[2];
542  r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
543  r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
544
545  m0 = r0[1];                 /* now back substitute row 0 */
546  s  = 1.0/r0[0];
547  r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
548  r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
549
550  MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
551  MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
552  MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
553  MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
554  MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
555  MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
556  MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
557  MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; 
558
559  return GL_TRUE;
560
561 #undef MAT
562 #undef SWAP_ROWS
563 }
564
565 GLint UnProject(GLdouble winx,GLdouble winy,GLdouble winz,
566                 const GLdouble model[16],const GLdouble proj[16],
567                 const GLint viewport[4],
568                 GLdouble *objx,GLdouble *objy,GLdouble *objz)
569 {
570     /* matrice de transformation */
571     GLdouble m[16], A[16];
572     GLdouble in[4],out[4];
573
574     /* transformation coordonnees normalisees entre -1 et 1 */
575     in[0]=(winx-viewport[0])*2/viewport[2] - 1.0;
576     in[1]=(winy-viewport[1])*2/viewport[3] - 1.0;
577     in[2]=2*winz - 1.0;
578     in[3]=1.0;
579
580     /* calcul transformation inverse */
581     matmul(A,proj,model);
582     invert_matrix(A,m);
583
584     /* d'ou les coordonnees objets */
585     transform_point(out,m,in);
586     if (out[3]==0.0)
587        return GL_FALSE;
588     *objx=out[0]/out[3];
589     *objy=out[1]/out[3];
590     *objz=out[2]/out[3];
591     return GL_TRUE;
592 }
593
594 void CPortalsRender::Draw3D()
595 {
596         if(!portals.show_3d || portals.portal_count < 1)
597                 return;
598
599         g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
600
601         double cam[3];
602         double proj_m[16];
603         double model_m[16];
604         float min_check[3];
605         float max_check[3];
606         float trans = (100.0f - portals.trans_3d) / 100.0f;
607         int view[4];
608
609         g_QglTable.m_pfn_qglGetDoublev(GL_PROJECTION_MATRIX, proj_m);
610         g_QglTable.m_pfn_qglGetDoublev(GL_MODELVIEW_MATRIX, model_m);
611         g_QglTable.m_pfn_qglGetIntegerv(GL_VIEWPORT, view);
612
613         UnProject(0.5 * (double)view[2], 0.5 * (double)view[3], 0.0, model_m, proj_m, view, cam, cam+1, cam+2);
614
615         min_check[0] = (float)cam[0] + (portals.clip_range * 64.0f);
616         min_check[1] = (float)cam[1] + (portals.clip_range * 64.0f);
617         min_check[2] = (float)cam[2] + (portals.clip_range * 64.0f);
618         max_check[0] = (float)cam[0] - (portals.clip_range * 64.0f);
619         max_check[1] = (float)cam[1] - (portals.clip_range * 64.0f);
620         max_check[2] = (float)cam[2] - (portals.clip_range * 64.0f);
621
622         g_QglTable.m_pfn_qglHint(GL_FOG_HINT, GL_NICEST);
623         
624         g_QglTable.m_pfn_qglDisable(GL_CULL_FACE);
625
626         g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);
627         g_QglTable.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
628
629         g_QglTable.m_pfn_qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
630
631         g_QglTable.m_pfn_qglShadeModel(GL_SMOOTH);
632
633         g_QglTable.m_pfn_qglEnable(GL_BLEND);
634         g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
635         g_QglTable.m_pfn_qglEnable(GL_POLYGON_SMOOTH);
636
637         if(portals.aa_3d)
638                 g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);
639         else
640                 g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);
641
642         if(portals.fog)
643         {
644                 g_QglTable.m_pfn_qglEnable(GL_FOG);
645
646                 g_QglTable.m_pfn_qglFogi(GL_FOG_MODE, GL_EXP);
647                 g_QglTable.m_pfn_qglFogf(GL_FOG_DENSITY, 0.001f);
648                 g_QglTable.m_pfn_qglFogf(GL_FOG_START, 10.0f);
649                 g_QglTable.m_pfn_qglFogf(GL_FOG_END, 10000.0f);
650                 g_QglTable.m_pfn_qglFogi(GL_FOG_INDEX, 0);
651                 g_QglTable.m_pfn_qglFogfv(GL_FOG_COLOR, portals.fp_color_fog);
652         }
653         else
654         {
655                 g_QglTable.m_pfn_qglDisable(GL_FOG);
656         }
657
658         switch(portals.zbuffer)
659         {
660         case 1:
661                 g_QglTable.m_pfn_qglEnable(GL_DEPTH_TEST);
662                 g_QglTable.m_pfn_qglDepthMask(GL_FALSE);
663                 break;
664         case 2:
665                 g_QglTable.m_pfn_qglDisable(GL_DEPTH_TEST);
666                 break;
667         default:
668                 g_QglTable.m_pfn_qglEnable(GL_DEPTH_TEST);
669                 g_QglTable.m_pfn_qglDepthMask(GL_TRUE);
670         }
671
672         g_QglTable.m_pfn_qglLineWidth(portals.width_3d * 0.5f);
673
674         unsigned int n, p;
675
676         if(portals.polygons)
677         {
678                 if(portals.zbuffer != 0)
679                 {
680                         float d;
681
682                         for(n = 0; n < portals.portal_count; n++)
683                         {
684                                 d = (float)cam[0] - portals.portal[n].center.p[0];
685                                 portals.portal[n].dist = d * d;
686
687                                 d = (float)cam[1] - portals.portal[n].center.p[1];
688                                 portals.portal[n].dist += d * d;
689
690                                 d = (float)cam[2] - portals.portal[n].center.p[2];
691                                 portals.portal[n].dist += d * d;
692
693                                 portals.portal_sort[n] = n;
694                         }
695
696                         qsort(portals.portal_sort, portals.portal_count, 4, compare);
697                                         
698                         for(n = 0; n < portals.portal_count; n++)
699                         {
700                                 if(portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint)
701                                         continue;
702         
703                                 if(portals.clip)
704                                 {
705                                         if(min_check[0] < portals.portal[portals.portal_sort[n]].min[0])
706                                                 continue;
707                                         else if(min_check[1] < portals.portal[portals.portal_sort[n]].min[1])
708                                                 continue;
709                                         else if(min_check[2] < portals.portal[portals.portal_sort[n]].min[2])
710                                                 continue;
711                                         else if(max_check[0] > portals.portal[portals.portal_sort[n]].max[0])
712                                                 continue;
713                                         else if(max_check[1] > portals.portal[portals.portal_sort[n]].max[1])
714                                                 continue;
715                                         else if(max_check[2] > portals.portal[portals.portal_sort[n]].max[2])
716                                                 continue;
717                                 }
718
719                                 g_QglTable.m_pfn_qglColor4f(portals.portal[portals.portal_sort[n]].fp_color_random[0], portals.portal[portals.portal_sort[n]].fp_color_random[1],
720                                         portals.portal[portals.portal_sort[n]].fp_color_random[2], trans);
721
722                                 g_QglTable.m_pfn_qglBegin(GL_POLYGON);
723
724                                         for(p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++)
725                                                 g_QglTable.m_pfn_qglVertex3fv(portals.portal[portals.portal_sort[n]].point[p].p);
726
727                                 g_QglTable.m_pfn_qglEnd();
728                         }
729                 }
730                 else
731                 {
732                         for(n = 0; n < portals.portal_count; n++)
733                         {
734                                 if(portals.polygons == 2 && !portals.portal[n].hint)
735                                         continue;
736
737                                 if(portals.clip)
738                                 {
739                                         if(min_check[0] < portals.portal[n].min[0])
740                                                 continue;
741                                         else if(min_check[1] < portals.portal[n].min[1])
742                                                 continue;
743                                         else if(min_check[2] < portals.portal[n].min[2])
744                                                 continue;
745                                         else if(max_check[0] > portals.portal[n].max[0])
746                                                 continue;
747                                         else if(max_check[1] > portals.portal[n].max[1])
748                                                 continue;
749                                         else if(max_check[2] > portals.portal[n].max[2])
750                                                 continue;
751                                 }
752
753                                 g_QglTable.m_pfn_qglColor4f(portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
754                                         portals.portal[n].fp_color_random[2], trans);
755
756                                 g_QglTable.m_pfn_qglBegin(GL_POLYGON);
757
758                                         for(p = 0; p < portals.portal[n].point_count; p++)
759                                                 g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].point[p].p);
760
761                                 g_QglTable.m_pfn_qglEnd();
762                         }
763                 }
764         }
765
766         if(portals.lines)
767         {
768                 g_QglTable.m_pfn_qglColor4fv(portals.fp_color_3d);
769
770                 for(n = 0; n < portals.portal_count; n++)
771                 {
772                         if(portals.lines == 2 && !portals.portal[n].hint)
773                                 continue;
774
775                         if(portals.clip)
776                         {
777                                 if(min_check[0] < portals.portal[n].min[0])
778                                         continue;
779                                 else if(min_check[1] < portals.portal[n].min[1])
780                                         continue;
781                                 else if(min_check[2] < portals.portal[n].min[2])
782                                         continue;
783                                 else if(max_check[0] > portals.portal[n].max[0])
784                                         continue;
785                                 else if(max_check[1] > portals.portal[n].max[1])
786                                         continue;
787                                 else if(max_check[2] > portals.portal[n].max[2])
788                                         continue;
789                         }
790
791                         g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
792
793                         for(p = 0; p < portals.portal[n].point_count; p++)
794                                 g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].inner_point[p].p);
795
796                         g_QglTable.m_pfn_qglEnd();
797                 }
798         }
799
800         g_QglTable.m_pfn_qglPopAttrib();
801 }
802