Hi! I,m trying to implement post process in my project. I´ve implemented a RenderTarget Class as follow:
class RenderTarget {
public:
LPDIRECT3DTEXTURE9 texture; //La textura donde realmente voy a pintar.
LPDIRECT3DSURFACE9 surface; //La surface de la textura anterior donde se va a pintar realmente.
LPD3DXRENDERTOSURFACE render_to_surface; //Para gestionar el cambio.
bool uses_active_zbuffer;
std::string shader_tech; //Por defecto, "PostProcessTech".
RenderTarget() : texture(NULL), surface(NULL), render_to_surface(NULL), uses_active_zbuffer(false), shader_tech("PostProcessTech") {}
void init(int, int, bool);
void beginRender();
void endRender();
void renderPostProcess();
void save(std::string);
void destroy();
};
The implementation of the init, beginRender, endRender and save methods is the following:
void RenderTarget::init(int xres, int yres, bool a_uses_active_zbuffer) {
uses_active_zbuffer = a_uses_active_zbuffer;
HRESULT hr = D3DXCreateTexture( g_pd3dDevice, xres, yres, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture);
assert(hr == D3D_OK);
hr = texture->GetSurfaceLevel(0, &surface);
assert(hr == D3D_OK);
hr = D3DXCreateRenderToSurface( g_pd3dDevice, xres, yres, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render_to_surface);
assert(hr == D3D_OK);
}
void RenderTarget::beginRender() {
LPDIRECT3DSURFACE9 depth_surface;
HRESULT hr;
if(uses_active_zbuffer) {
hr = g_pd3dDevice->GetDepthStencilSurface(&depth_surface); assert(hr == D3D_OK);
}
assert(render_to_surface != NULL);
hr = render_to_surface->BeginScene(surface, NULL);
assert(hr == D3D_OK);
if(uses_active_zbuffer) {
g_pd3dDevice->SetDepthStencilSurface(depth_surface); assert(hr == D3D_OK);
depth_surface->Release();
}
}
void RenderTarget::endRender() {
assert(render_to_surface != NULL);
HRESULT hr = render_to_surface->EndScene(0);
assert(hr == D3D_OK);
}
void RenderTarget::save(std::string file_name) {
HRESULT hr = D3DXSaveTextureToFile(file_name.c_str(), D3DXIFF_JPG, texture, NULL);
assert(hr == D3D_OK);
}
The general render method is:
void render(float delta_time) {
renderTarget.beginRender();
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x80808080, 1.0f, 0);
renderWorld(); //My scene render.
renderTarget.endRender();
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
renderTarget.renderPostProcess();
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
I have a method to save the RenderTarget texture, so I can see the texture. The texture is not the actual frame of the scene, IT IS ALWAYS BLACK. All DirectX method returns a S_OK, the texture has the same size than the main window of the application. And, of course, the renderWorld method works fine. Any idea?
Thanks and sorry for my poor English.
EDIT: Fixed up your 'source' tags and gave the thread a meaningful title
[Edited by - Romanturbo on September 17, 2008 9:43:38 AM]