]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/prtview/portals.cpp
Inject OpenGLBinding instead of using GlobalOpenGL() everywhere
[xonotic/netradiant.git] / contrib / prtview / portals.cpp
index 378bb85d06d96eb372a60836b8a036e151ed7155..7dbc4052ba60c358e0b5c0f1b52f43c01b5c041e 100644 (file)
-/*\r
-PrtView plugin for GtkRadiant\r
-Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com\r
-\r
-This library is free software; you can redistribute it and/or\r
-modify it under the terms of the GNU Lesser General Public\r
-License as published by the Free Software Foundation; either\r
-version 2.1 of the License, or (at your option) any later version.\r
-\r
-This library is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
-Lesser General Public License for more details.\r
-\r
-You should have received a copy of the GNU Lesser General Public\r
-License along with this library; if not, write to the Free Software\r
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
-*/\r
-\r
-#include "stdafx.h"\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#ifndef __APPLE__\r
-#include <search.h>\r
-#endif\r
-#include <stdio.h>\r
-\r
-#define LINE_BUF 1000\r
-\r
-CPortals portals;\r
-CPortalsRender render;\r
-\r
-int compare( const void *arg1, const void *arg2 )\r
-{\r
-\r
-       if(portals.portal[*((int *)arg1)].dist > portals.portal[*((int *)arg2)].dist)\r
-               return -1;\r
-       else if(portals.portal[*((int *)arg1)].dist < portals.portal[*((int *)arg2)].dist)\r
-               return 1;\r
-\r
-       return 0;\r
-}\r
-\r
-\r
-CBspPortal::CBspPortal()\r
-{\r
-       memset(this, 0, sizeof(CBspPortal));\r
-}\r
-\r
-CBspPortal::~CBspPortal()\r
-{\r
-       delete[] point;\r
-       delete[] inner_point;\r
-}\r
-\r
-qboolean CBspPortal::Build(char *def)\r
-{\r
-       char *c = def;\r
-       unsigned int n;\r
-       int dummy1, dummy2;\r
-       int res_cnt, i;\r
-\r
-       if(portals.hint_flags)\r
-       {\r
-               res_cnt = sscanf(def, "%u %d %d %d", &point_count, &dummy1, &dummy2, (int *)&hint);\r
-       }\r
-       else\r
-       {\r
-               sscanf(def, "%u", &point_count);\r
-               hint = FALSE;\r
-       }\r
-\r
-       if(point_count < 3 || (portals.hint_flags && res_cnt < 4))\r
-               return FALSE;\r
-\r
-       point = new CBspPoint[point_count];\r
-       inner_point = new CBspPoint[point_count];\r
-\r
-       for(n = 0; n < point_count; n++)\r
-       {\r
-               for(; *c != 0 && *c != '('; c++);\r
-\r
-               if(*c == 0)\r
-                       return FALSE;\r
-\r
-               c++;\r
-\r
-               sscanf(c, "%f %f %f", point[n].p, point[n].p+1, point[n].p+2);\r
-\r
-               center.p[0] += point[n].p[0];\r
-               center.p[1] += point[n].p[1];\r
-               center.p[2] += point[n].p[2];\r
-\r
-               if(n == 0)\r
-               {\r
-                       for(i = 0; i < 3; i++)\r
-                       {\r
-                               min[i] = point[n].p[i];\r
-                               max[i] = point[n].p[i];\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       for(i = 0; i < 3; i++)\r
-                       {\r
-                               if(min[i] > point[n].p[i])\r
-                                       min[i] = point[n].p[i];\r
-                               if(max[i] < point[n].p[i])\r
-                                       max[i] = point[n].p[i];\r
-                       }\r
-               }\r
-       }\r
-\r
-       center.p[0] /= (float)point_count;\r
-       center.p[1] /= (float)point_count;\r
-       center.p[2] /= (float)point_count;\r
-\r
-       for(n = 0; n < point_count; n++)\r
-       {\r
-               inner_point[n].p[0] = (0.01f * center.p[0]) + (0.99f * point[n].p[0]);\r
-               inner_point[n].p[1] = (0.01f * center.p[1]) + (0.99f * point[n].p[1]);\r
-               inner_point[n].p[2] = (0.01f * center.p[2]) + (0.99f * point[n].p[2]);\r
-       }\r
-\r
-       fp_color_random[0] = (float)(rand() & 0xff) / 255.0f;\r
-       fp_color_random[1] = (float)(rand() & 0xff) / 255.0f;\r
-       fp_color_random[2] = (float)(rand() & 0xff) / 255.0f;\r
-       fp_color_random[3] = 1.0f;\r
-\r
-       return TRUE;\r
-}\r
-\r
-CPortals::CPortals()\r
-{\r
-       memset(this, 0, sizeof(CPortals));\r
-}\r
-\r
-CPortals::~CPortals()\r
-{\r
-       Purge();\r
-}\r
-\r
-void CPortals::Purge()\r
-{\r
-       delete[] portal;\r
-       delete[] portal_sort;\r
-       portal = NULL;\r
-       portal_sort = NULL;\r
-       portal_count = 0;\r
-\r
-       /*\r
-       delete[] node;\r
-       node = NULL;\r
-       node_count = 0;\r
-       */\r
-}\r
-\r
-void CPortals::Load()\r
-{\r
-       char buf[LINE_BUF+1];\r
-\r
-       memset(buf, 0, LINE_BUF + 1);\r
-       \r
-       Purge();\r
-\r
-       Sys_Printf(MSG_PREFIX "Loading portal file %s.\n", fn);\r
-\r
-       FILE *in;\r
-\r
-       in = fopen(fn, "rt");\r
-\r
-       if(in == NULL)\r
-       {\r
-               Sys_Printf("  ERROR - could not open file.\n");\r
-\r
-               return;\r
-       }\r
-\r
-       if(!fgets(buf, LINE_BUF, in))\r
-       {\r
-               fclose(in);\r
-\r
-               Sys_Printf("  ERROR - File ended prematurely.\n");\r
-\r
-               return;\r
-       }\r
-\r
-       if(strncmp("PRT1", buf, 4) != 0)\r
-       {\r
-               fclose(in);\r
-\r
-               Sys_Printf("  ERROR - File header indicates wrong file type (should be \"PRT1\").\n");\r
-\r
-               return;\r
-       }\r
-\r
-       if(!fgets(buf, LINE_BUF, in))\r
-       {\r
-               fclose(in);\r
-\r
-               Sys_Printf("  ERROR - File ended prematurely.\n");\r
-\r
-               return;\r
-       }\r
-\r
-       sscanf(buf, "%u", &node_count);\r
-/*\r
-       if(node_count > 0xFFFF)\r
-       {\r
-               fclose(in);\r
-\r
-               node_count = 0;\r
-\r
-               Sys_Printf("  ERROR - Extreme number of nodes, aborting.\n");\r
-\r
-               return;\r
-       }\r
-       */\r
-\r
-       if(!fgets(buf, LINE_BUF, in))\r
-       {\r
-               fclose(in);\r
-\r
-               node_count = 0;\r
-\r
-               Sys_Printf("  ERROR - File ended prematurely.\n");\r
-\r
-               return;\r
-       }\r
-\r
-       sscanf(buf, "%u", &portal_count);\r
-\r
-       if(portal_count > 0xFFFF)\r
-       {\r
-               fclose(in);\r
-\r
-               portal_count = 0;\r
-               node_count = 0;\r
-\r
-               Sys_Printf("  ERROR - Extreme number of portals, aborting.\n");\r
-\r
-               return;\r
-       }\r
-\r
-       if(portal_count < 0)\r
-       {\r
-               fclose(in);\r
-\r
-               portal_count = 0;\r
-               node_count = 0;\r
-\r
-               Sys_Printf("  ERROR - number of portals equals 0, aborting.\n");\r
-\r
-               return;\r
-       }\r
-\r
-//     node = new CBspNode[node_count];\r
-       portal = new CBspPortal[portal_count];\r
-       portal_sort = new int[portal_count];\r
-\r
-       unsigned int n;\r
-       qboolean first = TRUE;\r
-       unsigned test_vals_1, test_vals_2;\r
-\r
-       hint_flags = FALSE;\r
-\r
-       for(n = 0; n < portal_count; )\r
-       {\r
-               if(!fgets(buf, LINE_BUF, in))\r
-               {\r
-                       fclose(in);\r
-\r
-                       Purge();\r
-\r
-                       Sys_Printf("  ERROR - Could not find information for portal number %d of %d.\n", n + 1, portal_count);\r
-\r
-                       return;\r
-               }\r
-\r
-               if(!portal[n].Build(buf))\r
-               {\r
-                       if(first && sscanf(buf, "%d %d", &test_vals_1, &test_vals_2) == 1) // skip additional counts of later data, not needed\r
-                       {\r
-                               // We can count on hint flags being in the file\r
-                               hint_flags = TRUE;\r
-                               continue;\r
-                       }\r
-\r
-                       first = FALSE;\r
-\r
-                       fclose(in);\r
-\r
-                       Purge();\r
-\r
-                       Sys_Printf("  ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, portal_count);\r
-\r
-                       return;\r
-               }\r
-\r
-               n++;\r
-       }\r
-\r
-       fclose(in);\r
-\r
-       Sys_Printf("  %u portals read in.\n", node_count, portal_count);\r
-}\r
-\r
-void CPortals::FixColors()\r
-{\r
-       fp_color_2d[0] = (float)GetRValue(color_2d) / 255.0f;\r
-       fp_color_2d[1] = (float)GetGValue(color_2d) / 255.0f;\r
-       fp_color_2d[2] = (float)GetBValue(color_2d) / 255.0f;\r
-       fp_color_2d[3] = 1.0f;\r
-\r
-       fp_color_3d[0] = (float)GetRValue(color_3d) / 255.0f;\r
-       fp_color_3d[1] = (float)GetGValue(color_3d) / 255.0f;\r
-       fp_color_3d[2] = (float)GetBValue(color_3d) / 255.0f;\r
-       fp_color_3d[3] = 1.0f;\r
-\r
-       fp_color_fog[0] = 0.0f;//(float)GetRValue(color_fog) / 255.0f;\r
-       fp_color_fog[1] = 0.0f;//(float)GetGValue(color_fog) / 255.0f;\r
-       fp_color_fog[2] = 0.0f;//(float)GetBValue(color_fog) / 255.0f;\r
-       fp_color_fog[3] = 1.0f;\r
-}\r
-\r
-CPortalsRender::CPortalsRender()\r
-{\r
-       refCount = 1;\r
-}\r
-\r
-CPortalsRender::~CPortalsRender()\r
-{\r
-}\r
-\r
-void CPortalsRender::Register()\r
-{\r
-       g_QglTable.m_pfnHookGL2DWindow( this );\r
-       g_QglTable.m_pfnHookGL3DWindow( this );\r
-}\r
-\r
-void CPortalsRender::Draw2D( VIEWTYPE vt )\r
-{\r
-       if(!portals.show_2d || portals.portal_count < 1)\r
-               return;\r
-\r
-       g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);\r
-\r
-       if(portals.aa_2d)\r
-       {\r
-               g_QglTable.m_pfn_qglEnable(GL_BLEND);\r
-               g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);\r
-       }\r
-       else\r
-       {\r
-               g_QglTable.m_pfn_qglDisable(GL_BLEND);\r
-               g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);\r
-       }\r
-\r
-       switch(vt)\r
-       {\r
-       case XY:\r
-               break;\r
-       case XZ:\r
-               g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);\r
-               break;\r
-       case YZ:\r
-               g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);\r
-               g_QglTable.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);\r
-               break;\r
-       }\r
-\r
-       g_QglTable.m_pfn_qglLineWidth(portals.width_2d * 0.5f);\r
-\r
-       g_QglTable.m_pfn_qglColor4fv(portals.fp_color_2d);\r
-\r
-       unsigned int n, p;\r
-\r
-       for(n = 0; n < portals.portal_count; n++)\r
-       {\r
-               g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);\r
-\r
-               for(p = 0; p < portals.portal[n].point_count; p++)\r
-                       g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].point[p].p);\r
-\r
-               g_QglTable.m_pfn_qglEnd();\r
-       }\r
-\r
-       g_QglTable.m_pfn_qglPopAttrib();\r
-}\r
-\r
-/*\r
- * Transform a point (column vector) by a 4x4 matrix.  I.e.  out = m * in\r
- * Input:  m - the 4x4 matrix\r
- *         in - the 4x1 vector\r
- * Output:  out - the resulting 4x1 vector.\r
- */\r
-static void transform_point( GLdouble out[4], const GLdouble m[16],\r
-                            const GLdouble in[4] )\r
-{\r
-#define M(row,col)  m[col*4+row]\r
-   out[0] = M(0,0) * in[0] + M(0,1) * in[1] + M(0,2) * in[2] + M(0,3) * in[3];\r
-   out[1] = M(1,0) * in[0] + M(1,1) * in[1] + M(1,2) * in[2] + M(1,3) * in[3];\r
-   out[2] = M(2,0) * in[0] + M(2,1) * in[1] + M(2,2) * in[2] + M(2,3) * in[3];\r
-   out[3] = M(3,0) * in[0] + M(3,1) * in[1] + M(3,2) * in[2] + M(3,3) * in[3];\r
-#undef M\r
-}\r
-\r
-#include <math.h>\r
-\r
-\r
-/*\r
- * Perform a 4x4 matrix multiplication  (product = a x b).\r
- * Input:  a, b - matrices to multiply\r
- * Output:  product - product of a and b\r
- */\r
-static void matmul( GLdouble *product, const GLdouble *a, const GLdouble *b )\r
-{\r
-   /* This matmul was contributed by Thomas Malik */\r
-   GLdouble temp[16];\r
-   GLint i;\r
-\r
-#define A(row,col)  a[(col<<2)+row]\r
-#define B(row,col)  b[(col<<2)+row]\r
-#define T(row,col)  temp[(col<<2)+row]\r
-\r
-   /* i-te Zeile */\r
-   for (i = 0; i < 4; i++)\r
-     {\r
-       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);\r
-       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);\r
-       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);\r
-       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);\r
-     }\r
-\r
-#undef A\r
-#undef B\r
-#undef T\r
-   memcpy ( product, temp, 16*sizeof(GLdouble) );\r
-}\r
-\r
-\r
-\r
-/*\r
- * Compute inverse of 4x4 transformation matrix.\r
- * Code contributed by Jacques Leroy jle@star.be\r
- * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)\r
- */\r
-static GLboolean invert_matrix( const GLdouble *m, GLdouble *out )\r
-{\r
-/* NB. OpenGL Matrices are COLUMN major. */\r
-#define SWAP_ROWS(a, b) { GLdouble *_tmp = a; (a)=(b); (b)=_tmp; }\r
-#define MAT(m,r,c) (m)[(c)*4+(r)]\r
-\r
- GLdouble wtmp[4][8];\r
- GLdouble m0, m1, m2, m3, s;\r
- GLdouble *r0, *r1, *r2, *r3;\r
-\r
- r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];\r
-\r
- r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),\r
- r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),\r
- r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,\r
-\r
- r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),\r
- r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),\r
- r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,\r
-\r
- r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),\r
- r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),\r
- r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,\r
-\r
- r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),\r
- r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),\r
- r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;\r
-\r
- /* choose pivot - or die */\r
- if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);\r
- if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);\r
- if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);\r
- if (0.0 == r0[0])  return GL_FALSE;\r
-\r
- /* eliminate first variable     */\r
- m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];\r
- s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;\r
- s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;\r
- s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;\r
- s = r0[4];\r
- if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }\r
- s = r0[5];\r
- if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }\r
- s = r0[6];\r
- if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }\r
- s = r0[7];\r
- if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }\r
-\r
- /* choose pivot - or die */\r
- if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);\r
- if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);\r
- if (0.0 == r1[1])  return GL_FALSE;\r
-\r
- /* eliminate second variable */\r
- m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];\r
- r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];\r
- r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];\r
- s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }\r
- s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }\r
- s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }\r
- s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }\r
-\r
- /* choose pivot - or die */\r
- if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);\r
- if (0.0 == r2[2])  return GL_FALSE;\r
-\r
- /* eliminate third variable */\r
- m3 = r3[2]/r2[2];\r
- r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],\r
- r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],\r
- r3[7] -= m3 * r2[7];\r
-\r
- /* last check */\r
- if (0.0 == r3[3]) return GL_FALSE;\r
-\r
- s = 1.0/r3[3];              /* now back substitute row 3 */\r
- r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;\r
-\r
- m2 = r2[3];                 /* now back substitute row 2 */\r
- s  = 1.0/r2[2];\r
- r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),\r
- r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);\r
- m1 = r1[3];\r
- r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,\r
- r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;\r
- m0 = r0[3];\r
- r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,\r
- r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;\r
-\r
- m1 = r1[2];                 /* now back substitute row 1 */\r
- s  = 1.0/r1[1];\r
- r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),\r
- r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);\r
- m0 = r0[2];\r
- r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,\r
- r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;\r
-\r
- m0 = r0[1];                 /* now back substitute row 0 */\r
- s  = 1.0/r0[0];\r
- r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),\r
- r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);\r
-\r
- MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],\r
- MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],\r
- MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],\r
- MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],\r
- MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],\r
- MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],\r
- MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],\r
- MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; \r
-\r
- return GL_TRUE;\r
-\r
-#undef MAT\r
-#undef SWAP_ROWS\r
-}\r
-\r
-GLint UnProject(GLdouble winx,GLdouble winy,GLdouble winz,\r
-               const GLdouble model[16],const GLdouble proj[16],\r
-               const GLint viewport[4],\r
-               GLdouble *objx,GLdouble *objy,GLdouble *objz)\r
-{\r
-    /* matrice de transformation */\r
-    GLdouble m[16], A[16];\r
-    GLdouble in[4],out[4];\r
-\r
-    /* transformation coordonnees normalisees entre -1 et 1 */\r
-    in[0]=(winx-viewport[0])*2/viewport[2] - 1.0;\r
-    in[1]=(winy-viewport[1])*2/viewport[3] - 1.0;\r
-    in[2]=2*winz - 1.0;\r
-    in[3]=1.0;\r
-\r
-    /* calcul transformation inverse */\r
-    matmul(A,proj,model);\r
-    invert_matrix(A,m);\r
-\r
-    /* d'ou les coordonnees objets */\r
-    transform_point(out,m,in);\r
-    if (out[3]==0.0)\r
-       return GL_FALSE;\r
-    *objx=out[0]/out[3];\r
-    *objy=out[1]/out[3];\r
-    *objz=out[2]/out[3];\r
-    return GL_TRUE;\r
-}\r
-\r
-void CPortalsRender::Draw3D()\r
-{\r
-       if(!portals.show_3d || portals.portal_count < 1)\r
-               return;\r
-\r
-       g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);\r
-\r
-       double cam[3];\r
-       double proj_m[16];\r
-       double model_m[16];\r
-       float min_check[3];\r
-       float max_check[3];\r
-       float trans = (100.0f - portals.trans_3d) / 100.0f;\r
-       int view[4];\r
-\r
-       g_QglTable.m_pfn_qglGetDoublev(GL_PROJECTION_MATRIX, proj_m);\r
-       g_QglTable.m_pfn_qglGetDoublev(GL_MODELVIEW_MATRIX, model_m);\r
-       g_QglTable.m_pfn_qglGetIntegerv(GL_VIEWPORT, view);\r
-\r
-       UnProject(0.5 * (double)view[2], 0.5 * (double)view[3], 0.0, model_m, proj_m, view, cam, cam+1, cam+2);\r
-\r
-       min_check[0] = (float)cam[0] + (portals.clip_range * 64.0f);\r
-       min_check[1] = (float)cam[1] + (portals.clip_range * 64.0f);\r
-       min_check[2] = (float)cam[2] + (portals.clip_range * 64.0f);\r
-       max_check[0] = (float)cam[0] - (portals.clip_range * 64.0f);\r
-       max_check[1] = (float)cam[1] - (portals.clip_range * 64.0f);\r
-       max_check[2] = (float)cam[2] - (portals.clip_range * 64.0f);\r
-\r
-       g_QglTable.m_pfn_qglHint(GL_FOG_HINT, GL_NICEST);\r
-       \r
-       g_QglTable.m_pfn_qglDisable(GL_CULL_FACE);\r
-\r
-       g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);\r
-       g_QglTable.m_pfn_qglDisable(GL_POLYGON_SMOOTH);\r
-\r
-       g_QglTable.m_pfn_qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);\r
-\r
-       g_QglTable.m_pfn_qglShadeModel(GL_SMOOTH);\r
-\r
-       g_QglTable.m_pfn_qglEnable(GL_BLEND);\r
-       g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
-       g_QglTable.m_pfn_qglEnable(GL_POLYGON_SMOOTH);\r
-\r
-       if(portals.aa_3d)\r
-               g_QglTable.m_pfn_qglEnable(GL_LINE_SMOOTH);\r
-       else\r
-               g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);\r
-\r
-       if(portals.fog)\r
-       {\r
-               g_QglTable.m_pfn_qglEnable(GL_FOG);\r
-\r
-               g_QglTable.m_pfn_qglFogi(GL_FOG_MODE, GL_EXP);\r
-               g_QglTable.m_pfn_qglFogf(GL_FOG_DENSITY, 0.001f);\r
-               g_QglTable.m_pfn_qglFogf(GL_FOG_START, 10.0f);\r
-               g_QglTable.m_pfn_qglFogf(GL_FOG_END, 10000.0f);\r
-               g_QglTable.m_pfn_qglFogi(GL_FOG_INDEX, 0);\r
-               g_QglTable.m_pfn_qglFogfv(GL_FOG_COLOR, portals.fp_color_fog);\r
-       }\r
-       else\r
-       {\r
-               g_QglTable.m_pfn_qglDisable(GL_FOG);\r
-       }\r
-\r
-       switch(portals.zbuffer)\r
-       {\r
-       case 1:\r
-               g_QglTable.m_pfn_qglEnable(GL_DEPTH_TEST);\r
-               g_QglTable.m_pfn_qglDepthMask(GL_FALSE);\r
-               break;\r
-       case 2:\r
-               g_QglTable.m_pfn_qglDisable(GL_DEPTH_TEST);\r
-               break;\r
-       default:\r
-               g_QglTable.m_pfn_qglEnable(GL_DEPTH_TEST);\r
-               g_QglTable.m_pfn_qglDepthMask(GL_TRUE);\r
-       }\r
-\r
-       g_QglTable.m_pfn_qglLineWidth(portals.width_3d * 0.5f);\r
-\r
-       unsigned int n, p;\r
-\r
-       if(portals.polygons)\r
-       {\r
-               if(portals.zbuffer != 0)\r
-               {\r
-                       float d;\r
-\r
-                       for(n = 0; n < portals.portal_count; n++)\r
-                       {\r
-                               d = (float)cam[0] - portals.portal[n].center.p[0];\r
-                               portals.portal[n].dist = d * d;\r
-\r
-                               d = (float)cam[1] - portals.portal[n].center.p[1];\r
-                               portals.portal[n].dist += d * d;\r
-\r
-                               d = (float)cam[2] - portals.portal[n].center.p[2];\r
-                               portals.portal[n].dist += d * d;\r
-\r
-                               portals.portal_sort[n] = n;\r
-                       }\r
-\r
-                       qsort(portals.portal_sort, portals.portal_count, 4, compare);\r
-                                       \r
-                       for(n = 0; n < portals.portal_count; n++)\r
-                       {\r
-                               if(portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint)\r
-                                       continue;\r
-       \r
-                               if(portals.clip)\r
-                               {\r
-                                       if(min_check[0] < portals.portal[portals.portal_sort[n]].min[0])\r
-                                               continue;\r
-                                       else if(min_check[1] < portals.portal[portals.portal_sort[n]].min[1])\r
-                                               continue;\r
-                                       else if(min_check[2] < portals.portal[portals.portal_sort[n]].min[2])\r
-                                               continue;\r
-                                       else if(max_check[0] > portals.portal[portals.portal_sort[n]].max[0])\r
-                                               continue;\r
-                                       else if(max_check[1] > portals.portal[portals.portal_sort[n]].max[1])\r
-                                               continue;\r
-                                       else if(max_check[2] > portals.portal[portals.portal_sort[n]].max[2])\r
-                                               continue;\r
-                               }\r
-\r
-                               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],\r
-                                       portals.portal[portals.portal_sort[n]].fp_color_random[2], trans);\r
-\r
-                               g_QglTable.m_pfn_qglBegin(GL_POLYGON);\r
-\r
-                                       for(p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++)\r
-                                               g_QglTable.m_pfn_qglVertex3fv(portals.portal[portals.portal_sort[n]].point[p].p);\r
-\r
-                               g_QglTable.m_pfn_qglEnd();\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       for(n = 0; n < portals.portal_count; n++)\r
-                       {\r
-                               if(portals.polygons == 2 && !portals.portal[n].hint)\r
-                                       continue;\r
-\r
-                               if(portals.clip)\r
-                               {\r
-                                       if(min_check[0] < portals.portal[n].min[0])\r
-                                               continue;\r
-                                       else if(min_check[1] < portals.portal[n].min[1])\r
-                                               continue;\r
-                                       else if(min_check[2] < portals.portal[n].min[2])\r
-                                               continue;\r
-                                       else if(max_check[0] > portals.portal[n].max[0])\r
-                                               continue;\r
-                                       else if(max_check[1] > portals.portal[n].max[1])\r
-                                               continue;\r
-                                       else if(max_check[2] > portals.portal[n].max[2])\r
-                                               continue;\r
-                               }\r
-\r
-                               g_QglTable.m_pfn_qglColor4f(portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],\r
-                                       portals.portal[n].fp_color_random[2], trans);\r
-\r
-                               g_QglTable.m_pfn_qglBegin(GL_POLYGON);\r
-\r
-                                       for(p = 0; p < portals.portal[n].point_count; p++)\r
-                                               g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].point[p].p);\r
-\r
-                               g_QglTable.m_pfn_qglEnd();\r
-                       }\r
-               }\r
-       }\r
-\r
-       if(portals.lines)\r
-       {\r
-               g_QglTable.m_pfn_qglColor4fv(portals.fp_color_3d);\r
-\r
-               for(n = 0; n < portals.portal_count; n++)\r
-               {\r
-                       if(portals.lines == 2 && !portals.portal[n].hint)\r
-                               continue;\r
-\r
-                       if(portals.clip)\r
-                       {\r
-                               if(min_check[0] < portals.portal[n].min[0])\r
-                                       continue;\r
-                               else if(min_check[1] < portals.portal[n].min[1])\r
-                                       continue;\r
-                               else if(min_check[2] < portals.portal[n].min[2])\r
-                                       continue;\r
-                               else if(max_check[0] > portals.portal[n].max[0])\r
-                                       continue;\r
-                               else if(max_check[1] > portals.portal[n].max[1])\r
-                                       continue;\r
-                               else if(max_check[2] > portals.portal[n].max[2])\r
-                                       continue;\r
-                       }\r
-\r
-                       g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);\r
-\r
-                       for(p = 0; p < portals.portal[n].point_count; p++)\r
-                               g_QglTable.m_pfn_qglVertex3fv(portals.portal[n].inner_point[p].p);\r
-\r
-                       g_QglTable.m_pfn_qglEnd();\r
-               }\r
-       }\r
-\r
-       g_QglTable.m_pfn_qglPopAttrib();\r
-}\r
-\r
+/*
+   PrtView plugin for GtkRadiant
+   Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   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 "globaldefs.h"
+#include <string.h>
+#include <stdlib.h>
+
+#if !GDEF_OS_MACOS
+
+#include <search.h>
+
+#endif
+
+#include <stdio.h>
+
+#include "iglrender.h"
+#include "cullable.h"
+
+#include "prtview.h"
+
+const int LINE_BUF = 1000;
+
+CPortals portals;
+CPortalsRender render;
+
+int compare(const void *arg1, const void *arg2)
+{
+
+    if (portals.portal[*((int *) arg1)].dist > portals.portal[*((int *) arg2)].dist) {
+        return -1;
+    } else if (portals.portal[*((int *) arg1)].dist < portals.portal[*((int *) arg2)].dist) {
+        return 1;
+    }
+
+    return 0;
+}
+
+
+CBspPortal::CBspPortal()
+{
+    memset(this, 0, sizeof(CBspPortal));
+}
+
+CBspPortal::~CBspPortal()
+{
+    delete[] point;
+    delete[] inner_point;
+}
+
+bool CBspPortal::Build(char *def)
+{
+    char *c = def;
+    unsigned int n;
+    int dummy1, dummy2;
+    int res_cnt, i;
+
+    if (portals.hint_flags) {
+        res_cnt = sscanf(def, "%u %d %d %d", &point_count, &dummy1, &dummy2, (int *) &hint);
+    } else {
+        sscanf(def, "%u", &point_count);
+        hint = false;
+    }
+
+    if (point_count < 3 || (portals.hint_flags && res_cnt < 4)) {
+        return false;
+    }
+
+    point = new CBspPoint[point_count];
+    inner_point = new CBspPoint[point_count];
+
+    for (n = 0; n < point_count; n++) {
+        for (; *c != 0 && *c != '('; c++) {}
+
+        if (*c == 0) {
+            return false;
+        }
+
+        c++;
+
+        sscanf(c, "%f %f %f", point[n].p, point[n].p + 1, point[n].p + 2);
+
+        center.p[0] += point[n].p[0];
+        center.p[1] += point[n].p[1];
+        center.p[2] += point[n].p[2];
+
+        if (n == 0) {
+            for (i = 0; i < 3; i++) {
+                min[i] = point[n].p[i];
+                max[i] = point[n].p[i];
+            }
+        } else {
+            for (i = 0; i < 3; i++) {
+                if (min[i] > point[n].p[i]) {
+                    min[i] = point[n].p[i];
+                }
+                if (max[i] < point[n].p[i]) {
+                    max[i] = point[n].p[i];
+                }
+            }
+        }
+    }
+
+    center.p[0] /= (float) point_count;
+    center.p[1] /= (float) point_count;
+    center.p[2] /= (float) point_count;
+
+    for (n = 0; n < point_count; n++) {
+        inner_point[n].p[0] = (0.01f * center.p[0]) + (0.99f * point[n].p[0]);
+        inner_point[n].p[1] = (0.01f * center.p[1]) + (0.99f * point[n].p[1]);
+        inner_point[n].p[2] = (0.01f * center.p[2]) + (0.99f * point[n].p[2]);
+    }
+
+    fp_color_random[0] = (float) (rand() & 0xff) / 255.0f;
+    fp_color_random[1] = (float) (rand() & 0xff) / 255.0f;
+    fp_color_random[2] = (float) (rand() & 0xff) / 255.0f;
+    fp_color_random[3] = 1.0f;
+
+    return true;
+}
+
+CPortals::CPortals()
+{
+    memset(this, 0, sizeof(CPortals));
+}
+
+CPortals::~CPortals()
+{
+    Purge();
+}
+
+void CPortals::Purge()
+{
+    delete[] portal;
+    delete[] portal_sort;
+    portal = NULL;
+    portal_sort = NULL;
+    portal_count = 0;
+
+    /*
+       delete[] node;
+       node = NULL;
+       node_count = 0;
+     */
+}
+
+void CPortals::Load()
+{
+    char buf[LINE_BUF + 1];
+
+    memset(buf, 0, LINE_BUF + 1);
+
+    Purge();
+
+    globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
+
+    FILE *in;
+
+    in = fopen(fn, "rt");
+
+    if (in == NULL) {
+        globalOutputStream() << "  ERROR - could not open file.\n";
+
+        return;
+    }
+
+    if (!fgets(buf, LINE_BUF, in)) {
+        fclose(in);
+
+        globalOutputStream() << "  ERROR - File ended prematurely.\n";
+
+        return;
+    }
+
+    if (strncmp("PRT1", buf, 4) != 0) {
+        fclose(in);
+
+        globalOutputStream() << "  ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
+
+        return;
+    }
+
+    if (!fgets(buf, LINE_BUF, in)) {
+        fclose(in);
+
+        globalOutputStream() << "  ERROR - File ended prematurely.\n";
+
+        return;
+    }
+
+    sscanf(buf, "%u", &node_count);
+/*
+    if(node_count > 0xFFFF)
+    {
+        fclose(in);
+
+        node_count = 0;
+
+        globalOutputStream() << "  ERROR - Extreme number of nodes, aborting.\n";
+
+        return;
+    }
+ */
+
+    if (!fgets(buf, LINE_BUF, in)) {
+        fclose(in);
+
+        node_count = 0;
+
+        globalOutputStream() << "  ERROR - File ended prematurely.\n";
+
+        return;
+    }
+
+    sscanf(buf, "%u", &portal_count);
+
+    if (portal_count > 0xFFFF) {
+        fclose(in);
+
+        portal_count = 0;
+        node_count = 0;
+
+        globalOutputStream() << "  ERROR - Extreme number of portals, aborting.\n";
+
+        return;
+    }
+
+    if (portal_count == 0) {
+        fclose(in);
+
+        portal_count = 0;
+        node_count = 0;
+
+        globalOutputStream() << "  ERROR - number of portals equals 0, aborting.\n";
+
+        return;
+    }
+
+//     node = new CBspNode[node_count];
+    portal = new CBspPortal[portal_count];
+    portal_sort = new int[portal_count];
+
+    unsigned int n;
+    bool first = true;
+    unsigned test_vals_1, test_vals_2;
+
+    hint_flags = false;
+
+    for (n = 0; n < portal_count;) {
+        if (!fgets(buf, LINE_BUF, in)) {
+            fclose(in);
+
+            Purge();
+
+            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", (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;
+                continue;
+            }
+
+            first = false;
+
+            fclose(in);
+
+            Purge();
+
+            globalOutputStream() << "  ERROR - Information for portal number " << n + 1 << " of " << portal_count
+                                 << " is not formatted correctly.\n";
+
+            return;
+        }
+
+        n++;
+    }
+
+    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;
+
+    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();
+}
+
+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;
+}
+
+void CPortalsRender::renderWireframe(Renderer &renderer, const VolumeTest &volume) const
+{
+    if (!portals.show_2d || portals.portal_count < 1) {
+        return;
+    }
+
+    renderer.SetState(g_shader_wireframe, Renderer::eWireframeOnly);
+
+    renderer.addRenderable(m_drawWireframe, g_matrix4_identity);
+}
+
+void CPortalsDrawWireframe::render(OpenGLBinding &GL, RenderStateFlags state) const
+{
+    unsigned int n, p;
+
+    for (n = 0; n < portals.portal_count; n++) {
+        glBegin(GL_LINE_LOOP);
+
+        for (p = 0; p < portals.portal[n].point_count; p++)
+            glVertex3fv(portals.portal[n].point[p].p);
+
+        glEnd();
+    }
+}
+
+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::renderSolid(Renderer &renderer, const VolumeTest &volume) const
+{
+    if (!portals.show_3d || portals.portal_count < 1) {
+        return;
+    }
+
+    CubicClipVolume clip = calculateCubicClipVolume(
+            matrix4_multiplied_by_matrix4(volume.GetProjection(), volume.GetModelview()));
+
+    if (portals.polygons) {
+        renderer.SetState(g_shader_solid, Renderer::eWireframeOnly);
+        renderer.SetState(g_shader_solid, Renderer::eFullMaterials);
+
+        m_drawSolid.clip = clip;
+        renderer.addRenderable(m_drawSolid, g_matrix4_identity);
+    }
+
+    if (portals.lines) {
+        renderer.SetState(g_shader_solid_outline, Renderer::eWireframeOnly);
+        renderer.SetState(g_shader_solid_outline, Renderer::eFullMaterials);
+
+        m_drawSolidOutline.clip = clip;
+        renderer.addRenderable(m_drawSolidOutline, g_matrix4_identity);
+    }
+}
+
+void CPortalsDrawSolid::render(OpenGLBinding &GL, RenderStateFlags state) const
+{
+    float trans = (100.0f - portals.trans_3d) / 100.0f;
+
+    unsigned int n, p;
+
+    if (portals.zbuffer != 0) {
+        float 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) clip.cam[1] - portals.portal[n].center.p[1];
+            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;
+        }
+
+        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;
+            }
+
+            if (portals.clip) {
+                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]) {
+                    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();
+        }
+    } else {
+        for (n = 0; n < portals.portal_count; n++) {
+            if (portals.polygons == 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;
+                }
+            }
+
+            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++)
+                glVertex3fv(portals.portal[n].point[p].p);
+
+            glEnd();
+        }
+    }
+}
+
+void CPortalsDrawSolidOutline::render(OpenGLBinding &GL, 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;
+            }
+        }
+
+        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();
+    }
+}