void key(unsigned char key, int , int) { switch ( key ) { case 27: exit(0); case 'a': UChar8 imgdata[32]; for ( int i = 0; i < 32; i++ ) imgdata[i] = static_cast<UChar8>(rand()); pImage->set( Image::OSG_RGB_PF, 2, 2, 2, 1, 1, 0, imgdata ); xchunk1->imageContentChanged(); break; case 'b': { UChar8 imgdata[16]; for ( int i = 0; i < 16; i++ ) imgdata[i] = static_cast<UChar8>(rand()); pImage->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata ); beginEditCP( xchunk1 ); xchunk1->setImage( pImage ); endEditCP( xchunk1 ); } break; case 'c': { beginEditCP( xchunk1 ); xchunk1->setMinFilter( GL_LINEAR_MIPMAP_LINEAR ); xchunk1->setMagFilter( GL_LINEAR ); endEditCP( xchunk1 ); } break; case 'd': { beginEditCP( xchunk1 ); xchunk1->setMinFilter( GL_NEAREST ); xchunk1->setMagFilter( GL_NEAREST ); endEditCP( xchunk1 ); } break; case 'e': { UChar8 imgdata[16]; for ( int i = 0; i < 16; i++ ) imgdata[i] = static_cast<UChar8>(rand()); pImage->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata ); xchunk1->imageContentChanged(1,1,1,1); } break; case 'f': { UChar8 imgdata[16]; for ( int i = 0; i < 16; i++ ) imgdata[i] = static_cast<UChar8>(rand()); pImage->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata ); xchunk1->imageContentChanged(0,1,0,0); } break; } }
void CharacterModel::convertMaterials(std::string configfile) { getMaterials().clear(); UInt32 mcnt = 0; PathHandler ph; ph.setBaseFile(configfile.c_str()); for(int mid = 0; mid < _coreModel->getCoreMaterialCount(); mid++) { CalCoreMaterial *coremat = _coreModel->getCoreMaterial(mid); SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); CalCoreMaterial::Color &calamb = coremat->getAmbientColor(); CalCoreMaterial::Color &caldif = coremat->getDiffuseColor(); CalCoreMaterial::Color &calspec = coremat->getSpecularColor(); mat->setAmbient(Color3f(calamb.red / 255.0f, calamb.green / 255.0f, calamb.blue / 255.0f)); mat->setDiffuse(Color3f(caldif.red / 255.0f, caldif.green / 255.0f, caldif.blue / 255.0f)); mat->setSpecular(Color3f(calspec.red / 255.0f, calspec.green / 255.0f, calspec.blue / 255.0f)); mat->setShininess(coremat->getShininess() * 100.f); mat->setLit(true); mat->setColorMaterial(GL_NONE); for(int mapId = 0; mapId < coremat->getMapCount(); mapId++) { std::string file = coremat->getMapFilename(mapId); std::string pfile = ph.findFile(file.c_str()); SINFO << "Loading texture '" << pfile << "'..." << endLog; ImagePtr img = Image::create(); if(!img->read(pfile.c_str())) { SWARNING << "CharacterModel::convertMaterials: error " << "loading image " << file << endLog; } else { // amz with my test scene paladin.cfg all textures were // upside down so I disabled the vertical flipping perhaps // they fixed the bug in Cal3D? #if 0 beginEditCP(img); { // For some reason Cal3D expects textures upside down ??? UInt32 bpl = img->getBpp() * img->getWidth(); UChar8 *t = img->getData(), *b = t + (img->getHeight() - 1) * bpl, dum; for(UInt32 y = img->getHeight() / 2; y > 0; --y) { for(UInt32 x = bpl; x > 0; --x, ++t, ++b) { dum = *t; *t = *b; *b = dum; } b -= bpl * 2; } } endEditCP(img); #endif TextureChunkPtr tex = TextureChunk::create(); beginEditCP(tex); tex->setImage(img); tex->setEnvMode(GL_MODULATE); endEditCP(tex); mat->addChunk(tex); } } endEditCP(mat); coremat->setUserData((Cal::UserData)mcnt); getMaterials().push_back(mat); mcnt ++; } }
int main(int argc, char **argv) { osgInit(argc,argv); // GLUT init glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); PassiveWindowPtr pwin=PassiveWindow::create(); pwin->init(); // create the texture tx1 = TextureChunk::create(); const UInt16 width = 16, height = 1; ImagePtr pImg1 = Image::create(); pImg1->set(Image::OSG_RGB_PF, width, height ); beginEditCP(pImg1); UInt8 *d = pImg1->editData(); for(UInt16 y = 0; y < height; ++y) { for(UInt16 x = 0; x < width; ++x) { *d++ = static_cast<UInt8>(x * 255.f / width); *d++ = static_cast<UInt8>(y * 255.f / height); *d++ = static_cast<UInt8>(128); } } endEditCP(pImg1); beginEditCP(tx1); tx1->setImage(pImg1); tx1->setMinFilter(GL_NEAREST); tx1->setMagFilter(GL_NEAREST); tx1->setWrapS(GL_REPEAT); tx1->setWrapT(GL_REPEAT); endEditCP(tx1); tg = TexGenChunk::create(); beginEditCP(tg); tg->setGenFuncS(GL_EYE_LINEAR); tg->setGenFuncSPlane(Vec4f(0,.15,0,0)); endEditCP(tg); // create the material SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setDiffuse(Color3f(1,1,1)); mat->setLit(false); mat->addChunk(tx1); mat->addChunk(tg); endEditCP(mat); // create the scene NodePtr torus = makeTorus( .5, 2, 16, 32 ); GeometryPtr geo = GeometryPtr::dcast(torus->getCore()); beginEditCP(geo, Geometry::MaterialFieldMask); geo->setMaterial(mat); endEditCP(geo, Geometry::MaterialFieldMask); transn1 = makeCoredNode<Transform>(&trans1); beginEditCP(transn1, Node::CoreFieldMask | Node::ChildrenFieldMask); { transn1->addChild(torus); } endEditCP (transn1, Node::CoreFieldMask | Node::ChildrenFieldMask); transn2 = makeCoredNode<Transform>(&trans2); NodePtr scene = makeCoredNode<Group>(); beginEditCP(scene); scene->addChild(transn1); scene->addChild(transn2); endEditCP(scene); beginEditCP(tg); tg->setSBeacon(torus); endEditCP(tg); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // create the window and initial camera/viewport mgr->setWindow(pwin ); // tell the manager what to manage mgr->setRoot (scene); // show the whole scene mgr->showAll(); mgr->redraw(); webInterface = new WebInterface(); webInterface->setRoot(scene); // GLUT main loop glutMainLoop(); return 0; }
TextureChunkPtr vtkOsgConverter::CreateTexture(vtkTexture* vtkTexture) { if(_verbose) std::cout << "Calling CreateTexture()" << std::endl; // if(! m_bTextureHasChanged) // { // if (_verbose) // { // std::cout << " ... nothing to do" << std::endl; // std::cout << " End CreateTexture()" << std::endl; // } // //can we check if the actual image has been updated, even if the texture is the same? // return; // } // else if(m_pvtkTexture == NULL) // { // if (_verbose) // { // std::cout << " ... texture is (still ?) NULL" << std::endl; // std::cout << " EndCreateTexture()" << std::endl; // } // //the texture has changed but it is NULL now. We should remove the texture from the material // return; // } // m_bTextureHasChanged = false; vtkImageData* imgData = vtkTexture->GetInput(); int iImgDims[3]; imgData->GetDimensions(iImgDims); vtkPointData* imgPointData = imgData->GetPointData(); vtkCellData* imgCellData = imgData->GetCellData(); vtkDataArray* data = NULL; if (imgPointData != NULL) if (NULL != imgPointData->GetScalars()) { data = imgPointData->GetScalars(); if (_verbose) std::cout << " Found texture data in point data" << std::endl; } if (imgCellData != NULL) if (NULL != imgCellData->GetScalars()) { data = imgCellData->GetScalars(); if (_verbose) std::cout << " Found texture data in cell data" << std::endl; } if (data == NULL) { std::cout << " could not load texture data" << std::endl; return NullFC; } int iImgComps = data->GetNumberOfComponents(); int iImgPixels = data->GetNumberOfTuples(); if (iImgPixels != (iImgDims[0] * iImgDims[1] * iImgDims[2])) { std::cout << "Number of pixels in data array seems to be wrong!" << std::endl; return NullFC; } UInt8* newImageData = new UInt8[iImgDims[0] * iImgDims[1] * iImgDims[2] * iImgComps]; vtkUnsignedCharArray* oldImageUChar = NULL; oldImageUChar = dynamic_cast<vtkUnsignedCharArray*>(data); int ucharCounter = 0; if (oldImageUChar != NULL) for (int i = 0; i < iImgPixels; i++) { unsigned char pixel[4]; oldImageUChar->GetTupleValue(i, pixel); for (int j = 0; j < iImgComps; j++) newImageData[ucharCounter + j] = pixel[j]; ucharCounter += iImgComps; } else std::cout << "Pixel data come in unsupported vtk type" << std::endl; ImagePtr osgImage = Image::create(); beginEditCP(osgImage); { osgImage->setWidth(iImgDims[0]); osgImage->setHeight(iImgDims[1]); osgImage->setDepth(iImgDims[2]); osgImage->setDataType(Image::OSG_UINT8_IMAGEDATA); if (iImgComps == 1) osgImage->setPixelFormat(Image::OSG_L_PF); else if (iImgComps == 2) osgImage->setPixelFormat(Image::OSG_LA_PF); else if (iImgComps == 3) osgImage->setPixelFormat(Image::OSG_RGB_PF); else if (iImgComps == 4) osgImage->setPixelFormat(Image::OSG_RGBA_PF); else { std::cout << "Unsupported image type!" << std::endl; delete [] newImageData; return NullFC; } osgImage->setData(newImageData); } endEditCP(osgImage); TextureChunkPtr osgTextureChunk = TextureChunk::create(); beginEditCP(osgTextureChunk); { osgTextureChunk->setImage(osgImage); } endEditCP(osgTextureChunk); if (_verbose) { std::cout << " Loading image with " << iImgDims[0] << " x " << iImgDims[1] << " x " << iImgDims[2] << " pixels." << std::endl; std::cout << " Components: " << iImgComps << std::endl; std::cout << "End CreateTexture()" << std::endl; } return osgTextureChunk; }
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(); }
int main(int argc, char **argv) { osgInit(argc,argv); // GLUT init glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); int id=glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); GLUTWindowPtr gwin=GLUTWindow::create(); gwin->setId(id); gwin->init(); // create the scene // NodePtr scene = makeTorus(.5, 2, 16, 16); NodePtr scene = makeBox(1,1,1, 1,1,1); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); // Create the textured material UChar8 imgdata[] = { 255,0,0,128, 0,255,0,128, 0,0,255,255, 255,255,255,255 }; ImagePtr img = Image::create(); beginEditCP(img); if(argc > 1) img->read(argv[1]); else img->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata ); endEditCP(img); addRefCP(img); SimpleTexturedMaterialPtr mat = SimpleTexturedMaterial::create(); beginEditCP(mat); mat->setLit(false); mat->setDiffuse(Color3f(0,1,0)); mat->setImage(img); mat->setMinFilter(GL_NEAREST); mat->setMagFilter(GL_NEAREST); mat->setEnvMode(GL_REPLACE); endEditCP(mat); // Add the polygon foregrounds Pnt2f pos[][4] = { { Vec2f(.4,.4), Vec2f(.6,.4), Vec2f(.6,.6), Vec2f(.4,.6) }, { Vec2f(0,0), Vec2f(1,0), Vec2f(1,.1), Vec2f(0,.1) }, { Vec2f(1,-100), Vec2f(-2,-100), Vec2f(-2,-2), Vec2f(1,-2) }, { Vec2f(-1000,-1000), Vec2f(0,0), Vec2f(0,0), Vec2f(0,0) } }; bool normx[] = { true, true, false }; bool normy[] = { true, true, false }; for(UInt16 ind = 0; pos[ind][0][0] != -1000; ++ind) { PolygonForegroundPtr pg = PolygonForeground::create(); beginEditCP(pg); pg->setMaterial(mat); pg->editMFTexCoords()->push_back(Vec3f( 0.,0.,0.)); pg->editMFPositions()->push_back(pos[ind][0]); pg->editMFTexCoords()->push_back(Vec3f( 1.,0.,0.)); pg->editMFPositions()->push_back(pos[ind][1]); pg->editMFTexCoords()->push_back(Vec3f( 1.,1.,0.)); pg->editMFPositions()->push_back(pos[ind][2]); pg->editMFTexCoords()->push_back(Vec3f( 0.,1.,0.)); pg->editMFPositions()->push_back(pos[ind][3]); pg->setNormalizedX(normx[ind]); pg->setNormalizedY(normy[ind]); endEditCP(pg); // take the viewport ViewportPtr vp = gwin->getPort(0); beginEditCP(vp); vp->editMFForegrounds()->push_back(pg); endEditCP (vp); } // Create the edgeblend foreground const int blendpixel = 50; UChar8 ebimgdata[] = { 0,0,0,0, 0,0,0,255 }; ImagePtr ebimg = Image::create(); beginEditCP(ebimg); ebimg->set( Image::OSG_RGBA_PF, 2, 1, 1, 1, 1, 0, ebimgdata ); endEditCP(ebimg); addRefCP(ebimg); TextureChunkPtr tc = TextureChunk::create(); beginEditCP(tc); tc->setImage(ebimg); tc->setMinFilter(GL_NEAREST); tc->setMagFilter(GL_LINEAR); tc->setEnvMode(GL_REPLACE); tc->setWrapS(GL_CLAMP); endEditCP(tc); BlendChunkPtr bl = BlendChunk::create(); beginEditCP(bl); bl->setSrcFactor(GL_SRC_ALPHA); bl->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); endEditCP(bl); ChunkMaterialPtr ebmat = ChunkMaterial::create(); beginEditCP(ebmat); ebmat->addChunk(tc); ebmat->addChunk(bl); endEditCP(ebmat); PolygonForegroundPtr ebpg = PolygonForeground::create(); beginEditCP(ebpg); ebpg->setMaterial(ebmat); ebpg->editMFTexCoords()->push_back(Vec3f( 0.25,0.,0.)); ebpg->editMFTexCoords()->push_back(Vec3f( 0.75,0.,0.)); ebpg->editMFTexCoords()->push_back(Vec3f( 0.75,1.,0.)); ebpg->editMFTexCoords()->push_back(Vec3f( 0.25,1.,0.)); ebpg->editMFPositions()->push_back(Pnt2f(-blendpixel-1, 0)); ebpg->editMFPositions()->push_back(Pnt2f( -1, 0)); ebpg->editMFPositions()->push_back(Pnt2f( -1, 1)); ebpg->editMFPositions()->push_back(Pnt2f(-blendpixel-1, 1)); ebpg->setNormalizedX(false); ebpg->setNormalizedY(true); endEditCP(ebpg); // take the viewport ViewportPtr vp = gwin->getPort(0); beginEditCP(vp); vp->editMFForegrounds()->push_back(ebpg); endEditCP (vp); // 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); gwin = GLUTWindow::create(); // create root rootNode = makeCoredNode<Group>(); NodePtr scene = makeCoredNode<Group>(); // create lights TransformPtr point1_trans; NodePtr point1 = makeCoredNode<PointLight>(&_point1_core); NodePtr point1_beacon = makeCoredNode<Transform>(&point1_trans); beginEditCP(point1_trans); point1_trans->editMatrix().setTranslate(0.0, 0.0, 25.0); endEditCP(point1_trans); beginEditCP(_point1_core); _point1_core->setAmbient(0.15,0.15,0.15,1); _point1_core->setDiffuse(0.4,0.4,0.4,1); _point1_core->setSpecular(0.0,0.0,0.0,1); _point1_core->setBeacon(point1_beacon); _point1_core->setOn(true); endEditCP(_point1_core); TransformPtr point2_trans; NodePtr point2 = makeCoredNode<PointLight>(&_point2_core); NodePtr point2_beacon = makeCoredNode<Transform>(&point2_trans); beginEditCP(point2_trans); point2_trans->editMatrix().setTranslate(5.0, 5.0, 20.0); endEditCP(point2_trans); beginEditCP(_point2_core); _point2_core->setAmbient(0.15,0.15,0.15,1); _point2_core->setDiffuse(0.4,0.4,0.4,1); _point2_core->setSpecular(0.0,0.0,0.0,1); _point2_core->setBeacon(point2_beacon); _point2_core->setOn(true); endEditCP(_point2_core); beginEditCP(point1); point1->addChild(point2); endEditCP(point1); beginEditCP(point2); point2->addChild(scene); endEditCP(point2); // create scene // bottom NodePtr plane = makePlane(25.0, 25.0, 128, 128); int size = imageWinWidth*imageWinHeight*256; ImagePtr plane_img = Image::create(); beginEditCP(plane_img); plane_img->set(Image::OSG_RGBA_PF, imageWinWidth, imageWinHeight, 1, 1, 1, 0, NULL); endEditCP(plane_img); TextureChunkPtr plane_tex = TextureChunk::create(); beginEditCP(plane_tex); plane_tex->setImage(plane_img); plane_tex->setMinFilter(GL_LINEAR); plane_tex->setMagFilter(GL_LINEAR); plane_tex->setTarget(GL_TEXTURE_2D); plane_tex->setInternalFormat(GL_RGBA16F_ARB); endEditCP(plane_tex); SHLChunkPtr shl = SHLChunk::create(); beginEditCP(shl); shl->setVertexProgram(_vp_program); shl->setFragmentProgram(_fp_program); shl->setUniformParameter("tex0", 0); endEditCP(shl); SimpleMaterialPtr plane_mat = SimpleMaterial::create(); beginEditCP(plane_mat); plane_mat->setAmbient(Color3f(0.3,0.3,0.3)); plane_mat->setDiffuse(Color3f(1.0,1.0,1.0)); plane_mat->addChunk(plane_tex); plane_mat->addChunk(shl); endEditCP(plane_mat); GeometryPtr plane_geo = GeometryPtr::dcast(plane->getCore()); beginEditCP(plane_geo); plane_geo->setMaterial(plane_mat); beginEditCP(plane_geo); // box box_trans_node = makeCoredNode<Transform>(&_box_trans); beginEditCP(_box_trans); _box_trans->editMatrix().setTranslate(0.0, 0.0, 12.0); endEditCP(_box_trans); NodePtr box = makeBox(4.0, 4.0, 0.8, 10, 10 , 10); beginEditCP(box_trans_node); box_trans_node->addChild(box); endEditCP(box_trans_node); PolygonChunkPtr pchunk = osg::PolygonChunk::create(); beginEditCP(pchunk); pchunk->setCullFace(GL_BACK); endEditCP(pchunk); SimpleMaterialPtr box_mat = SimpleMaterial::create(); beginEditCP(box_mat); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,0.0,1.0)); box_mat->addChunk(pchunk); endEditCP(box_mat); GeometryPtr box_geo = GeometryPtr::dcast(box->getCore()); beginEditCP(box_geo); box_geo->setMaterial(box_mat); beginEditCP(box_geo); // cylinder1 NodePtr cylinder1_trans_node = makeCoredNode<Transform>(&_cylinder1_trans); beginEditCP(_cylinder1_trans); _cylinder1_trans->editMatrix().setTranslate(0.0, 0.0, 5.0); endEditCP(_cylinder1_trans); NodePtr cylinder1 = OSG::makeCylinder(10.0, 0.4, 32, true, true ,true); beginEditCP(cylinder1_trans_node); cylinder1_trans_node->addChild(cylinder1); endEditCP(cylinder1_trans_node); SimpleMaterialPtr cylinder1_mat = SimpleMaterial::create(); beginEditCP(cylinder1_mat); cylinder1_mat->setAmbient(Color3f(0.0,0.0,0.0)); cylinder1_mat->setDiffuse(Color3f(1.0,0.0,0.0)); cylinder1_mat->addChunk(pchunk); endEditCP(cylinder1_mat); GeometryPtr cylinder1_geo = GeometryPtr::dcast(cylinder1->getCore()); beginEditCP(cylinder1_geo); cylinder1_geo->setMaterial(cylinder1_mat); beginEditCP(cylinder1_geo); // cylinder2 NodePtr cylinder2_trans_node = makeCoredNode<Transform>(&_cylinder2_trans); beginEditCP(_cylinder2_trans); _cylinder2_trans->editMatrix().setTranslate(0.0, 0.0, 8.0); endEditCP(_cylinder2_trans); NodePtr cylinder2 = OSG::makeCylinder(10.0, 0.4, 32, true, true ,true); beginEditCP(cylinder2_trans_node); cylinder2_trans_node->addChild(cylinder2); endEditCP(cylinder2_trans_node); SimpleMaterialPtr cylinder2_mat = SimpleMaterial::create(); beginEditCP(cylinder2_mat); cylinder2_mat->setAmbient(Color3f(0.0,0.0,0.0)); cylinder2_mat->setDiffuse(Color3f(0.0,1.0,0.0)); cylinder2_mat->addChunk(pchunk); endEditCP(cylinder2_mat); GeometryPtr cylinder2_geo = GeometryPtr::dcast(cylinder2->getCore()); beginEditCP(cylinder2_geo); cylinder2_geo->setMaterial(cylinder2_mat); beginEditCP(cylinder2_geo); // scene beginEditCP(scene); scene->addChild(plane); scene->addChild(box_trans_node); scene->addChild(cylinder1_trans_node); scene->addChild(cylinder2_trans_node); endEditCP(scene); vp = ShadowViewport::create(); GradientBackgroundPtr gbg = GradientBackground::create(); SolidBackgroundPtr sbg = SolidBackground::create(); UChar8 imgdata[] = { 255,0,0, 0,255,0, 0,0,255, 255,255,0 }; ImagePtr img1 = Image::create(); img1->set(Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata); TextureChunkPtr tcPtr = TextureChunk::create(); beginEditCP(tcPtr); tcPtr->setImage(img1); endEditCP(tcPtr); TextureBackgroundPtr bkg = TextureBackground::create(); beginEditCP(bkg); bkg->setTexture(tcPtr); endEditCP(bkg); beginEditCP(sbg); sbg->setColor(Color3f(0.2,0.4,0.6)); endEditCP(sbg); beginEditCP(gbg); gbg->addLine(Color3f(0.7, 0.7, 0.8), 0); gbg->addLine(Color3f(0.1, 0.1, 0.3), 1); endEditCP(gbg); beginEditCP(rootNode); rootNode->addChild(point1); rootNode->addChild(point1_beacon); rootNode->addChild(point2_beacon); endEditCP(rootNode); //FBOViewportPtr fbo_vp = FBOViewport::create(); addRefCP(fbo_vp); // the Camera for the map cam = PerspectiveCamera::create(); beginEditCP(cam); cam->setFov(osgdegree2rad(90)); cam->setAspect(1); cam->setNear(0.001); cam->setFar(10000); cam->setBeacon(box_trans_node); endEditCP(cam); beginEditCP(fbo_vp); fbo_vp->setBackground(bkg); fbo_vp->setRoot(rootNode); fbo_vp->setCamera(cam); fbo_vp->setSize(0,0,imageWinWidth-1, imageWinHeight-1); fbo_vp->setStorageWidth(imageWinWidth); fbo_vp->setStorageHeight(imageWinHeight); fbo_vp->setDirty(true); fbo_vp->editMFTextures ()->push_back(plane_tex); fbo_vp->editMFExcludeNodes()->push_back(plane); fbo_vp->setFboOn(true); endEditCP(fbo_vp); // normal shadow viewport beginEditCP(vp); vp->setBackground(gbg); vp->setRoot(rootNode); vp->setSize(0,0,1,1); endEditCP(vp); beginEditCP(gwin); //Window gwin->setId(winid); gwin->addPort(vp); gwin->init(); endEditCP(gwin); Vec3f min,max; rootNode->updateVolume(); rootNode->getVolume().getBounds( min, max ); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; mgr->setWindow(gwin); mgr->setRoot(rootNode); //Viewport beginEditCP(vp); vp->setCamera(mgr->getCamera()); endEditCP(vp); mgr->turnHeadlightOff(); mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }
FBOViewportPtr createSceneFBO(void) { //Create Camera Beacon Matrix CameraMat; CameraMat.setTranslate(0.0f,0.0f,4.0f); TransformPtr CameraBeconCore = Transform::create(); beginEditCP(CameraBeconCore, Transform::MatrixFieldMask); CameraBeconCore->setMatrix(CameraMat); endEditCP(CameraBeconCore, Transform::MatrixFieldMask); NodePtr CameraBeconNode = Node::create(); beginEditCP(CameraBeconNode, Node::CoreFieldMask); CameraBeconNode->setCore(CameraBeconCore); endEditCP(CameraBeconNode, Node::CoreFieldMask); //Create Camera PerspectiveCameraPtr TheCamera = PerspectiveCamera::create(); beginEditCP(TheCamera); TheCamera->setFov(deg2rad(60.0f)); TheCamera->setAspect(1.0f); TheCamera->setNear(0.1f); TheCamera->setFar(100.0f); TheCamera->setBeacon(CameraBeconNode); endEditCP(TheCamera); //Make the Material BlinnMaterialPtr TheMaterial = BlinnMaterial::create(); beginEditCP(TheMaterial); TheMaterial->setDiffuse(0.8); TheMaterial->setColor(Color3f(1.0,1.0,1.0)); TheMaterial->setAmbientColor(Color3f(1.0,1.0,1.0)); TheMaterial->setNumLights(1); endEditCP(TheMaterial); // Make Torus Node (creates Torus in background of scene) NodePtr TorusGeometryNode = makeTorus(.5, 2, 24, 48); beginEditCP(TorusGeometryNode->getCore()); GeometryPtr::dcast(TorusGeometryNode->getCore())->setMaterial(TheMaterial); endEditCP(TorusGeometryNode->getCore()); calcVertexNormals(GeometryPtr::dcast(TorusGeometryNode->getCore())); calcVertexTangents(GeometryPtr::dcast(TorusGeometryNode->getCore()),0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId); RootTransformCore = Transform::create(); NodePtr TorusTransformNode = Node::create(); beginEditCP(TorusTransformNode, Node::CoreFieldMask); TorusTransformNode->setCore(RootTransformCore); TorusTransformNode->addChild(TorusGeometryNode); endEditCP(TorusTransformNode, Node::CoreFieldMask); //Create Light Beacon Matrix LightMat; LightMat.setTranslate(0.0f,10.0f,1.0f); TransformPtr LightBeconCore = Transform::create(); beginEditCP(LightBeconCore, Transform::MatrixFieldMask); LightBeconCore->setMatrix(LightMat); endEditCP(LightBeconCore, Transform::MatrixFieldMask); NodePtr LightBeconNode = Node::create(); beginEditCP(LightBeconNode, Node::CoreFieldMask); LightBeconNode->setCore(LightBeconCore); endEditCP(LightBeconNode, Node::CoreFieldMask); //Create Light TheLight = PointLight::create(); beginEditCP(TheLight); TheLight->setBeacon(LightBeconNode); endEditCP(TheLight); NodePtr LightNode = Node::create(); beginEditCP(LightNode, Node::CoreFieldMask); LightNode->setCore(TheLight); LightNode->addChild(TorusTransformNode); endEditCP(LightNode, Node::CoreFieldMask); //Create Root NodePtr TheRoot = Node::create(); beginEditCP(TheRoot); TheRoot->setCore(Group::create()); TheRoot->addChild(CameraBeconNode); TheRoot->addChild(LightNode); TheRoot->addChild(LightBeconNode); endEditCP(TheRoot); //Create Background SolidBackgroundPtr TheBackground = SolidBackground::create(); TheBackground->setColor(Color3f(1.0,0.0,0.0)); //DepthClearBackgroundPtr TheBackground = DepthClearBackground::create(); //Create the Image ImagePtr TheColorImage = Image::create(); TheColorImage->set(Image::OSG_RGB_PF,2,2,1,1,1,0.0f,0,Image::OSG_FLOAT16_IMAGEDATA); //Create the texture TextureChunkPtr TheColorTextureChunk = TextureChunk::create(); beginEditCP(TheColorTextureChunk); TheColorTextureChunk->setImage(TheColorImage); TheColorTextureChunk->setMinFilter(GL_NEAREST); TheColorTextureChunk->setMagFilter(GL_NEAREST); TheColorTextureChunk->setWrapS(GL_CLAMP_TO_EDGE); TheColorTextureChunk->setWrapR(GL_CLAMP_TO_EDGE); TheColorTextureChunk->setScale(false); TheColorTextureChunk->setNPOTMatrixScale(true); TheColorTextureChunk->setEnvMode(GL_REPLACE); TheColorTextureChunk->setInternalFormat(GL_RGB16F); endEditCP(TheColorTextureChunk); //Create FBO FBOViewportPtr TheFBO = FBOViewport::create(); beginEditCP(TheFBO); TheFBO->setBackground(TheBackground); TheFBO->setRoot(TheRoot); TheFBO->setCamera(TheCamera); TheFBO->setEnabled(true); TheFBO->getTextures().push_back(TheColorTextureChunk); TheFBO->setStorageWidth(TheColorTextureChunk->getImage()->getWidth()); TheFBO->setStorageHeight(TheColorTextureChunk->getImage()->getHeight()); TheFBO->setSize(0,0,TheColorTextureChunk->getImage()->getWidth()-1, TheColorTextureChunk->getImage()->getHeight()-1); endEditCP(TheFBO); return TheFBO; }
int main( int argc, char *argv[] ) { osgInit(argc, argv); glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("OpenSG"); glutKeyboardFunc(key); // glutReshapeFunc(resize); glutDisplayFunc(display); // glutMouseFunc(mouse); // glutMotionFunc(motion); glutIdleFunc(display); pImage = Image::create(); // create the dummy structures // the window is needed for the chunks that access GLObjects win = GLUTWindow::create(); win->frameInit(); // test for preliminary calls not messing up GLexts win->init(); dact = DrawAction::create(); dact->setWindow(get_pointer(win)); win->init(); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, 1, 0.1, 10 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 3, 3, 3, 0, 0, 0, 0, 1, 0 ); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); dlid = glGenLists( 1 ); glNewList( dlid, GL_COMPILE ); glutSolidSphere( .8, 24, 24 ); glEndList(); dlid2 = glGenLists( 1 ); glNewList( dlid2, GL_COMPILE ); glBegin( GL_POLYGON ); glNormal3f( 0, 1, 0 ); glColor3f( 1, 1, 1 ); glTexCoord3f( 0, 0, 0 ); glVertex3f( -1.5, -1, -1.5 ); glTexCoord3f( 2, 0, 0 ); glVertex3f( 1.5, -1, -1.5 ); glTexCoord3f( 2, 2, 2 ); glVertex3f( 1.5, -1, 1.5 ); glTexCoord3f( 0, 2, 2 ); glVertex3f( -1.5, -1, 1.5 ); glEnd(); glEndList(); Matrix m; tchunk1 = TransformChunk::create(); m.setTranslate( 0, 1, 0 ); tchunk1->setMatrix( m ); tchunk2 = TransformChunk::create(); tchunk2->setMatrix( Matrix::identity() ); mchunk1 = MaterialChunk::create(); mchunk1->setDiffuse( Color4f( 1,0,0,0 ) ); mchunk1->setAmbient( Color4f( 1,0,0,0 ) ); mchunk1->setShininess( 20 ); mchunk2 = MaterialChunk::create(); mchunk2->setDiffuse( Color4f( 0,1,0,0 ) ); mchunk2->setAmbient( Color4f( 0,1,0,0 ) ); mchunk2->setShininess( 50 ); // Texture chunk // UChar8 imgdata[] = // { 255,0,0,0, 0,255,0,0, 0,0,255,255, 255,255,255,255 }; UChar8 imgdata[] = { 255,0,0, 255,0,0, 255,0,255, 255,0,0, 255,0,0, 255,0,255, 255,255,0, 255,255,0, 255,255,255, 255,255,0, 255,255,0, 255,255,255, }; // UChar8 limgdata[] = // { 0, 128, 64, 255 }; pImage->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata ); if ( argc > 1 ) pImage->read( argv[1] ); xchunk1 = TextureChunk::create(); beginEditCP(xchunk1); xchunk1->setImage( pImage ); xchunk1->setMinFilter( GL_LINEAR ); xchunk1->setMagFilter( GL_NEAREST ); xchunk1->setWrapS( GL_REPEAT ); xchunk1->setWrapT( GL_REPEAT ); xchunk1->setEnvMode( GL_REPLACE ); xchunk1->setScale( false ); endEditCP(xchunk1); xchunk1->imageContentChanged(); beginEditCP(xchunk1); xchunk1->setImage( pImage ); xchunk1->setLodBias( 10 ); endEditCP(xchunk1); UChar8 imgdata2[] = { 255,0,0, 0,255,0, 0,0,255, 255,255,255 }; ImagePtr pImage2 = Image::create(); pImage2->set(Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata2); xchunk2 = TextureChunk::create(); beginEditCP(xchunk2); xchunk2->setImage( pImage2 ); xchunk2->setMinFilter( GL_LINEAR ); xchunk2->setMagFilter( GL_NEAREST ); xchunk2->setWrapS( GL_REPEAT ); xchunk2->setWrapT( GL_REPEAT ); xchunk2->setEnvMode( GL_MODULATE ); xchunk2->setLodBias( 10 ); endEditCP(xchunk2); // Cube Texture chunk UChar8 negz[] = { 255,0,0, 128,0,0, 64,0,0, 255,255,255 }, posz[] = { 0,255,0, 0,128,0, 0,64,0, 255,255,255 }, negy[] = { 0,0,255, 0,0,128, 0,0,64, 255,255,255 }, posy[] = { 255,255,0, 128,128,0, 64,64,0, 255,255,255 }, negx[] = { 255,0,255, 128,0,128, 64,0,64, 255,255,255 }, posx[] = { 0,255,255, 0,128,128, 0,64,64, 255,255,255 }; ImagePtr inegz = Image::create(); ImagePtr iposz = Image::create(); ImagePtr inegy = Image::create(); ImagePtr iposy = Image::create(); ImagePtr inegx = Image::create(); ImagePtr iposx = Image::create(); inegz->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, negz ); iposz->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, posz ); inegy->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, negy ); iposy->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, posy ); inegx->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, negx ); iposx->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, posx ); cchunk1 = CubeTextureChunk::create(); beginEditCP(cchunk1); cchunk1->setImage( inegz ); cchunk1->setPosZImage( iposz ); cchunk1->setPosYImage( iposy ); cchunk1->setNegYImage( inegy ); cchunk1->setPosXImage( iposx ); cchunk1->setNegXImage( inegx ); cchunk1->setMinFilter( GL_LINEAR ); cchunk1->setMagFilter( GL_NEAREST ); cchunk1->setWrapS( GL_REPEAT ); cchunk1->setWrapT( GL_REPEAT ); cchunk1->setEnvMode( GL_MODULATE ); cchunk1->setLodBias( 10 ); endEditCP(cchunk1); gchunk1 = TexGenChunk::create(); beginEditCP(gchunk1); gchunk1->setGenFuncS(GL_REFLECTION_MAP_ARB); gchunk1->setGenFuncT(GL_REFLECTION_MAP_ARB); gchunk1->setGenFuncR(GL_REFLECTION_MAP_ARB); endEditCP(gchunk1); // blend chunk blchunk = BlendChunk::create(); #ifndef WIN32 // blchunk->setSrcFactor( GL_CONSTANT_ALPHA ); // blchunk->setDestFactor( GL_ONE_MINUS_CONSTANT_ALPHA ); #endif blchunk->setColor( Color4f( 1,1,1,0.1 ) ); // texture transform chunk txchunk = TextureTransformChunk::create(); beginEditCP(txchunk); // txchunk->setMatrix( Matrix(4,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1) ); endEditCP(txchunk); glutMainLoop(); return 0; }
MaterialPtr PerformerLoader::traverseGState(NodePtr node, pfGeoState *gstate) { FDEBUG(("PerformerLoader::traverseGState: traversing %p\n", gstate)); if(_materials.find(gstate) != _materials.end()) { FDEBUG(("PfL:traverseGState: found in matmap.\n")); return _materials[gstate]; } ChunkMaterialPtr mat = ChunkMaterial::create(); beginEditCP(mat); uint64_t inherit; inherit = gstate->getInherit(); static int pcolormode[] = { PFMTL_CMODE_AMBIENT_AND_DIFFUSE, PFMTL_CMODE_AMBIENT, PFMTL_CMODE_DIFFUSE, PFMTL_CMODE_EMISSION, PFMTL_CMODE_SPECULAR, PFMTL_CMODE_OFF, -1 }; static int ocolormode[] = { GL_AMBIENT_AND_DIFFUSE, GL_AMBIENT, GL_DIFFUSE, GL_EMISSION, GL_SPECULAR, GL_NONE }; pair<void *, void*> key; if((inherit & (PFSTATE_FRONTMTL | PFSTATE_BACKMTL)) != (PFSTATE_FRONTMTL | PFSTATE_BACKMTL) ) { MaterialChunkPtr matc; key.first = NULL; if (!(inherit & PFSTATE_FRONTMTL)) key.first = gstate->getAttr(PFSTATE_FRONTMTL); key.second = NULL; if (!(inherit & PFSTATE_BACKMTL)) key.second = gstate->getAttr(PFSTATE_BACKMTL); if(_chunks.find(key) != _chunks.end()) { matc = MaterialChunkPtr::dcast(_chunks[key]); } else { matc = MaterialChunk::create(); beginEditCP(matc); if (!(inherit & PFSTATE_FRONTMTL)) { pfMaterial *mat = (pfMaterial*)gstate->getAttr(PFSTATE_FRONTMTL); float r,g,b,a; a = mat->getAlpha(); mat->getColor(PFMTL_AMBIENT, &r, &g, &b); matc->setAmbient(Color4f(r,g,b,0)); mat->getColor(PFMTL_DIFFUSE, &r, &g, &b); matc->setDiffuse(Color4f(r,g,b,a)); mat->getColor(PFMTL_SPECULAR, &r, &g, &b); matc->setSpecular(Color4f(r,g,b,0)); mat->getColor(PFMTL_EMISSION, &r, &g, &b); matc->setEmission(Color4f(r,g,b,0)); matc->setShininess(mat->getShininess()); int cm = mat->getColorMode(PFMTL_FRONT); UInt32 om = GL_NONE; for (UInt16 i = 0; pcolormode[i] != -1; ++i) if(pcolormode[i] == cm) om = ocolormode[i]; matc->setColorMaterial(om); } if (!(inherit & PFSTATE_BACKMTL)) { pfMaterial *mat = (pfMaterial*)gstate->getAttr(PFSTATE_BACKMTL); matc->setBackMaterial(true); float r,g,b,a; a = mat->getAlpha(); mat->getColor(PFMTL_AMBIENT, &r, &g, &b); matc->setBackAmbient(Color4f(r,g,b,0)); mat->getColor(PFMTL_DIFFUSE, &r, &g, &b); matc->setBackDiffuse(Color4f(r,g,b,a)); mat->getColor(PFMTL_SPECULAR, &r, &g, &b); matc->setBackSpecular(Color4f(r,g,b,0)); mat->getColor(PFMTL_EMISSION, &r, &g, &b); matc->setBackEmission(Color4f(r,g,b,0)); matc->setBackShininess(mat->getShininess()); int cm = mat->getColorMode(PFMTL_FRONT); UInt32 om = GL_NONE; for (UInt16 i = 0; pcolormode[i] != -1; ++i) if(pcolormode[i] == cm) om = ocolormode[i]; matc->setBackColorMaterial(om); } endEditCP(matc); } mat->addChunk(matc); } if((inherit & (PFSTATE_TEXTURE | PFSTATE_TEXENV)) != (PFSTATE_TEXTURE | PFSTATE_TEXENV) ) { TextureChunkPtr texc; for(int t = 0; t < PF_MAX_TEXTURES; ++t) { if(gstate->getMultiAttr(PFSTATE_TEXTURE, t) == NULL) continue; key.first = NULL; if (!(inherit & PFSTATE_TEXTURE) && gstate->getMultiAttr(PFSTATE_TEXTURE, t)) key.first = gstate->getMultiAttr(PFSTATE_TEXTURE, t); key.second = NULL; if (!(inherit & PFSTATE_TEXENV) && gstate->getMultiAttr(PFSTATE_TEXENV, t)) key.second = gstate->getMultiAttr(PFSTATE_TEXENV, t); if(_chunks.find(key) != _chunks.end()) { texc = TextureChunkPtr::dcast(_chunks[key]); } else { texc = TextureChunk::create(); beginEditCP(texc); if (!(inherit & PFSTATE_TEXTURE)) { pfTexture *tex = (pfTexture*)gstate->getMultiAttr(PFSTATE_TEXTURE, t); unsigned int* pdata; int pf, w, h, d, type, comp, sides; UInt32 intformat = 0, extformat = 0; tex->getImage(&pdata, &comp, &w, &h, &d); sides = tex->getFormat(PFTEX_CUBE_MAP) ? 6 : 1; pf = tex->getFormat(PFTEX_IMAGE_FORMAT); if(pf == 0x7fff) { switch(comp) { case 1: pf = Image::OSG_L_PF; break; case 2: pf = Image::OSG_LA_PF; break; case 3: pf = Image::OSG_RGB_PF; break; case 4: pf = Image::OSG_RGBA_PF; break; } } switch(tex->getFormat(PFTEX_EXTERNAL_FORMAT)) { case PFTEX_PACK_8: type = Image::OSG_UINT8_IMAGEDATA; break; case PFTEX_PACK_16: type = Image::OSG_UINT16_IMAGEDATA; break; default: type = Image::OSG_UINT8_IMAGEDATA; FWARNING(("PerformerLoader::traverseGState: " "Unknown tex format %d!\n", tex->getFormat(PFTEX_EXTERNAL_FORMAT))); extformat = tex->getFormat(PFTEX_EXTERNAL_FORMAT); break; } intformat = tex->getFormat(PFTEX_INTERNAL_FORMAT); ImagePtr img = Image::create(); beginEditCP(img); img->set(pf, w, h, d, 1, 1, 0, NULL, type, 1, sides); if(sides == 1) { memcpy(img->getData(), pdata, img->getSize()); } else { FWARNING(("PerformerLoader::traverseGState: " "CubeTex not impl yet!\n")); } endEditCP(img); texc->setImage(img); texc->setInternalFormat(intformat); texc->setWrapS(tex->getRepeat(PFTEX_WRAP_S)); texc->setWrapT(tex->getRepeat(PFTEX_WRAP_T)); texc->setWrapR(tex->getRepeat(PFTEX_WRAP_R)); static int ptexfilter[] = { PFTEX_POINT, PFTEX_LINEAR, PFTEX_BILINEAR, PFTEX_TRILINEAR, PFTEX_QUADLINEAR, PFTEX_MIPMAP_POINT, PFTEX_MIPMAP_LINEAR, PFTEX_MIPMAP_BILINEAR, PFTEX_MIPMAP_TRILINEAR, PFTEX_MIPMAP_QUADLINEAR, -1 }; static int otexfilter[] = { GL_NEAREST, GL_LINEAR, GL_LINEAR, GL_LINEAR, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR }; int ptf = tex->getFilter(PFTEX_MINFILTER); UInt32 otf = GL_NONE; for (UInt16 i = 0; ptexfilter[i] != -1; ++i) if(ptexfilter[i] == ptf) otf = otexfilter[i]; texc->setMinFilter(otf); ptf = tex->getFilter(PFTEX_MAGFILTER); otf = GL_NONE; for (UInt16 i = 0; ptexfilter[i] != -1; ++i) if(ptexfilter[i] == ptf) otf = otexfilter[i]; texc->setMagFilter(otf); } if (!(inherit & PFSTATE_TEXENV)) { pfTexEnv *te = (pfTexEnv*)gstate->getMultiAttr(PFSTATE_TEXENV, t); if(te != NULL) { texc->setEnvMode(te->getMode()); float r,g,b,a; te->getBlendColor(&r,&g,&b,&a); texc->setEnvColor(Color4f(r,g,b,a)); } } endEditCP(texc); } mat->addChunk(texc); } // for t } /* Unhandled: lightmodel, lights, fog, texgen, texlod, vtxprog, fragprog, gprogparms, colortable, highlight, lpointstate, shadprog */ endEditCP(mat); _materials[gstate] = mat; return mat; }
int main (int argc, char **argv) { // GLUT init osgInit(argc, argv); osgLog().setLogLevel ( OSG::LOG_DEBUG ); FieldContainerPtr pProto = Geometry::getClassType().getPrototype(); GeometryPtr pGeoProto = GeometryPtr::dcast(pProto); if(pGeoProto != NullFC) { pGeoProto->setDlistCache(true); } glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); UInt32 id = glutCreateWindow("OpenSG"); glutKeyboardFunc(key); glutReshapeFunc(resize); glutDisplayFunc(display); // glutMouseFunc(mouse); // glutMotionFunc(motion); glutIdleFunc(display); // create a material (need that to test textures) ChunkMaterialPtr mat; beginEditCP(mat); mat = ChunkMaterial::create(); MaterialChunkPtr mc = MaterialChunk::create(); beginEditCP(mc); mc->setDiffuse( Color4f( 1,.8,.8,1 ) ); mc->setAmbient( Color4f( 0.1,0.1,0.1,1 ) ); mc->setSpecular( Color4f( 1,1,1,1 ) ); mc->setShininess( 20 ); mc->setBackMaterial(true); mc->setBackColorMaterial(GL_DIFFUSE); mc->setBackDiffuse( Color4f( 1,0,0,1 ) ); mc->setBackAmbient( Color4f( 0.1,0.1,0.1,1 ) ); mc->setBackSpecular( Color4f( 0,1,0,1 ) ); mc->setBackShininess( 10 ); mc->setLit(true); endEditCP(mc); mat->addChunk(mc); // Texture chunk UChar8 imgdata[] = { 255,0,0,128, 0,255,0,128, 0,0,255,255, 255,255,255,255 }; ImagePtr pImage = Image::create(); pImage->set( Image::OSG_RGBA_PF, 2, 2, 1, 1, 1, 0, imgdata ); if ( argc > 1 ) pImage->read( argv[1] ); TextureChunkPtr xchunk; xchunk = TextureChunk::create(); xchunk->setImage( pImage ); xchunk->setMinFilter( GL_NEAREST ); xchunk->setMagFilter( GL_NEAREST ); xchunk->setWrapS( GL_REPEAT ); xchunk->setWrapT( GL_REPEAT ); xchunk->setEnvMode( GL_MODULATE ); mat->addChunk( xchunk ); endEditCP(mat); objects[0] = makePolygon(ccwSquare, sizeof(ccwSquare)/sizeof(double[3])); objects[1] = makePolygon(ccwSquare, sizeof(ccwSquare)/sizeof(double[3])); objects[2] = makePolygon(star, sizeof(star)/sizeof(double[3])); objects[3] = makePolygon(star, sizeof(star)/sizeof(double[3])); objects[4] = makePolygon(cwSquare, sizeof(cwSquare)/sizeof(double[3])); objects[5] = makePolygon(cwSquare, sizeof(cwSquare)/sizeof(double[3])); objects[6] = makePolygon(doubleEight, sizeof(doubleEight)/sizeof(double[3])); objects[7] = makePolygon(doubleEight, sizeof(doubleEight)/sizeof(double[3])); //tesselate every second object for(int i = 1; i < nobjects; i+=2) { GeometryPtr::dcast(objects[i]->getCore())->setMaterial( mat ); std::cerr << "Polygon Node: " << std::hex << objects[i] << std::endl; // try to create convex primitives OSG::GeometryPtr pGeo = GeometryPtr::dcast(objects[i]->getCore()); std::cerr << "Tesselating polygon : " << i << std::endl; createConvexPrimitives(pGeo); } // normal material SimpleMaterialPtr nmat; nmat = SimpleMaterial::create(); beginEditCP(nmat); nmat->setEmission( Color3f( 0,1,0 ) ); endEditCP(nmat); for ( UInt16 i = 0; i < nobjects; i++ ) { normalobjects[i] = calcVertexNormalsGeo( GeometryPtr::dcast(objects[i]->getCore()), .5); GeometryPtr::dcast(normalobjects[i]->getCore())->setMaterial(nmat); } // // The action win = GLUTWindow::create(); win->setId(id); win->init(); glEnable( GL_LIGHT0 ); float p[4]={0,0,1,0}; glLightfv(GL_LIGHT0, GL_POSITION, p); float c[4]={1,1,1,1}; glLightfv(GL_LIGHT0, GL_DIFFUSE, c); glLightfv(GL_LIGHT0, GL_SPECULAR, c); glPointSize( 3 ); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glClearColor( .3, .3, .8, 1 ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, 1, 0.1, 10 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 3, 3, 3, 0, 0, 0, 0, 0, 1 ); dact = DrawAction::create(); dact->setWindow(get_pointer(win)); dact->setFrustumCulling( false ); glutMainLoop(); return 0; }
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); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(MainWindow); // Make Torus Node (creates Torus in background of scene) NodePtr TorusGeometryNode = makeTorus(.5, 2, 16, 16); // Make Main Scene Node and add the Torus NodePtr scene = osg::Node::create(); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); scene->setCore(osg::Group::create()); scene->addChild(TorusGeometryNode); endEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); //Create the Image Path ImagePath("./Data/TutorialImage.jpg"); ImagePtr TheImage = ImageFileHandler::the().read(ImagePath.string().c_str()); //Create the texture TextureChunkPtr TheTextureChunk = TextureChunk::create(); beginEditCP(TheTextureChunk); TheTextureChunk->setImage(TheImage); TheTextureChunk->setMinFilter(GL_NEAREST); TheTextureChunk->setMagFilter(GL_NEAREST); TheTextureChunk->setWrapS(GL_CLAMP_TO_EDGE); TheTextureChunk->setWrapR(GL_CLAMP_TO_EDGE); TheTextureChunk->setScale(false); TheTextureChunk->setNPOTMatrixScale(true); TheTextureChunk->setEnvMode(GL_REPLACE); endEditCP(TheTextureChunk); //Create a Texture Source TextureSourceTextureFilterPtr TutorialTextureSourceTextureFilter = TextureSourceTextureFilter::create(); beginEditCP(TutorialTextureSourceTextureFilter, TextureSourceTextureFilter::TextureFieldMask); TutorialTextureSourceTextureFilter->setTexture(TheTextureChunk); endEditCP(TutorialTextureSourceTextureFilter, TextureSourceTextureFilter::TextureFieldMask); //Create a Grayscale filter std::string GrayScaleFragProg = "uniform sampler2D Slot0Texture; void main() { gl_FragColor = vec4(vec3( dot(vec3(0.3,0.59,0.11), texture2D(Slot0Texture,gl_TexCoord[0].st).rgb)), 1.0); }"; //Create a shader Filter ShaderTextureFilterPtr GrayscaleTextureFilter = ShaderTextureFilter::create(); GrayscaleTextureFilter->attachSource(TutorialTextureSourceTextureFilter, 0, 0); GrayscaleTextureFilter->setFragmentSource(GrayScaleFragProg); //Create a Color Mult filter std::string ColorMultFragProg = "uniform sampler2D Slot0Texture; void main() { gl_FragColor = vec4(vec3(1.0,0.0,0.0) * texture2D(Slot0Texture,gl_TexCoord[0].st).rgb, 1.0); }"; //Create a shader Filter ShaderTextureFilterPtr ColorMultTextureFilter = ShaderTextureFilter::create(); ColorMultTextureFilter->attachSource(GrayscaleTextureFilter,0,0); ColorMultTextureFilter->setFragmentSource(ColorMultFragProg); ////Create a Blur filter //std::string BlurFragProg = ""; //BlurFragProg += //"uniform sampler2D Slot0Texture;" //"void main()" //"{" //" vec2 offsets[9];" //" offsets[0] = vec2(-0.000625,0.00111111111);" //" offsets[1] = vec2(0.0,0.00111111111);" //" offsets[2] = vec2(0.000625,0.00111111111);" //" offsets[3] = vec2(-0.000625,0.0);" //" offsets[4] = vec2(0.0,0.0);" //" offsets[5] = vec2(0.0,0.0);" //" offsets[6] = vec2(-0.000625,-0.00111111111);" //" offsets[7] = vec2(0.0,-0.00111111111);" //" offsets[8] = vec2(0.000625,-0.00111111111);" //" vec4 kernel[9];" ////" kernel[0] = vec4(0.0);" ////" kernel[1] = vec4(0.0);" ////" kernel[2] = vec4(0.0);" ////" kernel[3] = vec4(0.0);" ////" kernel[4] = vec4(1.0);" ////" kernel[5] = vec4(0.0);" ////" kernel[6] = vec4(0.0);" ////" kernel[7] = vec4(0.0);" ////" kernel[8] = vec4(0.0);" //" kernel[0] = vec4(0.0);" //" kernel[1] = vec4(0.15);" //" kernel[2] = vec4(0.0);" //" kernel[3] = vec4(0.15);" //" kernel[4] = vec4(0.4);" //" kernel[5] = vec4(0.15);" //" kernel[6] = vec4(0.0);" //" kernel[7] = vec4(0.15);" //" kernel[8] = vec4(0.0);" //" vec4 sum = vec4(0.0);" //" int i;" //" for(i = 0 ; i < 9 ; i++)" //" {" //" sum += kernel[i] * texture2D(Slot0Texture,gl_TexCoord[0].st + offsets[i]);" //" }" //" gl_FragColor = sum;" //"}"; ////Create a shader Filter //ShaderTextureFilterPtr BlurTextureFilter = ShaderTextureFilter::create(); //BlurTextureFilter->attachSource(ColorMultTextureFilter); //BlurTextureFilter->setFragmentSource(BlurFragProg); // Create the ImageProcessed Foreground Object ImageProcessedForegroundPtr TutorialImageProcessedForeground = ImageProcessedForeground::create(); beginEditCP(TutorialImageProcessedForeground, ImageProcessedForeground::FilterFieldMask | ImageProcessedForeground::OutputSlotFieldMask); TutorialImageProcessedForeground->setFilter(ColorMultTextureFilter); TutorialImageProcessedForeground->setOutputSlot(0); endEditCP(TutorialImageProcessedForeground, ImageProcessedForeground::FilterFieldMask | ImageProcessedForeground::OutputSlotFieldMask); mgr->setRoot(scene); // Add the ImageProcessed Foreground Object to the Scene ViewportPtr TutorialViewport = mgr->getWindow()->getPort(0); beginEditCP(TutorialViewport, Viewport::ForegroundsFieldMask); TutorialViewport->getForegrounds().push_back(TutorialImageProcessedForeground); beginEditCP(TutorialViewport, Viewport::ForegroundsFieldMask); // Show the whole Scene mgr->showAll(); //Open Window Vec2f WinSize(TutorialWindowEventProducer->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindowEventProducer->getDesktopSize() - WinSize) *0.5); TutorialWindowEventProducer->openWindow(WinPos, WinSize, "02SimpleImagePipeline"); //Enter main Loop TutorialWindowEventProducer->mainLoop(); osgExit(); return 0; }
int main( int argc, char *argv[] ) { osgInit(argc, argv); glutInit(&argc, argv); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("OpenSG"); glutKeyboardFunc(key); // glutReshapeFunc(resize); glutDisplayFunc(display); // glutMouseFunc(mouse); // glutMotionFunc(motion); glutIdleFunc(display); pImage = Image::create(); // create the dummy structures // the window is needed for the chunks that access GLObjects win = GLUTWindow::create(); win->frameInit(); // test for preliminary calls not messing up GLexts win->init(); dact = DrawAction::create(); dact->setWindow(get_pointer(win)); win->init(); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, 1, 0.1, 10 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 3, 3, 3, 0, 0, 0, 0, 1, 0 ); glEnable( GL_DEPTH_TEST ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); dlid = glGenLists( 1 ); glNewList( dlid, GL_COMPILE ); glutSolidSphere( .8, 8, 8 ); glEndList(); dlid2 = glGenLists( 1 ); glNewList( dlid2, GL_COMPILE ); glBegin( GL_POLYGON ); glNormal3f( 0, 1, 0 ); glColor3f( 1, 1, 1 ); glTexCoord2f( 0, 0 ); glVertex3f( -1.5, -1, -1.5 ); glTexCoord2f( 2, 0 ); glVertex3f( 1.5, -1, -1.5 ); glTexCoord2f( 2, 2 ); glVertex3f( 1.5, -1, 1.5 ); glTexCoord2f( 0, 2 ); glVertex3f( -1.5, -1, 1.5 ); glEnd(); glEndList(); Matrix m; tchunk1 = TransformChunk::create(); m.setTranslate( 0, 1, 0 ); tchunk1->setMatrix( m ); tchunk2 = TransformChunk::create(); tchunk2->setMatrix( Matrix::identity() ); mchunk1 = MaterialChunk::create(); mchunk1->setDiffuse( Color4f( 1,0,0,0 ) ); mchunk1->setAmbient( Color4f( 1,0,0,0 ) ); mchunk1->setShininess( 20 ); mchunk2 = MaterialChunk::create(); mchunk2->setDiffuse( Color4f( 0,1,0,0 ) ); mchunk2->setAmbient( Color4f( 0,1,0,0 ) ); mchunk2->setShininess( 50 ); // Texture chunk // UChar8 imgdata[] = // { 255,0,0,0, 0,255,0,0, 0,0,255,255, 255,255,255,255 }; UChar8 imgdata[] = { 255,0,0, 255,0,0, 255,0,255, 255,0,0, 255,0,0, 255,0,255, 255,255,0, 255,255,0, 255,255,255, 255,255,0, 255,255,0, 255,255,255, }; // UChar8 limgdata[] = // { 0, 128, 64, 255 }; pImage->set( Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata ); if ( argc > 1 ) pImage->read( argv[1] ); xchunk1 = TextureChunk::create(); beginEditCP(xchunk1); xchunk1->setImage( pImage ); // NOTE: the image is NOT copied, the variable // needs to be kept around as long as the // texture is used xchunk1->setMinFilter( GL_LINEAR ); xchunk1->setMagFilter( GL_NEAREST ); xchunk1->setWrapS( GL_REPEAT ); xchunk1->setWrapT( GL_REPEAT ); xchunk1->setEnvMode( GL_REPLACE ); xchunk1->setEnvColor( Color4f(.5,.5,.5,0) ); xchunk1->setScale( false ); // xchunk1->setShaderOperation(GL_PASS_THROUGH_NV); endEditCP(xchunk1); xchunk1->imageContentChanged(); beginEditCP(xchunk1); xchunk1->setImage( pImage ); endEditCP(xchunk1); // blend chunk blchunk = BlendChunk::create(); #ifdef GL_EXT_blend_color blchunk->setSrcFactor( GL_CONSTANT_ALPHA ); blchunk->setDestFactor( GL_ONE_MINUS_CONSTANT_ALPHA ); #endif blchunk->setColor( Color4f( .5,.5,.5,0.1 ) ); blchunk->setEquation(GL_FUNC_SUBTRACT); std::cout << "BlendChunk is trans:" << blchunk->isTransparent() << std::endl; // texture transform chunk txchunk = TextureTransformChunk::create(); beginEditCP(txchunk); txchunk->setMatrix( Matrix(4,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1) ); endEditCP(txchunk); // polygon chunk pchunk1 = PolygonChunk::create(); { UInt32 stipple[32] = { 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff, 0xffff0000, 0x0000ffff }; pchunk1->editMFStipple()->clear(); for ( int i = 0; i < 32; i++ ) pchunk1->editMFStipple()->push_back( stipple[i] ); } pchunk1->setFrontMode(GL_LINE); pchunk1->setBackMode(GL_FILL); pchunk2 = PolygonChunk::create(); { UInt32 stipple[32] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }; pchunk2->editMFStipple()->clear(); for ( int i = 0; i < 32; i++ ) pchunk2->editMFStipple()->push_back( stipple[i] ); } lichunk1 = LineChunk::create(); lichunk1->setSmooth(true); lichunk1->setStipplePattern(0xf0f0); lichunk2 = LineChunk::create(); lichunk2->setStippleRepeat(5); lichunk2->setStipplePattern(0xaaaa); glutMainLoop(); return 0; }
// Initialize GLUT & OpenSG and set up the scene 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); //Add Window Listener 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 a SphereNode for the point light LambertMaterialPtr TheLightMat = LambertMaterial::create(); beginEditCP(TheLightMat, LambertMaterial::IncandescenceFieldMask); TheLightMat->setIncandescence(Color3f(1.0,1.0,1.0)); endEditCP(TheLightMat, LambertMaterial::IncandescenceFieldMask); GeometryPtr LightSphereGeo = makeSphereGeo(2,2.0); beginEditCP(LightSphereGeo, Geometry::MaterialFieldMask); LightSphereGeo->setMaterial(TheLightMat); endEditCP (LightSphereGeo, Geometry::MaterialFieldMask); NodePtr LightSphereNode = Node::create(); beginEditCP(LightSphereNode, Node::CoreFieldMask); LightSphereNode->setCore(LightSphereGeo); endEditCP (LightSphereNode, Node::CoreFieldMask); //Create the beacon for the Point Light Matrix ThePointLightMat; ThePointLightMat.setTranslate(Vec3f(0.0,100.0,0.0)); ThePointLightBeaconTransform = Transform::create(); beginEditCP(ThePointLightBeaconTransform); ThePointLightBeaconTransform->setMatrix(ThePointLightMat); endEditCP(ThePointLightBeaconTransform); NodePtr ThePointLightBeaconNode = Node::create(); beginEditCP(ThePointLightBeaconNode); ThePointLightBeaconNode->setCore(ThePointLightBeaconTransform); ThePointLightBeaconNode->addChild(LightSphereNode); endEditCP(ThePointLightBeaconNode); //Set the light properties desired PointLightPtr ThePointLight = PointLight::create(); beginEditCP(ThePointLight); ThePointLight->setAmbient(0.3,0.3,0.3,0.3); ThePointLight->setDiffuse(1.0,1.0,1.0,1.0); ThePointLight->setSpecular(1.0,1.0,1.0,1.0); ThePointLight->setBeacon(ThePointLightBeaconNode); endEditCP(ThePointLight); NodePtr ThePointLightNode = Node::create(); beginEditCP(ThePointLightNode); ThePointLightNode->setCore(ThePointLight); endEditCP(ThePointLightNode); //Set the light properties desired SpotLightPtr TheSpotLight = SpotLight::create(); beginEditCP(TheSpotLight); TheSpotLight->setAmbient(0.3,0.3,0.3,0.3); TheSpotLight->setDiffuse(1.0,1.0,1.0,1.0); TheSpotLight->setSpecular(1.0,1.0,1.0,1.0); TheSpotLight->setBeacon(ThePointLightBeaconNode); TheSpotLight->setDirection(Vec3f(0.0,-1.0,0.0)); TheSpotLight->setSpotExponent(5.0); TheSpotLight->setSpotCutOff(1.1); endEditCP(TheSpotLight); NodePtr TheSpotLightNode = Node::create(); beginEditCP(TheSpotLightNode); TheSpotLightNode->setCore(TheSpotLight); endEditCP(TheSpotLightNode); //Load in the Heightmap Image ImagePtr PerlinNoiseImage = createPerlinImage(Vec2s(256,256), Vec2f(10.0f,10.0f),0.5f,1.0f,Vec2f(0.0f,0.0f),0.25f,6,PERLIN_INTERPOLATE_COSINE,false,Image::OSG_L_PF, Image::OSG_UINT8_IMAGEDATA); TextureChunkPtr TheTextureChunk = TextureChunk::create(); beginEditCP(TheTextureChunk); TheTextureChunk->setImage(PerlinNoiseImage); endEditCP(TheTextureChunk); //Lambert Material LambertMaterialPtr TheLambertMat = LambertMaterial::create(); beginEditCP(TheLambertMat, LambertMaterial::ColorFieldMask | LambertMaterial::AmbientColorFieldMask | LambertMaterial::DiffuseFieldMask | LambertMaterial::NumLightsFieldMask | LambertMaterial::DiffuseTextureFieldMask); TheLambertMat->setColor(Color3f(0.0,1.0,0.0)); TheLambertMat->setAmbientColor(Color3f(1.0,0.0,0.0)); TheLambertMat->setDiffuse(0.5); TheLambertMat->setNumLights(1); endEditCP(TheLambertMat, LambertMaterial::ColorFieldMask | LambertMaterial::AmbientColorFieldMask | LambertMaterial::DiffuseFieldMask | LambertMaterial::NumLightsFieldMask | LambertMaterial::DiffuseTextureFieldMask); //Blinn Material TheBlinnMat = BlinnMaterial::create(); beginEditCP(TheBlinnMat, BlinnMaterial::ColorFieldMask | BlinnMaterial::AmbientColorFieldMask | BlinnMaterial::DiffuseFieldMask | BlinnMaterial::SpecularColorFieldMask | BlinnMaterial::SpecularEccentricityFieldMask | BlinnMaterial::SpecularRolloffFieldMask | BlinnMaterial::DiffuseTextureFieldMask); TheBlinnMat->setColor(Color3f(1.0,0.0,0.0)); TheBlinnMat->setAmbientColor(Color3f(0.0,0.0,0.0)); TheBlinnMat->setSpecularColor(Color3f(0.0,0.0,1.0)); TheBlinnMat->setSpecularEccentricity(0.35); TheBlinnMat->setSpecularRolloff(0.85); TheBlinnMat->setDiffuse(0.65); TheBlinnMat->setDiffuseTexture(TheTextureChunk); endEditCP(TheBlinnMat, BlinnMaterial::ColorFieldMask | BlinnMaterial::AmbientColorFieldMask | BlinnMaterial::DiffuseFieldMask | BlinnMaterial::SpecularColorFieldMask | BlinnMaterial::SpecularEccentricityFieldMask | BlinnMaterial::SpecularRolloffFieldMask | BlinnMaterial::DiffuseTextureFieldMask); //Phong Material Phong2MaterialPtr ThePhongMat = Phong2Material::create(); beginEditCP(ThePhongMat, Phong2Material::ColorFieldMask | Phong2Material::AmbientColorFieldMask | Phong2Material::DiffuseFieldMask | Phong2Material::SpecularColorFieldMask | Phong2Material::SpecularCosinePowerFieldMask); ThePhongMat->setColor(Color3f(1.0,0.0,0.0)); ThePhongMat->setAmbientColor(Color3f(0.0,0.0,0.0)); ThePhongMat->setSpecularColor(Color3f(0.0,0.0,1.0)); ThePhongMat->setSpecularCosinePower(50.0); ThePhongMat->setDiffuse(0.65); endEditCP(ThePhongMat, Phong2Material::ColorFieldMask | Phong2Material::AmbientColorFieldMask | Phong2Material::DiffuseFieldMask | Phong2Material::SpecularColorFieldMask | Phong2Material::SpecularCosinePowerFieldMask); //Anisotropic Material AnisotropicMaterialPtr TheAnisotropicMat = AnisotropicMaterial::create(); beginEditCP(TheAnisotropicMat, AnisotropicMaterial::ColorFieldMask | AnisotropicMaterial::AmbientColorFieldMask | AnisotropicMaterial::DiffuseFieldMask | AnisotropicMaterial::SpecularColorFieldMask | AnisotropicMaterial::SpecularRoughnessFieldMask | AnisotropicMaterial::SpecularFresnelIndexFieldMask | AnisotropicMaterial::SpecularSpreadXFieldMask | AnisotropicMaterial::SpecularSpreadYFieldMask); TheAnisotropicMat->setColor(Color3f(1.0,0.0,0.0)); TheAnisotropicMat->setAmbientColor(Color3f(0.0,0.0,0.0)); TheAnisotropicMat->setDiffuse(0.65); TheAnisotropicMat->setSpecularColor(Color3f(0.0,0.0,1.0)); TheAnisotropicMat->setSpecularRoughness(32.0); TheAnisotropicMat->setSpecularFresnelIndex(0.85); TheAnisotropicMat->setSpecularSpreadX(1.0); TheAnisotropicMat->setSpecularSpreadY(1.0); endEditCP(TheAnisotropicMat, AnisotropicMaterial::ColorFieldMask | AnisotropicMaterial::AmbientColorFieldMask | AnisotropicMaterial::DiffuseFieldMask | AnisotropicMaterial::SpecularColorFieldMask | AnisotropicMaterial::SpecularRoughnessFieldMask | AnisotropicMaterial::SpecularFresnelIndexFieldMask | AnisotropicMaterial::SpecularSpreadXFieldMask | AnisotropicMaterial::SpecularSpreadYFieldMask); PointChunkPtr TempChunk = PointChunk::create(); //addRefCP(TempChunk); //Anisotropic Material TheRampMat = RampMaterial::create(); beginEditCP(TheRampMat); //Color TheRampMat->setRampSource(RampMaterial::RAMP_SOURCE_FACING_ANGLE); TheRampMat->getColors().push_back(Color3f(1.0,0.0,0.0)); TheRampMat->getColorPositions().push_back(0.4); TheRampMat->getColorInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH); TheRampMat->getColors().push_back(Color3f(0.0,1.0,0.0)); TheRampMat->getColorPositions().push_back(1.0); //Transparency TheRampMat->getTransparencies().push_back(Color3f(0.0,0.0,0.0)); TheRampMat->getTransparencyPositions().push_back(0.83); TheRampMat->getTransparencyInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH); TheRampMat->getTransparencies().push_back(Color3f(1.0,1.0,1.0)); TheRampMat->getTransparencyPositions().push_back(1.0); TheRampMat->setAmbientColor(Color3f(0.0,0.0,0.0)); TheRampMat->setSpecularity(1.0); TheRampMat->setSpecularEccentricity(0.8); TheRampMat->getSpecularColors().push_back(Color3f(1.0,1.0,1.0)); TheRampMat->getSpecularColorPositions().push_back(0.95); TheRampMat->getSpecularColorInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH); TheRampMat->getSpecularColors().push_back(Color3f(0.0,0.0,1.0)); TheRampMat->getSpecularColorPositions().push_back(1.0); TheRampMat->getSpecularRolloffs().push_back(1.0); TheRampMat->getExtraChunks().push_back(TempChunk); endEditCP(TheRampMat); //Make the Heightmap Geometry HeightmapGeometryPtr TutorialHeightmapGeo = HeightmapGeometry::create(); beginEditCP(TutorialHeightmapGeo, HeightmapGeometry::HeightImageFieldMask | HeightmapGeometry::DimensionsFieldMask | HeightmapGeometry::SegmentsFieldMask | HeightmapGeometry::ScaleFieldMask | HeightmapGeometry::OffsetFieldMask | HeightmapGeometry::MaterialFieldMask); TutorialHeightmapGeo->setHeightImage(PerlinNoiseImage); TutorialHeightmapGeo->setDimensions(Vec2f(200.0,200.0)); TutorialHeightmapGeo->setSegments(Vec2f(150.0,150.0)); TutorialHeightmapGeo->setScale(30.0); TutorialHeightmapGeo->setOffset(0.0); TutorialHeightmapGeo->setMaterial( TheBlinnMat ); endEditCP(TutorialHeightmapGeo, HeightmapGeometry::HeightImageFieldMask | HeightmapGeometry::DimensionsFieldMask | HeightmapGeometry::SegmentsFieldMask | HeightmapGeometry::ScaleFieldMask | HeightmapGeometry::OffsetFieldMask | HeightmapGeometry::MaterialFieldMask); calcVertexNormals(TutorialHeightmapGeo); calcVertexTangents(TutorialHeightmapGeo,0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId); //Make the Heightmap Node NodePtr TutorialHeightmapNode = Node::create(); beginEditCP(TutorialHeightmapNode, Node::CoreFieldMask); TutorialHeightmapNode->setCore(TutorialHeightmapGeo); endEditCP (TutorialHeightmapNode, Node::CoreFieldMask); //Make a SphereNode GeometryPtr SphereGeo = makeSphereGeo(2,50.0); //GeometryPtr SphereGeo = makeCylinderGeo(50,20.0, 16,true,true,true); beginEditCP(SphereGeo, Geometry::MaterialFieldMask); SphereGeo->setMaterial(TheLambertMat); endEditCP (SphereGeo, Geometry::MaterialFieldMask); calcVertexNormals(SphereGeo); calcVertexTangents(SphereGeo,0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId); NodePtr SphereNode = Node::create(); beginEditCP(SphereNode, Node::CoreFieldMask); SphereNode->setCore(SphereGeo); endEditCP (SphereNode, Node::CoreFieldMask); //Make Main Scene Node NodePtr scene = Node::create(); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); scene->setCore(Group::create()); // add the torus as a child scene->addChild(TutorialHeightmapNode); //scene->addChild(SphereNode); scene->addChild(ThePointLightBeaconNode); endEditCP (scene, Node::CoreFieldMask | Node::ChildrenFieldMask); //Add the scene to the Light Nodes //beginEditCP(ThePointLightNode, Node::ChildrenFieldMask); //ThePointLightNode->addChild(scene); //endEditCP(ThePointLightNode, Node::ChildrenFieldMask); //// tell the manager what to manage //mgr->setRoot (ThePointLightNode); beginEditCP(TheSpotLightNode, Node::ChildrenFieldMask); TheSpotLightNode->addChild(scene); endEditCP(TheSpotLightNode, Node::ChildrenFieldMask); // tell the manager what to manage mgr->setRoot (TheSpotLightNode); mgr->turnHeadlightOff(); // show the whole scene mgr->showAll(); //Open Window Vec2f WinSize(TutorialWindowEventProducer->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindowEventProducer->getDesktopSize() - WinSize) *0.5); TutorialWindowEventProducer->openWindow(WinPos, WinSize, "06Heightmap"); //Main Loop TutorialWindowEventProducer->mainLoop(); osgExit(); return 0; }