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
![Sad :(](./images/smilies/icon_sad.gif)
)
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,