📄 readerwritergeo.cpp
字号:
// GEO format (carbon graphics Inc) loader for the OSG real time scene graph// www.carbongraphics.com for more information about the Geo animation+ modeller// supports geometry and group & face level animations.// Vertex level animation partly supported - defines movement (translate, rotate, colour)!// Loader has been divided into two parts // 1- general geometry (here) & // 2- animation (see geoActions.cpp).// ver 1.2 GWM Nov 2003#include <string>#include <osg/Image>#include <osg/Group>#include <osg/LOD>#include <osg/Billboard>#include <osg/Sequence>#include <osg/Switch>#include <osg/Geode>#include <osg/Depth>#include <osg/LineWidth>#include <osg/Geometry>#include <osg/MatrixTransform>#include <osg/Material>#include <osg/Notify>#include <osg/Texture2D>#include <osg/TexEnv>#include <osg/StateSet>#include <osg/CullFace>#include <osg/Point>#include "ClipRegion.h"#include <osgSim/LightPointNode>#include <osgDB/FileNameUtils>#include <osgDB/FileUtils>#include <osgDB/fstream>#include <osgDB/Registry>#include <osgDB/ReadFile>#include <osgDB/WriteFile>#include <osgDB/Input>#include <osgDB/Output>#include <osgUtil/Tessellator>#include <stdio.h>// specific to GEO#include "geoFormat.h"#include "geoTypes.h"#include "geoUnits.h"#include "osgGeoAnimation.h"#include "osgGeoStructs.h"#include "osgGeoNodes.h"#include "osgGeoAction.h"#include <osgText/Text> // needed for text nodes//geoHeaderGeo::geoHeaderGeo(){ // animations for the header - actually updates all control variables intVars=new internalVars; useVars=new userVars; extVars=new userVars; _initialTick = _timer.tick(); color_palette=new colourPalette;}const geoValue *geoHeaderGeo::getGeoVar(const unsigned fid) const { const geoValue *st=intVars->getGeoVar(fid); if (!st) { st=useVars->getGeoVar(fid); if (!st) { st=extVars->getGeoVar(fid); } } return st;}double *geoHeaderGeo::getVar(const unsigned fid) const { double *dv=NULL; dv=intVars->getVar(fid); if (!dv) { dv=useVars->getVar(fid); if (!dv) { dv=extVars->getVar(fid); } } return dv;}void geoHeaderGeo::addUserVar(const georecord &gr){ // this georecord defines a single variable of type<> useVars->addUserVar(gr);}//== handler for updating internal variablesvoid geoHeaderGeo::update(const osg::FrameStamp *_frameStamp){ // update the scene osg::Timer_t _frameTick = _timer.tick(); _lastFrameTick=_frameTick; double time = _frameStamp->getSimulationTime(); intVars->update( _frameStamp); moveit(time);}void geoHeaderGeo::moveit(const double t){ // all the local and external variables declared in the geo modeller are here available. if (uvarupdate) { std::vector<geoValue> *lvals=useVars->getvars(); for (std::vector<geoValue>::iterator itr=lvals->begin(); itr!=lvals->end(); ++itr) {// for each user var double vv=uvarupdate(t, itr->getVal(), itr->getName()); // std::cout << " updatee " << itr->getName() << " " << vv << " " << itr->getVar() << std::endl; itr->setVal(vv); // vv=itr->getVal(); std::cout << " result " << itr->getName() << " " << vv << std::endl; } } if (extvarupdate) { std::vector<geoValue> *lvals=extVars->getvars(); for (std::vector<geoValue>::iterator itr=lvals->begin(); itr!=lvals->end(); ++itr) {// for each user var itr->setVal(extvarupdate(t, itr->getVal(), itr->getName())); } }}class geoHeaderCB: public osg::NodeCallback {public: geoHeaderCB() {} ~geoHeaderCB() {} virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { // update action vars geoHeaderGeo *gh=(geoHeaderGeo *)node; gh->update(nv->getFrameStamp()); nv->setNodeMaskOverride(0xffffffff); // need to make the visitor override the nodemask // so that it visits 'invisible' nodes to update visibility. Or could use // a visitor with setTraversalMode(TraversalMode==TRAVERSE_ALL_CHILDREN)? traverse(node,nv); // std::cout<<"update callback - post traverse"<< (float)_frameStamp->getSimulationTime() <<std::endl; }private:};//=============class vertexInfo { // holds vertex information for an entire osg::geometrypublic: vertexInfo() { norms=new osg::Vec3Array; coords=new osg::Vec3Array; txcoords=new osg::Vec2Array; colorindices=new osg::IntArray; coordindices=new osg::IntArray; normindices=new osg::IntArray; txindices=new osg::IntArray; colors=new osg::Vec4Array; cpool=NULL; npool=NULL; polycols= new osg::Vec4Array; // polygon colours } typedef std::vector<geoActionBehaviour *> drBehList; void setPools(const std::vector<osg::Vec3> *coord_pool, const std::vector<osg::Vec3> *normal_pool) { cpool=coord_pool; npool=normal_pool; } inline bool hasVertexActions(void) const { return !(BehList.empty()); } inline osg::Vec4Array *getColors() const { return colors;} inline osg::Vec3Array *getNorms() const { return norms;} inline osg::Vec3Array *getCoords() const { return coords;} inline osg::Vec2Array *getTexCoords() const { return txcoords;} inline osg::IntArray *getColorIndices() const { return colorindices;} inline osg::IntArray *getCoordIndices() const { return coordindices;} inline osg::IntArray *getNormIndices() const { return normindices;} inline osg::IntArray *getTextureIndices() const { return txindices;} void addPolcolour( osg::Vec4 cl) { polycols->push_back(cl);} osg::Vec4Array *getPolcolours() const { return polycols;} void addVertexActions(geoBehaviourDrawableCB *gcb) const { // add the actions to callback if ( !(BehList.empty()) ) { for (drBehList::const_iterator rcitr=BehList.begin(); rcitr!=BehList.end(); ++rcitr) { gcb->addBehaviour(*rcitr); } } } bool addFlat( const georecord *gface) { // this must only be called with a vertex georecord. bool isflat=false; const geoField *gfshade=gface->getField(GEO_DB_POLY_SHADEMODEL); // shaded gouraud, flat... int shademodel=gfshade ? gfshade->getInt() : -1; if (shademodel==GEO_POLY_SHADEMODEL_LIT) { // flat shaded - need the index const geoField *gfd=gface->getField(GEO_DB_POLY_NORMAL); if (gfd) { float *normal= (gfd) ? (gfd->getVec3Arr()):NULL; osg::Vec3 nrm(normal[0], normal[1], normal[2]); norms->push_back(nrm); isflat=true; } } return isflat; } bool addIndices(georecord *gr, const geoHeaderGeo *ghdr, const float cdef[4], const georecord *gface) { // this must only be called with a vertex georecord. // gr is tha vertex; gface is the face containing the vertex bool hbeh=false; // true if this vertex has a behaviour if (gr->getType()==DB_DSK_VERTEX || gr->getType()==DB_DSK_FAT_VERTEX || gr->getType()==DB_DSK_SLIM_VERTEX) { const geoField *gfshade=gface->getField(GEO_DB_POLY_SHADEMODEL); // shaded gouraud, flat... int shademodel=gfshade ? gfshade->getInt() : -1; if (shademodel!=GEO_POLY_SHADEMODEL_LIT && shademodel!=GEO_POLY_SHADEMODEL_FLAT) { const geoField *gfd=gr->getField(GEO_DB_VRTX_NORMAL); if (gfd->getType()==DB_UINT) { if (gfd) { unsigned int idx=gfd->getUInt(); normindices->push_back(idx); norms->push_back((*npool)[idx]); } else { osg::notify(osg::WARN) << "No valid vertex index" << std::endl; } } else if (gfd->getType()==DB_VEC3F) { float *p=gfd->getVec3Arr(); osg::Vec3 nrm; nrm.set(p[0],p[1],p[2]); norms->push_back(nrm); } } const geoField *gfd=gr->getField(GEO_DB_VRTX_COORD); osg::Vec3 pos; if (gfd->getType()==DB_INT) { if (gfd) { int idx=gfd->getInt(); pos=(*cpool)[idx]; coords->push_back((*cpool)[idx]); //osg::Vec3(cpool[3*idx],cpool[3*idx+1],cpool[3*idx+2])); coordindices->push_back(coords->size()); } else { osg::notify(osg::WARN) << "No valid vertex index" << std::endl; } } else if (gfd->getType()==DB_VEC3F) { float *p=gfd->getVec3Arr(); pos.set(p[0],p[1],p[2]); coords->push_back(pos); //osg::Vec3(cpool[3*idx],cpool[3*idx+1],cpool[3*idx+2])); } std::vector< georecord *>bhv=gr->getBehaviour(); // behaviours for vertices, eg tranlate, colour! if (!bhv.empty()) { int ncoord=coords->size(); for (std::vector< georecord *>::const_iterator rcitr=bhv.begin(); rcitr!=bhv.end(); ++rcitr) { if ((*rcitr)->getType()==DB_DSK_TRANSLATE_ACTION) { geoMoveVertexBehaviour *mb=new geoMoveVertexBehaviour; mb->makeBehave((*rcitr),ghdr); mb->setpos(pos); mb->setindx(ncoord-1); BehList.push_back(mb); } if ((*rcitr)->getType()==DB_DSK_ROTATE_ACTION) { geoMoveVertexBehaviour *mb=new geoMoveVertexBehaviour; mb->makeBehave((*rcitr),ghdr); mb->setpos(pos); mb->setindx(ncoord-1); BehList.push_back(mb); } if ((*rcitr)->getType()==DB_DSK_COLOR_RAMP_ACTION) { const geoField *gfd=gface->getField(GEO_DB_POLY_USE_MATERIAL_DIFFUSE); // true: use material... bool usemat= gfd ? gfd->getBool() : false; if (!usemat) { // modify the per vertex colours gfd=gface->getField(GEO_DB_POLY_SHADEMODEL); // shaded gouraud, flat... int shademodel=gfd ? gfd->getInt() : GEO_POLY_SHADEMODEL_LIT_GOURAUD; gfd=gface->getField(GEO_DB_POLY_USE_VERTEX_COLORS); // true: use material... bool usevert=gfd ? gfd->getBool() : false; if (usevert || shademodel==GEO_POLY_SHADEMODEL_GOURAUD) { // then the vertex colours are used geoColourBehaviour *cb=new geoColourBehaviour; cb->setColorPalette(ghdr->getColorPalette()); cb->setVertIndices(ncoord-1,1); // part of colours array to be modified bool ok=cb->makeBehave((*rcitr), ghdr); if (ok) BehList.push_back(cb); } // if the model does not use vertex colours... there can be no colour animation at vertex level } } } hbeh=true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -