将延迟数据输出到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 /延迟渲染/点光源所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)