]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/prtview/portals.cpp
Remove -Wno-sign-compare
[xonotic/netradiant.git] / contrib / prtview / portals.cpp
index 288a6c2cdd2972e8b70521ba207380c881387066..0522fe40e3e3b0c873517472bb7f4a3ac75d0566 100644 (file)
@@ -17,7 +17,7 @@
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "stdafx.h"
+#include "portals.h"
 #include <string.h>
 #include <stdlib.h>
 #ifndef __APPLE__
 #endif
 #include <stdio.h>
 
+#include "iglrender.h"
+#include "cullable.h"
+
+#include "prtview.h"
+
 #define LINE_BUF 1000
 
 CPortals portals;
@@ -52,7 +57,7 @@ CBspPortal::~CBspPortal(){
        delete[] inner_point;
 }
 
-qboolean CBspPortal::Build( char *def ){
+bool CBspPortal::Build( char *def ){
        char *c = def;
        unsigned int n;
        int dummy1, dummy2;
@@ -64,11 +69,11 @@ qboolean CBspPortal::Build( char *def ){
        else
        {
                sscanf( def, "%u", &point_count );
-               hint = FALSE;
+               hint = false;
        }
 
        if ( point_count < 3 || ( portals.hint_flags && res_cnt < 4 ) ) {
-               return FALSE;
+               return false;
        }
 
        point = new CBspPoint[point_count];
@@ -79,7 +84,7 @@ qboolean CBspPortal::Build( char *def ){
                for (; *c != 0 && *c != '('; c++ ) ;
 
                if ( *c == 0 ) {
-                       return FALSE;
+                       return false;
                }
 
                c++;
@@ -127,7 +132,7 @@ qboolean CBspPortal::Build( char *def ){
        fp_color_random[2] = (float)( rand() & 0xff ) / 255.0f;
        fp_color_random[3] = 1.0f;
 
-       return TRUE;
+       return true;
 }
 
 CPortals::CPortals(){
@@ -159,14 +164,14 @@ void CPortals::Load(){
 
        Purge();
 
-       Sys_Printf( MSG_PREFIX "Loading portal file %s.\n", fn );
+       globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
 
        FILE *in;
 
        in = fopen( fn, "rt" );
 
        if ( in == NULL ) {
-               Sys_Printf( "  ERROR - could not open file.\n" );
+               globalOutputStream() << "  ERROR - could not open file.\n";
 
                return;
        }
@@ -174,7 +179,7 @@ void CPortals::Load(){
        if ( !fgets( buf, LINE_BUF, in ) ) {
                fclose( in );
 
-               Sys_Printf( "  ERROR - File ended prematurely.\n" );
+               globalOutputStream() << "  ERROR - File ended prematurely.\n";
 
                return;
        }
@@ -182,7 +187,7 @@ void CPortals::Load(){
        if ( strncmp( "PRT1", buf, 4 ) != 0 ) {
                fclose( in );
 
-               Sys_Printf( "  ERROR - File header indicates wrong file type (should be \"PRT1\").\n" );
+               globalOutputStream() << "  ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
 
                return;
        }
@@ -190,7 +195,7 @@ void CPortals::Load(){
        if ( !fgets( buf, LINE_BUF, in ) ) {
                fclose( in );
 
-               Sys_Printf( "  ERROR - File ended prematurely.\n" );
+               globalOutputStream() << "  ERROR - File ended prematurely.\n";
 
                return;
        }
@@ -203,7 +208,7 @@ void CPortals::Load(){
 
         node_count = 0;
 
-        Sys_Printf("  ERROR - Extreme number of nodes, aborting.\n");
+        globalOutputStream() << "  ERROR - Extreme number of nodes, aborting.\n";
 
         return;
     }
@@ -214,7 +219,7 @@ void CPortals::Load(){
 
                node_count = 0;
 
-               Sys_Printf( "  ERROR - File ended prematurely.\n" );
+               globalOutputStream() << "  ERROR - File ended prematurely.\n";
 
                return;
        }
@@ -227,18 +232,18 @@ void CPortals::Load(){
                portal_count = 0;
                node_count = 0;
 
-               Sys_Printf( "  ERROR - Extreme number of portals, aborting.\n" );
+               globalOutputStream() << "  ERROR - Extreme number of portals, aborting.\n";
 
                return;
        }
 
-       if ( portal_count < 0 ) {
+       if ( portal_count == 0 ) {
                fclose( in );
 
                portal_count = 0;
                node_count = 0;
 
-               Sys_Printf( "  ERROR - number of portals equals 0, aborting.\n" );
+               globalOutputStream() << "  ERROR - number of portals equals 0, aborting.\n";
 
                return;
        }
@@ -248,10 +253,10 @@ void CPortals::Load(){
        portal_sort = new int[portal_count];
 
        unsigned int n;
-       qboolean first = TRUE;
+       bool first = true;
        unsigned test_vals_1, test_vals_2;
 
-       hint_flags = FALSE;
+       hint_flags = false;
 
        for ( n = 0; n < portal_count; )
        {
@@ -260,25 +265,25 @@ void CPortals::Load(){
 
                        Purge();
 
-                       Sys_Printf( "  ERROR - Could not find information for portal number %d of %d.\n", n + 1, portal_count );
+                       globalOutputStream() << "  ERROR - Could not find information for portal number " << n + 1 << " of " << portal_count << ".\n";
 
                        return;
                }
 
                if ( !portal[n].Build( buf ) ) {
-                       if ( first && sscanf( buf, "%d %d", &test_vals_1, &test_vals_2 ) == 1 ) { // skip additional counts of later data, not needed
+                       if ( first && sscanf( buf, "%d %d", (int *) &test_vals_1, (int *) &test_vals_2 ) == 1 ) { // skip additional counts of later data, not needed
                                // We can count on hint flags being in the file
-                               hint_flags = TRUE;
+                               hint_flags = true;
                                continue;
                        }
 
-                       first = FALSE;
+                       first = false;
 
                        fclose( in );
 
                        Purge();
 
-                       Sys_Printf( "  ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, portal_count );
+                       globalOutputStream() << "  ERROR - Information for portal number " << n + 1 << " of " << portal_count << " is not formatted correctly.\n";
 
                        return;
                }
@@ -288,544 +293,348 @@ void CPortals::Load(){
 
        fclose( in );
 
-       Sys_Printf( "  %u portals read in.\n", node_count, portal_count );
-}
-
-void CPortals::FixColors(){
-       fp_color_2d[0] = (float)GetRValue( color_2d ) / 255.0f;
-       fp_color_2d[1] = (float)GetGValue( color_2d ) / 255.0f;
-       fp_color_2d[2] = (float)GetBValue( color_2d ) / 255.0f;
-       fp_color_2d[3] = 1.0f;
-
-       fp_color_3d[0] = (float)GetRValue( color_3d ) / 255.0f;
-       fp_color_3d[1] = (float)GetGValue( color_3d ) / 255.0f;
-       fp_color_3d[2] = (float)GetBValue( color_3d ) / 255.0f;
-       fp_color_3d[3] = 1.0f;
-
-       fp_color_fog[0] = 0.0f; //(float)GetRValue(color_fog) / 255.0f;
-       fp_color_fog[1] = 0.0f; //(float)GetGValue(color_fog) / 255.0f;
-       fp_color_fog[2] = 0.0f; //(float)GetBValue(color_fog) / 255.0f;
-       fp_color_fog[3] = 1.0f;
-}
-
-CPortalsRender::CPortalsRender(){
-       refCount = 1;
-}
-
-CPortalsRender::~CPortalsRender(){
-}
-
-void CPortalsRender::Register(){
-       g_QglTable.m_pfnHookGL2DWindow( this );
-       g_QglTable.m_pfnHookGL3DWindow( this );
+       globalOutputStream() << "  " << node_count << " portals read in.\n";
 }
 
-void CPortalsRender::Draw2D( VIEWTYPE vt ){
-       if ( !portals.show_2d || portals.portal_count < 1 ) {
-               return;
+#include "math/matrix.h"
+
+const char* g_state_solid = "$plugins/prtview/solid";
+const char* g_state_solid_outline = "$plugins/prtview/solid_outline";
+const char* g_state_wireframe = "$plugins/prtview/wireframe";
+Shader* g_shader_solid = 0;
+Shader* g_shader_solid_outline = 0;
+Shader* g_shader_wireframe = 0;
+
+void Portals_constructShaders(){
+       OpenGLState state;
+       GlobalOpenGLStateLibrary().getDefaultState( state );
+       state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE;
+       state.m_sort = OpenGLState::eSortOverlayFirst;
+       state.m_linewidth = portals.width_2d * 0.5f;
+       state.m_colour[0] = portals.fp_color_2d[0];
+       state.m_colour[1] = portals.fp_color_2d[1];
+       state.m_colour[2] = portals.fp_color_2d[2];
+       state.m_colour[3] = portals.fp_color_2d[3];
+       if ( portals.aa_2d ) {
+               state.m_state |= RENDER_BLEND | RENDER_LINESMOOTH;
        }
+       GlobalOpenGLStateLibrary().insert( g_state_wireframe, state );
 
-       g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
+       GlobalOpenGLStateLibrary().getDefaultState( state );
+       state.m_state = RENDER_FILL | RENDER_BLEND | RENDER_COLOURWRITE | RENDER_COLOURCHANGE | RENDER_SMOOTH;
 
-       if ( portals.aa_2d ) {
-               g_QglTable.m_pfn_qglEnable( GL_BLEND );
-               g_QglTable.m_pfn_qglEnable( GL_LINE_SMOOTH );
-       }
-       else
-       {
-               g_QglTable.m_pfn_qglDisable( GL_BLEND );
-               g_QglTable.m_pfn_qglEnable( GL_LINE_SMOOTH );
+       if ( portals.aa_3d ) {
+               state.m_state |= RENDER_POLYGONSMOOTH;
        }
 
-       switch ( vt )
+       switch ( portals.zbuffer )
        {
-       case XY:
-               break;
-       case XZ:
-               g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
+       case 1:
+               state.m_state |= RENDER_DEPTHTEST;
                break;
-       case YZ:
-               g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
-               g_QglTable.m_pfn_qglRotatef( 270.0f, 0.0f, 0.0f, 1.0f );
+       case 2:
                break;
+       default:
+               state.m_state |= RENDER_DEPTHTEST;
+               state.m_state |= RENDER_DEPTHWRITE;
        }
 
-       g_QglTable.m_pfn_qglLineWidth( portals.width_2d * 0.5f );
-
-       g_QglTable.m_pfn_qglColor4fv( portals.fp_color_2d );
+       if ( portals.fog ) {
+               state.m_state |= RENDER_FOG;
+
+               state.m_fog.mode = GL_EXP;
+               state.m_fog.density = 0.001f;
+               state.m_fog.start = 10.0f;
+               state.m_fog.end = 10000.0f;
+               state.m_fog.index = 0;
+               state.m_fog.colour[0] = portals.fp_color_fog[0];
+               state.m_fog.colour[1] = portals.fp_color_fog[1];
+               state.m_fog.colour[2] = portals.fp_color_fog[2];
+               state.m_fog.colour[3] = portals.fp_color_fog[3];
+       }
+
+       GlobalOpenGLStateLibrary().insert( g_state_solid, state );
+
+       GlobalOpenGLStateLibrary().getDefaultState( state );
+       state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE;
+       state.m_sort = OpenGLState::eSortOverlayFirst;
+       state.m_linewidth = portals.width_3d * 0.5f;
+       state.m_colour[0] = portals.fp_color_3d[0];
+       state.m_colour[1] = portals.fp_color_3d[1];
+       state.m_colour[2] = portals.fp_color_3d[2];
+       state.m_colour[3] = portals.fp_color_3d[3];
 
-       unsigned int n, p;
+       if ( portals.aa_3d ) {
+               state.m_state |= RENDER_LINESMOOTH;
+       }
 
-       for ( n = 0; n < portals.portal_count; n++ )
+       switch ( portals.zbuffer )
        {
-               g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
+       case 1:
+               state.m_state |= RENDER_DEPTHTEST;
+               break;
+       case 2:
+               break;
+       default:
+               state.m_state |= RENDER_DEPTHTEST;
+               state.m_state |= RENDER_DEPTHWRITE;
+       }
 
-               for ( p = 0; p < portals.portal[n].point_count; p++ )
-                       g_QglTable.m_pfn_qglVertex3fv( portals.portal[n].point[p].p );
+       if ( portals.fog ) {
+               state.m_state |= RENDER_FOG;
 
-               g_QglTable.m_pfn_qglEnd();
+               state.m_fog.mode = GL_EXP;
+               state.m_fog.density = 0.001f;
+               state.m_fog.start = 10.0f;
+               state.m_fog.end = 10000.0f;
+               state.m_fog.index = 0;
+               state.m_fog.colour[0] = portals.fp_color_fog[0];
+               state.m_fog.colour[1] = portals.fp_color_fog[1];
+               state.m_fog.colour[2] = portals.fp_color_fog[2];
+               state.m_fog.colour[3] = portals.fp_color_fog[3];
        }
 
-       g_QglTable.m_pfn_qglPopAttrib();
-}
+       GlobalOpenGLStateLibrary().insert( g_state_solid_outline, state );
 
-/*
- * Transform a point (column vector) by a 4x4 matrix.  I.e.  out = m * in
- * Input:  m - the 4x4 matrix
- *         in - the 4x1 vector
- * Output:  out - the resulting 4x1 vector.
- */
-static void transform_point( GLdouble out[4], const GLdouble m[16],
-                                                        const GLdouble in[4] ){
-#define M( row,col )  m[col * 4 + row]
-       out[0] = M( 0,0 ) * in[0] + M( 0,1 ) * in[1] + M( 0,2 ) * in[2] + M( 0,3 ) * in[3];
-       out[1] = M( 1,0 ) * in[0] + M( 1,1 ) * in[1] + M( 1,2 ) * in[2] + M( 1,3 ) * in[3];
-       out[2] = M( 2,0 ) * in[0] + M( 2,1 ) * in[1] + M( 2,2 ) * in[2] + M( 2,3 ) * in[3];
-       out[3] = M( 3,0 ) * in[0] + M( 3,1 ) * in[1] + M( 3,2 ) * in[2] + M( 3,3 ) * in[3];
-#undef M
+       g_shader_solid = GlobalShaderCache().capture( g_state_solid );
+       g_shader_solid_outline = GlobalShaderCache().capture( g_state_solid_outline );
+       g_shader_wireframe = GlobalShaderCache().capture( g_state_wireframe );
 }
 
-#include <math.h>
-
-
-/*
- * Perform a 4x4 matrix multiplication  (product = a x b).
- * Input:  a, b - matrices to multiply
- * Output:  product - product of a and b
- */
-static void matmul( GLdouble *product, const GLdouble *a, const GLdouble *b ){
-       /* This matmul was contributed by Thomas Malik */
-       GLdouble temp[16];
-       GLint i;
-
-#define A( row,col )  a[( col << 2 ) + row]
-#define B( row,col )  b[( col << 2 ) + row]
-#define T( row,col )  temp[( col << 2 ) + row]
-
-       /* i-te Zeile */
-       for ( i = 0; i < 4; i++ )
-       {
-               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 );
-               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 );
-               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 );
-               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 );
-       }
-
-#undef A
-#undef B
-#undef T
-       memcpy( product, temp, 16 * sizeof( GLdouble ) );
+void Portals_destroyShaders(){
+       GlobalShaderCache().release( g_state_solid );
+       GlobalShaderCache().release( g_state_solid_outline );
+       GlobalShaderCache().release( g_state_wireframe );
+       GlobalOpenGLStateLibrary().erase( g_state_solid );
+       GlobalOpenGLStateLibrary().erase( g_state_solid_outline );
+       GlobalOpenGLStateLibrary().erase( g_state_wireframe );
 }
 
+void Portals_shadersChanged(){
+       Portals_destroyShaders();
+       portals.FixColors();
+       Portals_constructShaders();
+}
 
+void CPortals::FixColors(){
+       fp_color_2d[0] = (float)GetRValue( color_2d ) / 255.0f;
+       fp_color_2d[1] = (float)GetGValue( color_2d ) / 255.0f;
+       fp_color_2d[2] = (float)GetBValue( color_2d ) / 255.0f;
+       fp_color_2d[3] = 1.0f;
 
-/*
- * Compute inverse of 4x4 transformation matrix.
- * Code contributed by Jacques Leroy jle@star.be
- * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
- */
-static GLboolean invert_matrix( const GLdouble *m, GLdouble *out ){
-/* NB. OpenGL Matrices are COLUMN major. */
-#define SWAP_ROWS( a, b ) { GLdouble *_tmp = a; ( a ) = ( b ); ( b ) = _tmp; }
-#define MAT( m,r,c ) ( m )[( c ) * 4 + ( r )]
-
-       GLdouble wtmp[4][8];
-       GLdouble m0, m1, m2, m3, s;
-       GLdouble *r0, *r1, *r2, *r3;
-
-       r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
-
-       r0[0] = MAT( m,0,0 ), r0[1] = MAT( m,0,1 ),
-       r0[2] = MAT( m,0,2 ), r0[3] = MAT( m,0,3 ),
-       r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
-
-       r1[0] = MAT( m,1,0 ), r1[1] = MAT( m,1,1 ),
-       r1[2] = MAT( m,1,2 ), r1[3] = MAT( m,1,3 ),
-       r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
-
-       r2[0] = MAT( m,2,0 ), r2[1] = MAT( m,2,1 ),
-       r2[2] = MAT( m,2,2 ), r2[3] = MAT( m,2,3 ),
-       r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
+       fp_color_3d[0] = (float)GetRValue( color_3d ) / 255.0f;
+       fp_color_3d[1] = (float)GetGValue( color_3d ) / 255.0f;
+       fp_color_3d[2] = (float)GetBValue( color_3d ) / 255.0f;
+       fp_color_3d[3] = 1.0f;
 
-       r3[0] = MAT( m,3,0 ), r3[1] = MAT( m,3,1 ),
-       r3[2] = MAT( m,3,2 ), r3[3] = MAT( m,3,3 ),
-       r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
+       fp_color_fog[0] = 0.0f; //(float)GetRValue(color_fog) / 255.0f;
+       fp_color_fog[1] = 0.0f; //(float)GetGValue(color_fog) / 255.0f;
+       fp_color_fog[2] = 0.0f; //(float)GetBValue(color_fog) / 255.0f;
+       fp_color_fog[3] = 1.0f;
+}
 
-       /* choose pivot - or die */
-       if ( fabs( r3[0] ) > fabs( r2[0] ) ) {
-               SWAP_ROWS( r3, r2 );
-       }
-       if ( fabs( r2[0] ) > fabs( r1[0] ) ) {
-               SWAP_ROWS( r2, r1 );
-       }
-       if ( fabs( r1[0] ) > fabs( r0[0] ) ) {
-               SWAP_ROWS( r1, r0 );
-       }
-       if ( 0.0 == r0[0] ) {
-               return GL_FALSE;
+void CPortalsRender::renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
+       if ( !portals.show_2d || portals.portal_count < 1 ) {
+               return;
        }
 
-       /* eliminate first variable     */
-       m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
-       s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
-       s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
-       s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
-       s = r0[4];
-       if ( s != 0.0 ) {
-               r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s;
-       }
-       s = r0[5];
-       if ( s != 0.0 ) {
-               r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s;
-       }
-       s = r0[6];
-       if ( s != 0.0 ) {
-               r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s;
-       }
-       s = r0[7];
-       if ( s != 0.0 ) {
-               r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s;
-       }
+       renderer.SetState( g_shader_wireframe, Renderer::eWireframeOnly );
 
-       /* choose pivot - or die */
-       if ( fabs( r3[1] ) > fabs( r2[1] ) ) {
-               SWAP_ROWS( r3, r2 );
-       }
-       if ( fabs( r2[1] ) > fabs( r1[1] ) ) {
-               SWAP_ROWS( r2, r1 );
-       }
-       if ( 0.0 == r1[1] ) {
-               return GL_FALSE;
-       }
+       renderer.addRenderable( m_drawWireframe, g_matrix4_identity );
+}
 
-       /* eliminate second variable */
-       m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
-       r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
-       r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
-       s = r1[4]; if ( 0.0 != s ) {
-               r2[4] -= m2 * s; r3[4] -= m3 * s;
-       }
-       s = r1[5]; if ( 0.0 != s ) {
-               r2[5] -= m2 * s; r3[5] -= m3 * s;
-       }
-       s = r1[6]; if ( 0.0 != s ) {
-               r2[6] -= m2 * s; r3[6] -= m3 * s;
-       }
-       s = r1[7]; if ( 0.0 != s ) {
-               r2[7] -= m2 * s; r3[7] -= m3 * s;
-       }
+void CPortalsDrawWireframe::render( RenderStateFlags state ) const {
+       unsigned int n, p;
 
-       /* choose pivot - or die */
-       if ( fabs( r3[2] ) > fabs( r2[2] ) ) {
-               SWAP_ROWS( r3, r2 );
-       }
-       if ( 0.0 == r2[2] ) {
-               return GL_FALSE;
-       }
+       for ( n = 0; n < portals.portal_count; n++ )
+       {
+               glBegin( GL_LINE_LOOP );
 
-       /* eliminate third variable */
-       m3 = r3[2] / r2[2];
-       r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
-       r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
-       r3[7] -= m3 * r2[7];
+               for ( p = 0; p < portals.portal[n].point_count; p++ )
+                       glVertex3fv( portals.portal[n].point[p].p );
 
-       /* last check */
-       if ( 0.0 == r3[3] ) {
-               return GL_FALSE;
+               glEnd();
        }
-
-       s = 1.0 / r3[3];         /* now back substitute row 3 */
-       r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
-
-       m2 = r2[3];              /* now back substitute row 2 */
-       s  = 1.0 / r2[2];
-       r2[4] = s * ( r2[4] - r3[4] * m2 ), r2[5] = s * ( r2[5] - r3[5] * m2 ),
-       r2[6] = s * ( r2[6] - r3[6] * m2 ), r2[7] = s * ( r2[7] - r3[7] * m2 );
-       m1 = r1[3];
-       r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
-       r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
-       m0 = r0[3];
-       r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
-       r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
-
-       m1 = r1[2];              /* now back substitute row 1 */
-       s  = 1.0 / r1[1];
-       r1[4] = s * ( r1[4] - r2[4] * m1 ), r1[5] = s * ( r1[5] - r2[5] * m1 ),
-       r1[6] = s * ( r1[6] - r2[6] * m1 ), r1[7] = s * ( r1[7] - r2[7] * m1 );
-       m0 = r0[2];
-       r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
-       r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
-
-       m0 = r0[1];              /* now back substitute row 0 */
-       s  = 1.0 / r0[0];
-       r0[4] = s * ( r0[4] - r1[4] * m0 ), r0[5] = s * ( r0[5] - r1[5] * m0 ),
-       r0[6] = s * ( r0[6] - r1[6] * m0 ), r0[7] = s * ( r0[7] - r1[7] * m0 );
-
-       MAT( out,0,0 ) = r0[4]; MAT( out,0,1 ) = r0[5],
-       MAT( out,0,2 ) = r0[6]; MAT( out,0,3 ) = r0[7],
-       MAT( out,1,0 ) = r1[4]; MAT( out,1,1 ) = r1[5],
-       MAT( out,1,2 ) = r1[6]; MAT( out,1,3 ) = r1[7],
-       MAT( out,2,0 ) = r2[4]; MAT( out,2,1 ) = r2[5],
-       MAT( out,2,2 ) = r2[6]; MAT( out,2,3 ) = r2[7],
-       MAT( out,3,0 ) = r3[4]; MAT( out,3,1 ) = r3[5],
-       MAT( out,3,2 ) = r3[6]; MAT( out,3,3 ) = r3[7];
-
-       return GL_TRUE;
-
-#undef MAT
-#undef SWAP_ROWS
 }
 
-GLint UnProject( GLdouble winx,GLdouble winy,GLdouble winz,
-                                const GLdouble model[16],const GLdouble proj[16],
-                                const GLint viewport[4],
-                                GLdouble *objx,GLdouble *objy,GLdouble *objz ){
-       /* matrice de transformation */
-       GLdouble m[16], A[16];
-       GLdouble in[4],out[4];
-
-       /* transformation coordonnees normalisees entre -1 et 1 */
-       in[0] = ( winx - viewport[0] ) * 2 / viewport[2] - 1.0;
-       in[1] = ( winy - viewport[1] ) * 2 / viewport[3] - 1.0;
-       in[2] = 2 * winz - 1.0;
-       in[3] = 1.0;
-
-       /* calcul transformation inverse */
-       matmul( A,proj,model );
-       invert_matrix( A,m );
-
-       /* d'ou les coordonnees objets */
-       transform_point( out,m,in );
-       if ( out[3] == 0.0 ) {
-               return GL_FALSE;
-       }
-       *objx = out[0] / out[3];
-       *objy = out[1] / out[3];
-       *objz = out[2] / out[3];
-       return GL_TRUE;
+CubicClipVolume calculateCubicClipVolume( const Matrix4& viewproj ){
+       CubicClipVolume clip;
+       clip.cam = vector4_projected(
+               matrix4_transformed_vector4(
+                       matrix4_full_inverse( viewproj ),
+                       Vector4( 0, 0, -1, 1 )
+                       )
+               );
+       clip.min[0] = clip.cam[0] + ( portals.clip_range * 64.0f );
+       clip.min[1] = clip.cam[1] + ( portals.clip_range * 64.0f );
+       clip.min[2] = clip.cam[2] + ( portals.clip_range * 64.0f );
+       clip.max[0] = clip.cam[0] - ( portals.clip_range * 64.0f );
+       clip.max[1] = clip.cam[1] - ( portals.clip_range * 64.0f );
+       clip.max[2] = clip.cam[2] - ( portals.clip_range * 64.0f );
+       return clip;
 }
 
-void CPortalsRender::Draw3D(){
+void CPortalsRender::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
        if ( !portals.show_3d || portals.portal_count < 1 ) {
                return;
        }
 
-       g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
+       CubicClipVolume clip = calculateCubicClipVolume( matrix4_multiplied_by_matrix4( volume.GetProjection(), volume.GetModelview() ) );
 
-       double cam[3];
-       double proj_m[16];
-       double model_m[16];
-       float min_check[3];
-       float max_check[3];
-       float trans = ( 100.0f - portals.trans_3d ) / 100.0f;
-       int view[4];
-
-       g_QglTable.m_pfn_qglGetDoublev( GL_PROJECTION_MATRIX, proj_m );
-       g_QglTable.m_pfn_qglGetDoublev( GL_MODELVIEW_MATRIX, model_m );
-       g_QglTable.m_pfn_qglGetIntegerv( GL_VIEWPORT, view );
-
-       UnProject( 0.5 * (double)view[2], 0.5 * (double)view[3], 0.0, model_m, proj_m, view, cam, cam + 1, cam + 2 );
-
-       min_check[0] = (float)cam[0] + ( portals.clip_range * 64.0f );
-       min_check[1] = (float)cam[1] + ( portals.clip_range * 64.0f );
-       min_check[2] = (float)cam[2] + ( portals.clip_range * 64.0f );
-       max_check[0] = (float)cam[0] - ( portals.clip_range * 64.0f );
-       max_check[1] = (float)cam[1] - ( portals.clip_range * 64.0f );
-       max_check[2] = (float)cam[2] - ( portals.clip_range * 64.0f );
-
-       g_QglTable.m_pfn_qglHint( GL_FOG_HINT, GL_NICEST );
-
-       g_QglTable.m_pfn_qglDisable( GL_CULL_FACE );
-
-       g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
-       g_QglTable.m_pfn_qglDisable( GL_POLYGON_SMOOTH );
-
-       g_QglTable.m_pfn_qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
-
-       g_QglTable.m_pfn_qglShadeModel( GL_SMOOTH );
-
-       g_QglTable.m_pfn_qglEnable( GL_BLEND );
-       g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
-       g_QglTable.m_pfn_qglEnable( GL_POLYGON_SMOOTH );
+       if ( portals.polygons ) {
+               renderer.SetState( g_shader_solid, Renderer::eWireframeOnly );
+               renderer.SetState( g_shader_solid, Renderer::eFullMaterials );
 
-       if ( portals.aa_3d ) {
-               g_QglTable.m_pfn_qglEnable( GL_LINE_SMOOTH );
-       }
-       else{
-               g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
+               m_drawSolid.clip = clip;
+               renderer.addRenderable( m_drawSolid, g_matrix4_identity );
        }
 
-       if ( portals.fog ) {
-               g_QglTable.m_pfn_qglEnable( GL_FOG );
-
-               g_QglTable.m_pfn_qglFogi( GL_FOG_MODE, GL_EXP );
-               g_QglTable.m_pfn_qglFogf( GL_FOG_DENSITY, 0.001f );
-               g_QglTable.m_pfn_qglFogf( GL_FOG_START, 10.0f );
-               g_QglTable.m_pfn_qglFogf( GL_FOG_END, 10000.0f );
-               g_QglTable.m_pfn_qglFogi( GL_FOG_INDEX, 0 );
-               g_QglTable.m_pfn_qglFogfv( GL_FOG_COLOR, portals.fp_color_fog );
-       }
-       else
-       {
-               g_QglTable.m_pfn_qglDisable( GL_FOG );
-       }
+       if ( portals.lines ) {
+               renderer.SetState( g_shader_solid_outline, Renderer::eWireframeOnly );
+               renderer.SetState( g_shader_solid_outline, Renderer::eFullMaterials );
 
-       switch ( portals.zbuffer )
-       {
-       case 1:
-               g_QglTable.m_pfn_qglEnable( GL_DEPTH_TEST );
-               g_QglTable.m_pfn_qglDepthMask( GL_FALSE );
-               break;
-       case 2:
-               g_QglTable.m_pfn_qglDisable( GL_DEPTH_TEST );
-               break;
-       default:
-               g_QglTable.m_pfn_qglEnable( GL_DEPTH_TEST );
-               g_QglTable.m_pfn_qglDepthMask( GL_TRUE );
+               m_drawSolidOutline.clip = clip;
+               renderer.addRenderable( m_drawSolidOutline, g_matrix4_identity );
        }
+}
 
-       g_QglTable.m_pfn_qglLineWidth( portals.width_3d * 0.5f );
+void CPortalsDrawSolid::render( RenderStateFlags state ) const {
+       float trans = ( 100.0f - portals.trans_3d ) / 100.0f;
 
        unsigned int n, p;
 
-       if ( portals.polygons ) {
-               if ( portals.zbuffer != 0 ) {
-                       float d;
+       if ( portals.zbuffer != 0 ) {
+               float d;
 
-                       for ( n = 0; n < portals.portal_count; n++ )
-                       {
-                               d = (float)cam[0] - portals.portal[n].center.p[0];
-                               portals.portal[n].dist = d * d;
+               for ( n = 0; n < portals.portal_count; n++ )
+               {
+                       d = (float)clip.cam[0] - portals.portal[n].center.p[0];
+                       portals.portal[n].dist = d * d;
 
-                               d = (float)cam[1] - portals.portal[n].center.p[1];
-                               portals.portal[n].dist += d * d;
+                       d = (float)clip.cam[1] - portals.portal[n].center.p[1];
+                       portals.portal[n].dist += d * d;
 
-                               d = (float)cam[2] - portals.portal[n].center.p[2];
-                               portals.portal[n].dist += d * d;
+                       d = (float)clip.cam[2] - portals.portal[n].center.p[2];
+                       portals.portal[n].dist += d * d;
 
-                               portals.portal_sort[n] = n;
-                       }
+                       portals.portal_sort[n] = n;
+               }
 
-                       qsort( portals.portal_sort, portals.portal_count, 4, compare );
+               qsort( portals.portal_sort, portals.portal_count, 4, compare );
 
-                       for ( n = 0; n < portals.portal_count; n++ )
-                       {
-                               if ( portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint ) {
+               for ( n = 0; n < portals.portal_count; n++ )
+               {
+                       if ( portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint ) {
+                               continue;
+                       }
+
+                       if ( portals.clip ) {
+                               if ( clip.min[0] < portals.portal[portals.portal_sort[n]].min[0] ) {
                                        continue;
                                }
-
-                               if ( portals.clip ) {
-                                       if ( min_check[0] < portals.portal[portals.portal_sort[n]].min[0] ) {
-                                               continue;
-                                       }
-                                       else if ( min_check[1] < portals.portal[portals.portal_sort[n]].min[1] ) {
-                                               continue;
-                                       }
-                                       else if ( min_check[2] < portals.portal[portals.portal_sort[n]].min[2] ) {
-                                               continue;
-                                       }
-                                       else if ( max_check[0] > portals.portal[portals.portal_sort[n]].max[0] ) {
-                                               continue;
-                                       }
-                                       else if ( max_check[1] > portals.portal[portals.portal_sort[n]].max[1] ) {
-                                               continue;
-                                       }
-                                       else if ( max_check[2] > portals.portal[portals.portal_sort[n]].max[2] ) {
-                                               continue;
-                                       }
+                               else if ( clip.min[1] < portals.portal[portals.portal_sort[n]].min[1] ) {
+                                       continue;
                                }
-
-                               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],
-                                                                                        portals.portal[portals.portal_sort[n]].fp_color_random[2], trans );
-
-                               g_QglTable.m_pfn_qglBegin( GL_POLYGON );
-
-                               for ( p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++ )
-                                       g_QglTable.m_pfn_qglVertex3fv( portals.portal[portals.portal_sort[n]].point[p].p );
-
-                               g_QglTable.m_pfn_qglEnd();
-                       }
-               }
-               else
-               {
-                       for ( n = 0; n < portals.portal_count; n++ )
-                       {
-                               if ( portals.polygons == 2 && !portals.portal[n].hint ) {
+                               else if ( clip.min[2] < portals.portal[portals.portal_sort[n]].min[2] ) {
                                        continue;
                                }
-
-                               if ( portals.clip ) {
-                                       if ( min_check[0] < portals.portal[n].min[0] ) {
-                                               continue;
-                                       }
-                                       else if ( min_check[1] < portals.portal[n].min[1] ) {
-                                               continue;
-                                       }
-                                       else if ( min_check[2] < portals.portal[n].min[2] ) {
-                                               continue;
-                                       }
-                                       else if ( max_check[0] > portals.portal[n].max[0] ) {
-                                               continue;
-                                       }
-                                       else if ( max_check[1] > portals.portal[n].max[1] ) {
-                                               continue;
-                                       }
-                                       else if ( max_check[2] > portals.portal[n].max[2] ) {
-                                               continue;
-                                       }
+                               else if ( clip.max[0] > portals.portal[portals.portal_sort[n]].max[0] ) {
+                                       continue;
+                               }
+                               else if ( clip.max[1] > portals.portal[portals.portal_sort[n]].max[1] ) {
+                                       continue;
+                               }
+                               else if ( clip.max[2] > portals.portal[portals.portal_sort[n]].max[2] ) {
+                                       continue;
                                }
+                       }
 
-                               g_QglTable.m_pfn_qglColor4f( portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
-                                                                                        portals.portal[n].fp_color_random[2], trans );
+                       glColor4f( portals.portal[portals.portal_sort[n]].fp_color_random[0], portals.portal[portals.portal_sort[n]].fp_color_random[1],
+                                          portals.portal[portals.portal_sort[n]].fp_color_random[2], trans );
 
-                               g_QglTable.m_pfn_qglBegin( GL_POLYGON );
+                       glBegin( GL_POLYGON );
 
-                               for ( p = 0; p < portals.portal[n].point_count; p++ )
-                                       g_QglTable.m_pfn_qglVertex3fv( portals.portal[n].point[p].p );
+                       for ( p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++ )
+                               glVertex3fv( portals.portal[portals.portal_sort[n]].point[p].p );
 
-                               g_QglTable.m_pfn_qglEnd();
-                       }
+                       glEnd();
                }
        }
-
-       if ( portals.lines ) {
-               g_QglTable.m_pfn_qglColor4fv( portals.fp_color_3d );
-
+       else
+       {
                for ( n = 0; n < portals.portal_count; n++ )
                {
-                       if ( portals.lines == 2 && !portals.portal[n].hint ) {
+                       if ( portals.polygons == 2 && !portals.portal[n].hint ) {
                                continue;
                        }
 
                        if ( portals.clip ) {
-                               if ( min_check[0] < portals.portal[n].min[0] ) {
+                               if ( clip.min[0] < portals.portal[n].min[0] ) {
                                        continue;
                                }
-                               else if ( min_check[1] < portals.portal[n].min[1] ) {
+                               else if ( clip.min[1] < portals.portal[n].min[1] ) {
                                        continue;
                                }
-                               else if ( min_check[2] < portals.portal[n].min[2] ) {
+                               else if ( clip.min[2] < portals.portal[n].min[2] ) {
                                        continue;
                                }
-                               else if ( max_check[0] > portals.portal[n].max[0] ) {
+                               else if ( clip.max[0] > portals.portal[n].max[0] ) {
                                        continue;
                                }
-                               else if ( max_check[1] > portals.portal[n].max[1] ) {
+                               else if ( clip.max[1] > portals.portal[n].max[1] ) {
                                        continue;
                                }
-                               else if ( max_check[2] > portals.portal[n].max[2] ) {
+                               else if ( clip.max[2] > portals.portal[n].max[2] ) {
                                        continue;
                                }
                        }
 
-                       g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
+                       glColor4f( portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
+                                          portals.portal[n].fp_color_random[2], trans );
+
+                       glBegin( GL_POLYGON );
 
                        for ( p = 0; p < portals.portal[n].point_count; p++ )
-                               g_QglTable.m_pfn_qglVertex3fv( portals.portal[n].inner_point[p].p );
+                               glVertex3fv( portals.portal[n].point[p].p );
 
-                       g_QglTable.m_pfn_qglEnd();
+                       glEnd();
                }
        }
+}
+
+void CPortalsDrawSolidOutline::render( RenderStateFlags state ) const {
+       for ( unsigned int n = 0; n < portals.portal_count; n++ )
+       {
+               if ( portals.lines == 2 && !portals.portal[n].hint ) {
+                       continue;
+               }
+
+               if ( portals.clip ) {
+                       if ( clip.min[0] < portals.portal[n].min[0] ) {
+                               continue;
+                       }
+                       if ( clip.min[1] < portals.portal[n].min[1] ) {
+                               continue;
+                       }
+                       if ( clip.min[2] < portals.portal[n].min[2] ) {
+                               continue;
+                       }
+                       if ( clip.max[0] > portals.portal[n].max[0] ) {
+                               continue;
+                       }
+                       if ( clip.max[1] > portals.portal[n].max[1] ) {
+                               continue;
+                       }
+                       if ( clip.max[2] > portals.portal[n].max[2] ) {
+                               continue;
+                       }
+               }
 
-       g_QglTable.m_pfn_qglPopAttrib();
+               glBegin( GL_LINE_LOOP );
+
+               for ( unsigned int p = 0; p < portals.portal[n].point_count; p++ )
+                       glVertex3fv( portals.portal[n].inner_point[p].p );
+
+               glEnd();
+       }
 }