changed: gcc8 base update
[opensg.git] / Source / Contrib / Manipulators / OSGSimpleGeometryExt.cpp
blob7011d82a4032f9ce0c8e597257bbea9f2ffdb0ec
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include "OSGSimpleGeometryExt.h"
45 #include "OSGSimpleMaterial.h"
46 #include "OSGLineChunk.h"
47 #include "OSGGroup.h"
48 #include "OSGSimpleGeometry.h"
49 #include "OSGFaceIterator.h"
50 #include "OSGComponentTransform.h"
51 #include "OSGTypedGeoVectorProperty.h"
52 #include "OSGTypedGeoIntegralProperty.h"
54 using namespace std;
56 OSG_BEGIN_NAMESPACE
58 GeometryTransitPtr makeGridGeo(const Real32 width,
59 const Real32 depth,
60 const Real32 hCellWidth,
61 const Color3f &color )
63 Int32 nCols, nRows;
64 Real32 x, z;
66 const Color3f darkerColor =
67 Color3f(color[0] - 0.4, color[1] - 0.4, color[2] - 0.4);
69 GeometryTransitPtr geo = Geometry::create();
70 GeoPnt3fPropertyUnrecPtr pnts = GeoPnt3fProperty ::create();
71 GeoUInt32PropertyUnrecPtr lens = GeoUInt32Property ::create();
72 GeoUInt8PropertyUnrecPtr types = GeoUInt8Property ::create();
73 GeoColor3fPropertyUnrecPtr colors = GeoColor3fProperty::create();
74 // GeoIndicesUI32Ptr indices = GeoIndicesUI32::create();
75 SimpleMaterialUnrecPtr mat = SimpleMaterial::create();
77 Real32 height (0.0 );
78 Real32 widthHalf(width / 2.0);
79 Real32 depthHalf(depth / 2.0);
80 // hCellWidth = 1;
81 Real32 vCellWidth(hCellWidth);
82 nCols = Int32(width / hCellWidth);
83 nRows = Int32(depth / vCellWidth);
86 SLOG << "creating Grid: width=" << width
87 << ", depth=" << depth
88 << ", hCellWidth=" << hCellWidth
89 << ", vCellWidth=" << vCellWidth
90 << ", nCols=" << nCols
91 << ", nRows=" << nRows << endl;
93 Int32 pntCnt(0);
95 // horizontal lines
96 for( Real32 d = -nRows / 2; d <= nRows / 2; ++d )
98 z = d * vCellWidth;
99 pnts->addValue(Pnt3f(-widthHalf, height, z));
100 pnts->addValue(Pnt3f( widthHalf, height, z));
101 ++pntCnt;
102 ++pntCnt;
103 colors->addValue(color);
104 colors->addValue(color);
107 // vertical lines
108 for( Real32 w = -nCols/2; w <= nCols/2; ++w )
110 x = w * hCellWidth;
111 pnts->addValue(Pnt3f(x, height, -depthHalf));
112 pnts->addValue(Pnt3f(x, height, depthHalf));
113 ++pntCnt;
114 ++pntCnt;
115 colors->addValue(color);
116 colors->addValue(color);
120 // horizontal & vertical mid lines in different color
121 pnts->addValue(Pnt3f(-widthHalf, height, 0));
122 pnts->addValue(Pnt3f( widthHalf, height, 0));
123 ++pntCnt;
124 ++pntCnt;
125 colors->addValue(darkerColor);
126 colors->addValue(darkerColor);
128 pnts->addValue(Pnt3f(0, height, -depthHalf));
129 pnts->addValue(Pnt3f(0, height, depthHalf));
130 ++pntCnt;
131 ++pntCnt;
132 colors->addValue(darkerColor);
133 colors->addValue(darkerColor);
136 //SLOG << "Grid has " << pntCnt << " points.\n" << endl;
138 types->addValue(GL_LINES);
139 lens->addValue (pntCnt );
141 // set the color for the grid
142 mat->setAmbient(color);
143 mat->setDiffuse(color);
144 mat->setLit (false);
146 geo->setPositions(pnts );
147 // geo->setIndices (indices);
148 geo->setColors (colors );
149 geo->setTypes (types );
150 geo->setLengths (lens );
151 geo->setMaterial (mat );
153 commitChanges();
155 return geo;
158 GeometryTransitPtr makeGridBorderGeo(const Real32 width,
159 const Real32 depth,
160 const Color3f &color)
162 const Color3f darkerColor =
163 Color3f( color[0]-0.4, color[1]-0.4, color[2]-0.4 );
165 GeometryTransitPtr geo = Geometry::create();
166 GeoPnt3fPropertyUnrecPtr pnts = GeoPnt3fProperty::create();
167 GeoUInt32PropertyUnrecPtr lens = GeoUInt32Property::create();
168 GeoUInt8PropertyUnrecPtr types = GeoUInt8Property::create();
169 GeoColor3fPropertyUnrecPtr colors = GeoColor3fProperty::create();
170 // GeoIndicesUI32Ptr indices = GeoIndicesUI32::create();
171 SimpleMaterialUnrecPtr mat = SimpleMaterial::create();
172 LineChunkUnrecPtr lineChunk = LineChunk::create();
174 Real32 height (0.0 );
175 Real32 widthHalf(width / 2.0);
176 Real32 depthHalf(depth / 2.0);
178 Int32 pntCnt(0);
180 // horizontal & vertical mid lines in different color
181 pnts->addValue(Pnt3f(-widthHalf, height, 0));
182 pnts->addValue(Pnt3f( widthHalf, height, 0));
183 pntCnt++; pntCnt++;
184 colors->addValue(darkerColor);
185 colors->addValue(darkerColor);
187 pnts->addValue(Pnt3f(0, height, -depthHalf));
188 pnts->addValue(Pnt3f(0, height, depthHalf));
189 ++pntCnt;
190 ++pntCnt;
191 colors->addValue(darkerColor);
192 colors->addValue(darkerColor);
194 // the border
195 pnts->addValue(Pnt3f(-widthHalf, 0, depthHalf));
196 pnts->addValue(Pnt3f( widthHalf, 0, depthHalf));
197 ++pntCnt;
198 ++pntCnt;
199 colors->addValue(darkerColor);
200 colors->addValue(darkerColor);
202 pnts->addValue(Pnt3f(widthHalf, 0, depthHalf));
203 pnts->addValue(Pnt3f(widthHalf, 0, -depthHalf));
204 ++pntCnt;
205 ++pntCnt;
206 colors->addValue(darkerColor);
207 colors->addValue(darkerColor);
209 pnts->addValue(Pnt3f( widthHalf, 0, -depthHalf));
210 pnts->addValue(Pnt3f(-widthHalf, 0, -depthHalf));
211 ++pntCnt;
212 ++pntCnt;
213 colors->addValue(darkerColor);
214 colors->addValue(darkerColor);
216 pnts->addValue(Pnt3f(-widthHalf, 0, -depthHalf));
217 pnts->addValue(Pnt3f(-widthHalf, 0, depthHalf));
218 ++pntCnt;
219 ++pntCnt;
220 colors->addValue(darkerColor);
221 colors->addValue(darkerColor);
223 lineChunk->setWidth (1 );
224 lineChunk->setSmooth(true);
226 types->addValue(GL_LINES);
227 lens->addValue (pntCnt );
229 // set the color for the grid
230 mat->setLit (false );
231 mat->addChunk(lineChunk);
233 geo->setPositions(pnts );
234 // geo->setIndices (indices);
235 geo->setColors (colors );
236 geo->setTypes (types );
237 geo->setLengths (lens );
238 geo->setMaterial (mat );
240 commitChanges();
242 return geo;
245 /*! \brief create a grid
246 * \ingroup SimpleGeometryExt
248 * Creates a grid with the specified <i>width</i> and <i>height</i>.
249 * The grid has the base color <i>color</i>.
251 NodeTransitPtr makeGrid(const Real32 width,
252 const Real32 depth,
253 const Real32 hCellWidth,
254 const Color3f &color )
256 NodeTransitPtr grpNode = Node::create();
257 GroupUnrecPtr grpCore = Group::create();
258 NodeUnrecPtr gridNode = Node::create();
259 NodeUnrecPtr borderNode = Node::create();
261 gridNode->setCore (makeGridGeo(width, depth, hCellWidth, color));
262 borderNode->setCore(makeGridBorderGeo(width, depth, color) );
264 grpNode->setCore (grpCore );
265 grpNode->addChild(gridNode );
266 grpNode->addChild(borderNode);
268 commitChanges();
270 return grpNode;
274 NodeTransitPtr makeSelection(Node *geoN)
276 NodeTransitPtr transN = Node::create();
277 NodeUnrecPtr node = Node::create();
278 ComponentTransformUnrecPtr transC = ComponentTransform::create();
280 node->setCore(makeSelectionGeo(geoN));
282 transN->setCore (transC);
283 transN->addChild(node );
285 commitChanges();
287 return transN;
290 GeometryTransitPtr makeSelectionGeo(Node *geoN)
292 Pnt3f min, max;
293 geoN->getVolume().getBounds(min, max);
295 GeometryTransitPtr geo = Geometry::create();
296 GeoPnt3fPropertyUnrecPtr newPnts = GeoPnt3fProperty::create();
297 GeoUInt32PropertyUnrecPtr lens = GeoUInt32Property::create();
298 GeoUInt8PropertyUnrecPtr types = GeoUInt8Property::create();
299 GeoColor3fPropertyUnrecPtr colors = GeoColor3fProperty::create();
301 // GeoIndicesUI32Ptr indices = GeoIndicesUI32::create();
302 SimpleMaterialUnrecPtr mat = SimpleMaterial::create();
304 LineChunkUnrecPtr lineChunk = LineChunk::create();
306 Geometry *selGeoC = dynamic_cast<Geometry *>(geoN->getCore());
307 if( selGeoC != NULL )
309 FaceIterator fi;
311 for(fi=selGeoC->beginFaces(); fi!=selGeoC->endFaces(); ++fi)
313 newPnts->addValue(fi.getPosition(0));
314 newPnts->addValue(fi.getPosition(1));
315 newPnts->addValue(fi.getPosition(2));
317 colors->addValue(Color3f(1, 1, 1));
318 colors->addValue(Color3f(1, 1, 1));
319 colors->addValue(Color3f(1, 1, 1));
321 types->addValue(GL_LINE_STRIP);
322 lens->addValue (3 );
326 lineChunk->setWidth (1 );
327 lineChunk->setSmooth(false);
329 // set the color for the grid
330 mat->setLit (false );
331 mat->addChunk(lineChunk);
333 geo->setPositions(newPnts);
334 // geo->setIndices (indices);
335 // geo->setColors (colors );
336 geo->setTypes (types );
337 geo->setLengths (lens );
338 geo->setMaterial (mat );
340 commitChanges();
342 return geo;
346 PolygonChunkUnrecPtr _gayaPolygonChunk;
348 PolygonChunk *getPolygonChunk(const UInt16 faces,
349 const UInt16 mode )
351 if( _gayaPolygonChunk == NULL )
353 _gayaPolygonChunk = PolygonChunk::create();
355 _gayaPolygonChunk->setCullFace(faces);
356 _gayaPolygonChunk->setFrontMode(mode);
357 _gayaPolygonChunk->setSmooth(true);
358 _gayaPolygonChunk->setOffsetLine(true);
359 _gayaPolygonChunk->setOffsetFill(false);
360 _gayaPolygonChunk->setOffsetFactor(-1);
361 // _gayaPolygonChunk->setOffsetBias(1);
363 commitChanges();
365 return _gayaPolygonChunk;
368 MaterialChunkRecPtr _gayaMaterialChunk;
370 MaterialChunk *getMaterialChunk(const Color4f &ambientColor,
371 const Color4f &diffuseColor,
372 const Color4f &specularColor,
373 const bool lit )
375 if( _gayaMaterialChunk == NULL )
377 _gayaMaterialChunk = MaterialChunk::create();
379 _gayaMaterialChunk->setDiffuse(diffuseColor);
380 _gayaMaterialChunk->setAmbient(ambientColor);
381 _gayaMaterialChunk->setSpecular(specularColor);
382 _gayaMaterialChunk->setLit(lit);
384 commitChanges();
386 return _gayaMaterialChunk;
389 ChunkMaterialRecPtr _gayaSelectionMaterial;
391 ChunkMaterial *getSelectionMaterial()
393 if( _gayaSelectionMaterial == NULL )
395 _gayaSelectionMaterial = ChunkMaterial::create();
397 _gayaSelectionMaterial->addChunk(getPolygonChunk());
398 _gayaSelectionMaterial->addChunk(getMaterialChunk(Color4f(1,1,1,0),
399 Color4f(1,1,1,0)));
401 commitChanges();
403 return _gayaSelectionMaterial;
407 OSG_END_NAMESPACE