MaterialTransitPtr OctreeVisualization::createMatFilled(OctreePtr tree, const Octree::OTNodePtr node, const Color3f& CoolColor, const Color3f& HotColor, Real32 Alpha, BlendChunk* BaseBlendChunk, PolygonChunk* BasePolygonChunk ) { //Calculate the Color //Real32 t = static_cast<Real32>(node->getDepth())/static_cast<Real32>(tree->getDepth()); //Color3f NodeColor(t*HotColor + (1.0f-t)*CoolColor); Color3f NodeColor; Color4f NodeColorWithAlpha; if(node->getContainsObstacles()) { NodeColor = HotColor; NodeColorWithAlpha.setValuesRGBA(NodeColor.red(),NodeColor.green(),NodeColor.blue(),0.55); } else { NodeColor = CoolColor; NodeColorWithAlpha.setValuesRGBA(NodeColor.red(),NodeColor.green(),NodeColor.blue(),0.05); } MaterialChunkRecPtr DefaultMatChunk = MaterialChunk::create(); DefaultMatChunk->setAmbient(NodeColorWithAlpha); DefaultMatChunk->setDiffuse(NodeColorWithAlpha); ChunkMaterialRecPtr NodeMat = ChunkMaterial::create(); NodeMat->setSortKey(1); NodeMat->addChunk(BaseBlendChunk); NodeMat->addChunk(BasePolygonChunk); NodeMat->addChunk(DefaultMatChunk); return MaterialTransitPtr(NodeMat); }
void VRMLDirectionalLightBinder::finish(VRMLToOSGAction *pAction) { if(_pNode == NULL) return; VRMLDirectionalLight *pNode = dynamic_cast<VRMLDirectionalLight *>(_pNode); if(pNode == NULL) return; NodePtr pBeaconNode = Node ::create(); GroupPtr pBeaconCore = Group::create(); beginEditCP(pBeaconNode, Node::CoreFieldMask); { pBeaconNode->setCore(pBeaconCore); } endEditCP (pBeaconNode, Node::CoreFieldMask); _pFieldContainer = pBeaconNode; _pLight = DirectionalLight::create(); Color4f cCol; Real32 rAmbientIntens = pNode->getSFAmbientIntensity()->getValue(); Real32 rIntensity = pNode->getSFIntensity ()->getValue(); SFColor3f *pColor = pNode->getSFColor (); cCol.setValuesRGBA(pColor->getValue()[0] * rAmbientIntens, pColor->getValue()[1] * rAmbientIntens, pColor->getValue()[2] * rAmbientIntens, 1.f); beginEditCP(_pLight); { _pLight->setAmbient(cCol); _pLight->setBeacon(pBeaconNode); cCol.setValuesRGBA(pColor->getValue()[0] * rIntensity, pColor->getValue()[1] * rIntensity, pColor->getValue()[2] * rIntensity, 1.f); _pLight->setDiffuse (cCol); _pLight->setDirection(-pNode->getSFDirection()->getValue()[0], -pNode->getSFDirection()->getValue()[1], -pNode->getSFDirection()->getValue()[2]); _pLight->setOn(pNode->getSFOn()->getValue()); _pLight->setSpecular(cCol); } endEditCP (_pLight); pAction->dropLight(_pLight); }
GeometryPtr PerformerLoader::traverseGSet(NodePtr node, pfGeoSet *gset) { FINFO(("PerformerLoader::traverseGSet: traversing %p: ", gset)); if(gset->getType()->isDerivedFrom(pfGeoArray::getClassType())) { return traverseGArray(node, dynamic_cast<pfGeoArray*>(gset)); } pfGeoState *gstate = gset->getGState(); int primtype, primcount, vertcount; primtype = gset->getPrimType(); primcount = gset->getNumPrims(); FINFO(("pt %d, pc %d ", primtype, primcount)); int lencount, *lenlist; if (lenlist = gset->getPrimLengths()) { int i; for (i = 0, lencount = 0; i < primcount; i++) lencount += PF_ABS(lenlist[i]); } // Map Performer to OpenGL primtype UInt32 oprimtype = GL_POINTS; static int pprimtypes[] = { PFGS_POINTS, PFGS_LINES, PFGS_TRIS, PFGS_QUADS, PFGS_LINESTRIPS, PFGS_TRISTRIPS, PFGS_TRIFANS, PFGS_POLYS, PFGS_FLAT_LINESTRIPS, PFGS_FLAT_TRISTRIPS, PFGS_FLAT_TRIFANS, -1}; static int oprimtypes[] = { GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, GL_LINE_STRIP, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_POLYGON, GL_LINE_STRIP, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN }; for (UInt16 i = 0; pprimtypes[i] != -1; ++i) if(pprimtypes[i] == primtype) oprimtype = oprimtypes[i]; int cn_buffer = 0; switch (primtype) { case PFGS_POINTS: vertcount = primcount; primcount = 1; break; case PFGS_LINES: vertcount = primcount * 2; primcount = 1; break; case PFGS_TRIS: vertcount = primcount * 3; primcount = 1; break; case PFGS_QUADS: vertcount = primcount * 4; primcount = 1; break; case PFGS_LINESTRIPS: case PFGS_TRISTRIPS: case PFGS_TRIFANS: case PFGS_POLYS: vertcount = lencount; break; case PFGS_FLAT_LINESTRIPS: FDEBUG(("PerformerLoader::traverseGSet: found " "primitive PFGS_FLAT_LINESTRIPS, will split into" "individual lines!\n")); cn_buffer = 1; vertcount = lencount; break; case PFGS_FLAT_TRISTRIPS: FDEBUG(("PerformerLoader::traverseGSet: found " "primitive PFGS_FLAT_TRISTRIPS!\n")); cn_buffer = 2; vertcount = lencount; break; case PFGS_FLAT_TRIFANS: FDEBUG(("PerformerLoader::traverseGSet: found " "primitive PFGS_FLAT_TRIFANS!\n")); cn_buffer = 2; vertcount = lencount; break; } FINFO(("vc %d ", vertcount)); // Fill the Geometry GeometryPtr ogeo = Geometry::create(); beginEditCP(ogeo); if(gstate) { ogeo->setMaterial(traverseGState(node, gstate)); } else { ogeo->setMaterial(NullFC); } GeoPTypesPtr otypes = GeoPTypesUI8::create(); beginEditCP(otypes); if (lenlist = gset->getPrimLengths()) { GeoPLengthsUI32Ptr olens = GeoPLengthsUI32::create(); beginEditCP(olens); for (int i = 0; i < primcount; i++) { otypes->push_back(oprimtype); olens->push_back(lenlist[i]); } endEditCP(olens); ogeo->setLengths(olens); } else { otypes->push_back(oprimtype); lenlist = &vertcount; lencount = vertcount; if(primcount != 1) FWARNING(("PerformerLoader::traverseGSet: No lens, but " "primcount=%d.\n", primcount)); } endEditCP(otypes); ogeo->setTypes(otypes); // Convert the geo attributes int pmin, pmax; pfVec3 *pverts; ushort *pinds; GeoPositions3fPtr opos = GeoPositions3f::create(); GeoIndicesUI16Ptr oind = GeoIndicesUI16::create(); beginEditCP(opos); beginEditCP(oind); gset->getAttrLists(PFGS_COORD3, (void**)&pverts, &pinds); if(pverts) { if(pinds) { FINFO(("Vi %d-%d ", pmin, pmax)); gset->getAttrRange(PFGS_COORD3, &pmin, &pmax); for(UInt32 i = 0; i < pmax+1; ++i) opos->push_back(Vec3f(pverts[i].vec)); for(UInt32 i = 0; i < vertcount; ++i) oind->push_back(pinds[i]); } else { FINFO(("V ")); for(UInt32 i = 0; i < vertcount; ++i) opos->push_back(Vec3f(pverts[i].vec)); } } endEditCP(opos); endEditCP(oind); ogeo->setPositions(opos); if(oind->size()) { ogeo->setIndices(oind); } else { subRefCP(oind); } int bind; // Normals if((bind = gset->getAttrBind(PFGS_NORMAL3)) != PFGS_OFF) { if(bind == PFGS_PER_PRIM) { FNOTICE(("PerformerLoader::traverseGSet: found PER_PRIM binding " "for normals, ignoring them.\n")); } else if(bind == PFGS_OVERALL) { FINFO(("NO ")); GeoNormals3fPtr onorm = GeoNormals3f::create(); beginEditCP(onorm); pfVec3 *pnorms; gset->getAttrLists(PFGS_NORMAL3, (void**)&pnorms, &pinds); if(pnorms) { Vec3f c; c.setValues(pnorms[0].vec[0], pnorms[0].vec[1], pnorms[0].vec[2]); #if 0 // This assumes a single normal is used as overall. Not true for 1.x onorm->getField().push_back(c); #else for(int i = 0; i < vertcount; ++i) onorm->getField().push_back(c); #endif } endEditCP(onorm); ogeo->setNormals(onorm); } else { GeoNormals3fPtr onorm = GeoNormals3f::create(); beginEditCP(onorm); pfVec3 *pnorms; ushort *pninds; gset->getAttrLists(PFGS_NORMAL3, (void**)&pnorms, &pninds); if(pnorms) { if(pninds) { int pcmin, pcmax; gset->getAttrRange(PFGS_NORMAL3, &pcmin, &pcmax); FINFO(("NI %d-%d ", pcmin, pcmax)); // Check indices if(pcmax != pmax) FWARNING(("Normal pmax %d != pmax %d!\n", pcmax, pmax)); for(int i = 0; i < pcmax; ++i) { if(pinds[i] != pninds[i]) FWARNING(("Normal Index %d (%d) != vind (%d)!\n", i, pninds[i], pinds[i])); } int ind = 0; for(int j = 0; j < primcount; ++j) { Vec3f c; c.setValues(pnorms[0].vec[0], pnorms[0].vec[1], pnorms[0].vec[2]); for(UInt32 i = 0; i < cn_buffer; ++i) { onorm->getField().push_back(c); } for(UInt32 i = 0; i < lenlist[j] - cn_buffer; ++i, ++ind) { Vec3f c; c.setValues(pnorms[ind].vec[0], pnorms[ind].vec[1], pnorms[ind].vec[2]); onorm->getField().push_back(c); } } } else { FINFO(("N ")); int ind = 0; for(int j = 0; j < primcount; ++j) { Vec3f c; c.setValues(pnorms[0].vec[0], pnorms[0].vec[1], pnorms[0].vec[2]); for(UInt32 i = 0; i < cn_buffer; ++i) { onorm->getField().push_back(c); } for(UInt32 i = 0; i < lenlist[j] - cn_buffer; ++i, ++ind) { Vec3f c; c.setValues(pnorms[ind].vec[0], pnorms[ind].vec[1], pnorms[ind].vec[2]); onorm->getField().push_back(c); } } } } endEditCP(onorm); ogeo->setNormals(onorm); } } // Colors if((bind = gset->getAttrBind(PFGS_COLOR4)) != PFGS_OFF) { if(bind == PFGS_PER_PRIM) { FNOTICE(("PerformerLoader::traverseGSet: found PER_PRIM binding " "for colors, ignoring them.\n")); } else if(bind == PFGS_OVERALL) { FINFO(("CO ")); GeoColors4fPtr ocols = GeoColors4f::create(); beginEditCP(ocols); pfVec4 *pcols; gset->getAttrLists(PFGS_COLOR4, (void**)&pcols, &pinds); if(pcols) { Color4f c; c.setValuesRGBA(pcols[0].vec[0], pcols[0].vec[1], pcols[0].vec[2], pcols[0].vec[3]); #if 0 // This assumes a single colors is used as overall. Not true for 1.x ocols->getField().push_back(c); #else for(int i = 0; i < vertcount; ++i) ocols->getField().push_back(c); #endif } endEditCP(ocols); ogeo->setColors(ocols); } else { GeoColors4fPtr ocols = GeoColors4f::create(); beginEditCP(ocols); pfVec4 *pcols; ushort *pcinds; gset->getAttrLists(PFGS_COLOR4, (void**)&pcols, &pcinds); if(pcols) { if(pcinds) { int pcmin, pcmax; gset->getAttrRange(PFGS_COLOR4, &pcmin, &pcmax); FINFO(("CI %d-%d ", pcmin, pcmax)); if(pcmax != pmax) FWARNING(("Color pmax %d != pmax %d!\n", pcmax, pmax)); for(int i = 0; i < pcmax; ++i) { if(pinds[i] != pcinds[i]) FWARNING(("Color Index %d (%d) != vind (%d)!\n", i, pcinds[i], pinds[i])); } // !!! Indices ignored for now, assumed to be the same // as for positions int ind = 0; for(int j = 0; j < primcount; ++j) { Color4f c; c.setValuesRGBA(pcols[0].vec[0], pcols[0].vec[1], pcols[0].vec[2], pcols[0].vec[3]); for(UInt32 i = 0; i < cn_buffer; ++i) { ocols->getField().push_back(c); } for(UInt32 i = 0; i < lenlist[j] - cn_buffer; ++i, ++ind) { Color4f c; c.setValuesRGBA(pcols[ind].vec[0], pcols[ind].vec[1], pcols[ind].vec[2], pcols[ind].vec[3]); ocols->getField().push_back(c); } } } else { FINFO(("C ")); int ind = 0; for(int j = 0; j < primcount; ++j) { Color4f c; c.setValuesRGBA(pcols[0].vec[0], pcols[0].vec[1], pcols[0].vec[2], pcols[0].vec[3]); for(UInt32 i = 0; i < cn_buffer; ++i) { ocols->getField().push_back(c); } for(UInt32 i = 0; i < lenlist[j] - cn_buffer; ++i, ++ind) { Color4f c; c.setValuesRGBA(pcols[ind].vec[0], pcols[ind].vec[1], pcols[ind].vec[2], pcols[ind].vec[3]); ocols->getField().push_back(c); } } } } endEditCP(ocols); ogeo->setColors(ocols); } } // Texture coordinates if((bind = gset->getAttrBind(PFGS_TEXCOORD2)) != PFGS_OFF) { if(bind == PFGS_PER_PRIM) { FNOTICE(("PerformerLoader::traverseGSet: found PER_PRIM binding " "for texcoords, ignoring them.\n")); } else if(bind == PFGS_OVERALL) { FINFO(("TO ")); GeoTexCoords2fPtr otexc = GeoTexCoords2f::create(); beginEditCP(otexc); pfVec2 *ptexcs; gset->getAttrLists(PFGS_TEXCOORD2, (void**)&ptexcs, &pinds); if(ptexcs) { Vec2f tc; tc.setValues(ptexcs[0].vec[0], ptexcs[0].vec[1]); #if 0 // This assumes a single texcal is used as overall. Not true for 1.x otexc->getField().push_back(tc); #else for(int i = 0; i < vertcount; ++i) otexc->getField().push_back(tc); #endif } endEditCP(otexc); ogeo->setTexCoords(otexc); } else { GeoTexCoords2fPtr otexc = GeoTexCoords2f::create(); beginEditCP(otexc); pfVec2 *ptexcs; ushort *ptexinds; gset->getAttrLists(PFGS_TEXCOORD2, (void**)&ptexcs, &ptexinds); if(ptexcs) { if(ptexinds) { int pcmin, pcmax; gset->getAttrRange(PFGS_TEXCOORD2, &pcmin, &pcmax); FINFO(("TI %d-%d ", pcmin, pcmax)); // Check indices if(pcmax != pmax) FWARNING(("TexCoord pmax %d != pmax %d!\n", pcmax, pmax)); for(int i = 0; i < pcmax; ++i) { if(pinds[i] != ptexinds[i]) FWARNING(("TexCoord Index %d (%d) != vind (%d)!\n", i, ptexinds[i], pinds[i])); } int ind = 0; for(int j = 0; j < primcount; ++j) { Vec2f tc; tc.setValues(ptexcs[0].vec[0], ptexcs[0].vec[1]); for(UInt32 i = 0; i < cn_buffer; ++i) { otexc->getField().push_back(tc); } for(UInt32 i = 0; i < lenlist[j] - cn_buffer; ++i, ++ind) { Vec2f tc; tc.setValues(ptexcs[ind].vec[0], ptexcs[ind].vec[1]); otexc->getField().push_back(tc); } } } else { FINFO(("T ")); int ind = 0; for(int j = 0; j < primcount; ++j) { Vec2f tc; tc.setValues(ptexcs[0].vec[0], ptexcs[0].vec[1]); for(UInt32 i = 0; i < cn_buffer; ++i) { otexc->getField().push_back(tc); } for(UInt32 i = 0; i < lenlist[j] - cn_buffer; ++i, ++ind) { Vec2f tc; tc.setValues(ptexcs[ind].vec[0], ptexcs[ind].vec[1]); otexc->getField().push_back(tc); } } } } endEditCP(otexc); ogeo->setTexCoords(otexc); } } FINFO(("\n")); endEditCP(ogeo); //FINFO(("PerformerLoader::Geo dump %p\n", gset)); //ogeo->dump(); return ogeo; }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); TutorialWindow->setDisplayCallback(display); TutorialWindow->setReshapeCallback(reshape); TutorialKeyListener TheKeyListener; TutorialWindow->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindow->addMouseListener(&TheTutorialMouseListener); TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindow); //Particle System Material LineChunkRefPtr PSLineChunk = LineChunk::create(); PSLineChunk->setWidth(1.0f); BlendChunkRefPtr PSBlendChunk = BlendChunk::create(); PSBlendChunk->setSrcFactor(GL_SRC_ALPHA); PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); MaterialChunkRefPtr PSMaterialChunk = MaterialChunk::create(); PSMaterialChunk->setAmbient(Color4f(0.3f,0.3f,0.3f,1.0f)); PSMaterialChunk->setDiffuse(Color4f(0.7f,0.7f,0.7f,1.0f)); PSMaterialChunk->setSpecular(Color4f(0.9f,0.9f,0.9f,1.0f)); PSMaterialChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE); PSMaterialChunk->setLit(false); ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create(); PSMaterial->addChunk(PSLineChunk); PSMaterial->addChunk(PSMaterialChunk); PSMaterial->addChunk(PSBlendChunk); //Particle System ParticleSystemRefPtr ExampleParticleSystem = ParticleSystem::create(); ExampleParticleSystem->attachUpdateListener(TutorialWindow); //Create the particles UInt32 NumParticlesToGenerate(2500); Distribution3DRefPtr PositionDistribution = createPositionDistribution(); Distribution3DRefPtr NormalDistribution = createNormalDistribution(); Distribution3DRefPtr ColorDistribution = createColorDistribution(); Distribution3DRefPtr SizeDistribution = createSizeDistribution(); Distribution1DRefPtr LifespanDistribution = createLifespanDistribution(); Distribution3DRefPtr VelocityDistribution = createVelocityDistribution(); Distribution3DRefPtr AccelerationDistribution = createAccelerationDistribution(); Pnt3f PositionReturnValue; Vec3f NormalReturnValue = Vec3f(0.0,0.0f,1.0f); Color4f ColorReturnValue = Color4f(1.0,1.0f,1.0f, 1.0f); Vec3f SizeReturnValue; Time LifespanReturnValue = -1; Vec3f VelocityReturnValue; Vec3f AccelerationReturnValue; for(UInt32 i(0) ; i< NumParticlesToGenerate ; ++i) { if(PositionDistribution != NULL) { PositionReturnValue.setValue(PositionDistribution->generate().getValues()); } if(ColorDistribution != NULL) { Vec3f ColorRGB = ColorDistribution->generate(); ColorReturnValue.setValuesRGBA(ColorRGB[0],ColorRGB[1],ColorRGB[2],1.0f); } if(SizeDistribution != NULL) { SizeReturnValue = SizeDistribution->generate(); } if(LifespanDistribution != NULL) { LifespanReturnValue = LifespanDistribution->generate(); } if(VelocityDistribution != NULL) { VelocityReturnValue = VelocityDistribution->generate(); } ExampleParticleSystem->addParticle(PositionReturnValue, NormalReturnValue, ColorReturnValue, SizeReturnValue, LifespanReturnValue, VelocityReturnValue, AccelerationReturnValue ); } //Particle System Drawer LineParticleSystemDrawerRefPtr ExampleParticleSystemDrawer = LineParticleSystemDrawer::create(); ExampleParticleSystemDrawer->setLineDirectionSource(LineParticleSystemDrawer::DIRECTION_VELOCITY); ExampleParticleSystemDrawer->setLineLengthSource(LineParticleSystemDrawer::LENGTH_SIZE_X); ExampleParticleSystemDrawer->setEndPointFading(Vec2f(0.0f,1.0f)); //Particle System Node ParticleSystemCoreRefPtr ParticleNodeCore = ParticleSystemCore::create(); ParticleNodeCore->setSystem(ExampleParticleSystem); ParticleNodeCore->setDrawer(ExampleParticleSystemDrawer); ParticleNodeCore->setMaterial(PSMaterial); NodeRefPtr ParticleNode = Node::create(); ParticleNode->setCore(ParticleNodeCore); // Make Main Scene Node and add the Torus NodeRefPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(ParticleNode); mgr->setRoot(scene); // Show the whole Scene mgr->showAll(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "02DynamicDistribution"); //Enter main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }