void EnPlayerAnimation::initialize() { log_info << " initializing player animation instance '" << getInstanceName() << "' ..." << std::endl; if ( !_animCfgFile.length() ) { log_error << "*** missing animation config file parameter" << std::endl; return; } // setup and create a new model std::string file = yaf3d::Application::get()->getMediaPath() + _animCfgFile; std::string rootDir = yaf3d::extractPath( file ); std::string configfilename = yaf3d::extractFileName( file ); // all textures and cal3d files must be in root dir! if ( !setupAnimation( rootDir, configfilename ) ) return; // set initial animation _model->startLoop( _IdAnimIdle, 1.0f, 0.0f ); _anim = eIdle; // scale the model _coreModel->get()->scale( _scale ); // create a transform node in order to set position and rotation offsets _animNode = new osg::PositionAttitudeTransform; _animNode->setPosition( _position ); osg::Quat quat( _rotation.x() * osg::PI / 180.0f, osg::Vec3f( 1, 0, 0 ), _rotation.y() * osg::PI / 180.0f, osg::Vec3f( 0, 1, 0 ), _rotation.z() * osg::PI / 180.0f, osg::Vec3f( 0, 0, 1 ) ); _animNode->setAttitude( quat ); _animNode->addChild( _model.get() ); // setup the shaders for ( currently just disable glsl usage ) // first check if glsl is supported before setting up the shaders ( gl context 0 is assumed ) const osg::GL2Extensions* p_extensions = osg::GL2Extensions::Get( 0, true ); if ( p_extensions->isGlslSupported() ) { if ( !s_program.valid() ) { s_program = new osg::Program; s_program->setName( "_playerAnim_" ); s_program->addShader( new osg::Shader( osg::Shader::VERTEX, glsl_vp ) ); s_program->addShader( new osg::Shader( osg::Shader::FRAGMENT, glsl_fp ) ); } osg::StateSet* p_stateSet = _animNode->getOrCreateStateSet(); p_stateSet->setAttributeAndModes( s_program.get(), osg::StateAttribute::ON ); _animNode->setStateSet( p_stateSet ); } log << " initializing player animation instance completed" << std::endl; }
void ComputeNode::initComputingSetup() { _computeProgram = new osg::Program; _computeDispatch->setComputeGroups((NUM_ELEMENTS_X / WORK_GROUP_SIZE) <= 1 ? 1 : (NUM_ELEMENTS_X / WORK_GROUP_SIZE), (NUM_ELEMENTS_Y / WORK_GROUP_SIZE) <= 1 ? 1 : (NUM_ELEMENTS_Y / WORK_GROUP_SIZE), 1); _computeShader = osgDB::readRefShaderFile(osg::Shader::COMPUTE, _computeShaderSourcePath); _computeProgram->addShader(_computeShader.get()); setDataVariance(osg::Object::DYNAMIC); osg::StateSet* statesetComputation = getOrCreateStateSet(); statesetComputation->setAttributeAndModes(_computeProgram.get()); statesetComputation->addUniform(new osg::Uniform("numCols", (int)NUM_ELEMENTS_X)); statesetComputation->addUniform(new osg::Uniform("numRows", (int)NUM_ELEMENTS_Y)); statesetComputation->setMode(GL_LIGHTING, osg::StateAttribute::OFF); //blocksize int numParticles = NUM_ELEMENTS_X * NUM_ELEMENTS_Y; const unsigned blockSize = numParticles * __numChannels * __numDataValuesPerChannel* sizeof(GLfloat); //init all the particle data array int idx = 0; _data = new GLfloat[NUM_ELEMENTS_X * NUM_ELEMENTS_Y * __numChannels * __numDataValuesPerChannel]; _dataArray = new FloatArray; //init the data array somehow, this way all is stored in one BufferObject. maybe better using multiple buffers instead? not sure what is faster and better for threading for (int d = 0; d < __numDataValuesPerChannel; ++d) { for (int i = 0; i < NUM_ELEMENTS_X; ++i) { for (int j = 0; j < NUM_ELEMENTS_Y; ++j) { for (int k = 0; k < __numChannels; ++k) { switch (k) { case (RED_CHANNEL) : { if ((d == POSITION_NOW_OFFSET) || (d == POSITION_OLD_OFFSET) || (d == POSITION_INIT_OFFSET))//position { *_data = random(0.25, 0.75); } if ((d == VELOCITY_NOW_OFFSET) || (d == VELOCITY_OLD_OFFSET) || (d == VELOCITY_INIT_OFFSET))//velocity { *_data = random(-2.4, 2.4); } if (d == ACCELERATION_OFFSET) //acceleration { *_data = random(-3.0, 3.0); } if (d == PROPERTIES_OFFSET) //property particle mass (compute shader is computing sphere mass from radius instead) { *_data = random(0.2, 15.0); } break; } case (GREEN_CHANNEL) : { if ((d == POSITION_NOW_OFFSET) || (d == POSITION_OLD_OFFSET) || (d == POSITION_INIT_OFFSET))//position { *_data = random(0.25, 0.75); } if ((d == VELOCITY_NOW_OFFSET) || (d == VELOCITY_OLD_OFFSET) || (d == VELOCITY_INIT_OFFSET))//velocity { *_data = random(-2.4, 2.4); } if (d == ACCELERATION_OFFSET)//acceleration { *_data = random(-3.0, 3.0); } if (d == PROPERTIES_OFFSET) //property particle radius { *_data = random(0.07, 0.219); } break; } case (BLUE_CHANNEL) : { if ((d == POSITION_NOW_OFFSET) || (d == POSITION_OLD_OFFSET) || (d == POSITION_INIT_OFFSET))//position { *_data = random(0.25, 0.75); } if ((d == VELOCITY_NOW_OFFSET) || (d == VELOCITY_OLD_OFFSET) || (d == VELOCITY_INIT_OFFSET))//velocity { *_data = random(-2.4, 2.4); } if (d == ACCELERATION_OFFSET)//acceleration { *_data = random(-3.0, 3.0); } if (d == PROPERTIES_OFFSET) //place for some other property { *_data = random(0.0, 0.0); } break; } case (ALPHA_CHANNEL) : { if ((d == POSITION_NOW_OFFSET) || (d == POSITION_OLD_OFFSET) || (d == POSITION_INIT_OFFSET))//position { *_data = random(1.0, 1.0); } if ((d == VELOCITY_NOW_OFFSET) || (d == VELOCITY_OLD_OFFSET) || (d == VELOCITY_INIT_OFFSET))//velocity { *_data = random(-2.4, 2.4); } if (d == ACCELERATION_OFFSET) //acceleration { //*_data = random(1.0, 1.0); *_data = random(0.0, 0.0); } if (d == PROPERTIES_OFFSET) //place for some other property { *_data = random(0.3, 0.3); } break; } } _dataArray->push_back(*_data); _data++; idx++; } } } } _ssbo = new osg::ShaderStorageBufferObject; _dataArray->setBufferObject(_ssbo.get()); _ssbb = new osg::ShaderStorageBufferBinding(0, _dataArray.get(), 0, blockSize); statesetComputation->setAttributeAndModes(_ssbb.get(), osg::StateAttribute::ON); //option, do something useful with data or test the transfer speed //_ssbb->setUpdateCallback(new ShaderStorageBufferCallback); //adding a quad , visualizing data in buffer addDataMonitor(osg::Vec3(0, -1, 0), osg::Vec3(SUB_PLACEMENT_OFFSET_HORIZONTAL * 0, -SUB_PLACEMENT_OFFSET_VERTICAL * -2.0, SUB_PLACEMENT_OFFSET_HORIZONTAL * 0), 1.0, RGB_CHANNEL, POSITION_NOW_OFFSET, "X,Y,Z - PositionNow", -1.0, 1.0); //the coord from default dataset addHelperGeometry(); addComputationResultsRenderTree(); }
void ComputeNode::addComputationResultsRenderTree() { _computationResultsRenderProgram = new osg::Program; _vertexShader = osgDB::readRefShaderFile(osg::Shader::VERTEX, _vertexShaderSourcePath); _computationResultsRenderProgram->addShader(_vertexShader.get()); _geometryShader = osgDB::readRefShaderFile(osg::Shader::GEOMETRY, _geometryShaderSourcePath); _computationResultsRenderProgram->addShader(_geometryShader.get()); _fragmentShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, _fragmentShaderSourcePath); _computationResultsRenderProgram->addShader(_fragmentShader.get()); _computationResultsRenderProgram->addBindAttribLocation("tex_coords", 1); _computationResultsRenderGroup = new osg::Group; _computationResultsRenderGroup->setDataVariance(osg::Object::DYNAMIC); _computationResultsRenderStateSet = _computationResultsRenderGroup->getOrCreateStateSet(); _computationResultsRenderStateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); osg::PointSprite *sprite = new osg::PointSprite; int texture_unit = 0; _computationResultsRenderStateSet->setTextureAttributeAndModes(texture_unit, sprite, osg::StateAttribute::ON); _computationResultsRenderStateSet->setAttributeAndModes(_computationResultsRenderProgram.get(), osg::StateAttribute::ON); _computationResultsRenderStateSet->addUniform(new osg::Uniform("particleTexture", texture_unit)); _computationResultsRenderStateSet->addUniform(new osg::Uniform("numRows", (int)NUM_ELEMENTS_X)); _computationResultsRenderStateSet->addUniform(new osg::Uniform("numCols", (int)NUM_ELEMENTS_Y)); _computationResultsRenderStateSet->setMode(GL_POINT_SMOOTH, osg::StateAttribute::ON); _computationResultsRenderStateSet->setMode(GL_VERTEX_PROGRAM_POINT_SIZE_ARB, osg::StateAttribute::ON); _computationResultsRenderStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON); _computationResultsRenderStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); osg::Texture2D *tex = new osg::Texture2D(); osg::Image* particleImage = createSpotLightImage(osg::Vec4(1, 0, 0, 1), osg::Vec4(0.5, 0, 0, 0.0), 32, 0.7); if (particleImage) { tex->setImage(particleImage); } _computationResultsRenderStateSet->setTextureAttributeAndModes(texture_unit, tex, osg::StateAttribute::ON); osg::BlendFunc *blend = new osg::BlendFunc; if (false) //emissive particles { blend->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE); } else { blend->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); } _computationResultsRenderStateSet->setAttributeAndModes(blend, osg::StateAttribute::ON); osg::Depth* depth = new osg::Depth; depth->setRange(0.0f, 0.0f); depth->setFunction(osg::Depth::ALWAYS); depth->setWriteMask(false); depth->setFunction(osg::Depth::ALWAYS); _computationResultsRenderStateSet->setAttributeAndModes(depth, osg::StateAttribute::OFF); osg::Geode* particleGeode = new osg::Geode; unsigned int numVertices = NUM_ELEMENTS_X*NUM_ELEMENTS_Y; osg::Geometry* particleGeometry = new osg::Geometry; particleGeometry->setUseDisplayList(false); particleGeometry->setUseVertexBufferObjects(true); osg::Vec3Array* vertexarray = new osg::Vec3Array; osg::Vec2Array* tcoords = new osg::Vec2Array; osg::Vec2 bottom_texcoord(0.0f, 0.0f); osg::Vec2 dx_texcoord(1.0f / (float)(NUM_ELEMENTS_X), 0.0f); osg::Vec2 dy_texcoord(0.0f, 1.0f / (float)(NUM_ELEMENTS_Y)); for (int i = 0; i < NUM_ELEMENTS_X; i++) { osg::Vec2 texcoord = bottom_texcoord + dy_texcoord*(float)i; for (int j = 0; j < NUM_ELEMENTS_Y; j++) { vertexarray->push_back(osg::Vec3(texcoord.x(), texcoord.y(), 0.0)); tcoords->push_back(osg::Vec2(texcoord.x(), texcoord.y())); texcoord += dx_texcoord; } } particleGeometry->setVertexArray(vertexarray); particleGeometry->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, numVertices)); particleGeometry->setTexCoordArray(0, tcoords); //this glMemoryBarrier thing... not sure if we could better do instanced drawing? all the data is in Shader Storage Buffer.. particleGeometry->setVertexAttribArray(1, particleGeometry->getTexCoordArray(0), osg::Array::BIND_PER_VERTEX); _computationResultsRenderGroup->addChild(particleGeode); particleGeode->addDrawable(particleGeometry); addChild(_computationResultsRenderGroup.get()); }
void Scene::create() { _oneSidedProgram = new osg::Program; _oneSidedProgram->addShader(osgDB::readShaderFile(osg::Shader::VERTEX, "data/diffuse_directional1_vs.glsl")); _oneSidedProgram->addShader(osgDB::readShaderFile(osg::Shader::FRAGMENT, "data/diffuse_directional1_fs.glsl")); _twoSidedProgram = new osg::Program; _twoSidedProgram->addShader(osgDB::readShaderFile(osg::Shader::VERTEX, "data/diffuse_directional2_vs.glsl")); _twoSidedProgram->addShader(osgDB::readShaderFile(osg::Shader::FRAGMENT, "data/diffuse_directional2_fs.glsl")); _instancedProgram = new osg::Program; _instancedProgram->addShader(osgDB::readShaderFile(osg::Shader::VERTEX, "data/diffuse_billboard_instanced_vs.glsl")); _instancedProgram->addShader(osgDB::readShaderFile(osg::Shader::FRAGMENT, "data/diffuse_billboard_instanced_fs.glsl")); createPolyhedronNode(); createVertexNode(); createTextNode(); onPolyhedronChanged(); onLightModelChanged(); osg::ref_ptr<osgGA::LambdaEventHandler> eventHandler = new osgGA::LambdaEventHandler; eventHandler->onKeyDown([&](const osgGA::GUIEventAdapter& ea) { auto key = ea.getKey(); auto num = key - osgGA::GUIEventAdapter::KEY_0; if (0 <= num && num <= 9) { _faces ^= osgKaleido::PolyhedronGeometry::FaceMaskFromSides(wild::mod(num-1, 10) + 3); onFaceMaskChanged(); return true; } switch (key) { case osgGA::GUIEventAdapter::KEY_V: { _vgeode->setNodeMask(~_vgeode->getNodeMask()); return true; } case osgGA::GUIEventAdapter::KEY_L: { _twoSided = !_twoSided; onLightModelChanged(); return true; } case osgGA::GUIEventAdapter::KEY_Right: { _faces = osgKaleido::PolyhedronGeometry::All; _index++; onPolyhedronChanged(); return true; } case osgGA::GUIEventAdapter::KEY_Left: { _faces = osgKaleido::PolyhedronGeometry::All; _index--; onPolyhedronChanged(); return true; } default: return false; } }); _viewer->addEventHandler(eventHandler); }
TextRegion::TextRegion(TextRegionStyle* style) : StrokeRegion(style), _string(""), _text(NULL), _fontHeight(-1), _fontName(""), _boarderPadding(5.0f), _alignmentMode(NO_ALIGN_SET), _textColor(osg::Vec4(-0.1f,-0.1f,-0.1f,-1.0f)), _backdropType(NO_BACKDROP_SET), _backdropColor(osg::Vec4(-0.1f,-0.1f,-0.1f,-0.7f)), //callback events _onTextChangedEvent(new HudCallbackEvent(this, "OnTextChanged")) { //create the text label to add to the button _text = new osgText::Text; _text->setUseDisplayList(false); _text->setUseVertexBufferObjects(true); //_text->setCharacterSizeMode(osgText::TextBase::OBJECT_COORDS_WITH_MAXIMU_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); //add the text to a geode for drawing osg::Geode* textGeode = new osg::Geode(); //geode is visible, not pickable textGeode->setNodeMask(hogbox::MAIN_CAMERA_CULL); MakeHudGeodes(textGeode, new RegionWrapper(this)); osg::StateSet* stateset = textGeode->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); #ifndef OSG_GL_FIXED_FUNCTION_AVAILABLE if(!g_textQuadProgram.get()){ g_textQuadProgram = new osg::Program; g_textQuadProgram->setName("textShader"); g_textQuadProgram->addShader(new osg::Shader(osg::Shader::VERTEX, textVertSource)); g_textQuadProgram->addShader(new osg::Shader(osg::Shader::FRAGMENT, textFragSource)); } stateset->setAttributeAndModes(g_textQuadProgram, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); stateset->addUniform(new osg::Uniform("glyphTexture", 0)); #endif textGeode->setStateSet(stateset); textGeode->addDrawable(_text.get()); _textScale = new osg::MatrixTransform(); _textScale->addChild(textGeode); //use transform to orientate the text into correct plane and forward of the backdrop osg::MatrixTransform* oriText = new osg::MatrixTransform(); //flip plane float temp = 0.0f; osg::Vec3 offset = osg::Vec3(0,0,0.1); osg::Matrix oriMatrix; switch(_args->_planeType){ case hogbox::Quad::PLANE_XY: oriMatrix = osg::Matrix::translate(offset); break; case hogbox::Quad::PLANE_XZ: //flip x and z temp = offset.y(); offset.y() = offset.z(); offset.z() = temp; oriMatrix = osg::Matrix::translate(offset) * osg::Matrix::rotate(osg::DegreesToRadians(90.0f), osg::Vec3(1,0,0)); break; default:break; } oriText->setMatrix(oriMatrix); oriText->addChild(_textScale.get()); //attach the text to translate for now _rotate->addChild(oriText); _text->setColor(osg::Vec4(0.1f,0.1f,0.1f,1.0f)); this->SetFontType("Fonts/arial.ttf"); this->SetFontHeight(18.0f); this->SetAlignment(CENTER_ALIGN); this->SetBackDropType(NO_BACKDROP); this->SetBackDropColor(osg::Vec4(0.1f,0.1f,0.1f,0.7f)); }
bool init(osgAnimation::RigGeometry& geom) { osg::Vec3Array* pos = dynamic_cast<osg::Vec3Array*>(geom.getVertexArray()); if (!pos) { osg::notify(osg::WARN) << "RigTransformHardware no vertex array in the geometry " << geom.getName() << std::endl; return false; } if (!geom.getSkeleton()) { osg::notify(osg::WARN) << "RigTransformHardware no skeleting set in geometry " << geom.getName() << std::endl; return false; } osgAnimation::BoneMapVisitor mapVisitor; geom.getSkeleton()->accept(mapVisitor); osgAnimation::BoneMap bm = mapVisitor.getBoneMap(); if (!createPalette(pos->size(),bm, geom.getVertexInfluenceSet().getVertexToBoneList())) return false; int attribIndex = 11; int nbAttribs = getNumVertexAttrib(); // use a global program for all avatar if (!program.valid()) { program = new osg::Program; program->setName("HardwareSkinning"); if (!_shader.valid()) _shader = osg::Shader::readShaderFile(osg::Shader::VERTEX,"shaders/skinning.vert"); if (!_shader.valid()) { osg::notify(osg::WARN) << "RigTransformHardware can't load VertexShader" << std::endl; return false; } // replace max matrix by the value from uniform { std::string str = _shader->getShaderSource(); std::string toreplace = std::string("MAX_MATRIX"); std::size_t start = str.find(toreplace); std::stringstream ss; ss << getMatrixPaletteUniform()->getNumElements(); str.replace(start, toreplace.size(), ss.str()); _shader->setShaderSource(str); osg::notify(osg::INFO) << "Shader " << str << std::endl; } program->addShader(_shader.get()); for (int i = 0; i < nbAttribs; i++) { std::stringstream ss; ss << "boneWeight" << i; program->addBindAttribLocation(ss.str(), attribIndex + i); osg::notify(osg::INFO) << "set vertex attrib " << ss.str() << std::endl; } } for (int i = 0; i < nbAttribs; i++) { std::stringstream ss; ss << "boneWeight" << i; geom.setVertexAttribArray(attribIndex + i, getVertexAttrib(i)); } osg::ref_ptr<osg::StateSet> ss = new osg::StateSet; ss->addUniform(getMatrixPaletteUniform()); ss->addUniform(new osg::Uniform("nbBonesPerVertex", getNumBonesPerVertex())); ss->setAttributeAndModes(program.get()); geom.setStateSet(ss.get()); _needInit = false; return true; }