// 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->setSize( 800, 800 ); gwin->init(); // Create the shader material ChunkMaterialPtr cmat = ChunkMaterial::create(); SHLChunkPtr shl = SHLChunk::create(); beginEditCP(shl); shl->setProgramParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES_ADJACENCY_EXT); shl->setProgramParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLES); // ok we create a maximum of 6 vertices. shl->setProgramParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 6); shl->setVertexProgram(_vertex_shader); shl->setFragmentProgram(_fragment_shader); shl->setGeometryProgram(_geometry_shader); endEditCP(shl); beginEditCP(cmat); cmat->addChunk(shl); endEditCP(cmat); // create root node _scene = Node::create(); // create torus GeometryPtr geo = makeTorusGeo(.8, 1.8, 32, 32); beginEditCP( geo, Geometry::MaterialFieldMask); geo->setMaterial(cmat); endEditCP(geo, Geometry::MaterialFieldMask); NodePtr torus = Node::create(); beginEditCP(torus, Node::CoreFieldMask); torus->setCore(geo); endEditCP(torus, Node::CoreFieldMask); // add torus to scene GroupPtr group = Group::create(); beginEditCP(_scene); _scene->setCore(group); _scene->addChild(torus); 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; }
// // Initialize GLUT & OpenSG and set up the scene // int main(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // open a new scope, because the pointers below should go out of scope // before entering glutMainLoop. // Otherwise OpenSG will complain about objects being alive after shutdown. { // the connection between GLUT and OpenSG OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // create the SimpleSceneManager helper mgr = OSG::SimpleSceneManager::create(); mgr->setWindow(gwin); // create a pretty simple graph: a Group with two Transforms as children, // each of which carries a single Geometry. // The scene OSG::NodeRefPtr scene = OSG::Node::create(); // The cylinder and its transformation OSG::NodeRefPtr cyl = OSG::Node::create(); OSG::GeometryRefPtr cylgeo = OSG::makeCylinderGeo( 1.4f, .3f, 24, true, true, true ); cyl->setCore(cylgeo); cyltrans = OSG::Transform::create(); OSG::NodeRefPtr cyltransnode = OSG::Node::create(); cyltransnode->setCore (cyltrans); cyltransnode->addChild(cyl ); // add it to the scene scene->addChild(cyltransnode); // The torus and its transformation OSG::NodeRefPtr torus = OSG::Node::create(); OSG::GeometryRefPtr torusgeo = OSG::makeTorusGeo( .2f, 1, 24, 36 ); torus->setCore(torusgeo); tortrans = OSG::Transform::create(); OSG::NodeRefPtr tortransnode = OSG::Node::create(); tortransnode->setCore (tortrans); tortransnode->addChild(torus ); // add it to the scene scene->addChild(tortransnode); // // create the shader program // OSG::ShaderProgramChunkRefPtr prog_chunk = OSG::ShaderProgramChunk::create(); OSG::ShaderProgramRefPtr vertShader = OSG::ShaderProgram::createVertexShader(); OSG::ShaderProgramRefPtr fragShader = OSG::ShaderProgram::createFragmentShader(); vertShader->setProgram(get_vp_program()); fragShader->setProgram(get_fp_program()); // // binding the unifrom block to a buffer binding point can be performed // either by calling the shaders's addUniformBlock method or by // adding a 'uniform block' variable to a ShaderProgramVariableChunk. // In the following we use both variants for illustration. // fragShader->addUniformBlock("Materials", 1); // block binding point fragShader->addUniformBlock("Lights", 2); // block binding point // // The following is replaced by adding ShaderProgramVariableChunk objects // to the chunk material. See below... // // fragShader->addUniformBlock("GeomState", 3); // block binding point prog_chunk->addShader(vertShader); prog_chunk->addShader(fragShader); // // create uniform buffer objects and corresponding materials // OSG::UniformBufferObjChunkRefPtr ubo_material_database = create_material_database_state(materials); ubo_light_state = create_light_state(lights); OSG::PolygonChunkRefPtr polygon_chunk = OSG::PolygonChunk::create(); polygon_chunk->setFrontMode(GL_FILL); polygon_chunk->setBackMode(GL_FILL); polygon_chunk->setCullFace(GL_NONE); OSG::DepthChunkRefPtr depth_chunk = OSG::DepthChunk::create(); depth_chunk->setEnable(true); OSG::ChunkMaterialRefPtr prog_state = OSG::ChunkMaterial::create(); prog_state->addChunk(ubo_material_database, 1); // buffer binding point 1 prog_state->addChunk(ubo_light_state, 2); // buffer binding point 2 prog_state->addChunk(prog_chunk); prog_state->addChunk(polygon_chunk); prog_state->addChunk(depth_chunk); OSG::ShaderProgramVariableChunkRefPtr shader_var_chunk = OSG::ShaderProgramVariableChunk::create(); shader_var_chunk->addUniformBlock("GeomState", 3); GeomState geom1; geom1.material_index = dist(generator); OSG::ChunkMaterialRefPtr geom1_state = OSG::ChunkMaterial::create(); ubo_geom_state_1 = create_geometry_material_state(geom1); geom1_state->addChunk(ubo_geom_state_1, 3); // buffer binding point 3 geom1_state->addChunk(shader_var_chunk); // block binding point GeomState geom2; geom2.material_index = dist(generator); OSG::ChunkMaterialRefPtr geom2_state = OSG::ChunkMaterial::create(); ubo_geom_state_2 = create_geometry_material_state(geom2); geom2_state->addChunk(ubo_geom_state_2, 3); // buffer binding point 3 geom1_state->addChunk(shader_var_chunk); // block binding point cylgeo ->setMaterial(geom1_state); torusgeo->setMaterial(geom2_state); OSG::MaterialChunkOverrideGroupRefPtr mgrp = OSG::MaterialChunkOverrideGroup::create(); mgrp->setMaterial(prog_state); scene->setCore(mgrp); OSG::commitChanges(); mgr->setRoot(scene); // show the whole scene mgr->showAll(); } // GLUT main loop glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // open a new scope, because the pointers below should go out of scope // before entering glutMainLoop. // Otherwise OpenSG will complain about objects being alive after shutdown. { // the connection between GLUT and OpenSG OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // load the scene OSG::NodeRefPtr scene; if(argc < 2) { FWARNING(("No file given!\n")); FWARNING(("Supported file formats:\n")); std::list<const char*> suffixes; OSG::SceneFileHandler::the()->getSuffixList(suffixes, OSG::SceneFileType::OSG_READ_SUPPORTED); for(std::list<const char*>::iterator it = suffixes.begin(); it != suffixes.end(); ++it) { FWARNING(("%s\n", *it)); } scene = OSG::makeTorus(.5, 2, 16, 16); } else { /* All scene file loading is handled via the SceneFileHandler. */ scene = OSG::SceneFileHandler::the()->read(argv[1]); } OSG::commitChanges(); // calc size of the scene OSG::Vec3f min, max; OSG::BoxVolume vol; scene->getWorldVolume(vol); vol.getBounds(min, max); OSG::Vec3f d = max - min; OSG::Real32 offset = d.length() / 2.0f; // now create a deep clone OSG::NodeRefPtr sceneClone = OSG::deepCloneTree(scene); // this clones all nodes but the cores of type Material and Transform are shared. //NodePtr sceneClone = deepCloneTree(scene, "Material, Transform"); // now change all geometries from the cloned scene just to show // that it is a real deep copy. traverse(sceneClone, &changeGeo); // create a small scene graph with two transformation nodes. OSG::NodeRefPtr root = OSG::makeCoredNode<OSG::Group>(); OSG::ComponentTransformRefPtr t1; OSG::NodeRefPtr tn1 = OSG::makeCoredNode<OSG::ComponentTransform>(&t1); OSG::ComponentTransformRefPtr t2; OSG::NodeRefPtr tn2 = OSG::makeCoredNode<OSG::ComponentTransform>(&t2); t1->setTranslation(OSG::Vec3f(- offset, 0.0f, 0.0f)); t2->setTranslation(OSG::Vec3f(offset, 0.0f, 0.0f)); tn1->addChild(scene); tn2->addChild(sceneClone); root->addChild(tn1); root->addChild(tn2); OSG::commitChanges(); // create the SimpleSceneManager helper mgr = new OSG::SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (root); // show the whole scene mgr->showAll(); } // GLUT main loop glutMainLoop(); return 0; }
int main(int argc, char *argv[]) { // Init the OpenSG subsystem OSG::osgInit(argc, argv); { // We create a GLUT Window (that is almost the same for most applications) int winid = setupGLUT(&argc, argv); OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // Create the face std::string family = "SANS"; OSG::TextFace::Style style = OSG::TextFace::STYLE_PLAIN; OSG::TextTXFParam txfParam; txfParam.size = 46; txfParam.gap = 1; txfParam.setCharacters("Hello World!"); txfParam.textureWidth = 0; OSG::TextTXFFaceRefPtr face = OSG::TextTXFFace::create(family, style, txfParam); if (face == 0) { std::cerr << "ERROR: Cannot create face object!" << std::endl; return -1; } // Lay out one single line of text std::string text = "Hello World!"; // Use UTF-8 encoding! OSG::TextLayoutParam layoutParam; layoutParam.horizontal = true; layoutParam.leftToRight = true; layoutParam.topToBottom = true; layoutParam.majorAlignment = OSG::TextLayoutParam::ALIGN_FIRST; layoutParam.minorAlignment = OSG::TextLayoutParam::ALIGN_FIRST; layoutParam.spacing = 1.f; layoutParam.length.push_back(0.f); layoutParam.maxExtend = 0.f; OSG::TextLayoutResult layoutResult; face->layout(text, layoutParam, layoutResult); // Create the text geometry OSG::Real32 scale = 1.f; OSG::NodeRecPtr scene = face->makeNode(layoutResult, scale); // Get the texture that contains the characters of the font OSG::ImageRecPtr image = face->getTexture(); // Create the texture that will hold the image OSG::SimpleTexturedMaterialRecPtr tex = OSG::SimpleTexturedMaterial::create(); tex->setImage(image); tex->setEnvMode(GL_MODULATE); tex->setDiffuse(OSG::Color3f(1, 0, 0)); // Assign the texture to the geometry OSG::GeometryRecPtr geo = dynamic_cast<OSG::Geometry *>(scene->getCore()); geo->setMaterial(tex); // Create and setup the SSM mgr = new OSG::SimpleSceneManager; mgr->setWindow(gwin); mgr->setRoot(scene); // Create a blue background OSG::SolidBackgroundRecPtr bg = OSG::SolidBackground::create(); bg->setColor(OSG::Color3f(0.1, 0.1, 0.5)); gwin->getPort(0)->setBackground(bg); mgr->showAll(); OSG::commitChanges(); } // Give Control to the GLUT Main Loop glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { printf("Usage: testCGShader [base texture filename] [normal map filename]\n"); char *base_img_name = "wood.jpg"; char *normal_map_img_name = "normalmap.jpg"; Color4f tmp; if( argc > 1 ) base_img_name = argv[1]; if( argc > 2 ) normal_map_img_name = argv[2]; // 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->setSize( 800, 800 ); gwin->init(); // Create the shader material // Read the image for the base texture ImagePtr base_img = Image::create(); if(!base_img->read(base_img_name)) { fprintf(stderr, "Couldn't read base texture '%s'!\n", base_img_name); return 1; } ImagePtr normal_map_img = Image::create(); if(!normal_map_img->read(normal_map_img_name)) { fprintf(stderr, "Couldn't read normalmap texture '%s'!\n", normal_map_img_name); return 1; } ChunkMaterialPtr cmat = ChunkMaterial::create(); MaterialChunkPtr matc = MaterialChunk::create(); beginEditCP(matc); matc->setAmbient(Color4f(0.1, 0.1, 0.1, 1.0)); matc->setDiffuse(Color4f(0.3, 0.3, 0.3, 1.0)); matc->setSpecular(Color4f(0.8, 0.8, 0.8, 1.0)); matc->setShininess(100); matc->setLit(true); endEditCP(matc); // we use the glstate in the cg program so we force // to use the CG_PROFILE_ARBVP1 and CG_PROFILE_ARBFP1 extensions. CGChunkPtr cg = CGChunk::create(); beginEditCP(cg); cg->setVertexProfile(CG_PROFILE_ARBVP1); cg->setVertexProgram(_vp_program); cg->setFragmentProfile(CG_PROFILE_ARBFP1); cg->setFragmentProgram(_fp_program); // some optional parameters. //cg->setVertexEntryPoint("main"); //cg->getVertexArguments().push_back("..."); //cg->setFragmentEntryPoint("main"); //cg->getFragmentArguments().push_back("..."); endEditCP(cg); TextureChunkPtr tex_base = TextureChunk::create(); beginEditCP(tex_base); tex_base->setImage(base_img); tex_base->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_base->setMagFilter(GL_LINEAR); tex_base->setWrapS(GL_REPEAT); tex_base->setWrapT(GL_REPEAT); tex_base->setEnvMode(GL_MODULATE); endEditCP(tex_base); TextureChunkPtr tex_normal_map = TextureChunk::create(); beginEditCP(tex_normal_map); tex_normal_map->setImage(normal_map_img); tex_normal_map->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_normal_map->setMagFilter(GL_LINEAR); tex_normal_map->setWrapS(GL_REPEAT); tex_normal_map->setWrapT(GL_REPEAT); tex_normal_map->setEnvMode(GL_MODULATE); endEditCP(tex_normal_map); beginEditCP(cmat); cmat->addChunk(matc); cmat->addChunk(cg); cmat->addChunk(tex_base); cmat->addChunk(tex_normal_map); endEditCP(cmat); // create root node _scene = Node::create(); // create torus GeometryPtr geo = makeTorusGeo(.8, 1.8, 128, 128); beginEditCP( geo, Geometry::MaterialFieldMask); geo->setMaterial(cmat); endEditCP(geo, Geometry::MaterialFieldMask); NodePtr torus = Node::create(); beginEditCP(torus, Node::CoreFieldMask); torus->setCore(geo); endEditCP(torus, Node::CoreFieldMask); // add torus to scene GroupPtr group = Group::create(); beginEditCP(_scene); _scene->setCore(group); _scene->addChild(torus); 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; }
// 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(); beginEditCP(gwin); gwin->setId(winid); gwin->setSize( 800, 800 ); gwin->init(); endEditCP(gwin); // create root node _scene = makeCoredNode<Group>(); GeometryPtr geo = makeBoxGeo(0.5, 0.5, 0.5, 1, 1, 1); // share the chunk CGChunkPtr cg = CGChunk::create(); beginEditCP(cg); cg->setVertexProfile(CG_PROFILE_ARBVP1); cg->setVertexProgram(_vp_program); cg->setFragmentProfile(CG_PROFILE_ARBFP1); cg->setFragmentProgram(_fp_program); endEditCP(cg); Int32 size = 4; // start color Vec3f sc(0.0, 0.0, 0.0); // end color Vec3f ec(1.0, 1.0, 1.0); Real32 sr = (ec[0] - sc[0]) / Real32((size*2)); Real32 sg = (ec[1] - sc[1]) / Real32((size*2)); Real32 sb = (ec[2] - sc[2]) / Real32((size*2)); Vec3f color(sc); Int32 x = - size; Int32 y = - size; Int32 z = - size; UInt32 iterations = size*2 * size*2 * size*2; printf("Creating %u cubes ...\n", iterations); for(UInt32 i=0;i<iterations;++i) { ChunkMaterialPtr cmat = ChunkMaterial::create(); // ok use one CGChunk and n CGParameterChunks CGParameterChunkPtr cgparameter = CGParameterChunk::create(); beginEditCP(cgparameter); cgparameter->setCGChunk(cg); cgparameter->setUniformParameter("SurfaceColor", color); endEditCP(cgparameter); _cgparameter = cgparameter; beginEditCP(cmat); cmat->addChunk(cg); cmat->addChunk(cgparameter); endEditCP(cmat); TransformPtr trans; NodePtr trans_node = makeCoredNode<Transform>(&trans); beginEditCP(trans); trans->getMatrix().setTranslate(Real32(x), Real32(y), Real32(z)); endEditCP(trans); MaterialGroupPtr mg; NodePtr mg_node = makeCoredNode<MaterialGroup>(&mg); beginEditCP(mg, MaterialGroup::MaterialFieldMask); mg->setMaterial(cmat); endEditCP(mg, MaterialGroup::MaterialFieldMask); NodePtr geonode = Node::create(); beginEditCP(geonode, Node::CoreFieldMask); geonode->setCore(geo); endEditCP(geonode, Node::CoreFieldMask); beginEditCP(mg_node); mg_node->addChild(geonode); endEditCP(mg_node); beginEditCP(trans_node); trans_node->addChild(mg_node); endEditCP(trans_node); // add to scene beginEditCP(_scene); _scene->addChild(trans_node); endEditCP(_scene); // ---- ++x; color[0] += sr; if(x == size) { x = - size; ++y; color[0] = sc[0]; color[1] += sg; if(y == size) { y = - size; ++z; color[1] = sc[1]; color[2] += sb; } } } // 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(); // create a gradient background. GradientBackgroundPtr gback = GradientBackground::create(); beginEditCP(gback, GradientBackground::LineFieldMask); gback->clearLines(); gback->addLine(Color3f(0.7, 0.7, 0.8), 0); gback->addLine(Color3f(0.0, 0.1, 0.3), 1); endEditCP(gback, GradientBackground::LineFieldMask); WindowPtr win = _mgr->getWindow(); beginEditCP(win); for(int i=0;i<win->getPort().size();++i) { ViewportPtr vp = win->getPort()[i]; beginEditCP(vp); vp->setBackground(gback); endEditCP(vp); } endEditCP(win); // GLUT main loop glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int doMain(int argc, char **argv) { printf("Press key '1', '2', or '3' to toggle the light sources.\n"); // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->setSize( 800, 800 ); gwin->init(); // Create the shader material OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create(); OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create(); matc->setAmbient(OSG::Color4f(0.1f, 0.1f, 0.1f, 1.0f)); matc->setDiffuse(OSG::Color4f(0.3f, 0.3f, 0.3f, 1.0f)); matc->setSpecular(OSG::Color4f(0.8f, 0.8f, 0.8f, 1.0f)); matc->setShininess(100); matc->setLit(true); OSG::ShaderProgramChunkUnrecPtr shl = OSG::ShaderProgramChunk::create(); OSG::ShaderProgramUnrecPtr shl_vp = OSG::ShaderProgram::createVertexShader(); shl_vp->setProgram(_vp_program); shl->addShader(shl_vp); OSG::ShaderProgramUnrecPtr shl_fp = OSG::ShaderProgram::createFragmentShader(); shl_fp->setProgram(_fp_program); shl->addShader(shl_fp); shl_vp->addProceduralVariable ("Light0Active", &light0Active); shl_vp->addProceduralVariable ("Light1Active", &light1Active); shl_vp->addNodeProceduralVariable("Light2Active", &light2Active); cmat->addChunk(matc); cmat->addChunk(shl); // create root node _scene = OSG::Node::create(); // create two light sources. OSG::TransformUnrecPtr point1_trans; OSG::NodeUnrecPtr point1 = OSG::makeCoredNode<OSG::PointLight>(&_point1_core); point1_beacon = OSG::makeCoredNode<OSG::Transform >(&point1_trans); point1_trans->editMatrix().setTranslate(-10.0, 5.0, 5.0); _point1_core->setAmbient(0.0f, 0.0f, 0.0f , 1.0f); _point1_core->setDiffuse(1.0f, 0.0f, 0.0f, 1.0f); _point1_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f); _point1_core->setBeacon(point1_beacon); _point1_core->setOn(true); OSG::TransformUnrecPtr point2_trans; OSG::NodeUnrecPtr point2 = OSG::makeCoredNode<OSG::PointLight>(&_point2_core); point2_beacon = OSG::makeCoredNode<OSG::Transform >(&point2_trans); point2_trans->editMatrix().setTranslate(10.0, 5.0, 5.0); _point2_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f); _point2_core->setDiffuse(0.0f, 1.0f, 0.0f, 1.0f); _point2_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f); _point2_core->setBeacon(point2_beacon); _point2_core->setOn(true); point1->addChild(point2); OSG::TransformUnrecPtr point3_trans; OSG::NodeUnrecPtr point3 = OSG::makeCoredNode<OSG::PointLight>(&_point3_core); point3_beacon = OSG::makeCoredNode<OSG::Transform >(&point3_trans); point3_trans->editMatrix().setTranslate(0.0, -12.0, 5.0); _point3_core->setAmbient(0.0f, 0.0f, 0.0f, 1.0f); _point3_core->setDiffuse(0.5f, 0.0f, 1.0f, 1.0f); _point3_core->setSpecular(1.0f, 1.0f, 1.0f, 1.0f); _point3_core->setBeacon(point3_beacon); _point3_core->setOn(true); point2->addChild(point3); // create a sphere. OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0); geo->setMaterial(cmat); OSG::NodeUnrecPtr sphere = OSG::makeNodeFor(geo); point3->addChild(sphere); _scene->setCore(OSG::Group::create()); _scene->addChild(point1); // create the SimpleSceneManager helper _mgr = OSG::SimpleSceneManager::create(); // tell the manager what to manage _mgr->setWindow(gwin ); _mgr->setRoot(_scene); _mgr->turnHeadlightOff(); // show the whole scene _mgr->showAll(); // enable local lights. // OSG::RenderAction *ract = // dynamic_cast<OSG::RenderAction *>(_mgr->getRenderAction()); // ract->setLocalLights(true); return 0; }
int main(int argc, char *argv[]) { // Init the OpenSG subsystem OSG::osgInit(argc, argv); { // We create a GLUT Window (that is almost the same for most applications) int winid = setupGLUT(&argc, argv); OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // Create the face std::string family = "SANS"; OSG::TextFace::Style style = OSG::TextFace::STYLE_PLAIN; OSG::UInt32 size = 32; OSG::TextPixmapFaceRefPtr face = OSG::TextPixmapFace::create(family, style, size); if (face == 0) { std::cerr << "ERROR: Cannot create face object!" << std::endl; return -1; } // Lay out one single line of text std::string text = "Hello World!"; // Use UTF-8 encoding! OSG::TextLayoutParam layoutParam; layoutParam.horizontal = true; layoutParam.leftToRight = true; layoutParam.topToBottom = true; layoutParam.majorAlignment = OSG::TextLayoutParam::ALIGN_FIRST; layoutParam.minorAlignment = OSG::TextLayoutParam::ALIGN_FIRST; layoutParam.spacing = 1.f; layoutParam.length.push_back(0.f); layoutParam.maxExtend = 0.f; OSG::TextLayoutResult layoutResult; face->layout(text, layoutParam, layoutResult); // Render the text into an OpenSG image OSG::Vec2f offset; OSG::UInt32 border = 1; OSG::ImageRecPtr imagePtr = face->makeImage(layoutResult, offset, border); // Create the geometry which we will assign the texture to OSG::Real32 width = imagePtr->getWidth(); OSG::Real32 height = imagePtr->getHeight(); OSG::NodeRecPtr plane = OSG::makePlane(width, height, 1, 1); // Create the texture that will hold the image OSG::SimpleTexturedMaterialRecPtr tex = OSG::SimpleTexturedMaterial::create(); tex->setImage(imagePtr); tex->setEnvMode(GL_MODULATE); tex->setDiffuse(OSG::Color3f(1, 0, 0)); // Assign the texture to the geometry OSG::GeometryRecPtr geo = dynamic_cast<OSG::Geometry *>(plane->getCore()); geo->setMaterial(tex); // Transform the geometry so that the origin of the text is at // the origin of the world coordinate system OSG::NodeRecPtr scene = OSG::Node::create(); OSG::TransformRecPtr transformPtr = OSG::Transform::create(); OSG::Matrix m; m.setTranslate(offset.x() + width / 2, offset.y() - height / 2, 0); transformPtr->setMatrix(m); scene->setCore(transformPtr); scene->addChild(plane); // Create and setup the SSM mgr = new OSG::SimpleSceneManager; mgr->setWindow(gwin); mgr->setRoot(scene); // Create a blue background OSG::SolidBackgroundRecPtr bg = OSG::SolidBackground::create(); bg->setColor(OSG::Color3f(0.1, 0.1, 0.5)); gwin->getPort(0)->setBackground(bg); mgr->showAll(); OSG::commitChanges(); } // Give Control to the GLUT Main Loop glutMainLoop(); return 0; }
int main(int argc, char **argv) { // enableFPE(); #ifdef __sgi signal(SIGSEGV, (void (*)(int))sighand); signal(SIGTRAP, (void (*)(int))sighand); signal(SIGBUS, (void (*)(int))sighand); #endif // OSG init osgInit(argc,argv); if (argc > 1 && ! strcmp(argv[1],"-bench")) { runBench = true; argc--; argv++; glutInitWindowPosition(0,0); glutInitWindowSize(600,600); } if (argc > 1 && ! strcmp(argv[1],"-test")) { testSet = true; doMotion = false; 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(); NodePtr pnode = Node::create(); ComponentTransformPtr trans = ComponentTransform::create(); beginEditCP(scene); scene->setCore(trans); scene->addChild(pnode); endEditCP(scene); beginEditCP(trans); trans->setTranslation(Vec3f(2,0,0)); trans->setRotation(Quaternion(Vec3f(0,1,0),Pi/2)); trans->setScale(Vec3f(2,2,2)); endEditCP(trans); particles = Particles::create(); beginEditCP(pnode); pnode->setCore(particles); endEditCP(pnode); numParticles = 100; if (argc > 1) { numParticles=atoi(argv[1]); } beginEditCP(particles); pnts=GeoPositions3f::create(); secpnts=GeoPositions3f::create(); addRefCP(pnts); addRefCP(secpnts); GeoColors3fPtr cols = GeoColors3f::create(); GeoNormals3fPtr norms = GeoNormals3f::create(); MFPnt3f* p=pnts->editFieldPtr(); MFPnt3f* sp=secpnts->editFieldPtr(); MFVec3f *size=particles->editMFSizes(); indices = particles->editMFIndices(); velocities=new Vec3f [numParticles]; beginEditCP(pnts); beginEditCP(secpnts); beginEditCP(cols); if(!testSet) { for(UInt32 i=0; i < numParticles; ++i) { Pnt3f pnt(osgrand(),osgrand(),osgrand()); indices->push_back(i); p->push_back(pnt); sp->push_back(pnt); velocities[i].setValues(osgrand()/30.f/2, osgrand()/30.f/2, osgrand()/30.f/2); cols->editFieldPtr()->push_back( Color3f(osgrand()/2.f + .5f,osgrand()/2.f + .5f,osgrand()/2.f + .5f) ); size->push_back( Vec3f(osgrand()/20.f+0.05,osgrand()/20.f+0.05,osgrand()/20.f+0.05)); } } else { Pnt3f tpos[] = { Pnt3f(.5,.5,.5), Pnt3f (.5,.5,.7), Pnt3f(.5,.5,.9), Pnt3f(.7,.5,.5), Pnt3f(.5,.7,.5), Pnt3f (-1000,-1000,-1000) }; Pnt3f tsecpos[] = { Pnt3f(0,0,0), Pnt3f(0,0,0), Pnt3f(0,0,0), Pnt3f(0,0,0), Pnt3f(0,0,0) }; Vec3f tvel[] = { Vec3f(0,0,0), Vec3f(0,0,0), Vec3f(0,0,0), Vec3f(0,0,0), Vec3f(0,0,0) }; Color3f tcol[] = { Color3f(1,0,0), Color3f(0,1,0), Color3f(0,0,1), Color3f(1,1,0), Color3f(1,0,1), Color3f(0,1,1), Color3f(1,1,1) }; Vec3f tsize[] = { Vec3f(.1,0,0), Vec3f(.1,0,0), Vec3f(.1,0,0), Vec3f(.1,0,0), Vec3f(.1,0,0) }; for(UInt32 i=0; tpos[i][0] > -1000; ++i) { indices->push_back(i); p->push_back(tpos[i]); sp->push_back(tsecpos[i]); velocities[i].setValue(tvel[i]); cols->editFieldPtr()->push_back(tcol[i]); size->push_back(tsize[i]); } } endEditCP(pnts); endEditCP(secpnts); endEditCP(cols); beginEditCP(norms); norms->push_back(Vec3f(0,1,0)); endEditCP(norms); particles->setPositions( pnts ); particles->setSecPositions( secpnts ); particles->setColors( cols ); particles->setNormals( norms ); endEditCP(particles); // set volume static to prevent constant update beginEditCP(pnode, Node::VolumeFieldMask); #ifndef OSG_2_PREP Volume &v = pnode->editVolume(false).getInstance(); #else Volume &v = pnode->editVolume(false); #endif v.setEmpty(); v.extendBy(Pnt3f(0,0,0)); v.extendBy(Pnt3f(1,1,1)); v.setStatic(); #ifndef OSG_2_PREP pnode->editVolume(false).instanceChanged(); #endif endEditCP (pnode, Node::VolumeFieldMask); SimpleTexturedMaterialPtr tm; tm = SimpleTexturedMaterial::create(); UChar8 imgdata[] = { 255,255,255, 255,0,0, 255,0,255, 255,0,0, 255,0,0, 255,255,255 }; ImagePtr pImage = Image::create(); if (argc > 2) { pImage->read(argv[2]); } else { pImage->set(Image::OSG_RGB_PF, 3, 2, 1, 1, 1, 0, imgdata); } beginEditCP(tm); tm->setDiffuse( Color3f( 1,1,1 ) ); tm->setLit( false ); tm->setImage( pImage ); tm->setEnvMode( GL_MODULATE ); BlendChunkPtr bl=BlendChunk::create(); beginEditCP(bl); bl->setSrcFactor(GL_SRC_ALPHA); //bl->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); bl->setDestFactor(GL_ONE); #if 0 bl->setAlphaFunc(GL_EQUAL); bl->setAlphaValue(1); #endif endEditCP(bl); tm->addChunk(bl); endEditCP(tm); particles->setMaterial( tm ); #if 0 // write it, just for testing std::ofstream out("test.osg"); OSGWriter w(out); w.write(scene); #endif // 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->setHighlight(scene); // GLUT main loop 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(); // create the scene /* Transformation accumulate through the graph, i.e. all nodes below a Transformation are influenced by it, even other Transformations. This can be used to create models of objects that move together and in relation to each other, the prime examples being a robot arm and a planetary system. This example does something not quite unlike a robot arm. */ // create the scene /* This time the graph is not wide, but deep, i.e. every Transformation only has two children, a Geometry and another transformation. The end resulting motion of the geometry is the accumulation of all the Transformations above it. */ // use a cylinder this time GeometryPtr cyl = makeCylinderGeo( 1, .3, 8, true, true, true ); // the single transformation Core used trans = Transform::create(); // setup an intial transformation Matrix m; m.setTransform(Vec3f(0, .9, 0)); beginEditCP(trans, Transform::MatrixFieldMask); { trans->setMatrix(m); } endEditCP (trans, Transform::MatrixFieldMask); /* NullFC is the generic NULL value for FieldContainer pointer. */ NodePtr last = NullFC; // create the copied transformations and their geometry nodes for(UInt16 i = 1; i < ncopies; ++i) { // create the shared Geometry NodePtr geonode = Node::create(); beginEditCP(geonode, Node::CoreFieldMask ); { geonode->setCore(cyl); } endEditCP (geonode, Node::CoreFieldMask ); // add a transformation to the Geometry NodePtr transnode = Node::create(); beginEditCP(transnode, Node::CoreFieldMask | Node::ChildrenFieldMask); { transnode->setCore (trans); transnode->addChild(geonode ); if(last != NullFC) transnode->addChild(last); } endEditCP (transnode, Node::CoreFieldMask | Node::ChildrenFieldMask); last = transnode; } NodePtr scene = last; // 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; }
// 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->setSize( 800, 800 ); gwin->init(); // Create the shader material ChunkMaterialPtr cmat = ChunkMaterial::create(); // Read the image for the normal texture ImagePtr earth_map_img = Image::create(); if(!earth_map_img->read("earth.jpg")) { fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n"); return 1; } TextureChunkPtr tex_earth = TextureChunk::create(); beginEditCP(tex_earth); tex_earth->setImage(earth_map_img); tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_earth->setMagFilter(GL_LINEAR); tex_earth->setWrapS(GL_REPEAT); tex_earth->setWrapT(GL_REPEAT); tex_earth->setEnvMode(GL_MODULATE); endEditCP(tex_earth); // Read the image for the normal texture ImagePtr earth_night_map_img = Image::create(); if(!earth_night_map_img->read("EarthNight.jpg")) { fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n"); return 1; } TextureChunkPtr tex_earth_night = TextureChunk::create(); beginEditCP(tex_earth_night); tex_earth_night->setImage(earth_night_map_img); tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_earth_night->setMagFilter(GL_LINEAR); tex_earth_night->setWrapS(GL_REPEAT); tex_earth_night->setWrapT(GL_REPEAT); tex_earth_night->setEnvMode(GL_MODULATE); endEditCP(tex_earth_night); // Read the image for the normal texture ImagePtr earth_clouds_map_img = Image::create(); if(!earth_clouds_map_img->read("EarthClouds.jpg")) { fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n"); return 1; } TextureChunkPtr tex_earth_clouds = TextureChunk::create(); beginEditCP(tex_earth_clouds); tex_earth_clouds->setImage(earth_clouds_map_img); tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_earth_clouds->setMagFilter(GL_LINEAR); tex_earth_clouds->setWrapS(GL_REPEAT); tex_earth_clouds->setWrapT(GL_REPEAT); tex_earth_clouds->setEnvMode(GL_MODULATE); endEditCP(tex_earth_clouds); _shl = SHLChunk::create(); beginEditCP(_shl); if(!_shl->readVertexProgram("Earth.vp")) fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n"); if(!_shl->readFragmentProgram("Earth.fp")) fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n"); _shl->setUniformParameter("EarthDay", 0); _shl->setUniformParameter("EarthNight", 1); _shl->setUniformParameter("EarthCloudGloss", 2); _shl->setUniformParameter("season", 0.0f); _shl->setUniformParameter("cos_time_0_2PI", -0.406652f); _shl->setUniformParameter("sin_time_0_2PI", -0.913583f); endEditCP(_shl); beginEditCP(cmat); cmat->addChunk(_shl); cmat->addChunk(tex_earth); cmat->addChunk(tex_earth_night); cmat->addChunk(tex_earth_clouds); endEditCP(cmat); // create root node _scene = Node::create(); GeometryPtr geo = makeLatLongSphereGeo (100, 100, 1.0); beginEditCP( geo, Geometry::MaterialFieldMask); geo->setMaterial(cmat); endEditCP(geo, Geometry::MaterialFieldMask); NodePtr torus = Node::create(); beginEditCP(torus, Node::CoreFieldMask); torus->setCore(geo); endEditCP(torus, Node::CoreFieldMask); // add torus to scene GroupPtr group = Group::create(); beginEditCP(_scene); _scene->setCore(group); _scene->addChild(torus); 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; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // Args given? if(argc > 1) { if(sscanf(argv[1], "%u", &nlights) != 1) { FWARNING(("Number of lights '%s' not understood.\n", argv[1])); nlights = 3; } } // open a new scope, because the pointers below should go out of scope // before entering glutMainLoop. // Otherwise OpenSG will complain about objects being alive after shutdown. { // the connection between GLUT and OpenSG OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); /* A Light defines a source of light in the scene. Generally, two types of information are of interest: The position of the light source (geometry), and what elements of the scene are lit (semantics). Using the position of the light in the graph for geometry allows moving the Light just like any other node, by putting it below a OSG::Transform Node and changing the transformation. This consistency also simplifies attaching Lights to moving parts in the scene: just put them below the same Transform and they will move with the object. The semantic interpretation also makes sense, it lets you restrict the influence area of the light to a subgraph of the scene. This can be used for efficiency, as every active light increases the amount of calculations necessary per vertex, even if the light doesn't influence the vertex, because it is too far away. It can also be used to overcome the restrictions on the number of lights. OpenSG currently only allows 8 concurrently active lights. It is also not difficult to imagine situations where both interpretations are necessary at the same time. Take for example a car driving through a night scene. You'd want to headlights to be fixed to the car and move together with it. But at the same time they should light the houses you're driving by, and not the mountains in the distance. Thus there should be a way to do both at the same time. OpenSG solves this by splitting the two tasks to two Nodes. The Light's Node is for the sematntic part, it defines which object are lit by the Light. FOr the geometrc part the Light keeps a SFNodePtr to a different Node, the so called beacon. The local coordinate system of the beacon provides the reference coordinate system for the light's position. Thus the typical setup of an OpenSG scenegraph starts with a set of lights, which light the whole scene, followed by the actual geometry. Tip: Using the beacon of the camera (see \ref PageSystemWindowCamera) as the beacon of a light source creates a headlight. Every light is closely related to OpenGL's light specification. It has a diffuse, specular and ambient color. Additionally it can be switched on and off using the on field. */ // Create the scene OSG::NodeRefPtr scene = OSG::Node::create(); OSG::GroupRefPtr group = OSG::Group::create(); scene->setCore(group); // create the scene to be lit // a simple torus is fine for now. // You can add more Geometry here if you want to. OSG::NodeRefPtr lit_scene = OSG::makeTorus(.5, 2, 32, 64); // helper node to keep the lights on top of each other OSG::NodeRefPtr lastnode = lit_scene; // create the light sources OSG::Color3f colors[] = { OSG::Color3f(1,0,0), OSG::Color3f(0,1,0), OSG::Color3f(0,0,1), OSG::Color3f(1,1,0), OSG::Color3f(0,1,1), OSG::Color3f(1,0,1), OSG::Color3f(1,1,1), OSG::Color3f(1,1,1) }; if(nlights > 8) { FWARNING(("Currently only 8 lights supported\n")); nlights = 8; } // scale the lights to not overexpose everything. Just a little. OSG::Real32 scale = OSG::osgMax(1., 1.5 / nlights); for(OSG::UInt16 i = 0; i < nlights; ++i) { // create the light source OSG::NodeRefPtr light = OSG::Node::create(); OSG::LightRefPtr light_core; OSG::NodeRefPtr geo_node; switch((i % 3) + 0) { /* The PointLight has a position to define its location. In addition, as it really is located in the scene, it has attenuation parameters to change the light's intensity depending on the distance to the light. Point lights are more expesinve to compute than directional lights, but not quite as expesive as spot lights. If you need to see the localized effects of the light, a point light is a good compromise between speed and quality. */ case 0: { OSG::PointLightRefPtr l = OSG::PointLight::create(); l->setPosition (0, 0, 0); l->setConstantAttenuation (1); l->setLinearAttenuation (0); l->setQuadraticAttenuation (3); // a little sphere to show where the light is geo_node = OSG::makeLatLongSphere(8, 8, 0.1f); OSG::GeometryRefPtr geo = dynamic_cast<OSG::Geometry *>(geo_node->getCore()); OSG::SimpleMaterialRefPtr sm = OSG::SimpleMaterial::create(); sm->setLit(false); sm->setDiffuse(OSG::Color3f( colors[i][0], colors[i][1], colors[i][2] )); geo->setMaterial(sm); light_core = l; } break; /* The DirectionalLight just has a direction. To use it as a headlight use (0,0,-1) as a direction. it is the computationally cheapest light source. Thus for the fastest lit rendering, just a single directional light source. The osg::SimpleSceneManager's headlight is a directional light source. */ case 1: { OSG::DirectionalLightRefPtr l = OSG::DirectionalLight::create(); l->setDirection(0, 0, 1); // a little cylinder to show where the light is geo_node = OSG::makeCylinder(.1f, .03f, 8, true, true, true); OSG::GeometryRefPtr geo = dynamic_cast<OSG::Geometry *>(geo_node->getCore()); OSG::SimpleMaterialRefPtr sm = OSG::SimpleMaterial::create(); sm->setLit(false); sm->setDiffuse(OSG::Color3f( colors[i][0], colors[i][1], colors[i][2] )); geo->setMaterial(sm); light_core = l; } break; /* The SpotLight adds a direction to the PointLight and a spotCutOff angle to define the area that's lit. To define the light intensity fallof within that area the spotExponent field is used. Spot lights are very expensive to compute, use them sparingly. */ case 2: { OSG::SpotLightRefPtr l = OSG::SpotLight::create(); l->setPosition (OSG::Pnt3f(0, 0, 0)); l->setDirection (OSG::Vec3f(0, -1, 0)); l->setSpotExponent (2); l->setSpotCutOff (OSG::osgDegree2Rad(45)); l->setConstantAttenuation (1); l->setLinearAttenuation (0); l->setQuadraticAttenuation (3); // a little cone to show where the light is geo_node = OSG::makeCone(.2f, .2f, 8, true, true); OSG::GeometryRefPtr geo = dynamic_cast<OSG::Geometry *>(geo_node->getCore()); OSG::SimpleMaterialRefPtr sm = OSG::SimpleMaterial::create(); sm->setLit(false); sm->setDiffuse(OSG::Color3f( colors[i][0], colors[i][1], colors[i][2] )); geo->setMaterial(sm); light_core = l; } break; } // create the beacon and attach it to the scene OSG::NodeRefPtr beacon = OSG::Node::create(); OSG::TransformRefPtr beacon_core = OSG::Transform::create(); lightBeacons[i] = beacon_core; beacon->setCore(beacon_core); beacon->addChild(geo_node); scene->addChild(beacon); light_core->setAmbient (colors[i][0] / scale, colors[i][1] / scale, colors[i][2] / scale, 1); light_core->setDiffuse (colors[i][0] / scale, colors[i][1] / scale, colors[i][2] / scale, 1); light_core->setSpecular(1 / scale, 1 / scale, 1 / scale, 1); light_core->setBeacon (beacon); light->setCore(light_core); light->addChild(lastnode); lights[i] = light_core; lastnode = light; } scene->addChild(lastnode); OSG::commitChanges(); // create the SimpleSceneManager helper mgr = OSG::SimpleSceneManager::create(); // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); // switch the headlight off, we have enough lights as is mgr->setHeadlight(false); // show the whole scene mgr->showAll(); } // GLUT main loop 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(); // create the scene first = Node::create (); beginEditCP(first); first->setCore(Group::create()); unsigned i; for (i=0; i<numFirst; ++i) { first->addChild(makePerturbedAll(4, 1.0f)); //makeTransformedCube(1.0f,1.0f,1.0f,1,1,1, Color3f(1,0,0))); } endEditCP(first); second = Node::create (); beginEditCP(second); second->setCore(Group::create()); for (i=0; i<numSecond; ++i) { second->addChild(makePerturbedAll(4, 1.0f, 0.0f)); //makeTransformedCube(1.0f,1.0f,1.0f,1,1,1, Color3f(0,1,0))); } endEditCP(second); for (i=0; i<numFirst; ++i) { TransformPtr trf = TransformPtr::dcast(first->getChild(i)->getCore()); beginEditCP(trf); randomTransform(trf->getMatrix()); endEditCP(trf); } for (i=0; i<numSecond; ++i) { TransformPtr trf = TransformPtr::dcast(second->getChild(i)->getCore()); beginEditCP(trf); randomTransform(trf->getMatrix()); endEditCP(trf); } scene = Node::create (); beginEditCP(scene); scene->setCore(Group::create()); scene->addChild(first); scene->addChild(second); endEditCP(scene); addRefCP(scene); SLOG << "scene created" << std::endl; // 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(); // prepare for collision detection // * fill cache OSGCache::the().setHierarchy(NULL); OSGCache::the().apply(scene); SLOG << "cache filled." << std::endl; // * build hierarchy OSGCache::the().setHierarchy(&hier); OSGCache::the().apply(scene); SLOG << "adapters created.." << std::endl; // * create double traverser for collision detection traverser = new Traverser(); traverser->setUseCoherency(false); // do not use generalized front cache for frame coherency traverser->getDataTyped().setStopFirst(false); #ifndef GV_WITH_RAPID SWARNING << "For RAPID support you have to define GV_WITH_RAPID (see Kernel/OSGGVBase.h)" << std::endl; #endif // * create all traverser all = new AllTraverser<OpenSGTraits>(); all->setDoubleTraverser(traverser); // GLUT main loop Profiler::the().Reset(); 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(); PathHandler paths; paths.push_backPath("."); // create the scene // build a special txf-font with only a little set of characters FontStyle *fontStyle = FontStyleFactory::the().create(paths, argv[1], 1); assert(fontStyle); //((TTFontStyle *) fontStyle)->createTXFMap((UChar8 *) ". fps0123456789"); ((TTFontStyle *) fontStyle)->createTXFMap(); // write it somewhere #if 1 std::ofstream target; target.open("statistics.txf"); fontStyle->dump(target); target.close(); #else ostrstream target; fontStyle->dump(target); #endif #if 1 std::ifstream source; source.open("statistics.txf"); #else #if 0 // Hack, to get the stuff into memory int bufSize = 100000; char *buffer = new char[bufSize]; int numRead; FILE *in = fopen("statistics.txf", "r"); numRead = fread(buffer, 1, bufSize, in); fclose(in); istrstream source(buffer, numRead); #else istrstream source(target.str(), target.pcount()); #endif #endif TXFFont *font = new TXFFont("test.txf", source); font->initFont(); fontText.setSize(1); font->createInstance(&fontText); fontText.setJustifyMajor(MIDDLE_JT); lineVec.push_back("0000.00 fps"); // TXF-Style Texture+Geometry n = Node::create(); txfGeo = Geometry::create(); ImagePtr pTxfImg = Image::create(); if(fontText.fillTXFGeo(*txfGeo, true, lineVec)) { fontText.fillTXFImage(pTxfImg); SimpleTexturedMaterialPtr mat = SimpleTexturedMaterial::create(); beginEditCP(mat); { mat->setImage(pTxfImg); } endEditCP(mat); txfGeo->setMaterial(mat); beginEditCP(n, Node::CoreFieldMask); { n->setCore(txfGeo); } } scene = Node::create(); // add a transformation to make it move trans = Transform::create(); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); { scene->setCore(trans); scene->addChild(n); } endEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); // 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; }
OSG_END_NAMESPACE // Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // open a new scope, because the pointers below should go out of scope // before entering glutMainLoop. // Otherwise OpenSG will complain about objects being alive after shutdown. { // the connection between GLUT and OpenSG OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // load the scene OSG::NodeRefPtr scene; if(argc < 2) { FWARNING(("No file given!\n")); scene = OSG::makeTorus(.5, 2, 16, 16); } else { scene = OSG::SceneFileHandler::the()->read(argv[1]); } /* An Attachment is a special field container that can be attached to many of the internal classes like Nodes, NodeCores and many others. There can be multiple Attachments attached to an object. Attachments can be attached to all FieldContainers that are derived from AttachmentContainer. This includes most higher-level classes in the system, like Nodes, NodeCores, Windows, Viewports etc. See the inheritance graph for details. One predefined kind of Attachment is the Name, which can keep the name of an object. Some of loaders (e.g. the WRL loader) create these kinds of Attachments for named nodes. */ /* An Attachment is a FieldContainer and as such needs to be created using ::create(). */ OSG::NameRefPtr name = OSG::Name::create(); /* The NameAttachment only has a single field, there's no need to use the mask here. */ name->editFieldPtr()->setValue("Scene"); /* Attach the name to the scene node. */ scene->addAttachment(name); /* Check if the scene has a Name attachment Attachments are categorized by the GroupID of their class. Every AttachmentContainer generally keeps only one attachment of a specific kind. */ OSG::AttachmentRefPtr a; a = scene->findAttachment(OSG::Name::getClassType()); if(a != NULL) { OSG::NameRefPtr n = OSG::dynamic_pointer_cast<OSG::Name>(a); SLOG << "Node name: " << n->getField().getValue() << OSG::endLog; } else { SLOG << "Node has no name!" << OSG::endLog; } /* As names are used quite often there are two convenience functions that wrap the code given above: setName and getName. They are declared in OSGSimpleAttachments.h. */ if(getName(scene)) { SLOG << "Node is named: " << getName(scene) << OSG::endLog; } else { SLOG << "Node is unnamed." << OSG::endLog; } setName(scene, "Scene"); // use the NamedNodeFinder helper to find a named object NamedNodeFinder f; OSG::NodeRefPtr found; found = f(scene, "Scene"); SLOG << "Found object " << found << " named Scene." << OSG::endLog; found = f(scene, "TF_DETAIL"); if(found == NULL) { SLOG << "Found no object named TF_DETAIL (did you load the tie?)." << OSG::endLog; } else { SLOG << "Found object " << found << " named TF_DETAIL." << OSG::endLog; } // Use the simple attachment defined above OSG::MyAttachmentRefPtr mya = OSG::MyAttachment::create(); mya->editFieldPtr()->setValue(42); // attach it to the scene scene->addAttachment(mya); // and check if it's still there a = scene->findAttachment(OSG::MyAttachment::getClassType()); if(a != NULL) { OSG::MyAttachmentRefPtr m = OSG::dynamic_pointer_cast<OSG::MyAttachment>(a); SLOG << "Node my value: " << m->getField().getValue() << OSG::endLog; } else { SLOG << "Node has no myAttachment!" << OSG::endLog; } /* In case you don't want to create a new Attachment or cannot do that because it needs to reference external data, you just attach a void* using the VoidPAttachment. This is somewhat equivalent to the standard userdata pointer. Note that the referenced data will not be threadsafe. Every thread has its own copy of the reference, but if if multiple threads reference the same data they can interfere. Future versions will have some provisions to allow making this threadsafe. Stay tuned. */ OSG::VoidPAttachmentRefPtr myvoid = OSG::VoidPAttachment::create(); OSG::UInt32 dummy = 1234; myvoid->editFieldPtr()->setValue(&dummy); // attach it to the scene scene->addAttachment(myvoid); // and check if it's still there a = scene->findAttachment(OSG::VoidPAttachment::getClassType()); if(a != NULL) { OSG::VoidPAttachmentRefPtr m = OSG::dynamic_pointer_cast<OSG::VoidPAttachment>(a); SLOG << "Node voidp value: " << *(static_cast<OSG::UInt32 *>(m->getField().getValue())) << OSG::endLog; } else { SLOG << "Node has no voidp attachment!" << OSG::endLog; } // create the SimpleSceneManager helper mgr = new OSG::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; }
// 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 NodePtr 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)); } scene = makeTorus(.5, 2, 16, 16); } else { /* All scene file loading is handled via the SceneFileHandler. */ scene = SceneFileHandler::the().read(argv[1]); } // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); FWARNING(("scene: %p %lu\n", scene.getCPtr(), scene.getCPtr())); // show the whole scene mgr->showAll(); // start the debug tool startSceneGraphViewThread(scene); // GLUT main loop glutMainLoop(); // stop the debug tool stopSceneGraphViewThread(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int doMain(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->setSize( 800, 800 ); gwin->init(); // Create the shader material OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create(); // Read the image for the normal texture OSG::ImageUnrecPtr earth_map_img = OSG::Image::create(); if(!earth_map_img->read("Earth.jpg")) { fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n"); return 1; } OSG::TextureObjChunkUnrecPtr tex_earth = OSG::TextureObjChunk::create(); OSG::TextureEnvChunkUnrecPtr tex_earth_env = OSG::TextureEnvChunk::create(); tex_earth->setImage(earth_map_img); tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_earth->setMagFilter(GL_LINEAR); tex_earth->setWrapS(GL_REPEAT); tex_earth->setWrapT(GL_REPEAT); tex_earth_env->setEnvMode(GL_MODULATE); // Read the image for the normal texture OSG::ImageUnrecPtr earth_night_map_img = OSG::Image::create(); if(!earth_night_map_img->read("EarthNight.jpg")) { fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n"); return 1; } OSG::TextureObjChunkUnrecPtr tex_earth_night = OSG::TextureObjChunk::create(); OSG::TextureEnvChunkUnrecPtr tex_earth_night_env = OSG::TextureEnvChunk::create(); tex_earth_night->setImage(earth_night_map_img); tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_earth_night->setMagFilter(GL_LINEAR); tex_earth_night->setWrapS(GL_REPEAT); tex_earth_night->setWrapT(GL_REPEAT); tex_earth_night_env->setEnvMode(GL_MODULATE); // Read the image for the normal texture OSG::ImageUnrecPtr earth_clouds_map_img = OSG::Image::create(); if(!earth_clouds_map_img->read("EarthClouds.jpg")) { fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n"); return 1; } OSG::TextureObjChunkUnrecPtr tex_earth_clouds = OSG::TextureObjChunk::create(); OSG::TextureEnvChunkUnrecPtr tex_earth_clouds_env = OSG::TextureEnvChunk::create(); tex_earth_clouds->setImage(earth_clouds_map_img); tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR); tex_earth_clouds->setMagFilter(GL_LINEAR); tex_earth_clouds->setWrapS(GL_REPEAT); tex_earth_clouds->setWrapT(GL_REPEAT); tex_earth_clouds_env->setEnvMode(GL_MODULATE); _shl = OSG::SHLChunk::create(); if(!_shl->readVertexProgram("Earth.vp")) fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n"); if(!_shl->readFragmentProgram("Earth.fp")) fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n"); _shl->addUniformVariable("EarthDay", 0); _shl->addUniformVariable("EarthNight", 1); _shl->addUniformVariable("EarthCloudGloss", 2); _shl->addUniformVariable("season", 0.0f); _shl->addUniformVariable("cos_time_0_2PI", -0.406652f); _shl->addUniformVariable("sin_time_0_2PI", -0.913583f); // _shl->setUniformParameter("foo", -0.913583f); cmat->addChunk(_shl); cmat->addChunk(tex_earth); cmat->addChunk(tex_earth_env); cmat->addChunk(tex_earth_night); cmat->addChunk(tex_earth_night_env); cmat->addChunk(tex_earth_clouds); cmat->addChunk(tex_earth_clouds_env); // create root node _scene = OSG::Node::create(); OSG::GeometryUnrecPtr geo = OSG::makeLatLongSphereGeo (100, 100, 1.0); geo->setMaterial(cmat); OSG::NodeUnrecPtr torus = OSG::Node::create(); torus->setCore(geo); // add torus to scene OSG::GroupUnrecPtr group = OSG::Group::create(); _scene->setCore(group); _scene->addChild(torus); // create the SimpleSceneManager helper _mgr = OSG::SimpleSceneManager::create(); // tell the manager what to manage _mgr->setWindow(gwin ); _mgr->setRoot(_scene); // show the whole scene _mgr->showAll(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { printf("Usage: testCGShader <filename.vp> <filename.fp>\n"); if( argc < 3 ) return 0; // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG OSG::GLUTWindowUnrecPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->setSize( 800, 800 ); gwin->init(); // Create the shader material OSG::ChunkMaterialUnrecPtr cmat = OSG::ChunkMaterial::create(); OSG::MaterialChunkUnrecPtr matc = OSG::MaterialChunk::create(); matc->setAmbient(OSG::Color4f(0.1, 0.1, 0.1, 1.0)); matc->setDiffuse(OSG::Color4f(0.3, 0.3, 0.3, 1.0)); matc->setSpecular(OSG::Color4f(0.8, 0.8, 0.8, 1.0)); matc->setShininess(100); matc->setLit(true); OSG::SHLChunkUnrecPtr shl = OSG::SHLChunk::create(); shl->readVertexProgram(argv[1]); shl->readFragmentProgram(argv[2]); cmat->addChunk(shl); // create root node _scene = OSG::Node::create(); // create torus OSG::GeometryUnrecPtr geo = OSG::makeTorusGeo(.8, 1.8, 128, 128); geo->setMaterial(cmat); OSG::NodeUnrecPtr torus = OSG::Node::create(); torus->setCore(geo); // add torus to scene OSG::GroupUnrecPtr group = OSG::Group::create(); _scene->setCore(group); _scene->addChild(torus); // create the SimpleSceneManager helper _mgr = OSG::SimpleSceneManager::create(); // tell the manager what to manage _mgr->setWindow(gwin ); _mgr->setRoot(_scene); /* // create point headlight _mgr->turnHeadlightOff(); NodePtr headlight = _mgr->getHighlight(); PointLightPtr light = PointLight::create(); beginEditCP(light); light->setAmbient (.3, .3, .3, 1); light->setDiffuse ( 1, 1, 1, 1); light->setSpecular ( 1, 1, 1, 1); light->setBeacon (_mgr->getCamera()->getBeacon()); endEditCP(light); beginEditCP(_scene); _scene->setCore(light); endEditCP(_scene); */ // show the whole scene _mgr->showAll(); // GLUT main loop 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; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { printf("Press key '1' or '2' to switch between one and two light sources.\n"); // OSG init OSG::osgInit(argc,argv); globals = new GlobalObjects; // open a new scope, because the pointers below should go out of scope // before entering glutMainLoop. // Otherwise OpenSG will complain about objects being alive after shutdown. { // GLUT init int winid = setupGLUT(&argc, argv); globals->gwin = OSG::GLUTWindow::create(); /* Construct a scene with a ground plane, some objects and two lights. Nothing very interesting at this point. See further down for the part relevant to shadows. */ globals->rootNode = OSG::makeCoredNode<OSG::Group>(); OSG::NodeRefPtr scene = OSG::makeCoredNode<OSG::Group>(); // create lights OSG::TransformRefPtr point1_trans; OSG::NodeRefPtr point1 = OSG::makeCoredNode<OSG::PointLight>(&globals->point1_core); OSG::NodeRefPtr point1_beacon = OSG::makeCoredNode<OSG::Transform >(&point1_trans); point1_trans->editMatrix().setTranslate(0.0, 0.0, 15.0); globals->point1_core->setAmbient(0.15f,0.15f,0.15f,1); globals->point1_core->setDiffuse(0.4f,0.4f,0.4f,1); globals->point1_core->setSpecular(0.0f,0.0f,0.0f,1); globals->point1_core->setBeacon(point1_beacon); globals->point1_core->setOn(true); OSG::TransformRefPtr point2_trans; OSG::NodeRefPtr point2 = OSG::makeCoredNode<OSG::PointLight>(&globals->point2_core); OSG::NodeRefPtr point2_beacon = OSG::makeCoredNode<OSG::Transform >(&point2_trans); point2_trans->editMatrix().setTranslate(2.5, 2.5, 15.0); globals->point2_core->setAmbient(0.15f,0.15f,0.15f,1); globals->point2_core->setDiffuse(0.4f,0.4f,0.4f,1); globals->point2_core->setSpecular(0.0f,0.0f,0.0f,1); globals->point2_core->setBeacon(point2_beacon); globals->point2_core->setOn(true); point1->addChild(point2); point2->addChild(scene ); // create scene // bottom OSG::NodeRefPtr plane = OSG::makePlane(25.0, 25.0, 128, 128); OSG::UChar8 imgdata[] = { 255,0,0, 0,255,0, 0,0,255, 255,255,0 }; OSG::ImageRefPtr plane_img = OSG::Image::create(); plane_img->set(OSG::Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata); OSG::TextureObjChunkRefPtr plane_tex = OSG::TextureObjChunk::create(); plane_tex->setImage(plane_img); plane_tex->setMinFilter(GL_LINEAR); plane_tex->setMagFilter(GL_LINEAR); plane_tex->setWrapS(GL_REPEAT); plane_tex->setWrapT(GL_REPEAT); OSG::TextureEnvChunkRefPtr plane_tex_env = OSG::TextureEnvChunk::create(); plane_tex_env->setEnvMode(GL_MODULATE); OSG::SimpleMaterialRefPtr plane_mat = OSG::SimpleMaterial::create(); plane_mat->setAmbient(OSG::Color3f(0.3f,0.3f,0.3f)); plane_mat->setDiffuse(OSG::Color3f(1.0f,1.0f,1.0f)); plane_mat->addChunk(plane_tex); plane_mat->addChunk(plane_tex_env); OSG::GeometryRefPtr plane_geo = dynamic_cast<OSG::Geometry *>(plane->getCore()); plane_geo->setMaterial(plane_mat); // box OSG::NodeRefPtr box_trans_node = OSG::makeCoredNode<OSG::Transform>(&globals->box_trans); globals->box_trans->editMatrix().setTranslate(0.0, 0.0, 2.0); OSG::NodeRefPtr box = OSG::makeBox(8.0f, 8.0f, 0.8f, 10, 10 , 10); box_trans_node->addChild(box); OSG::SimpleMaterialRefPtr box_mat = OSG::SimpleMaterial::create(); box_mat->setAmbient(OSG::Color3f(0.0f,0.0f,0.0f)); box_mat->setDiffuse(OSG::Color3f(0.0f,0.0f,1.0f)); OSG::GeometryRefPtr box_geo = dynamic_cast<OSG::Geometry *>(box->getCore()); box_geo->setMaterial(box_mat); // cylinder1 OSG::NodeRefPtr cylinder1_trans_node = OSG::makeCoredNode<OSG::Transform>(&globals->cylinder1_trans); globals->cylinder1_trans->editMatrix().setTranslate(0.0, 0.0, 5.0); OSG::NodeRefPtr cylinder1 = OSG::makeCylinder(10.0f, 0.4f, 32, true, true ,true); cylinder1_trans_node->addChild(cylinder1); OSG::SimpleMaterialRefPtr cylinder1_mat = OSG::SimpleMaterial::create(); cylinder1_mat->setAmbient(OSG::Color3f(0.0,0.0,0.0)); cylinder1_mat->setDiffuse(OSG::Color3f(1.0,0.0,0.0)); OSG::GeometryRefPtr cylinder1_geo = dynamic_cast<OSG::Geometry *>(cylinder1->getCore()); cylinder1_geo->setMaterial(cylinder1_mat); // cylinder2 OSG::NodeRefPtr cylinder2_trans_node = OSG::makeCoredNode<OSG::Transform>(&globals->cylinder2_trans); globals->cylinder2_trans->editMatrix().setTranslate(0.0, 0.0, 8.0); OSG::NodeRefPtr cylinder2 = OSG::makeCylinder(10.0f, 0.4f, 32, true, true ,true); cylinder2_trans_node->addChild(cylinder2); OSG::SimpleMaterialRefPtr cylinder2_mat = OSG::SimpleMaterial::create(); cylinder2_mat->setAmbient(OSG::Color3f(0.0,0.0,0.0)); cylinder2_mat->setDiffuse(OSG::Color3f(0.0,1.0,0.0)); OSG::GeometryRefPtr cylinder2_geo = dynamic_cast<OSG::Geometry *>(cylinder2->getCore()); cylinder2_geo->setMaterial(cylinder2_mat); // scene scene->addChild(plane); scene->addChild(box_trans_node); scene->addChild(cylinder1_trans_node); scene->addChild(cylinder2_trans_node); globals->rootNode->addChild(point1); globals->rootNode->addChild(point1_beacon); globals->rootNode->addChild(point2_beacon); globals->gwin->setGlutId(winid); globals->gwin->init(); /* SHADOWS Shadows are implemented as LightEngines (every Light can have one to augment it's regular effect). */ // create the engines OSG::SimpleShadowMapEngineRefPtr ssme1 = OSG::SimpleShadowMapEngine::create(); OSG::SimpleShadowMapEngineRefPtr ssme2 = OSG::SimpleShadowMapEngine::create(); // add them to the light sources globals->point1_core->setLightEngine(ssme1); globals->point2_core->setLightEngine(ssme2); ssme1->setWidth (SM_RESOLUTION); ssme1->setHeight(SM_RESOLUTION); ssme1->setShadowColor(OSG::Color4f(0.1f, 0.1f, 0.1f, 1.0f)); ssme2->setWidth (SM_RESOLUTION); ssme2->setHeight(SM_RESOLUTION); OSG::Vec3f min,max; globals->rootNode->updateVolume(); globals->rootNode->getVolume ().getBounds( min, max ); OSG::commitChanges(); // create the SimpleSceneManager helper globals->mgr = new OSG::SimpleSceneManager; globals->mgr->setWindow(globals->gwin ); globals->mgr->setRoot (globals->rootNode); globals->mgr->turnHeadlightOff(); globals->mgr->showAll(); } // GLUT main loop glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int doMain(int argc, char **argv) { printf("Press key '9' or '0' to toggle light sources.\n"); printf("Set the shadow mode with key '1' ... '8'\n"); printf("Change MapSize with keys 'y' = 512, 'x' = 1024, 'c' = 2048\n"); printf("NOTE: Real point lights only supported for ShadowMode 1...6!\n"); // OSG init OSG::osgInit(argc, argv); // GLUT init int winid = setupGLUT(&argc, argv); gwin = OSG::GLUTWindow::create(); //Erstellen der ben�tigten Komponenten-------------------------------------- rootNode = OSG::makeCoredNode<OSG::Group>(); OSG::NodeUnrecPtr scene = OSG::makeCoredNode<OSG::Group>(); /* // create lights //Directional Light 1 OSG::NodeUnrecPtr light1 = OSG::makeCoredNode<OSG::DirectionalLight>(&_light1_core); OSG::NodeUnrecPtr light1_beacon = OSG::makeCoredNode<OSG::Transform>(&_light1_trans); _light1_trans->editMatrix().setTranslate(0.0, 0.0, 0.0); _light1_core->setDirection(0.8,0.8,0.5); _light1_core->setAmbient(0.15,0.15,0.15,1); _light1_core->setDiffuse(0.5,0.5,0.5,1); _light1_core->setSpecular(0.0,0.0,0.0,1); _light1_core->setBeacon(light1_beacon); _light1_core->setShadowIntensity(0.7); _light1_core->setOn(true); */ // Point Light 1 OSG::NodeUnrecPtr light1 = OSG::makeCoredNode<OSG::PointLight>(&_light1_core); OSG::NodeUnrecPtr light1_beacon = OSG::makeCoredNode<OSG::Transform >(&_light1_trans); _light1_trans->editMatrix().setTranslate(50.0, 50.0, 10.0); _light1_core->setAmbient(0.15f, 0.15f, 0.15f, 1); _light1_core->setDiffuse(0.5f, 0.5f, 0.5f, 1); _light1_core->setSpecular(0.0f, 0.0f, 0.0f, 1); _light1_core->setBeacon(light1_beacon); _light1_core->setOn(true); _light1_core->setShadowIntensity(0.8f); // Spot Light 2 OSG::NodeUnrecPtr light2 = OSG::makeCoredNode<OSG::SpotLight>(&_light2_core); OSG::NodeUnrecPtr light2_beacon = OSG::makeCoredNode<OSG::Transform>(&_light2_trans); //_light2_trans->editMatrix().setTranslate(75.0, 0.0, 25.0); _light2_trans->editMatrix().setTranslate(250.0, -250.0, 300.0); _light2_core->setAmbient(0.15,0.15,0.15,1); _light2_core->setDiffuse(0.5,0.5,0.5,1); _light2_core->setSpecular(0.0,0.0,0.0,1); _light2_core->setSpotCutOffDeg(40.0); _light2_core->setSpotDirection(-0.85,0.85,-1.0); _light2_core->setBeacon(light2_beacon); _light2_core->setShadowIntensity(0.7); _light2_core->setOn(true); /* // Point Light 2 OSG::NodeUnrecPtr light2 = OSG::makeCoredNode<OSG::PointLight>(&_light2_core); OSG::NodeUnrecPtr light2_beacon = OSG::makeCoredNode<OSG::Transform> (&_light2_trans); _light2_trans->editMatrix().setTranslate(40.0, 0.0, 40.0); _light2_core->setAmbient(0.15f, 0.15f, 0.15f, 1); _light2_core->setDiffuse(0.5f, 0.5f, 0.5f, 1); _light2_core->setSpecular(0.0f, 0.0f, 0.0f, 1); _light2_core->setBeacon(light2_beacon); _light2_core->setOn(false); _light2_core->setShadowIntensity(0.7f); */ light1->addChild(light2); light2->addChild(scene); //Eigene Kamera erstellen Pcamera = OSG::PerspectiveCamera::create(); cam_beacon = OSG::makeCoredNode<OSG::Transform>(&cam_trans); cam_trans->editMatrix().setTranslate(0.0, 0.0, 25.0); Pcamera->setBeacon(cam_beacon); Pcamera->setFov(OSG::osgDegree2Rad(60)); Pcamera->setNear(1.0); Pcamera->setFar(1000); // create scene // bottom OSG::NodeUnrecPtr plane = OSG::makePlane(300.0, 300.0, 256, 256); OSG::ImageUnrecPtr plane_img = OSG::Image::create(); plane_img->read("gras.jpg"); OSG::TextureObjChunkUnrecPtr plane_tex_obj = OSG::TextureObjChunk::create(); OSG::TextureEnvChunkUnrecPtr plane_tex_env = OSG::TextureEnvChunk::create(); plane_tex_obj->setImage(plane_img); plane_tex_obj->setMinFilter(GL_LINEAR); plane_tex_obj->setMagFilter(GL_LINEAR); plane_tex_obj->setWrapS(GL_REPEAT); plane_tex_obj->setWrapT(GL_REPEAT); plane_tex_env->setEnvMode(GL_MODULATE); OSG::SimpleMaterialUnrecPtr plane_mat = OSG::SimpleMaterial::create(); plane_mat->setAmbient(OSG::Color3f(0.3f, 0.3f, 0.3f)); plane_mat->setDiffuse(OSG::Color3f(1.0f, 1.0f, 1.0f)); plane_mat->addChunk(plane_tex_obj); plane_mat->addChunk(plane_tex_env); OSG::Geometry *plane_geo = dynamic_cast<OSG::Geometry *>(plane->getCore()); plane_geo->setMaterial(plane_mat); //load Tree Objects OSG::NodeUnrecPtr tree1_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree1_trans); OSG::NodeUnrecPtr tree2_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree2_trans); OSG::NodeUnrecPtr tree3_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree3_trans); OSG::NodeUnrecPtr tree4_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree4_trans); OSG::NodeUnrecPtr tree5_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree5_trans); OSG::NodeUnrecPtr tree6_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree6_trans); OSG::NodeUnrecPtr tree7_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree7_trans); OSG::NodeUnrecPtr tree8_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree8_trans); OSG::NodeUnrecPtr tree9_trans_node = OSG::makeCoredNode<OSG::Transform> (&_tree9_trans); _tree1_trans->editMatrix().setTranslate(-80.0, -80.0, 0.0); _tree1_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree2_trans->editMatrix().setTranslate(0.0, -80.0, 0.0); _tree2_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree3_trans->editMatrix().setTranslate(80.0, -80.0, 0.0); _tree3_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree4_trans->editMatrix().setTranslate(-80.0, 0.0, 0.0); _tree4_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree5_trans->editMatrix().setTranslate(0.0, 0.0, 0.0); _tree5_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree6_trans->editMatrix().setTranslate(80.0, 0.0, 0.0); _tree6_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree7_trans->editMatrix().setTranslate(-80.0, 80.0, 0.0); _tree7_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree8_trans->editMatrix().setTranslate(0.0, 80.0, 0.0); _tree8_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); _tree9_trans->editMatrix().setTranslate(80.0, 80.0, 0.0); _tree9_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0)); OSG::NodeUnrecPtr tree1 = OSG::SceneFileHandler::the()->read("tree1.3ds"); // NodeUnrecPtr tree1 = makeSphere(2, 2.0); tree1_trans_node->addChild(tree1); tree2_trans_node->addChild(cloneTree(tree1)); tree3_trans_node->addChild(cloneTree(tree1)); tree4_trans_node->addChild(cloneTree(tree1)); tree5_trans_node->addChild(cloneTree(tree1)); tree6_trans_node->addChild(cloneTree(tree1)); tree7_trans_node->addChild(cloneTree(tree1)); tree8_trans_node->addChild(cloneTree(tree1)); tree9_trans_node->addChild(cloneTree(tree1)); OSG::NodeUnrecPtr trees = OSG::makeCoredNode<OSG::Group>(); trees->addChild(tree1_trans_node); trees->addChild(tree2_trans_node); trees->addChild(tree3_trans_node); trees->addChild(tree4_trans_node); trees->addChild(tree5_trans_node); trees->addChild(tree6_trans_node); trees->addChild(tree7_trans_node); trees->addChild(tree8_trans_node); trees->addChild(tree9_trans_node); /*//load Airplane Object NodePtr obj1_trans_node = makeCoredNode<Transform>(&_obj1_trans); _obj1_trans->editMatrix().setTranslate(0.0, 0.0, 10.0); _obj1_trans->editMatrix().setScale(Vec3f(0.15,0.15,0.15)); NodePtr object1 = SceneFileHandler::the().read("triplane.3ds"); obj1_trans_node->addChild(object1); */ //Load a Quad as Pointlight OSG::GeometryUnrecPtr boxGeo = OSG::makeBoxGeo(15, 15, 15, 1, 1, 1); OSG::NodeUnrecPtr boxNode = OSG::Node::create(); boxNode->setCore(boxGeo); OSG::SimpleMaterialUnrecPtr box_mat = OSG::SimpleMaterial::create(); box_mat->setAmbient(OSG::Color3f(0.95f, 1.0f, 0.2f)); box_mat->setDiffuse(OSG::Color3f(0.95f, 1.0f, 0.2f)); boxGeo->setMaterial(box_mat); OSG::NodeUnrecPtr obj1_trans_node = OSG::makeCoredNode<OSG::Transform> (&_obj1_trans); obj1_trans_node->addChild(boxNode); //load Dino Objects OSG::NodeUnrecPtr dino1_trans_node = OSG::makeCoredNode<OSG::Transform> (&_dino1_trans); OSG::NodeUnrecPtr dino2_trans_node = OSG::makeCoredNode<OSG::Transform> (&_dino2_trans); OSG::NodeUnrecPtr dino3_trans_node = OSG::makeCoredNode<OSG::Transform> (&_dino3_trans); OSG::NodeUnrecPtr dino4_trans_node = OSG::makeCoredNode<OSG::Transform> (&_dino4_trans); _dino1_trans->editMatrix().setTranslate(-20.0, -20.0, 10.0); _dino1_trans->editMatrix().setScale(OSG::Vec3f(5.0, 5.0, 5.0)); _dino2_trans->editMatrix().setTranslate(-20.0, -20.0, 6.0); _dino2_trans->editMatrix().setScale(OSG::Vec3f(3.0, 3.0, 3.0)); _dino3_trans->editMatrix().setTranslate(-20.0, -20.0, 6.0); _dino3_trans->editMatrix().setScale(OSG::Vec3f(3.0, 3.0, 3.0)); _dino4_trans->editMatrix().setTranslate(-20.0, -20.0, 6.0); _dino4_trans->editMatrix().setScale(OSG::Vec3f(3.0, 3.0, 3.0)); OSG::NodeUnrecPtr dino1 = OSG::SceneFileHandler::the()->read("dinopet.3ds"); // NodeUnrecPtr dino1 = makeBox(2., 2., 2., 4, 4, 4); dino1_trans_node->addChild(dino1); dino2_trans_node->addChild(cloneTree(dino1)); dino3_trans_node->addChild(cloneTree(dino1)); dino4_trans_node->addChild(cloneTree(dino1)); OSG::NodeUnrecPtr dinos = OSG::makeCoredNode<OSG::Group>(); dinos->addChild(dino1_trans_node); dinos->addChild(dino2_trans_node); dinos->addChild(dino3_trans_node); dinos->addChild(dino4_trans_node); //load Stone Objects OSG::TransformUnrecPtr _stone_trans1, _stone_trans2, _stone_trans3, _stone_trans4, _stone_trans5, _stone_trans6, _stone_trans7, _stone_trans8, _stone_trans9; OSG::NodeUnrecPtr stone_trans_node1 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans1); OSG::NodeUnrecPtr stone_trans_node2 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans2); OSG::NodeUnrecPtr stone_trans_node3 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans3); OSG::NodeUnrecPtr stone_trans_node4 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans4); OSG::NodeUnrecPtr stone_trans_node5 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans5); OSG::NodeUnrecPtr stone_trans_node6 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans6); OSG::NodeUnrecPtr stone_trans_node7 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans7); OSG::NodeUnrecPtr stone_trans_node8 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans8); OSG::NodeUnrecPtr stone_trans_node9 = OSG::makeCoredNode<OSG::Transform> (&_stone_trans9); _stone_trans1->editMatrix().setTranslate(-70, -70, 0); _stone_trans2->editMatrix().setTranslate(10, -70, 0); _stone_trans3->editMatrix().setTranslate(90, -70, 0); _stone_trans4->editMatrix().setTranslate(-70, 10, 0); _stone_trans5->editMatrix().setTranslate(10, 10, 0); _stone_trans6->editMatrix().setTranslate(90, 10, 0); _stone_trans7->editMatrix().setTranslate(-70, 90, 0); _stone_trans8->editMatrix().setTranslate(10, 90, 0); _stone_trans9->editMatrix().setTranslate(90, 90, 0); OSG::NodeUnrecPtr stone1 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone2 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone3 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone4 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone5 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone6 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone7 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone8 = OSG::makeSphere(1, 7.0); OSG::NodeUnrecPtr stone9 = OSG::makeSphere(1, 7.0); OSG::ImageUnrecPtr plane_img2 = OSG::Image::create(); plane_img2->read("stone.jpg"); OSG::TextureObjChunkUnrecPtr plane_tex2_obj = OSG::TextureObjChunk::create(); OSG::TextureEnvChunkUnrecPtr plane_tex2_env = OSG::TextureEnvChunk::create(); plane_tex2_obj->setImage(plane_img2); plane_tex2_obj->setMinFilter(GL_LINEAR); plane_tex2_obj->setMagFilter(GL_LINEAR); plane_tex2_obj->setWrapS(GL_REPEAT); plane_tex2_obj->setWrapT(GL_REPEAT); plane_tex2_env->setEnvMode(GL_MODULATE); OSG::SimpleMaterialUnrecPtr plane_mat2 = OSG::SimpleMaterial::create(); plane_mat2->setAmbient(OSG::Color3f(0.3f, 0.3f, 0.3f)); plane_mat2->setDiffuse(OSG::Color3f(1.0f, 1.0f, 1.0f)); plane_mat2->addChunk(plane_tex2_obj); plane_mat2->addChunk(plane_tex2_env); OSG::Geometry *plane_geo3 = dynamic_cast<OSG::Geometry *>(stone1->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone2->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone3->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone4->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone5->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone6->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone7->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone8->getCore()); plane_geo3->setMaterial(plane_mat2); plane_geo3 = dynamic_cast<OSG::Geometry *>(stone9->getCore()); plane_geo3->setMaterial(plane_mat2); stone_trans_node1->addChild(stone1); stone_trans_node2->addChild(stone2); stone_trans_node3->addChild(stone3); stone_trans_node4->addChild(stone4); stone_trans_node5->addChild(stone5); stone_trans_node6->addChild(stone6); stone_trans_node7->addChild(stone7); stone_trans_node8->addChild(stone8); stone_trans_node9->addChild(stone9); OSG::NodeUnrecPtr stones = OSG::makeCoredNode<OSG::Group>(); stones->addChild(stone_trans_node1); stones->addChild(stone_trans_node2); stones->addChild(stone_trans_node3); stones->addChild(stone_trans_node4); stones->addChild(stone_trans_node5); stones->addChild(stone_trans_node6); stones->addChild(stone_trans_node7); stones->addChild(stone_trans_node8); stones->addChild(stone_trans_node9); scene->addChild(plane); scene->addChild(obj1_trans_node); scene->addChild(trees); scene->addChild(stones); scene->addChild(dinos); svp = OSG::ShadowStage::create(); OSG::GradientBackgroundUnrecPtr gbg = OSG::GradientBackground::create(); OSG::SolidBackgroundUnrecPtr sbg = OSG::SolidBackground::create(); gbg->addLine(OSG::Color3f(0.7f, 0.7f, 0.8f), 0); gbg->addLine(OSG::Color3f(0.0f, 0.1f, 0.3f), 1); rootNode->setCore(svp); rootNode->addChild(light1); rootNode->addChild(light1_beacon); rootNode->addChild(light2_beacon); rootNode->addChild(cam_beacon); // Shadow viewport #ifdef SHADOW_CHECK svp->setBackground(gbg); svp->setRoot(rootNode); svp->setSize(0, 0, 1, 1); #endif //svp->setOffFactor(4.0); //svp->setOffBias(8.0); //used to set global shadow intensity, ignores shadow intensity from light sources if != 0.0 //svp->setGlobalShadowIntensity(0.8); svp->setMapSize(1024); //ShadowSmoothness used for PCF_SHADOW_MAP and VARIANCE_SHADOW_MAP, defines Filter Width. Range can be 0.0 ... 1.0. //ShadowSmoothness also used to define the light size for PCSS_SHADOW_MAP svp->setShadowSmoothness(0.5); // add light sources here //svp->editMFLightNodes ()->push_back(light1); //svp->editMFLightNodes ()->push_back(light2); svp->editMFExcludeNodes()->push_back(obj1_trans_node); svp->setAutoSearchForLights(true); //one active light at startup _light2_core->setOn(true); _light2_core->setAmbient(0.3f, 0.3f, 0.3f, 1); _light2_core->setDiffuse(0.8f, 0.8f, 0.8f, 1); _light1_core->setOn(false); _light1_core->setAmbient(0.3, 0.3, 0.3, 1); _light1_core->setDiffuse(0.8, 0.8, 0.8, 1); gwin->setGlutId(winid); #ifdef SHADOW_CHECK gwin->addPort(svp); #endif gwin->init(); OSG::Vec3f min, max; rootNode->updateVolume(); rootNode->getVolume().getBounds(min, max); // create the SimpleSceneManager helper mgr = OSG::SimpleSceneManager::create(); mgr->setWindow(gwin); mgr->setCamera(Pcamera); mgr->setRoot(rootNode); _navigator.setMode(OSG::Navigator::TRACKBALL); #ifdef SHADOW_CHECK _navigator.setViewport(svp); #endif _navigator.setCameraTransformation(cam_beacon); OSG::Vec3f up(0,1,0); OSG::Pnt3f at(0,0,0); OSG::Pnt3f from(0.0f,-100.1f,20.0f); _navigator.set(from, at, up); _navigator.setMotionFactor(0.5f); #ifdef SHADOW_CHECK svp->setCamera(Pcamera); #endif //activate Framecounter startFpsCounter(); // dynamic_cast<RenderAction *>(mgr->getAction())->setLocalLights(true); mgr->turnHeadlightOff(); mgr->showAll(); mgr->getCamera()->setNear( 1.0f); mgr->getCamera()->setFar (1000000.f ); _navigator.setViewport(gwin->getPort(0)); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); { // the connection between GLUT and OpenSG OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // create the scene OSG::NodeRefPtr torus = OSG::makeTorus( .5, 2, 16, 32 ); OSG::NodeRefPtr scene = OSG::Node::create(); trans = OSG::Transform::create(); scene->setCore(trans); scene->addChild(torus); // Create the parts needed for the video background OSG::UInt32 width = 640; OSG::UInt32 height = 480; // get the desired size from the command line if(argc >= 3) { width = atoi(argv[1]); height = atoi(argv[2]); } // To check OpenGL extensions, the Window needs to have run through // frameInit at least once. This automatically happens when rendering, // but we can't wait for that here. gwin->activate (); gwin->frameInit(); // Now we can check for OpenGL extensions hasNPOT = gwin->hasExtension("GL_ARB_texture_non_power_of_two"); // Print what we've got SLOG << "Got " << (isPOT?"":"non-") << "power-of-two images and " << (hasNPOT?"can":"cannot") << " use NPOT textures, changing " << (changeOnlyPart?"part":"all") << " of the screen" << std::endl; // Ok, now for the meat of the code... // first we need an Image to hold the picture(s) to show image = OSG::Image::create(); // set the image's size and type, and allocate memory // this example uses RGB. On some systems (e.g. Windows) BGR // or BGRA might be faster, it depends on how the images are // acquired image->set(OSG::Image::OSG_RGB_PF, width, height); // Now create the texture to be used for the background texObj = OSG::TextureObjChunk::create(); // Associate image and texture texObj->setImage(image); // Set filtering modes. LINEAR is cheap and good if the image size // changes very little (i.e. the window is about the same size as // the images). texObj->setMinFilter(GL_LINEAR); texObj->setMagFilter(GL_LINEAR); // Set the wrapping modes. We don't need repetition, it might actually // introduce artifactes at the borders, so switch it off. texObj->setWrapS(GL_CLAMP_TO_EDGE); texObj->setWrapT(GL_CLAMP_TO_EDGE); // Newer versions of OpenGL can handle NPOT textures directly. // OpenSG will do that internally automatically. // // Older versions need POT textures. By default OpenSG // will scale an NPOT texture to POT while defining it. // For changing textures that's too slow. // So tell OpenSG not to scale the image and adjust the texture // coordinates used by the TextureBackground (see below). texObj->setScale(false); // Create the background OSG::TextureBackgroundRefPtr back = OSG::TextureBackground::create(); // Set the texture to use back->setTexture(texObj); // if the image is NPOT and we don't have hardware support for it // adjust the texture coordinates. if(isPOT == false && hasNPOT == false) { OSG::UInt32 potWidth = OSG::osgNextPower2(width ); OSG::UInt32 potHeight = OSG::osgNextPower2(height); OSG::Real32 tcRight = OSG::Real32(width ) / OSG::Real32(potWidth ); OSG::Real32 tcTop = OSG::Real32(height) / OSG::Real32(potHeight); back->editMFTexCoords()->push_back(OSG::Vec2f( 0.f, 0.f)); back->editMFTexCoords()->push_back(OSG::Vec2f(tcRight, 0.f)); back->editMFTexCoords()->push_back(OSG::Vec2f(tcRight, tcTop)); back->editMFTexCoords()->push_back(OSG::Vec2f( 0.f, tcTop)); } OSG::commitChanges(); // create the SimpleSceneManager helper mgr = OSG::SimpleSceneManager::create(); // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); mgr->setStatistics(true); // replace the background // This has to be done after the viewport has been created, which the // SSM does in setRoot(). OSG::ViewportRefPtr vp = gwin->getPort(0); vp->setBackground(back); } // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }