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