1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 /** \file
20  * \ingroup freestyle
21  */
22 
23 #include <float.h>
24 #include <string.h>
25 
26 #include "../geometry/BBox.h"
27 #include "../geometry/Geom.h"
28 #include "../geometry/GeomCleaner.h"
29 #include "../geometry/GeomUtils.h"
30 #include "../scene_graph/IndexedFaceSet.h"
31 #include "../scene_graph/NodeGroup.h"
32 #include "../scene_graph/NodeShape.h"
33 #include "../scene_graph/NodeTransform.h"
34 #include "../system/FreestyleConfig.h"
35 #include "../system/RenderMonitor.h"
36 
37 #include "MEM_guardedalloc.h"
38 
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 
46 #include "render_types.h"
47 
48 #include "BKE_customdata.h"
49 #include "BKE_lib_id.h"
50 #include "BKE_material.h"
51 #include "BKE_mesh.h"
52 #include "BKE_scene.h"
53 
54 #include "BLI_iterator.h"
55 #include "BLI_listbase.h"
56 #include "BLI_math.h"
57 
58 #include "DEG_depsgraph_query.h"
59 
60 #ifdef WITH_CXX_GUARDEDALLOC
61 #  include "MEM_guardedalloc.h"
62 #endif
63 
64 namespace Freestyle {
65 
66 class NodeGroup;
67 
68 struct LoaderState {
69   float *pv;
70   float *pn;
71   IndexedFaceSet::FaceEdgeMark *pm;
72   unsigned *pvi;
73   unsigned *pni;
74   unsigned *pmi;
75   unsigned currentIndex;
76   unsigned currentMIndex;
77   float minBBox[3];
78   float maxBBox[3];
79 };
80 
81 class BlenderFileLoader {
82  public:
83   /*! Builds a MaxFileLoader */
84   BlenderFileLoader(Render *re, ViewLayer *view_layer, Depsgraph *depsgraph);
85   virtual ~BlenderFileLoader();
86 
87   /*! Loads the 3D scene and returns a pointer to the scene root node */
88   NodeGroup *Load();
89 
90   /*! Gets the number of read faces */
numFacesRead()91   inline unsigned int numFacesRead()
92   {
93     return _numFacesRead;
94   }
95 
96 #if 0
97   /*! Gets the smallest edge size read */
98   inline real minEdgeSize()
99   {
100     return _minEdgeSize;
101   }
102 #endif
103 
104   /*! Modifiers */
setRenderMonitor(RenderMonitor * iRenderMonitor)105   inline void setRenderMonitor(RenderMonitor *iRenderMonitor)
106   {
107     _pRenderMonitor = iRenderMonitor;
108   }
109 
110  protected:
111   void insertShapeNode(Object *ob, Mesh *mesh, int id);
112   int testDegenerateTriangle(float v1[3], float v2[3], float v3[3]);
113   int countClippedFaces(float v1[3], float v2[3], float v3[3], int clip[3]);
114   void clipLine(float v1[3], float v2[3], float c[3], float z);
115   void clipTriangle(int numTris,
116                     float triCoords[][3],
117                     float v1[3],
118                     float v2[3],
119                     float v3[3],
120                     float triNormals[][3],
121                     float n1[3],
122                     float n2[3],
123                     float n3[3],
124                     bool edgeMarks[5],
125                     bool em1,
126                     bool em2,
127                     bool em3,
128                     const int clip[3]);
129   void addTriangle(struct LoaderState *ls,
130                    float v1[3],
131                    float v2[3],
132                    float v3[3],
133                    float n1[3],
134                    float n2[3],
135                    float n3[3],
136                    bool fm,
137                    bool em1,
138                    bool em2,
139                    bool em3);
140 
141  protected:
142   struct detri_t {
143     unsigned viA, viB, viP;  // 0 <= viA, viB, viP < viSize
144     Vec3r v;
145     unsigned n;
146   };
147   Render *_re;
148   Depsgraph *_depsgraph;
149   NodeGroup *_Scene;
150   unsigned _numFacesRead;
151 #if 0
152   real _minEdgeSize;
153 #endif
154   bool _smooth; /* if true, face smoothness is taken into account */
155   float _viewplane_left;
156   float _viewplane_right;
157   float _viewplane_bottom;
158   float _viewplane_top;
159   float _z_near, _z_far;
160   float _z_offset;
161 
162   RenderMonitor *_pRenderMonitor;
163 
164 #ifdef WITH_CXX_GUARDEDALLOC
165   MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:BlenderFileLoader")
166 #endif
167 };
168 
169 } /* namespace Freestyle */
170