// Initialises the given render method (vertex + pixel shader), returns true on success
bool LoadMethod( int method )
{
	// If the vertex shader for this method has not already been initialised
	if (!renderMethods[method].vertexShader)
	{
		// Load the vertex shader file specified above, storing resultant DirectX data
		if (!LoadVertexShader( renderMethods[method].vertexShaderFile,
                               &renderMethods[method].vertexShader,
		                       &renderMethods[method].vertexConsts ))
		{
			return false;
		}
	}
	if (!renderMethods[method].pixelShader)
	{
		// Load the vertex shader file specified above, storing resultant DirectX data
		if (!LoadPixelShader( renderMethods[method].pixelShaderFile,
		                      &renderMethods[method].pixelShader,
		                      &renderMethods[method].pixelConsts ))
		{
			return false;
		}
	}

	if (!renderMethods[method].vertexDecl)
	{
		if (FAILED(g_pd3dDevice->CreateVertexDeclaration( renderMethods[method].vertexElts,
		                                                  &renderMethods[method].vertexDecl )))
		{
			return false;
		}
	}

	return true;
}
Exemple #2
0
HRESULT HookIDirect3DDevice9::CreateVertexDeclaration(LPVOID _this,
													  CONST D3DVERTEXELEMENT9* pVertexElements,
													  IDirect3DVertexDeclaration9** ppDecl)
{
	LOG_API();
	return pD3Dev->CreateVertexDeclaration(pVertexElements, ppDecl);
}
void initVertexData()
{
	size_t count = partCount;
	VertexData *vertexData = new VertexData[count];

	for(size_t i = 0; i < count; ++i)
	{
		vertexData[i].x = particlesCoord[i].x;
		vertexData[i].y = particlesCoord[i].y;
		vertexData[i].z = 0.f;
		vertexData[i].u = 0;
		vertexData[i].v = 0;
	}

	void *pRectBuffer = NULL;
	device->CreateVertexBuffer(count*sizeof(VertexData), D3DUSAGE_WRITEONLY,
		D3DFVF_XYZ | D3DFVF_TEX0, D3DPOOL_DEFAULT, &pVertexObject, NULL);

	pVertexObject->Lock(0, count*sizeof(VertexData), &pRectBuffer, 0);

	memcpy(pRectBuffer, vertexData, count*sizeof(VertexData));
	pVertexObject->Unlock();

	delete[] vertexData;
	vertexData = nullptr;

	D3DVERTEXELEMENT9 decl[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	device->CreateVertexDeclaration(decl, &vertexDecl);
}
void initRectangleVertexes()
{
	VertexData data[4] = {
		/*   x          y           z       u   v   */
		{ 0, 0, 0, 0, 0 },
		{ Width, 0, 0, 1, 0 },
		{ 0, Height, 0, 0, 1 },
		{ Width, Height, 0, 1, 1 }
	};

	void *pRectBuffer = NULL;
	device->CreateVertexBuffer(4 * sizeof(VertexData), D3DUSAGE_WRITEONLY,
		D3DFVF_XYZ | D3DFVF_TEX0, D3DPOOL_DEFAULT, &pRectObject, NULL);

	pRectObject->Lock(0, 4 * sizeof(VertexData), &pRectBuffer, 0);

	memcpy(pRectBuffer, data, 4 * sizeof(VertexData));
	pRectObject->Unlock();

	D3DVERTEXELEMENT9 decl[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	device->CreateVertexDeclaration(decl, &RectDecl);
}
Exemple #5
0
void DXDirectionalLight::CreateShadowMap(LPDIRECT3DDEVICE9 device, DWORD type, DWORD size)
{
	HRESULT hr;

	DXLight::CreateShadowMap(device, type, size);
	hr = device->CreateTexture(size, size, 1, D3DUSAGE_RENDERTARGET, D3DFMT_G32R32F, D3DPOOL_DEFAULT, &shadowmap, NULL);

	if( SUCCEEDED(hr) )
		hr = device->CreateTexture(size, size, 1, D3DUSAGE_RENDERTARGET, D3DFMT_G32R32F, D3DPOOL_DEFAULT, &blur, NULL);

	if( FAILED(hr) )
	{
		if( shadowmap )
			shadowmap->Release();

		shadowmap = blur = NULL;
	}

	if( !blurdeclforpointfordirectional )
	{
		D3DVERTEXELEMENT9 elem[] =
		{
			{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
			{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
			D3DDECL_END()
		};

		device->CreateVertexDeclaration(elem, &blurdeclforpointfordirectional);
	}
	else
		blurdeclforpointfordirectional->AddRef();
}
Exemple #6
0
void CompileShaders() {
	std::string errorMsg;
	HRESULT hr = -1;

	if (!CompileVertexShader(vscode, &pFramebufferVertexShader, NULL, errorMsg)) {
		OutputDebugStringA(errorMsg.c_str());
		DebugBreak();
	}

	if (!CompilePixelShader(pscode, &pFramebufferPixelShader, NULL, errorMsg)) {
		OutputDebugStringA(errorMsg.c_str());
		DebugBreak();
	}

	pD3Ddevice->CreateVertexDeclaration(VertexElements, &pFramebufferVertexDecl);
	pD3Ddevice->SetVertexDeclaration(pFramebufferVertexDecl);
	pD3Ddevice->CreateVertexDeclaration(SoftTransVertexElements, &pSoftVertexDecl);
}
Exemple #7
0
//-----------------------------------------------------------------------------
HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, LPCWSTR strFilename )
{
    WCHAR        strPath[MAX_PATH];
    LPD3DXBUFFER pAdjacencyBuffer = NULL;
    LPD3DXBUFFER pMtrlBuffer = NULL;
    HRESULT      hr;

    // Cleanup previous mesh if any
    Destroy();

    // Find the path for the file, and convert it to ANSI (for the D3DX API)
    DXUTFindDXSDKMediaFileCch( strPath, sizeof(strPath) / sizeof(WCHAR), strFilename );

    // Load the mesh
    if( FAILED( hr = D3DXLoadMeshFromX( strPath, D3DXMESH_MANAGED, pd3dDevice, 
                                        &pAdjacencyBuffer, &pMtrlBuffer, NULL,
                                        &m_dwNumMaterials, &m_pMesh ) ) )
    {
        return hr;
    }

    // Optimize the mesh for performance
    if( FAILED( hr = m_pMesh->OptimizeInplace(
                        D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
                        (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
    {
        SAFE_RELEASE( pAdjacencyBuffer );
        SAFE_RELEASE( pMtrlBuffer );
        return hr;
    }

    // Set strPath to the path of the mesh file
    WCHAR *pLastBSlash = wcsrchr( strPath, L'\\' );
    if( pLastBSlash )
        *(pLastBSlash + 1) = L'\0';
    else
        *strPath = L'\0';

    D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
    hr = CreateMaterials( strPath, pd3dDevice, d3dxMtrls, m_dwNumMaterials );

    SAFE_RELEASE( pAdjacencyBuffer );
    SAFE_RELEASE( pMtrlBuffer );

    // Extract data from m_pMesh for easy access
    D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
    m_dwNumVertices    = m_pMesh->GetNumVertices();
    m_dwNumFaces       = m_pMesh->GetNumFaces();
    m_dwBytesPerVertex = m_pMesh->GetNumBytesPerVertex();
    m_pMesh->GetIndexBuffer( &m_pIB );
    m_pMesh->GetVertexBuffer( &m_pVB );
    m_pMesh->GetDeclaration( decl );
    pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl );

    return hr;
}
bool CompileShaders(std::string &errorMsg) {
	if (!CompileVertexShader(vscode, &pFramebufferVertexShader, NULL, errorMsg)) {
		OutputDebugStringA(errorMsg.c_str());
		return false;
	}

	if (!CompilePixelShader(pscode, &pFramebufferPixelShader, NULL, errorMsg)) {
		OutputDebugStringA(errorMsg.c_str());
		if (pFramebufferVertexShader) {
			pFramebufferVertexShader->Release();
		}
		return false;
	}

	pD3Ddevice->CreateVertexDeclaration(VertexElements, &pFramebufferVertexDecl);
	pD3Ddevice->SetVertexDeclaration(pFramebufferVertexDecl);
	pD3Ddevice->CreateVertexDeclaration(SoftTransVertexElements, &pSoftVertexDecl);

	return true;
}
Exemple #9
0
void InitAllVertexDeclarations(LPDIRECT3DDEVICE9 pD3DDevice)
{
	//===============================================================
	// VertexPos

	D3DVERTEXELEMENT9 VertexPosElements[] = 
	{
		{0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		D3DDECL_END()
	};	
	pD3DDevice->CreateVertexDeclaration(VertexPosElements, &VertexPos::Decl);
}
		LPDIRECT3DVERTEXDECLARATION9 CreateVertexDeclaration( LPDIRECT3DDEVICE9 device, D3DVERTEXELEMENT9* elements )
		{
			if ( device && elements )
			{
				LPDIRECT3DVERTEXDECLARATION9 vertDecl = NULL;
				HRESULT hr = device->CreateVertexDeclaration( elements, &vertDecl );
				if ( SUCCEEDED( hr ) )
				{
					return vertDecl;
				}
			}
			return NULL;
		}
Exemple #11
0
//-----------------------------------------------------------------------------
HRESULT CSkybox::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice, float fSize, IDirect3DCubeTexture9* pCubeTexture,
                                 WCHAR* strEffectFileName )
{
    HRESULT hr;

    m_pd3dDevice = pd3dDevice;
    m_fSize = fSize;
    m_pEnvironmentMap = pCubeTexture;

    // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the shader debugger.  
    // Debugging vertex shaders requires either REF or software vertex processing, and debugging 
    // pixel shaders requires REF.  The D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug 
    // experience in the shader debugger.  It enables source level debugging, prevents instruction 
    // reordering, prevents dead code elimination, and forces the compiler to compile against the next 
    // higher available software target, which ensures that the unoptimized shaders do not exceed 
    // the shader model limitations.  Setting these flags will cause slower rendering since the shaders 
    // will be unoptimized and forced into software.  See the DirectX documentation for more information 
    // about using the shader debugger.
    DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;

#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DXSHADER_DEBUG;
    #endif

#ifdef DEBUG_VS
        dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
    #endif
#ifdef DEBUG_PS
        dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
    #endif

    // Read the D3DX effect file
    WCHAR str[MAX_PATH];
    V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strEffectFileName ) );

    // If this fails, there should be debug output as to 
    // they the .fx file failed to compile
    V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL,
                                        dwShaderFlags, NULL, &m_pEffect, NULL ) );

    // Create vertex declaration
    V_RETURN( pd3dDevice->CreateVertexDeclaration( g_aSkyboxDecl, &m_pVertexDecl ) );

    return S_OK;
}
HRESULT InitFX()
{
	D3DVERTEXELEMENT9	decl[MAX_FVF_DECL_SIZE];
	// FVF를 사용해서 정점선언값을 자동으로 채워넣는다
	D3DXDeclaratorFromFVF( MYVERTEX::FVF, decl );
	// 정점선언값으로 g_pDecl을 생성한다.
	g_pd3dDevice->CreateVertexDeclaration( decl, &g_pDecl );

	// simple.FX 파일을 읽어와서 ID3DXEffect인터페이스를 생성한다.
	if( FAILED( D3DXCreateEffectFromFile( g_pd3dDevice, "shader.fx", NULL, NULL, 0, NULL, &g_pEffect, NULL ) ) )
	{
		MessageBox( NULL, "Effect Load Failed", "shader.fx", MB_OK );
		return E_FAIL;
	}

	return S_OK;
}
Exemple #13
0
//-----------------------------------------------------------------------------
HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice,
                           LPD3DXFILEDATA pFileData )
{
    LPD3DXBUFFER pMtrlBuffer = NULL;
    LPD3DXBUFFER pAdjacencyBuffer = NULL;
    HRESULT      hr;

    // Cleanup previous mesh if any
    Destroy();

    // Load the mesh from the DXFILEDATA object
    if( FAILED( hr = D3DXLoadMeshFromXof( pFileData, D3DXMESH_MANAGED, pd3dDevice,
                                          &pAdjacencyBuffer, &pMtrlBuffer, NULL,
                                          &m_dwNumMaterials, &m_pMesh ) ) )
    {
        return hr;
    }

    // Optimize the mesh for performance
    if( FAILED( hr = m_pMesh->OptimizeInplace(
                        D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
                        (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
    {
        SAFE_RELEASE( pAdjacencyBuffer );
        SAFE_RELEASE( pMtrlBuffer );
        return hr;
    }

    D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
    hr = CreateMaterials( L"", pd3dDevice, d3dxMtrls, m_dwNumMaterials );

    SAFE_RELEASE( pAdjacencyBuffer );
    SAFE_RELEASE( pMtrlBuffer );

    // Extract data from m_pMesh for easy access
    D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
    m_dwNumVertices    = m_pMesh->GetNumVertices();
    m_dwNumFaces       = m_pMesh->GetNumFaces();
    m_dwBytesPerVertex = m_pMesh->GetNumBytesPerVertex();
    m_pMesh->GetIndexBuffer( &m_pIB );
    m_pMesh->GetVertexBuffer( &m_pVB );
    m_pMesh->GetDeclaration( decl );
    pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl );

    return hr;
}
Exemple #14
0
//-----------------------------------------------------------------------------
HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, ID3DXMesh* pInMesh, 
                           D3DXMATERIAL* pd3dxMaterials, DWORD dwMaterials )
{
    // Cleanup previous mesh if any
    Destroy();

    // Optimize the mesh for performance
    DWORD *rgdwAdjacency = NULL;
    rgdwAdjacency = new DWORD[pInMesh->GetNumFaces() * 3];
    if( rgdwAdjacency == NULL )
        return E_OUTOFMEMORY;
    pInMesh->GenerateAdjacency(1e-6f,rgdwAdjacency);

    D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
    pInMesh->GetDeclaration( decl );

    DWORD dwOptions = pInMesh->GetOptions();
    dwOptions &= ~(D3DXMESH_32BIT | D3DXMESH_SYSTEMMEM | D3DXMESH_WRITEONLY);
    dwOptions |= D3DXMESH_MANAGED;
    dwOptions |= D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE;

    ID3DXMesh* pTempMesh = NULL;
    if( FAILED( pInMesh->Optimize( dwOptions, rgdwAdjacency, NULL, NULL, NULL, &pTempMesh ) ) )
    {
        SAFE_DELETE_ARRAY( rgdwAdjacency );
        return E_FAIL;
    }

    SAFE_DELETE_ARRAY( rgdwAdjacency );
    SAFE_RELEASE( m_pMesh );
    m_pMesh = pTempMesh;

    HRESULT hr;
    hr = CreateMaterials( L"", pd3dDevice, pd3dxMaterials, dwMaterials );

    // Extract data from m_pMesh for easy access
    m_dwNumVertices    = m_pMesh->GetNumVertices();
    m_dwNumFaces       = m_pMesh->GetNumFaces();
    m_dwBytesPerVertex = m_pMesh->GetNumBytesPerVertex();
    m_pMesh->GetIndexBuffer( &m_pIB );
    m_pMesh->GetVertexBuffer( &m_pVB );
    m_pMesh->GetDeclaration( decl );
    pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl );

    return hr;
}
HRESULT InitVS()
{
	D3DVERTEXELEMENT9	decl[MAX_FVF_DECL_SIZE];
	// FVF를 사용해서 정점선언값을 자동으로 채워넣는다
	D3DXDeclaratorFromFVF( MYVERTEX::FVF, decl );
	// 정점선언값으로 g_pDecl을 생성한다.
	g_pd3dDevice->CreateVertexDeclaration( decl, &g_pDecl );
	LPD3DXBUFFER pCode;

	// simple.vs 파일을 읽어와서 정점쉐이더 인터페이스를 생성한다.
	if( FAILED( D3DXAssembleShaderFromFile( "simple.vs", NULL, NULL, 0, &pCode, NULL ) ) )
		return E_FAIL;

	g_pd3dDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(), &g_pVS);

	S_REL( pCode );

	return S_OK;
}
Exemple #16
0
bool CObject3D::InitStaticDeviceObjects(LPDIRECT3DDEVICE9 device)
{
	D3DVERTEXELEMENT9 decl[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 },
		{ 0, 20, D3DDECLTYPE_SHORT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 },
		{ 0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 36, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	if (FAILED(device->CreateVertexDeclaration(decl, &s_skinVertexDeclaration)))
	{
		qCritical("Can't create skin vertex declaration. You must (re)install the lastest DirectX9 runtime on http://www.microsoft.com/en-us/download/details.aspx?id=8109");
		return false;
	}

	LPD3DXBUFFER code;
	if (FAILED(D3DXAssembleShaderFromResource(NULL, MAKEINTRESOURCE(IDR_SKINVS), NULL, NULL, 0, &code, NULL)))
	{
		qCritical("Can't assemble skin vertex shader from resource. You must (re)install the lastest DirectX9 runtime on http://www.microsoft.com/en-us/download/details.aspx?id=8109");
		return false;
	}

	if (FAILED(device->CreateVertexShader((DWORD*)code->GetBufferPointer(), &s_skinVS)))
	{
		qCritical("Can't create skin vertex shader. You must (re)install the lastest DirectX9 runtime on http://www.microsoft.com/en-us/download/details.aspx?id=8109");
		Release(code);
		return false;
	}
	Release(code);

	s_reflectTexture.SetDevice(device);
	if (!s_reflectTexture.Load("Model/Texture/etc_reflect.tga"))
	{
		qCritical("Can't load Model/Texture/etc_reflect.tga");
		return false;
	}

	return true;
}
Exemple #17
0
HRESULT InitScene()
{
	HRESULT hr;
	LPD3DXBUFFER errors = NULL;

	D3DVERTEXELEMENT9 decl[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	SetWindowText(hwnd, TITLE);

	MYVALID(CreateColorTex(device, 0xff77FF70, &texture));
	MYVALID(D3DXLoadMeshFromXA("../media/meshes/knot.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &mesh));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/intensity.png", &intensity));

	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &colortarget, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &normaltarget, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &edgetarget, NULL));
	MYVALID(device->CreateVertexDeclaration(decl, &vertexdecl));

	edgetarget->GetSurfaceLevel(0, &edgesurface);
	colortarget->GetSurfaceLevel(0, &colorsurface);
	normaltarget->GetSurfaceLevel(0, &normalsurface);

	MYVALID(device->CreateTexture(512, 512, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &text, NULL));
	MYVALID(DXCreateEffect("../media/shaders/celshading.fx", device, &effect));

	DXRenderText(HELP_TEXT, text, 512, 512);

	D3DXVECTOR3 eye(0.5f, 0.5f, -1.5f);
	D3DXVECTOR3 look(0, 0, 0);
	D3DXVECTOR3 up(0, 1, 0);

	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 3, (float)screenwidth / (float)screenheight, 0.1f, 10);
	D3DXMatrixLookAtLH(&view, &eye, &look, &up);
	D3DXMatrixIdentity(&world);

	return S_OK;
}
Exemple #18
0
HRESULT CShader::LoadEffect( LPDIRECT3DDEVICE9 pd3dDevice, LPCTSTR szFileName )
{
    HRESULT hr;
    LPD3DXEFFECT pEffect;

	D3DVERTEXELEMENT9	decl[MAX_FVF_DECL_SIZE];

	D3DXDeclaratorFromFVF( D3DFVF_XYZRHW | D3DFVF_TEX1, decl );
	pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl );
	
