[SOLVED] Vertex Normal is the same in GeoMipTerrain

I use GeoMipTerrain with my terrain shader (from panda3d forum).

Shader file like this:

//Cg
//Cg profile arbvp1 arbfp1

// shader loosely based on pro-rsoft's from the panda3d forums
// shader assumes 6 texture stages:
// 0-2 textures
// 3-5 alpha maps
// vertex shader accepts unit 0 and 3 texture coords as input 
// and outputs unit 0 scaled and unit 3 unchanged
// fragment shader uses unit 0 coordinates to access textures
// and unit 3 coordinates (un-scaled) to access alpha maps
// gsk, june-2008

void vshader( in float4 vtx_position : POSITION,
	      in float3 vtx_normal : NORMAL,
              in float2 vtx_texcoord0 : TEXCOORD0,
              in float2 vtx_texcoord3 : TEXCOORD3,
              in uniform float4x4 mat_modelproj,
	      in uniform float4 k_lightvec,
	      in uniform float4 k_lightcolor,
	      in uniform float4 k_ambientlight,
	      in uniform float4 k_tscale, 
	      out float l_brightness,
              out float2 l_texcoord0 : TEXCOORD0,
              out float2 l_texcoord3 : TEXCOORD3,
              out float4 l_position : POSITION)
{

  l_position=mul(mat_modelproj,vtx_position);
  l_texcoord0=vtx_texcoord0*k_tscale;
  l_texcoord3=vtx_texcoord3;

  // lighting
  float3 N = normalize( vtx_normal );
  float3 L = normalize( k_lightvec.xyz );
  l_brightness = (max( dot( -N, L ), 0.0f )*k_lightcolor)+k_ambientlight;

}

void fshader( in float4 l_position : POSITION,
              in float2 l_texcoord0 : TEXCOORD0,
              in float2 l_texcoord3 : TEXCOORD3,
	      in float  l_brightness,
              in uniform sampler2D tex_0 : TEXUNIT0,
              in uniform sampler2D tex_1 : TEXUNIT1,
              in uniform sampler2D tex_2 : TEXUNIT2,
              in uniform sampler2D tex_3 : TEXUNIT3,
              in uniform sampler2D tex_4 : TEXUNIT4,
              in uniform sampler2D tex_5 : TEXUNIT5,
              out float4 o_color : COLOR )
{
  float4 tex1=tex2D(tex_0,l_texcoord0);
  float4 tex2=tex2D(tex_1,l_texcoord0);
  float4 tex3=tex2D(tex_2,l_texcoord0);
  float alpha1=tex2D(tex_3,l_texcoord3).z;
  float alpha2=tex2D(tex_4,l_texcoord3).z;
  float alpha3=tex2D(tex_5,l_texcoord3).z;
  o_color =tex1*alpha1;
  o_color+=tex2*alpha2;
  o_color+=tex3*alpha3;
  o_color=o_color*(l_brightness);
  o_color.a=1.0;
}

And it is result:

I think vtx_normal is the same for all vertex. How to fix it? [/img]

You have to transform the normals into the appropriate coordinate space first, since the terrain will have a scale applied to it. If you only have a z scale applied, you can divide the z component by your z scale, and then normalize the vector.

I have read this tutorial about Coordinate Spaces: panda3d.org/manual/index.php … ate_Spaces

I have tried all of them by this way:

. Add “in uniform float4x4 trans_x_to_y” to vertex shader parameter

with

trans_x_to_y is one value in the link above (ex: trans_model_to_world)

. Change

 float3 N = normalize( normal );

by

  float3 normal= mul(trans_x_to_y, float4(vtx_normal, 1)).xyz;  
  normal = normal.xyz / normal.z;
  float3 N = normalize( normal );

but the result is not “good”.

“Good” is something like that (terrain have dark side looks like shadow):

What’s wrong?

[SOLVED]

Oh, thanks to rdb, I found that:

  • vtx_normal is not the same for all vertex
  • vtx_normal is too small to see the different
  • divide vtx_normal.z to zscale which I apply to my terrain, then normalize it .
    (ex:
vtx_normal.z /= zscale;
  float3 N = normalize( vtx_normal);  

zscale is in parameter in vshader