]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DVisDrawer.cpp
reformat code! now the code is only ugly on the *inside*
[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 =
75             RENDER_FILL | RENDER_BLEND | RENDER_COLOURWRITE | RENDER_COLOURCHANGE | RENDER_SMOOTH | RENDER_DEPTHWRITE;
76
77     GlobalOpenGLStateLibrary().insert(g_state_solid, state);
78
79     m_shader_solid = GlobalShaderCache().capture(g_state_solid);
80     m_shader_wireframe = GlobalShaderCache().capture(g_state_wireframe);
81 }
82
83 void DVisDrawer::destroyShaders()
84 {
85     GlobalShaderCache().release(g_state_solid);
86     GlobalShaderCache().release(g_state_wireframe);
87     GlobalOpenGLStateLibrary().erase(g_state_solid);
88     GlobalOpenGLStateLibrary().erase(g_state_wireframe);
89 }
90
91 void DVisDrawer::render(RenderStateFlags state) const
92 {
93     //bleh
94     std::list<DWinding *>::const_iterator l = m_list->begin();
95
96     for (; l != m_list->end(); l++) {
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
115     renderer.SetState(m_shader_wireframe, Renderer::eWireframeOnly);
116
117     renderer.addRenderable(*this, g_matrix4_identity);
118 }
119
120 void DVisDrawer::renderSolid(Renderer &renderer, const VolumeTest &volume) const
121 {
122     if (!m_list) {
123         return;
124     }
125
126     renderer.SetState(m_shader_solid, Renderer::eWireframeOnly);
127     renderer.SetState(m_shader_solid, Renderer::eFullMaterials);
128
129     renderer.addRenderable(*this, g_matrix4_identity);
130 }
131
132 void DVisDrawer::SetList(std::list<DWinding *> *pointList)
133 {
134     if (m_list) {
135         ClearPoints();
136     }
137
138     m_list = pointList;
139 }
140
141 void DVisDrawer::ClearPoints()
142 {
143     std::list<DWinding *>::const_iterator deadPoint = m_list->begin();
144     for (; deadPoint != m_list->end(); deadPoint++) {
145         delete *deadPoint;
146     }
147     m_list->clear();
148 }