Cubemapping

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Cubemapping

Post by Phunk »

I am trying to build cubemap support in the engine, but as I have no experience in directx(or 3d before irrlicht) I wonder if I am in the right direction, so please tell me if this is wrong. It compiles allright and there are no errors in runtime(everything is in dx8.1)

all modifications are done in CVideoDirectX8.h
The variable m_pCubeMap is a ITexture* so I can call
node->setMaterialTexture(0, m_pCubeMap);

Creation of cubemap:

pID3DDevice->CreateCubeTexture( 256, 1, D3DUSAGE_RENDERTARGET, present.BackBufferFormat,
D3DPOOL_DEFAULT, (IDirect3DCubeTexture8**)((void*)&m_pCubeMap) );

the setRenderStates3DMode() has a new material:

case EMT_CUBE_MAP:

pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
pID3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

pID3DDevice->SetTransform( D3DTS_TEXTURE0, &CubeMapMatrix );//
pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );//

break;
the CubeMapMatrix is not set yet!(but I do not forsee any problems here)


LPDIRECT3DSURFACE8 pFace;
((IDirect3DCubeTexture8*)m_pCubeMap)->GetCubeMapSurface( (D3DCUBEMAP_FACES)1, 0, &pFace );

// some code to load an ITexture in pFace, any sugestions??

pFace->Release();

thats it for now, I hope this not too badly coded :oops: , so please comment!
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Post by Phunk »

just that you know: if you can compile the engine, it is (fairly)easy to hard code a material for bump mapping for DX8/9 in the engine. ill implement it as soon as I'm done with cubemapping.
For all that don't want to wait: look at the bumpmapping code in the DirectX SDK
Greets, Robin
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Post by Phunk »

ok, I see my errors now! I cannot just point a ITexture to a cubemapstucture, ah well, I think that I will put a iscubemap flag in SMaterial then, and if I´m realy lazy, Ill just put in a pointer to a cubemap, so I don´t have to deal with ITexture at all(but that means that I will have to rewrite setMaterial() setTexture() and more)
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Succes!

Post by Phunk »

IT worked! the projectionmatrix is wrong and te code needs cleaning up, but You can see a cubemap!(derivered from ITexture) I will post the code here shortly!
Greets,
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Post by Phunk »

hmm stuck again, does anybody know how to set ETS_WORLD, ETS_VIEW, ETS_PROJECTION and if I need a matrix(and what kind of) to render D3DTS_TEXTURE0 (using directx8), I can see a cubemapped object, but the texturemapping is very wrong, I'v tried almost any combination I could think of, with no luck.

