/* File: tutOSG.cpp */ #include #include #include #include #include #include #include #include #include #include #include #include "tutOSG.h" #include #include #define ENTER #define LEAVE osg::Geode* tutOSGcreateQuad(int nvert, float *vert, float *color) { int ivert; int doit; float *pvert; osg::Vec3f p0, p1, p2, v1, v2, n; // float d; ENTER osg::Vec3Array* coords = new osg::Vec3Array; osg::Vec4Array* colors = new osg::Vec4Array; osg::UByteArray* coordIndices = new osg::UByteArray; osg::UByteArray* colorIndices = new osg::UByteArray; osg::Vec3Array* normals = new osg::Vec3Array; // pvert = vert+(3*(nvert-1)); // for (ivert=0; ivertpush_back(osg::Vec3(pvert[0], pvert[1], pvert[2])); coordIndices->push_back(ivert); colorIndices->push_back(0); } colors->push_back(osg::Vec4(color[0], color[1], color[2], color[3])); osg::Geometry* geom = new osg::Geometry; geom->setVertexArray(coords); geom->setVertexIndices(coordIndices); geom->setColorArray(colors); geom->setColorIndices(colorIndices); geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); p0.set(vert[0], vert[1], vert[2]); p1.set(vert[3], vert[4], vert[5]); p2.set(vert[6], vert[7], vert[8]); v1 = p1-p0; v2 = p2-p0; v1.normalize(); v2.normalize(); n.set( v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1]-v1[1]*v2[0] ); normals->push_back(n); geom->setNormalArray(normals); geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coordIndices->size())); osg::Geode* geode = new osg::Geode(); osg::StateSet* stateset = new osg::StateSet(); geode->setStateSet( stateset ); geode->addDrawable(geom); LEAVE return geode; } osg::Geode* tutOSGcreateShape(int type, float radius, float height, float *color) { static char routine[] = "tutOSGcreateShape"; int doit; ENTER osg::ShapeDrawable *drawable; osg::Geode* geode = new osg::Geode(); osg::StateSet* stateset = new osg::StateSet(); geode->setStateSet( stateset ); float xcen = 0.0f; float ycen = 0.0f; float zcen = 0.0f; // osg::Vec3* vcen = new osg::Vec3(xcen, ycen, zcen); osg::Vec3 vcen(xcen, ycen, zcen); osg::TessellationHints* hints = new osg::TessellationHints; hints->setDetailRatio(0.5f); doit = 1; switch(type) { case SHAPE_BOX: drawable = new osg::ShapeDrawable( new osg::Box(vcen,radius),hints); break; case SHAPE_CONE: drawable = new osg::ShapeDrawable( new osg::Cone(vcen,radius,height),hints); break; case SHAPE_CYLINDER: drawable = new osg::ShapeDrawable( new osg::Cylinder(vcen,radius,height),hints); break; case SHAPE_SPHERE: drawable = new osg::ShapeDrawable( new osg::Sphere(vcen,radius),hints); break; case SHAPE_CAPSULE: drawable = new osg::ShapeDrawable( new osg::Capsule(vcen,radius,height),hints); break; default: fprintf(stderr, "ERROR in %s: type = %d\n", routine); fflush(stderr); doit = 0; break; } if (doit) { geode->addDrawable(drawable); drawable->setColor(osg::Vec4(color[0], color[1], color[2], color[3])); } LEAVE return geode; } osg::Node* tutOSGcreateBaseGrid(int xmin, int xmax, int ymin, int ymax) { osg::Group* base = new osg::Group; osg::Geode* geode; int ix, iy; float zsq = -0.01; float zbg = -0.02; float colorsq[4] = {0.6, 0.6, 0.6, 1.0}; float colorbg[4] = {0.3, 0.3, 0.3, 1.0}; float inset = 0.04; float cellvert[12] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0}; float vert[12]; vert[ 2] = zsq; vert[ 5] = zsq; vert[ 8] = zsq; vert[11] = zsq; for(iy=ymin; iyaddChild(geode); } } vert[ 0] = xmin; vert[ 3] = xmax; vert[ 6] = xmax; vert[ 9] = xmin; vert[ 1] = ymin; vert[ 4] = ymin; vert[ 7] = ymax; vert[10] = ymax; vert[ 2] = zbg; vert[ 5] = zbg; vert[ 8] = zbg; vert[11] = zbg; geode = tutOSGcreateQuad(4, vert, colorbg); base->addChild(geode); return base; }