Fix for a static variable in BlenderStrokeRenderer::RenderStrokeRep() left after
[blender.git] / source / blender / freestyle / intern / view_map / ViewMapTesselator.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2010 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #ifndef __FREESTYLE_VIEW_MAP_TESSELATOR_H__
29 #define __FREESTYLE_VIEW_MAP_TESSELATOR_H__
30
31 /** \file blender/freestyle/intern/view_map/ViewMapTesselator.h
32  *  \ingroup freestyle
33  *  \brief Class to build a Node Tree designed to be displayed from a Silhouette View Map structure.
34  *  \author Stephane Grabli
35  *  \date 26/03/2002
36  */
37
38 #include "Silhouette.h"
39 #include "ViewMap.h"
40
41 #include "../scene_graph/LineRep.h"
42 #include "../scene_graph/NodeShape.h"
43 #include "../scene_graph/NodeGroup.h"
44 #include "../scene_graph/OrientedLineRep.h"
45 #include "../scene_graph/VertexRep.h"
46
47 #include "../winged_edge/WEdge.h"
48
49 class NodeShape;
50 class NodeGroup;
51 class SShape;
52 class WShape;
53
54 class LIB_VIEW_MAP_EXPORT ViewMapTesselator
55 {
56 public:
57         inline ViewMapTesselator()
58         {
59                 _nature = Nature::SILHOUETTE | Nature::BORDER | Nature::CREASE;
60                 _FrsMaterial.setDiffuse(0, 0, 0, 1);
61                 _overloadFrsMaterial = false;
62         }
63
64         virtual ~ViewMapTesselator() {}
65
66         /*! Builds a set of lines rep contained under a a NodeShape, itself contained under a NodeGroup from a ViewMap */
67         NodeGroup *Tesselate(ViewMap *iViewMap);
68
69         /*! Builds a set of lines rep contained under a a NodeShape, itself contained under a NodeGroup from a set of
70          *  view edges
71          */
72         template<class ViewEdgesIterator>
73         NodeGroup *Tesselate(ViewEdgesIterator begin, ViewEdgesIterator end);
74
75         /*! Builds a set of lines rep contained among a NodeShape, from a WShape */
76         NodeGroup *Tesselate(WShape *iWShape);
77
78         inline void setNature(Nature::EdgeNature iNature)
79         {
80                 _nature = iNature;
81         }
82
83         inline void setFrsMaterial(const FrsMaterial& iMaterial)
84         {
85                 _FrsMaterial = iMaterial;
86                 _overloadFrsMaterial = true;
87         }
88
89         inline Nature::EdgeNature nature()
90         {
91                 return _nature;
92         }
93
94         inline const FrsMaterial& frs_material() const
95         {
96                 return _FrsMaterial;
97         }
98
99 protected:
100         virtual void AddVertexToLine(LineRep *iLine, SVertex *v) = 0;
101
102 private:
103         Nature::EdgeNature _nature;
104         FrsMaterial _FrsMaterial;
105         bool _overloadFrsMaterial;
106 };
107
108 /*! Class to tesselate the 2D projected silhouette */
109 class ViewMapTesselator2D : public ViewMapTesselator
110 {
111 public:
112         inline ViewMapTesselator2D() : ViewMapTesselator() {}
113         virtual ~ViewMapTesselator2D() {}
114
115 protected:
116         virtual void AddVertexToLine(LineRep *iLine, SVertex *v)
117         {
118                 iLine->AddVertex(v->point2D());
119         }
120 };
121
122 /*! Class to tesselate the 3D silhouette */
123 class ViewMapTesselator3D : public ViewMapTesselator
124 {
125 public:
126         inline ViewMapTesselator3D() : ViewMapTesselator() {}
127         virtual ~ViewMapTesselator3D() {}
128
129 protected:
130         virtual void AddVertexToLine(LineRep *iLine, SVertex *v)
131         {
132                 iLine->AddVertex(v->point3D());
133         }
134 };
135
136 //
137 // Implementation
138 //
139 ///////////////////////////////////////////////
140
141 template<class ViewEdgesIterator>
142 NodeGroup *ViewMapTesselator::Tesselate(ViewEdgesIterator begin, ViewEdgesIterator end)
143 {
144         NodeGroup *group = new NodeGroup;
145         NodeShape *tshape = new NodeShape;
146         group->AddChild(tshape);
147         //tshape->frs_material().setDiffuse(0.0f, 0.0f, 0.0f, 1.0f);
148         tshape->setFrsMaterial(_FrsMaterial);
149
150         LineRep *line;
151
152         FEdge *firstEdge;
153         FEdge *nextFEdge, *currentEdge;
154
155         int id = 0;
156         //for (vector<ViewEdge*>::const_iterator c = viewedges.begin(), cend = viewedges.end(); c != cend; c++)
157         for (ViewEdgesIterator c = begin, cend = end; c != cend; c++) {
158 #if 0
159                 if ((*c)->qi() > 0) {
160                         continue;
161                 }
162                 if (!((*c)->nature() & (_nature))) {
163                         continue;
164                 }
165 #endif
166                 firstEdge = (*c)->fedgeA();
167
168 #if 0
169                 if (firstEdge->invisibility() > 0)
170                         continue;
171 #endif
172
173                 line = new OrientedLineRep();
174                 if (_overloadFrsMaterial)
175                         line->setFrsMaterial(_FrsMaterial);
176
177                 // there might be chains containing a single element
178                 if (0 == (firstEdge)->nextEdge()) {
179                         line->setStyle(LineRep::LINES);
180                         //line->AddVertex((*c)->vertexA()->point3D());
181                         //line->AddVertex((*c)->vertexB()->point3D());
182                         AddVertexToLine(line, firstEdge->vertexA());
183                         AddVertexToLine(line, firstEdge->vertexB());
184                 }
185                 else {
186                         line->setStyle(LineRep::LINE_STRIP);
187
188                         //firstEdge = (*c);
189                         nextFEdge = firstEdge;
190                         currentEdge = firstEdge;
191                         do {
192                                 //line->AddVertex(nextFEdge->vertexA()->point3D());
193                                 AddVertexToLine(line, nextFEdge->vertexA());
194                                 currentEdge = nextFEdge;
195                                 nextFEdge = nextFEdge->nextEdge();
196                         } while ((nextFEdge != NULL) && (nextFEdge != firstEdge));
197                         // Add the last vertex
198                         //line->AddVertex(currentEdge->vertexB()->point3D());
199                         AddVertexToLine(line, currentEdge->vertexB());
200                 }
201
202                 line->setId((*c)->getId().getFirst());
203                 line->ComputeBBox();
204                 tshape->AddRep(line);
205                 id++;
206         }
207
208         return group;
209 }
210
211 #endif // __FREESTYLE_VIEW_MAP_TESSELATOR_H__