Jump to content

  • Log In with Google      Sign In   
  • Create Account

Heightmap from VertexShader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 montify   Members   -  Reputation: 388

Like
0Likes
Like

Posted 20 November 2012 - 03:28 PM

Hello

Anyone can tell me how i get the Heightmap from VertexShader?
Note: I normalize the Cube to get a Sphere (Planet)

float4x4 World;
float4x4 View;
float4x4 Projection;
float4 color;
float seaLevel;
texture myTexture;
float maxHeight = 128;
float height;

sampler2D mySampler = sampler_state
{
Texture = <myTexture>;
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};

struct VertexShaderInput
{
float4 Position : POSITION0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float height = tex2Dlod(mySampler, float4(12,32,0 ,0));
worldPosition.z += height;
worldPosition = float4(normalize(worldPosition.xyz) * seaLevel, 1);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
return output;
}


float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return color;
}
technique Technique1
{
pass Pass1
{

VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}

This is all i have, but the the result is nothing :/

Please help me...

best regards, alex

Sponsor:

#2 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 20 November 2012 - 03:48 PM

I haven't used tex2Dlod(), but from the MSDN page, it looks like x and y in your texcoord should be a standard UV texcoord, so most likely 0..1 normalized. And since your addressing is set to CLAMP, it will just grab the edges which may be flat? Try playing with those a bit.

Also I'd use PIX and see if the texture is actually bound and, more importantly, has valid data in it.

Good luck!
Perception is when one imagination clashes with another

#3 montify   Members   -  Reputation: 388

Like
0Likes
Like

Posted 21 November 2012 - 04:10 AM

Hello
here is my new Shader:

float4x4 World; 
float4x4 View; 
float4x4 Projection; 
float axis;

float4 color; 
float seaLevel; 
float height; 
texture myTexture; 

sampler2D mySampler = sampler_state 
{ 
Texture = <myTexture>; 
MinFilter = Point; 
MagFilter = Point; 
MipFilter = Point; 
AddressU = Clamp; 
AddressV = Clamp; 
}; 

struct VertexShaderInput 
{ 
float4 Position : POSITION0; 
float4 uv : TEXCOORD0;
}; 

struct VertexShaderOutput 
{ 
float4 Position : POSITION0; 
float4 uv : TEXCOORD0;
}; 

//#############  VERTEX SHADER  #############
VertexShaderOutput VertexShaderFunction(VertexShaderInput input) 
{ 
	VertexShaderOutput output; 
	float4 worldPosition = mul(input.Position, World); 
	float axis;
	float2 planetUV; 
	

	//---------------AXIS--------------
   if(axis == 0) //left 
   { 
      planetUV = (worldPosition.zy + 1) * 0.5; 
      planetUV.y = 1 - planetUV.y; 
   } 
   else if(axis == 1) //right 
   {
	  planetUV = (worldPosition.zy + 1) * 0.5; 
      planetUV = 1 - ((worldPosition.zy + 1) * 0.5); 
   }
   else if(axis == 2) //top 
   {
	   planetUV = (worldPosition.zy + 1) * 0.5; 
      planetUV = (worldPosition.xz + 1) * 0.5; 
   }
   else if(axis == 3) //bottom 
   { 
      planetUV = (worldPosition.xz + 1) * 0.5; 
      planetUV.y = 1 - planetUV.y; 
   } 
   else if(axis == 4) //front 
   { 
      planetUV = (worldPosition.xy + 1) * 0.5; 
      planetUV.y = 1 - planetUV.y; 
   } 
   else if(axis == 5) //back 
	  planetUV = (worldPosition.zy + 1) * 0.5; 
      planetUV = 1 - (worldPosition.xy + 1) * 0.5; 
   //---------------AXIS END--------------


	float3 heightCol = tex2Dlod( mySampler, float4(input.uv.xy, 0, 0) );

	worldPosition= mul(float4(input.Position.x, 0, input.Position.z, 1) *seaLevel, World); 
	worldPosition.y = heightCol * planetUV.y;
	worldPosition = float4(normalize(worldPosition.xyz) * seaLevel, 1); 
	
	float4 viewPosition = mul(worldPosition, View); 

	output.Position = mul(viewPosition, Projection); 
	output.uv = input.uv;

	return output; 
} 



//#############  PIXEL SHADER  #############
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 
{ 
	return color; 
} 

technique Technique1 
{ 
pass Pass1 
{ 


VertexShader = compile vs_3_0 VertexShaderFunction(); 
PixelShader = compile ps_3_0 PixelShaderFunction(); 
} 
}

But here is my new Error:
The current vertex declaration does not include all the elements required by the current vertex shader. TextureCoordinate0 is missing

Anyone can tell me what i'm doing wrong?

Edited by montify, 21 November 2012 - 04:38 AM.


#4 Nik02   Crossbones+   -  Reputation: 2829

Like
0Likes
Like

Posted 21 November 2012 - 07:36 AM

The symbol "TextureCoordinate0" is not in the code you posted. Are you sure that this shader actually gets compiled and set as the active shader? Furthermore, are you sure that you want to use float4 for the uv coordinate input and output? The logic in the code suggests that you only make use of the xy part (which fits in a float2), and even without trying to compile the code I can guess that this would cause compilation errors as well.

The actual error is just as it says on the tin: the vertex declaration (which you establish in the host program, and which describes your vertex buffer format to the hardware) has to contain all the elements that the vertex shader expects to find in the input streams.

The error can be caused by the fact that an another shader is bound instead of the one you expect to be bound; this, in turn, can indirectly be caused by a failure to compile and set the expected shader.

Niko Suni


#5 phil_t   Crossbones+   -  Reputation: 3914

Like
0Likes
Like

Posted 22 November 2012 - 03:20 AM

Sounds like this is XNA, which has pretty good error checking. TextureCoordinate0 is just referring to the TEXCOORD0 semantic.

So the problem is that the vertices you're drawing with your effect do not contain TEXCOORD0. What is the vertex format you're using? What does your draw call look like?

#6 xexuxjy   Members   -  Reputation: 607

Like
0Likes
Like

Posted 22 November 2012 - 08:04 AM

Sounds like your vertex declaration is probably only using something like a Vector2 , which if you've got a regular grid should really be all you need along with a texelwidth to calculate the uv for the heightmap?
I've got a sample terrain object doing a shader heightmap at :
http://code.google.com/p/xexuxjy-xna-games/source/browse/trunk/MagicCarpet/MagicCarpet/com/xexuxjy/magiccarpet/terrain/Terrain.cs
http://code.google.com/p/xexuxjy-xna-games/source/browse/trunk/MagicCarpet/MagicCarpetContent/Effects/Terrain/ClipTerrain.fx




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS