OGRE CG教程 (三): 渐隐效果

Description

This shader applies to an object a color that fade to dark when getting away from the camera. I use it for large object, to emphase the feeling of largeness. The object is not textured, but just colorized.

这个shader可以为物体创造渐隐效果.把它用在大的物体上,可以体现出物体的大.这个物体并没有纹理,它只是被着色了.


Usage

To use the shader, you will need to give 2 custom parameters:

  • the color of the object.
  • the radius of the object.

为了使用这个shader,我们需要两个custom变量

  • 物体的颜色
  • 物体的半径

And then apply the shader/gradient material to your object. 

接下来我们在程序中应用纹理.
Example :

例子:

Vector4 vColour = Vector4(circlecolour.r, circlecolour.g, circlecolour.b, circlecolour.a);
 Vector4 vRadius = Vector4(mRadius,0,0,0);
 mObject->setCustomParameter(0,vColour);
 mObject->setCustomParameter(1,vRadius);
 mObject->setMaterialName("shader/gradient");

Gradientshader.cg

// --------------------------------------------------------------------
 // Gradient shader : vertex program
 // Author : David de Lorenzo
 // -----------------------------------------------------------------
 void gradient_vp( in      float4   position  : POSITION,
                  uniform float4x4 worldViewProj,  //物体坐标->世界坐标  的转换矩阵
                  uniform float3   camera_position_object_space,  //摄像机在物体空间下的坐标
                  uniform float4   customParamColour,
                  uniform float4   customParamRadius,
                  out     float4   oPosition : POSITION,  //输出的位置
                  out     float4   oColor    : COLOR  //输出的颜色
              )
 {
     // Get the custom parameters
     float  objectradius = customParamRadius.x;  //获得渐隐
 
     // calculate output position
     oPosition = mul(worldViewProj, position);  //计算输出的位置
 
     // Calculate the color value, depending on the position  根据摄像机位置计算颜色
     float3 vect_center_to_cam =  camera_position_object_space;  
     float3 position_closest   =  normalize(vect_center_to_cam) * objectradius;  //把摄像机向量单位化再乘以渐隐的半径,获得最靠近的位置
 
     //Rc其实就是两个向量的点积  与摄像机夹角越大,值就越小
     float Rc = (position_closest.z * position.z ) + (position_closest.y * position.y ) + (position_closest.x * position.x);  // Ratio on Center-to-Cam axis
     Rc =  Rc / (objectradius * objectradius);   // recalibrate (-1..1)  如果渐隐半径比物体小的话,获得的值就会超过1
     float ratio  = (1+Rc);                      // recalibrate ( 0..2) - (x2 to emphasis the color)
 
     oColor= ratio * customParamColour;
 }
 
 // -----------------------------------------------------------------
 // Gradient shader : fragment program
 // Author : David de Lorenzo
 // -----------------------------------------------------------------
 float4 main_fp(in  float4 color  : COLOR) :  COLOR0
 {
   return (color);
 }

下面分析下default_params的地方:

    default_params那段代码。这段定义了在执行期给着色器的参数。param_named_auto 定义了一个被Ogre 3D自动传给着色器的参数【这里的参数是指非语义指定的由渲染管线自动填充数值的参数,而是指由程序显示传进来的参数,此参数和c语义函数的参数意义一样】。
    关键字uniform表示参数在每次绘图调用是恒定的【一次完整的绘制过程中是恒定的,但下次完整的绘制时可能是另一个值】。

Gradientshader.material

vertex_program shader/gradientVP cg
 {
      source gradientshader.cg
     entry_point gradient_vp
     profiles vs_1_1
 
     default_params    
     {
        param_named_auto worldViewProj  worldviewproj_matrix
           param_named_auto camera_position_object_space camera_position_object_space  //摄像机在物体坐标系下的坐标
        }
 } 
 
 fragment_program shader/gradientFP cg
 {
     source gradientshader.cg
     entry_point main_fp
     profiles ps_1_1
 }
 
 material shader/gradient
 { 
      technique
     {
         pass
         {
             vertex_program_ref shader/gradientVP
             {
                          param_named_auto customParamColour custom 0
                          param_named_auto customParamRadius custom 1
             }
             fragment_program_ref shader/gradientFP
             {
             }
         }
     }
 }














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OGRE(O-O Graphics Rendering Engine) 使用指南________________________________________________________________1 作者_________________________________________________________________________ 6 序____________________________________________________________________________7 教程的目的_________________________________________________________________ 7 读者要求___________________________________________________________________ 7 教程的由来_________________________________________________________________ 7 OGRE简介___________________________________________________________________ 8 OGRE特点_________________________________________________________________ 8 效率特性__________________________________________________________________8 平台和3D API 支持________________________________________________________ 8 网格Meshes_______________________________________________________________ 8 场景特性__________________________________________________________________9 特效______________________________________________________________________9 其它特性__________________________________________________________________9 OGRE中的模块_____________________________________________________________ 9 OgreMain 模块____________________________________________________________ 10 SDL 平台管理模块________________________________________________________ 11 Win32 平台管理模块_______________________________________________________11 BSP 场景管理_____________________________________________________________11 文件系统插件_____________________________________________________________11 GuiElement 插件__________________________________________________________ 11 OctreeSceneManager 插件___________________________________________________ 11 ParticleFX 插件___________________________________________________________ 12 Direct3D7 渲染系统插件____________________________________________________12 Direct3D8 渲染系统插件____________________________________________________12 SDL 渲染系统插件________________________________________________________ 12 3ds2oof 工具______________________________________________________________12 3Dstudio Max 导出器_______________________________________________________12 位图字体创建工具_________________________________________________________12 Milkshape3D 导出器_______________________________________________________ 12 Python 接口______________________________________________________________ 13 XML 转换器______________________________________________________________13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值