1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_EdgeRenderer
21 #define TrenchBroom_EdgeRenderer
22 
23 #include "Color.h"
24 #include "Reference.h"
25 #include "Renderer/IndexArray.h"
26 #include "Renderer/IndexArrayMap.h"
27 #include "Renderer/IndexRangeMap.h"
28 #include "Renderer/Renderable.h"
29 #include "Renderer/VertexArray.h"
30 
31 #include <vector>
32 
33 namespace TrenchBroom {
34     namespace Renderer {
35         class RenderBatch;
36         class RenderContext;
37         class Vbo;
38 
39         class EdgeRenderer {
40         public:
41             struct Params {
42                 float width;
43                 float offset;
44                 bool onTop;
45                 bool useColor;
46                 Color color;
47                 Params(float i_width, float i_offset, bool i_onTop);
48                 Params(float i_width, float i_offset, bool i_onTop, const Color& i_color);
49                 Params(float i_width, float i_offset, bool i_onTop, bool i_useColor, const Color& i_color);
50             };
51 
52             class RenderBase {
53             private:
54                 const Params m_params;
55             public:
56                 RenderBase(const Params& params);
57                 virtual ~RenderBase();
58             protected:
59                 void renderEdges(RenderContext& renderContext);
60             private:
61                 virtual void doRenderVertices(RenderContext& renderContext) = 0;
62             };
63         public:
64             virtual ~EdgeRenderer();
65 
66             void render(RenderBatch& renderBatch, float width = 1.0f, float offset = 0.0f);
67             void render(RenderBatch& renderBatch, const Color& color, float width = 1.0f, float offset = 0.0f);
68             void render(RenderBatch& renderBatch, bool useColor, const Color& color, float width = 1.0f, float offset = 0.0f);
69             void renderOnTop(RenderBatch& renderBatch, float width = 1.0f, float offset = 0.2f);
70             void renderOnTop(RenderBatch& renderBatch, const Color& color, float width = 1.0f, float offset = 0.2f);
71             void renderOnTop(RenderBatch& renderBatch, bool useColor, const Color& color, float width = 1.0f, float offset = 0.2f);
72             void render(RenderBatch& renderBatch, bool useColor, const Color& color, bool onTop, float width, float offset);
73         private:
74             virtual void doRender(RenderBatch& renderBatch, const Params& params) = 0;
75         };
76 
77         class DirectEdgeRenderer : public EdgeRenderer {
78         private:
79             class Render : public RenderBase, public DirectRenderable {
80             private:
81                 VertexArray m_vertexArray;
82                 IndexRangeMap m_indexRanges;
83             public:
84                 Render(const Params& params, VertexArray& vertexArray, IndexRangeMap& indexRanges);
85             private:
86                 void doPrepareVertices(Vbo& vertexVbo);
87                 void doRender(RenderContext& renderContext);
88                 void doRenderVertices(RenderContext& renderContext);
89             };
90         private:
91             VertexArray m_vertexArray;
92             IndexRangeMap m_indexRanges;
93         public:
94             DirectEdgeRenderer();
95             DirectEdgeRenderer(const VertexArray& vertexArray, const IndexRangeMap& indexRanges);
96             DirectEdgeRenderer(const VertexArray& vertexArray, PrimType primType);
97 
98             DirectEdgeRenderer(const DirectEdgeRenderer& other);
99             DirectEdgeRenderer& operator=(DirectEdgeRenderer other);
100 
101             friend void swap(DirectEdgeRenderer& left, DirectEdgeRenderer& right);
102         private:
103             void doRender(RenderBatch& renderBatch, const EdgeRenderer::Params& params);
104         };
105 
106         class IndexedEdgeRenderer : public EdgeRenderer {
107         private:
108             class Render : public RenderBase, public IndexedRenderable {
109             private:
110                 VertexArray m_vertexArray;
111                 IndexArray m_indexArray;
112                 IndexArrayMap m_indexRanges;
113             public:
114                 Render(const Params& params, VertexArray& vertexArray, IndexArray& indexArray, IndexArrayMap& indexRanges);
115             private:
116                 void doPrepareVertices(Vbo& vertexVbo);
117                 void doPrepareIndices(Vbo& indexVbo);
118                 void doRender(RenderContext& renderContext);
119                 void doRenderVertices(RenderContext& renderContext);
120             };
121         private:
122             VertexArray m_vertexArray;
123             IndexArray m_indexArray;
124             IndexArrayMap m_indexRanges;
125         public:
126             IndexedEdgeRenderer();
127             IndexedEdgeRenderer(const VertexArray& vertexArray, const IndexArray& indexArray, const IndexArrayMap& indexRanges);
128 
129             IndexedEdgeRenderer(const IndexedEdgeRenderer& other);
130             IndexedEdgeRenderer& operator=(IndexedEdgeRenderer other);
131 
132             friend void swap(IndexedEdgeRenderer& left, IndexedEdgeRenderer& right);
133         private:
134             void doRender(RenderBatch& renderBatch, const EdgeRenderer::Params& params);
135         };
136     }
137 }
138 
139 #endif /* defined(TrenchBroom_EdgeRenderer) */
140