How shall I post the code? I think it will be only the files that I changed , is that fine with everybody, or does somebody have a better suggestion(and maybe webspace, cause I can only upload it myself to a place that has a max of 5kb per sec=freewebs.com :( )

I wanted to post the code when it's finished,
ah well...

Code: Select all

//------------CDirectX8CubeTexture.h-------------------------
// made by Robin
// As u can see, I copied it almost directly from CDirectX8Texture, mipmapping could be enabled, but I wanted it to work first
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in Irrlicht.h

#ifndef __C_DIRECTX8_CUBETEXTURE_H_INCLUDED__
#define __C_DIRECTX8_CUBETEXTURE_H_INCLUDED__

#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_DIRECTX_8_

#include "ITexture.h"
#include "IImage.h"
#include <d3d8.h>

namespace irr
{
namespace video  
{

/*!
	interface for a Video Driver dependent Texture.
*/
class CDirectX8CubeTexture : public ITexture
{
public:

	//! constructor
	CDirectX8CubeTexture( IDirect3DDevice8* device,
		u32 flags);

	//! destructor
	virtual ~CDirectX8CubeTexture();

	//! lock function
	virtual void* lock();

	//! unlock function
	virtual void unlock();

	//! Returns original size of the texture.
	virtual const core::dimension2d<s32>& getOriginalSize();

	//! Returns (=size) of the texture.
	virtual const core::dimension2d<s32>& getSize();	

	//! returns driver type of texture (=the driver, who created the texture)
	virtual EDriverType getDriverType();

	//! returns color format of texture
	virtual ECOLOR_FORMAT getColorFormat();

	//! returns pitch of texture (in bytes)
	virtual s32 getPitch();

	//! returns the DirectX8 Texture
	IDirect3DCubeTexture8* getDX8CubeTexture();

	virtual void setFace(UINT sface);//used for lock and unlock

    virtual UINT getFace();//used for lock and unlock

	virtual bool isCubeTexture();//used for renderer, dunnow if it could be done diffirently(more elegant)
private:

	//! returns the size of a texture which would be the optimize size for rendering it
	inline s32 getTextureSizeFromImageSize(s32 size);

	//! creates the hardware texture
	void createTexture(u32 flags);

	IDirect3DDevice8* Device;
	IDirect3DCubeTexture8* CubeTexture;
	core::dimension2d<s32> TextureSize;
	core::dimension2d<s32> ImageSize;
	s32 Pitch;
	ECOLOR_FORMAT ColorFormat;
	bool SufaceHasSameSize; // true if image has the same dimension as texture.
	bool HasMipMaps;
	UINT face;//face of cube to lock/unlock
};


} // end namespace video
} // end namespace irr

#endif // _IRR_COMPILE_WITH_DIRECTX_8_

#endif // __C_DIRECTX8_CUBETEXTURE_H_INCLUDED__


Code: Select all

//--------------------CDirectX8CubeTexture.cpp-----------------------
#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_DIRECTX_8_

#include "CDirectX8CubeTexture.h"
#include "os.h"

#include <stdio.h>

#ifndef _IRR_COMPILE_WITH_DIRECTX_9_
// The D3DXFilterTexture function seems to get linked wrong when
// compiling with both D3D8 and 9, causing it not to work in the D3D9 device.
#include <d3dx9tex.h>
#pragma comment(lib, "d3dx9.lib")
#endif // _IRR_COMPILE_WITH_DIRECTX_9_

namespace irr
{
namespace video  
{

//! constructor
CDirectX8CubeTexture::CDirectX8CubeTexture(IDirect3DDevice8* device,
								   u32 flags)
: Device(device), TextureSize(0,0), 
CubeTexture(0), Pitch(0), ImageSize(0,0), HasMipMaps(false)
{
	#ifdef _DEBUG
    setDebugName("CDirectX8CubeTexture");
	#endif
	face=0;
	bool generateMipLevels = (flags & video::ETCF_CREATE_MIP_MAPS) != 0;

	if (Device)
		Device->AddRef();

	
	createTexture(flags);

	if (CubeTexture)
	{
		if (generateMipLevels)
		{
			// create mip maps.
			#ifndef _IRR_COMPILE_WITH_DIRECTX_9_
				// The D3DXFilterTexture function seems to get linked wrong when
				// compiling with both D3D8 and 9, causing it not to work in the D3D9 device.
				HRESULT hr  = D3DXFilterCubeTexture(CubeTexture, NULL, 0 , D3DX_DEFAULT );
				if (FAILED(hr))
					os::Printer::log("Could not create direct3d mip map levels.", ELL_WARNING);
				else
					HasMipMaps = true;
			#endif
		}				
	}
	else
		os::Printer::log("Could not create DirectX8 CubeTexture.", ELL_WARNING);
}



//! creates the hardware texture
void CDirectX8CubeTexture::createTexture(u32 flags)
{
	HRESULT hr;
	D3DFORMAT format = D3DFMT_A1R5G5B5;
	
	switch(getTextureFormatFromFlags(flags))
	{
	case ETCF_ALWAYS_16_BIT:
		format = D3DFMT_A1R5G5B5; break;
	case ETCF_ALWAYS_32_BIT:
		format = D3DFMT_A8R8G8B8; break;
	case ETCF_OPTIMIZED_FOR_QUALITY:
		format = D3DFMT_A8R8G8B8; break;
	case ETCF_OPTIMIZED_FOR_SPEED:
		format = D3DFMT_A1R5G5B5; break;
	}

	bool mipmaps = (flags & video::ETCF_CREATE_MIP_MAPS) != 0;

	hr = Device->CreateCubeTexture(256, 1, D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8 ,D3DPOOL_MANAGED, &CubeTexture);
                               
	if (FAILED(hr))
	{
		// try brute force 16 bit

		format = D3DFMT_A1R5G5B5;

		hr = Device->CreateCubeTexture(256, 1,
			D3DUSAGE_RENDERTARGET, 
			D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &CubeTexture);
	}

	ColorFormat = (format == D3DFMT_A1R5G5B5) ? ECF_A1R5G5B5 : ECF_A8R8G8B8;

	TextureSize.Width = 1;//should make here a function for
	TextureSize.Height = 1;//should make here a function for
}


/*//should make here a function for!
//! copies the image to the texture
bool CDirectX8CubeTexture::copyTexture()
{
	if (CubeTexture && Image)
	{
		D3DSURFACE_DESC desc;
		CubeTexture->GetLevelDesc(0, &desc);

		TextureSize.Width = desc.Width;
		TextureSize.Height = desc.Height;

		SufaceHasSameSize = (TextureSize == ImageSize);

		if (desc.Format == D3DFMT_A1R5G5B5)
			return copyTo16BitTexture();
		else
		if (desc.Format == D3DFMT_A8R8G8B8)
			return copyTo32BitTexture();
		else
			os::Printer::log("CDirectX8CubeTexture: Unsupported D3D8 hardware texture format", ELL_ERROR);
	}

	return true;
}
*/







//! destructor
CDirectX8CubeTexture::~CDirectX8CubeTexture()
{
	if (Device)
		Device->Release();

	if (CubeTexture)
		CubeTexture->Release();
}



//! lock function
void* CDirectX8CubeTexture::lock()
{
	if (!CubeTexture)
		return 0;

	D3DLOCKED_RECT rect;
	HRESULT hr = CubeTexture->LockRect((D3DCUBEMAP_FACES)face,0, &rect, 0, 0);
	if (FAILED(hr))
	{
		os::Printer::log("Could not lock DirectX8 CubeTexture.", ELL_ERROR);
		return 0;
	}

	return rect.pBits; 
}



//! unlock function
void CDirectX8CubeTexture::unlock()
{
	if (!CubeTexture)
		return;

	CubeTexture->UnlockRect((D3DCUBEMAP_FACES)face,0);
}


//! Returns original size of the texture.
const core::dimension2d<s32>& CDirectX8CubeTexture::getOriginalSize()
{
	return ImageSize;
}


//! Returns (=size) of the texture.
const core::dimension2d<s32>& CDirectX8CubeTexture::getSize()
{
	return TextureSize;
}


//! returns the size of a texture which would be the optimize size for rendering it
inline s32 CDirectX8CubeTexture::getTextureSizeFromImageSize(s32 size)
{
	s32 ts = 0x01;

	while(ts < size)
		ts <<= 1;

	if (ts > size && ts > 64)
		ts >>= 1;

	return ts;
}



//! returns driver type of texture (=the driver, who created the texture)
EDriverType CDirectX8CubeTexture::getDriverType()
{
	return EDT_DIRECTX8;
}



//! returns color format of texture
ECOLOR_FORMAT CDirectX8CubeTexture::getColorFormat()
{
	return ColorFormat;
}



//! returns pitch of texture (in bytes)
s32 CDirectX8CubeTexture::getPitch()
{
	return Pitch;
}



//! returns the DirectX8 CubeTexture
IDirect3DCubeTexture8* CDirectX8CubeTexture::getDX8CubeTexture()
{

	return CubeTexture;
}

void CDirectX8CubeTexture::setFace(UINT sface)//used for lock and unlock
{
	face = sface;
}

UINT CDirectX8CubeTexture::getFace()//used for lock and unlock
{
	return face;
}

bool CDirectX8CubeTexture::isCubeTexture()//used for renderer, dunnow if it could be done diffirently(more elegant)
{
	return true;
}

} // end namespace video
} // end namespace irr

#endif // _IRR_COMPILE_WITH_DIRECTX_8_

Code: Select all

//------------CVideoNull.cpp
ITexture* CVideoNull::addCubeTexture( const c8* name,E_TEXTURE_CREATION_FLAG cflag)//Edited by Robin
{
	if (!name)
		return 0;

	setTextureCreationFlag(cflag,true);

	ITexture* t = createDeviceDependentCubeTexture();
	addTexture(t, name);

	if (t)
		t->drop();

	return t;
}//Edited by Robin

Code: Select all


//--------------CVideoDirectX8.cpp


video::ITexture* CVideoDirectX8::createDeviceDependentCubeTexture()//Edited by Robin
{
	bool generateMipLevels = false;

	return new CDirectX8CubeTexture( pID3DDevice,
		TextureCreationFlags);
}//Edited by Robin


void CVideoDirectX8::startRenderToCubeMapFace(video::ITexture* cubetexture,unsigned int face)//Edited by Robin
{//Edited by Robin, used to render to a cubemapface, instead of the screen.
	if (!cubetexture)
	{
		os::Printer::log("No Texture", ELL_INFORMATION);
		return;
	}
	if(!cubetexture->isCubeTexture())
	{
		os::Printer::log("No Cubemap Texture", ELL_INFORMATION);
		return;
	}
	LPDIRECT3DSURFACE8 pFace;

	pID3DDevice->GetRenderTarget(&backBuffer);
	pID3DDevice->GetDepthStencilSurface(&depthStencilSurface);

	IDirect3DCubeTexture8* hl = ((CDirectX8CubeTexture*)cubetexture)->getDX8CubeTexture();

    if(hl)
		hl->GetCubeMapSurface( (D3DCUBEMAP_FACES)face, 0, &pFace );
	else
	{
		os::Printer::log("Could not get Cubemap pointer from texture", ELL_INFORMATION);
		return;
	}
	if(!pFace)
	{
		os::Printer::log("Could not get Cubemap Face", ELL_INFORMATION);
		return;
	}
	pID3DDevice->SetRenderTarget ( pFace , depthStencilSurface );
	pFace->Release();
}//Edited by Robin

void CVideoDirectX8::setTexture(s32 stage, video::ITexture* texture)
{
	if (CurrentTexture[stage] == texture)
		return;

	if (texture && texture->getDriverType() != EDT_DIRECTX8)
	{
		os::Printer::log("Fatal Error: Tried to set a texture not owned by this driver.", ELL_ERROR);
		return;
	}
	if (CurrentTexture[stage])
		CurrentTexture[stage]->drop();
	
	if(texture && (texture->isCubeTexture()==true))//Edited by Robin,using lazy evalyation, if C++ sees that statement 1 isn't true, it won't test statement 2(causes a crash!)
	{
		CurrentTexture[stage] = texture;	//Edited by Robin
		pID3DDevice->SetTexture(stage, ((CDirectX8CubeTexture*)texture)->getDX8CubeTexture());//Edited by Robin
		texture->grab();//Edited by Robin
		
	}
	else
	{
			
		if (!texture)
			pID3DDevice->SetTexture(stage, 0);
		else
		{
			pID3DDevice->SetTexture(stage, ((CDirectX8Texture*)texture)->getDX8Texture());
			texture->grab();
		}
		CurrentTexture[stage] = texture;
	}
}

//this is what's giving me problems!

void CVideoDirectX8::setRenderStates3DMode()
{
//stuff here

case EMT_CUBE_MAP://Edited by Robin

			pID3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
			pID3DDevice->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
			pID3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP,  D3DTOP_DISABLE);

			pID3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
			pID3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
			pID3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
			
			pID3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);// Edited by Robin
