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; }
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; }