// -*-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 <u.hertlein@sandbox.de>");

/// 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轮廓特效 【转】的更多相关文章

  1. C# vb .net实现轮廓特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的轮廓特效呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  2. OSG描边特效osgFX::Outline的修改

    对一个三维场景中的物体实现描边特效,可以参考osg范例osgoutline 这个描边特效使用了模板缓存Stencil来实现,参见源代码osgFX/Outline.cpp 使用了两个Pass 第一个Pa ...

  3. OSG报警特效学习总结

    方法一:粒子系统         OSG的粒子系统有自己定义好的模块,如osgParticle::ExplosionEffect(爆炸模拟):osgParticle::SmokeEffect(烟雾模拟 ...

  4. [原][osg][粒子特效]spark粒子特效生成流程

  5. PDF 文件编写器 C# 类库(版本 1.28.0)使用详解

    PDF File Writer 是一个 C# .NET 类库,允许应用程序创建 PDF 文件. PDF File Writer C# 类库使 .NET 应用程序能够生成 PDF 文档.该库使应用程序免 ...

  6. 对osg节点添加glsl特效(片断着色器FragmentShader)

    读取一个模型到节点node,然后想对node施加一些特效,这时可以只使用片段着色器 其中: gl_Color表示固定管线计算出来的颜色,包含光照效果 gl_TexCoord[]表示纹理坐标 unifo ...

  7. [原][osg][osgEarth][粒子特效]关于粒子特效库在osgEarth中,位置摆放问题,跟踪节点移动问题

    首先粒子在地球上位置摆放很简单: //传入的经纬度坐标 osg::Vec3d geoPoint; const SpatialReference* latLong = SpatialReference: ...

  8. [粒子特效]osg的自带粒子系统osgParticle::ParticleSystem

    osgParticle示例简单的演示了在osg中使用粒子系统的效果,用到了osgParticle库中的相关类,在osgParticle中主要有: (以下部分材料摘取自osg向场景中添加osgParti ...

  9. [比较老的文章]三维渲染引擎 OGRE 与 OSG 的比较综述

    1 .引言随着计算机可视化.虚拟现实技术的飞速发展,人们对实时真实感渲染以及场景复杂度提出了更高的要求.传统的直接使用底层图形接口如OpenGL.DirectX开发图形应用的模式越来越暴露出开发复杂性 ...

随机推荐

  1. C语言实现strcpy

    strcpy.h: #ifndef STRCPY_H #define STRCPY_H #include <stdio.h> char *cat_strcpy(char *dst, con ...

  2. 新霸哥带你进入java的世界

    新霸哥从近期大家的留言中注意到了大家对基础知识比较重视,很多的朋友希望多讲一些入门的知识,为了满足广大开发爱好者的需求,新霸哥决定从最基础的做起,一点一点的帮助大家一起走进云计算的世界.下面新霸哥首先 ...

  3. 黑马程序员——Foundation之NSString和NSMutableString

    ------Java培训.Android培训.iOS培训.Net培训.期待与您交流!------ 在OC中NSString是一个非常重要的字符串类;和C语言的字符串不用的是,C语言的字符串是用双引号括 ...

  4. 棒棒的毛笔字PS教程

    跟大家分享一下毛笔字怎么做出来的,主要通过字体和素材叠加,十分简单,喜欢的一起练习.做完记得交作业. 先看看最终效果: 在网上是不是经常看这些碉堡了的毛笔感觉是不是很羡慕啊,现在我就教大家怎么做出这样 ...

  5. Azure杂七杂八系列(二) - 如何在Azure上重新配置VM

    我们经常遇到这样的问题,  对于已经建立的VM进行性能提升, 比如需要更好的虚拟机或者需要迁移到其他的虚拟网络 那么我们可以使用以下的方法进行修改. 1. 如图所示, TESTVMXX位于North ...

  6. 第二百一十九天 how can I 坚持

    今天好冷,白天在家待了一天,晚上,老贾生日,生日快乐,去海底捞吃了个火锅,没感觉呢. 今天还发现了个好游戏,纪念碑谷,挺新颖,就是难度有点大了. 好累.睡觉,明天想去爬山啊,可是该死的天气.

  7. Angular 中得 scope 作用域梳理

    $scope 的使用贯穿整个 Angular App 应用,它与数据模型相关联,同时也是表达式执行的上下文.有了 $scope 就在视图和控制器之间建立了一个通道,基于作用域视图在修改数据时会立刻更新 ...

  8. oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)

    数据库返回的是结果集,存储过程返回的是一个或者多个值,所以不要使用while循环去读取,也不要使用datareader函数去调用.v_class_name是返回函数 使用.net调用oracle数据库 ...

  9. 从输入一个URL到页面呈现,网络上都发生了什么?

    归纳一下其中涉及到前端的一些基础知识,主要包括:http协议.web标准.w3c标准等.       这个问题虽然只有两个2个动作:输入URL和呈现页面,但这背后发生了很多"有趣" ...

  10. Mongo数据模型

    Mongo数据模型 一个Mongo系统(参考上述部署)包含一组数据库 一个 database 包含一组collection 一个 collection 包含一组document 一个 document ...