2 PrtView plugin for GtkRadiant
3 Copyright (C) 2001 Geoffrey Dewan, Loki software and qeradiant.com
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "iglrender.h"
36 CPortalsRender render;
38 int compare( const void *arg1, const void *arg2 )
41 if(portals.portal[*((int *)arg1)].dist > portals.portal[*((int *)arg2)].dist)
43 else if(portals.portal[*((int *)arg1)].dist < portals.portal[*((int *)arg2)].dist)
50 CBspPortal::CBspPortal()
52 memset(this, 0, sizeof(CBspPortal));
55 CBspPortal::~CBspPortal()
61 bool CBspPortal::Build(char *def)
68 if(portals.hint_flags)
70 res_cnt = sscanf(def, "%u %d %d %d", &point_count, &dummy1, &dummy2, (int *)&hint);
74 sscanf(def, "%u", &point_count);
78 if(point_count < 3 || (portals.hint_flags && res_cnt < 4))
81 point = new CBspPoint[point_count];
82 inner_point = new CBspPoint[point_count];
84 for(n = 0; n < point_count; n++)
86 for(; *c != 0 && *c != '('; c++);
93 sscanf(c, "%f %f %f", point[n].p, point[n].p+1, point[n].p+2);
95 center.p[0] += point[n].p[0];
96 center.p[1] += point[n].p[1];
97 center.p[2] += point[n].p[2];
101 for(i = 0; i < 3; i++)
103 min[i] = point[n].p[i];
104 max[i] = point[n].p[i];
109 for(i = 0; i < 3; i++)
111 if(min[i] > point[n].p[i])
112 min[i] = point[n].p[i];
113 if(max[i] < point[n].p[i])
114 max[i] = point[n].p[i];
119 center.p[0] /= (float)point_count;
120 center.p[1] /= (float)point_count;
121 center.p[2] /= (float)point_count;
123 for(n = 0; n < point_count; n++)
125 inner_point[n].p[0] = (0.01f * center.p[0]) + (0.99f * point[n].p[0]);
126 inner_point[n].p[1] = (0.01f * center.p[1]) + (0.99f * point[n].p[1]);
127 inner_point[n].p[2] = (0.01f * center.p[2]) + (0.99f * point[n].p[2]);
130 fp_color_random[0] = (float)(rand() & 0xff) / 255.0f;
131 fp_color_random[1] = (float)(rand() & 0xff) / 255.0f;
132 fp_color_random[2] = (float)(rand() & 0xff) / 255.0f;
133 fp_color_random[3] = 1.0f;
140 memset(this, 0, sizeof(CPortals));
143 CPortals::~CPortals()
148 void CPortals::Purge()
151 delete[] portal_sort;
163 void CPortals::Load()
165 char buf[LINE_BUF+1];
167 memset(buf, 0, LINE_BUF + 1);
171 globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
175 in = fopen(fn, "rt");
179 globalOutputStream() << " ERROR - could not open file.\n";
184 if(!fgets(buf, LINE_BUF, in))
188 globalOutputStream() << " ERROR - File ended prematurely.\n";
193 if(strncmp("PRT1", buf, 4) != 0)
197 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
202 if(!fgets(buf, LINE_BUF, in))
206 globalOutputStream() << " ERROR - File ended prematurely.\n";
211 sscanf(buf, "%u", &node_count);
213 if(node_count > 0xFFFF)
219 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
225 if(!fgets(buf, LINE_BUF, in))
231 globalOutputStream() << " ERROR - File ended prematurely.\n";
236 sscanf(buf, "%u", &portal_count);
238 if(portal_count > 0xFFFF)
245 globalOutputStream() << " ERROR - Extreme number of portals, aborting.\n";
250 if(portal_count == 0)
257 globalOutputStream() << " ERROR - number of portals equals 0, aborting.\n";
262 // node = new CBspNode[node_count];
263 portal = new CBspPortal[portal_count];
264 portal_sort = new int[portal_count];
268 unsigned test_vals_1, test_vals_2;
272 for(n = 0; n < portal_count; )
274 if(!fgets(buf, LINE_BUF, in))
280 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of " << portal_count << ".\n";
285 if(!portal[n].Build(buf))
287 if(first && sscanf(buf, "%d %d", &test_vals_1, &test_vals_2) == 1) // skip additional counts of later data, not needed
289 // We can count on hint flags being in the file
300 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << portal_count << " is not formatted correctly.\n";
310 globalOutputStream() << " " << node_count << " portals read in.\n";
313 #include "math/matrix.h"
315 const char* g_state_solid = "$plugins/prtview/solid";
316 const char* g_state_solid_outline = "$plugins/prtview/solid_outline";
317 const char* g_state_wireframe = "$plugins/prtview/wireframe";
318 Shader* g_shader_solid = 0;
319 Shader* g_shader_solid_outline = 0;
320 Shader* g_shader_wireframe = 0;
322 void Portals_constructShaders()
325 GlobalOpenGLStateLibrary().getDefaultState(state);
326 state.m_state = RENDER_COLOURWRITE|RENDER_DEPTHWRITE;
327 state.m_sort = OpenGLState::eSortOverlayFirst;
328 state.m_linewidth = portals.width_2d * 0.5f;
329 state.m_colour[0] = portals.fp_color_2d[0];
330 state.m_colour[1] = portals.fp_color_2d[1];
331 state.m_colour[2] = portals.fp_color_2d[2];
332 state.m_colour[3] = portals.fp_color_2d[3];
335 state.m_state |= RENDER_BLEND|RENDER_LINESMOOTH;
337 GlobalOpenGLStateLibrary().insert(g_state_wireframe, state);
339 GlobalOpenGLStateLibrary().getDefaultState(state);
340 state.m_state = RENDER_FILL|RENDER_BLEND|RENDER_COLOURWRITE|RENDER_COLOURCHANGE|RENDER_SMOOTH;
344 state.m_state |= RENDER_POLYGONSMOOTH;
347 switch(portals.zbuffer)
350 state.m_state |= RENDER_DEPTHTEST;
355 state.m_state |= RENDER_DEPTHTEST;
356 state.m_state |= RENDER_DEPTHWRITE;
361 state.m_state |= RENDER_FOG;
363 state.m_fog.mode = GL_EXP;
364 state.m_fog.density = 0.001f;
365 state.m_fog.start = 10.0f;
366 state.m_fog.end = 10000.0f;
367 state.m_fog.index = 0;
368 state.m_fog.colour[0] = portals.fp_color_fog[0];
369 state.m_fog.colour[1] = portals.fp_color_fog[1];
370 state.m_fog.colour[2] = portals.fp_color_fog[2];
371 state.m_fog.colour[3] = portals.fp_color_fog[3];
374 GlobalOpenGLStateLibrary().insert(g_state_solid, state);
376 GlobalOpenGLStateLibrary().getDefaultState(state);
377 state.m_state = RENDER_COLOURWRITE|RENDER_DEPTHWRITE;
378 state.m_sort = OpenGLState::eSortOverlayFirst;
379 state.m_linewidth = portals.width_3d * 0.5f;
380 state.m_colour[0] = portals.fp_color_3d[0];
381 state.m_colour[1] = portals.fp_color_3d[1];
382 state.m_colour[2] = portals.fp_color_3d[2];
383 state.m_colour[3] = portals.fp_color_3d[3];
387 state.m_state |= RENDER_LINESMOOTH;
390 switch(portals.zbuffer)
393 state.m_state |= RENDER_DEPTHTEST;
398 state.m_state |= RENDER_DEPTHTEST;
399 state.m_state |= RENDER_DEPTHWRITE;
404 state.m_state |= RENDER_FOG;
406 state.m_fog.mode = GL_EXP;
407 state.m_fog.density = 0.001f;
408 state.m_fog.start = 10.0f;
409 state.m_fog.end = 10000.0f;
410 state.m_fog.index = 0;
411 state.m_fog.colour[0] = portals.fp_color_fog[0];
412 state.m_fog.colour[1] = portals.fp_color_fog[1];
413 state.m_fog.colour[2] = portals.fp_color_fog[2];
414 state.m_fog.colour[3] = portals.fp_color_fog[3];
417 GlobalOpenGLStateLibrary().insert(g_state_solid_outline, state);
419 g_shader_solid = GlobalShaderCache().capture(g_state_solid);
420 g_shader_solid_outline = GlobalShaderCache().capture(g_state_solid_outline);
421 g_shader_wireframe = GlobalShaderCache().capture(g_state_wireframe);
424 void Portals_destroyShaders()
426 GlobalShaderCache().release(g_state_solid);
427 GlobalShaderCache().release(g_state_solid_outline);
428 GlobalShaderCache().release(g_state_wireframe);
429 GlobalOpenGLStateLibrary().erase(g_state_solid);
430 GlobalOpenGLStateLibrary().erase(g_state_solid_outline);
431 GlobalOpenGLStateLibrary().erase(g_state_wireframe);
434 void Portals_shadersChanged()
436 Portals_destroyShaders();
438 Portals_constructShaders();
441 void CPortals::FixColors()
443 fp_color_2d[0] = (float)GetRValue(color_2d) / 255.0f;
444 fp_color_2d[1] = (float)GetGValue(color_2d) / 255.0f;
445 fp_color_2d[2] = (float)GetBValue(color_2d) / 255.0f;
446 fp_color_2d[3] = 1.0f;
448 fp_color_3d[0] = (float)GetRValue(color_3d) / 255.0f;
449 fp_color_3d[1] = (float)GetGValue(color_3d) / 255.0f;
450 fp_color_3d[2] = (float)GetBValue(color_3d) / 255.0f;
451 fp_color_3d[3] = 1.0f;
453 fp_color_fog[0] = 0.0f;//(float)GetRValue(color_fog) / 255.0f;
454 fp_color_fog[1] = 0.0f;//(float)GetGValue(color_fog) / 255.0f;
455 fp_color_fog[2] = 0.0f;//(float)GetBValue(color_fog) / 255.0f;
456 fp_color_fog[3] = 1.0f;
459 void CPortalsRender::renderWireframe(Renderer& renderer, const VolumeTest& volume) const
461 if(!portals.show_2d || portals.portal_count < 1)
464 renderer.SetState(g_shader_wireframe, Renderer::eWireframeOnly);
466 renderer.addRenderable(m_drawWireframe, g_matrix4_identity);
469 void CPortalsDrawWireframe::render(RenderStateFlags state) const
473 for(n = 0; n < portals.portal_count; n++)
475 glBegin(GL_LINE_LOOP);
477 for(p = 0; p < portals.portal[n].point_count; p++)
478 glVertex3fv(portals.portal[n].point[p].p);
484 CubicClipVolume calculateCubicClipVolume(const Matrix4& viewproj)
486 CubicClipVolume clip;
487 clip.cam = vector4_projected(
488 matrix4_transformed_vector4(
489 matrix4_full_inverse(viewproj),
493 clip.min[0] = clip.cam[0] + (portals.clip_range * 64.0f);
494 clip.min[1] = clip.cam[1] + (portals.clip_range * 64.0f);
495 clip.min[2] = clip.cam[2] + (portals.clip_range * 64.0f);
496 clip.max[0] = clip.cam[0] - (portals.clip_range * 64.0f);
497 clip.max[1] = clip.cam[1] - (portals.clip_range * 64.0f);
498 clip.max[2] = clip.cam[2] - (portals.clip_range * 64.0f);
502 void CPortalsRender::renderSolid(Renderer& renderer, const VolumeTest& volume) const
504 if(!portals.show_3d || portals.portal_count < 1)
507 CubicClipVolume clip = calculateCubicClipVolume(matrix4_multiplied_by_matrix4(volume.GetProjection(), volume.GetModelview()));
511 renderer.SetState(g_shader_solid, Renderer::eWireframeOnly);
512 renderer.SetState(g_shader_solid, Renderer::eFullMaterials);
514 m_drawSolid.clip = clip;
515 renderer.addRenderable(m_drawSolid, g_matrix4_identity);
520 renderer.SetState(g_shader_solid_outline, Renderer::eWireframeOnly);
521 renderer.SetState(g_shader_solid_outline, Renderer::eFullMaterials);
523 m_drawSolidOutline.clip = clip;
524 renderer.addRenderable(m_drawSolidOutline, g_matrix4_identity);
528 void CPortalsDrawSolid::render(RenderStateFlags state) const
530 float trans = (100.0f - portals.trans_3d) / 100.0f;
534 if(portals.zbuffer != 0)
538 for(n = 0; n < portals.portal_count; n++)
540 d = (float)clip.cam[0] - portals.portal[n].center.p[0];
541 portals.portal[n].dist = d * d;
543 d = (float)clip.cam[1] - portals.portal[n].center.p[1];
544 portals.portal[n].dist += d * d;
546 d = (float)clip.cam[2] - portals.portal[n].center.p[2];
547 portals.portal[n].dist += d * d;
549 portals.portal_sort[n] = n;
552 qsort(portals.portal_sort, portals.portal_count, 4, compare);
554 for(n = 0; n < portals.portal_count; n++)
556 if(portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint)
561 if(clip.min[0] < portals.portal[portals.portal_sort[n]].min[0])
563 else if(clip.min[1] < portals.portal[portals.portal_sort[n]].min[1])
565 else if(clip.min[2] < portals.portal[portals.portal_sort[n]].min[2])
567 else if(clip.max[0] > portals.portal[portals.portal_sort[n]].max[0])
569 else if(clip.max[1] > portals.portal[portals.portal_sort[n]].max[1])
571 else if(clip.max[2] > portals.portal[portals.portal_sort[n]].max[2])
575 glColor4f(portals.portal[portals.portal_sort[n]].fp_color_random[0], portals.portal[portals.portal_sort[n]].fp_color_random[1],
576 portals.portal[portals.portal_sort[n]].fp_color_random[2], trans);
580 for(p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++)
581 glVertex3fv(portals.portal[portals.portal_sort[n]].point[p].p);
588 for(n = 0; n < portals.portal_count; n++)
590 if(portals.polygons == 2 && !portals.portal[n].hint)
595 if(clip.min[0] < portals.portal[n].min[0])
597 else if(clip.min[1] < portals.portal[n].min[1])
599 else if(clip.min[2] < portals.portal[n].min[2])
601 else if(clip.max[0] > portals.portal[n].max[0])
603 else if(clip.max[1] > portals.portal[n].max[1])
605 else if(clip.max[2] > portals.portal[n].max[2])
609 glColor4f(portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
610 portals.portal[n].fp_color_random[2], trans);
614 for(p = 0; p < portals.portal[n].point_count; p++)
615 glVertex3fv(portals.portal[n].point[p].p);
622 void CPortalsDrawSolidOutline::render(RenderStateFlags state) const
624 for(int n = 0; n < portals.portal_count; n++)
626 if(portals.lines == 2 && !portals.portal[n].hint)
631 if(clip.min[0] < portals.portal[n].min[0])
633 else if(clip.min[1] < portals.portal[n].min[1])
635 else if(clip.min[2] < portals.portal[n].min[2])
637 else if(clip.max[0] > portals.portal[n].max[0])
639 else if(clip.max[1] > portals.portal[n].max[1])
641 else if(clip.max[2] > portals.portal[n].max[2])
645 glBegin(GL_LINE_LOOP);
647 for(int p = 0; p < portals.portal[n].point_count; p++)
648 glVertex3fv(portals.portal[n].inner_point[p].p);