//			pID3DDevice->SetTransform( D3DTS_TEXTURE0,&SphereMapMatrix);// &CubeMapMatrix );//Edited by Robin
			pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 );//Edited by Robin
			pID3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );//Edited by Robin
			
			break;


You also have to update the .h files, and the include files, but nothing special here.

oh yeah, a function in the other materials that return false on ->isCubeTexture

that's all, the rest is done in the main() function of the program, that is drawing to cubesurfaces ect

please speak up if you find an error/bug that I didn't mention, I'm sure there' lots of!
Greets,
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Post by Phunk »

nobody here with an awnser? or do we just let the punk solve it himself? :P Ah well, I'm out of ideas, so I'll get busy on extending the functions a bit more, like a loader, so you can apply an image to a cubeface, a function that generates a snapshot of the surroundings for a cubemap. And Ill think that I just set the matrices public, so you can figure the reflection out for yourself. To Niko: how should I post everything? just the (modified)cpp files in a zip, an with a link from here, or shall I email/pm it to you? If anyone else wants the source/binary, ill post it, but like I said, my webspace server is verry slow!
Gr.
Tels
Posts: 65
Joined: Fri Feb 27, 2004 7:56 pm
Location: Antarctica
Contact:

Post by Tels »

I hope you can solve this, and Niko incorporate it. If you email everything (modified) to him, he will probably do so. Sorry, can't help you with that problem. But I like the result :D

