osg轮廓特效 【转】

// -*-c++-*-

/*
 * OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
 */

/*
 * osgFX::Outline - Copyright (C) 2004,2009 Ulrich Hertlein
 */

#ifndef OSGFX_OUTLINE_
#define OSGFX_OUTLINE_

#include <osgFX/Export>
#include <osgFX/Effect>

namespace osgFX
{
    /**
     * Outline effect.
     */
    class Outline : public Effect
    {
    public:
        /// Constructor.
        Outline();
        Outline(const Outline& copy,
                const osg::CopyOp& op = osg::CopyOp::SHALLOW_COPY)
            : Effect(copy, op) {
            _width = copy._width;
            _color = copy._color;
        }

// Effect class info
        META_Effect(osgFX, Outline, "Outline",
                    "Stencil buffer based object outlining.",
                    "Ulrich Hertlein <[email protected]>");

/// Set outline width.
        void setWidth(float w) {
            _width = w;
        }

/// Get outline width.
        float getWidth() const {
            return _width;
        }

/// Set outline color.
        void setColor(const osg::Vec4& col) {
            _color = col;
        }

/// Get outline color.
        const osg::Vec4& getColor() const {
            return _color;
        }

protected:
        /// Destructor.
        virtual ~Outline() {
        }

/// Define available techniques.
        bool define_techniques();

private:
        /// Outline width.
        float _width;

/// Outline color.
        osg::Vec4 _color;
    };
};

#endif

// -*-c++-*-

/*
 * OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
 */

/*
 * osgFX::Outline - Copyright (C) 2004,2009 Ulrich Hertlein
 */

#include <osgFX/Outline>
#include <osgFX/Registry>

#include <osg/Group>
#include <osg/Stencil>
#include <osg/CullFace>
#include <osg/PolygonMode>
#include <osg/LineWidth>
#include <osg/Material>

#include <osg/NodeCallback>
#include <osgUtil/CullVisitor>

#include <iostream>

const unsigned int Override_On = osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE;
const unsigned int Override_Off = osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE;

