convert a IDirect3DSurface9 to an IDirect3DTexture9

Started by
5 comments, last by Cambo_frog 18 years, 9 months ago
Quite simply I want to render the 6 surfaces of a cubemap to 6 different textures which will then be textured onto 6 different quads (so I can visualize the cubemap for debugging purposes). I was wondering if there is a way to do this?
Advertisement
Hi Khaos,
Could you clarify a little?
Do you have an existing cubemap texture in a file(.dds) and you want to view its surfaces separately?

If so the easiest way would be to load the texture in the "DirectX Texture Tool" from the SDK and view the separate surfaces by selecting (from the main menu):
View->Cube Map Face.
Then select the face you want to view.

HTH,
Cambo_frog
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
I'm not sure at all about this, or if this is the best way...
But here goes my 5 cents:

UpdateSurface (or StretchRect)... you can copy from a surface to another surface using that, and locking your texture and getting the surface would then allow you to copy your surface to the texture I guess.


This is a cubemap I have rendered to via my main app so its not prestored in a file.
For debugging purposes, you should be able to simply use D3DXLoadSurfaceFromSurface() to copy each surface to the surface of another texture. Quick 'n' dirty example:
void CopyCubeSurfacesToTextures(IDirect3DCubeTexture9* pCubeTexture, IDirect3DTexture9** ppTextures){  if (pCubeTexture != NULL && ppTexture != NULL)  {    IDirect3DSurface9* pCubeSurface = NULL;    IDirect3DSurface9* pTextureSurface = NULL;    for (int i = 0; i < 6; ++i)    {      if (ppTexture != NULL)      {        switch (i)        {          case 0: pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_X, 0, &pCubeSurface); break;          case 1: pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_X, 0, &pCubeSurface); break;          case 2: pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Y, 0, &pCubeSurface); break;          case 3: pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Y, 0, &pCubeSurface); break;          case 4: pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Z, 0, &pCubeSurface); break;          case 5: pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Z, 0, &pCubeSurface); break;          default: pCubeSurface = NULL; break;        }        if (pCubeSurface != NULL)        {          ppTextures->GetSurfaceLevel(0, &pTextureSurface);          if (pTextureSurface != NULL)          {            D3DXLoadSurfaceFromSurface(pTextureSurface, NULL, NULL, pCubeSurface, NULL, NULL, D3DX_FILTER_NONE, 0x00000000);            pTextureSurface->Release();          }          pCubeSurface->Release();        }      }    }  }}
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Thank you Agony, that code shows perfectly how to interact with a textures surface. It also occurred to me after searching through the msdn that I can use D3DXSaveTextureToFile to save the cubemap to a dds file which can then be viewed with the sdk tools, but I think I will do it in real time to avoid the hassle of reloading the cubemap multiple times.
Hi Khaos,
Another alternative would be to use cubic texture coordinates directly in your quad vertices and cut out the extra level of creating extra textures\retrieving surfaces.

Define your vertex structure something like:

struct CUBEMAP_VERTEX {
FLOAT x,y,z; // position
FLOAT u,v,w; // cubic texture coordinates
};

and the FVF for this vertex would be:
#define FVF_CUBEMAP D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0)

Think of your cube having 8 corners:

The definition of the cubic texture coordinates (u, v, w) of the corners are below. Read +x +y +x as positive x, positive y positive z etc.
Read left as negative x, right as positive x, top as positive y , bottom as negative y, back as positive z and front as negative z:

// +x +y +z
// right top back corner
1.0f, 1.0f, 1.0f

// +x +y -z
// right top front corner
1.0f, 1.0f,-1.0f

// +x -y +z
// right bottom back corner
1.0f,-1.0f, 1.0f

// +x -y -z
// right bottom front corner
1.0f,-1.0f,-1.0f

// -x +y +z
// left top back corner
-1.0f, 1.0f, 1.0f

// -x +y -z
// left top front corner
-1.0f, 1.0f,-1.0f

// -x -y +z
// left bottom back corner
-1.0f,-1.0f, 1.0f

// -x -y -z
// left bottom back corner
-1.0f,-1.0f,-1.0f

Also FYI the vertices for a skybox using the cube map would be:

static CUBEMAP_VERTEX SkyboxVerts[] =
{
// +x +y +z
// right top back corner
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f},
// +x +y -z
// right top front corner
{ 1.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f},
// +x -y +z
// right bottom back corner
{ 1.0f, -1.0f, 1.0f, 1.0f,-1.0f, 1.0f},
// +x -y -z
// right bottom front corner
{ 1.0f, -1.0f,-1.0f, 1.0f,-1.0f,-1.0f},
// -x +y +z
// left top back corner
{-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f},
// -x +y -z
// left top front corner
{-1.0f, 1.0f,-1.0f,-1.0f, 1.0f,-1.0f},
// -x -y +z
// left bottom back corner
{-1.0f, -1.0f, 1.0f,-1.0f,-1.0f, 1.0f},
// -x -y -z
// left bottom front corner
{-1.0f, -1.0f,-1.0f,-1.0f,-1.0f,-1.0f}
};

and the 16 bit indices for rendering these vertices via DrawIndexedPrimative would be:

static WORD SkyboxIndices[] =
{
0, 1, 3, 0, 3, 2, 4, 7, 5, 4, 6, 7,
0, 2, 6, 0, 6, 4, 1, 7, 3, 1, 5, 7,
0, 5, 1, 0, 4, 5, 2, 3, 7, 2, 7, 6
};

Note: if you are using the cubemap as for a skybox and/or your vertices don't contain normals, remember to disable lighting while rendering (SetRenderState(D3DRS_LIGHTING, FALSE ) ).
Also if rendering a skybox, you will brobably want to disable z buffer before rendering (assuming you have set z buffer values previously via Clear() ).

Remember to re-anable z buffer and lighting after (if required).

HTH,
Cambo_frog

[Edited by - Cambo_frog on July 25, 2005 5:00:58 PM]
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.

This topic is closed to new replies.

Advertisement