   Trouble rendering scene to a texture
  Hi guys I have been trying to learn how to render scene to a texture so I can try to do some special effects like this one http://www.gamedev.net/community/forums/topic.asp?topic_id=395311I started with a simple example where I want to render a teapot onto a texture and use the texture on a simple cube. However it seems that my texture only shows up for one frame. The texture shows if I only render the teapot to it once but I can not update the texture by rendering to it again in every rendering loop.I have been searching around but could find any solution...Thank you in advance for any suggestion/pointers.Here is some of my code. variable definations are omitted.//Init some D3D object...called when application startsvoid init( void ){    g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );    D3DDISPLAYMODE d3ddm;    //some routine d3d setup omitted    g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice );    g_pd3dDevice->CreateVertexBuffer( 24*sizeof(Vertex),0, Vertex::FVF_Flags,   D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL );    void *pVertices = NULL;    g_pVertexBuffer->Lock( 0, sizeof(g_cubeVertices), (void**)&pVertices, 0 );    //g_cubeVertices is initialized    memcpy( pVertices, g_cubeVertices, sizeof(g_cubeVertices) );    g_pVertexBuffer->Unlock();    g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);    D3DXMATRIX matProj;    D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ),   640.0f / 480.0f, 0.1f, 100.0f );    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );    //create a tea pot, i want to render the teapot to the texture    D3DXCreateTeapot(g_pd3dDevice, &g_teapotMesh, 0);    //get a pointer to current back buffer so i can restore it later    g_pd3dDevice->GetRenderTarget(0,&pBackBuffer);    //create a LPDIRECT3DTEXTURE9 and LPDIRECT3DSURFACE9, teapot mesh will    //be rendered onto this texture    g_pd3dDevice->CreateTexture(256,256,1,D3DUSAGE_RENDERTARGET,D3DFMT_R5G6B5,D3DPOOL_DEFAULT,&pRenderTexture,NULL);    pRenderTexture->GetSurfaceLevel(0,&pRenderSurface);}//actual renderingvoid render( void ){    D3DXMATRIX matWorld;    D3DXMATRIX matTrans;    D3DXMATRIX matRot;    D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, g_fDistance );    //rotate the cube on mouse move    D3DXMatrixRotationYawPitchRoll( &matRot,    D3DXToRadian(g_fSpinX),    D3DXToRadian(g_fSpinY),    0.0f );    matWorld = matRot * matTrans;    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );    ////render texture first    g_pd3dDevice->SetRenderTarget(0,pRenderSurface);    g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(100,100,100),1.0f,0);    g_pd3dDevice->BeginScene();    g_teapotMesh->DrawSubset(0);    g_pd3dDevice->EndScene();    //restore backbuffer, render cube with texture    g_pd3dDevice->SetRenderTarget(0,pBackBuffer);    //render scene    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_COLORVALUE(0.35f, 0.53f, 0.7, 1.0f), 1.0f, 0 );  g_pd3dDevice->BeginScene();   g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);    g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);    g_pd3dDevice->SetTexture( 0, pRenderTexture );    g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(Vertex) );    g_pd3dDevice->SetFVF( Vertex::FVF_Flags );    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );    g_pd3dDevice->EndScene();    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );} TIA![Edited by - plasm1d on June 2, 2006 11:14:10 AM]
  Can some one please look at this and point me to the right direction? If the code I posted isnt clear enough I have the full version in a very small app I can email or attach.Thank you guys.   ;  After first render(), your texture is set as active for stage 0. That makes you unable to set it as render target at the same time. Of course, you're not writing to the texture while reading at the same time in this code, but the device driver is not so eager to check for it, so it just cancels the drawing as being invalid.You got to manually un-set it, by swapping active texture for ny another, or simply calling:g_pd3dDevice->SetTexture(0, NULL)before drawing to texture.