namespace osgFX
{
    /// Register prototype.
    Registry::Proxy proxy(new Outline);

/**
     * Outline technique.
     */
    class OutlineTechnique : public Technique
    {
    public:
        /// Constructor.
        OutlineTechnique(const Outline& outline) : Technique() {
            _outline = &outline;
        }

/// Validate.
        bool validate(osg::State&) const {
            return true;
        }

protected:
        /// Define render passes.
        void define_passes() {

/*
             * draw
             * - set stencil buffer to ref=1 where draw occurs
             * - clear stencil buffer to 0 where test fails
             */
            {
                osg::StateSet* state = new osg::StateSet;

// stencil op
                osg::Stencil* stencil  = new osg::Stencil;
                stencil->setFunction(osg::Stencil::ALWAYS, 1, ~0);
                stencil->setOperation(osg::Stencil::KEEP,
                                      osg::Stencil::KEEP,
                                      osg::Stencil::REPLACE);
                state->setAttributeAndModes(stencil, Override_On);

addPass(state);
            }

/*
             * post-draw
             * - only draw where draw didn‘t set the stencil buffer
             * - draw only back-facing polygons
             * - draw back-facing polys as lines
             * - disable depth-test, lighting & texture
             */
            {
                osg::StateSet* state = new osg::StateSet;

// stencil op
                osg::Stencil* stencil  = new osg::Stencil;
                stencil->setFunction(osg::Stencil::NOTEQUAL, 1, ~0);
                stencil->setOperation(osg::Stencil::KEEP,
                                      osg::Stencil::KEEP,
                                      osg::Stencil::REPLACE);
                state->setAttributeAndModes(stencil, Override_On);

// cull front-facing polys
                osg::CullFace* cf = new osg::CullFace;
                cf->setMode(osg::CullFace::FRONT);
                state->setAttributeAndModes(cf, Override_On);

// poly mode for back-facing polys
                osg::PolygonMode* pm = new osg::PolygonMode;
                pm->setMode(osg::PolygonMode::BACK, osg::PolygonMode::LINE);
                state->setAttributeAndModes(pm, Override_On);

// outline width
                osg::LineWidth* lw = new osg::LineWidth;
                lw->setWidth(_outline->getWidth());
                state->setAttributeAndModes(lw, Override_On);

// outline color/material
                const osg::Material::Face face = osg::Material::FRONT_AND_BACK;
                osg::Material* mtl = new osg::Material;
                mtl->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
                mtl->setAmbient(face, _outline->getColor());
                mtl->setDiffuse(face, _outline->getColor());
                mtl->setEmission(face, _outline->getColor());
                state->setAttributeAndModes(mtl, Override_On);

// disable modes
                state->setMode(GL_BLEND, Override_Off);
                state->setMode(GL_DEPTH_TEST, Override_Off);
                state->setTextureMode(0, GL_TEXTURE_1D, Override_Off);
                state->setTextureMode(0, GL_TEXTURE_2D, Override_Off);
                state->setTextureMode(0, GL_TEXTURE_3D, Override_Off);

addPass(state);
            }
        }

private:
        /// Outline effect.
        osg::ref_ptr<const Outline> _outline;
    };

/**
     * Enable stencil clear callback.
     */
    class EnableStencilCallback : public osg::NodeCallback
    {
    public:
        EnableStencilCallback() {}

virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {

osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
            if (cv) {
                // enable stencil clear on render stage
                osgUtil::RenderStage* render = cv->getRenderStage();
                unsigned int mask = render->getClearMask();
                if ((mask & GL_STENCIL_BUFFER_BIT) == 0) {
                    render->setClearMask(mask | GL_STENCIL_BUFFER_BIT);
                    render->setClearStencil(0);
                    //std::cerr << "osgFX::Outline activated stencil/n";
                }
            }

traverse(node, nv);
        }

private:
    };

/// Constructor.
    Outline::Outline() : Effect()
    {
        _width = 3.0f;
        _color.set(1.0f,1.0f,1.0f,1.0f);
        addCullCallback(new EnableStencilCallback);
    }

/// Define available techniques.
    bool Outline::define_techniques()
    {
        addTechnique(new OutlineTechnique(*this));
        return true;
    }
};

osg轮廓特效 【转】

时间: 2024-10-11 07:04:11

osg轮廓特效 【转】的相关文章

OSG报警特效学习总结

方法一:粒子系统 OSG的粒子系统有自己定义好的模块,如osgParticle::ExplosionEffect(爆炸模拟):osgParticle::SmokeEffect(烟雾模拟):osgParticle::FireEffect(火光模拟).我觉得可以利用烟雾模拟和火光模拟来做报警特效的展示. OSG向场景中添加osgParticle粒子效果 目的: 向场景中添加自定义的osgParticle实例,模拟坦克模型在地形上运动时产生的烟尘. --------------------------

OSG 3.0 三维视景仿真技术开发详解

第一章 OSG三维渲染引擎概述 OSG的主要功能包括以下几个方面: 1. 可以实时高效地绘制和控制使用建模软件所建立的3D模型, 如3DMAX.MAYA.Creator等制作的3D模型, 该功能是场景渲染的基本功能. 2. 支持多种外设, 如操作杆.游戏柄.轨迹球.方向盘.键盘鼠标等. 3. 除了传统的二维屏幕上进行三维展示外, OSG还可以完成红绿偏移的立体投影, 实现真正的立体展示. 4. 支持骨骼动画.关键帧动画.颜色动画等各种流行的动画. OSG的相关扩展: OSG的相关扩展,OSG针对

【翻译】圣斗士星矢:圣域传说 制作介绍 特效合成等

