33 #define _def_PackedNoise true
35 namespace Hydrax{
namespace Noise
38 :
Noise(
"Perlin", true)
42 , mGPUNormalMapManager(0)
47 :
Noise(
"Perlin", true)
52 , mGPUNormalMapManager(0)
102 getTechnique(0)->getPass(0)->
103 getVertexProgramParameters()->
107 getTechnique(0)->getPass(0)->
108 getFragmentProgramParameters()->
112 getTechnique(0)->getPass(0)->
113 getFragmentProgramParameters()->
136 Ogre::TexturePtr mPerlinTexture0
137 = Ogre::TextureManager::getSingleton().
138 createManual(
"_Hydrax_Perlin_Noise0",
139 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
143 Ogre::TU_DYNAMIC_WRITE_ONLY);
145 Ogre::TexturePtr mPerlinTexture1
146 = Ogre::TextureManager::getSingleton().
147 createManual(
"_Hydrax_Perlin_Noise1",
148 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
152 Ogre::TU_DYNAMIC_WRITE_ONLY);
161 Ogre::String VertexProgramData, FragmentProgramData;
162 Ogre::GpuProgramParametersSharedPtr VP_Parameters, FP_Parameters;
163 Ogre::String EntryPoints[2] = {
"main_vp",
"main_fp"};
164 Ogre::String GpuProgramsData[2]; Ogre::String GpuProgramNames[2];
176 "float4 iPosition : POSITION,\n" +
178 "out float4 oPosition : POSITION,\n" +
179 "out float3 oPosition_ : TEXCOORD0,\n" +
180 "out float4 oWorldUV : TEXCOORD1,\n" +
181 "out float oScale : TEXCOORD2,\n" +
182 "out float3 oCameraPos : TEXCOORD3,\n" +
183 "out float3 oCameraToPixel : TEXCOORD4,\n" +
185 "uniform float4x4 uWorldViewProj,\n" +
186 "uniform float4x4 uWorld, \n" +
187 "uniform float3 uCameraPos,\n"+
188 "uniform float uScale)\n" +
190 "oPosition = mul(uWorldViewProj, iPosition);\n" +
191 "oPosition_ = iPosition.xyz;\n" +
192 "float2 Scale = uScale*mul(uWorld, iPosition).xz*0.0078125;\n" +
193 "oWorldUV.xy = Scale;\n" +
194 "oWorldUV.zw = Scale*16;\n" +
195 "oScale = uScale;\n" +
196 "oCameraPos = uCameraPos,\n" +
197 "oCameraToPixel = iPosition - uCameraPos;\n"+
213 FragmentProgramData +=
217 "float3 iPosition : TEXCOORD0,\n" +
218 "float4 iWorldCoord : TEXCOORD1,\n" +
219 "float iScale : TEXCOORD2,\n" +
220 "float3 iCameraPos : TEXCOORD3,\n" +
221 "float3 iCameraToPixel : TEXCOORD4,\n" +
223 "out float4 oColor : COLOR,\n" +
225 "uniform float uStrength,\n" +
226 "uniform float3 uLODParameters,\n" +
227 "uniform float3 uCameraPos,\n" +
228 "uniform sampler2D uNoise0 : register(s0),\n" +
229 "uniform sampler2D uNoise1 : register(s1))\n" +
231 "float Distance = length(iCameraToPixel);\n" +
232 "float Attenuation = saturate(Distance/uLODParameters.z);\n" +
234 "uLODParameters.x += (uLODParameters.y-uLODParameters.x)*Attenuation;\n"+
235 "uLODParameters.x *= iScale;\n" +
237 "float AngleAttenuation = 1/abs(normalize(iCameraToPixel).y);\n"+
238 "uLODParameters.x *= AngleAttenuation;\n"+
240 "float2 dx = float2(uLODParameters.x*0.0078125, 0);\n" +
241 "float2 dy = float2(0, dx.x);\n" +
243 "float3 p_dx, m_dx, p_dy, m_dy;\n" +
247 "iPosition.x+uLODParameters.x,\n" +
249 "tex2D(uNoise0, iWorldCoord.xy+dx).x + tex2D(uNoise1, iWorldCoord.zw+dx*16).x,\n" +
255 "iPosition.x-uLODParameters.x,\n" +
257 "tex2D(uNoise0, iWorldCoord.xy-dx).x + tex2D(uNoise1, iWorldCoord.zw-dx*16).x,\n" +
265 "tex2D(uNoise0, iWorldCoord.xy+dy).x + tex2D(uNoise1, iWorldCoord.zw+dy*16).x,\n" +
267 "iPosition.z+uLODParameters.x);\n" +
273 "tex2D(uNoise0, iWorldCoord.xy-dy).x + tex2D(uNoise1, iWorldCoord.zw-dy*16).x,\n" +
275 "iPosition.z-uLODParameters.x);\n" +
277 "uStrength *= (1-Attenuation);\n" +
278 "p_dx.y *= uStrength; m_dx.y *= uStrength;\n" +
279 "p_dy.y *= uStrength; m_dy.y *= uStrength;\n" +
281 "float3 normal = normalize(cross(p_dx-m_dx, p_dy-m_dy));\n" +
283 "oColor = float4(saturate(1-(0.5+0.5*normal)),1);\n" +
297 mNormalMapMaterial = Ogre::MaterialManager::getSingleton().create(
"_Hydrax_GPU_Normal_Map_Material", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
299 Ogre::Pass *Technique0_Pass0 = mNormalMapMaterial->getTechnique(0)->getPass(0);
301 Technique0_Pass0->setLightingEnabled(
false);
302 Technique0_Pass0->setCullingMode(Ogre::CULL_NONE);
303 Technique0_Pass0->setDepthWriteEnabled(
true);
304 Technique0_Pass0->setDepthCheckEnabled(
true);
306 GpuProgramsData[0] = VertexProgramData; GpuProgramsData[1] = FragmentProgramData;
307 GpuProgramNames[0] =
"_Hydrax_GPU_Normal_Map_VP"; GpuProgramNames[1] =
"_Hydrax_GPU_Normal_Map_FP";
311 VP_Parameters = Technique0_Pass0->getVertexProgramParameters();
313 VP_Parameters->setNamedAutoConstant(
"uWorldViewProj", Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
314 VP_Parameters->setNamedAutoConstant(
"uWorld", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
315 VP_Parameters->setNamedAutoConstant(
"uCameraPos", Ogre::GpuProgramParameters::ACT_CAMERA_POSITION_OBJECT_SPACE);
318 FP_Parameters = Technique0_Pass0->getFragmentProgramParameters();
324 ->setTextureAddressingMode(Ogre::TextureUnitState::TAM_MIRROR);
326 ->setTextureAddressingMode(Ogre::TextureUnitState::TAM_MIRROR);
328 mNormalMapMaterial->load();
380 unsigned short *Data;
382 Ogre::HardwarePixelBufferSharedPtr PixelBuffer;
384 for (
int k = 0; k < 2; k++)
389 PixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
390 const Ogre::PixelBox& PixelBox = PixelBuffer->getCurrentLock();
392 Data =
reinterpret_cast<unsigned short*
>(PixelBox.data);
396 Data[u] = 32768+
p_noise[u+Offset];
399 PixelBuffer->unlock();
414 v0, v1, v2, u0, u1, u2, f;
418 temp =
static_cast<float>(rand())/RAND_MAX;
419 tempnoise[i] = 4*(temp - 0.5f);
436 temp = (1.0f/14.0f) *
437 (tempnoise[f + v0 + u0] + tempnoise[f + v0 + u1] + tempnoise[f + v0 + u2] +
438 tempnoise[f + v1 + u0] + 6.0f*tempnoise[f + v1 + u1] + tempnoise[f + v1 + u2] +
439 tempnoise[f + v2 + u0] + tempnoise[f + v2 + u1] + tempnoise[f + v2 + u2]);
454 unsigned int image[3];
459 double dImage, fraction;
465 sum += f_multitable[i];
470 f_multitable[i] /= sum;
478 double r_timemulti = 1.0;
479 const float PI_3 = Ogre::Math::PI/3;
483 fraction = modf(
time*r_timemulti,&dImage);
484 iImage =
static_cast<int>(dImage);
486 amount[0] =
scale_magnitude*f_multitable[o]*(pow(sin((fraction+2)*PI_3),2)/1.5);
487 amount[1] =
scale_magnitude*f_multitable[o]*(pow(sin((fraction+1)*PI_3),2)/1.5);
488 amount[2] =
scale_magnitude*f_multitable[o]*(pow(sin((fraction )*PI_3),2)/1.5);
528 int iu, iup, iv, ivp, fu, fv,
558 for(i=0; i<hoct; i++)
573 pu = u >> upsamplepower,
574 pv = v >> upsamplepower,
587 return o >> (upsamplepower+upsamplepower);