refactored plugin api; refactored callback library; added signals library
[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 {
44         m_list = NULL;
45
46   constructShaders();
47   GlobalShaderCache().attachRenderable(*this);
48 }
49
50 DVisDrawer::~DVisDrawer()
51 {
52   GlobalShaderCache().detachRenderable(*this);
53   destroyShaders();
54
55         g_VisView = NULL;
56 }
57
58 //////////////////////////////////////////////////////////////////////
59 // Implementation
60 //////////////////////////////////////////////////////////////////////
61 const char* g_state_solid = "$bobtoolz/visdrawer/solid";
62 const char* g_state_wireframe = "$bobtoolz/visdrawer/wireframe";
63
64 void DVisDrawer::constructShaders()
65 {
66   OpenGLState state;
67   GlobalOpenGLStateLibrary().getDefaultState(state);
68   state.m_state = RENDER_COLOURWRITE|RENDER_DEPTHWRITE|RENDER_COLOURCHANGE;
69   state.m_linewidth = 1;
70
71   GlobalOpenGLStateLibrary().insert(g_state_wireframe, state);
72
73   GlobalOpenGLStateLibrary().getDefaultState(state);
74   state.m_state = RENDER_FILL|RENDER_BLEND|RENDER_COLOURWRITE|RENDER_COLOURCHANGE|RENDER_SMOOTH|RENDER_DEPTHWRITE;
75
76   GlobalOpenGLStateLibrary().insert(g_state_solid, state);
77
78   m_shader_solid = GlobalShaderCache().capture(g_state_solid);
79   m_shader_wireframe = GlobalShaderCache().capture(g_state_wireframe);
80 }
81
82 void DVisDrawer::destroyShaders()
83 {
84   GlobalShaderCache().release(g_state_solid);
85   GlobalShaderCache().release(g_state_wireframe);
86   GlobalOpenGLStateLibrary().erase(g_state_solid);
87   GlobalOpenGLStateLibrary().erase(g_state_wireframe);
88 }
89
90 void DVisDrawer::render(RenderStateFlags state) const
91 {
92         //bleh
93         std::list<DWinding *>::const_iterator l=m_list->begin();
94
95         for(; l != m_list->end(); l++)
96         {
97                 DWinding* w = *l;
98
99                 glColor4f(w->clr[0], w->clr[1], w->clr[2], 0.5f);
100
101                 glBegin(GL_POLYGON);
102                 for(int i = 0; i < w->numpoints; i++) {
103                         glVertex3f((w->p[i])[0], (w->p[i])[1], (w->p[i])[2]);
104                 }
105                 glEnd();
106         }
107 }
108
109 void DVisDrawer::renderWireframe(Renderer& renderer, const VolumeTest& volume) const
110 {
111         if(!m_list)
112                 return;
113
114   renderer.SetState(m_shader_wireframe, Renderer::eWireframeOnly);
115
116   renderer.addRenderable(*this, g_matrix4_identity);
117 }
118
119 void DVisDrawer::renderSolid(Renderer& renderer, const VolumeTest& volume) const
120 {
121         if(!m_list)
122                 return;
123
124   renderer.SetState(m_shader_solid, Renderer::eWireframeOnly);
125   renderer.SetState(m_shader_solid, Renderer::eFullMaterials);
126
127   renderer.addRenderable(*this, g_matrix4_identity);
128 }
129
130 void DVisDrawer::SetList(std::list<DWinding*> *pointList)
131 {
132         if(m_list)
133                 ClearPoints();
134
135         m_list = pointList;
136 }
137
138 void DVisDrawer::ClearPoints()
139 {
140   std::list<DWinding *>::const_iterator deadPoint=m_list->begin();
141         for(; deadPoint!=m_list->end(); deadPoint++)
142                 delete *deadPoint;
143         m_list->clear();
144 }