Jump to content
  • Advertisement
Sign in to follow this  
Promethium

Problem with rendering to texture

This topic is 3905 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

I'm trying to use a texture as a render target in DX9, but the result is just a completly black texture. I've made this helper class:
[.h]
class RenderTarget {
public:
    RenderTarget( LPDIRECT3DDEVICE9 device, UINT width, UINT height );
    ~RenderTarget();

    void Enable();
    void Disable();

    const LPDIRECT3DTEXTURE9& Texture() { return mRenderTexture; }

    void Save( const std::string& filename ) const;

private:
    LPDIRECT3DDEVICE9 mDevice;
    LPDIRECT3DTEXTURE9 mRenderTexture;
    LPDIRECT3DSURFACE9 mRenderSurface;
    LPDIRECT3DSURFACE9 mBackBuffer;

    UINT mWidth;
    UINT mHeight;
};

[.cpp]
RenderTarget::RenderTarget( LPDIRECT3DDEVICE9 device, UINT width, UINT height ) 
    : mDevice( device )
    , mRenderTexture( 0 )
    , mRenderSurface( 0 )
    , mBackBuffer( 0 )
    , mWidth( width )
    , mHeight( height )
{
    assert( mWidth );
    assert( mHeight );

    HRESULT hr = mDevice->CreateTexture( mWidth, mHeight, 1, D3DUSAGE_RENDERTARGET, 
        D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &mRenderTexture, 0 );
    if( FAILED( hr ) )
        throw Exception( "Could not create texture" );

    hr = mRenderTexture->GetSurfaceLevel( 0, &mRenderSurface );
    if( FAILED( hr ) )
        throw Exception( "Could not get texture surface" );
}

RenderTarget::~RenderTarget() {
    if( mBackBuffer )
        Disable();

    if( mRenderSurface )
        mRenderSurface->Release();

    if( mRenderTexture )
        mRenderTexture->Release();
}   

void RenderTarget::Enable() {
    HRESULT hr = mDevice->GetRenderTarget( 0, &mBackBuffer );
    if( FAILED( hr ) )
        throw Exception( "Could not get render target" );

    hr = mDevice->SetRenderTarget( 0, mRenderSurface );
    if( FAILED( hr ) )
        throw Exception( "Could not set render target" );
}

void RenderTarget::Disable() {
    assert( mBackBuffer );

    HRESULT hr = mDevice->SetRenderTarget( 0, mBackBuffer );
    if( FAILED( hr ) )
        throw Exception( "Could not set render target" );

    mBackBuffer->Release();
    mBackBuffer = 0;
}

void RenderTarget::Save( const std::string& filename ) const {
    HRESULT hr = D3DXSaveSurfaceToFile( filename.c_str(), D3DXIFF_PNG, mRenderSurface, 0, 0 );
    if( FAILED( hr ) )
        throw Exception( "Could not save texture to " + filename );
}


I then use it like this:
gRenderTarget->Enable();
gDevice->Clear( 0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, from_argb( 0, 0, 255 ), 1, 0 );
gDevice->BeginScene();

// Do some simple rendering
RenderCube();

gDevice->EndScene();
gRenderTarget->Disable();


This should cause the texture I use as render target to become blue with a cube in the center. However if I try to use it, or save it to a file it's just black. Can anyone help me figuring out what I am missing?

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!