c – Ogre3d 延迟渲染点光源

c – Ogre3d 延迟渲染点光源,第1张

概述我试图使用ogre compositor框架设置延迟渲染器.我尝试在下面的代码中实现一个点光着色器(作为全屏四边形效果,没有衰减或镜面反射计算): 将延迟数据输出到GBuffer的材料: void ToGBufferVP ( float4 iPosition : POSITION, fl 我试图使用ogre compositor框架设置延迟渲染器.我尝试在下面的代码中实现一个点光着色器(作为全屏四边形效果,没有衰减或镜面反射计算):

将延迟数据输出到GBuffer的材料:

voID ToGBufferVP                (                    float4 iposition : position,float3 inormal : norMAL,float2 iUV0 : TEXCOORD,out float4 oposition : position,out float3 oVIEwPos : TEXCOORD0,out float3 onormal : TEXCOORD1,out float2 oUV0 : TEXCOORD2,uniform float4x4 cWorldVIEwProj,uniform float4x4 cWorldVIEw                ){    oposition = mul(cWorldVIEwProj,iposition);    onormal = mul(cWorldVIEw,float4(inormal,0)).xyz;    oVIEwPos = mul(cWorldVIEw,iposition).xyz;    oUV0 = iUV0;}voID ToGBufferFP                (                    float3 iVIEwPos : TEXCOORD0,float3 inormal : TEXCOORD1,float2 iUV0 : TEXCOORD2,out float4 ocolor0 : color0,out float4 ocolor1 : color1,uniform sampler2D sTex : register(s0),uniform sampler2D sspec : register(s1),uniform float cFardistance                ){    ocolor0.rgb = tex2D(sTex,iUV0);    ocolor0.a = tex2D(sspec,iUV0);    ocolor1.rgb = normalize(inormal);    ocolor1.a = length(iVIEwPos) / cFardistance;}

顶点程序说明:

vertex_program ScreenQuadDeBUGlight_VS cg{    source MyDeferredpostshader.hlsl    profiles vs_1_1 arbvp1    entry_point ScreenQuadDeBUGlight_VS    default_params    {        param_named_auto worldVIEwProj worldvIEwproj_matrix    }}

片段程序说明:

fragment_program ScreenQuadDeBUGlight_PS cg{    source MyDeferredpostshader.hlsl    profiles ps_2_0 arbfp1    entry_point ScreenQuadDeBUGlight_PS    default_params    {        param_named_auto vpWIDth vIEwport_wIDth        param_named_auto vpHeight vIEwport_height               param_named_auto flip render_target_flipPing        param_named_auto farClipdistance far_clip_distance        param_named_auto lightPos light_position_vIEw_space 0    }}

轻材料脚本:

material DeferredShadingPostQuadlight{    technique    {        pass        {            cull_harDWare none            cull_software none            depth_func always_pass            vertex_program_ref ScreenQuadDeBUGlight_VS            {            }            fragment_program_ref ScreenQuadDeBUGlight_PS            {            }            texture_unit            {                tex_coord_set 0                tex_address_mode clamp                filtering none            }            texture_unit            {                tex_coord_set 1                tex_address_mode clamp                filtering none            }        }    }}

浅色着色器:

voID ScreenQuadDeBUGlight_VS    (        float4 Pos: position,out float4 oPos: position,out float4 oTexCoord : TEXCOORD0,uniform float4x4 worldVIEwProj    ){    float4 projpos = mul(worldVIEwProj,Pos);    oTexCoord = projpos;        oPos = projpos;}float4 ScreenQuadDeBUGlight_PS    (        float4 projpos : TEXCOORD0,uniform sampler Tex0: register(s0),uniform sampler Tex1: register(s1),uniform float vpWIDth,uniform float vpHeight,uniform float flip,uniform float farClipdistance,uniform float3 lightPos    ) : color {    // Get homogenous coordinates    projpos.xy /= projpos.w;    // Compensate texture coordinate half pixel jitter    float2 texCoord = 0.5f * (float2(projpos.x,-projpos.y) + 1);    float2 halfPixel = float2(0.5/vpWIDth,0.5/vpHeight);    texCoord += halfPixel;    float3 ray = float3(projpos.x,projpos.y * flip,1);    float4 a0 = tex2D(Tex0,texCoord); // Albedo and specularity    float4 a1 = tex2D(Tex1,texCoord); // normal and Depth    // Attributes    float3 colour = a0.rgb;    float specularity = a0.a;    float distance = a1.w;    float3 normal = a1.xyz;    float3 vIEwPos = normalize(ray);    vIEwPos.z = distance;    float3 objTolightVec =  lightPos - vIEwPos;    float len_sq = dot(objTolightVec,objTolightVec);    float len = sqrt(len_sq);    float3 objTolightDir = normalize(objTolightVec);    float3 total_light_contrib;    total_light_contrib = max(0.0,dot(objTolightDir,normal));    return float4(total_light_contrib,0.0);}

这就是我在.cpp文件中声明光的方式:

llightSceneNodeHolder = mSceneMgr->getRootSceneNode()->createChildSceneNode();Ogre::light *light;light = mSceneMgr->createlight();light->setType(Ogre::light::LT_POINT);light->setposition(Ogre::Vector3(0,-0.7f));light->setVisible(true);light->setDiffuseColour(Ogre::ColourValue::White);light->setspecularColour(Ogre::ColourValue::White);llightSceneNodeHolder->attachObject(light);

我得到输出,一切正常 – 除了我不能让灯光正常工作. G Buffer包含有效数据 – 视图空间法线,线性z深度,纹理.我也将视图空间中的光位置作为参数 – 但是在矢量计算期间存在一些问题 – 并且输出与点光源不同.我在这做错了什么?

谢谢!

附:我也尝试通过合成器监听器手动传递lightPos参数,但随后灯光看起来更像是定向灯……

解决方法 问题在于:

float3 ray = float3(projpos.x,1);

它必须乘以farCorner值,这是相机视锥的远角:

float3 ray = float3(projpos.x,1)* farCorner;

你可以通过使用获得它

mCamera-> getWorldspaceCorners()[1];

然后将它插入到compositor侦听器中,如下所示:

voID lightListener::notifyMaterialSetup(Ogre::uint32 pass_ID,Ogre::MaterialPtr &mat){    vpParams = mat->getBestTechnique()->getpass(0)->getVertexProgramParameters();    fpParams = mat->getBestTechnique()->getpass(0)->getFragmentProgramParameters();}voID lightListener::notifyMaterialRender(Ogre::uint32 pass_ID,Ogre::MaterialPtr &mat){    vpParams->setnamedConstant("lightPos",lightPos);    fpParams->setnamedConstant("farCorner",mCamera->getWorldspaceCorners()[1]);}
总结

以上是内存溢出为你收集整理的c – Ogre3d /延迟渲染/点光源全部内容,希望文章能够帮你解决c – Ogre3d /延迟渲染/点光源所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1226428.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存