#ifdef _DEBUG
    if(FAILED(hr = D3DXCreateEffectFromFile( D3DDEVICE, szFileName, NULL, NULL, 0, NULL, &pEffect, NULL )))
	{
		Error( "CShader::LoadEffect : %s 생성 실패", szFileName );
        return hr;
	}
#else

	if(FAILED(hr = D3DXCreateEffectFromResource( D3DDEVICE, NULL, MAKEINTRESOURCE(IDR_DATA2), NULL, NULL, 0, NULL, &pEffect, NULL )))
	{
		Error( "CShader::LoadEffect : %s 생성 실패", szFileName );
		return hr;
	}
#endif

	m_pEffect = pEffect;
	
    D3DXHANDLE hTechnique;
    if( FAILED(hr = m_pEffect->FindNextValidTechnique(NULL, &hTechnique)) )
	{
		Error( "CShader::LoadEffect : %s Not Valid", szFileName );
        return hr;
	}
	
	m_pEffect->SetTechnique( hTechnique );
//	m_pEffect->SetTexture( "DiffuseTexture", m_pTexture0 );

	return S_OK;
}
Exemple #19
0
//-------------------------------------
// DirectX9()
//-------------------------------------
DirectX9::DirectX9()
{
	D3DPRESENT_PARAMETERS parameters;
	D3DDISPLAYMODE display;
	LPDIRECT3D9 directx9;
	LPDIRECT3DDEVICE9 device;
	DWORD multisample_quality;

	directx9 = Direct3DCreate9(D3D_SDK_VERSION);
	if (!directx9)
	{
		ASSERT_ERROR("DirectX9の生成に失敗");
		return;
	}

	if (FAILED(directx9->GetAdapterDisplayMode(
		D3DADAPTER_DEFAULT,
		&display)))
	{
		ASSERT_ERROR("ディスプレイ状態の取得に失敗");
		return;
	}

	ZeroMemory(&parameters, sizeof(parameters));
	parameters.BackBufferCount = 1;
	parameters.BackBufferWidth =
		static_cast<UINT>(SCREEN_WIDTH);
	parameters.BackBufferHeight =
		static_cast<UINT>(SCREEN_HEIGHT);
	parameters.BackBufferFormat = display.Format;
	parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
	parameters.Windowed = TRUE;
	parameters.EnableAutoDepthStencil = TRUE;
	parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
	parameters.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES;

	directx9->CheckDeviceMultiSampleType(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		display.Format,
		TRUE,
		D3DMULTISAMPLE_4_SAMPLES,
		&multisample_quality);

	parameters.MultiSampleQuality = multisample_quality - 1;

	if (parameters.Windowed)
	{
		parameters.FullScreen_RefreshRateInHz = 0;
		parameters.PresentationInterval =
			D3DPRESENT_INTERVAL_IMMEDIATE;
	}
	else
	{
		parameters.BackBufferFormat = D3DFMT_R5G6B5;
		parameters.FullScreen_RefreshRateInHz =
			D3DPRESENT_RATE_DEFAULT;
		parameters.PresentationInterval =
			D3DPRESENT_INTERVAL_DEFAULT;
	}


	if (FAILED(directx9->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		WindowHolder::handle_,
		D3DCREATE_HARDWARE_VERTEXPROCESSING |
		D3DCREATE_MULTITHREADED,
		&parameters,
		&device)))
	{
		if (FAILED(directx9->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			WindowHolder::handle_,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING |
			D3DCREATE_MULTITHREADED,
			&parameters,
			&device)))
		{
			if (FAILED(directx9->CreateDevice(
				D3DADAPTER_DEFAULT,
				D3DDEVTYPE_REF,
				WindowHolder::handle_,
				D3DCREATE_SOFTWARE_VERTEXPROCESSING |
				D3DCREATE_MULTITHREADED,
				&parameters,
				&device)))
			{
				ASSERT_ERROR("描画デバイスの生成に失敗");
				return;
			}
		}
	}

	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
	device->SetRenderState(D3DRS_ZENABLE, TRUE);
	device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	device->SetRenderState(D3DRS_LIGHTING, TRUE);

	device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
	device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, 1);

	device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
	device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);


	D3DVERTEXELEMENT9 velement2d[] = {
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		{ 0, 16, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
		{ 0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END(),
	};

	if (FAILED(device->CreateVertexDeclaration(
		velement2d,
		&DirectX9Holder::vertex_declaration_2d_)))
	{
		ASSERT_ERROR("2D用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velement3d[] = {
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
		{ 0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END(),
	};

	if (FAILED(device->CreateVertexDeclaration(
		velement3d,
		&DirectX9Holder::vertex_declaration_3d_)))
	{
		ASSERT_ERROR("3D用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velementx[] = {
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END(),
	};

	if (FAILED(device->CreateVertexDeclaration(
		velementx,
		&DirectX9Holder::vertex_declaration_x_)))
	{
		ASSERT_ERROR("Xモデル用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velementfbx[] = {
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 },
		{ 0, 24, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 },
		{ 0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 40, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
		{ 0, 44, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	if (FAILED(device->CreateVertexDeclaration(
		velementfbx,
		&DirectX9Holder::vertex_declaration_fbx_)))
	{
		ASSERT_ERROR("fbx用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velementins[] = {
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		{1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
		D3DDECL_END(),
	};

	if(FAILED(device->CreateVertexDeclaration(
		velementins,
		&DirectX9Holder::vertex_declaration_instancing_)))
	{
		ASSERT_ERROR("インスタンシング用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velementbench[] = {
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		{ 1, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
		{ 1, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2 },
		{ 1, 32, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3 },
		{ 1, 48, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4 },
		D3DDECL_END(),
	};

	if (FAILED(device->CreateVertexDeclaration(
		velementbench,
		&DirectX9Holder::vertex_declaration_instancing_bench_)))
	{
		ASSERT_ERROR("インスタンシングベンチ用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velementfield[] = {
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
		{ 0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		{ 0, 36, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
		D3DDECL_END(),
	};

	if (FAILED(device->CreateVertexDeclaration(
		velementfield,
		&DirectX9Holder::vertex_declaration_field_)))
	{
		ASSERT_ERROR("地形用頂点宣言生成に失敗");
	}

	D3DVERTEXELEMENT9 velementshadow[] = {
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		D3DDECL_END(),
	};

	if(FAILED(device->CreateVertexDeclaration(
		velementshadow,
		&DirectX9Holder::vertex_declaration_shadow_)))
	{
		ASSERT_ERROR("地形用頂点宣言生成に失敗");
	}

	DirectX9Holder::directx9_ = directx9;
	DirectX9Holder::device_ = device;
}
Exemple #20
0
HRESULT InitScene()
{
	HRESULT hr;
	D3DDISPLAYMODE mode;

	if( FAILED(hr = direct3d->GetAdapterDisplayMode(0, &mode)) )
	{
		MYERROR("Could not get adapter mode");
		return hr;
	}
	
	if( FAILED(hr = direct3d->CheckDeviceFormat( 0, D3DDEVTYPE_HAL, mode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) )
	{
		MYERROR("No floating point rendertarget support");
		return hr;
	}
	
	// más depth/stencil-el még müködhet
	if( FAILED(hr = direct3d->CheckDepthStencilMatch( 0, D3DDEVTYPE_HAL, mode.Format, D3DFMT_A16B16G16R16F, D3DFMT_D24S8)) )
	{
		MYERROR("D3DFMT_A16B16G16R16F does not support D3DFMT_D24S8");
		return hr;
	}

	D3DVERTEXELEMENT9 elem[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};
	
	SetWindowText(hwnd, TITLE);

	MYVALID(D3DXLoadMeshFromX("../media/meshes/skullocc3.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &mesh1));
	MYVALID(D3DXLoadMeshFromX("../media/meshes//knot.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &mesh2));
	MYVALID(D3DXLoadMeshFromX("../media/meshes//teapot.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &mesh3));
	MYVALID(D3DXLoadMeshFromX("../media/meshes/sky.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &skymesh));

	mesh = mesh1;

	//MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/altar.dds", &skytexture));
	//MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/altar_rough.dds", &roughspecular));
	MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/grace.dds", &skytexture));
	MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/grace_rough.dds", &roughspecular));
	//MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/beach.dds", &skytexture));
	//MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/beach_rough.dds", &roughspecular));
	//MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/stpeters.dds", &skytexture));
	//MYVALID(D3DXCreateCubeTextureFromFile(device, "../media/textures/stpeters_rough.dds", &roughspecular));

	MYVALID(D3DXCreateTextureFromFile(device, "../media/textures/gold.jpg", &texture));
	MYVALID(D3DXCreateTextureFromFile(device, "../media/textures/fresnel.png", &fresneltexture));

	// downsample & blur textures
	for( int i = 0; i < 5; ++i )
	{
		MYVALID(device->CreateTexture(screenwidth / (2 << i), screenheight / (2 << i), 1, D3DUSAGE_RENDERTARGET,
			D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &dstargets[i], NULL));

		MYVALID(device->CreateTexture(screenwidth / (2 << i), screenheight / (2 << i), 1, D3DUSAGE_RENDERTARGET,
			D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &blurtargets[i], NULL));

		MYVALID(blurtargets[i]->GetSurfaceLevel(0, &blursurfaces[i]));
		MYVALID(dstargets[i]->GetSurfaceLevel(0, &dssurfaces[i]));
	}

	// star textures (8x 1 MB @ 1080p)
	for( int i = 0; i < 4; ++i )
	{
		for( int j = 0; j < 2; ++j )
		{
			MYVALID(device->CreateTexture(screenwidth / 4, screenheight / 4, 1, D3DUSAGE_RENDERTARGET,
				D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &startargets[i][j], NULL));

			MYVALID(startargets[i][j]->GetSurfaceLevel(0, &starsurfaces[i][j]));
		}
	}

	// lens flare textures (2x 4 MB @ 1080p)
	for( int i = 0; i < 2; ++i )
	{
		MYVALID(device->CreateTexture(screenwidth / 2, screenheight / 2, 1, D3DUSAGE_RENDERTARGET,
			D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &ghosttargets[i], NULL));

		MYVALID(ghosttargets[i]->GetSurfaceLevel(0, &ghostsurfaces[i]));
	}

	// luminance textures
	for( int i = 0; i < 4; ++i )
	{
		UINT j = 256 / (4 << (2 * i));

		MYVALID(device->CreateTexture(j, j, 1, D3DUSAGE_RENDERTARGET,
			D3DFMT_R16F, D3DPOOL_DEFAULT, &avglumtargets[i], NULL));

		MYVALID(avglumtargets[i]->GetSurfaceLevel(0, &avglumsurfaces[i]));
	}

	// adapted luminance textures
	MYVALID(device->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R16F, D3DPOOL_DEFAULT, &avglumtargets[4], NULL));
	MYVALID(device->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R16F, D3DPOOL_DEFAULT, &avglumtargets[5], NULL));

	MYVALID(avglumtargets[4]->GetSurfaceLevel(0, &avglumsurfaces[4]));
	MYVALID(avglumtargets[5]->GetSurfaceLevel(0, &avglumsurfaces[5]));

	// afterimage textures (2x 4 MB @ 1080p)
	MYVALID(device->CreateTexture(screenwidth / 2, screenheight / 2, 1, D3DUSAGE_RENDERTARGET,
		D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &afterimages[0], NULL));

	MYVALID(device->CreateTexture(screenwidth / 2, screenheight / 2, 1, D3DUSAGE_RENDERTARGET,
		D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &afterimages[1], NULL));

	MYVALID(afterimages[0]->GetSurfaceLevel(0, &aftersurfaces[0]));
	MYVALID(afterimages[1]->GetSurfaceLevel(0, &aftersurfaces[1]));

	// other
	MYVALID(device->CreateTexture(512, 512, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &text, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &scenetarget, NULL));
	MYVALID(device->CreateVertexDeclaration(elem, &vertexdecl));

	// other
	MYVALID(scenetarget->GetSurfaceLevel(0, &scenesurface));

	MYVALID(DXCreateEffect("../media/shaders/hdreffects.fx", device, &hdreffect));
	MYVALID(DXCreateEffect("../media/shaders/hdrfresnel.fx", device, &fresnel));
	MYVALID(DXCreateEffect("../media/shaders/sky.fx", device, &skyeffect));

	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
	device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
	device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);

	device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
	device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
	device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);

	device->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(2, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
	device->SetSamplerState(2, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
	device->SetSamplerState(2, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);

	device->SetSamplerState(3, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(3, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(3, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	device->SetSamplerState(4, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(4, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(4, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	device->SetSamplerState(5, D3DSAMP_MINFILTER, D3DTEXF_POINT);
	device->SetSamplerState(5, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
	device->SetSamplerState(5, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	DXRenderText(HELP_TEXT, text, 512, 512);

	// setup camera
	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 3, (float)screenwidth / (float)screenheight, 1, 50);
	D3DXMatrixIdentity(&world);

	cameraangle				= D3DXVECTOR2(0.6f, 0.1f);
	objectangle				= D3DXVECTOR2(0, 0);
	exposurevelocity		= 0;
	destexposurevelocity	= 0;
	exposure				= 0.05f;
	targetluminance			= 0.03f;

	UpdateText();

	return S_OK;
}
Exemple #21
0
HRESULT InitScene()
{
	HRESULT hr;
	D3DCAPS9 caps;

	SetWindowText(hwnd, TITLE);

	device->GetDeviceCaps(&caps);

	if( caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0) )
	{
		MYERROR("This demo requires Shader Model 2.0 capable video card");
		return E_FAIL;
	}

	palm = new DXObject(device);
	sandplane = new DXObject(device);
	waterplane = new DXObject(device);

	if( !palm->Load("../media/meshes/palm.qm") )
	{
		MYERROR("Could not load palm");
		return E_FAIL;
	}

	if( !sandplane->CreatePlane(50, 50, 10, 10) )
	{
		MYERROR("Could not create sand plane");
		return E_FAIL;
	}

	if( !waterplane->CreatePlane(50, 50, 5, 5) )
	{
		MYERROR("Could not create water plane");
		return E_FAIL;
	}

	waterplane->GenerateTangentFrame();

	MYVALID(D3DXLoadMeshFromXA("../media/meshes/sky.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &skymesh));
	MYVALID(D3DXCreateCubeTextureFromFileA(device, "../media/textures/sky7.dds", &skytex));

	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/bark.jpg", &bark));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/leaf.jpg", &leaves));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/sand.jpg", &sand));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/wave2.png", &waves));

	MYVALID(DXCreateEffect("../media/shaders/ambient.fx", device, &ambient));
	MYVALID(DXCreateEffect("../media/shaders/blinnphong.fx", device, &specular));
	MYVALID(DXCreateEffect("../media/shaders/water.fx", device, &water));
	MYVALID(DXCreateEffect("../media/shaders/simplebloom.fx", device, &bloom));
	MYVALID(DXCreateEffect("../media/shaders/godray.fx", device, &godray));
	MYVALID(DXCreateEffect("../media/shaders/sky.fx", device, &skyeffect));

	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &refraction, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &reflection, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8, D3DPOOL_DEFAULT, &occluders, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8, D3DPOOL_DEFAULT, &blurtex, NULL));
	MYVALID(device->CreateTexture(screenwidth, screenheight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &sceneldr, NULL));

	MYVALID(device->CreateTexture(screenwidth / 2, screenheight / 2, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &bloomtex1, NULL));
	MYVALID(device->CreateTexture(screenwidth / 2, screenheight / 2, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &bloomtex2, NULL));

	refraction->GetSurfaceLevel(0, &refractsurf);
	reflection->GetSurfaceLevel(0, &reflectsurf);
	occluders->GetSurfaceLevel(0, &occludersurf);
	blurtex->GetSurfaceLevel(0, &blursurf);
	sceneldr->GetSurfaceLevel(0, &sceneldrsurf);
	bloomtex1->GetSurfaceLevel(0, &bloomsurf1);
	bloomtex2->GetSurfaceLevel(0, &bloomsurf2);

	D3DVERTEXELEMENT9 elem[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	MYVALID(device->CreateVertexDeclaration(elem, &quaddecl));

	cameraangle = D3DXVECTOR2(-1.13f * D3DX_PI, 0.55f);

	return S_OK;
}
Exemple #22
0
void CompileShaders() {
	ID3DXBuffer* pShaderCode = NULL;
	ID3DXBuffer* pErrorMsg = NULL;
	HRESULT hr = -1;

#ifdef _XBOX

	// Compile vertex shader.
	hr = D3DXCompileShader( vscode, 
		(UINT)strlen( vscode ),
		NULL, 
		NULL, 
		"main", 
		"vs_2_0", 
		0, 
		&pShaderCode, 
		&pErrorMsg,
		NULL );
#endif 

	if( FAILED(hr) )
	{
		OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
		DebugBreak();
	}

	// Create pixel shader.
	pD3Ddevice->CreateVertexShader( (DWORD*)pShaderCode->GetBufferPointer(), 
		&pFramebufferVertexShader );

	pShaderCode->Release();

#ifdef _XBOX
	// Compile pixel shader.
	hr = D3DXCompileShader( pscode, 
		(UINT)strlen( pscode ),
		NULL, 
		NULL, 
		"main", 
		"ps_2_0", 
		0, 
		&pShaderCode, 
		&pErrorMsg,
		NULL );
#endif

	if( FAILED(hr) )
	{
		OutputDebugStringA((CHAR*)pErrorMsg->GetBufferPointer());
		DebugBreak();
	}

	// Create pixel shader.
	pD3Ddevice->CreatePixelShader( (DWORD*)pShaderCode->GetBufferPointer(), 
		&pFramebufferPixelShader );

	pShaderCode->Release();

	pD3Ddevice->CreateVertexDeclaration( VertexElements, &pFramebufferVertexDecl );
	pD3Ddevice->SetVertexDeclaration( pFramebufferVertexDecl );

	pD3Ddevice->CreateVertexDeclaration( SoftTransVertexElements, &pSoftVertexDecl );
}
HRESULT InitScene()
{
	HRESULT hr;
	
	D3DVERTEXELEMENT9 elem[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	SetWindowText(hwnd, TITLE);

	MYVALID(D3DXLoadMeshFromX("../media/meshes/box.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &box));
	MYVALID(D3DXLoadMeshFromX("../media/meshes/skullocc3.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &skull));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/marble.dds", &texture1));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/wood2.jpg", &texture2));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/crate.jpg", &texture3));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/pcfnoise.bmp", &noise));

	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &shadowmap, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &blurRGBA32F, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &sincoeffs, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &coscoeffs, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &blurARGB8, NULL));
	
	MYVALID(device->CreateTexture(800, 512, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &text, NULL));
	MYVALID(device->CreateVertexDeclaration(elem, &vertexdecl));

	MYVALID(DXCreateEffect("../media/shaders/exponentialshadow.fx", device, &exponential));
	MYVALID(DXCreateEffect("../media/shaders/convolutionshadow.fx", device, &convolution));
	MYVALID(DXCreateEffect("../media/shaders/varianceshadow.fx", device, &variance));
	MYVALID(DXCreateEffect("../media/shaders/expvarianceshadow.fx", device, &expvariance));
	MYVALID(DXCreateEffect("../media/shaders/boxblur5x5.fx", device, &boxblur5x5));
	MYVALID(DXCreateEffect("../media/shaders/pcfshadow5x5.fx", device, &pcf5x5));
	MYVALID(DXCreateEffect("../media/shaders/pcss.fx", device, &pcss));
	MYVALID(DXCreateEffect("../media/shaders/irregularpcf.fx", device, &pcfirreg));

	DXRenderText(
		"Use the mouse to rotate the camera and the light\n\n0 - Unfiltered\n1 - PCF (5x5)\n2 - Irregular PCF\n3 - Variance\n"
		"4 - Convolution\n5 - Exponential\n6 - Exponential variance\n7 - PCSS", text, 800, 512);
	
	cameraangle = D3DXVECTOR2(0.78f, 0.78f);
	lightangle = D3DXVECTOR2(3.2f, 0.85f);

	DXAABox			boxbb;
	DXAABox			skullbb;
	DXAABox			tmpbb;
	D3DXMATRIX		tmp1, tmp2, tmp3;
	D3DXVECTOR3*	vdata;

	box->LockVertexBuffer(D3DLOCK_READONLY, (void**)&vdata);
	D3DXComputeBoundingBox(vdata, box->GetNumVertices(), box->GetNumBytesPerVertex(), &boxbb.Min, &boxbb.Max);
	box->UnlockVertexBuffer();

	skull->LockVertexBuffer(D3DLOCK_READONLY, (void**)&vdata);
	D3DXComputeBoundingBox(vdata, skull->GetNumVertices(), skull->GetNumBytesPerVertex(), &skullbb.Min, &skullbb.Max);
	skull->UnlockVertexBuffer();

	for( int i = 0; i < numobjects; ++i )
	{
		SceneObject& obj = objects[i];

		D3DXMatrixScaling(&tmp1, obj.scale.x, obj.scale.y, obj.scale.z);
		D3DXMatrixRotationYawPitchRoll(&tmp2, obj.angles.x, obj.angles.y, obj.angles.z);
		D3DXMatrixTranslation(&tmp3, obj.position.x, obj.position.y, obj.position.z);

		D3DXMatrixMultiply(&obj.world, &tmp1, &tmp2);
		D3DXMatrixMultiply(&obj.world, &obj.world, &tmp3);

		if( obj.type == SKULL )
			tmpbb = skullbb;
		else
			tmpbb = boxbb;

		tmpbb.TransformAxisAligned(obj.world);

		scenebb.Add(tmpbb.Min);
		scenebb.Add(tmpbb.Max);
	}

	return S_OK;
}
void SkinMesh::LoadModelFile(char *filename){
	int i = 0, j = 0;					//loop index
	FILE *fp = NULL;
	VERTEX_BLEND_3D *work_vtx = NULL;
	WORD *work_index = NULL;
	int file_size;
	unsigned char *loading_buff;
	int cursor = 0;
	LPDIRECT3DDEVICE9 device = NULL;		//デバイス取得用

	device = _renderer->GetDevice();

	fp = fopen(filename, "rb");

	if(fp == NULL){return;}

	//いったん全部読み込む!
	fseek(fp, 0, SEEK_END);
	file_size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	//プールの動的確保
	loading_buff = new unsigned char[file_size];
	fread(loading_buff, 1, file_size, fp);

	fclose(fp);

	//メッシュ数
	memcpy(&_mesh_count, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
	//fwrite(&mesh_count, sizeof(int), 1, fp);

	//メッシュ配列の動的確保
	_mesh = new MESH[_mesh_count];
	memset(_mesh, 0, sizeof(MESH)*_mesh_count);

	

	//頂点要素の定義
	//D3DXVECTOR3 vtx;		// 頂点座標
	//float weight[3];		// 重み
	//DWORD mtx_index;		// 頂点インデックス(下位バイトが若番)
	//D3DXVECTOR3 nor;		// 法線ベクトル
	//D3DCOLOR diffuse;		// 反射光
	//D3DXVECTOR2 tex;		// テクスチャ座標

	D3DVERTEXELEMENT9 v_ele[] = {
		{0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0},
		{0, 24, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0},
		{0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		{0, 40, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
		{0, 44, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		D3DDECL_END()
	};

	//declaration初期化
	if(FAILED(device->CreateVertexDeclaration(v_ele, &_decl))){
		return;
	}

	//メッシュ配列
	for(i=0; i<_mesh_count; i++){
		//頂点数
		memcpy(&_mesh[i].vertex_count, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
		//fwrite(&mesh[i].vertex_count, sizeof(int), 1, fp);

		//頂点
		if(FAILED(device->CreateVertexBuffer(sizeof(VERTEX_BLEND_3D) * _mesh[i].vertex_count,
											 D3DUSAGE_WRITEONLY,
											 0,
											 D3DPOOL_MANAGED,
											 &_mesh[i].vertex,
											 NULL))){
			//頂点確保に失敗
			_mesh[i].vertex = NULL;
		}
		_mesh[i].vertex->Lock(0, 0, (void**)&work_vtx, 0);
		memcpy(work_vtx, loading_buff + cursor, sizeof(VERTEX_BLEND_3D) * _mesh[i].vertex_count);
		cursor += sizeof(VERTEX_BLEND_3D) * _mesh[i].vertex_count;
		//fwrite(work_vtx, sizeof(VERTEX_BLEND_3D), mesh[i].vertex_count, fp);		
		_mesh[i].vertex->Unlock();

		//index数
		memcpy(&_mesh[i].index_count, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
		//fwrite(&mesh[i].index_count, sizeof(int), 1, fp);

		//index
		if(FAILED(device->CreateIndexBuffer(sizeof(WORD) * _mesh[i].index_count,
											D3DUSAGE_WRITEONLY,
											D3DFMT_INDEX16,
											D3DPOOL_MANAGED,
											&_mesh[i].index,
											NULL))){
			_mesh[i].index = NULL;
		}
		_mesh[i].index->Lock(0, 0, (void**)&work_index, 0);
		memcpy(work_index, loading_buff + cursor, sizeof(WORD) * _mesh[i].index_count);
		cursor += sizeof(WORD) * _mesh[i].index_count;
		//fwrite(work_index, sizeof(WORD), mesh[i].index_count, fp);		
		_mesh[i].index->Unlock();

		//マテリアル
		memcpy(&_mesh[i].material, loading_buff + cursor, sizeof(D3DMATERIAL9)); cursor += sizeof(D3DMATERIAL9);
		//fwrite(&mesh[i].material, sizeof(D3DMATERIAL9), 1, fp);

		//ファイル名
		memcpy(_mesh[i].texture_filename, loading_buff + cursor, sizeof(char) * 128); cursor += sizeof(char) * 128;
		//fwrite(mesh[i].texture_filename, sizeof(char), 128, fp);

		//テクスチャロード
		char filename[256] = "";
		sprintf(filename, "data/TEXTURE/%s", _mesh[i].texture_filename);
		_mesh[i].texture = _renderer->LoadTexture(filename);
	}

	//ボーン数
	memcpy(&_bone_count, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
	//fwrite(&bone_count, sizeof(int), 1, fp);

	//ボーン配列
	_bone = new BONE[_bone_count];

	for(i=0; i<_bone_count; i++){
		//ボーン構造体を生で リストポインタとpoolの中のキーフレーム配列×3にNULLを
		memcpy(&_bone[i], loading_buff + cursor, sizeof(BONE)); cursor += sizeof(BONE);
		//fwrite(&bone_buff, sizeof(BONE), 1, fp);

		//子のid(NULLならば-1)
		int child_id = -1;
		memcpy(&child_id, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
		if(child_id == -1){_bone[i].child = NULL;}
		else{_bone[i].child = &_bone[child_id];}
		//fwrite(&child_id, sizeof(int), 1, fp);

		//兄弟のid
		int sibling_id = -1;
		memcpy(&sibling_id, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
		if(sibling_id == -1){_bone[i].sibling = NULL;}
		else{_bone[i].sibling = &_bone[sibling_id];}
		//fwrite(&sibling_id, sizeof(int), 1, fp);

		//アニメーションプール
		int key_num = 0;
		//key配列
		key_num = _bone[i].key_count;
		_bone[i].key = new KEY_FRAME[key_num];
		memcpy(_bone[i].key, loading_buff + cursor, sizeof(KEY_FRAME) * key_num);
		cursor += sizeof(KEY_FRAME) * key_num;
		//fwrite(bone_table[i]->pool.scl_key, sizeof(SCALING_KEY), bone_table[i]->pool.scl_key_max, fp);
		
	}

	//animationの個数
	memcpy(&_animation_count, loading_buff + cursor, sizeof(int)); cursor += sizeof(int);
	//fwrite(&animation_max, sizeof(int), 1, fp);

	//animation配列 current_timeは0で洗っておく
	if(_animation_count > 0){
		_animation = new ANIMATION[_animation_count];
		memcpy(_animation, loading_buff + cursor, sizeof(ANIMATION) * _animation_count);
		cursor += sizeof(ANIMATION) * _animation_count;
	}
	//if(animation != NULL){
	//	fwrite(animation, sizeof(ANIMATION), 1, fp);
	//}

	delete [] loading_buff;

	//root登録
	_root = &_bone[0];

#ifdef _CONVERTER_
	hidden = new bool[mesh_count];
	selected = new bool[mesh_count];
	for(i=0; i<mesh_count; i++){
		hidden[i] = false;
		selected[i] = false;
	}
#endif
}
HRESULT InitScene()
{
	HRESULT hr;

	SetWindowText(hwnd, TITLE);

	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/marble.dds", &texture1));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/wood2.jpg", &texture2));
	MYVALID(DXCreateEffect("../media/shaders/ambient.fx", device, &ambient));
	MYVALID(DXCreateEffect("../media/shaders/blinnphong.fx", device, &specular));
	MYVALID(DXCreateEffect("../media/shaders/extrude.fx", device, &extrude));

	MYVALID(device->CreateTexture(512, 128, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &text, NULL));
	MYVALID(DXCreateTexturedBox(device, D3DXMESH_MANAGED, &shadowreceiver));

	// box
	MYVALID(DXCreateTexturedBox(device, D3DXMESH_MANAGED, &objects[0].object));
	MYVALID(DXCreateCollisionBox(device, D3DXMESH_SYSTEMMEM, &objects[0].caster));

	D3DXMatrixTranslation(&objects[0].world, -1.5f, 0.55f, 1.2f);

	MYVALID(device->CreateVertexBuffer(VOLUME_NUMVERTICES(objects[0].caster->GetNumVertices()) * sizeof(D3DXVECTOR4), D3DUSAGE_DYNAMIC, D3DFVF_XYZW, D3DPOOL_DEFAULT, &objects[0].vertices, NULL));
	MYVALID(device->CreateIndexBuffer(VOLUME_NUMINDICES(objects[0].caster->GetNumFaces()) * sizeof(WORD), D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &objects[0].indices, NULL));

	// sphere
	MYVALID(DXCreateTexturedSphere(device, 0.5f, 30, 30, D3DXMESH_SYSTEMMEM, &objects[1].object));
	MYVALID(DXCreateCollisionSphere(device, 0.5f, 30, 30, D3DXMESH_SYSTEMMEM, &objects[1].caster));

	D3DXMatrixTranslation(&objects[1].world, 1.0f, 0.55f, 0.7f);

	MYVALID(device->CreateVertexBuffer(VOLUME_NUMVERTICES(objects[1].caster->GetNumVertices()) * sizeof(D3DXVECTOR4), D3DUSAGE_DYNAMIC, D3DFVF_XYZW, D3DPOOL_DEFAULT, &objects[1].vertices, NULL));
	MYVALID(device->CreateIndexBuffer(VOLUME_NUMINDICES(objects[1].caster->GetNumFaces()) * sizeof(WORD), D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &objects[1].indices, NULL));

	// L shape
	MYVALID(DXCreateTexturedLShape(device, D3DXMESH_MANAGED, &objects[2].object));
	MYVALID(DXCreateCollisionLShape(device, D3DXMESH_SYSTEMMEM, &objects[2].caster));

	D3DXMatrixTranslation(&objects[2].world, 0, 0.55f, -1);

	MYVALID(device->CreateVertexBuffer(VOLUME_NUMVERTICES(objects[2].caster->GetNumVertices()) * sizeof(D3DXVECTOR4), D3DUSAGE_DYNAMIC, D3DFVF_XYZW, D3DPOOL_DEFAULT, &objects[2].vertices, NULL));
	MYVALID(device->CreateIndexBuffer(VOLUME_NUMINDICES(objects[2].caster->GetNumFaces()) * sizeof(WORD), D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &objects[2].indices, NULL));

#if 0
	D3DXMATERIAL defmat;

	defmat.pTextureFilename = 0;
	defmat.MatD3D.Ambient = D3DXCOLOR(1, 1, 1, 1);
	defmat.MatD3D.Diffuse = D3DXCOLOR(1, 1, 1, 1);
	defmat.MatD3D.Specular = D3DXCOLOR(1, 1, 1, 1);
	defmat.MatD3D.Emissive = D3DXCOLOR(0, 0, 0, 0);
	defmat.MatD3D.Power = 20;

	DXSaveMeshToQM("../media/meshes10/box.qm", objects[0].object, &defmat, 1);
	DXSaveMeshToQM("../media/meshes10/collisionbox.qm", objects[0].caster, &defmat, 1);

	DXSaveMeshToQM("../media/meshes10/sphere.qm", objects[1].object, &defmat, 1);
	DXSaveMeshToQM("../media/meshes10/collisionsphere.qm", objects[1].caster, &defmat, 1);

	DXSaveMeshToQM("../media/meshes10/lshape.qm", objects[2].object, &defmat, 1);
	DXSaveMeshToQM("../media/meshes10/collisionlshape.qm", objects[2].caster, &defmat, 1);
#endif

	// generate edges
	std::cout << "Generating edge info...\n";

	for( int i = 0; i < NUM_OBJECTS; ++i )
		GenerateEdges(objects[i].edges, objects[i].caster);

	// shadow volume decl
	D3DVERTEXELEMENT9 elem[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		D3DDECL_END()
	};

	MYVALID(device->CreateVertexDeclaration(elem, &shadowdecl));

	// effect
	specular->SetFloat("ambient", 0);

	cameraangle = D3DXVECTOR2(0.78f, 0.78f);
	lightangle = D3DXVECTOR2(2.8f, 0.78f);

	DXRenderText("Use mouse to rotate camera and light\n\n1: draw silhouette\n2: draw shadow volume", text, 512, 128);

	return S_OK;
}
void InitFullScreenQuad()
{
	// vertex declaration
	D3DVERTEXELEMENT9 vtxDesc[3];
	int offset = 0;
	int i = 0;

	// position
	vtxDesc[i].Stream = 0;
	vtxDesc[i].Offset = offset;
	vtxDesc[i].Type = D3DDECLTYPE_FLOAT3;
	vtxDesc[i].Method = D3DDECLMETHOD_DEFAULT;
	vtxDesc[i].Usage = D3DDECLUSAGE_POSITION;
	vtxDesc[i].UsageIndex = 0;

	offset += sizeof(float)* 3;
	++i;

	// UV coords 0
	vtxDesc[i].Stream = 0;
	vtxDesc[i].Offset = offset;
	vtxDesc[i].Type = D3DDECLTYPE_FLOAT2;
	vtxDesc[i].Method = D3DDECLMETHOD_DEFAULT;
	vtxDesc[i].Usage = D3DDECLUSAGE_TEXCOORD;
	vtxDesc[i].UsageIndex = 0;

	offset += sizeof(float)* 2;
	++i;

	// end of the vertex format (D3DDECL_END())
	vtxDesc[i].Stream = 0xFF;
	vtxDesc[i].Offset = 0;
	vtxDesc[i].Type = D3DDECLTYPE_UNUSED;
	vtxDesc[i].Method = 0;
	vtxDesc[i].Usage = 0;
	vtxDesc[i].UsageIndex = 0;

	gpD3DDevice->CreateVertexDeclaration(vtxDesc, &gpFullscreenQuadDecl);

	// create a vertex buffer
	gpD3DDevice->CreateVertexBuffer(offset * 4, 0, 0, D3DPOOL_MANAGED, &gpFullscreenQuadVB, NULL);
	void * vertexData = NULL;
	gpFullscreenQuadVB->Lock(0, 0, &vertexData, 0);
	{
		float * data = (float*)vertexData;
		*data++ = -1.0f;	*data++ = 1.0f;		*data++ = 0.0f;
		*data++ = 0.0f;		*data++ = 0.0f;

		*data++ = 1.0f;		*data++ = 1.0f;		*data++ = 0.0f;
		*data++ = 1.0f;		*data++ = 0;

		*data++ = 1.0f;		*data++ = -1.0f;	*data++ = 0.0f;
		*data++ = 1.0f;		*data++ = 1.0f;

		*data++ = -1.0f;	*data++ = -1.0f;	*data++ = 0.0f;
		*data++ = 0.0f;		*data++ = 1.0f;
	}
	gpFullscreenQuadVB->Unlock();

	// create an index buffer
	gpD3DDevice->CreateIndexBuffer(sizeof(short)* 6, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &gpFullscreenQuadIB, NULL);
	void * indexData = NULL;
	gpFullscreenQuadIB->Lock(0, 0, &indexData, 0);
	{
		unsigned short * data = (unsigned short*)indexData;
		*data++ = 0;	*data++ = 1;	*data++ = 3;
		*data++ = 3;	*data++ = 1;	*data++ = 2;
	}
	gpFullscreenQuadIB->Unlock();
}