Hi all, I'm an OpenGL programmer (and have been all my life), but this current
project means I have to use Direct3D
8.1 (ie. Not 9+)
I am currently trying to
copy the current back-buffer surface
to a texture, but its not at all working. I dont want to re-render my entire
scene to a texture, just copy whats on the back-buffer to a texture then
present it on a screen-aligned quad.
I first got a wierd texture whcih seemed to be sceene related (colors changed
etc) but now i currently (with a bit of fiddling) get nothing, and its laggy,
somthing aweful! Currently quad doesnt even show up on screen, even with the
null texture, any help would be appreciated!
here is my code thus far:
header:
class G13_PostProcessor
{
// for the screen aligned quad
LPDIRECT3DVERTEXBUFFER8 m_VB;
LPDIRECT3DINDEXBUFFER8 m_IB;
// render target textures
LPDIRECT3DTEXTURE8 m_ScreenTexture;
LPDIRECT3DSURFACE8 m_ScreenSurface;
LPDIRECT3DTEXTURE8 m_ScreenTextureTemp1;
LPDIRECT3DSURFACE8 m_ScreenSurfaceTemp1;
LPDIRECT3DTEXTURE8 m_ScreenTextureTemp2;
LPDIRECT3DSURFACE8 m_ScreenSurfaceTemp2;
DWORD m_Height;
DWORD m_Width;
DWORD m_Format;
// shaders that we will be using
DWORD m_ShaderVSDefault;
DWORD m_ShaderPSBlurX;
DWORD m_ShaderPSBlurY;
DWORD m_ShaderPSCombine;
bool isInitialized;
public:
G13_PostProcessor();
virtual ~G13_PostProcessor();
virtual void Init( LPDIRECT3DDEVICE8 d3dDevice, DWORD height, DWORD width );
virtual void UpdateRTT( LPDIRECT3DDEVICE8 d3dDevice, LPDIRECT3DSURFACE8 srcSurf );
virtual void Render( LPDIRECT3DDEVICE8 d3dDevice );
};
implementation:
#include "G13_PostProcess.h"
#include <d3dx8.h>
//------------------------------------------------------------------------------------------------
//constants
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
//structures
struct VERTEX_XYZUV
{
float x, y, z;
float u, v;
};
VERTEX_XYZUV quadVerts[] =
{
{ 0.0f, 768.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ 1024.0f, 768.0f, 0.0f, 1.0f, 0.0f },
{ 1024.0f, 0.0f, 0.0f, 1.0f, 1.0f }
};
//------------------------------------------------------------------------------------------------
// base constructor and destructor
//------------------------------------------------------------------------------------------------
G13_PostProcessor::G13_PostProcessor()
: isInitialized(false)
{
}
G13_PostProcessor::~G13_PostProcessor()
{
}
//------------------------------------------------------------------------------------------------
// initialize the post processor
//------------------------------------------------------------------------------------------------
void G13_PostProcessor::Init( LPDIRECT3DDEVICE8 d3dDevice, DWORD height, DWORD width )
{
LPDIRECT3DSURFACE8 backBuffer;
D3DSURFACE_DESC backBufferDesc;
d3dDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer );
backBuffer->GetDesc( &backBufferDesc );
// currently the game runs at 1024x768
// and here the height and width == 1024
HRESULT a = d3dDevice->CreateTexture( height, width, 0, D3DUSAGE_RENDERTARGET,
backBufferDesc.Format, D3DPOOL_DEFAULT, &m_ScreenTexture );
m_ScreenTexture->GetSurfaceLevel( 0, &m_ScreenSurface );
HRESULT b = d3dDevice->CreateVertexBuffer( sizeof(quadVerts), D3DUSAGE_WRITEONLY,
D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_VB );
if( FAILED(a) || FAILED(b) )
{
MessageBox( NULL, "Failed to create Render Targets", "Error", MB_ICONERROR|MB_OK );
return;
}
BYTE* data;
m_VB->Lock( 0, sizeof(data), (BYTE**)&data, 0 );
memcpy( data, quadVerts, sizeof(quadVerts) );
m_VB->Unlock();
isInitialized = true;
}
//------------------------------------------------------------------------------------------------
// update the render-target surfaces
//------------------------------------------------------------------------------------------------
void G13_PostProcessor::UpdateRTT( LPDIRECT3DDEVICE8 d3dDevice, LPDIRECT3DSURFACE8 srcSurf )
{
if( !isInitialized )
return;
/* tried this, got a kinda result texture looked like fractals no coherent image.
if( FAILED( d3dDevice->CopyRects( srcSurf, NULL, 0, m_ScreenSurface, NULL ) ) )
{
MessageBox( NULL, "Failed to create Render Targets", "Error", MB_ICONERROR|MB_OK );
PostQuitMessage(0);
return;
}
*/
D3DXLoadSurfaceFromSurface( m_ScreenSurface, NULL, NULL, srcSurf, NULL, NULL,
D3DX_FILTER_NONE, 0 );
}
//------------------------------------------------------------------------------------------------
// do the final processing and present the final post-processed image
//------------------------------------------------------------------------------------------------
void G13_PostProcessor::Render( LPDIRECT3DDEVICE8 d3dDevice )
{
static D3DXMATRIX matTran, matProj;
if( !isInitialized )
return;
D3DXMatrixIdentity( &matTran );
D3DXMatrixIdentity( &matProj );
D3DXMatrixTranslation( &matTran, 320, 240, 0 );
D3DXMatrixOrthoRH( &matProj, 640, 480, -1, 1 );
d3dDevice->BeginScene();
d3dDevice->SetPixelShader( NULL );
d3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
d3dDevice->SetTransform(D3DTS_WORLD, &matTran);
d3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
d3dDevice->SetTexture( 0, m_ScreenTexture );
d3dDevice->SetStreamSource( 0, m_VB, sizeof(VERTEX_XYZUV) );
d3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
d3dDevice->EndScene();
}
if anyone can see anything that could be causing any issues, pleeeaaase let me know.