GeometryPtr createGeo(UInt32 mat) { GeoIndicesPtr ind = _ind[mat]; FDEBUG(("CreateGeoRec::createGeo: Mat %d: %d inds (%d tris)\n", mat, ind->getSize(), ind->getSize()/3)); endEditCP(ind); GeometryPtr geo = Geometry::create(); GeoPLengthsUI32Ptr len = GeoPLengthsUI32::create(); beginEditCP(len); len->push_back(ind->getSize()); endEditCP(len); GeoPTypesUI8Ptr typ = GeoPTypesUI8::create(); beginEditCP(typ); typ->push_back(GL_TRIANGLES); endEditCP(typ); beginEditCP(geo); if(random_color) { SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); Color3f c( drand48() * .7f + .3f, drand48() * .7f + .3f, drand48() * .7f + .3f); mat->setDiffuse(c); mat->setSpecular(Color3f(1.f,1.f,1.f)); mat->setShininess(10.f); endEditCP(mat); geo->setMaterial(mat); } else { geo->setMaterial(MaterialPool::getMaterial(mat)); } geo->setPositions(_pos); geo->setIndices(ind); geo->setLengths(len); geo->setTypes(typ); geo->getIndexMapping().push_back(Geometry::MapPosition); endEditCP(geo); calcVertexNormals(geo); //calcFaceNormals(geo); createSingleIndex(geo); return geo; }
GeometryPtr OpenSGLatticeGeometry::createGeometry(const bool all) { showAll = all; latticeGeo = Geometry::create(); #if OSG_MAJOR_VERSION < 2 // 2013-08-27 ZaJ: is this portable to OpenSG2? I've got no idea where setName is coming from... setName(latticeGeo, "LatticeGeometry"); #endif #if OSG_MAJOR_VERSION >= 2 latticeTypes = GeoUInt8Property::create(); latticePoints = GeoPnt3fProperty::create(); latticeIndices = GeoUInt32Property::create(); latticeColors = GeoColor3fProperty::create(); numOfPointsPerType = GeoUInt32Property::create(); #else // OpenSG1.8: latticeTypes = GeoPTypesUI8::create(); latticePoints = GeoPositions3f::create(); latticeIndices = GeoIndicesUI32::create(); latticeColors = GeoColors3f::create(); numOfPointsPerType = GeoPLengthsUI32::create(); #endif SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat, SimpleMaterial::LitFieldMask); mat->setLit(false); endEditCP(mat, SimpleMaterial::LitFieldMask); latticeGeo->setMaterial(mat); // define OPEN_GL types for lattice beginEditCP(latticeTypes, GeoPTypesUI8::GeoPropDataFieldMask); latticeTypes->addValue(GL_POINTS); latticeTypes->addValue(GL_LINES); endEditCP(latticeTypes, GeoPTypesUI8::GeoPropDataFieldMask); calcNumOfPoints(all); // define number of points for each OPEN_GL type for lattice beginEditCP(numOfPointsPerType, GeoPLengthsUI32::GeoPropDataFieldMask); numOfPointsPerType->addValue(numOfLatticePoints); numOfPointsPerType->addValue(numOfLatticeLinePoints); endEditCP(numOfPointsPerType, GeoPLengthsUI32::GeoPropDataFieldMask); updateGeometry(all); return latticeGeo; }
// This function creates a cylinder similarly to the teapot // and the torus above. This time the cylinder is trimmed with // a circle defined by 9 square-based control points to show // an example of defining a circle without using infinite control // points (see [Piegl and Tiller], pp. 299, Ex7.2 (Figure 7.16)) as // well as to show how to define a rational trimming curve. NodePtr makeTrimmedCylinder() { SimpleMaterialPtr cylmat = SimpleMaterial::create(); beginEditCP(cylmat); { cylmat->setDiffuse( Color3f(0.0, 0.8, 0.7)); cylmat->setAmbient( Color3f(0.2, 0.2, 0.2)); cylmat->setEmission( Color3f(0.02, 0.02, 0.02) ); cylmat->setSpecular( Color3f(0.78, 0.78, 0.78) ); cylmat->setShininess( 128 ); cylmat->addChunk(g_fb_chunk); } endEditCP(cylmat); float knots4[4] = {0, 0, 1, 1}; float knots_circle[8] = {0, 0, 0, 0.5, 0.5, 1, 1, 1}; float knots_outertrim[7] = {0, 0, 1, 2, 3, 4, 4}; float knots_trimcircle[12] = {0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1 }; float cylcps[10][4] = { { 1.00, 0, -.50, 1}, { 1.00, 0, .50, 1}, { 0, 1.00, 0, 0}, { 0, 1.00, 0, 0}, {-1.00, 0, -.50, 1}, {-1.00, 0, .50, 1}, { 0, -1.00, 0, 0}, { 0, -1.00, 0, 0}, { 1.00, 0, -.50, 1}, { 1.00, 0, .50, 1}, }; float outertrim_cps[5][2] = { {0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, }; float trimcircle_cps[9][3] = { {0.541667, 0.5, 1}, {0.383016, 0.235702, 0.707107}, {0.5, 0.333333, 1}, {0.324091, 0.235702, 0.707107}, {0.458333, 0.5, 1}, {0.324091, 0.471405, 0.707107}, {0.5, 0.666667, 1}, {0.383016, 0.471405, 0.707107}, {0.541667, 0.5, 1}, }; NodePtr cylnode = makeSurface(10, cylcps, 2, 1, 8, knots_circle, 4, knots4, 0.001, cylmat); SurfacePtr s = SurfacePtr::dcast(cylnode->getCore()); // add outer trimming around the domain addTrimCurve(s, 5, outertrim_cps, 1, 7, knots_outertrim, true); // add inside circle trimming addTrimCurve(s, 9, trimcircle_cps, 2, 12, knots_trimcircle, true); return cylnode; }
//! Copy Constructor DVRVolume::DVRVolume(const DVRVolume &source) : Inherited (source), drawStyleListValid(false ), textureManager (this ), shadingInitialized(false ) { SINFO << "DVRVolume::DVRVolume(const DVRVolume &source) this: " << this << std::endl; //!! FIXME: //!! This is only performed during instantiation of real objects //!! Which is only done with the copy constructor //!! //!! Otherwise my code cores - (maybe the copy constructor of FCPtr is //!! broken?!) DVRVolumePtr ptr(*this); // Fake material for render action SimpleMaterialPtr m = SimpleMaterial::create(); beginEditCP(m); { m->setTransparency(0.001f ); m->setLit (false ); m->setDiffuse (Color3f(1.0, 1.0, 1.0)); m->setAmbient (Color3f(1.0, 1.0, 1.0)); } endEditCP(m); // Chunk material as storage fieldcontainer for textures ChunkMaterialPtr cm = SimpleMaterial::create(); // Add all beginEditCP(ptr, RenderMaterialFieldMask | TextureStorageFieldMask); { setRenderMaterial(m ); setTextureStorage(cm); } endEditCP (ptr, RenderMaterialFieldMask | TextureStorageFieldMask); commonConstructor(); }
NodePtr createScenegraph(){ SimpleMaterialPtr simpleMaterial = SimpleMaterial::create(); beginEditCP (simpleMaterial, SimpleMaterial::DiffuseFieldMask | SimpleMaterial::AmbientFieldMask | SimpleMaterial::TransparencyFieldMask); simpleMaterial->setDiffuse(Color3f(1,0,0)); simpleMaterial->setAmbient(Color3f(0.2, 0.2, 0.2)); simpleMaterial->setTransparency(0.5); endEditCP (simpleMaterial, SimpleMaterial::DiffuseFieldMask | SimpleMaterial::AmbientFieldMask | SimpleMaterial::TransparencyFieldMask); NodePtr simpleMaterialNode = Node::create(); NodePtr sphere = makeSphere(2,6); MaterialGroupPtr mg = MaterialGroup::create(); beginEditCP(mg); mg->setMaterial(simpleMaterial); endEditCP(mg); beginEditCP(simpleMaterialNode); simpleMaterialNode->setCore(mg); simpleMaterialNode->addChild(sphere); endEditCP(simpleMaterialNode); NodePtr root = Node::create(); beginEditCP(root); root->setCore(Group::create()); root->addChild(simpleMaterialNode); endEditCP(root); return root; }
// This function creates a torus from a single NURBS surface // using only 25 control points (of which 12 are unique). // Similarly to the teapot, this uses the half-circle // constructed from 3 control points (of which the middle point // is infinite) which is then mirrored to form a full circle. // (See the NURBS book [Piegl and Tiller], pp. 296, Ex7.1 (Figure 7.15) // for details on the half-circle.) NodePtr makeTorus() { SimpleMaterialPtr torusmat = SimpleMaterial::create(); beginEditCP(torusmat); { torusmat->setDiffuse( Color3f(1.0, 0.0, 0.2)); torusmat->setAmbient( Color3f(0.2, 0.2, 0.2)); torusmat->setEmission( Color3f(0.02, 0.02, 0.02) ); torusmat->setSpecular( Color3f(0.78, 0.78, 0.78) ); torusmat->setShininess( 128 ); torusmat->addChunk(g_fb_chunk); } endEditCP(torusmat); float knots_circle[8] = {0, 0, 0, 0.5, 0.5, 1, 1, 1}; float toruscps[25][4] = { { 1.00, 0, .75, 1}, // { .25, 0, 0, 0}, // { 1.00, 0, 1.25, 1}, // { -.25, 0, 0, 0}, // { 1.00, 0, .75, 1}, { 0, 1.00, 0, 0}, // { 0, .25, 0, 0}, // { 0, 1.00, 0, 0}, { 0, -.25, 0, 0}, // { 0, 1.00, 0, 0}, {-1.00, 0, .75, 1}, // { -.25, 0, 0, 0}, // {-1.00, 0, 1.25, 1}, // { .25, 0, 0, 0}, // {-1.00, 0, .75, 1}, { 0, -1.00, 0, 0}, // { 0, -.25, 0, 0}, { 0, -1.00, 0, 0}, { 0, .25, 0, 0}, { 0, -1.00, 0, 0}, { 1.00, 0, .75, 1}, { .25, 0, 0, 0}, { 1.00, 0, 1.25, 1}, { -.25, 0, 0, 0}, { 1.00, 0, .75, 1}, }; NodePtr torus = makeSurface(25, toruscps, 2, 2, 8, knots_circle, 8, knots_circle, 0.005, torusmat); return torus; }
int main(int argc, char *argv[]) { osgLogP->setLogLevel(LOG_NOTICE); osgInit(argc, argv); int winid = setupGLUT(&argc, argv); // create a GLUT window GLUTWindowPtr gwin = GLUTWindow::create(); gwin->setId(winid); gwin->init(); osgLogP->setLogLevel(LOG_DEBUG); // build the test scene NodePtr pRoot = Node ::create(); GroupPtr pRootCore = Group::create(); NodePtr pRayGeo = Node ::create(); NodePtr pScene = buildGraph(); GroupPtr pSceneCore = Group::create(); Time tStart; Time tStop; Time tDFTotal = 0.0; Time tDFSTotal = 0.0; Time tPTotal = 0.0; Time tOTotal = 0.0; StatCollector statP; StatCollector statDF; StatCollector statDFS; beginEditCP(pRoot, Node::CoreFieldId | Node::ChildrenFieldId); pRoot->setCore (pRootCore ); pRoot->addChild(pScene ); pRoot->addChild(pRayGeo ); endEditCP (pRoot, Node::CoreFieldId | Node::ChildrenFieldId); createRays(uiNumRays, testRays); // build the geometry to visualize the rays pPoints = GeoPositions3f::create(); beginEditCP(pPoints); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); pPoints->addValue(Pnt3f(0.0, 0.0, 0.0)); endEditCP (pPoints); GeoIndicesUI32Ptr pIndices = GeoIndicesUI32::create(); beginEditCP(pIndices); pIndices->addValue(0); pIndices->addValue(1); pIndices->addValue(2); pIndices->addValue(3); pIndices->addValue(4); endEditCP (pIndices); GeoPLengthsPtr pLengths = GeoPLengthsUI32::create(); beginEditCP(pLengths); pLengths->addValue(2); pLengths->addValue(3); endEditCP (pLengths); GeoPTypesPtr pTypes = GeoPTypesUI8::create(); beginEditCP(pTypes); pTypes->addValue(GL_LINES ); pTypes->addValue(GL_TRIANGLES); endEditCP (pTypes); GeoColors3fPtr pColors = GeoColors3f::create(); beginEditCP(pColors); pColors->addValue(Color3f(1.0, 1.0, 1.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); pColors->addValue(Color3f(1.0, 0.0, 0.0)); endEditCP (pColors); SimpleMaterialPtr pMaterial = SimpleMaterial::create(); beginEditCP(pMaterial); pMaterial->setLit(false); endEditCP (pMaterial); GeometryPtr pRayGeoCore = Geometry::create(); beginEditCP(pRayGeoCore); pRayGeoCore->setPositions(pPoints ); pRayGeoCore->setIndices (pIndices ); pRayGeoCore->setLengths (pLengths ); pRayGeoCore->setTypes (pTypes ); pRayGeoCore->setColors (pColors ); pRayGeoCore->setMaterial (pMaterial); endEditCP (pRayGeoCore); beginEditCP(pRayGeo, Node::CoreFieldId); pRayGeo->setCore(pRayGeoCore); endEditCP (pRayGeo, Node::CoreFieldId); IntersectActor::regDefaultClassEnter( osgTypedFunctionFunctor2CPtr< NewActionTypes::ResultE, NodeCorePtr, ActorBase::FunctorArgumentType & >(enterDefault)); NewActionBase *pDFAction = DepthFirstAction ::create(); NewActionBase *pDFSAction = DepthFirstStateAction::create(); NewActionBase *pPAction = PriorityAction ::create(); IntersectActor *pIActorDF = IntersectActor ::create(); IntersectActor *pIActorDFS = IntersectActor ::create(); IntersectActor *pIActorP = IntersectActor ::create(); pDFAction ->setStatistics(&statDF ); pDFSAction->setStatistics(&statDFS); pPAction ->setStatistics(&statP ); // IntersectActor with DFS-Action does not need leave calls pIActorDFS->setLeaveNodeFlag(false); pDFAction ->addActor(pIActorDF ); pDFSAction->addActor(pIActorDFS); pPAction ->addActor(pIActorP ); // create old action IntersectAction *pIntAction = IntersectAction ::create(); // make sure bv are up to date pScene->updateVolume(); SINFO << "-=< Intersect >=-" << endLog; std::vector<Line>::iterator itRays = testRays.begin(); std::vector<Line>::iterator endRays = testRays.end (); for(; itRays != endRays; ++itRays) { // DepthFirst tStart = getSystemTime(); pIActorDF->setRay (*itRays); pIActorDF->setMaxDistance(10000.0); pIActorDF->reset ( ); pDFAction->apply(pScene); tStop = getSystemTime(); tDFTotal += (tStop - tStart); if(pIActorDF->getHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIActorDF->getHitObject (); result._tri = pIActorDF->getHitTriangleIndex(); result._dist = pIActorDF->getHitDistance (); result._time = (tStop - tStart); resultsDF.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsDF.push_back(result); } std::string strStatDF; statDF.putToString(strStatDF); //SINFO << "stat DF: " << strStatDF << endLog; // Depth First State tStart = getSystemTime(); pIActorDFS->setRay (*itRays); pIActorDFS->setMaxDistance(10000.0); pIActorDFS->reset ( ); pDFSAction->apply(pScene); tStop = getSystemTime(); tDFSTotal += (tStop - tStart); if(pIActorDFS->getHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIActorDFS->getHitObject (); result._tri = pIActorDFS->getHitTriangleIndex(); result._dist = pIActorDFS->getHitDistance (); result._time = (tStop - tStart); resultsDFS.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsDFS.push_back(result); } std::string strStatDFS; statDFS.putToString(strStatDFS); //SINFO << "stat DFS: " << strStatDFS << endLog; // Priority tStart = getSystemTime(); pIActorP->setRay (*itRays); pIActorP->setMaxDistance(10000.0); pIActorP->reset ( ); pPAction->apply(pScene); tStop = getSystemTime(); tPTotal += (tStop - tStart); if(pIActorP->getHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIActorP->getHitObject (); result._tri = pIActorP->getHitTriangleIndex(); result._dist = pIActorP->getHitDistance (); result._time = (tStop - tStart); resultsP.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsP.push_back(result); } std::string strStatP; statP.putToString(strStatP); //SINFO << "stat P: " << strStatP << endLog; // Old tStart = getSystemTime(); pIntAction->setLine(*itRays, 100000); pIntAction->apply (pScene ); tStop = getSystemTime(); tOTotal += (tStop - tStart); if(pIntAction->didHit() == true) { IntersectResult result; result._hit = true; result._pObj = pIntAction->getHitObject (); result._tri = pIntAction->getHitTriangle(); result._dist = pIntAction->getHitT (); result._time = (tStop - tStart); resultsO.push_back(result); } else { IntersectResult result; result._hit = false; result._pObj = NullFC; result._tri = -1; result._dist = 0.0; result._time = (tStop - tStart); resultsO.push_back(result); } } UInt32 DFwins = 0; UInt32 DFwinsHit = 0; UInt32 DFwinsMiss = 0; UInt32 DFSwins = 0; UInt32 DFSwinsHit = 0; UInt32 DFSwinsMiss = 0; UInt32 Pwins = 0; UInt32 PwinsHit = 0; UInt32 PwinsMiss = 0; UInt32 Owins = 0; UInt32 OwinsHit = 0; UInt32 OwinsMiss = 0; UInt32 failCount = 0; UInt32 passCount = 0; UInt32 hitCount = 0; UInt32 missCount = 0; for(UInt32 i = 0; i < uiNumRays; ++i) { bool DFfastest = ((resultsDF [i]._time <= resultsDFS[i]._time) && (resultsDF [i]._time <= resultsP [i]._time) && (resultsDF [i]._time <= resultsO [i]._time) ); bool DFSfastest = ((resultsDFS[i]._time <= resultsDF [i]._time) && (resultsDFS[i]._time <= resultsP [i]._time) && (resultsDFS[i]._time <= resultsO [i]._time) ); bool Pfastest = ((resultsP [i]._time <= resultsDF [i]._time) && (resultsP [i]._time <= resultsDFS[i]._time) && (resultsP [i]._time <= resultsO [i]._time) ); bool Ofastest = ((resultsO [i]._time <= resultsDF [i]._time) && (resultsO [i]._time <= resultsDFS[i]._time) && (resultsO [i]._time <= resultsP [i]._time) ); if((resultsDF [i]._hit == resultsDFS[i]._hit) && (resultsDFS[i]._hit == resultsP [i]._hit) && (resultsP [i]._hit == resultsO [i]._hit) ) { if((osgabs(resultsDF [i]._dist - resultsDFS[i]._dist) >= 0.001) || (osgabs(resultsDFS[i]._dist - resultsP [i]._dist) >= 0.001) || (osgabs(resultsP [i]._dist - resultsO [i]._dist) >= 0.001) || (osgabs(resultsO [i]._dist - resultsDF [i]._dist) >= 0.001) ) { ++failCount; SINFO << "FAIL: df: " << resultsDF [i]._dist << " dfs: " << resultsDFS[i]._dist << " p: " << resultsP [i]._dist << " o: " << resultsO [i]._dist << endLog; SINFO << "FAIL: df: " << resultsDF [i]._tri << " dfs: " << resultsDFS[i]._tri << " p: " << resultsP [i]._tri << " o: " << resultsO [i]._tri << endLog; } else { ++passCount; } if(resultsDF[i]._hit == true) { ++hitCount; DFwinsHit = DFfastest ? DFwinsHit + 1 : DFwinsHit; DFSwinsHit = DFSfastest ? DFSwinsHit + 1 : DFSwinsHit; PwinsHit = Pfastest ? PwinsHit + 1 : PwinsHit; OwinsHit = Ofastest ? OwinsHit + 1 : OwinsHit; } else { ++missCount; DFwinsMiss = DFfastest ? DFwinsMiss + 1 : DFwinsMiss; DFSwinsMiss = DFSfastest ? DFSwinsMiss + 1 : DFSwinsMiss; PwinsMiss = Pfastest ? PwinsMiss + 1 : PwinsMiss; OwinsMiss = Ofastest ? OwinsMiss + 1 : OwinsMiss; } DFwins = DFfastest ? DFwins + 1 : DFwins; DFSwins = DFSfastest ? DFSwins + 1 : DFSwins; Pwins = Pfastest ? Pwins + 1 : Pwins; Owins = Ofastest ? Owins + 1 : Owins; } else { ++failCount; } //SINFO << i << " \t" << (DFfastest ? "D ->" : " ") << " hit: " << resultsDF [i]._hit << " time: " << resultsDF [i]._time << endLog; //SINFO << " \t" << (DFSfastest ? "S ->" : " ") << " hit: " << resultsDFS[i]._hit << " time: " << resultsDFS[i]._time << endLog; //SINFO << " \t" << (Pfastest ? "P ->" : " ") << " hit: " << resultsP [i]._hit << " time: " << resultsP [i]._time << endLog; //SINFO << " \t" << (Ofastest ? "O ->" : " ") << " hit: " << resultsO [i]._hit << " time: " << resultsO [i]._time << endLog; } SINFO << " df total: " << tDFTotal << (tDFTotal < tDFSTotal && tDFTotal < tPTotal && tDFTotal < tOTotal ? " *" : " ") << " wins: " << DFwins << " (" << (static_cast<Real32>(DFwins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << DFwinsHit << " (" << (static_cast<Real32>(DFwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << DFwinsMiss << " (" << (static_cast<Real32>(DFwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << " dfs total: " << tDFSTotal << (tDFSTotal < tDFTotal && tDFSTotal < tPTotal && tDFSTotal < tOTotal ? " *" : " ") << " wins: " << DFSwins << " (" << (static_cast<Real32>(DFSwins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << DFSwinsHit << " (" << (static_cast<Real32>(DFSwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << DFSwinsMiss << " (" << (static_cast<Real32>(DFSwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << " p total: " << tPTotal << (tPTotal < tDFTotal && tPTotal < tDFSTotal && tPTotal < tOTotal ? " *" : " ") << " wins: " << Pwins << " (" << (static_cast<Real32>(Pwins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << PwinsHit << " (" << (static_cast<Real32>(PwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << PwinsMiss << " (" << (static_cast<Real32>(PwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << " o total: " << tOTotal << (tOTotal < tDFTotal && tOTotal < tDFSTotal && tOTotal < tPTotal ? " *" : " ") << " wins: " << Owins << " (" << (static_cast<Real32>(Owins) / static_cast<Real32>(passCount)) * 100.0 << "%)\t" << " wins on hit: " << OwinsHit << " (" << (static_cast<Real32>(OwinsHit) / static_cast<Real32>(hitCount )) * 100.0 << "%)\t" << " wins on miss: " << OwinsMiss << " (" << (static_cast<Real32>(OwinsMiss) / static_cast<Real32>(missCount)) * 100.0 << "%)" << endLog; SINFO << "pass: "******" fail: " << failCount << " hit: " << hitCount << " miss: " << missCount << endLog; osgLogP->setLogLevel(LOG_NOTICE); #if 0 // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (pRoot); // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); #endif return 0; }
NodePtr Puck::init() { // CREATE THE PUCK NodePtr puck_trans_node = makeCoredNode<Transform>(&transPtr); beginEditCP(transPtr); { transPtr->getMatrix().setTranslate(position[0],position[1],position[2]); } endEditCP(transPtr); NodePtr puck = OSG::makeCylinder(PUCK_HALF_HEIGHT*2.0,radius, 32, true, true ,true); beginEditCP(puck_trans_node); { puck_trans_node->addChild(puck); } endEditCP(puck_trans_node); SimpleMaterialPtr puck_mat = SimpleMaterial::create(); beginEditCP(puck_mat); { puck_mat->setAmbient(Color3f(0.0,0.0,0.0)); puck_mat->setDiffuse(Color3f(1.0,0.0,0.0)); } endEditCP(puck_mat); GeometryPtr puck_geo = GeometryPtr::dcast(puck->getCore()); beginEditCP(puck_geo); puck_geo->setMaterial(puck_mat); beginEditCP(puck_geo); ///////////////////////////// // SETUP THE INTERSECTION TEST COMPONENTS // Create a small geometry to show the ray and what was hit // Contains a line and a single triangle. // The line shows the ray, the triangle whatever was hit. isectPoints = GeoPositions3f::create(); beginEditCP(isectPoints); { isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); } endEditCP(isectPoints); GeoIndicesUI32Ptr index = GeoIndicesUI32::create(); beginEditCP(index); { index->addValue(0); index->addValue(1); index->addValue(2); index->addValue(3); index->addValue(4); } endEditCP(index); GeoPLengthsUI32Ptr lens = GeoPLengthsUI32::create(); beginEditCP(lens); { lens->addValue(2); lens->addValue(3); } endEditCP(lens); GeoPTypesUI8Ptr type = GeoPTypesUI8::create(); beginEditCP(type); { type->addValue(GL_LINES); type->addValue(GL_TRIANGLES); } endEditCP(type); SimpleMaterialPtr red = SimpleMaterial::create(); beginEditCP(red); { red->setDiffuse (Color3f( 1,0,0 )); red->setTransparency(0.5); red->setLit (false); } endEditCP (red); testgeocore = Geometry::create(); beginEditCP(testgeocore); { testgeocore->setPositions(isectPoints); testgeocore->setIndices(index); testgeocore->setLengths(lens); testgeocore->setTypes(type); testgeocore->setMaterial(red); } endEditCP(testgeocore); NodePtr testgeo = Node::create(); beginEditCP(testgeo); { testgeo->setCore(testgeocore); } endEditCP(testgeo); beginEditCP(puck_trans_node); { puck_trans_node->addChild(testgeo); } return puck_trans_node; }
NodePtr createScenegraph(){ // the scene must be created here for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) wMesh[x][z] = 0; // GeoPTypes will define the types of primitives to be used GeoPTypesPtr type = GeoPTypesUI8::create(); beginEditCP(type, GeoPTypesUI8::GeoPropDataFieldMask); // we want to use quads ONLY type->addValue(GL_QUADS); endEditCP(type, GeoPTypesUI8::GeoPropDataFieldMask); // GeoPLength will define the number of vertices of // the used primitives GeoPLengthsPtr length = GeoPLengthsUI32::create(); beginEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask); // the length of our quads is four ;-) length->addValue((N-1)*(N-1)*4); endEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask); // GeoPositions3f stores the positions of all vertices used in // this specific geometry core GeoPositions3fPtr pos = GeoPositions3f::create(); beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); // here they all come for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) pos->addValue(Pnt3f(x, wMesh[x][z], z)); endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); //GeoColors3f stores all color values that will be used GeoColors3fPtr colors = GeoColors3f::create(); beginEditCP(colors, GeoColors3f::GeoPropDataFieldMask); for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) colors->addValue(Color3f(0,0,1)); endEditCP(colors, GeoColors3f::GeoPropDataFieldMask); GeoNormals3fPtr norms = GeoNormals3f::create(); beginEditCP(norms, GeoNormals3f::GeoPropDataFieldMask); for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) // As initially all heights are set to zero thus yielding a plane, // we set all normals to (0,1,0) parallel to the y-axis norms->addValue(Vec3f(0,1,0)); endEditCP(norms, GeoNormals3f::GeoPropDataFieldMask); SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setDiffuse(Color3f(0,0,1)); endEditCP(mat); // GeoIndicesUI32 points to all relevant data used by the // provided primitives GeoIndicesUI32Ptr indices = GeoIndicesUI32::create(); beginEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask); for (int x = 0; x < N-1; x++) for (int z = 0; z < N-1; z++){ // points to four vertices that will // define a single quad indices->addValue(z*N+x); indices->addValue((z+1)*N+x); indices->addValue((z+1)*N+x+1); indices->addValue(z*N+x+1); } endEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask); GeometryPtr geo = Geometry::create(); beginEditCP(geo, Geometry::TypesFieldMask | Geometry::LengthsFieldMask | Geometry::IndicesFieldMask | Geometry::PositionsFieldMask | Geometry::NormalsFieldMask | Geometry::MaterialFieldMask | Geometry::ColorsFieldMask | Geometry::DlistCacheFieldMask ); geo->setTypes(type); geo->setLengths(length); geo->setIndices(indices); geo->setPositions(pos); geo->setNormals(norms); geo->setMaterial(mat); //geo->setColors(colors); geo->setDlistCache(false); endEditCP(geo, Geometry::TypesFieldMask | Geometry::LengthsFieldMask | Geometry::IndicesFieldMask | Geometry::PositionsFieldMask | Geometry::NormalsFieldMask | Geometry::MaterialFieldMask | Geometry::ColorsFieldMask | Geometry::DlistCacheFieldMask ); PointLightPtr pLight = PointLight::create(); NodePtr root = Node::create(); NodePtr water = Node::create(); NodePtr pLightTransformNode = Node::create(); TransformPtr pLightTransform = Transform::create(); NodePtr pLightNode = Node::create(); beginEditCP(pLightNode); pLightNode->setCore(Group::create()); endEditCP(pLightNode); Matrix m; m.setIdentity(); m.setTranslate(50,25,50); beginEditCP(pLightTransform); pLightTransform->setMatrix(m); endEditCP(pLightTransform); //we add a little spehere that will represent the light source GeometryPtr sphere = makeSphereGeo(2,2); SimpleMaterialPtr sm = SimpleMaterial::create(); beginEditCP(sm, SimpleMaterial::DiffuseFieldMask | SimpleMaterial::LitFieldMask); { sm->setLit(false); sm->setDiffuse(Color3f(1,1,1)); } endEditCP (sm, SimpleMaterial::DiffuseFieldMask | SimpleMaterial::LitFieldMask); beginEditCP(sphere, Geometry::MaterialFieldMask); { sphere->setMaterial(sm); } endEditCP (sphere, Geometry::MaterialFieldMask); NodePtr sphereNode = Node::create(); beginEditCP(sphereNode); sphereNode->setCore(sphere); endEditCP(sphereNode); beginEditCP(pLightTransformNode); pLightTransformNode->setCore(pLightTransform); pLightTransformNode->addChild(pLightNode); pLightTransformNode->addChild(sphereNode); endEditCP(pLightTransformNode); beginEditCP(pLight); pLight->setPosition(Pnt3f(0,0,0)); //Attenuation parameters pLight->setConstantAttenuation(1); pLight->setLinearAttenuation(0); pLight->setQuadraticAttenuation(0); //color information pLight->setDiffuse(Color4f(1,1,1,1)); pLight->setAmbient(Color4f(0.2,0.2,0.2,1)); pLight->setSpecular(Color4f(1,1,1,1)); //set the beacon pLight->setBeacon(pLightNode); endEditCP (pLight); beginEditCP(water); water->setCore(geo); endEditCP(water); beginEditCP(root); root->setCore(pLight); root->addChild(water); root->addChild(pLightTransformNode); endEditCP(root); return root; }
int main(int argc, char **argv) { osgInit(argc,argv); // GLUT init glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); PassiveWindowPtr pwin=PassiveWindow::create(); pwin->init(); // create the texture tx1 = TextureChunk::create(); const UInt16 width = 16, height = 1; ImagePtr pImg1 = Image::create(); pImg1->set(Image::OSG_RGB_PF, width, height ); beginEditCP(pImg1); UInt8 *d = pImg1->editData(); for(UInt16 y = 0; y < height; ++y) { for(UInt16 x = 0; x < width; ++x) { *d++ = static_cast<UInt8>(x * 255.f / width); *d++ = static_cast<UInt8>(y * 255.f / height); *d++ = static_cast<UInt8>(128); } } endEditCP(pImg1); beginEditCP(tx1); tx1->setImage(pImg1); tx1->setMinFilter(GL_NEAREST); tx1->setMagFilter(GL_NEAREST); tx1->setWrapS(GL_REPEAT); tx1->setWrapT(GL_REPEAT); endEditCP(tx1); tg = TexGenChunk::create(); beginEditCP(tg); tg->setGenFuncS(GL_EYE_LINEAR); tg->setGenFuncSPlane(Vec4f(0,.15,0,0)); endEditCP(tg); // create the material SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setDiffuse(Color3f(1,1,1)); mat->setLit(false); mat->addChunk(tx1); mat->addChunk(tg); endEditCP(mat); // create the scene NodePtr torus = makeTorus( .5, 2, 16, 32 ); GeometryPtr geo = GeometryPtr::dcast(torus->getCore()); beginEditCP(geo, Geometry::MaterialFieldMask); geo->setMaterial(mat); endEditCP(geo, Geometry::MaterialFieldMask); transn1 = makeCoredNode<Transform>(&trans1); beginEditCP(transn1, Node::CoreFieldMask | Node::ChildrenFieldMask); { transn1->addChild(torus); } endEditCP (transn1, Node::CoreFieldMask | Node::ChildrenFieldMask); transn2 = makeCoredNode<Transform>(&trans2); NodePtr scene = makeCoredNode<Group>(); beginEditCP(scene); scene->addChild(transn1); scene->addChild(transn2); endEditCP(scene); beginEditCP(tg); tg->setSBeacon(torus); endEditCP(tg); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // create the window and initial camera/viewport mgr->setWindow(pwin ); // tell the manager what to manage mgr->setRoot (scene); // show the whole scene mgr->showAll(); mgr->redraw(); webInterface = new WebInterface(); webInterface->setRoot(scene); // GLUT main loop glutMainLoop(); return 0; }
int main (int argc, char **argv) { // GLUT init osgInit(argc, argv); osgLog().setLogLevel ( OSG::LOG_DEBUG ); FieldContainerPtr pProto = Geometry::getClassType().getPrototype(); GeometryPtr pGeoProto = GeometryPtr::dcast(pProto); if(pGeoProto != NullFC) { pGeoProto->setDlistCache(true); } glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); UInt32 id = glutCreateWindow("OpenSG"); glutKeyboardFunc(key); glutReshapeFunc(resize); glutDisplayFunc(display); // glutMouseFunc(mouse); // glutMotionFunc(motion); glutIdleFunc(display); // create a material (need that to test textures) ChunkMaterialPtr mat; beginEditCP(mat); mat = ChunkMaterial::create(); MaterialChunkPtr mc = MaterialChunk::create(); beginEditCP(mc); mc->setDiffuse( Color4f( 1,.8,.8,1 ) ); mc->setAmbient( Color4f( 0.1,0.1,0.1,1 ) ); mc->setSpecular( Color4f( 1,1,1,1 ) ); mc->setShininess( 20 ); mc->setBackMaterial(true); mc->setBackColorMaterial(GL_DIFFUSE); mc->setBackDiffuse( Color4f( 1,0,0,1 ) ); mc->setBackAmbient( Color4f( 0.1,0.1,0.1,1 ) ); mc->setBackSpecular( Color4f( 0,1,0,1 ) ); mc->setBackShininess( 10 ); mc->setLit(true); endEditCP(mc); mat->addChunk(mc); // Texture chunk UChar8 imgdata[] = { 255,0,0,128, 0,255,0,128, 0,0,255,255, 255,255,255,255 }; ImagePtr pImage = Image::create(); pImage->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata ); if ( argc > 1 ) pImage->read( argv[1] ); TextureChunkPtr xchunk; xchunk = TextureChunk::create(); xchunk->setImage( pImage ); xchunk->setMinFilter( GL_NEAREST ); xchunk->setMagFilter( GL_NEAREST ); xchunk->setWrapS( GL_REPEAT ); xchunk->setWrapT( GL_REPEAT ); xchunk->setEnvMode( GL_MODULATE ); mat->addChunk( xchunk ); endEditCP(mat); objects[0] = makePolygon(ccwSquare, sizeof(ccwSquare)/sizeof(double[3])); objects[1] = makePolygon(ccwSquare, sizeof(ccwSquare)/sizeof(double[3])); objects[2] = makePolygon(star, sizeof(star)/sizeof(double[3])); objects[3] = makePolygon(star, sizeof(star)/sizeof(double[3])); objects[4] = makePolygon(cwSquare, sizeof(cwSquare)/sizeof(double[3])); objects[5] = makePolygon(cwSquare, sizeof(cwSquare)/sizeof(double[3])); objects[6] = makePolygon(doubleEight, sizeof(doubleEight)/sizeof(double[3])); objects[7] = makePolygon(doubleEight, sizeof(doubleEight)/sizeof(double[3])); //tesselate every second object for(int i = 1; i < nobjects; i+=2) { GeometryPtr::dcast(objects[i]->getCore())->setMaterial( mat ); std::cerr << "Polygon Node: " << std::hex << objects[i] << std::endl; // try to create convex primitives OSG::GeometryPtr pGeo = GeometryPtr::dcast(objects[i]->getCore()); std::cerr << "Tesselating polygon : " << i << std::endl; createConvexPrimitives(pGeo); } // normal material SimpleMaterialPtr nmat; nmat = SimpleMaterial::create(); beginEditCP(nmat); nmat->setEmission( Color3f( 0,1,0 ) ); endEditCP(nmat); for ( UInt16 i = 0; i < nobjects; i++ ) { normalobjects[i] = calcVertexNormalsGeo( GeometryPtr::dcast(objects[i]->getCore()), .5); GeometryPtr::dcast(normalobjects[i]->getCore())->setMaterial(nmat); } // // The action win = GLUTWindow::create(); win->setId(id); win->init(); glEnable( GL_LIGHT0 ); float p[4]={0,0,1,0}; glLightfv(GL_LIGHT0, GL_POSITION, p); float c[4]={1,1,1,1}; glLightfv(GL_LIGHT0, GL_DIFFUSE, c); glLightfv(GL_LIGHT0, GL_SPECULAR, c); glPointSize( 3 ); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glClearColor( .3, .3, .8, 1 ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, 1, 0.1, 10 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 3, 3, 3, 0, 0, 0, 0, 0, 1 ); dact = DrawAction::create(); dact->setWindow(get_pointer(win)); dact->setFrustumCulling( false ); glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG GLUTWindowPtr gwin= GLUTWindow::create(); gwin->setId(winid); gwin->init(); // The scene group NodePtr scene = Node::create(); GroupPtr g = Group::create(); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); scene->setCore(g); if(argc < 2) { FWARNING(("No file given!\n")); FWARNING(("Supported file formats:\n")); std::list<const char*> suffixes; SceneFileHandler::the().getSuffixList(suffixes); for(std::list<const char*>::iterator it = suffixes.begin(); it != suffixes.end(); ++it) { FWARNING(("%s\n", *it)); } fileroot = makeTorus(.5, 2, 16, 16); } else { /* All scene file loading is handled via the SceneFileHandler. */ fileroot = SceneFileHandler::the().read(argv[1]); } scene->addChild(fileroot); // Create a small geometry to show the ray and what was hit // Contains a line and a single triangle. // The line shows the ray, the triangle whatever was hit. SimpleMaterialPtr red = SimpleMaterial::create(); beginEditCP(red); { red->setDiffuse (Color3f( 1,0,0 )); red->setTransparency(0.5); red->setLit (false); } endEditCP (red); isectPoints = GeoPositions3f::create(); beginEditCP(isectPoints); { isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); isectPoints->addValue(Pnt3f(0,0,0)); } endEditCP(isectPoints); GeoIndicesUI32Ptr index = GeoIndicesUI32::create(); beginEditCP(index); { index->addValue(0); index->addValue(1); index->addValue(2); index->addValue(3); index->addValue(4); index->addValue(5); index->addValue(6); } endEditCP(index); GeoPLengthsUI32Ptr lens = GeoPLengthsUI32::create(); beginEditCP(lens); { lens->addValue(4); lens->addValue(3); } endEditCP(lens); GeoPTypesUI8Ptr type = GeoPTypesUI8::create(); beginEditCP(type); { type->addValue(GL_LINES); type->addValue(GL_TRIANGLES); } endEditCP(type); testgeocore = Geometry::create(); beginEditCP(testgeocore); { testgeocore->setPositions(isectPoints); testgeocore->setIndices(index); testgeocore->setLengths(lens); testgeocore->setTypes(type); testgeocore->setMaterial(red); } endEditCP(testgeocore); NodePtr testgeo = Node::create(); beginEditCP(testgeo); { testgeo->setCore(testgeocore); } endEditCP(testgeo); scene->addChild(testgeo); endEditCP(scene); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); // show the whole scene mgr->showAll(); mgr->getCamera()->setNear(mgr->getCamera()->getNear() / 10); // Show the bounding volumes? Not for now mgr->getAction()->setVolumeDrawing(false); // GLUT main loop glutMainLoop(); return 0; // GLUT main loop glutMainLoop(); return 0; }
int main (int argc, char **argv) { osgInit(argc,argv); // GLUT init glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); int winid = glutCreateWindow("OpenSG"); glutKeyboardFunc(key); glutVisibilityFunc(vis); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutIdleFunc(display); // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); // OSG SceneFileHandler::the().print(); // create the graph // beacon for camera and light NodePtr b1n = Node::create(); GroupPtr b1 = Group::create(); beginEditCP(b1n); b1n->setCore( b1 ); endEditCP(b1n); // transformation NodePtr t1n = Node::create(); TransformPtr t1 = Transform::create(); beginEditCP(t1n); t1n->setCore( t1 ); t1n->addChild( b1n ); endEditCP(t1n); cam_trans = t1; // light NodePtr dlight = Node::create(); DirectionalLightPtr dl = DirectionalLight::create(); beginEditCP(dlight); dlight->setCore( dl ); endEditCP(dlight); beginEditCP(dl); dl->setAmbient( .3, .3, .3, 1 ); dl->setDiffuse( 1, 1, 1, 1 ); dl->setDirection(0,0,1); dl->setBeacon( b1n); endEditCP(dl); // root root = Node::create(); GroupPtr gr1 = Group::create(); beginEditCP(root); root->setCore( gr1 ); root->addChild( t1n ); root->addChild( dlight ); endEditCP(root); // Load the file NodePtr file = NullFC; NodePtr file1 = NullFC; if ( argc > 1 ) file1 = SceneFileHandler::the().read(argv[1]); if ( file1 == NullFC ) { std::cerr << "Couldn't load file, ignoring" << std::endl; file1 = makeTorus( .5, 2, 16, 16 ); } file1->updateVolume(); // file->dump(); // subRefCP(file); // return 0; Vec3f min,max; file1->getVolume().getBounds( min, max ); std::cout << "Volume: from " << min << " to " << max << std::endl; file = Node::create(); MaterialGroupPtr testMat = MaterialGroup::create(); SimpleMaterialPtr defaultMaterial = SimpleMaterial::create(); beginEditCP(defaultMaterial); defaultMaterial->setDiffuse(Color3f(1,.0,.0)); defaultMaterial->setAmbient(Color3f(0.1,0.1,0.1)); defaultMaterial->setSpecular(Color3f(1,1,1)); defaultMaterial->setShininess(20); endEditCP (defaultMaterial); testMat->setMaterial(defaultMaterial); beginEditCP(file); { // file->setCore(testMat); file->setCore(Group::create()); file->addChild(file1); } endEditCP (file); scene_trans = Transform::create(); NodePtr sceneTrN = Node::create(); beginEditCP(sceneTrN); { sceneTrN->setCore(scene_trans); sceneTrN->addChild(file); sceneTrN->addChild(makeTorus( .5, 2, 16, 16 )); } endEditCP(sceneTrN); beginEditCP(dlight); dlight->addChild(sceneTrN); endEditCP(dlight); std::cerr << "Tree: " << std::endl; // root->dump(); // Camera cam = PerspectiveCamera::create(); cam->setBeacon( b1n ); cam->setFov( deg2rad( 90 ) ); cam->setNear( 0.1 ); cam->setFar( 100000 ); // Background SolidBackgroundPtr bkgnd = SolidBackground::create(); beginEditCP(bkgnd, SolidBackground::ColorFieldMask); bkgnd->setColor(Color3f(1,1,1)); endEditCP(bkgnd, SolidBackground::ColorFieldMask); // Viewport vp = Viewport::create(); vp->setCamera( cam ); vp->setBackground( bkgnd ); vp->setRoot( root ); vp->setSize( 0,0, 1,1 ); // Window std::cout << "GLUT winid: " << winid << std::endl; GLUTWindowPtr gwin; GLint glvp[4]; glGetIntegerv( GL_VIEWPORT, glvp ); gwin = GLUTWindow::create(); gwin->setId(winid); gwin->setSize( glvp[2], glvp[3] ); win = gwin; win->addPort( vp ); win->init(); // Action ract = DrawAction::create(); renact = RenderAction::create(); // tball Vec3f pos; pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), min[1] + ((max[1] - min[1]) * 0.5), max[2] + ( max[2] - min[2] ) * 1.5 ); float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6; Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2, min[1] + (max[1] - min[1]) / 2, min[2] + (max[2] - min[2]) / 2); tball.setMode( Trackball::OSGObject ); tball.setStartPosition( pos, true ); tball.setSum( true ); tball.setTranslationMode( Trackball::OSGFree ); tball.setTranslationScale(scale); tball.setRotationCenter(tCenter); // run... glutMainLoop(); return 0; }
// This function creates the classic Utah teapot from 4 NURBS // surfaces: one surface each for the lid, body, handle and spout. // The "classic" representation is using 18 rational Bezier patches, // but in order to show the construction of more complex surfaces // these have been combined into as few NURBS surfaces as possible. NodePtr makeTeapot() { SimpleMaterialPtr teapotmat = SimpleMaterial::create(); beginEditCP(teapotmat); { teapotmat->setDiffuse( Color3f(0.8, 0.8, 0.8)); teapotmat->setAmbient( Color3f(0.2, 0.2, 0.2)); teapotmat->setEmission( Color3f(0.02, 0.02, 0.02) ); teapotmat->setSpecular( Color3f(0.78, 0.78, 0.78) ); teapotmat->setShininess( 128 ); teapotmat->addChunk(g_fb_chunk); } endEditCP(teapotmat); NodePtr teapotroot = makeCoredNode<Group>(); float knots11[11] = {0, 0, 0, 0, 0.5, 0.5, 0.5, 1, 1, 1, 1}; float knots14[14] = {0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3}; float knots_circle[8] = {0, 0, 0, 0.5, 0.5, 1, 1, 1}; float lidcps[35][4] = { { 0.0000, 4.0000, 0.0000, 1.0000}, { 0.0000, 4.0000, 0.8000, 1.0000}, { 0.0000, 3.6000, 0.0000, 1.0000}, { 0.0000, 3.4000, 0.2000, 1.0000}, { 0.0000, 3.2000, 0.4000, 1.0000}, { 0.0000, 3.2000, 1.3000, 1.0000}, { 0.0000, 3.0000, 1.3000, 1.0000}, {-0.0000, 0.0000, 0.0000, 0.0000}, {-0.8000, 0.0000, 0.0000, 0.0000}, {-0.0000, 0.0000, 0.0000, 0.0000}, {-0.2000, 0.0000, 0.0000, 0.0000}, {-0.4000, 0.0000, 0.0000, 0.0000}, {-1.3000, 0.0000, 0.0000, 0.0000}, {-1.3000, 0.0000, 0.0000, 0.0000}, { 0.0000, 4.0000,-0.0000, 1.0000}, { 0.0000, 4.0000,-0.8000, 1.0000}, { 0.0000, 3.6000,-0.0000, 1.0000}, { 0.0000, 3.4000,-0.2000, 1.0000}, { 0.0000, 3.2000,-0.4000, 1.0000}, { 0.0000, 3.2000,-1.3000, 1.0000}, { 0.0000, 3.0000,-1.3000, 1.0000}, { 0.0000, 0.0000, 0.0000, 0.0000}, { 0.8000, 0.0000, 0.0000, 0.0000}, { 0.0000, 0.0000, 0.0000, 0.0000}, { 0.2000, 0.0000, 0.0000, 0.0000}, { 0.4000, 0.0000, 0.0000, 0.0000}, { 1.3000, 0.0000, 0.0000, 0.0000}, { 1.3000, 0.0000, 0.0000, 0.0000}, { 0.0000, 4.0000, 0.0000, 1.0000}, { 0.0000, 4.0000, 0.8000, 1.0000}, { 0.0000, 3.6000, 0.0000, 1.0000}, { 0.0000, 3.4000, 0.2000, 1.0000}, { 0.0000, 3.2000, 0.4000, 1.0000}, { 0.0000, 3.2000, 1.3000, 1.0000}, { 0.0000, 3.0000, 1.3000, 1.0000}, }; NodePtr lid = makeSurface(35, lidcps, 2, 3, 8, knots_circle, 11, knots11, 0.005, teapotmat); float bodycps[50][4] = { { 0.0000, 3.0000, 1.4000, 1.0000}, { 0.0000, 3.1750, 1.3375, 1.0000}, { 0.0000, 3.1750, 1.4375, 1.0000}, { 0.0000, 3.0000, 1.5000, 1.0000}, { 0.0000, 2.3000, 1.7500, 1.0000}, { 0.0000, 1.6000, 2.0000, 1.0000}, { 0.0000, 1.0000, 2.0000, 1.0000}, { 0.0000, 0.4000, 2.0000, 1.0000}, { 0.0000, 0.2000, 1.5000, 1.0000}, { 0.0000, 0.0000, 1.5000, 1.0000}, {-1.4000, 0.0000, 0.0000, 0.0000}, {-1.3375, 0.0000, 0.0000, 0.0000}, {-1.4375, 0.0000, 0.0000, 0.0000}, {-1.5000, 0.0000, 0.0000, 0.0000}, {-1.7500, 0.0000, 0.0000, 0.0000}, {-2.0000, 0.0000, 0.0000, 0.0000}, {-2.0000, 0.0000, 0.0000, 0.0000}, {-2.0000, 0.0000, 0.0000, 0.0000}, {-1.5000, 0.0000, 0.0000, 0.0000}, {-1.5000, 0.0000, 0.0000, 0.0000}, { 0.0000, 3.0000,-1.4000, 1.0000}, { 0.0000, 3.1750,-1.3375, 1.0000}, { 0.0000, 3.1750,-1.4375, 1.0000}, { 0.0000, 3.0000,-1.5000, 1.0000}, { 0.0000, 2.3000,-1.7500, 1.0000}, { 0.0000, 1.6000,-2.0000, 1.0000}, { 0.0000, 1.0000,-2.0000, 1.0000}, { 0.0000, 0.4000,-2.0500, 1.0000}, { 0.0000, 0.2000,-1.5000, 1.0000}, { 0.0000, 0.0000,-1.5000, 1.0000}, { 1.4000, 0.0000, 0.0000, 0.0000}, { 1.3375, 0.0000, 0.0000, 0.0000}, { 1.4375, 0.0000, 0.0000, 0.0000}, { 1.5000, 0.0000, 0.0000, 0.0000}, { 1.7500, 0.0000, 0.0000, 0.0000}, { 2.0000, 0.0000, 0.0000, 0.0000}, { 2.0000, 0.0000, 0.0000, 0.0000}, { 2.0000, 0.0000, 0.0000, 0.0000}, { 1.5000, 0.0000, 0.0000, 0.0000}, { 1.5000, 0.0000, 0.0000, 0.0000}, { 0.0000, 3.0000, 1.4000, 1.0000}, { 0.0000, 3.1750, 1.3375, 1.0000}, { 0.0000, 3.1750, 1.4375, 1.0000}, { 0.0000, 3.0000, 1.5000, 1.0000}, { 0.0000, 2.3000, 1.7500, 1.0000}, { 0.0000, 1.6000, 2.0000, 1.0000}, { 0.0000, 1.0000, 2.0000, 1.0000}, { 0.0000, 0.4000, 2.0000, 1.0000}, { 0.0000, 0.2000, 1.5000, 1.0000}, { 0.0000, 0.0000, 1.5000, 1.0000}, }; NodePtr body = makeSurface(50, bodycps, 2, 3, 8, knots_circle, 14, knots14, 0.005, teapotmat); float handlecps[35][4] = { { 1.5000, 2.8000, 0.0000, 1.0000}, { 2.5000, 2.8000, 0.0000, 1.0000}, { 3.0000, 2.8000, 0.0000, 1.0000}, { 3.0000, 2.2000, 0.0000, 1.0000}, { 3.0000, 1.6000, 0.0000, 1.0000}, { 2.6500, 1.0500, 0.0000, 1.0000}, { 1.9000, 0.6000, 0.0000, 1.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 0.0000, 0.0000, 0.3000, 0.0000}, { 1.6000, 2.5000, 0.0000, 1.0000}, { 2.3000, 2.5000, 0.0000, 1.0000}, { 2.7000, 2.5000, 0.0000, 1.0000}, { 2.7000, 2.2000, 0.0000, 1.0000}, { 2.7000, 1.9000, 0.0000, 1.0000}, { 2.5000, 1.5000, 0.0000, 1.0000}, { 2.0000, 1.0000, 0.0000, 1.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 0.0000, 0.0000,-0.3000, 0.0000}, { 1.5000, 2.8000, 0.0000, 1.0000}, { 2.5000, 2.8000, 0.0000, 1.0000}, { 3.0000, 2.8000, 0.0000, 1.0000}, { 3.0000, 2.2000, 0.0000, 1.0000}, { 3.0000, 1.6000, 0.0000, 1.0000}, { 2.6500, 1.0500, 0.0000, 1.0000}, { 1.9000, 0.6000, 0.0000, 1.0000}, }; NodePtr handle = makeSurface(35, handlecps, 2, 3, 8, knots_circle, 11, knots11, 0.005, teapotmat); float spoutcps[35][4] = { {-1.7000, 0.6000, 0.0000, 1.0000}, {-3.1000, 0.9000, 0.0000, 1.0000}, {-2.4000, 2.6000, 0.0000, 1.0000}, {-3.3000, 3.0000, 0.0000, 1.0000}, {-3.5250, 3.1250, 0.0000, 1.0000}, {-3.4500, 3.1500, 0.0000, 1.0000}, {-3.2000, 3.0000, 0.0000, 1.0000}, { 0.0000, 0.0000, 0.6600, 0.0000}, { 0.0000, 0.0000, 0.6600, 0.0000}, { 0.0000, 0.0000, 0.2500, 0.0000}, { 0.0000, 0.0000, 0.2500, 0.0000}, { 0.0000, 0.0000, 0.2500, 0.0000}, { 0.0000, 0.0000, 0.1500, 0.0000}, { 0.0000, 0.0000, 0.1500, 0.0000}, {-1.7000, 1.7000, 0.0000, 1.0000}, {-2.6000, 1.7000, 0.0000, 1.0000}, {-2.3000, 2.6000, 0.0000, 1.0000}, {-2.7000, 3.0000, 0.0000, 1.0000}, {-2.8000, 3.1000, 0.0000, 1.0000}, {-2.9000, 3.1000, 0.0000, 1.0000}, {-2.8000, 3.0000, 0.0000, 1.0000}, { 0.0000, 0.0000,-0.6600, 0.0000}, { 0.0000, 0.0000,-0.6600, 0.0000}, { 0.0000, 0.0000,-0.2500, 0.0000}, { 0.0000, 0.0000,-0.2500, 0.0000}, { 0.0000, 0.0000,-0.2500, 0.0000}, { 0.0000, 0.0000,-0.1500, 0.0000}, { 0.0000, 0.0000,-0.1500, 0.0000}, {-1.7000, 0.6000, 0.0000, 1.0000}, {-3.1000, 0.9000, 0.0000, 1.0000}, {-2.4000, 2.6000, 0.0000, 1.0000}, {-3.3000, 3.0000, 0.0000, 1.0000}, {-3.5250, 3.1250, 0.0000, 1.0000}, {-3.4500, 3.1500, 0.0000, 1.0000}, {-3.2000, 3.0000, 0.0000, 1.0000}, }; NodePtr spout = makeSurface(35, spoutcps, 2, 3, 8, knots_circle, 11, knots11, 0.005, teapotmat); beginEditCP(teapotroot); teapotroot->addChild(lid); teapotroot->addChild(body); teapotroot->addChild(handle); teapotroot->addChild(spout); endEditCP(teapotroot); return teapotroot; }
NodePtr createRose() { GeometryPtr geoPtr = Geometry::create(); beginEditCP(geoPtr); { GeoPTypesUI8Ptr typesPtr = GeoPTypesUI8::create(); typesPtr->push_back(GL_QUADS); geoPtr->setTypes(typesPtr); GeoPLengthsUI32Ptr lensPtr = GeoPLengthsUI32::create(); lensPtr->push_back(96); geoPtr->setLengths(lensPtr); GeoPositions3fPtr posPtr = GeoPositions3f::create(); // top posPtr->push_back(Vec3f( 0.00f, 1.00f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, 0.10f)); posPtr->push_back(Vec3f( 0.05f, 0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, 1.00f, 0.00f)); posPtr->push_back(Vec3f( 0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.05f, 0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, -0.10f)); posPtr->push_back(Vec3f( 0.00f, 1.00f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, -0.10f)); posPtr->push_back(Vec3f(-0.05f, 0.05f, -0.05f)); posPtr->push_back(Vec3f(-0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, 1.00f, 0.00f)); posPtr->push_back(Vec3f(-0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f(-0.05f, 0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, 0.10f)); // bottom posPtr->push_back(Vec3f( 0.00f, -1.00f, 0.00f)); posPtr->push_back(Vec3f( 0.10f, -0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.05f, -0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, 0.10f)); posPtr->push_back(Vec3f( 0.00f, -1.00f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, 0.10f)); posPtr->push_back(Vec3f(-0.05f, -0.05f, 0.05f)); posPtr->push_back(Vec3f(-0.10f, -0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, -1.00f, 0.00f)); posPtr->push_back(Vec3f(-0.10f, -0.10f, 0.00f)); posPtr->push_back(Vec3f(-0.05f, -0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, -0.10f)); posPtr->push_back(Vec3f( 0.00f, -1.00f, 0.00f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, -0.10f)); posPtr->push_back(Vec3f( 0.05f, -0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.10f, -0.10f, 0.00f)); // left posPtr->push_back(Vec3f(-1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f(-0.10f, -0.10f, 0.00f)); posPtr->push_back(Vec3f(-0.05f, -0.05f, 0.05f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, 0.10f)); posPtr->push_back(Vec3f(-1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, 0.10f)); posPtr->push_back(Vec3f(-0.05f, 0.05f, 0.05f)); posPtr->push_back(Vec3f(-0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f(-1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f(-0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f(-0.05f, 0.05f, -0.05f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f(-1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f(-0.05f, -0.05f, -0.05f)); posPtr->push_back(Vec3f(-0.10f, -0.10f, 0.00f)); // right posPtr->push_back(Vec3f( 1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, 0.10f)); posPtr->push_back(Vec3f( 0.05f, -0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.10f, -0.10f, 0.00f)); posPtr->push_back(Vec3f( 1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f( 0.10f, -0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.05f, -0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f( 1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f( 0.05f, 0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f( 1.00f, 0.00f, 0.00f)); posPtr->push_back(Vec3f( 0.10f, 0.10f, 0.00f)); posPtr->push_back(Vec3f( 0.05f, 0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, 0.10f)); // front posPtr->push_back(Vec3f( 0.00f, 0.00f, 1.00f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, 0.10f)); posPtr->push_back(Vec3f( 0.05f, 0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, 0.10f)); posPtr->push_back(Vec3f( 0.00f, 0.00f, 1.00f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, 0.10f)); posPtr->push_back(Vec3f(-0.05f, 0.05f, 0.05f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, 0.10f)); posPtr->push_back(Vec3f( 0.00f, 0.00f, 1.00f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, 0.10f)); posPtr->push_back(Vec3f(-0.05f, -0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, 0.10f)); posPtr->push_back(Vec3f( 0.00f, 0.00f, 1.00f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, 0.10f)); posPtr->push_back(Vec3f( 0.05f, -0.05f, 0.05f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, 0.10f)); // back posPtr->push_back(Vec3f( 0.00f, 0.00f, -1.00f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f(-0.05f, 0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, -0.10f)); posPtr->push_back(Vec3f( 0.00f, 0.00f, -1.00f)); posPtr->push_back(Vec3f( 0.00f, 0.10f, -0.10f)); posPtr->push_back(Vec3f( 0.05f, 0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f( 0.00f, 0.00f, -1.00f)); posPtr->push_back(Vec3f( 0.10f, 0.00f, -0.10f)); posPtr->push_back(Vec3f( 0.05f, -0.05f, -0.05f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, -0.10f)); posPtr->push_back(Vec3f( 0.00f, 0.00f, -1.00f)); posPtr->push_back(Vec3f( 0.00f, -0.10f, -0.10f)); posPtr->push_back(Vec3f(-0.05f, -0.05f, -0.05f)); posPtr->push_back(Vec3f(-0.10f, 0.00f, -0.10f)); geoPtr->setPositions(posPtr); SimpleMaterialPtr matPtr = SimpleMaterial::create(); beginEditCP(matPtr); { matPtr->setDiffuse(Color3f(1, 0, 0)); } endEditCP(matPtr); geoPtr->setMaterial(matPtr); } endEditCP(geoPtr); calcFaceNormals(geoPtr); NodePtr nodePtr = Node::create(); beginEditCP(nodePtr, Node::CoreFieldMask); { nodePtr->setCore(geoPtr); } endEditCP(nodePtr, Node::CoreFieldMask); return nodePtr; }
int main(int argc, char **argv) { osgInit(argc,argv); // GLUT init glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); PassiveWindowPtr pwin=PassiveWindow::create(); pwin->init(); // create the scene NodePtr scene; scene = Node::create(); // create the material // Pass 1: Simple red, shiny, depth-tested DepthChunkPtr cl1 = DepthChunk::create(); beginEditCP(cl1); cl1->setEnable(true); cl1->setFunc(GL_LEQUAL); endEditCP(cl1); SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setDiffuse(Color3f(1,0,0)); mat->setSpecular(Color3f(.9,.9,.9)); mat->setShininess(30); mat->setLit(true); mat->addChunk(cl1); endEditCP(mat); // Pass 2: Green unlit, without depth testing DepthChunkPtr cl2 = DepthChunk::create(); beginEditCP(cl2); cl2->setEnable(false); endEditCP(cl2); SimpleMaterialPtr mat2 = SimpleMaterial::create(); beginEditCP(mat2); mat2->setDiffuse(Color3f(0,1,0)); mat2->setLit(false); mat2->setTransparency(.8); mat2->addChunk(cl2); endEditCP(mat2); // Bring them together MultiPassMaterialPtr mpm = MultiPassMaterial::create(); beginEditCP(mpm); mpm->editMFMaterials()->push_back(mat); mpm->editMFMaterials()->push_back(mat2); endEditCP(mpm); GeometryPtr g1 = makeTorusGeo(0.2, 2, 8, 16); beginEditCP(scene); scene->setCore(g1); endEditCP(scene); beginEditCP(g1); g1->setMaterial(mpm); endEditCP(g1); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // create the window and initial camera/viewport mgr->setWindow(pwin ); // tell the manager what to manage mgr->setRoot (scene); // show the whole scene mgr->showAll(); mgr->redraw(); // GLUT main loop glutMainLoop(); return 0; }
int main(int argc, char** argv) { std::cout << "osgInit()..." << std::endl; osgInit( argc, argv ); std::cout << "glutInit()..." << std::endl; glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE ); int winID = glutCreateWindow("OpenSG"); glutKeyboardFunc(key); glutVisibilityFunc(vis); glutReshapeFunc(resize); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutIdleFunc(display); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); glClearColor( .3, .3, .8, 1 ); std::cout << "Creating objects..." << std::endl; //BEACON NodePtr beaconNode = Node::create(); GroupPtr beaconCore = Group::create(); beginEditCP( beaconNode ); beaconNode->setCore( beaconCore ); endEditCP( beaconNode ); /* //LIGHT NodePtr dlightNode = Node::create(); DirectionalLightPtr dlightCore = DirectionalLight::create(); beginEditCP(dlightNode); dlightNode->setCore(dlightCore); endEditCP(dlightNode); beginEditCP(dlightCore); dlightCore->setAmbient( .3, .3, .3, 1 ); dlightCore->setDiffuse( .5, .5, .5, 1 ); dlightCore->setDirection( 0, 0 , 1 ); dlightCore->setBeacon( beaconNode ); endEditCP(dlightCore); */ //TRANSFORM NodePtr transNode = Node::create(); TransformPtr transCore = Transform::create(); beginEditCP( transNode ); transNode->setCore( transCore ); transNode->addChild( beaconNode ); endEditCP( transNode ); cam_trans = transCore; //LOD NodePtr lowDetailNode = makeBox( 1,1,1, 1,1,1 ); GeometryPtr boxCore = GeometryPtr::dcast(lowDetailNode->getCore()); SimpleMaterialPtr lowDetailMat = SimpleMaterial::create(); lowDetailMat->setDiffuse( Color3f( 1,0,0 ) ); lowDetailMat->setAmbient( Color3f( 1,0,0 ) ); boxCore->setMaterial( lowDetailMat ); NodePtr medDetailNode = makeSphere( 1, 1 ); GeometryPtr medSphereCore = GeometryPtr::dcast(medDetailNode->getCore()); SimpleMaterialPtr medDetailMat = SimpleMaterial::create(); medDetailMat->setDiffuse( Color3f( 0,1,0 ) ); medDetailMat->setAmbient( Color3f( 0,1,0 ) ); medSphereCore->setMaterial( medDetailMat ); NodePtr highDetailNode = makeSphere( 2, 1 ); GeometryPtr highSphereCore = GeometryPtr::dcast(highDetailNode->getCore()); SimpleMaterialPtr highDetailMat = SimpleMaterial::create(); highDetailMat->setDiffuse( Color3f( 0,0,1 ) ); highDetailMat->setAmbient( Color3f( 0,0,1 ) ); highDetailMat->setSpecular( Color3f(1, 1, 1) ); highDetailMat->setShininess( 10 ); highSphereCore->setMaterial( highDetailMat ); NodePtr lodNode = Node::create(); DistanceLODPtr lodNodeCore = DistanceLOD::create(); beginEditCP(lodNode); lodNode->setCore(lodNodeCore); lodNode->addChild( highDetailNode ); lodNode->addChild( medDetailNode ); lodNode->addChild( lowDetailNode ); endEditCP(lodNode); beginEditCP(lodNodeCore); lodNodeCore->editSFCenter()->setValue( Pnt3f(0, 0, 2) ); lodNodeCore->editMFRange()->push_back( 4.0 ); lodNodeCore->editMFRange()->push_back( 8.0 ); lodNodeCore->editMFRange()->push_back( 11.0 ); endEditCP(lodNodeCore); //TRANSFORM LOD NodePtr transLODNode = Node::create(); TransformPtr transLODCore = Transform::create(); transMat.setTranslate( 1, -1, -1 ); beginEditCP(transLODCore); transLODCore->editSFMatrix()->setValue( transMat ); endEditCP(transLODCore); beginEditCP( transLODNode ); transLODNode->setCore( transLODCore ); transLODNode->addChild( lodNode ); endEditCP( transLODNode ); //ROOT root = Node::create(); GroupPtr rootCore = Group::create(); beginEditCP(root); root->setCore(rootCore); root->addChild(transNode); root->addChild(transLODNode); endEditCP(root); camera = PerspectiveCamera::create(); beginEditCP(camera); camera->setBeacon( beaconNode ); camera->setFov( deg2rad(90) ); camera->setNear( 0.1 ); camera->setFar( 10000 ); endEditCP(camera); SolidBackgroundPtr background = SolidBackground::create(); viewp = Viewport::create(); beginEditCP(viewp); viewp->setCamera( camera ); viewp->setBackground( background ); viewp->setRoot( root ); viewp->setSize( 0,0, 1,1 ); endEditCP(viewp); GLint glVP[4]; glGetIntegerv( GL_VIEWPORT, glVP ); GLUTWindowPtr gwin = GLUTWindow::create(); gwin->setId(winID); gwin->setSize(glVP[2], glVP[3]); window = gwin; beginEditCP(window); window->addPort( viewp ); window->init(); endEditCP(window); drAct = DrawAction::create(); Vec3f pos( 0, 0, 3 ); tball.setMode( Trackball::OSGObject ); tball.setStartPosition( pos, true ); tball.setSum( true ); tball.setTranslationMode( Trackball::OSGFree ); glutMainLoop(); return 0; }
int main (int argc, char **argv) { osgInit(argc, argv); FieldContainerPtr pProto = Geometry::getClassType().getPrototype(); GeometryPtr pGeoProto = GeometryPtr::dcast(pProto); if(pGeoProto != NullFC) { pGeoProto->setDlistCache(false); } // init GLUT glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("OpenSG"); glutKeyboardFunc(key); // glutReshapeFunc(resize); glutDisplayFunc(display); // glutMouseFunc(mouse); // glutMotionFunc(motion); glutIdleFunc(display); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, 1, 0.1, 10 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 3, 3, 3, 0, 0, 0, 0, 0, 1 ); glDepthFunc( GL_LEQUAL ); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glLineWidth( 3 ); #if 0 // for testing only: register Node type functions IntersectAction::registerEnterDefault(Group::getStaticType(), osgFunctionFunctor2(groupEnter)); IntersectAction::registerEnterDefault(Geometry::getStaticType(), osgFunctionFunctor2(geometryEnter)); IntersectAction::registerEnterDefault(Transform::getStaticType(), osgFunctionFunctor2(transformEnter)); IntersectAction::registerLeaveDefault(Transform::getStaticType(), osgFunctionFunctor2(transformLeave)); #endif // create test scene SimpleMaterialPtr white = SimpleMaterial::create(); SimpleMaterialPtr red = SimpleMaterial::create(); white->setEmission( Color3f( 1,1,1 ) ); red->setEmission( Color3f( 1,0,0 ) ); // g1->(g2->g3->p1,t1->p2) GeometryPtr g; NodePtr p1 = makePlane( 2,2,2,2 ); g = GeometryPtr::dcast(p1->getCore()); g->setMaterial( white ); p1->updateVolume(); NodePtr p2 = makePlane( 2,2,2,2 ); g = GeometryPtr::dcast(p2->getCore()); g->setMaterial( white ); p2->updateVolume(); NodePtr g4 = Node::create(); TransformPtr t1 = Transform::create(); beginEditCP(t1); t1->editMatrix().setRotate( Quaternion( Vec3f(1,0,0), 30 ) ); endEditCP(t1); beginEditCP(g4); g4->setCore( t1 ); g4->addChild( p2 ); g4->updateVolume(); endEditCP(g4); NodePtr g3 = Node::create(); GroupPtr g3c = Group::create(); beginEditCP(g3); g3->setCore( g3c ); g3->addChild( p1 ); g3->updateVolume(); endEditCP(g3); NodePtr g2 = Node::create(); GroupPtr g2c = Group::create(); beginEditCP(g2); g2->setCore( g2c ); g2->addChild( g3 ); g2->updateVolume(); endEditCP(g2); iroot = Node::create(); GroupPtr g1c = Group::create(); beginEditCP(iroot); iroot->setCore( g1c ); iroot->addChild( g2 ); iroot->addChild( g4 ); iroot->updateVolume(); endEditCP(iroot); // make the root and test objects points = GeoPositions3f::create(); beginEditCP(points); points->addValue( Pnt3f(0,0,0) ); points->addValue( Pnt3f(0,0,0) ); points->addValue( Pnt3f(0,0,0) ); points->addValue( Pnt3f(0,0,0) ); points->addValue( Pnt3f(0,0,0) ); endEditCP(points); GeoIndicesUI32Ptr index = GeoIndicesUI32::create(); beginEditCP(index); index->addValue( 0 ); index->addValue( 1 ); index->addValue( 2 ); index->addValue( 3 ); index->addValue( 4 ); endEditCP(index); GeoPLengthsPtr lens = GeoPLengthsUI32::create(); beginEditCP(lens); lens->addValue( 2 ); lens->addValue( 3 ); endEditCP(lens); GeoPTypesPtr type = GeoPTypesUI8::create(); beginEditCP(type); type->addValue( GL_LINES ); type->addValue( GL_TRIANGLES ); endEditCP(type); GeometryPtr testgeocore = Geometry::create(); beginEditCP(testgeocore); testgeocore->setPositions( points ); testgeocore->setIndices( index ); testgeocore->setLengths( lens ); testgeocore->setTypes( type ); testgeocore->setMaterial( red ); endEditCP( testgeocore ); NodePtr testgeo = Node::create(); beginEditCP(testgeo); testgeo->setCore( testgeocore ); endEditCP( testgeo ); root = Node::create(); GroupPtr rootc = Group::create(); beginEditCP(root); root->setCore( rootc ); root->addChild( iroot ); root->addChild( testgeo ); endEditCP( root ); dact = DrawAction::create(); dact->setFrustumCulling(false); glutMainLoop(); return 0; }
void CharacterModel::convertMaterials(std::string configfile) { getMaterials().clear(); UInt32 mcnt = 0; PathHandler ph; ph.setBaseFile(configfile.c_str()); for(int mid = 0; mid < _coreModel->getCoreMaterialCount(); mid++) { CalCoreMaterial *coremat = _coreModel->getCoreMaterial(mid); SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); CalCoreMaterial::Color &calamb = coremat->getAmbientColor(); CalCoreMaterial::Color &caldif = coremat->getDiffuseColor(); CalCoreMaterial::Color &calspec = coremat->getSpecularColor(); mat->setAmbient(Color3f(calamb.red / 255.0f, calamb.green / 255.0f, calamb.blue / 255.0f)); mat->setDiffuse(Color3f(caldif.red / 255.0f, caldif.green / 255.0f, caldif.blue / 255.0f)); mat->setSpecular(Color3f(calspec.red / 255.0f, calspec.green / 255.0f, calspec.blue / 255.0f)); mat->setShininess(coremat->getShininess() * 100.f); mat->setLit(true); mat->setColorMaterial(GL_NONE); for(int mapId = 0; mapId < coremat->getMapCount(); mapId++) { std::string file = coremat->getMapFilename(mapId); std::string pfile = ph.findFile(file.c_str()); SINFO << "Loading texture '" << pfile << "'..." << endLog; ImagePtr img = Image::create(); if(!img->read(pfile.c_str())) { SWARNING << "CharacterModel::convertMaterials: error " << "loading image " << file << endLog; } else { // amz with my test scene paladin.cfg all textures were // upside down so I disabled the vertical flipping perhaps // they fixed the bug in Cal3D? #if 0 beginEditCP(img); { // For some reason Cal3D expects textures upside down ??? UInt32 bpl = img->getBpp() * img->getWidth(); UChar8 *t = img->getData(), *b = t + (img->getHeight() - 1) * bpl, dum; for(UInt32 y = img->getHeight() / 2; y > 0; --y) { for(UInt32 x = bpl; x > 0; --x, ++t, ++b) { dum = *t; *t = *b; *b = dum; } b -= bpl * 2; } } endEditCP(img); #endif TextureChunkPtr tex = TextureChunk::create(); beginEditCP(tex); tex->setImage(img); tex->setEnvMode(GL_MODULATE); endEditCP(tex); mat->addChunk(tex); } } endEditCP(mat); coremat->setUserData((Cal::UserData)mcnt); getMaterials().push_back(mat); mcnt ++; } }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG GLUTWindowPtr gwin= GLUTWindow::create(); gwin->setId(winid); gwin->init(); // Create the Scene NodePtr scene = Node::create(); GroupPtr grcore = Group::create(); beginEditCP(scene); scene->setCore(grcore); // Add a Torus for background scene->addChild(makeTorus(.2, 2, 16, 16)); // The actual creation of the functors is a little complicated. // Mainly because the braindead M$ compiler has problems with, you have to // explicitly create the functor for the right combination of return value // type, number of arguments and argument types (values, pointers, // references, etc.) and whether it is a function, a static method or an // instance method. // Given that the signatures of the draw and volumeUpdate functions are // fixed, the three variants shown here should cover all the bases // Add the DrawFunctor node for standard functions NodePtr df = Node::create(); DrawFunctorCorePtr func = DrawFunctorCore::create(); beginEditCP(func); func->setMaterial(getDefaultUnlitMaterial()); func->setDraw( osgTypedFunctionFunctor1Ptr< Action::ResultE, DrawActionBase >(draw)); func->setVolumeUpdate( osgTypedFunctionVoidFunctor1Ptr< Volume >(volUpdate)); endEditCP(func); beginEditCP(df); df->setCore(func); endEditCP(df); scene->addChild(df); // Some transparent material // Even though the DrawFunctorCore can do any kind of OpenGL functions, the // Material of the Core is used to decide whether the node should be sorted // and rendered last SimpleMaterialPtr transmat = SimpleMaterial::create(); beginEditCP(transmat); transmat->setTransparency(0.1); // The actual value is overriden by the draw // anyway, but it has to be != 0 to be // considered transparent transmat->setLit(false); endEditCP(transmat); // Add the DrawFunctor node for static methods of a class // Static methods are pretty much the same as functions NodePtr dfsm = Node::create(); DrawFunctorCorePtr funcsm = DrawFunctorCore::create(); beginEditCP(funcsm); funcsm->setMaterial(transmat); funcsm->setDraw( osgTypedFunctionFunctor1Ptr< Action::ResultE, DrawActionBase >( &SWrapper::draw)); funcsm->setVolumeUpdate( osgTypedFunctionVoidFunctor1Ptr< Volume >(&SWrapper::volUpdate)); endEditCP(funcsm); beginEditCP(dfsm); dfsm->setCore(funcsm); endEditCP(dfsm); scene->addChild(dfsm); // Add the DrawFunctor node for methods // The functor creation functions need the type of the object for which // methods should be called. Wrapper wrap(Color4f(0,1,0,.3)); NodePtr dfm = Node::create(); DrawFunctorCorePtr funcm = DrawFunctorCore::create(); beginEditCP(funcm); funcm->setMaterial(transmat); funcm->setDraw( osgTypedMethodFunctor1ObjPtr< Action::ResultE, Wrapper, DrawActionBase >( &wrap, &Wrapper::draw)); funcm->setVolumeUpdate( osgTypedMethodVoidFunctor1ObjPtr< Wrapper, Volume >( &wrap, &Wrapper::volUpdate)); endEditCP(funcm); beginEditCP(dfm); dfm->setCore(funcm); endEditCP(dfm); scene->addChild(dfm); // Add a Sphere, just for fun scene->addChild(makeLatLongSphere(16,16,.4)); endEditCP(scene); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }
NodePtr createScenegraph(){ //At first we load all needed models from file NodePtr w_high = SceneFileHandler::the().read("data/woman_high.wrl"); NodePtr w_medium = SceneFileHandler::the().read("data/woman_medium.wrl"); NodePtr w_low = SceneFileHandler::the().read("data/woman_low.wrl"); //we check the result if ((w_high == NullFC)&&(w_medium == NullFC)&&(w_low == NullFC)){ std::cout << "It was not possible to load all needed models from file" << std::endl; return NullFC; } //now the LOD core DistanceLODPtr lod = DistanceLOD::create(); beginEditCP(lod, DistanceLOD::CenterFieldMask | DistanceLOD::RangeFieldMask); lod->getSFCenter()->setValue(Pnt3f(0,0,0)); lod->getMFRange()->push_back(200); lod->getMFRange()->push_back(500); endEditCP(lod, DistanceLOD::CenterFieldMask | DistanceLOD::RangeFieldMask); //the node containing the LOD core. The three models will be //added as its children NodePtr lodNode = Node::create(); beginEditCP(lodNode); lodNode->setCore(lod); lodNode->addChild(w_high); lodNode->addChild(w_medium); lodNode->addChild(w_low); endEditCP(lodNode); //create the node with switch core ******************** SwitchPtr sw = Switch::create(); beginEditCP(sw, Switch::ChoiceFieldMask); //Notice: the first choice is 0 sw->setChoice(0); endEditCP(sw, Switch::ChoiceFieldMask); NodePtr switchNode = Node::create(); beginEditCP(switchNode); switchNode->setCore(sw); switchNode->addChild(lodNode); endEditCP(switchNode); //end witch creation ********************************** NodePtr root = Node::create(); beginEditCP(root); root->setCore(Group::create()); root->addChild(switchNode); endEditCP(root); // we know want to extract the mesh geometry out of the graph // it is sufficent to pass the model only as root for searching NodePtr womanGeometry = checkName(w_high); GeometryPtr geo; if (womanGeometry != NullFC){ geo = GeometryPtr::dcast(womanGeometry->getCore()); if (geo == NullFC) std::cout << "Casting failed!" << std::endl; }else{ std::cout << "No correct geometry node found!" << std::endl; //create a dummy object geo = makeBoxGeo(0.5,0.5,0.5,1,1,1); } // generating a material ********************************* SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setAmbient(Color3f(0.2,0.2,0.2)); mat->setDiffuse(Color3f(0.6,0.3,0.1)); mat->setSpecular(Color3f(1,1,1)); mat->setShininess(0.8); endEditCP(mat); beginEditCP(geo, Geometry::MaterialFieldMask); geo->setMaterial(mat); endEditCP(geo, Geometry::MaterialFieldMask); // end material generation ******************************* //new node with "old" geometry core referenced NodePtr woman = Node::create(); beginEditCP(woman); woman->setCore(geo); endEditCP(woman); //translate it a bit to see both women NodePtr womanTrans = Node::create(); TransformPtr t = Transform::create(); beginEditCP(t); Matrix m; m.setIdentity(); m.setTranslate(Vec3f(0,0,200)); t->setMatrix(m); endEditCP(t); beginEditCP(womanTrans); womanTrans->setCore(t); womanTrans->addChild(woman); endEditCP(womanTrans); //add it to the root beginEditCP(root); root->addChild(womanTrans); endEditCP(root); return root; }
NodePtr createScoreBoards() { GeometryPtr geo; NodePtr bg; SimpleMaterialPtr m; ScoreBoard1 = new TextStuff() ; ScoreBoard2 = new TextStuff() ; // First get the global group node OSG::NodePtr scoreBoardsNodePtr = OSG::Node::create(); scoreBoardsNodePtr->setCore(OSG::Group::create()); // Setup text 1 ScoreBoard1->initialize(); ScoreBoard1->updateFace(); ScoreBoard1->updateScore(0,0); // Setup text 2 ScoreBoard2->initialize(); ScoreBoard2->updateFace(); ScoreBoard2->updateScore(0,0); ////////// 1 ///////// // make its transform TransformPtr trans1; NodePtr trans_node1 = makeCoredNode<Transform>(&trans1); beginEditCP(trans1); trans1->getMatrix().setTransform(Vec3f(0,4,-10.5),Quaternion( Vec3f(0,1,0),deg2rad(0))); endEditCP(trans1); // make geometry bg = makePlane(9.3, 1, 8,2); m= SimpleMaterial::create(); beginEditCP(m); { m->setAmbient (Color3f(0,0,0)); m->setDiffuse (Color3f(0.0,0.0,0.0)); } endEditCP (m); geo = GeometryPtr::dcast(bg->getCore()); beginEditCP(geo); geo->setMaterial(m); beginEditCP(geo); beginEditCP(bg); bg->addChild(ScoreBoard1->mRootNode); endEditCP(bg); beginEditCP(trans_node1); trans_node1->addChild(bg); endEditCP(trans_node1); ////////// 2 ///////// // make its transform TransformPtr trans2; NodePtr trans_node2 = makeCoredNode<Transform>(&trans2); beginEditCP(trans2); trans2->getMatrix().setTransform(Vec3f(0,4,10.5),Quaternion( Vec3f(0,1,0),deg2rad(180))); endEditCP(trans2); // make geometry bg = makePlane(9.3, 1, 8,2); m = SimpleMaterial::create(); beginEditCP(m); { m->setAmbient (Color3f(0,0,0)); m->setDiffuse (Color3f(0.0,0.0,0.0)); } endEditCP (m); geo = GeometryPtr::dcast(bg->getCore()); beginEditCP(geo); geo->setMaterial(m); beginEditCP(geo); beginEditCP(bg); bg->addChild(ScoreBoard2->mRootNode); endEditCP(bg); beginEditCP(trans_node2); trans_node2->addChild(bg); endEditCP(trans_node2); beginEditCP(scoreBoardsNodePtr); scoreBoardsNodePtr->addChild(trans_node1); scoreBoardsNodePtr->addChild(trans_node2); endEditCP(scoreBoardsNodePtr); return scoreBoardsNodePtr; }