void updateFace() { // Try to create new face //TextTXFFace *newFace = TextTXFFace::createFromFile("./haeberli.txf"); TextTXFFace *newFace = TextTXFFace::create(family, style); if (newFace == 0) return; subRefP(face); face = newFace; addRefP(face); // Update information on the screen family = face->getFamily(); statfg->editCollector().getElem(familyDesc)->set(family); filename = family; string::size_type i; for (i = 0; i < filename.size(); ) if (isalnum(filename[i]) == false) filename.erase(i, 1); else ++i; style = face->getStyle(); StatStringElem *statElem = statfg->editCollector().getElem(styleDesc); switch (style) { case TextFace::STYLE_PLAIN: statElem->set("Plain"); filename.append("-Plain.txf"); break; case TextFace::STYLE_BOLD: statElem->set("Bold"); filename.append("-Bold.txf"); break; case TextFace::STYLE_ITALIC: statElem->set("Italic"); filename.append("-Italic.txf"); break; case TextFace::STYLE_BOLDITALIC: statElem->set("Bold & Italic"); filename.append("-BoldItalic.txf"); break; } glutSetMenu(mainMenuID); glutChangeToMenuEntry(8, (string("Write to ") + filename).c_str(), COMMAND_WRITE_TO_FILE); }
// 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(); lines.push_back(argc >= 2 ? argv[1] : "Hello World!"); lines.push_back("Powered by OpenSG"); lines.push_back("3rd line"); layoutParam.spacing = 1.5f; //layoutParam.length.push_back(10.f); //layoutParam.length.push_back(7.f); //layoutParam.length.push_back(-1.f); // put the geometry core into a node scene = Node::create(); GroupPtr groupPtr = Group::create(); beginEditCP(scene, Node::CoreFieldMask); { scene->setCore(groupPtr); } endEditCP(scene, Node::CoreFieldMask); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); statfg = SimpleStatisticsForeground::create(); beginEditCP(statfg); statfg->setSize(25); statfg->setColor(Color4f(0,1,0,0.9)); statfg->addElement(familyDesc, "Family: %s"); statfg->addElement(styleDesc, "Style: %s"); statfg->addElement(majorAlignDesc, "Major Alignment: %s"); statfg->addElement(minorAlignDesc, "Minor Alignment: %s"); statfg->addElement(dirDesc, "%s"); statfg->addElement(horiDirDesc, "%s"); statfg->addElement(vertDirDesc, "%s"); endEditCP(statfg); // Create the background SolidBackgroundPtr bg = SolidBackground::create(); beginEditCP(bg); bg->setColor(Color3f(0.1, 0.1, 0.5)); endEditCP(bg); updateFace(); updateScene(); // add the statistics forground beginEditCP(gwin->getPort(0)); gwin->editPort(0)->editMFForegrounds()->push_back(statfg); gwin->editPort(0)->setBackground(bg); endEditCP(gwin->getPort(0)); // GLUT main loop glutMainLoop(); return 0; }
void updateScene() { statfg->editCollector().getElem(majorAlignDesc)->set(alignmentToString(layoutParam.majorAlignment)); statfg->editCollector().getElem(minorAlignDesc)->set(alignmentToString(layoutParam.minorAlignment)); statfg->editCollector().getElem(dirDesc)->set(layoutParam.horizontal ? "Horizontal" : "Vertical"); statfg->editCollector().getElem(horiDirDesc)->set(layoutParam.leftToRight ? "Left to right" : "Right to left"); statfg->editCollector().getElem(vertDirDesc)->set(layoutParam.topToBottom ? "Top to bottom" : "Bottom to top"); if(face == NULL) return; // Put it all together into a Geometry NodeCore. TextLayoutResult layoutResult; Real32 scale = 2.f; face->layout(lines, layoutParam, layoutResult); #if 0 GeometryPtr geo = Geometry::create(); face->fillGeo(geo, layoutResult, scale); NodePtr textNode = Node::create(); beginEditCP(textNode, Node::CoreFieldMask); { textNode->setCore(geo); } endEditCP(textNode, Node::CoreFieldMask); #else NodePtr textNode = face->makeNode(layoutResult, scale); GeometryPtr geo = GeometryPtr::dcast(textNode->getCore()); #endif NodePtr transNodePtr = Node::create(); TransformPtr transPtr = Transform::create(); Matrix transMatrix; transMatrix.setTranslate(0.f, 0.f, -0.03f); beginEditCP(transPtr); { transPtr->setMatrix(transMatrix); } endEditCP(transPtr); beginEditCP(transNodePtr, Node::CoreFieldMask | Node::ChildrenFieldMask); { transNodePtr->setCore(transPtr); transNodePtr->addChild(textNode); } endEditCP(transNodePtr, Node::CoreFieldMask | Node::ChildrenFieldMask); ImagePtr imagePtr = face->getTexture(); TextureChunkPtr texChunk = TextureChunk::create(); beginEditCP(texChunk); { texChunk->setImage(imagePtr); texChunk->setWrapS(GL_CLAMP); texChunk->setWrapT(GL_CLAMP); texChunk->setMagFilter(GL_NEAREST); texChunk->setMinFilter(GL_NEAREST); texChunk->setEnvMode(GL_MODULATE); } endEditCP(texChunk); MaterialChunkPtr matChunk = MaterialChunk::create(); beginEditCP(matChunk); { matChunk->setAmbient(Color4f(1.f, 1.f, 1.f, 1.f)); matChunk->setDiffuse(Color4f(1.f, 1.f, 1.f, 1.f)); matChunk->setEmission(Color4f(0.f, 0.f, 0.f, 1.f)); matChunk->setSpecular(Color4f(0.f, 0.f, 0.f, 1.f)); matChunk->setShininess(0); } endEditCP(matChunk); BlendChunkPtr blendChunk = BlendChunk::create(); beginEditCP(blendChunk); { blendChunk->setSrcFactor(GL_SRC_ALPHA); blendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); } endEditCP(blendChunk); ChunkMaterialPtr m = ChunkMaterial::create(); beginEditCP(m); { m->addChunk(texChunk); m->addChunk(matChunk); m->addChunk(blendChunk); } endEditCP(m); beginEditCP(geo, Geometry::MaterialFieldMask); { geo->setMaterial(m); } endEditCP(geo, Geometry::MaterialFieldMask); beginEditCP(scene, Node::ChildrenFieldMask); { scene->editMFChildren()->clear(); scene->addChild(createCoordinateCross()); scene->addChild(createMetrics(face, scale, layoutParam, layoutResult)); scene->addChild(transNodePtr); } endEditCP(scene, Node::ChildrenFieldMask); mgr->showAll(); glutPostRedisplay(); }
// 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(); // load the scene 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)); } std::vector<std::string> suffixesVec; suffixesVec = FCFileHandler::the()->getSuffixList(); for(std::vector<std::string>::iterator it = suffixesVec.begin(); it != suffixesVec.end(); ++it) { FWARNING(("%s\n", *it)); } RootNodes.push_back( makeTorus(.5, 2, 16, 16) ); glutSetWindowTitle("No file Loaded"); } else { glutSetWindowTitle(argv[1]); Load(std::string(argv[1]), RootNodes, Cameras); if(RootNodes.size() < 1) { std::cout << "There are no root nodes defined." << std::endl; return 0; } } //Create Statistics Foreground SimpleStatisticsForegroundPtr TheStatForeground = SimpleStatisticsForeground::create(); beginEditCP(TheStatForeground); TheStatForeground->setSize(25); TheStatForeground->setColor(Color4f(0,1,0,0.7)); TheStatForeground->addElement(RenderAction::statDrawTime, "Draw FPS: %r.3f"); TheStatForeground->addElement(DrawActionBase::statTravTime, "TravTime: %.3f s"); TheStatForeground->addElement(RenderAction::statDrawTime, "DrawTime: %.3f s"); TheStatForeground->addElement(DrawActionBase::statCullTestedNodes, "%d Nodes culltested"); TheStatForeground->addElement(DrawActionBase::statCulledNodes, "%d Nodes culled"); TheStatForeground->addElement(RenderAction::statNMaterials, "%d material changes"); TheStatForeground->addElement(RenderAction::statNMatrices, "%d matrix changes"); TheStatForeground->addElement(RenderAction::statNGeometries, "%d Nodes drawn"); TheStatForeground->addElement(RenderAction::statNTransGeometries, "%d transparent Nodes drawn"); TheStatForeground->addElement(Drawable::statNTriangles, "%d triangles drawn"); TheStatForeground->addElement(Drawable::statNLines, "%d lines drawn"); TheStatForeground->addElement(Drawable::statNPoints, "%d points drawn"); TheStatForeground->addElement(Drawable::statNPrimitives, "%d primitive groups drawn"); TheStatForeground->addElement(Drawable::statNVertices, "%d vertices transformed"); TheStatForeground->addElement(Drawable::statNGeoBytes, "%d bytes of geometry used"); TheStatForeground->addElement(RenderAction::statNTextures, "%d textures used"); TheStatForeground->addElement(RenderAction::statNTexBytes, "%d bytes of texture used"); endEditCP(TheStatForeground); //Set up Selection SelectedRootNode = 0; SelectedCamera = -1; // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (RootNodes[SelectedRootNode]); mgr->turnHeadlightOff(); beginEditCP(mgr->getWindow()->getPort(0), Viewport::ForegroundsFieldMask); mgr->getWindow()->getPort(0)->getForegrounds().push_back(TheStatForeground); endEditCP(mgr->getWindow()->getPort(0), Viewport::ForegroundsFieldMask); StatCollector *collector = &TheStatForeground->getCollector(); // add optional elements collector->getElem(Drawable::statNTriangles); mgr->getAction()->setStatistics(collector); // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the rootNode int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindowEventProducer = createDefaultWindowEventProducer(); WindowPtr MainWindow = TutorialWindowEventProducer->initWindow(); TutorialWindowEventProducer->setDisplayCallback(display); TutorialWindowEventProducer->setReshapeCallback(reshape); TutorialKeyListener TheKeyListener; TutorialWindowEventProducer->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindowEventProducer->addMouseListener(&TheTutorialMouseListener); TutorialWindowEventProducer->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindowEventProducer->getWindow()); //Make Torus Node TriGeometryBase = makeTorus(.55, 1.5, 16, 16); //Make Main Scene Node NodePtr scene = makeCoredNode<Group>(); setName(scene, "scene"); rootNode = Node::create(); setName(rootNode, "rootNode"); ComponentTransformPtr Trans; Trans = ComponentTransform::create(); beginEditCP(rootNode, Node::CoreFieldMask | Node::ChildrenFieldMask); rootNode->setCore(Trans); // add the torus as a child rootNode->addChild(scene); endEditCP (rootNode, Node::CoreFieldMask | Node::ChildrenFieldMask); //Setup Physics Scene physicsWorld = PhysicsWorld::create(); beginEditCP(physicsWorld, PhysicsWorld::WorldContactSurfaceLayerFieldMask | PhysicsWorld::AutoDisableFlagFieldMask | PhysicsWorld::AutoDisableTimeFieldMask | PhysicsWorld::WorldContactMaxCorrectingVelFieldMask | PhysicsWorld::GravityFieldMask); physicsWorld->setWorldContactSurfaceLayer(0.005); physicsWorld->setAutoDisableFlag(1); physicsWorld->setAutoDisableTime(0.75); physicsWorld->setWorldContactMaxCorrectingVel(100.0); physicsWorld->setGravity(Vec3f(0.0, 0.0, -9.81)); endEditCP(physicsWorld, PhysicsWorld::WorldContactSurfaceLayerFieldMask | PhysicsWorld::AutoDisableFlagFieldMask | PhysicsWorld::AutoDisableTimeFieldMask | PhysicsWorld::WorldContactMaxCorrectingVelFieldMask | PhysicsWorld::GravityFieldMask); //Create the Collision Space physicsSpace = PhysicsHashSpace::create(); //Setup the default collision parameters CollisionContactParametersPtr DefaultCollisionParams = CollisionContactParameters::createEmpty(); beginEditCP(DefaultCollisionParams); DefaultCollisionParams->setMode(dContactApprox1); DefaultCollisionParams->setMu(0.3); DefaultCollisionParams->setMu2(0.0); DefaultCollisionParams->setBounce(0.0); DefaultCollisionParams->setBounceSpeedThreshold(0.0); DefaultCollisionParams->setSoftCFM(0.1); DefaultCollisionParams->setSoftERP(0.2); DefaultCollisionParams->setMotion1(0.0); DefaultCollisionParams->setMotion2(0.0); DefaultCollisionParams->setMotionN(0.0); DefaultCollisionParams->setSlip1(0.0); DefaultCollisionParams->setSlip2(0.0); endEditCP(DefaultCollisionParams); beginEditCP(physicsSpace, PhysicsSpace::DefaultCollisionParametersFieldMask); physicsSpace->setDefaultCollisionParameters(DefaultCollisionParams); endEditCP(physicsSpace, PhysicsSpace::DefaultCollisionParametersFieldMask); //Bouncy Sphere collision parameters CollisionContactParametersPtr BouncySphereCollisionParams = CollisionContactParameters::createEmpty(); beginEditCP(BouncySphereCollisionParams); BouncySphereCollisionParams->setMode(dContactApprox1 | dContactBounce); BouncySphereCollisionParams->setMu(0.3); BouncySphereCollisionParams->setMu2(0.0); BouncySphereCollisionParams->setBounce(0.8); BouncySphereCollisionParams->setBounceSpeedThreshold(0.1); BouncySphereCollisionParams->setSoftCFM(0.1); BouncySphereCollisionParams->setSoftERP(0.2); BouncySphereCollisionParams->setMotion1(0.0); BouncySphereCollisionParams->setMotion2(0.0); BouncySphereCollisionParams->setMotionN(0.0); BouncySphereCollisionParams->setSlip1(0.0); BouncySphereCollisionParams->setSlip2(0.0); endEditCP(BouncySphereCollisionParams); physicsSpace->addCollisionContactCategory(SphereCategory, TerrainCategory, BouncySphereCollisionParams); physicsSpace->addCollisionContactCategory(SphereCategory, BoxCategory, BouncySphereCollisionParams); physicsSpace->addCollisionContactCategory(SphereCategory, SphereCategory, BouncySphereCollisionParams); physicsSpace->addCollisionContactCategory(SphereCategory, TriCategory, BouncySphereCollisionParams); //Soft Box collision parameters CollisionContactParametersPtr SlickBoxParams = CollisionContactParameters::createEmpty(); beginEditCP(SlickBoxParams); SlickBoxParams->setMode(dContactApprox1); SlickBoxParams->setMu(0.01); SlickBoxParams->setMu2(0.0); SlickBoxParams->setBounce(0.0); SlickBoxParams->setBounceSpeedThreshold(0.0); SlickBoxParams->setSoftCFM(0.0); SlickBoxParams->setSoftERP(0.2); SlickBoxParams->setMotion1(0.0); SlickBoxParams->setMotion2(0.0); SlickBoxParams->setMotionN(0.0); SlickBoxParams->setSlip1(0.0); SlickBoxParams->setSlip2(0.0); endEditCP(SlickBoxParams); physicsSpace->addCollisionContactCategory(BoxCategory, TerrainCategory, SlickBoxParams); physicsSpace->addCollisionContactCategory(BoxCategory, BoxCategory, SlickBoxParams); //physicsSpace->addCollisionContactCategory(BoxCategory, SphereCategory, SlickBoxParams); physicsSpace->addCollisionContactCategory(BoxCategory, TriCategory, SlickBoxParams); TutorialCollisionListener BoxColListener(Path("./Data/click.wav")); physicsSpace->addCollisionListener(&BoxColListener,BoxCategory, 2.0); TutorialCollisionListener SphereColListener(Path("./Data/pop.wav")); physicsSpace->addCollisionListener(&SphereColListener,SphereCategory, 2.0); physHandler = PhysicsHandler::create(); beginEditCP(physHandler, PhysicsHandler::WorldFieldMask | PhysicsHandler::SpacesFieldMask | PhysicsHandler::StepSizeFieldMask | PhysicsHandler::UpdateNodeFieldMask); physHandler->setWorld(physicsWorld); physHandler->getSpaces().push_back(physicsSpace); physHandler->setStepSize(0.001); physHandler->setUpdateNode(rootNode); endEditCP(physHandler, PhysicsHandler::WorldFieldMask | PhysicsHandler::SpacesFieldMask | PhysicsHandler::StepSizeFieldMask | PhysicsHandler::UpdateNodeFieldMask); physHandler->attachUpdateProducer(TutorialWindowEventProducer); beginEditCP(rootNode, Node::AttachmentsFieldMask); rootNode->addAttachment(physHandler); rootNode->addAttachment(physicsWorld); rootNode->addAttachment(physicsSpace); endEditCP(rootNode, Node::AttachmentsFieldMask); /************************************************************************/ /* create spaces, geoms and bodys */ /************************************************************************/ //create a group for our space GroupPtr spaceGroup; spaceGroupNode = makeCoredNode<Group>(&spaceGroup); //create the ground plane GeometryPtr plane; NodePtr planeNode = makeBox(30.0, 30.0, 1.0, 1, 1, 1); plane = GeometryPtr::dcast(planeNode->getCore()); //and its Material SimpleMaterialPtr plane_mat = SimpleMaterial::create(); beginEditCP(plane_mat); plane_mat->setAmbient(Color3f(0.7,0.7,0.7)); plane_mat->setDiffuse(Color3f(0.9,0.6,1.0)); endEditCP(plane_mat); beginEditCP(plane, Geometry::MaterialFieldMask); plane->setMaterial(plane_mat); endEditCP(plane); //create Physical Attachments PhysicsBoxGeomPtr planeGeom = PhysicsBoxGeom::create(); beginEditCP(planeGeom, PhysicsBoxGeom::LengthsFieldMask | PhysicsBoxGeom::SpaceFieldMask | PhysicsBoxGeom::CategoryBitsFieldMask); planeGeom->setLengths(Vec3f(30.0, 30.0, 1.0)); //add geoms to space for collision planeGeom->setSpace(physicsSpace); //Set the Geoms Category - this will be used by the collision space //for determining if collision tests should occur //and for selecting the collision contact parameters when a collision does occur planeGeom->setCategoryBits(TerrainCategory); endEditCP(planeGeom, PhysicsBoxGeom::LengthsFieldMask | PhysicsBoxGeom::SpaceFieldMask | PhysicsBoxGeom::CategoryBitsFieldMask); //add Attachments to nodes... beginEditCP(spaceGroupNode, Node::AttachmentsFieldMask | Node::ChildrenFieldMask); spaceGroupNode->addAttachment(physicsSpace); spaceGroupNode->addChild(planeNode); endEditCP(spaceGroupNode, Node::AttachmentsFieldMask | Node::ChildrenFieldMask); beginEditCP(planeNode, Node::AttachmentsFieldMask); planeNode->addAttachment(planeGeom); endEditCP(planeNode, Node::AttachmentsFieldMask); beginEditCP(scene, Node::ChildrenFieldMask); scene->addChild(spaceGroupNode); endEditCP(scene, Node::ChildrenFieldMask); //Create Statistics Foreground SimpleStatisticsForegroundPtr PhysicsStatForeground = SimpleStatisticsForeground::create(); beginEditCP(PhysicsStatForeground); PhysicsStatForeground->setSize(25); PhysicsStatForeground->setColor(Color4f(0,1,0,0.7)); PhysicsStatForeground->addElement(PhysicsHandler::statPhysicsTime, "Physics time: %.3f s"); PhysicsStatForeground->addElement(PhysicsHandler::statCollisionTime, "Collision time: %.3f s"); PhysicsStatForeground->addElement(PhysicsHandler::statSimulationTime, "Simulation time: %.3f s"); PhysicsStatForeground->addElement(PhysicsHandler::statNCollisions, "%d collisions"); PhysicsStatForeground->addElement(PhysicsHandler::statNCollisionTests, "%d collision tests"); PhysicsStatForeground->addElement(PhysicsHandler::statNPhysicsSteps, "%d simulation steps per frame"); endEditCP(PhysicsStatForeground); // tell the manager what to manage mgr->setRoot (rootNode); beginEditCP(mgr->getWindow()->getPort(0), Viewport::ForegroundsFieldMask); mgr->getWindow()->getPort(0)->getForegrounds().push_back(PhysicsStatForeground); endEditCP(mgr->getWindow()->getPort(0), Viewport::ForegroundsFieldMask); physHandler->setStatistics(&PhysicsStatForeground->getCollector()); // show the whole rootNode mgr->showAll(); //Attach the Sound Manager to the update and the camera SoundManager::the()->attachUpdateProducer(TutorialWindowEventProducer); SoundManager::the()->setCamera(mgr->getCamera()); Vec2f WinSize(TutorialWindowEventProducer->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindowEventProducer->getDesktopSize() - WinSize) *0.5); TutorialWindowEventProducer->openWindow(WinPos, WinSize, "21Collisions"); //Enter main Loop TutorialWindowEventProducer->mainLoop(); osgExit(); return 0; }