Terrain Problem

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
locosoftware
Posts: 15
Joined: Sat Mar 08, 2008 3:15 pm
Location: Shanghai

Terrain Problem

Post by locosoftware »

I want to known how to blend different textures on each material's edge. There's many source code on Internet. But they are three or four texture types blend by pixel shader. I mean how can I blend any number of textures on each texture's edge.
The effect like this:
[img]http://www.azure.com.cn/uploads/200802/ ... scene4.jpg
[/img]
doqkhanh
Posts: 158
Joined: Sat Mar 01, 2008 3:14 am
Location: Tokyo, Japan
Contact:

Re: Terrain Problem

Post by doqkhanh »

locosoftware wrote:I want to known how to blend different textures on each material's edge.
please show me what material?
locosoftware wrote: There's many source code on Internet. But they are three or four texture types blend by pixel shader.
Can you share your url or source code? I need them too.
locosoftware
Posts: 15
Joined: Sat Mar 08, 2008 3:15 pm
Location: Shanghai

Post by locosoftware »

There's a good example in http://www.gameprojects.com/project/?id=8fc946fc5f with XNA Framework 2.0. But it's only support four texture blending. I want to make a terrain editor and can paint many textures on the terrain with irrlicht.
locosoftware
Posts: 15
Joined: Sat Mar 08, 2008 3:15 pm
Location: Shanghai

Post by locosoftware »

the problem has been solved....
doqkhanh
Posts: 158
Joined: Sat Mar 01, 2008 3:14 am
Location: Tokyo, Japan
Contact:

Post by doqkhanh »

locosoftware wrote:the problem has been solved....
May you share your solution with Irrlicht engine?
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

I'm working on something very similar:

Image

It can blend up to 32 different kinds of textures on a terrain's surface.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
doqkhanh
Posts: 158
Joined: Sat Mar 01, 2008 3:14 am
Location: Tokyo, Japan
Contact:

It's look like Terrain Texture Splatting

Post by doqkhanh »

It's look like Terrain Texture Splatting, isn't it?:
http://irrlicht.sourceforge.net/phpBB2/ ... rain+splat
Ktrainers
Posts: 10
Joined: Sun Mar 23, 2008 5:46 pm

Post by Ktrainers »

I've been trying to do the texture splatting on terrain using a pixel shader but none of the details in my textures come out. It does use the blend map to determine where the textures are to be placed on the terrain, however the textures lose all of their details. For example, instead of having a dirt road that has cracks, twigs, and other detail like it does in the .jpg texture file, it just comes up as a brown color on the terrain with no detail at all. This gives my terrain a very smooth look that does not look realistic. I was wondering if anyone could tell me, why I'm losing the detail in my textures.

I add a terrain Scene Node and set the material textures and the material type with the following code:

Code: Select all

scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
      "media/terrain-heightmap.bmp",
      0,                  // parent node
      -1,                  // node id
      core::vector3df(0.f, 0.f, 0.f),         // position
      core::vector3df(0.f, 0.f, 0.f),         // rotation
      core::vector3df(40.0f, 4.4f, 40.0f),      // scale
      video::SColor ( 255, 255, 255, 255 ),   // vertexColor,
      5,                  // maxLOD
      scene::ETPS_17,               // patchSize
      5                     // smoothFactor
      );

terrain->setMaterialTexture(0,driver->getTexture("media/blend_hm17.jpg"));   terrain->setMaterialTexture(1,driver->getTexture("media/grass.jpg"));
terrain->setMaterialTexture(2,driver->getTexture("media/dirt.jpg"));
terrain->setMaterialTexture(3,driver->getTexture("media/stone.jpg"));

s32 newMaterialType = driver->getGPUProgrammingServices()->addHighLevelShaderMaterialFromFiles( 0,0,video::EVST_VS_2_0,"TerrainPS.psh","ps_main",video::EPST_PS_2_0,0video::EMT_SOLID);

terrain->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType); 
My pixel Shader looks like this:

Code: Select all

float4x4 matViewProjection : ViewProjection;
float texScale = 10.0;


