14 #include <osg/PolygonMode>
15 #include <osg/ShapeDrawable>
16 #include <osg/Material>
17 #include <osg/Geometry>
20 #include <osg/Texture2D>
21 #include <osgDB/ReadFile>
24 osg::ref_ptr< osg::Group >
32 const std::string &_name,
33 osg::ref_ptr< osg::Group > _group)
35 osg::ref_ptr< osg::Vec3Array > points =
new osg::Vec3Array;
36 osg::ref_ptr< osg::Geometry > geometry =
new osg::Geometry;
37 osg::ref_ptr< osg::Material > pMaterial =
new osg::Material;
38 osg::ref_ptr< osg::Geode > geode =
new osg::Geode;
40 points->push_back(osg::Vec3(_x, _y, _z));
41 points->push_back(osg::Vec3(_x2, _y2, _z2));
43 geometry->setVertexArray(points.get());
44 geometry->addPrimitiveSet(
new osg::DrawArrays(GL_LINES, 0, 2));
46 geode->addDrawable(geometry);
49 geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
50 geode->getOrCreateStateSet()->setAttribute(pMaterial,
51 osg::StateAttribute::OVERRIDE);
53 geode->setName(_name);
55 _group->addChild(geode);
60 osg::ref_ptr< osg::Group >
69 const std::string &_name,
70 osg::ref_ptr< osg::Group > _group)
72 osg::ref_ptr< osg::Cylinder > cylinder;
73 osg::ref_ptr< osg::ShapeDrawable > cylinderDrawable;
74 osg::ref_ptr< osg::Material > pMaterial =
new osg::Material;
75 osg::ref_ptr< osg::Geode > geode =
new osg::Geode;
77 double height = osg::Vec3(_x - _x2, _y - _y2, _z - _z2).length();
78 osg::Vec3 center = osg::Vec3((_x + _x2) / 2, (_y + _y2) / 2, (_z + _z2) / 2);
79 osg::Vec3 dir = osg::Vec3(
83 osg::Vec3 p = osg::Vec3(_x - _x2, _y - _y2, _z - _z2);
84 osg::Vec3 t = dir ^ p;
85 double angle =
acos((dir * p) / p.length());
87 cylinder =
new osg::Cylinder(center, _r, height);
88 cylinder->setRotation(osg::Quat(angle, osg::Vec3(t.x(), t.y(), t.z())));
90 cylinderDrawable =
new osg::ShapeDrawable(cylinder);
91 geode->addDrawable(cylinderDrawable);
94 geode->getOrCreateStateSet()->setAttribute(pMaterial,
95 osg::StateAttribute::OVERRIDE);
97 geode->setName(_name);
99 _group->addChild(geode);
104 osg::ref_ptr< osg::Group >
110 const std::string &_name,
111 osg::ref_ptr< osg::Group > _group)
113 osg::ref_ptr< osg::Box > box;
114 osg::ref_ptr< osg::ShapeDrawable > boxDrawable;
115 osg::ref_ptr< osg::Material > pMaterial =
new osg::Material;
116 osg::ref_ptr< osg::Geode > geode =
new osg::Geode;
118 box =
new osg::Box(osg::Vec3(_x, _y, _z), _r);
120 boxDrawable =
new osg::ShapeDrawable(box);
121 geode->addDrawable(boxDrawable);
124 geode->getOrCreateStateSet()->setAttribute(pMaterial,
125 osg::StateAttribute::OVERRIDE);
127 geode->setName(_name);
129 _group->addChild(geode);
134 osg::ref_ptr< osg::Group >
140 const std::string &_name,
141 osg::ref_ptr< osg::Group > _group)
143 osg::ref_ptr< osg::Sphere > sphere;
144 osg::ref_ptr< osg::ShapeDrawable > sphereDrawable;
145 osg::ref_ptr< osg::Material > pMaterial =
new osg::Material;
146 osg::ref_ptr< osg::Geode > geode =
new osg::Geode;
148 osg::Vec3 center = osg::Vec3(_x, _y, _z);
149 sphere =
new osg::Sphere(center, _r);
151 sphereDrawable =
new osg::ShapeDrawable(sphere);
152 geode->addDrawable(sphereDrawable);
155 geode->getOrCreateStateSet()->setAttribute(pMaterial,
156 osg::StateAttribute::OVERRIDE);
158 geode->setName(_name);
160 _group->addChild(geode);
165 osg::ref_ptr< osg::Group >
170 const std::string &_name,
171 osg::ref_ptr< osg::Group > _group)
173 double r2 = _r / 2.0;
175 osg::ref_ptr< osg::Geometry > pyramidGeometry =
new osg::Geometry;
176 osg::ref_ptr< osg::Geode > geode =
new osg::Geode;
178 geode->addDrawable(pyramidGeometry);
180 osg::ref_ptr< osg::Vec3Array > pyramidVertices =
new osg::Vec3Array;
181 osg::ref_ptr< osg::Vec3Array > pyramidNormals =
new osg::Vec3Array;
183 osg::Vec3 p0 = osg::Vec3(_x - r2, _y - r2, _z - r2);
184 osg::Vec3 p1 = osg::Vec3(_x + r2, _y - r2, _z - r2);
185 osg::Vec3 p2 = osg::Vec3(_x + r2, _y + r2, _z - r2);
186 osg::Vec3 p3 = osg::Vec3(_x - r2, _y + r2, _z - r2);
187 osg::Vec3 p4 = osg::Vec3(_x, _y, _z + r2);
189 pyramidVertices->push_back(p0);
190 pyramidVertices->push_back(p1);
191 pyramidVertices->push_back(p2);
192 pyramidVertices->push_back(p3);
193 pyramidVertices->push_back(p4);
195 pyramidGeometry->setVertexArray(pyramidVertices);
197 osg::ref_ptr< osg::DrawElementsUInt > pyramidBase =
198 new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
199 pyramidBase->push_back(3);
200 pyramidBase->push_back(2);
201 pyramidBase->push_back(1);
202 pyramidBase->push_back(0);
203 osg::Vec3 n0 = osg::Vec3(0.0f, 0.0f, -1.0f);
204 pyramidNormals->push_back(n0);
205 pyramidGeometry->addPrimitiveSet(pyramidBase);
207 osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceOne =
208 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
209 pyramidFaceOne->push_back(0);
210 pyramidFaceOne->push_back(1);
211 pyramidFaceOne->push_back(4);
212 osg::Vec3 n1 = ((p4 - p1) ^ (p0 - p1));
213 n1 = n1 / n1.length();
214 pyramidNormals->push_back(n1);
215 pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
217 osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceTwo =
218 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
219 pyramidFaceTwo->push_back(1);
220 pyramidFaceTwo->push_back(2);
221 pyramidFaceTwo->push_back(4);
222 osg::Vec3 n2 = ((p4 - p2) ^ (p1 - p2));
223 n2 = n2 / n2.length();
224 pyramidNormals->push_back(n2);
225 pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
227 osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceThree =
228 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
229 pyramidFaceThree->push_back(2);
230 pyramidFaceThree->push_back(3);
231 pyramidFaceThree->push_back(4);
232 osg::Vec3 n3 = ((p4 - p3) ^ (p2 - p3));
233 n3 = n3 / n3.length();
234 pyramidNormals->push_back(n3);
235 pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
237 osg::ref_ptr< osg::DrawElementsUInt > pyramidFaceFour =
238 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
239 pyramidFaceFour->push_back(3);
240 pyramidFaceFour->push_back(0);
241 pyramidFaceFour->push_back(4);
242 osg::Vec3 n4 = ((p4 - p0) ^ (p3 - p0));
243 n4 = n4 / n4.length();
244 pyramidNormals->push_back(n4);
245 pyramidGeometry->addPrimitiveSet(pyramidFaceFour);
247 pyramidGeometry->setNormalArray(pyramidNormals,
248 osg::Array::BIND_PER_PRIMITIVE_SET);
250 osg::ref_ptr< osg::Vec4Array > colors =
new osg::Vec4Array;
257 pyramidGeometry->setColorArray(colors, osg::Array::BIND_PER_PRIMITIVE_SET);
260 osg::ref_ptr< osg::Vec2Array > texcoords =
new osg::Vec2Array(5);
261 (*texcoords)[0].set(0.00f, 0.0f);
262 (*texcoords)[1].set(0.25f, 0.0f);
263 (*texcoords)[2].set(0.50f, 0.0f);
264 (*texcoords)[3].set(0.75f, 0.0f);
265 (*texcoords)[4].set(0.50f, 1.0f);
266 pyramidGeometry->setTexCoordArray(0, texcoords);
268 osg::ref_ptr< osg::Texture2D > texture =
new osg::Texture2D;
271 texture->setDataVariance(osg::Object::DYNAMIC);
274 osg::ref_ptr< osg::Image > image =
275 osgDB::readImageFile(
"C:\\tmp\\textures_osg\\texture0.tga");
279 texture->setImage(image);
284 osg::ref_ptr< osg::StateSet > stateOne =
new osg::StateSet();
288 stateOne->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
291 geode->setStateSet(stateOne);
295 std::cout <<
"Couldn't find texture file." << std::endl;
299 geode->setName(_name);
301 _group->addChild(geode);
306 osg::ref_ptr< osg::Group >
310 const double weight = 0.03;
313 0, 0, 0, size, 0, 0, weight, Color::Red(),
"(Gizmo) Cylinder", _group);
315 0, 0, 0, 0, size, 0, weight, Color::Green(),
"(Gizmo) Cylinder", _group);
317 0, 0, 0, 0, 0, size, weight, Color::Blue(),
"(Gizmo) Cylinder", _group);
319 if(_group->getName() ==
"")
321 _group->setName(
"Gizmo");
328 osg::ref_ptr< osg::Group >
332 const Color color = Color::Clouds();
334 for(
int x_axis = -size; x_axis <= size; ++x_axis)
337 x_axis, -size, 0, x_axis, size, 0, color,
"(UnitGrid) Line", _group);
340 for(
int y_axis = -size; y_axis <= size; ++y_axis)
343 -size, y_axis, 0, size, y_axis, 0, color,
"(UnitGrid) Line", _group);
346 if(_group->getName() ==
"")
348 _group->setName(
"UnitGrid");
355 osg::ref_ptr< osg::Group >
357 osg::ref_ptr< osg::Group > _group)
359 std::string timesFont(
"fonts/times.ttf");
363 osg::Vec3 position(0.0f, 0.0f, 0.0f);
366 osg::Geode *geode =
new osg::Geode();
367 osg::StateSet *stateset = geode->getOrCreateStateSet();
368 stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
369 stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
370 geode->setName(
"(Hud) Hud");
371 geode->addDrawable(updateText);
373 updateText->setCharacterSize(20.0f);
374 updateText->setFont(timesFont);
375 updateText->setColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
376 updateText->setText(
"");
377 updateText->setPosition(position);
378 updateText->setDataVariance(osg::Object::DYNAMIC);
380 _group->addChild(geode);
382 if(_group->getName() ==
"")
384 _group->setName(
"Hud");
394 std::string blanks(level*2,
' ');
396 osg::Geode *geode =
dynamic_cast< osg::Geode *
>(nd);
400 std::cout << blanks <<
"geode " << (
void *)geode << geode->getName() << std::endl;
405 osg::Group *gp =
dynamic_cast< osg::Group *
>(nd);
408 std::cout << blanks <<
"group " << gp->getName() << std::endl;
409 for(
unsigned int ic = 0; ic < gp->getNumChildren(); ic++)
414 std::cout << blanks <<
"unknown node " << nd << std::endl;