Sign in to follow this  
sipickles

Building a world space normal map using texture shader

Recommended Posts

Hello, As the subject suggests, I am trying to use a texture shader to generate a world space normal map from a heightmap at initialisation. My question is, can I use the textureshader to sample another texture (ie the heightmap) This is the sort of thing I want in the shader:
// normalGen.fx  - uses a texture Shader to procedurally generate a normal map from the supplied heightmap

// Texture
texture heightMap;

sampler heightSampler = sampler_state
{
    Texture   = (heightMap);
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};


float4 TX( float2 Tex : POSITION ) : COLOR
{
        float stepsize = 0.001;

	// Get height from heightmap
	float4 Height1 = tex2D( heightSampler, Tex );
	float4 Height2 = tex2D( heightSampler, float2(Tex.x - stepsize, Tex.y );
	float4 Height2 = tex2D( heightSampler, float2(Tex.x, Tex.y + stepsize );
// etc ....to work out average normal at point

        // pack vector info into 0-1 range
	Normal *= 0.5;
	Normal += 0.5;
	return float4( Normal, 1.0 ); // return as a colour

}



This is how I am setting up:
HRESULT cTerrainManager::CreateNormalMap( void )
{
	HRESULT hr;

    LPD3DXBUFFER pShader = NULL;

	// Create the procedural texture
    hr = D3DXCompileShaderFromFile(
        L"data\\shaders\\normalGen.fx",
        NULL, // A NULL terminated array of D3DXMACROs
        NULL, // A #include handler
        "TX",  
        "tx_1_0", 
        D3DXSHADER_DEBUG,
        &pShader, 
        NULL,    // error messages 
        NULL );  // constant table pointer

    if( FAILED( hr ) )
    {
          SAFE_RELEASE( pShader );
        return hr;
    }

    // Procedurally fill texture
    hr = D3DXFillTextureTX( m_normalMap, (LPD3DXTEXTURESHADER)pShader->GetBufferPointer()  );
    if( FAILED( hr ) )
    {
        SAFE_RELEASE( m_normalMap );
        SAFE_RELEASE( pShader );
        return hr;
    }

	return S_OK;
}

Is this possible? how do I set the heightmap texture? Through a constant table perhaps? If this isnt possible, can I just do it with D3DXFillTexture and a software function? Its not a runtime thing so speed isnt so important. Thanks for your advice Simon

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this