Jump to content
  • Advertisement
Sign in to follow this  
jackharmon

Rendering to a texture?

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

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
You can copy the small textures into the render target using StretchRect.

Share this post


Link to post
Share on other sites
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 );


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!