]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/prtview/portals.cpp
This is a major change that updates the 3rd party libs on Windows builds.
[xonotic/netradiant.git] / contrib / prtview / portals.cpp
index 6b3178c06648e70363c33f333e8ea7866b1aef75..0c57d214a3dd36312f17694a5fb1264dd3b1b1d0 100644 (file)
@@ -17,7 +17,7 @@ License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
-#include "portals.h"
+#include "stdafx.h"
 #include <string.h>
 #include <stdlib.h>
 #ifndef __APPLE__
@@ -25,11 +25,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #endif
 #include <stdio.h>
 
-#include "iglrender.h"
-#include "cullable.h"
-
-#include "prtview.h"
-
 #define LINE_BUF 1000
 
 CPortals portals;
@@ -58,7 +53,7 @@ CBspPortal::~CBspPortal()
        delete[] inner_point;
 }
 
-bool CBspPortal::Build(char *def)
+qboolean CBspPortal::Build(char *def)
 {
        char *c = def;
        unsigned int n;
@@ -72,11 +67,11 @@ bool 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];
        inner_point = new CBspPoint[point_count];
@@ -86,7 +81,7 @@ bool CBspPortal::Build(char *def)
                for(; *c != 0 && *c != '('; c++);
 
                if(*c == 0)
-                       return false;
+                       return FALSE;
 
                c++;
 
@@ -132,7 +127,7 @@ bool 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()
@@ -168,7 +163,7 @@ void CPortals::Load()
        
        Purge();
 
-       globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
+       Sys_Printf(MSG_PREFIX "Loading portal file %s.\n", fn);
 
        FILE *in;
 
@@ -176,7 +171,7 @@ void CPortals::Load()
 
        if(in == NULL)
        {
-               globalOutputStream() << "  ERROR - could not open file.\n";
+               Sys_Printf("  ERROR - could not open file.\n");
 
                return;
        }
@@ -185,7 +180,7 @@ void CPortals::Load()
        {
                fclose(in);
 
-               globalOutputStream() << "  ERROR - File ended prematurely.\n";
+               Sys_Printf("  ERROR - File ended prematurely.\n");
 
                return;
        }
@@ -194,7 +189,7 @@ void CPortals::Load()
        {
                fclose(in);
 
-               globalOutputStream() << "  ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
+               Sys_Printf("  ERROR - File header indicates wrong file type (should be \"PRT1\").\n");
 
                return;
        }
@@ -203,7 +198,7 @@ void CPortals::Load()
        {
                fclose(in);
 
-               globalOutputStream() << "  ERROR - File ended prematurely.\n";
+               Sys_Printf("  ERROR - File ended prematurely.\n");
 
                return;
        }
@@ -216,7 +211,7 @@ void CPortals::Load()
 
                node_count = 0;
 
-               globalOutputStream() << "  ERROR - Extreme number of nodes, aborting.\n";
+               Sys_Printf("  ERROR - Extreme number of nodes, aborting.\n");
 
                return;
        }
@@ -228,7 +223,7 @@ void CPortals::Load()
 
                node_count = 0;
 
-               globalOutputStream() << "  ERROR - File ended prematurely.\n";
+               Sys_Printf("  ERROR - File ended prematurely.\n");
 
                return;
        }
@@ -242,7 +237,7 @@ void CPortals::Load()
                portal_count = 0;
                node_count = 0;
 
-               globalOutputStream() << "  ERROR - Extreme number of portals, aborting.\n";
+               Sys_Printf("  ERROR - Extreme number of portals, aborting.\n");
 
                return;
        }
@@ -254,7 +249,7 @@ void CPortals::Load()
                portal_count = 0;
                node_count = 0;
 
-               globalOutputStream() << "  ERROR - number of portals equals 0, aborting.\n";
+               Sys_Printf("  ERROR - number of portals equals 0, aborting.\n");
 
                return;
        }
@@ -264,10 +259,10 @@ void CPortals::Load()
        portal_sort = new int[portal_count];
 
        unsigned int n;
-       bool first = true;
+       qboolean first = TRUE;
        unsigned test_vals_1, test_vals_2;
 
-       hint_flags = false;
+       hint_flags = FALSE;
 
        for(n = 0; n < portal_count; )
        {
@@ -277,7 +272,7 @@ void CPortals::Load()
 
                        Purge();
 
-                       globalOutputStream() << "  ERROR - Could not find information for portal number " << n + 1 << " of " << portal_count << ".\n";
+                       Sys_Printf("  ERROR - Could not find information for portal number %d of %d.\n", n + 1, portal_count);
 
                        return;
                }
