Rendering to a texture?

Started by
10 comments, last by jackharmon 15 years, 10 months ago
Currently, I'm rendering 128x128 quads each frame onto a mesh and it's very slow. Is there a way I can render the completed 128x128 quads to a single texture so I can splat that across the entire mesh each frame instead of building the quads each frame? ( Ultimately, I plan to blend each quad with its neighbors which will make things even slower. ) I'm using the DX9 ( Nov 2007 ) SDK in Visual C++ 2005. Thanks! ---------------------------------- Here's part of the code that I'm running each frame:

m_pSysMemMesh->GetVertexBuffer( &pVB );
m_pSysMemMesh->GetIndexBuffer( &pIB );
device3D->SetIndices( pIB );
device3D->SetStreamSource( 0, pVB, 0, sizeof( D3DVERTEX ) );
device3D->SetFVF( D3DVERTEX::FVF );
device3D->SetMaterial( &m_pMaterials[0] );

for( int y = 0; y < 128; y++ )
{
   for( int x = 0; x < 128; x++ )
   {
      device3D->SetTexture( 0, indexMap[y][x].texture );
      device3D->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, ( y * 129 ) + x, 0, 0, 0, 2 );
   }
}

pIB->Release();
pVB->Release();

[Edited by - jackharmon on June 19, 2008 1:23:11 AM]
Advertisement
short answer.

Setup a texture as a render target and draw your quads to that, then use that texture to draw onto your mesh.

check the docs, or search here, I know theres tons of threads already on the subject cause I just implemented rendering to a texture a week ago.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
kk - ty!

Will search on texture render target.
Ok, I'm getting close, however, I'm stumped on how to get 128 x 128 textures tiled onto the single texture.

I have a terrain model that I'm rendering 128 x 128 textures onto the quads. I'd like to do this once by writing to a texture that I will splat over the entire mesh.

So far, I'm able to render the 4096 x 4096 splat onto the mesh, however, the splat is just the color of the clear call. I'm attempting to render the quads to the texture using the following.

device3D->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, verts, sizeof(CUSTOMVERTEX) );

verts is an array of 4 CUSTOMVERTEX with just XYZ position and texture coords. The XYZ coords are iterated by 32 ( 4096 / 128 ) and I think this is where I'm having issues. How should each of my 128 x 128 quads be positioned when I call DrawPrimitiveUp?

Thanks!
You can copy the small textures into the render target using StretchRect.
The textures are sometimes sent to a shader to blend, then are drawn to the surface, so I'm not sure how I could get a bit copy ( stretch ) to to work.

Thanks!

device3D->SetPixelShader( splatShader );device3D->SetTexture( 0, alphaMap );device3D->SetTexture( 1, indexMap[y-1][x].texture );device3D->SetTexture( 2, indexMap[y][x+1].texture );device3D->SetTexture( 3, indexMap[y+1][x].texture );device3D->SetTexture( 4, indexMap[y][x-1].texture );device3D->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, ( y * 129 ) + x, 0, 0, 0, 2 );
I think I'm just missing setting up the view / world transformation. Gonna search around for that info.
If you're drawing quads and you need them to be perfectly aligned on the render-target, you can use an off-center orthographic projection. Create it using D3DXMatrixOrthoOffCenterLH with l and b set to 0, and r and t set to the dimensions of your render target. Have your quad vertices set at <0, 0, 0>, <0, 128, 0>, <128, 0, 0>, etc. Then when you render each quad, set create a world transform using D3DXMatrixTranslation with x and y set to the position on the texture where you want the quad rendered, and z set to some value such that it will be in between the znear/zfar values you set for your projection matrix. Your view matrix can be identity.
Yep - needed to setup a projection matrix and the following.

device3D->SetRenderState( D3DRS_ZENABLE, false ); device3D->SetRenderState( D3DRS_ZWRITEENABLE, false ); device3D->SetRenderState( D3DRS_LIGHTING, FALSE );
Ok, I have this working well now, however I've run into a problem. I want to do this in the background while my engine continues to render the scene. Since rendering to the texture takes over the device when I call:

device3D->SetRenderTarget( 0, splatSurface );

how can I build the texture while still rendering my primary scene? I tried creating a new device but was unsuccessful.

Thanks!

This topic is closed to new replies.

Advertisement