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"
29 #include "iglrender.h"
34 const int LINE_BUF = 1000;
37 CPortalsRender render;
39 int compare( const void *arg1, const void *arg2 ){
41 if ( portals.portal[*( (int *)arg1 )].dist > portals.portal[*( (int *)arg2 )].dist ) {
44 else if ( portals.portal[*( (int *)arg1 )].dist < portals.portal[*( (int *)arg2 )].dist ) {
52 CBspPortal::CBspPortal(){
53 memset( this, 0, sizeof( CBspPortal ) );
56 CBspPortal::~CBspPortal(){
61 bool CBspPortal::Build( char *def ){
67 if ( portals.hint_flags ) {
68 res_cnt = sscanf( def, "%u %d %d %d", &point_count, &dummy1, &dummy2, (int *)&hint );
72 sscanf( def, "%u", &point_count );
76 if ( point_count < 3 || ( portals.hint_flags && res_cnt < 4 ) ) {
80 point = new CBspPoint[point_count];
81 inner_point = new CBspPoint[point_count];
83 for ( n = 0; n < point_count; n++ )
85 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];
100 for ( i = 0; i < 3; i++ )
102 min[i] = point[n].p[i];
103 max[i] = point[n].p[i];
108 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++ )
126 inner_point[n].p[0] = ( 0.01f * center.p[0] ) + ( 0.99f * point[n].p[0] );
127 inner_point[n].p[1] = ( 0.01f * center.p[1] ) + ( 0.99f * point[n].p[1] );
128 inner_point[n].p[2] = ( 0.01f * center.p[2] ) + ( 0.99f * point[n].p[2] );
131 fp_color_random[0] = (float)( rand() & 0xff ) / 255.0f;
132 fp_color_random[1] = (float)( rand() & 0xff ) / 255.0f;
133 fp_color_random[2] = (float)( rand() & 0xff ) / 255.0f;
134 fp_color_random[3] = 1.0f;
139 CPortals::CPortals(){
140 memset( this, 0, sizeof( CPortals ) );
143 CPortals::~CPortals(){
147 void CPortals::Purge(){
149 delete[] portal_sort;
161 void CPortals::Load(){
162 char buf[LINE_BUF + 1];
164 memset( buf, 0, LINE_BUF + 1 );
168 globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
172 in = fopen( fn, "rt" );
175 globalOutputStream() << " ERROR - could not open file.\n";
180 if ( !fgets( buf, LINE_BUF, in ) ) {
183 globalOutputStream() << " ERROR - File ended prematurely.\n";
188 if ( strncmp( "PRT1", buf, 4 ) != 0 ) {
191 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
196 if ( !fgets( buf, LINE_BUF, in ) ) {
199 globalOutputStream() << " ERROR - File ended prematurely.\n";
204 sscanf( buf, "%u", &node_count );
206 if(node_count > 0xFFFF)
212 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
218 if ( !fgets( buf, LINE_BUF, in ) ) {
223 globalOutputStream() << " ERROR - File ended prematurely.\n";
228 sscanf( buf, "%u", &portal_count );
230 if ( portal_count > 0xFFFF ) {
236 globalOutputStream() << " ERROR - Extreme number of portals, aborting.\n";
241 if ( portal_count == 0 ) {
247 globalOutputStream() << " ERROR - number of portals equals 0, aborting.\n";
252 // node = new CBspNode[node_count];
253 portal = new CBspPortal[portal_count];
254 portal_sort = new int[portal_count];
258 unsigned test_vals_1, test_vals_2;
262 for ( n = 0; n < portal_count; )
264 if ( !fgets( buf, LINE_BUF, in ) ) {
269 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of " << portal_count << ".\n";
274 if ( !portal[n].Build( buf ) ) {
275 if ( first && sscanf( buf, "%d %d", (int *) &test_vals_1, (int *) &test_vals_2 ) == 1 ) { // skip additional counts of later data, not needed
276 // We can count on hint flags being in the file
287 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << portal_count << " is not formatted correctly.\n";
297 globalOutputStream() << " " << node_count << " portals read in.\n";
300 #include "math/matrix.h"
302 const char* g_state_solid = "$plugins/prtview/solid";
303 const char* g_state_solid_outline = "$plugins/prtview/solid_outline";
304 const char* g_state_wireframe = "$plugins/prtview/wireframe";
305 Shader* g_shader_solid = 0;
306 Shader* g_shader_solid_outline = 0;
307 Shader* g_shader_wireframe = 0;
309 void Portals_constructShaders(){
311 GlobalOpenGLStateLibrary().getDefaultState( state );
312 state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE;
313 state.m_sort = OpenGLState::eSortOverlayFirst;
314 state.m_linewidth = portals.width_2d * 0.5f;
315 state.m_colour[0] = portals.fp_color_2d[0];
316 state.m_colour[1] = portals.fp_color_2d[1];
317 state.m_colour[2] = portals.fp_color_2d[2];
318 state.m_colour[3] = portals.fp_color_2d[3];
319 if ( portals.aa_2d ) {
320 state.m_state |= RENDER_BLEND | RENDER_LINESMOOTH;
322 GlobalOpenGLStateLibrary().insert( g_state_wireframe, state );
324 GlobalOpenGLStateLibrary().getDefaultState( state );
325 state.m_state = RENDER_FILL | RENDER_BLEND | RENDER_COLOURWRITE | RENDER_COLOURCHANGE | RENDER_SMOOTH;
327 if ( portals.aa_3d ) {
328 state.m_state |= RENDER_POLYGONSMOOTH;
331 switch ( portals.zbuffer )
334 state.m_state |= RENDER_DEPTHTEST;
339 state.m_state |= RENDER_DEPTHTEST;
340 state.m_state |= RENDER_DEPTHWRITE;
344 state.m_state |= RENDER_FOG;
346 state.m_fog.mode = GL_EXP;
347 state.m_fog.density = 0.001f;
348 state.m_fog.start = 10.0f;
349 state.m_fog.end = 10000.0f;
350 state.m_fog.index = 0;
351 state.m_fog.colour[0] = portals.fp_color_fog[0];
352 state.m_fog.colour[1] = portals.fp_color_fog[1];
353 state.m_fog.colour[2] = portals.fp_color_fog[2];
354 state.m_fog.colour[3] = portals.fp_color_fog[3];
357 GlobalOpenGLStateLibrary().insert( g_state_solid, state );
359 GlobalOpenGLStateLibrary().getDefaultState( state );
360 state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE;
361 state.m_sort = OpenGLState::eSortOverlayFirst;
362 state.m_linewidth = portals.width_3d * 0.5f;
363 state.m_colour[0] = portals.fp_color_3d[0];
364 state.m_colour[1] = portals.fp_color_3d[1];
365 state.m_colour[2] = portals.fp_color_3d[2];
366 state.m_colour[3] = portals.fp_color_3d[3];
368 if ( portals.aa_3d ) {
369 state.m_state |= RENDER_LINESMOOTH;
372 switch ( portals.zbuffer )
375 state.m_state |= RENDER_DEPTHTEST;
380 state.m_state |= RENDER_DEPTHTEST;
381 state.m_state |= RENDER_DEPTHWRITE;
385 state.m_state |= RENDER_FOG;
387 state.m_fog.mode = GL_EXP;
388 state.m_fog.density = 0.001f;
389 state.m_fog.start = 10.0f;
390 state.m_fog.end = 10000.0f;
391 state.m_fog.index = 0;
392 state.m_fog.colour[0] = portals.fp_color_fog[0];
393 state.m_fog.colour[1] = portals.fp_color_fog[1];
394 state.m_fog.colour[2] = portals.fp_color_fog[2];
395 state.m_fog.colour[3] = portals.fp_color_fog[3];
398 GlobalOpenGLStateLibrary().insert( g_state_solid_outline, state );
400 g_shader_solid = GlobalShaderCache().capture( g_state_solid );
401 g_shader_solid_outline = GlobalShaderCache().capture( g_state_solid_outline );
402 g_shader_wireframe = GlobalShaderCache().capture( g_state_wireframe );
405 void Portals_destroyShaders(){
406 GlobalShaderCache().release( g_state_solid );
407 GlobalShaderCache().release( g_state_solid_outline );
408 GlobalShaderCache().release( g_state_wireframe );
409 GlobalOpenGLStateLibrary().erase( g_state_solid );
410 GlobalOpenGLStateLibrary().erase( g_state_solid_outline );
411 GlobalOpenGLStateLibrary().erase( g_state_wireframe );
414 void Portals_shadersChanged(){
415 Portals_destroyShaders();
417 Portals_constructShaders();
420 void CPortals::FixColors(){
421 fp_color_2d[0] = (float)GetRValue( color_2d ) / 255.0f;
422 fp_color_2d[1] = (float)GetGValue( color_2d ) / 255.0f;
423 fp_color_2d[2] = (float)GetBValue( color_2d ) / 255.0f;
424 fp_color_2d[3] = 1.0f;
426 fp_color_3d[0] = (float)GetRValue( color_3d ) / 255.0f;
427 fp_color_3d[1] = (float)GetGValue( color_3d ) / 255.0f;
428 fp_color_3d[2] = (float)GetBValue( color_3d ) / 255.0f;
429 fp_color_3d[3] = 1.0f;
431 fp_color_fog[0] = 0.0f; //(float)GetRValue(color_fog) / 255.0f;
432 fp_color_fog[1] = 0.0f; //(float)GetGValue(color_fog) / 255.0f;
433 fp_color_fog[2] = 0.0f; //(float)GetBValue(color_fog) / 255.0f;
434 fp_color_fog[3] = 1.0f;
437 void CPortalsRender::renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
438 if ( !portals.show_2d || portals.portal_count < 1 ) {
442 renderer.SetState( g_shader_wireframe, Renderer::eWireframeOnly );
444 renderer.addRenderable( m_drawWireframe, g_matrix4_identity );
447 void CPortalsDrawWireframe::render( RenderStateFlags state ) const {
450 for ( n = 0; n < portals.portal_count; n++ )
452 glBegin( GL_LINE_LOOP );
454 for ( p = 0; p < portals.portal[n].point_count; p++ )
455 glVertex3fv( portals.portal[n].point[p].p );
461 CubicClipVolume calculateCubicClipVolume( const Matrix4& viewproj ){
462 CubicClipVolume clip;
463 clip.cam = vector4_projected(
464 matrix4_transformed_vector4(
465 matrix4_full_inverse( viewproj ),
466 Vector4( 0, 0, -1, 1 )
469 clip.min[0] = clip.cam[0] + ( portals.clip_range * 64.0f );
470 clip.min[1] = clip.cam[1] + ( portals.clip_range * 64.0f );
471 clip.min[2] = clip.cam[2] + ( portals.clip_range * 64.0f );
472 clip.max[0] = clip.cam[0] - ( portals.clip_range * 64.0f );
473 clip.max[1] = clip.cam[1] - ( portals.clip_range * 64.0f );
474 clip.max[2] = clip.cam[2] - ( portals.clip_range * 64.0f );
478 void CPortalsRender::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
479 if ( !portals.show_3d || portals.portal_count < 1 ) {
483 CubicClipVolume clip = calculateCubicClipVolume( matrix4_multiplied_by_matrix4( volume.GetProjection(), volume.GetModelview() ) );
485 if ( portals.polygons ) {
486 renderer.SetState( g_shader_solid, Renderer::eWireframeOnly );
487 renderer.SetState( g_shader_solid, Renderer::eFullMaterials );
489 m_drawSolid.clip = clip;
490 renderer.addRenderable( m_drawSolid, g_matrix4_identity );
493 if ( portals.lines ) {
494 renderer.SetState( g_shader_solid_outline, Renderer::eWireframeOnly );
495 renderer.SetState( g_shader_solid_outline, Renderer::eFullMaterials );
497 m_drawSolidOutline.clip = clip;
498 renderer.addRenderable( m_drawSolidOutline, g_matrix4_identity );
502 void CPortalsDrawSolid::render( RenderStateFlags state ) const {
503 float trans = ( 100.0f - portals.trans_3d ) / 100.0f;
507 if ( portals.zbuffer != 0 ) {
510 for ( n = 0; n < portals.portal_count; n++ )
512 d = (float)clip.cam[0] - portals.portal[n].center.p[0];
513 portals.portal[n].dist = d * d;
515 d = (float)clip.cam[1] - portals.portal[n].center.p[1];
516 portals.portal[n].dist += d * d;
518 d = (float)clip.cam[2] - portals.portal[n].center.p[2];
519 portals.portal[n].dist += d * d;
521 portals.portal_sort[n] = n;
524 qsort( portals.portal_sort, portals.portal_count, 4, compare );
526 for ( n = 0; n < portals.portal_count; n++ )
528 if ( portals.polygons == 2 && !portals.portal[portals.portal_sort[n]].hint ) {
532 if ( portals.clip ) {
533 if ( clip.min[0] < portals.portal[portals.portal_sort[n]].min[0] ) {
536 else if ( clip.min[1] < portals.portal[portals.portal_sort[n]].min[1] ) {
539 else if ( clip.min[2] < portals.portal[portals.portal_sort[n]].min[2] ) {
542 else if ( clip.max[0] > portals.portal[portals.portal_sort[n]].max[0] ) {
545 else if ( clip.max[1] > portals.portal[portals.portal_sort[n]].max[1] ) {
548 else if ( clip.max[2] > portals.portal[portals.portal_sort[n]].max[2] ) {
553 glColor4f( portals.portal[portals.portal_sort[n]].fp_color_random[0], portals.portal[portals.portal_sort[n]].fp_color_random[1],
554 portals.portal[portals.portal_sort[n]].fp_color_random[2], trans );
556 glBegin( GL_POLYGON );
558 for ( p = 0; p < portals.portal[portals.portal_sort[n]].point_count; p++ )
559 glVertex3fv( portals.portal[portals.portal_sort[n]].point[p].p );
566 for ( n = 0; n < portals.portal_count; n++ )
568 if ( portals.polygons == 2 && !portals.portal[n].hint ) {
572 if ( portals.clip ) {
573 if ( clip.min[0] < portals.portal[n].min[0] ) {
576 else if ( clip.min[1] < portals.portal[n].min[1] ) {
579 else if ( clip.min[2] < portals.portal[n].min[2] ) {
582 else if ( clip.max[0] > portals.portal[n].max[0] ) {
585 else if ( clip.max[1] > portals.portal[n].max[1] ) {
588 else if ( clip.max[2] > portals.portal[n].max[2] ) {
593 glColor4f( portals.portal[n].fp_color_random[0], portals.portal[n].fp_color_random[1],
594 portals.portal[n].fp_color_random[2], trans );
596 glBegin( GL_POLYGON );
598 for ( p = 0; p < portals.portal[n].point_count; p++ )
599 glVertex3fv( portals.portal[n].point[p].p );
606 void CPortalsDrawSolidOutline::render( RenderStateFlags state ) const {
607 for ( unsigned int n = 0; n < portals.portal_count; n++ )
609 if ( portals.lines == 2 && !portals.portal[n].hint ) {
613 if ( portals.clip ) {
614 if ( clip.min[0] < portals.portal[n].min[0] ) {
617 if ( clip.min[1] < portals.portal[n].min[1] ) {
620 if ( clip.min[2] < portals.portal[n].min[2] ) {
623 if ( clip.max[0] > portals.portal[n].max[0] ) {
626 if ( clip.max[1] > portals.portal[n].max[1] ) {
629 if ( clip.max[2] > portals.portal[n].max[2] ) {
634 glBegin( GL_LINE_LOOP );
636 for ( unsigned int p = 0; p < portals.portal[n].point_count; p++ )
637 glVertex3fv( portals.portal[n].inner_point[p].p );