@@ -287,17 +282,17 @@ void CPortals::Load()
                        if(first && sscanf(buf, "%d %d", &test_vals_1, &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();
 
-                       globalOutputStream() << "  ERROR - Information for portal number " << n + 1 << " of " << portal_count << " is not formatted correctly.\n";
+                       Sys_Printf("  ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, portal_count);
 
                        return;
                }
@@ -307,135 +302,7 @@ void CPortals::Load()
 
        fclose(in);
 
-       globalOutputStream() << "  " << node_count << " portals read in.\n";
-}
-
-#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);
-
-  GlobalOpenGLStateLibrary().getDefaultState(state);
-  state.m_state = RENDER_FILL|RENDER_BLEND|RENDER_COLOURWRITE|RENDER_COLOURCHANGE|RENDER_SMOOTH|RENDER_POLYGONSMOOTH;
-
-       if(portals.aa_3d)
-  {
-               state.m_state |= RENDER_POLYGONSMOOTH;
-  }
-
-       switch(portals.zbuffer)
-       {
-       case 1:
-               state.m_state |= RENDER_DEPTHTEST;
-               break;
-       case 2:
-               break;
-       default:
-               state.m_state |= RENDER_DEPTHTEST;
-               state.m_state |= RENDER_DEPTHWRITE;
-       }
-
-       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];
-
-       if(portals.aa_3d)
-  {
-               state.m_state |= RENDER_LINESMOOTH;
-  }
-
-       switch(portals.zbuffer)
-       {
-       case 1:
-               state.m_state |= RENDER_DEPTHTEST;
-               break;
-       case 2:
-               break;
-       default:
-               state.m_state |= RENDER_DEPTHTEST;
-               state.m_state |= RENDER_DEPTHWRITE;
-       }
-
-       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_outline, state);
-
-  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);
-}
-
-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();
+       Sys_Printf("  %u portals read in.\n", node_count, portal_count);
 }
 
 void CPortals::FixColors()
@@ -456,197 +323,480 @@ void CPortals::FixColors()
        fp_color_fog[3] = 1.0f;
 }
 
-void CPortalsRender::renderWireframe(Renderer& renderer, const VolumeTest& volume) const
+CPortalsRender::CPortalsRender()
 {
-       if(!portals.show_2d || portals.portal_count < 1)
-               return;
+       refCount = 1;
+}
 
-  renderer.SetState(g_shader_wireframe, Renderer::eWireframeOnly);
+CPortalsRender::~CPortalsRender()
+{
+}
 
-  renderer.addRenderable(m_drawWireframe, g_matrix4_identity);
+void CPortalsRender::Register()
+{
+       g_QglTable.m_pfnHookGL2DWindow( this );
+       g_QglTable.m_pfnHookGL3DWindow( this );
 }
 
-void CPortalsDrawWireframe::render(RenderStateFlags state) const
+void CPortalsRender::Draw2D( VIEWTYPE vt )
 {
+       if(!portals.show_2d || portals.portal_count < 1)
+               return;
+
+       g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
+
+       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);
+       }
+
+       switch(vt)
+       {
+       case XY:
+               break;
+       case XZ:
+               g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
+               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);
+               break;
+       }
+
+       g_QglTable.m_pfn_qglLineWidth(portals.width_2d * 0.5f);
+
+       g_QglTable.m_pfn_qglColor4fv(portals.fp_color_2d);
+
        unsigned int n, p;
 
        for(n = 0; n < portals.portal_count; n++)
        {
-               glBegin(GL_LINE_LOOP);
+               g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
 
                for(p = 0; p < portals.portal[n].point_count; p++)
-                       glVertex3fv(portals.portal[n].point[p].p);
+                       g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].point[p].p);
 
-               glEnd();
+               g_QglTable.m_pfn_qglEnd();
        }
+
+       g_QglTable.m_pfn_qglPopAttrib();
+}
+
+/*
+ * 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
+}
+
+#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) );
+}
+
+
+
+/*
+ * 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,
+
+ 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;
+
+ /* 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;
+
+ /* 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; }
+
+ /* 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;
+
+ /* 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; }
+
+ /* choose pivot - or die */
+ if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
+ if (0.0 == r2[2])  return GL_FALSE;
+
+ /* 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];
+
+ /* last check */
+ if (0.0 == r3[3]) return GL_FALSE;
+
+ 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
 }
 