Best wishes,

Tels
Perl + Irrlicht + Audiere = Game: http://bloodgate.com/perl/game
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Post by Phunk »

just an update, I think I've found out why it didn' t work as intended, and as soon as I can start windows and compile again, I will rewrite the stuff, the problem is in the (Flexible) Vertex Format, and there is no need for a separate matrix for the textures(that was just my ignorance). There must also be an OR after the CAMERAREFLECTIONVECTOR macro, stating the index of the texture normal. Damn! if it werent for those hackers that infected my home network(2 PC's with no valuble info, why in earths name?)
I would have a working version plus basic bumpmapping by now(I think, without a specular environment map.), ah well, it will come eventually, just not these 2 weeks(school & stuff)
Greets
Tels
Posts: 65
Joined: Fri Feb 27, 2004 7:56 pm
Location: Antarctica
Contact:

Post by Tels »

Phunk wrote: Damn! if it werent for those hackers that infected my home network(2 PC's with no valuble info, why in earths name?)
Greets
Because:

* they can
* they don't care
* they can use the machines as zombies/spam relays
* they earn money with this

and the number 1 reason is:

* because you let them.

Next time put a hardware firewall (router) in front of your network, use secure OS (latest windows + patch + hotfix, or linux/bsd/whatever). I know it is hard :)

Anyway, good to hear and I hope you can fix it up soon :)

