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
21 #include "globaldefs.h"
33 #include "iglrender.h"
38 const int LINE_BUF = 1000;
41 CPortalsRender render;
43 int compare(const void *arg1, const void *arg2)
46 if (portals.portal[*((int *) arg1)].dist > portals.portal[*((int *) arg2)].dist) {
48 } else if (portals.portal[*((int *) arg1)].dist < portals.portal[*((int *) arg2)].dist) {
56 CBspPortal::CBspPortal()
58 memset(this, 0, sizeof(CBspPortal));
61 CBspPortal::~CBspPortal()
67 bool CBspPortal::Build(char *def)
74 if (portals.hint_flags) {
75 res_cnt = sscanf(def, "%u %d %d %d", &point_count, &dummy1, &dummy2, (int *) &hint);
77 sscanf(def, "%u", &point_count);
81 if (point_count < 3 || (portals.hint_flags && res_cnt < 4)) {
85 point = new CBspPoint[point_count];
86 inner_point = new CBspPoint[point_count];
88 for (n = 0; n < point_count; n++) {
89 for (; *c != 0 && *c != '('; c++) {}
97 sscanf(c, "%f %f %f", point[n].p, point[n].p + 1, point[n].p + 2);
99 center.p[0] += point[n].p[0];
100 center.p[1] += point[n].p[1];
101 center.p[2] += point[n].p[2];
104 for (i = 0; i < 3; i++) {
105 min[i] = point[n].p[i];
106 max[i] = point[n].p[i];
109 for (i = 0; i < 3; i++) {
110 if (min[i] > point[n].p[i]) {
111 min[i] = point[n].p[i];
113 if (max[i] < point[n].p[i]) {
114 max[i] = point[n].p[i];
120 center.p[0] /= (float) point_count;
121 center.p[1] /= (float) point_count;
122 center.p[2] /= (float) point_count;
124 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");
178 globalOutputStream() << " ERROR - could not open file.\n";
183 if (!fgets(buf, LINE_BUF, in)) {
186 globalOutputStream() << " ERROR - File ended prematurely.\n";
191 if (strncmp("PRT1", buf, 4) != 0) {
194 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
199 if (!fgets(buf, LINE_BUF, in)) {
202 globalOutputStream() << " ERROR - File ended prematurely.\n";
207 sscanf(buf, "%u", &node_count);
209 if(node_count > 0xFFFF)
215 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
221 if (!fgets(buf, LINE_BUF, in)) {
226 globalOutputStream() << " ERROR - File ended prematurely.\n";
231 sscanf(buf, "%u", &portal_count);
233 if (portal_count > 0xFFFF) {
239 globalOutputStream() << " ERROR - Extreme number of portals, aborting.\n";
244 if (portal_count == 0) {
250 globalOutputStream() << " ERROR - number of portals equals 0, aborting.\n";
255 // node = new CBspNode[node_count];
256 portal = new CBspPortal[portal_count];
257 portal_sort = new int[portal_count];
261 unsigned test_vals_1, test_vals_2;
265 for (n = 0; n < portal_count;) {
266 if (!fgets(buf, LINE_BUF, in)) {
271 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of "
272 << portal_count << ".\n";
277 if (!portal[n].Build(buf)) {
278 if (first && sscanf(buf, "%d %d", (int *) &test_vals_1, (int *) &test_vals_2) ==
279 1) { // skip additional counts of later data, not needed
280 // We can count on hint flags being in the file
291 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << portal_count
292 << " is not formatted correctly.\n";
302 globalOutputStream() << " " << node_count << " portals read in.\n";
305 #include "math/matrix.h"
307 const char *g_state_solid = "$plugins/prtview/solid";
308 const char *g_state_solid_outline = "$plugins/prtview/solid_outline";
309 const char *g_state_wireframe = "$plugins/prtview/wireframe";
310 Shader *g_shader_solid = 0;
311 Shader *g_shader_solid_outline = 0;
312 Shader *g_shader_wireframe = 0;
314 void Portals_constructShaders()
317 GlobalOpenGLStateLibrary().getDefaultState(state);
318 state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE;
319 state.m_sort = OpenGLState::eSortOverlayFirst;
320 state.m_linewidth = portals.width_2d * 0.5f;
321 state.m_colour[0] = portals.fp_color_2d[0];
322 state.m_colour[1] = portals.fp_color_2d[1];
323 state.m_colour[2] = portals.fp_color_2d[2];
324 state.m_colour[3] = portals.fp_color_2d[3];
326 state.m_state |= RENDER_BLEND | RENDER_LINESMOOTH;
328 GlobalOpenGLStateLibrary().insert(g_state_wireframe, state);
330 GlobalOpenGLStateLibrary().getDefaultState(state);
331 state.m_state = RENDER_FILL | RENDER_BLEND | RENDER_COLOURWRITE | RENDER_COLOURCHANGE | RENDER_SMOOTH;
334 state.m_state |= RENDER_POLYGONSMOOTH;
337 switch (portals.zbuffer) {
339 state.m_state |= RENDER_DEPTHTEST;
344 state.m_state |= RENDER_DEPTHTEST;
345 state.m_state |= RENDER_DEPTHWRITE;
349 state.m_state |= RENDER_FOG;
351 state.m_fog.mode = GL_EXP;
352 state.m_fog.density = 0.001f;
353 state.m_fog.start = 10.0f;
354 state.m_fog.end = 10000.0f;
355 state.m_fog.index = 0;
356 state.m_fog.colour[0] = portals.fp_color_fog[0];
357 state.m_fog.colour[1] = portals.fp_color_fog[1];
358 state.m_fog.colour[2] = portals.fp_color_fog[2];
359 state.m_fog.colour[3] = portals.fp_color_fog[3];
362 GlobalOpenGLStateLibrary().insert(g_state_solid, state);
364 GlobalOpenGLStateLibrary().getDefaultState(state);
365 state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE;
366 state.m_sort = OpenGLState::eSortOverlayFirst;
367 state.m_linewidth = portals.width_3d * 0.5f;
368 state.m_colour[0] = portals.fp_color_3d[0];
369 state.m_colour[1] = portals.fp_color_3d[1];
370 state.m_colour[2] = portals.fp_color_3d[2];
371 state.m_colour[3] = portals.fp_color_3d[3];
374 state.m_state |= RENDER_LINESMOOTH;
377 switch (portals.zbuffer) {
379 state.m_state |= RENDER_DEPTHTEST;
384 state.m_state |= RENDER_DEPTHTEST;
385 state.m_state |= RENDER_DEPTHWRITE;
389 state.m_state |= RENDER_FOG;
391 state.m_fog.mode = GL_EXP;
392 state.m_fog.density = 0.001f;
393 state.m_fog.start = 10.0f;
394 state.m_fog.end = 10000.0f;
395 state.m_fog.index = 0;
396 state.m_fog.colour[0] = portals.fp_color_fog[0];
397 state.m_fog.colour[1] = portals.fp_color_fog[1];
398 state.m_fog.colour[2] = portals.fp_color_fog[2];
399 state.m_fog.colour[3] = portals.fp_color_fog[3];
402 GlobalOpenGLStateLibrary().insert(g_state_solid_outline, state);
404 g_shader_solid = GlobalShaderCache().capture(g_state_solid);
405 g_shader_solid_outline = GlobalShaderCache().capture(g_state_solid_outline);
406 g_shader_wireframe = GlobalShaderCache().capture(g_state_wireframe);
409 void Portals_destroyShaders()
411 GlobalShaderCache().release(g_state_solid);
412 GlobalShaderCache().release(g_state_solid_outline);
413 GlobalShaderCache().release(g_state_wireframe);
414 GlobalOpenGLStateLibrary().erase(g_state_solid);
415 GlobalOpenGLStateLibrary().erase(g_state_solid_outline);
416 GlobalOpenGLStateLibrary().erase(g_state_wireframe);
419 void Portals_shadersChanged()
421 Portals_destroyShaders();
423 Portals_constructShaders();
426 void CPortals::FixColors()
428 fp_color_2d[0] = (float) GetRValue(color_2d) / 255.0f;
429 fp_color_2d[1] = (float) GetGValue(color_2d) / 255.0f;
430 fp_color_2d[2] = (float) GetBValue(color_2d) / 255.0f;
431 fp_color_2d[3] = 1.0f;
433 fp_color_3d[0] = (float) GetRValue(color_3d) / 255.0f;
434 fp_color_3d[1] = (float) GetGValue(color_3d) / 255.0f;
435 fp_color_3d[2] = (float) GetBValue(color_3d) / 255.0f;
436 fp_color_3d[3] = 1.0f;
438 fp_color_fog[0] = 0.0f; //(float)GetRValue(color_fog) / 255.0f;
439 fp_color_fog[1] = 0.0f; //(float)GetGValue(color_fog) / 255.0f;
440 fp_color_fog[2] = 0.0f; //(float)GetBValue(color_fog) / 255.0f;
441 fp_color_fog[3] = 1.0f;
444 void CPortalsRender::renderWireframe(Renderer &renderer, const VolumeTest &volume) const
446 if (!portals.show_2d || portals.portal_count < 1) {
450 renderer.SetState(g_shader_wireframe, Renderer::eWireframeOnly);
452 renderer.addRenderable(m_drawWireframe, g_matrix4_identity);
455 void CPortalsDrawWireframe::render(RenderStateFlags state) const
459 for (n = 0; n < portals.portal_count; n++) {
460 glBegin(GL_LINE_LOOP);
462 for (p = 0; p < portals.portal[n].point_count; p++)
463 glVertex3fv(portals.portal[n].point[p].p);
469 CubicClipVolume calculateCubicClipVolume(const Matrix4 &viewproj)
471 CubicClipVolume clip;
472 clip.cam = vector4_projected(
473 matrix4_transformed_vector4(
474 matrix4_full_inverse(viewproj),
478 clip.min[0] = clip.cam[0] + (portals.clip_range * 64.0f);
479 clip.min[1] = clip.cam[1] + (portals.clip_range * 64.0f);
480 clip.min[2] = clip.cam[2] + (portals.clip_range * 64.0f);
481 clip.max[0] = clip.cam[0] - (portals.clip_range * 64.0f);
482 clip.max[1] = clip.cam[1] - (portals.clip_range * 64.0f);
483 clip.max[2] = clip.cam[2] - (portals.clip_range * 64.0f);
487 void CPortalsRender::renderSolid(Renderer &renderer, const VolumeTest &volume) const
489 if (!portals.show_3d || portals.portal_count < 1) {
493 CubicClipVolume clip = calculateCubicClipVolume(
494 matrix4_multiplied_by_matrix4(volume.GetProjection(), volume.GetModelview()));
496 if (portals.polygons) {
497 renderer.SetState(g_shader_solid, Renderer::eWireframeOnly);
498 renderer.SetState(g_shader_solid, Renderer::eFullMaterials);
500 m_drawSolid.clip = clip;
501 renderer.addRenderable(m_drawSolid, g_matrix4_identity);
505 renderer.SetState(g_shader_solid_outline, Renderer::eWireframeOnly);
506 renderer.SetState(g_shader_solid_outline, Renderer::eFullMaterials);
508 m_drawSolidOutline.clip = clip;
509 renderer.addRenderable(m_drawSolidOutline, g_matrix4_identity);
513 void CPortalsDrawSolid::render(RenderStateFlags state) const
515 float trans = (100.0f - portals.trans_3d) / 100.0f;
519 if (portals.zbuffer != 0) {
522 for (n = 0; n < portals.portal_count; n++) {
523 d = (float) clip.cam[0] - portals.portal[n].center.p[0];
524 portals.portal[n].dist = d * d;
526 d = (float) clip.cam[1] - portals.portal[n].center.p[1];
527 portals.portal[n].dist += d * d;
529 d = (float) clip.cam[2] - portals.portal[n].center.p[2];
530 portals.portal[n].dist += d * d;
532 portals.portal_sort[n] = n;
535 qsort(portals.portal_sort, portals.portal_count, 4, compare);
537 for (n = 0; n < portals.portal_count; n++) {
538 if (portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint) {
543 if (clip.min[0] < portals.portal[portals.portal_sort[n]].min[0]) {
545 } else if (clip.min[1] < portals.portal[portals.portal_sort[n]].min[1]) {
547 } else if (clip.min[2] < portals.portal[portals.portal_sort[n]].min[2]) {
549 } else if (clip.max[0] > portals.portal[portals.portal_sort[n]].max[0]) {
551 } else if (clip.max[1] > portals.portal[portals.portal_sort[n]].max[1]) {
553 } else if (clip.max[2] > portals.portal[portals.portal_sort[n]].max[2]) {
558 glColor4f(portals.portal[portals.portal_sort[n]].fp_color_random[0],
559 portals.portal[portals.portal_sort[n]].fp_color_random[1],
560 portals.portal[portals.portal_sort[n]].fp_color_random[2], trans);
564 for (p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++)
565 glVertex3fv(portals.portal[portals.portal_sort[n]].point[p].p);
570 for (n = 0; n < portals.portal_count; n++) {
571 if (portals.polygons == 2 && !portals.portal[n].hint) {
576 if (clip.min[0] < portals.portal[n].min[0]) {
578 } else if (clip.min[1] < portals.portal[n].min[1]) {
580 } else if (clip.min[2] < portals.portal[n].min[2]) {
582 } else if (clip.max[0] > portals.portal[n].max[0]) {
584 } else if (clip.max[1] > portals.portal[n].max[1]) {
586 } else if (clip.max[2] > portals.portal[n].max[2]) {
591 glColor4f(portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
592 portals.portal[n].fp_color_random[2], trans);
596 for (p = 0; p < portals.portal[n].point_count; p++)
597 glVertex3fv(portals.portal[n].point[p].p);
604 void CPortalsDrawSolidOutline::render(RenderStateFlags state) const
606 for (unsigned int n = 0; n < portals.portal_count; n++) {
607 if (portals.lines == 2 && !portals.portal[n].hint) {
612 if (clip.min[0] < portals.portal[n].min[0]) {
615 if (clip.min[1] < portals.portal[n].min[1]) {
618 if (clip.min[2] < portals.portal[n].min[2]) {
621 if (clip.max[0] > portals.portal[n].max[0]) {
624 if (clip.max[1] > portals.portal[n].max[1]) {
627 if (clip.max[2] > portals.portal[n].max[2]) {
632 glBegin(GL_LINE_LOOP);
634 for (unsigned int p = 0; p < portals.portal[n].point_count; p++)
635 glVertex3fv(portals.portal[n].inner_point[p].p);