fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Drawables / Geometry / Base / OSGGeoBuilder.cpp
blob1082a52b4256168201ba21a09a66e9f0233645d1
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 * *
16 * *
17 * This library is free software; you can redistribute it and/or modify it *
18 * under the terms of the GNU Library General Public License as published *
19 * by the Free Software Foundation, version 2. *
20 * *
21 * This library is distributed in the hope that it will be useful, but *
22 * WITHOUT ANY WARRANTY; without even the implied warranty of *
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
24 * Library General Public License for more details. *
25 * *
26 * You should have received a copy of the GNU Library General Public *
27 * License along with this library; if not, write to the Free Software *
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
29 * *
30 * *
31 \*---------------------------------------------------------------------------*/
32 /*---------------------------------------------------------------------------*\
33 * Changes *
34 * *
35 * *
36 * *
37 * *
38 * *
39 * *
40 \*---------------------------------------------------------------------------*/
42 //---------------------------------------------------------------------------
43 // Includes
44 //---------------------------------------------------------------------------
46 #include <cstdlib>
47 #include <cstdio>
49 #include "OSGConfig.h"
51 #include "OSGPrimeMaterial.h"
52 #include "OSGGeoBuilder.h"
54 OSG_USING_NAMESPACE
57 /***************************************************************************\
58 * Description *
59 \***************************************************************************/
61 // Default Typenames for the given attribute
62 const char* GeoBuilder::_defaultPropTypes[Geometry::MaxAttribs] =
64 "GeoPnt3fProperty", // Positions = 0
65 "GeoVec3fProperty", // 1 unused
66 "GeoVec3fProperty", // NormalsIndex = 2
67 "GeoColor4fProperty",// ColorsIndex = 3
68 "GeoColor4fProperty",// SecondaryColorsIndex = 4
69 "GeoVec3fProperty", // 5 unused
70 "GeoVec3fProperty", // 6 unused
71 "GeoVec3fProperty", // 7 unused
72 "GeoVec2fProperty", // TexCoordsIndex = 8
73 "GeoVec2fProperty", // TexCoords1Index = 9
74 "GeoVec2fProperty", // TexCoords2Index = 10
75 "GeoVec2fProperty", // TexCoords3Index = 11
76 "GeoVec2fProperty", // TexCoords4Index = 12
77 "GeoVec2fProperty", // TexCoords5Index = 13
78 "GeoVec2fProperty", // TexCoords6Index = 14
79 "GeoVec2fProperty", // TexCoords7Index = 15
82 GeoBuilder::GeoBuilder(void) :
83 _geo ( ),
84 _actLen (0 ),
85 _actType(GL_NONE)
87 reset();
90 GeoBuilder::~GeoBuilder()
94 void GeoBuilder::reset(void)
96 _geo = NULL;
98 _geo = Geometry::create();
100 PrimeMaterial *mat = getDefaultMaterial();
102 _geo->setMaterial(mat);
104 _actLen = 0;
105 _actType = -1;
108 // Property Helper
109 GeoVectorProperty *GeoBuilder::getProperty(UInt32 propIndex)
111 GeoVectorPropertyUnrecPtr att;
113 if(propIndex >= _geo->getMFProperties()->size() ||
114 _geo->getProperty(propIndex) == NULL)
116 att = dynamic_pointer_cast<GeoVectorProperty>(
117 FieldContainerFactory::the()->createContainer(
118 _defaultPropTypes[propIndex]));
120 _geo->setProperty(att, propIndex);
122 else
124 att = _geo->getProperty(propIndex);
127 return att;
130 // Finish the vertex. Make sure all Properties have the same number of
131 // attributes and adjust the index, if necessary
132 UInt32 GeoBuilder::finishVertex(void)
134 UInt32 possize = _geo->getProperty(Geometry::PositionsIndex)->size32();
136 for(UInt16 i = 1; i < _geo->getMFProperties()->size(); ++i)
138 if(_geo->getProperty(i) != NULL)
140 GeoVectorProperty::MaxTypeT val;
142 _geo->getProperty(i)->getValue(val,
143 _geo->getProperty(i)->size() - 1);
145 for(UInt32 propsize = _geo->getProperty(i)->size32();
146 propsize < possize;
147 ++propsize)
149 _geo->getProperty(i)->addValue(val);
154 // Are we in a begin/end loop? Then add current vertex to index
155 if(_actType != -1)
156 index(possize - 1);
158 _actLen++;
159 return possize - 1;
163 void GeoBuilder::addType(Int32 type)
165 if(_geo->getTypes() == NULL)
167 GeoIntegralPropertyUnrecPtr t = GeoUInt8Property::create();
168 _geo->setTypes(t);
171 _geo->getTypes()->addValue(type);
174 void GeoBuilder::addLength(UInt32 length)
176 if(_geo->getLengths() == NULL)
178 GeoIntegralPropertyUnrecPtr l = GeoUInt32Property::create();
179 _geo->setLengths(l);
181 _geo->getLengths()->addValue(length);
184 void GeoBuilder::index(UInt32 newIndex)
186 if(_actType == -1)
188 FWARNING(("GeoBuilder::index: called outside begin/end block!\n"));
189 return;
192 if(_geo->getIndices() == NULL)
194 GeoIntegralPropertyUnrecPtr i = GeoUInt32Property::create();
195 _geo->setIndices(i);
198 _geo->getIndices()->push_back(newIndex);
201 // Face Creation
204 void GeoBuilder::begin(UInt32 type)
206 _actLen = 0;
207 _actType = type;
209 addType(type);
212 void GeoBuilder::end(void)
214 addLength(_actLen);
216 _actLen = 0;
217 _actType = -1;
220 void GeoBuilder::line(UInt32 start)
222 begin(GL_LINES);
224 index(start );
225 index(start + 1);
227 _actLen += 2;
229 end();
232 void GeoBuilder::line(UInt32 i1, UInt32 i2)
234 begin(GL_LINES);
236 index(i1);
237 index(i2);
239 _actLen += 2;
241 end();
244 void GeoBuilder::tri(UInt32 start)
246 begin(GL_TRIANGLES);
248 index(start );
249 index(start + 1);
250 index(start + 2);
252 _actLen += 3;
254 end();
257 void GeoBuilder::tri(UInt32 i1, UInt32 i2, UInt32 i3)
259 begin(GL_TRIANGLES);
261 index(i1);
262 index(i2);
263 index(i3);
265 _actLen += 3;
267 end();
270 void GeoBuilder::quad(UInt32 start)
272 begin(GL_QUADS);
274 index(start );
275 index(start + 1);
276 index(start + 2);
277 index(start + 3);
279 _actLen += 4;
281 end();
284 void GeoBuilder::quad(UInt32 i1, UInt32 i2, UInt32 i3, UInt32 i4)
286 begin(GL_QUADS);
288 index(i1);
289 index(i2);
290 index(i3);
291 index(i4);
293 _actLen += 4;
295 end();
299 Geometry *GeoBuilder::getGeometry(void)
301 return _geo;