sampler AlphaMap = sampler_state
{

MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureOne = sampler_state
{
 
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureTwo = sampler_state
{
   
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureThree = sampler_state
{
   
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};




struct PS_OUTPUT
{
   float4 diffuse : COLOR0;
};



PS_OUTPUT ps_main(float shade : TEXCOORD0,
        float2 tiledTexC : TEXCOORD1,
        float2 nonTiledTexC : TEXCOORD0) : COLOR
{
   PS_OUTPUT output = (PS_OUTPUT)0;

   // Layer maps are tiled
   
       float3 c0 = tex2D(TextureOne, mul(tiledTexC,texScale));
       float3 c1 = tex2D(TextureTwo, mul(tiledTexC,texScale));
           float3 c2 = tex2D(TextureThree, mul(tiledTexC,texScale));
   
   
      // Blendmap is not tiled.
    float3 B = tex2D(AlphaMap, nonTiledTexC);
 
 
  // Find the inverse of all the blend weights so that we can
     // scale the total color to the range [0, 1].
      float totalInverse = 1.0f / (B.r + B.g + B.b);
     
      c0 *= B.r * totalInverse;
     c1 *= B.g * totalInverse;
      c2 *= B.b * totalInverse;
     
    float3 final = (c0 + c1 + c2);//* shade;


   output.diffuse = float4(final,1.0f);

   return output;
}

technique Default_DirectX_Effect
{
   pass Pass_0
   {
      PixelShader = compile ps_2_0 ps_main();
   }
} 
Any help with this would be much appreciated. Thanks!
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Both of those can only combine 3 or 4 texture types on a terrain surface. Mine can theoretically do up to 256 different textures (But doing that much would cost quality/performance, the recommended amount is 8-32).

Cheers

@KTrain: You need to scale your texture coordinates, try setting texScale to a higher number.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Ktrainers
Posts: 10
Joined: Sun Mar 23, 2008 5:46 pm

Post by Ktrainers »

Hmmm.... changing textScale doesn't seem to be affecting anything. Here's what my terrain looks like:
Image

And here are some of the textures I'm using:

Image

Image

As you can see, it loses alot of detail.

Any suggestions?
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Lets see your vertex shader, I bet your not setting the second set of texcoords correctly.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Ktrainers
Posts: 10
Joined: Sun Mar 23, 2008 5:46 pm

Post by Ktrainers »

Okay here's my vertex shader:

Code: Select all

float4x4 matViewProjection : ViewProjection;
float texScale = 100.0;
uniform extern float2   gTexScale;
uniform extern float3   gDirToSunW; // Assumed to be unit length.

sampler AlphaMap = sampler_state
{
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureOne = sampler_state
{
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureTwo = sampler_state
{
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};

sampler TextureThree = sampler_state
{
   MipFilter = LINEAR;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   ADDRESSU = WRAP;
   ADDRESSV = WRAP;
   ADDRESSW = WRAP;
};


struct VS_OUTPUT
{
   float4 Position : POSITION0;
   float2 alphamap : TEXCOORD0;
   float2 tex : TEXCOORD1;
   float2 nonTex : TEXCOORD2;
};


VS_OUTPUT vs_main( float4 posL    : POSITION0, 
                            float3 normalL: NORMAL0,  
                            float2 tiledTexC    : TEXCOORD1,
                            float2 nonTiledTexC : TEXCOORD2,
                            float2 alpha : TEXCOORD0) 
{
   VS_OUTPUT Output = (VS_OUTPUT)0;
  
   Output.alphamap = alpha;

  
  // Project position homogeneous clip space.
      Output.Position  = mul(posL, matViewProjection); 
      
      
      // Scale tiled tex-coords.
      Output.tex.x  = tiledTexC.x * texScale;
      Output.tex.y  = tiledTexC.y * texScale;
      
      // Forward non-tiled tex-coords to pixel shader.
    Output.nonTex = nonTiledTexC;

   return( Output );
}

technique Default_DirectX_Effect
{
   pass Pass_0
   {
      VertexShader = compile vs_2_0 vs_main();
   }
}

Hope this helps. Thanks!
locosoftware
Posts: 15
Joined: Sat Mar 08, 2008 3:15 pm
Location: Shanghai

Post by locosoftware »

BlindSide wrote:I'm working on something very similar:

Image

It can blend up to 32 different kinds of textures on a terrain's surface.
Yes! This effect is exactly I want. But I didn't figure out how to implement it.
May you share your solution with Irrlicht engine?
Last edited by locosoftware on Wed Mar 26, 2008 6:47 am, edited 1 time in total.
locosoftware
Posts: 15
Joined: Sat Mar 08, 2008 3:15 pm
Location: Shanghai

Post by locosoftware »

doqkhanh wrote:
locosoftware wrote:the problem has been solved....
May you share your solution with Irrlicht engine?
The effect is still unsuccessful. :cry:
locosoftware
Posts: 15
Joined: Sat Mar 08, 2008 3:15 pm
Location: Shanghai

Post by locosoftware »

Post Reply