chap8_2 Fresnel in OGRE
Fresnel效应就是折射与反射的结合,当入射光与平面法向量重合时,反射占主要地位,当视角与平面角度接近180充时,折射占主要地位.具体的原理在DX实现时里面讲解.
/*------------------------------------------------------------main.cpp -- fresnel in OGRE(c) Seamanj.2013/8/18
------------------------------------------------------------*/
//phase1 : Add Framework
//phase2 : Add environment
//phase3 : Add water
//phase4 : add props#define phase1 1
#define phase2 1
#define phase3 1
#define phase4 1#if phase1
#include "ExampleApplication.h"
#include <windows.h>
#if phase2
#include "SdkCameraMan.h"
#endif
#if phase4
#include "SdkTrays.h"
#endif
class MyApplication : public ExampleApplication
#if phase3, public RenderTargetListener
#endif
{
#if phase3RenderTarget* mRefractionTarget;RenderTarget* mReflectionTarget;Plane mWaterPlane;Entity* mWater;std::vector<Entity*> mSurfaceEnts;std::vector<Entity*> mSubmergedEnts;
#endifpublic:MyApplication(){}~MyApplication(){}
#if phase3void preRenderTargetUpdate(const RenderTargetEvent& evt){mWater->setVisible(false); // hide the waterif (evt.source == mReflectionTarget) // for reflection, turn on camera reflection and hide submerged entities{mCamera->enableReflection(mWaterPlane);for (std::vector<Entity*>::iterator i = mSubmergedEnts.begin(); i != mSubmergedEnts.end(); i++)(*i)->setVisible(false);}else // for refraction, hide surface entities{for (std::vector<Entity*>::iterator i = mSurfaceEnts.begin(); i != mSurfaceEnts.end(); i++)(*i)->setVisible(false);}}void postRenderTargetUpdate(const RenderTargetEvent& evt){mWater->setVisible(true); // unhide the waterif (evt.source == mReflectionTarget) // for reflection, turn off camera reflection and unhide submerged entities{mCamera->disableReflection();for (std::vector<Entity*>::iterator i = mSubmergedEnts.begin(); i != mSubmergedEnts.end(); i++)(*i)->setVisible(true);}else // for refraction, unhide surface entities{for (std::vector<Entity*>::iterator i = mSurfaceEnts.begin(); i != mSurfaceEnts.end(); i++)(*i)->setVisible(true);}}
#endif
protected:void createScene(){
#if phase2mCamera->setPosition(-50, 125, 760);mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");// create lightLight* l = mSceneMgr->createLight();l->setType(Light::LT_DIRECTIONAL);l->setDirection(Vector3::NEGATIVE_UNIT_Y);
#if phase3// create our reflection & refraction render textures, and setup their render targetsfor (unsigned int i = 0; i < 2; i++){TexturePtr tex = TextureManager::getSingleton().createManual(i == 0 ? "refraction" : "reflection",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, 512, 512, 0, PF_R8G8B8, TU_RENDERTARGET);RenderTarget* rtt = tex->getBuffer()->getRenderTarget();rtt->addViewport(mCamera)->setOverlaysEnabled(false);rtt->addListener(this);if (i == 0) mRefractionTarget = rtt;else mReflectionTarget = rtt;}// create our water plane meshmWaterPlane = Plane(Vector3::UNIT_Y, 0);MeshManager::getSingleton().createPlane("water", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,mWaterPlane, 700, 1300, 10, 10, true, 1, 3, 5, Vector3::UNIT_Z);// create a water entity using our mesh, give it the shader material, and attach it to the originmWater = mSceneMgr->createEntity("Water", "water");mWater->setMaterialName("Examples/FresnelReflectionRefraction");//知识点1
/*知识点1:Fresnel材质
---------------------------------------------
来自ogre_src_v1-7-4\Samples\Media\materials\scripts\Examples-Advanced.material文件
---------------------------------------------
//----------------------------
// Distortion effects
//----------------------------vertex_program Examples/FresnelRefractReflectVP cg
{source Example_Fresnel.cg//知识点2entry_point main_vpprofiles vs_1_1 arbvp1
}
vertex_program Examples/FresnelRefractReflectVPold cg
{source Example_Fresnel.cgentry_point main_vp_oldprofiles vs_1_1 arbvp1
}fragment_program Examples/FresnelRefractReflectFP cg
{source Example_Fresnel.cgentry_point main_fp// sorry, ps_1_1 and fp20 can't do thisprofiles ps_2_0 arbfp1
}fragment_program Examples/FresnelRefractReflectPS asm
{source Example_FresnelPS.asm// sorry, only for ps_1_4 :)syntax ps_1_4}
material Examples/FresnelReflectionRefraction
{// ps_2_0 / arbfp1technique{pass {vertex_program_ref Examples/FresnelRefractReflectVP{param_named_auto worldViewProjMatrix worldviewproj_matrixparam_named_auto eyePosition camera_position_object_spaceparam_named_auto timeVal time 0.05param_named scroll float 1 param_named scale float 1 param_named noise float 1 // scroll and noisePos will need updating per frame}fragment_program_ref Examples/FresnelRefractReflectFP{param_named fresnelBias float -0.1 param_named fresnelScale float 1.8 param_named fresnelPower float 8 param_named tintColour float4 0 0.05 0.05 1param_named noiseScale float 0.05 }// Noisetexture_unit{// Perlin noise volumetexture waves2.dds// min / mag filtering, no mipfiltering linear linear none}// Reflectiontexture_unit{// Will be filled in at runtimetexture reflectiontex_address_mode clamp}// Refractiontexture_unit{// Will be filled in at runtimetexture refractiontex_address_mode clamp}}}// ATI 8500 +technique{pass{vertex_program_ref Examples/FresnelRefractReflectVPold{param_named_auto worldViewProjMatrix worldviewproj_matrixparam_named_auto eyePosition camera_position_object_spaceparam_named fresnelBias float -0.3param_named fresnelScale float 1.4param_named fresnelPower float 8param_named_auto timeVal time_0_1 20param_named scroll float 1 param_named scale float 4 param_named noise float 1// scroll and noisePos will need updating per frame}// for ATI RADEON 8500 - 9200fragment_program_ref Examples/FresnelRefractReflectPS{// distortionRangeparam_indexed 0 float 0.025 // tintColourparam_indexed 1 float4 0.05 0.12 0.15 1}// Noisetexture_unit{// Perlin noise volumetexture perlinvolume.dds 3d// min / mag filtering, no mipfiltering linear linear none}// Reflectiontexture_unit{// Will be filled in at runtimetexture Reflectiontex_address_mode clamp}// Refractiontexture_unit{// Will be filled in at runtimetexture Refractiontex_address_mode clamp}}}
}
*//*知识点2:Fresnel shader
---------------------------------------------
来自ogre_src_v1-7-4\Samples\Media\materials\programs\Example_Fresnel.cg文件
---------------------------------------------
// Vertex program for fresnel reflections / refractions
void main_vp(float4 pos : POSITION,float4 normal : NORMAL,float2 tex : TEXCOORD0,out float4 oPos : POSITION,out float3 noiseCoord : TEXCOORD0,out float4 projectionCoord : TEXCOORD1,out float3 oEyeDir : TEXCOORD2, out float3 oNormal : TEXCOORD3, uniform float4x4 worldViewProjMatrix,uniform float3 eyePosition, // object spaceuniform float timeVal,uniform float scale, // the amount to scale the noise texture byuniform float scroll, // the amount by which to scroll the noiseuniform float noise // the noise perturb as a factor of the time)
{oPos = mul(worldViewProjMatrix, pos);// Projective texture coordinates, adjust for mappingfloat4x4 scalemat = float4x4(0.5, 0, 0, 0.5, 0,-0.5, 0, 0.5,0, 0, 0.5, 0.5,0, 0, 0, 1);projectionCoord = mul(scalemat, oPos);// Noise map coordsnoiseCoord.xy = (tex + (timeVal * scroll)) * scale;noiseCoord.z = noise * timeVal;oEyeDir = normalize(pos.xyz - eyePosition); oNormal = normal.rgb; }// Fragment program for distorting a texture using a 3D noise texture
void main_fp(float3 noiseCoord : TEXCOORD0,float4 projectionCoord : TEXCOORD1,float3 eyeDir : TEXCOORD2,float3 normal : TEXCOORD3,out float4 col : COLOR,uniform float4 tintColour,uniform float noiseScale, uniform float fresnelBias,uniform float fresnelScale,uniform float fresnelPower,uniform sampler2D noiseMap : register(s0),uniform sampler2D reflectMap : register(s1),uniform sampler2D refractMap : register(s2))
{// Do the tex projection manually so we can distort _after_float2 final = projectionCoord.xy / projectionCoord.w;// Noisefloat3 noiseNormal = (tex2D(noiseMap, (noiseCoord.xy / 5)).rgb - 0.5).rbg * noiseScale;final += noiseNormal.xz;// Fresnel//normal = normalize(normal + noiseNormal.xz);float fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);// Reflection / refractionfloat4 reflectionColour = tex2D(reflectMap, final);float4 refractionColour = tex2D(refractMap, final) + tintColour;// Final colourcol = lerp(refractionColour, reflectionColour, fresnel);}// Old version to match ATI PS 1.3 implementation
void main_vp_old(float4 pos : POSITION,float4 normal : NORMAL,float2 tex : TEXCOORD0,out float4 oPos : POSITION,out float fresnel : COLOR,out float3 noiseCoord : TEXCOORD0,out float4 projectionCoord : TEXCOORD1,uniform float4x4 worldViewProjMatrix,uniform float3 eyePosition, // object spaceuniform float fresnelBias,uniform float fresnelScale,uniform float fresnelPower,uniform float timeVal,uniform float scale, // the amount to scale the noise texture byuniform float scroll, // the amount by which to scroll the noiseuniform float noise // the noise perturb as a factor of the time)
{oPos = mul(worldViewProjMatrix, pos);// Projective texture coordinates, adjust for mappingfloat4x4 scalemat = float4x4(0.5, 0, 0, 0.5, 0,-0.5, 0, 0.5,0, 0, 0.5, 0.5,0, 0, 0, 1);projectionCoord = mul(scalemat, oPos);// Noise map coordsnoiseCoord.xy = (tex + (timeVal * scroll)) * scale;noiseCoord.z = noise * timeVal;// calc fresnel factor (reflection coefficient)float3 eyeDir = normalize(pos.xyz - eyePosition);fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);}
*/mSceneMgr->getRootSceneNode()->attachObject(mWater);
#endif#if phase4Entity* ent;ent = mSceneMgr->createEntity("UpperBath", "RomanBathUpper.mesh" );mSceneMgr->getRootSceneNode()->attachObject(ent); mSurfaceEnts.push_back(ent);ent = mSceneMgr->createEntity("Columns", "columns.mesh");mSceneMgr->getRootSceneNode()->attachObject(ent); mSurfaceEnts.push_back(ent);ent = mSceneMgr->createEntity("Head", "ogrehead.mesh");ent->setMaterialName("RomanBath/OgreStone");mSurfaceEnts.push_back(ent);SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();headNode->setPosition(-350, 55, 130);headNode->yaw(Degree(90));headNode->attachObject(ent);ent = mSceneMgr->createEntity("LowerBath", "RomanBathLower.mesh");mSceneMgr->getRootSceneNode()->attachObject(ent);mSubmergedEnts.push_back(ent);#endif
#endif}
};INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{MyApplication app;try{app.go();}catch( Exception& e){MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);}return 0;
}#endif
运行结果:
chap8_2 Fresnel in OGRE相关推荐
- [导入]了解OGRE——起步常见问题
Original version by Antiarc (28 Dec 2004) 简体中文译本由aaron@ogre3d.cn翻译(2007年4月16日),也就是acer@gameres.com,是 ...
- OGRE 2.1 Windows 编译
版权所有,转载请注明链接 OGRE 2.1 Windows 编译 环境: Windows 7 64Bit Visual Studio 2012 OGRE 2.1 CMake 2.8.12.1 OGRE ...
- (转)修改ETM,用Ogre实现《天龙八部》地形与部分场景详解(附源码)
本文主要讲的是<天龙八部>游戏的地形和一部分场景的具体实现,使用C++, Ogre1.6,我摸索了段时间,可能方法用的并不是最好的,但好歹实现了.文章可能讲得有点罗嗦,很多简单的东西都讲了 ...
- Ogre貌似开始推荐MYGUI了~~
近段时间出差,回来突然发现Ogre的wiki上多了mygui的介绍~虽然以前下1.7的时候就有预感CEGUI即将完蛋,不过感觉偶还是预测的蛮准的~~不过我也算是后知后觉了,这个mygui的更新时间是1 ...
- 【转】OGRE资源相关分析
原文地址:http://blog.csdn.net/pizi0475/archive/2010/02/23/5319890.aspx Resource Resource的类继承体系如下: Resour ...
- Ogre 2011-11-29
添加R按键改变绘制模式,播放动画 class Example25FrameListener : public Ogre::FrameListener { public:Example25FrameLi ...
- 《Pro Ogre 3D Programming》读书笔记 之 第十章 布告板与粒子 第一部分 (转)
布告板(Billboard) Ogre中的布告板简单的来讲就是场景中的一个四边形,但它的朝向与相机有关.通常布告板随着相机的视方向旋转以便与相机的视线方向对齐.把布告板放在场景的任何地方,它将会朝向相 ...
- OGRE例子:Demo_Ocean 源码解析
因为最近在做水,所以这篇海洋的例子是必须要读懂的.耐着性子看了一下,还是能看懂的.现在将代码解析放上来.程序共有四个文件:MaterialControls.h :MaterialControls.cp ...
- Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)
主界面如上文设计完成后,场景刚开始添加了是Ogre例子里的,发现场景里实物太少,于是想到直接把天龙的场景拿下来,天龙网上有源码,参考了下,把天龙的地形用Ogre的地形组件完成了下,如下是效果图: 因为 ...
最新文章
- Python 源码函数格式带有*和/说明
- 2018年第九届省赛C/C++A组第2题——星期一
- 公用ip地址查询_是什么使您无法更改公用IP地址并在Internet上造成严重破坏?
- 还在纠结Dapper或者EF Core?不妨试试“混合ORM”--RepoDb
- linux .forward,linux forward的实现
- SQL Server 字段类型Char Varchar 比较
- matlab用mkdir在指定的文件夹下创建新的文件夹,并把图像保存在该文件夹内
- 探针台常见问题—如何减少LHe制冷剂消耗
- 工作簿中工作表内容批量提取
- python语言采用________方式运行程序_静态语言采用________方式执行,如C语言;脚本语言采用________方式执行,如Python语言。_学小易找答案...
- 网店三大要素:产品、运营与品牌
- 服务器代码review要点
- 使用两个路由器级连实现共用一个宽带
- python语言工具_可爱的 Python
- 华为在俄罗斯开设基于ARM技术的数据中心:技术之外,环境使然?
- antd从v2升级到v3
- Juniper初始化之配置管理接口
- 我给鸿星尔克写了一个720°全景看鞋展厅
- 加油站优惠券组合方式
- defined but not used [-Wunused-function] 使用 __attribute__((unused)) 告诉编译器忽略此告警