Problem with HLSL shader and terrain scene node

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
mits
Posts: 8
Joined: Tue Jan 16, 2007 9:53 pm

Problem with HLSL shader and terrain scene node

Post by mits »

Hi, I am new to this forum. I am dealing with the HLSL for the first time so I made this for texture blending using a heightmap:

Code: Select all

float4x4 matViewProjection;

struct VS_INPUT 
{
   float4 Position : POSITION0;
   float2 inTxr1 : TEXCOORD0;
   
};

struct VS_OUTPUT 
{
   float4 Position : POSITION0;
   float2 inTxr1 : TEXCOORD0;
};

VS_OUTPUT vs_main( VS_INPUT Input )
{
   VS_OUTPUT Output;

   Output.Position = mul( Input.Position, matViewProjection );
   Output.inTxr1 = Input.inTxr1;
   
   return( Output );
}



// The constants
float minHeight1 = 0.0f;
float maxHeight1 = 0.7f;
float minHeight2 = 0.3f;
float maxHeight2 = 1.0f;
 
// Calculate percentage1
float Percentage1(float height, float minHeight, float maxHeight)
{
   // The percentage
   float p = 0.0f;
   
   // Check to see if it's in the boundaries
   if(height<=maxHeight)
   {
      // Calculate percentage
      p = 1.0f - (height/(maxHeight - minHeight));
   }
   
   // Return it
   return p;
}
   
     
// Calculate percentage2
float Percentage2(float height, float minHeight, float maxHeight)
{
   // The percentage
   float p = 0.0f;
   
   // Check to see if it's in the boundaries
   if(height>=minHeight)
   {
      // Calculate percentage
      p = (height - minHeight)/(maxHeight - minHeight);
   }
   
   // Return it
   return p;
}

// Ground texture
sampler Texture0 : register(s0)  
{
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

// Grass texture
sampler Texture1 : register(s1)
{
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

// Heightmap texture
sampler Texture2 : register(s2)
{
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

float4 ps_main(float2 inTxr1 : TEXCOORD0) : COLOR0
{   
   // Get the height from heightmap
   float height = tex2D(Texture2, inTxr1).x;
   
   // The percentages
   float p1 = Percentage1(height, minHeight1, maxHeight1);
   float p2 = Percentage2(height, minHeight2, maxHeight2);
   
   return clamp( (p1 * tex2D(Texture0, inTxr1)) + (p2 * tex2D(Texture1, inTxr1)), 0, 1);
}



This works fine in RenderMonkey. But when I use these in Irrlicht :

Code: Select all


// Add a terrain node and set its properties
	scene::ITerrainSceneNode *terrain = m_Smgr->addTerrainSceneNode("E:\\Program Files\\Mitsos\\irrlicht-1.2\\media\\terrain-heightmap.bmp");
	terrain->setScale(core::vector3df(10.0f, 0.3f, 10.0f));
	terrain->setMaterialFlag(video::EMF_LIGHTING, false);
	terrain->setMaterialTexture(0, m_Driver->getTexture("E:\\Program Files\\Mitsos\\irrlicht-1.2\\media\\texture-ground1.bmp"));
	terrain->setMaterialTexture(1, m_Driver->getTexture("E:\\Program Files\\Mitsos\\irrlicht-1.2\\media\\drygrass.bmp"));
	terrain->setMaterialTexture(2, m_Driver->getTexture("E:\\Program Files\\Mitsos\\irrlicht-1.2\\media\\terrain-heightmap.bmp"));

	terrain->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType1);
	terrain->scaleTexture(10.0f, 10.0f);

the terrain appears white. The shader callback has been setup up according the shader tutorial and the only shader constant I am passing to the shader from my C++ code is the matViewProjection. Any idea what it might be wrong? Thanks in advance.
sdi2000
Posts: 129
Joined: Thu Aug 25, 2005 12:19 pm
Location: Berlin, DE
Contact:

Post by sdi2000 »

okay try this instead of your samplers

Code: Select all

texture tex0 : register(s0);
texture tex1 : register(s1);
texture tex2 : register(s2);
// Ground texture
sampler Texture0 = sampler_state 
{
   texture = tex0;
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

// Grass texture
sampler Texture1 = sampler_state 
{
  texture = tex1;
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

// Heightmap texture
sampler Texture2 = sampler_state
{
  texture = tex2;
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};
mits
Posts: 8
Joined: Tue Jan 16, 2007 9:53 pm

Post by mits »

Thanks for the answer but it didn't work again. I can't understand what's wrong since the shader works fine in RenderMonkey. I made a simple texture blending before with two textures which worked perfectly in Irrlicht but now that I have 3 textures nothing happens. Perhaps it has something to do with the 3 textures. Any help would be greatly appreciated since I certainly do not know where to begin. Thanks.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

those constants have to have static before them if you are going to set their values in the shader file. Otherwise, they default to 'extern' and must be set by a call to setVertexShaderConstant or setPixelShaderConstant.

Oh, and RenderMonkey I've found does many things behind the scenes and when you try to make their examples work outside of RenderMonkey, you have to figure them out, like I had to with the Electricity and the Fire samples. Point being, don't rely on, "It worked in RenderMonkey, it should work in my app", as apparently here, they are not using default HLSL syntax, maybe it's seeing that you set the value so it's adding the static keyword for you when it compiles the shaders, who knows.
Image
mits
Posts: 8
Joined: Tue Jan 16, 2007 9:53 pm

Post by mits »

Thanks a lot for the answer. It worked by using the static keyword and this syntax for the samplers:

Code: Select all


// Ground texture
sampler Texture0 : register(s0) = sampler_state
{
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

// Grass texture
sampler Texture1 : register(s1) = sampler_state
{
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

// Heightmap
sampler Texture2 : register(s2) = sampler_state
{
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

I'll keep in mind your advice about RenderMonkey. Thanks.
Post Reply