Cheers,

Tels
Perl + Irrlicht + Audiere = Game: http://bloodgate.com/perl/game
funcdoobiest
Posts: 48
Joined: Thu Jun 15, 2006 6:35 pm

Post by funcdoobiest »

I have written a water shader for my game but i need need dynamic cubemaps to implement it and I came across your stuff here phunk. I was wondering how I would use it.

I am assuming you would need to call startRenderToCubeMapFace() something like below? And I guess I will need to set the camera correctly myself before each call to startRenderToCubeMapFace()?

Code: Select all

//if updating cubemap (rather than drawing scene to screen)

driver->beginScene(true, true, backColor);
smgr->drawAll();
//don't draw GUI etc
//call startRenderToCubeMapFace() for all faces
driver->endScene();

//draw scene as normal
driver->beginScene(true, true, backColor);
smgr->drawAll();
etc.....
I realsie that this probably won't work as it is, I am assuming that you wrote this for an older version of Irrlicht*. I intend to rewrite the code for DX9 but i don't think this will really take much work on the actual cubemap class.

*As far as I can tell the render states are now set by the material renderers themselves rather than it being dealt with by the driver class, so I would have to create a new class from CD3D9MaterialRenderer rather than modifying the functions for setting modes in the driver class. (that wasn't really a question, just thinking out loud so people can correct me if I am way off).
Phunk
Posts: 78
Joined: Sun Dec 14, 2003 8:18 pm
Location: The Netherlands

Post by Phunk »

Hey, Nice that you are creating this, I must admit I never finished it(among other things) I think now that irrlicht has render to texture you do not need my code anymore, like you said.

You would simply put the cam at the object that is to be cubemapped, and take a screenshot in all 6 directions, so you can build a cube of it. Then comes the thing I never got right, the conversion of the matrix, so that the texture behaves like a reflection(mine moved with, instead of mirrored). I think the best way to do this now, is trough the pixel shader, where you need the correct calculation involving the world, view and projection matrix to calculate the correct pixel coordinates.

In other words; my code has become useless, and I would love to see you release this, as it would give irrlicht another great feature. When I wrote that code, I made it by copying bits from the direct x sdk and must admit i never tried to comprehend the math behind it. But if you have more questions about this, I can at least try to help.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

IrrSpintz already has cubemaps implemented( not in DX8.1 ) but in DX9 and OpenGL, and has rendertarget support for them.

You can use that code as a reference for this implementation.
Image
funcdoobiest
Posts: 48
Joined: Thu Jun 15, 2006 6:35 pm

Post by funcdoobiest »

Hahaha I thought no one would reply to this and I haven't actually touched it for a while, then the day after I spend until 4 in the moring trying to get it to work, people do!!

Yeah I was planning on doing the calcualtions in a shader, I actually have the shader working in RenderMonkey, then realised there are no cubemaps in Irrlicht.

I am currently having some troubles with the multisampling types and the setting of rendertargets (just my luck considering all the stuff they DON'T change between DX versions, they decided to change the cubemap and rendertarget stuff slightly).

After having called startRenderToCubeMapFace() begin scene breaks with a DX error:

Direct3D9: (ERROR) :MultiSampleType between DepthStencil Buffer and RenderTarget must match.

I think it may have something to do with wmy incorrect use of the new SetRenderTarget and GetRenderTarget functions. Bleow is phunk's DX 8 code.

Code: Select all

pID3DDevice->GetRenderTarget(&backBuffer);
   pID3DDevice->GetDepthStencilSurface(&depthStencilSurface);

   IDirect3DCubeTexture8* hl = ((CDirectX8CubeTexture*)cubetexture)->getDX8CubeTexture();

    if(hl)
      hl->GetCubeMapSurface( (D3DCUBEMAP_FACES)face, 0, &pFace );
   else
   {
      os::Printer::log("Could not get Cubemap pointer from texture", ELL_INFORMATION);
      return;
   }
   if(!pFace)
   {
      os::Printer::log("Could not get Cubemap Face", ELL_INFORMATION);
      return;
   }
   pID3DDevice->SetRenderTarget ( pFace , depthStencilSurface );
   pFace->Release(); 
This is my DX 9 code, whihc I am sure must just be plain wrong, one thing that I can't figure is that GetRenderTarget takes two parameters and SetRenderTarget now takes a DWORD as its first param I realsie both the first parameteres on each are used to index multiple render targets BUT in the DX9 SDK docs their cubemapping example doesn't use these versions, it just uses them the same way as in 8 - I guess its a mistake but it means I can't find any examples of how I should do it.

Oh and I know it won't actually do anything right nowwith me resetting the render target at the end without having drawn anything etc but i figured if this worked it would be trivial to add another fucntion to reset the rendertargets etc. and call these wo etierh side of the necessary transformations and renders in my app.

Code: Select all

  pID3DDevice->GetRenderTarget(0, &backBuffer);
   pID3DDevice->GetDepthStencilSurface(&depthStencilSurface);

   IDirect3DCubeTexture9* hl = ((CD3D9CubeTexture*)cubetexture)->getDX9CubeTexture();

    if(hl)
      hl->GetCubeMapSurface( (D3DCUBEMAP_FACES)face, 0, &pFace );
	else
	{
		os::Printer::log("Could not get Cubemap pointer from texture", ELL_INFORMATION);
		return;
	}
	if(!pFace)
	{
		os::Printer::log("Could not get Cubemap Face", ELL_INFORMATION);
		return;
	}
	pID3DDevice->SetRenderTarget( 0 , pFace);
	pFace->Release();
	//set render target back to back buffer.
	pID3DDevice->SetRenderTarget( 0, backBuffer);
	backBuffer->Release();
	depthStencilSurface->Release();

@Spintz, yeah thanks I was thinking about taking a look at some of the engines derived form Irrlicht to see how they got it working may have a look tonight.

EDIT: had a look, I see you have used an adapted version of the engines own setrendertarget() function, i had wondered about doing that then realsied that it probably wasn't best to start anything new in the state I was in but looking at that has definitely helped - seems like the obvious choice now :roll: don't know why I didn't even think to look at it for an example of using the DX9 set/setrendertarget() functions :oops:
funcdoobiest
Posts: 48
Joined: Thu Jun 15, 2006 6:35 pm

Post by funcdoobiest »

Spintz, I have been looking at IrrSpintz and I have a question, when you use setRenderTarget (in the dx9 version) to set the cubeface as the render target how do you get away without having to set a new DepthStencilSurface? Because the only way I can get mine to run without breaking on a DX error (the one about multisample types matching) is to also create a new depth stencil surface with the multisample parameter set to D3DMULTISAMPLE_NONE, saving the old one so it can be restored with the previous render target. I am hoping that me doing this may be causing the problems I am having (because otherwise I have no idea)
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Maybe mine doesn't work. The tests I've done with that, always have the stencil buffer disabled. I can test it out.

EDIT: I just tried and it doesn't crash with StencilBuffer enabled. I'll play around with stuff more and see if I can see what you're talking about.
Image
Post Reply