-CubicClipVolume calculateCubicClipVolume(const Matrix4& viewproj)
+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)
 {
-  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;
+    /* 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;
 }
 
-void CPortalsRender::renderSolid(Renderer& renderer, const VolumeTest& volume) const
+void CPortalsRender::Draw3D()
 {
        if(!portals.show_3d || portals.portal_count < 1)
                return;
 
-  CubicClipVolume clip = calculateCubicClipVolume(matrix4_multiplied_by_matrix4(volume.GetProjection(), volume.GetModelview()));
+       g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
 
-       if(portals.polygons)
-       {
-    renderer.SetState(g_shader_solid, Renderer::eWireframeOnly);
-    renderer.SetState(g_shader_solid, Renderer::eFullMaterials);
+       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];
 
-    m_drawSolid.clip = clip;
-    renderer.addRenderable(m_drawSolid, g_matrix4_identity);
-  }
+       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);
 
-       if(portals.lines)
+       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.aa_3d)
+               g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);
+       else
+               g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);
+
+       if(portals.fog)
        {
-    renderer.SetState(g_shader_solid_outline, Renderer::eWireframeOnly);
-    renderer.SetState(g_shader_solid_outline, Renderer::eFullMaterials);
+               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);
+       }
 
-    m_drawSolidOutline.clip = clip;
-    renderer.addRenderable(m_drawSolidOutline, g_matrix4_identity);
-  }
-}
+       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);
+       }
 
-void CPortalsDrawSolid::render(RenderStateFlags state) const
-{
-       float trans = (100.0f - portals.trans_3d) / 100.0f;
+       g_QglTable.m_pfn_qglLineWidth(portals.width_3d * 0.5f);
 
        unsigned int n, p;
 
-       if(portals.zbuffer != 0)
+       if(portals.polygons)
        {
-               float d;
-
-               for(n = 0; n < portals.portal_count; n++)
+               if(portals.zbuffer != 0)
                {
-                       d = (float)clip.cam[0] - portals.portal[n].center.p[0];
-                       portals.portal[n].dist = d * d;
+                       float d;
 
-                       d = (float)clip.cam[1] - portals.portal[n].center.p[1];
-                       portals.portal[n].dist += d * 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;
 
-                       d = (float)clip.cam[2] - portals.portal[n].center.p[2];
-                       portals.portal[n].dist += d * d;
+                               d = (float)cam[1] - portals.portal[n].center.p[1];
+                               portals.portal[n].dist += d * d;
 
-                       portals.portal_sort[n] = n;
-               }
+                               d = (float)cam[2] - portals.portal[n].center.p[2];
+                               portals.portal[n].dist += d * d;
 
-               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)
-                               continue;
+                               portals.portal_sort[n] = n;
+                       }
 
-                       if(portals.clip)
+                       qsort(portals.portal_sort, portals.portal_count, 4, compare);
+                                       
+                       for(n = 0; n < portals.portal_count; n++)
                        {
-                               if(clip.min[0] < portals.portal[portals.portal_sort[n]].min[0])
-                                       continue;
-                               else if(clip.min[1] < portals.portal[portals.portal_sort[n]].min[1])
-                                       continue;
-                               else if(clip.min[2] < portals.portal[portals.portal_sort[n]].min[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])
+                               if(portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint)
                                        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;
+                               }
+
+                               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)
+                                       continue;
 
-                       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);
-
-                       glBegin(GL_POLYGON);
-
-                               for(p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++)
-                                       glVertex3fv(portals.portal[portals.portal_sort[n]].point[p].p);
-
-                       glEnd();
+                               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;
+                               }
+
+                               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);
+
+                               g_QglTable.m_pfn_qglBegin(GL_POLYGON);
+
+                                       for(p = 0; p < portals.portal[n].point_count; p++)
+                                               g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].point[p].p);
+
+                               g_QglTable.m_pfn_qglEnd();
+                       }
                }
        }
-       else
+
+       if(portals.lines)
        {
+               g_QglTable.m_pfn_qglColor4fv(portals.fp_color_3d);
+
                for(n = 0; n < portals.portal_count; n++)
                {
-                       if(portals.polygons == 2 && !portals.portal[n].hint)
+                       if(portals.lines == 2 && !portals.portal[n].hint)
                                continue;
 
                        if(portals.clip)
                        {
-                               if(clip.min[0] < portals.portal[n].min[0])
+                               if(min_check[0] < portals.portal[n].min[0])
                                        continue;
-                               else if(clip.min[1] < portals.portal[n].min[1])
+                               else if(min_check[1] < portals.portal[n].min[1])
                                        continue;
-                               else if(clip.min[2] < portals.portal[n].min[2])
+                               else if(min_check[2] < portals.portal[n].min[2])
                                        continue;
-                               else if(clip.max[0] > portals.portal[n].max[0])
+                               else if(max_check[0] > portals.portal[n].max[0])
                                        continue;
-                               else if(clip.max[1] > portals.portal[n].max[1])
+                               else if(max_check[1] > portals.portal[n].max[1])
                                        continue;
-                               else if(clip.max[2] > portals.portal[n].max[2])
+                               else if(max_check[2] > portals.portal[n].max[2])
                                        continue;
                        }
 
-                       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);
+                       g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
 
-                               for(p = 0; p < portals.portal[n].point_count; p++)
-                                       glVertex3fv(portals.portal[n].point[p].p);
+                       for(p = 0; p < portals.portal[n].point_count; p++)
+                               g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].inner_point[p].p);
 
-                       glEnd();
+                       g_QglTable.m_pfn_qglEnd();
                }
        }
-}
-
-void CPortalsDrawSolidOutline::render(RenderStateFlags state) const
-{
-  for(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;
-                       else if(clip.min[1] < portals.portal[n].min[1])
-                               continue;
-                       else if(clip.min[2] < portals.portal[n].min[2])
-                               continue;
-                       else if(clip.max[0] > portals.portal[n].max[0])
-                               continue;
-                       else if(clip.max[1] > portals.portal[n].max[1])
-                               continue;
-                       else if(clip.max[2] > portals.portal[n].max[2])
-                               continue;
-               }
-
-               glBegin(GL_LINE_LOOP);
-
-               for(int p = 0; p < portals.portal[n].point_count; p++)
-                       glVertex3fv(portals.portal[n].inner_point[p].p);
 
-               glEnd();
-       }
+       g_QglTable.m_pfn_qglPopAttrib();
 }
+