Jump to content
  • Advertisement
Sign in to follow this  
sipickles

Building a world space normal map using texture shader

This topic is 4494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!