]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DVisDrawer.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / contrib / bobtoolz / DVisDrawer.cpp
1 /*
2    BobToolz plugin for GtkRadiant
3    Copyright (C) 2001 Gordon Biggans
4
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.
9
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.
14
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
18  */
19
20 // BobView.cpp: implementation of the DVisDrawer class.
21 //
22 //////////////////////////////////////////////////////////////////////
23
24 #include "DVisDrawer.h"
25
26 #include "iglrender.h"
27 #include "math/matrix.h"
28
29 #include <list>
30 #include "str.h"
31
32 #include "DPoint.h"
33 #include "DWinding.h"
34
35 #include "misc.h"
36 #include "funchandlers.h"
37
38 //////////////////////////////////////////////////////////////////////
39 // Construction/Destruction
40 //////////////////////////////////////////////////////////////////////
41
42 DVisDrawer::DVisDrawer(){
43         m_list = NULL;
44
45         constructShaders();
46         GlobalShaderCache().attachRenderable( *this );
47 }
48
49 DVisDrawer::~DVisDrawer(){
50         GlobalShaderCache().detachRenderable( *this );
51         destroyShaders();
52
53         g_VisView = NULL;
54 }
55
56 //////////////////////////////////////////////////////////////////////
57 // Implementation
58 //////////////////////////////////////////////////////////////////////
59 const char* g_state_solid = "$bobtoolz/visdrawer/solid";
60 const char* g_state_wireframe = "$bobtoolz/visdrawer/wireframe";
61
62 void DVisDrawer::constructShaders(){
63         OpenGLState state;
64         GlobalOpenGLStateLibrary().getDefaultState( state );
65         state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE | RENDER_COLOURCHANGE;
66         state.m_linewidth = 1;
67
68         GlobalOpenGLStateLibrary().insert( g_state_wireframe, state );
69
70         GlobalOpenGLStateLibrary().getDefaultState( state );
71         state.m_state = RENDER_FILL | RENDER_BLEND | RENDER_COLOURWRITE | RENDER_COLOURCHANGE | RENDER_SMOOTH | RENDER_DEPTHWRITE;
72
73         GlobalOpenGLStateLibrary().insert( g_state_solid, state );
74
75         m_shader_solid = GlobalShaderCache().capture( g_state_solid );
76         m_shader_wireframe = GlobalShaderCache().capture( g_state_wireframe );
77 }
78
79 void DVisDrawer::destroyShaders(){
80         GlobalShaderCache().release( g_state_solid );
81         GlobalShaderCache().release( g_state_wireframe );
82         GlobalOpenGLStateLibrary().erase( g_state_solid );
83         GlobalOpenGLStateLibrary().erase( g_state_wireframe );
84 }
85
86 void DVisDrawer::render( RenderStateFlags state ) const {
87         //bleh
88         std::list<DWinding *>::const_iterator l = m_list->begin();
89
90         for (; l != m_list->end(); l++ )
91         {
92                 DWinding* w = *l;
93
94                 glColor4f( w->clr[0], w->clr[1], w->clr[2], 0.5f );
95
96                 glBegin( GL_POLYGON );
97                 for ( int i = 0; i < w->numpoints; i++ ) {
98                         glVertex3f( ( w->p[i] )[0], ( w->p[i] )[1], ( w->p[i] )[2] );
99                 }
100                 glEnd();
101         }
102 }
103
104 void DVisDrawer::renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
105         if ( !m_list ) {
106                 return;
107         }
108
109         renderer.SetState( m_shader_wireframe, Renderer::eWireframeOnly );
110
111         renderer.addRenderable( *this, g_matrix4_identity );
112 }
113
114 void DVisDrawer::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
115         if ( !m_list ) {
116                 return;
117         }
118
119         renderer.SetState( m_shader_solid, Renderer::eWireframeOnly );
120         renderer.SetState( m_shader_solid, Renderer::eFullMaterials );
121
122         renderer.addRenderable( *this, g_matrix4_identity );
123 }
124
125 void DVisDrawer::SetList( std::list<DWinding*> *pointList ){
126         if ( m_list ) {
127                 ClearPoints();
128         }
129
130         m_list = pointList;
131 }
132
133 void DVisDrawer::ClearPoints(){
134         std::list<DWinding *>::const_iterator deadPoint = m_list->begin();
135         for (; deadPoint != m_list->end(); deadPoint++ )
136                 delete *deadPoint;
137         m_list->clear();
138 }