1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
43 #include "OSGSimpleGeometryExt.h"
45 #include "OSGSimpleMaterial.h"
46 #include "OSGLineChunk.h"
48 #include "OSGSimpleGeometry.h"
49 #include "OSGFaceIterator.h"
50 #include "OSGComponentTransform.h"
51 #include "OSGTypedGeoVectorProperty.h"
52 #include "OSGTypedGeoIntegralProperty.h"
58 GeometryTransitPtr
makeGridGeo(const Real32 width
,
60 const Real32 hCellWidth
,
61 const Color3f
&color
)
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();
78 Real32
widthHalf(width
/ 2.0);
79 Real32
depthHalf(depth
/ 2.0);
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;
96 for( Real32 d
= -nRows
/ 2; d
<= nRows
/ 2; ++d
)
99 pnts
->addValue(Pnt3f(-widthHalf
, height
, z
));
100 pnts
->addValue(Pnt3f( widthHalf
, height
, z
));
103 colors
->addValue(color
);
104 colors
->addValue(color
);
108 for( Real32 w
= -nCols
/2; w
<= nCols
/2; ++w
)
111 pnts
->addValue(Pnt3f(x
, height
, -depthHalf
));
112 pnts
->addValue(Pnt3f(x
, height
, depthHalf
));
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));
125 colors->addValue(darkerColor);
126 colors->addValue(darkerColor);
128 pnts->addValue(Pnt3f(0, height, -depthHalf));
129 pnts->addValue(Pnt3f(0, height, depthHalf));
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
);
146 geo
->setPositions(pnts
);
147 // geo->setIndices (indices);
148 geo
->setColors (colors
);
149 geo
->setTypes (types
);
150 geo
->setLengths (lens
);
151 geo
->setMaterial (mat
);
158 GeometryTransitPtr
makeGridBorderGeo(const Real32 width
,
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);
180 // horizontal & vertical mid lines in different color
181 pnts
->addValue(Pnt3f(-widthHalf
, height
, 0));
182 pnts
->addValue(Pnt3f( widthHalf
, height
, 0));
184 colors
->addValue(darkerColor
);
185 colors
->addValue(darkerColor
);
187 pnts
->addValue(Pnt3f(0, height
, -depthHalf
));
188 pnts
->addValue(Pnt3f(0, height
, depthHalf
));
191 colors
->addValue(darkerColor
);
192 colors
->addValue(darkerColor
);
195 pnts
->addValue(Pnt3f(-widthHalf
, 0, depthHalf
));
196 pnts
->addValue(Pnt3f( widthHalf
, 0, depthHalf
));
199 colors
->addValue(darkerColor
);
200 colors
->addValue(darkerColor
);
202 pnts
->addValue(Pnt3f(widthHalf
, 0, depthHalf
));
203 pnts
->addValue(Pnt3f(widthHalf
, 0, -depthHalf
));
206 colors
->addValue(darkerColor
);
207 colors
->addValue(darkerColor
);
209 pnts
->addValue(Pnt3f( widthHalf
, 0, -depthHalf
));
210 pnts
->addValue(Pnt3f(-widthHalf
, 0, -depthHalf
));
213 colors
->addValue(darkerColor
);
214 colors
->addValue(darkerColor
);
216 pnts
->addValue(Pnt3f(-widthHalf
, 0, -depthHalf
));
217 pnts
->addValue(Pnt3f(-widthHalf
, 0, depthHalf
));
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
);
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
,
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
);
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
);
290 GeometryTransitPtr
makeSelectionGeo(Node
*geoN
)
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
)
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
);
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
);
346 PolygonChunkUnrecPtr _gayaPolygonChunk
;
348 PolygonChunk
*getPolygonChunk(const UInt16 faces
,
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);
365 return _gayaPolygonChunk
;
368 MaterialChunkRecPtr _gayaMaterialChunk
;
370 MaterialChunk
*getMaterialChunk(const Color4f
&ambientColor
,
371 const Color4f
&diffuseColor
,
372 const Color4f
&specularColor
,
375 if( _gayaMaterialChunk
== NULL
)
377 _gayaMaterialChunk
= MaterialChunk::create();
379 _gayaMaterialChunk
->setDiffuse(diffuseColor
);
380 _gayaMaterialChunk
->setAmbient(ambientColor
);
381 _gayaMaterialChunk
->setSpecular(specularColor
);
382 _gayaMaterialChunk
->setLit(lit
);
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),
403 return _gayaSelectionMaterial
;