内部工具的开发 近年来,伴随着3DGC表现的高度化和复杂化,管线(Pipeline)的构筑和开发管理的重要性也变的越来越高.这样,东映动画数字影像部利用大公司的强势有精力的继续内部工具的独自开发. 以面向全CG最适化的管线实现为目标 本项目由于是大规模数据的管理以及必须多个部门的同时并行的分工,要致力于输出流程的设定到管线的再构筑.例如,为scene setup小组检查的各种数据中的动画的一部分数序要更新的情况考虑,设计在[Publish发布]前面的[Pool(临时发布)]的状态,回避紧凑的计划

UnityShader实例15:屏幕特效之Bloom

http://blog.csdn.net/u011047171/article/details/48522073 Bloom特效 概述 Bloom,又称"全屏泛光",是游戏中常用的一种镜头效果,是一种比较廉价的"伪HDR"效果(如下右图):使用了Bloom效果后,画面的对比会得到增强,亮的地方曝光也会得到加强,画面也会呈现一种朦胧,梦幻的效果,婚纱摄影中照片处理经常用到这种类似处理效果.Bloom效果一般用来近似模拟HDR效果,效果也比较相向,但实现原理却完全不同.

jQuery UI特效

effect(type, options, speed, callback) $.()          字符串  {对象} type 效果 blind 百叶窗 bounce 弹跳 clip 将两条相对的边移动至中间相遇 drop 掉出页面 explode 分解成碎片散射 fade   fold 合并相对的一组边框,对另一组边框做相同操作 highlight 改变元素的背景颜色 puff 调整不透明度时原地放大或缩小 pulsate 闪烁 scale 通过百分比放大或缩小 shake 来回晃动元

paip.关于动画特效原理 html js 框架总结

paip.关于动画特效原理 html js 框架总结 1. 动画框架的来源:flex,jqueryui 3 2. 特效的分类 3 2.1. Property effects 动态改变一个或多个目标对象的属性 (Animate.Fade, Resize, and AnimateColor) 4 2.2. Transform effects 缩入.旋转和位置的改变 .(Move, Rotate, and Scale) 4 2.3. Pixel-shader effects 主要是针对图片象素着色的动

基于HTML5 SVG炫酷文字爆炸特效

这是一款使用html5 svg.css3和js制作的炫酷文字爆炸特效.该文字特效用SVG path属性将文字路径切割为很多小块,然后使用css3和js在鼠标滑过文字时制作文字爆炸分裂的炫酷效果. 在线预览   源码下载 这是一款使用html5 svg.css3和js制作的炫酷文字爆炸特效.不论是HTML还是CSS都没有能力将文字分割成小块,但是SVG可以实现这个效果. 制作SVG文字 可以使用矢量图制作工具,如Adobe Illustrator等,将文字变成轮廓,然后使用“ Knife ”工具在

[转]显卡帝揭秘3D游戏画质特效

显卡帝揭秘3D游戏画质特效 近几年来,大量采用最新技术制作的大型3D游戏让大部分玩家都享受到了前所未有的游戏画质体验,同时在显卡硬件方面的技术革新也日新月异.对于经常玩游戏的玩家来说,可能对游戏画质提升有种不知不觉之感,而对于那些前些年经常玩游戏,现在突然又来玩新游戏的玩家来说,估计会度现在的游戏画质赞不绝口甚至惊呼“不可思议”.不过有一个现象我们不得不承认:游戏画质的设定选项越来越丰富了,同时玩家也对这些“乱花渐欲迷人眼”的游戏画质特效有点摸不着头脑了.所以今天显卡帝就来为您详细解读3D游戏特

OSG选中效果展示

<OpenSceneGraph三维渲染引擎编程指南>书中选中高亮效果示例.osgFX特效 2.得到鼠标的位置 osgFX特效1.高亮,在开头的时候写了: 2.线框: 1 #include<osgFX/Outline> 2 osg::Geode *geode = new osg::Geode; 3 geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(), 2))); 测试可行 4 //声明Effect