Example #1
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();
}
Example #2
0
IDirect3DTexture9 *CreatTex(LPSTR dat)
{
	LPDIRECT3DTEXTURE9 pTex = NULL;
	UINT xx,yy;
	xx = *(UINT*)(dat+4);
	yy = *(UINT*)(dat+8);
	D3DLOCKED_RECT rc;
	HRESULT hr;

	if (*(DWORD*)(dat+0x28) == 'DXT3')
	{
		hr = g_pD3DDevice->CreateTexture(xx,yy,0,0 ,D3DFMT_DXT3,D3DPOOL_MANAGED,&pTex,NULL);
		if (hr != D3D_OK) return NULL;

		hr = pTex->LockRect(0,&rc,NULL,0);
		if (hr == D3D_OK)
		{
			CopyMemory(rc.pBits,dat+0x28+12,(xx/4) * (yy/4) * 16 );
			pTex->UnlockRect(0);
		}
		return pTex;
	}

	if (*(DWORD*)(dat+0x28) == 'DXT1')
	{
		hr = g_pD3DDevice->CreateTexture(xx,yy,0,0,D3DFMT_DXT1,D3DPOOL_MANAGED,&pTex,NULL);
		if (hr != D3D_OK) return NULL;

		hr = pTex->LockRect(0,&rc,NULL,0);
		if (hr == D3D_OK)
		{
			CopyMemory(rc.pBits,dat+0x28+12,(xx/4) * (yy/4) * 8  );
			pTex->UnlockRect(0);
		}
		return pTex;
	}

	hr = g_pD3DDevice->CreateTexture(xx,yy,0,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&pTex,NULL);
	if (hr != D3D_OK) return NULL;

	hr = pTex->LockRect(0,&rc,NULL,0);
	if (hr == D3D_OK)
	{
		for (DWORD jy = 0; jy < yy; ++jy)
		{
			for (DWORD jx = 0; jx < xx; ++jx)
			{
				DWORD *pp  = (DWORD *)rc.pBits;
				BYTE  *idx = (BYTE  *)(dat+0x28+0x400);
				DWORD *pal = (DWORD *)(dat+0x28);
				pp[(yy-jy-1)*xx+jx] = pal[idx[jy*xx+jx]];
			}
		}
		pTex->UnlockRect(0);
	}
	return pTex;
}
Example #3
0
unsigned RageDisplay_D3D::CreateTexture( 
	RagePixelFormat pixfmt,
	RageSurface* img,
	bool bGenerateMipMaps )
{
	// texture must be power of two
	ASSERT( img->w == power_of_two(img->w) );
	ASSERT( img->h == power_of_two(img->h) );


	HRESULT hr;
	IDirect3DTexture9* pTex;
	hr = g_pd3dDevice->CreateTexture( img->w, img->h, 1, 0, D3DFORMATS[pixfmt], D3DPOOL_MANAGED, &pTex, NULL );

#if defined(XBOX)
	while(hr == E_OUTOFMEMORY)
	{
		if(!vmem_Manager.DecommitLRU())
			break;
		hr = g_pd3dDevice->CreateTexture( img->w, img->h, 1, 0, D3DFORMATS[pixfmt], D3DPOOL_MANAGED, &pTex );
	}
#endif

	if( FAILED(hr) )
		RageException::Throw( "CreateTexture(%i,%i,pixfmt=%i) failed: %s", 
		img->w, img->h, pixfmt, GetErrorString(hr).c_str() );

	unsigned uTexHandle = (unsigned)pTex;

	if( pixfmt == FMT_PAL )
	{
		// Save palette
		TexturePalette pal;
		memset( pal.p, 0, sizeof(pal.p) );
		for( int i=0; i<img->format->palette->ncolors; i++ )
		{
			RageSurfaceColor &c = img->format->palette->colors[i];
			pal.p[i].peRed = c.r;
			pal.p[i].peGreen = c.g;
			pal.p[i].peBlue = c.b;
			pal.p[i].peFlags = c.a;
		}

		ASSERT( g_TexResourceToTexturePalette.find(uTexHandle) == g_TexResourceToTexturePalette.end() );
		g_TexResourceToTexturePalette[uTexHandle] = pal;
	}

	UpdateTexture( uTexHandle, img, 0, 0, img->w, img->h );

	return uTexHandle;
}
Example #4
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	g_projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, 1.0f, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 改變三角形正面的面向
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	// 使用自動normalize功能
	device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

	// 配置動態貼圖
	{
		device->CreateTexture(g_framebuffer_w, g_framebuffer_h, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL);
		device->CreateDepthStencilSurface(g_framebuffer_w, g_framebuffer_h, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &g_pDepthStencil, NULL);

		device->CreateTexture(g_framebuffer_w, g_framebuffer_h, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pBlurTextures[0], NULL);
		device->CreateTexture(g_framebuffer_w, g_framebuffer_h, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pBlurTextures[1], NULL);
	}

	// 載入模型
	{
		CGutModel::SetTexturePath("../../textures/");

		g_Model_DX9.ConvertToDX9Model(&g_Model);
		g_Terrain_DX9.ConvertToDX9Model(&g_Terrain);
	}

	// 設定貼圖內插方法
	{
		device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

		device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

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

		device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
	}

	return true;
}
Example #5
0
//------------------------------------------------------------
// Initialization code
//------------------------------------------------------------
bool InitEverything(HWND hWnd)
{
	// init D3D
	if (!InitD3D(hWnd))
	{
		return false;
	}

	// create a fullscreen quad
	InitFullScreenQuad();

	// create a render target
	if (FAILED(gpD3DDevice->CreateTexture(WIN_WIDTH, WIN_HEIGHT,
		1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT, &gpSceneRenderTarget, NULL)))
	{
		return false;
	}

	// loading models, shaders and textures
	if (!LoadAssets())
	{
		return false;
	}

	// load fonts
	if (FAILED(D3DXCreateFont(gpD3DDevice, 20, 10, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, (DEFAULT_PITCH | FF_DONTCARE),
		"Arial", &gpFont)))
	{
		return false;
	}

	return true;
}
Example #6
0
static HRESULT CreateColorTex(LPDIRECT3DDEVICE9 device, DWORD color, LPDIRECT3DTEXTURE9* texture)
{
	UINT width = 4;
	UINT height = 4;

	D3DLOCKED_RECT rect;
	HRESULT hr = device->CreateTexture(width, height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, texture, NULL);

	if( FAILED(hr) )
		return hr;

	(*texture)->LockRect(0, &rect, NULL, 0);

	for( UINT i = 0; i < height; ++i )
	{
		for( UINT j = 0; j < width; ++j )
		{
			DWORD* ptr = ((DWORD*)rect.pBits + i * width + j);
			*ptr = color;
		}
	}

	(*texture)->UnlockRect(0);
	return S_OK;
}
Example #7
0
void InitStateDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 使用trilinear
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
	//
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	//
	device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);

	device->GetRenderTarget(0, &g_pMainFrameBuffer);
	device->GetDepthStencilSurface(&g_pMainDepthBuffer);

	int width, height;
	GutGetWindowSize(width, height);

	g_iFrameBufferWidth = width * 2;
	g_iFrameBufferHeight = height * 2;

	device->CreateTexture(g_iFrameBufferWidth, g_iFrameBufferHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL);
	device->CreateDepthStencilSurface(g_iFrameBufferWidth, g_iFrameBufferHeight, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &g_pDepthStencil, NULL);
	g_pTexture->GetSurfaceLevel(0, &g_pFrameBuffer);
}
Example #8
0
// 텍스처
// 생성자 (크기)
CTexture::CTexture(LPDIRECT3DDEVICE9 device, int w, int h)
	: Device(device), Texture(nullptr), TextureW(w), TextureH(h), OriginalW(w), OriginalH(h)
{
	device->CreateTexture(
		w, h, 1, D3DUSAGE_RENDERTARGET,
		D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &Texture, nullptr);
}
Example #9
0
	DistortingCallback( ::EffekseerRendererDX9::Renderer* renderer, 
		LPDIRECT3DDEVICE9 device, int texWidth, int texHeight )
		: renderer( renderer ), device( device )
	{
		device->CreateTexture( texWidth, texHeight, 1, D3DUSAGE_RENDERTARGET, 
			D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &texture, NULL );
	}
Example #10
0
bool CDynamicTexture::CreateTexture(LPDIRECT3DDEVICE9 device, UINT width, UINT height, D3DFORMAT format)
{
    assert(m_textureSystem == NULL && m_surfaceSystem == NULL
           && m_textureDefault == NULL && m_surfaceDefault == NULL);
    m_device = device;

    m_width = width;
    m_height = height;
    m_format = format;

    if (CreateDefaultTexture() == false)
        return false;

    if (FAILED(device->CreateTexture(width, height, 1,
                                     D3DUSAGE_DYNAMIC, format,
                                     D3DPOOL_SYSTEMMEM, &m_textureSystem, NULL)))
    {
        MessageBox(NULL, "Create Texture Error", "ERROR", 0);
        return false;
    }
    if (FAILED(m_textureSystem->GetSurfaceLevel(0, &m_surfaceSystem)))
    {
        m_textureSystem->Release();
        m_textureSystem = NULL;
        MessageBox(NULL, "Get Surface Error", "ERROR", 0);
        return false;
    }
    return true;
}
Example #11
0
/**
 * load() method loads in a lightmap from pixel buffer "data", using the
 * information found in face. The d3dFace parameter is the face used for
 * triangulating a BSP Face. The d3dFace also keeps track of the texture
 * coordinates used for the lightmap, which is why it is needed to load
 * the lightmap. The "device" parameter is used in making a texture from
 * the lightmap with Direct3D.
 */
void LightMap::load( char *data, BSP::Face *face, D3D::Face *d3dFace, LPDIRECT3DDEVICE9 device ) {

    // compute the width and height of this lightmap
    width = ceil( d3dFace->getMaxU() / 16 ) - floor( d3dFace->getMinU() / 16 ) + 1;
    height = ceil( d3dFace->getMaxV() / 16 ) - floor( d3dFace->getMinV() / 16 ) + 1;

    // The maximum width or height of a lightmap is 16 pixels
    if ( width > 16 ) {
        width = 16;
    }
    if ( height > 16 ) {
        height = 16;
    }

    // Copy the data from the data pointer into a temporary Pixel buffer
    vector< Pixel > pixels;
    pixels.resize( width * height );

    data += face->lightmap_offset;

    Pixel *copy = ( Pixel * ) data;

    for ( int i = 0; i < width * height; ++i ) {
        pixels[ i ].r = copy->b;
        pixels[ i ].g = copy->g;
        pixels[ i ].b = copy->r;
        pixels[ i ].a = 255;

        data += 3;
        copy = ( Pixel * ) data;
    }

    // Give DirectX our lightmap information
    D3DLOCKED_RECT lr;

    device->CreateTexture(width,
		height, 1, 0,
		D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, NULL);

	texture->LockRect( 0, &lr, NULL, 0 );

	unsigned char* pRect = ( UCHAR* ) lr.pBits;

    // copy image to pRect
    memcpy( pRect, &pixels[ 0 ], width * height * sizeof( Pixel ) );

	texture->UnlockRect( 0 );

    //d3dFace->divideLMTexCoords( d3dFace->getMaxU() - d3dFace->getMinU(), d3dFace->getMaxV() - d3dFace->getMinV() );
    //d3dFace->shiftLMTexCoords( -d3dFace->getMinU(), -d3dFace->getMinV() );

    // Top-left corner is at luxel coord: (-8, -8)
    d3dFace->shiftLMTexCoords( -d3dFace->getMinU() - 8, -d3dFace->getMinV() - 8 );
    //d3dFace->shiftLMTexCoords( -d3dFace->getMinU() - 7, -d3dFace->getMinV() - 7 );
    //d3dFace->shiftLMTexCoords( -8, -8 );
    d3dFace->divideLMTexCoords( d3dFace->getMaxU() - d3dFace->getMinU(), d3dFace->getMaxV() - d3dFace->getMinV() );

};
Example #12
0
HRESULT HookIDirect3DDevice9::CreateTexture(LPVOID _this, UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DTexture9** ppTexture,HANDLE* pSharedHandle)
{
	LOG_API();
	HRESULT res = pD3Dev->CreateTexture(Width, Height, Levels, Usage, Format, Pool, ppTexture, pSharedHandle);
	if (Usage & D3DUSAGE_RENDERTARGET) {
		logmsg("RENDER_TARGET %d, %d = %x\n", Width, Height, *ppTexture);
	}
	return res;
}
//-----------------------------------------------------------------------------
// Desc: 初始化Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D(HWND hWnd)
{
	//创建Direct3D对象, 该对象用于创建Direct3D设备对象
	if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
		return E_FAIL;

	//设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

	//创建Direct3D设备对象
	if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp, &g_pd3dDevice)))
	{
		return E_FAIL;
	}

	//禁用照明效果
	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	HRESULT hr = g_pd3dDevice->CreateTexture(256, 256, 1, D3DUSAGE_RENDERTARGET,
		D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pRenderTex, NULL);
	if (FAILED(hr))
	{
		return E_FAIL;
	}
	g_pRenderTex->GetSurfaceLevel(0, &g_pRenderSur);

	hr = g_pd3dDevice->CreateTexture(256, 256, 1, D3DUSAGE_RENDERTARGET,
		D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pRender2Tex, NULL);
	if (FAILED(hr))
	{
		return E_FAIL;
	}
	g_pRender2Tex->GetSurfaceLevel(0, &g_pRender2Sur);

	return S_OK;
}
bool CDX9TextureObject::MakeBlankTexture( UINT sizex, UINT sizey, UINT colordepth, IHashString * hformat, UINT mips )
{
	DeInit();

	LPDIRECT3DDEVICE9 pDevice;
	if( !m_Renderer )
	{
		return false;
	}
	pDevice = (LPDIRECT3DDEVICE9)m_Renderer->GetAPIDevice();
	if( !pDevice )
	{
		return false;
	}
	UINT numMips = mips;
	CDX9Renderer * crend = dynamic_cast<CDX9Renderer*>(m_Renderer);
	D3DFORMAT format = D3DFMT_A8R8G8B8;
	if( hformat )
	{
		format = EEDX9FormatFromString( hformat );
		if( format == D3DFMT_UNKNOWN )
		{
			format = D3DFMT_A8R8G8B8;
		}			
		m_Compressed = EEDX9IsCompressedFormat( format );
	}
	else
		format = EEDX9FormatFromColorBits( colordepth );

	//create new texture	
	LPDIRECT3DTEXTURE9 temptex;
	//TODO: more control over texture creation?
	if(FAILED(pDevice->CreateTexture( sizex, //width
									 sizey, //height
									numMips, //number of mips
									0,	//usage - 0 unless for render targets
									format,
									D3DPOOL_MANAGED, //TODO: managed vs. Unmanaged! unmanaged you can't lock!								
									&temptex,
									NULL)))
		{
			return false;
		}
		
	if( !temptex )
	{
		return false;
	}
	
	m_Texture = temptex;
	m_Height = sizey;
	m_Width = sizex;
	m_ColorDepth = colordepth;
	m_bRenderTarget = false;
	return true;
}
Example #15
0
// ---------- framework : setup dx ----------
bool rtvsD3dApp::setupDX (LPDIRECT3DDEVICE9 pd3dDevice)
{
  pd3dDevice->SetRenderState( D3DRS_LIGHTING , TRUE );
  pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_GAUSSIANQUAD );

  // ---- create a texture object ----
  //D3DXCreateTextureFromFile( pd3dDevice, "image/baboon.jpg", &pTexture );

  // get mode
  D3DDISPLAYMODE mode;
  returnvalue = pd3dDevice->GetDisplayMode(0, &mode);
  if (FAILED(returnvalue))
    return E_FAIL;

  // try create texture
  returnvalue = pd3dDevice->CreateTexture(WIDTH, HEIGHT, 1, 0,
		mode.Format, D3DPOOL_MANAGED, &pTexture, NULL);

	if (FAILED(returnvalue))
    return E_FAIL;

	
	// ---- QUAD ----
  void *pVertices = NULL;
  // ---- initialise quad vertex data ----
 	QuadVertex quadVertices[] =
	{
		//    x      y     z      nx     ny     nz    tu     tv
		{  1.28f,  0.93f, 2.0f,   0.0f,  1.0f,  0.0f,  0.0f,  0.0f }, //top right
		{ -1.28f,  0.93f, 2.0f,   0.0f,  1.0f,  0.0f,  1.0f,  0.0f }, //top left
		{  1.28f, -0.93f, 2.0f,   0.0f,  1.0f,  0.0f,  0.0f,  1.0f },
		{ -1.28f, -0.93f, 2.0f,   0.0f,  1.0f,  0.0f,  1.0f,  1.0f }
	};

	// ---- create quad vertex buffer ----
	int numQuadVertices = sizeof(quadVertices) / ( sizeof(float) * 8 /* +  sizeof(DWORD)*/);
	numQuadTriangles = numQuadVertices / 2;
	pd3dDevice->CreateVertexBuffer( numQuadVertices*sizeof(QuadVertex),
                                      D3DUSAGE_WRITEONLY,
                                      QuadVertex::FVF_Flags,
                                      D3DPOOL_MANAGED, // does not have to be properly Released before calling IDirect3DDevice9::Reset
                                      //D3DPOOL_DEFAULT,   // must be Released properly before calling IDirect3DDevice9::Reset
                                      &pQuadVertexBuffer, NULL );

	// ---- block copy into quad vertex buffer ----
	pVertices = NULL;
	pQuadVertexBuffer->Lock( 0, sizeof(quadVertices), (void**)&pVertices, 0 );
	memcpy( pVertices, quadVertices, sizeof(quadVertices) );
	pQuadVertexBuffer->Unlock();

  setupAntTW(pd3dDevice);

	// ok
	return true;
}
Example #16
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;
}
Example #17
0
// EXPORT FUNCTION:Init()
__declspec(dllexport) bool __stdcall OpenNIInit( HWND hWnd, bool EngFlag, LPDIRECT3DDEVICE9 lpDevice, WCHAR* f_path, CHAR* onifilename )
{
	TrackingF=false;
	for( int i = 0; i < 15; ++ i )
		TrCount[i] = 0;

	SetCurrentDirectoryW( f_path );

	if( nite::NiTE::initialize() == nite::STATUS_OK )
	{
		if( g_UserTracker.create() == nite::STATUS_OK )
		{
			nite::UserTrackerFrameRef mUserFrame;
			if( g_UserTracker.readFrame( &mUserFrame ) == nite::STATUS_OK )
			{
				openni::VideoFrameRef mDepthMap = mUserFrame.getDepthFrame();
				int x = mDepthMap.getWidth(),
					y = mDepthMap.getHeight();
				
				texWidth =  getClosestPowerOfTwo( x / 4 );
				texHeight = getClosestPowerOfTwo( y / 4 );
				
				if( FAILED( lpDevice->CreateTexture( texWidth, texHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &DepthTex, NULL ) ) )
				{
					MessageBox( hWnd, L"Cannot create depth texture", L"NiTE2", MB_OK );
					OpenNIClean();
					return false;
				}

				return true;
			}
			else
			{
				printError( hWnd, "UserTracker.readFrame" );
				MessageBox( hWnd, L"Cannot read user tracker frame", L"NiTE2", MB_OK );
				OpenNIClean();
				return false;
			}
		}
		else
		{
			printError( hWnd, "UserTracker.create" );
			MessageBox( hWnd, L"Cannot create user tracker", L"NiTE2", MB_OK );
			OpenNIClean();
			return false;
		}
	}
	else
	{
		printError( hWnd, "Init" );
		MessageBox( hWnd, L"Cannot initial NiTE", L"NiTE2", MB_OK );
		return false;
	}
}
Example #18
0
bool CGutFontUniCodeDX9::CreateTexture(int w, int h)
{
	LPDIRECT3DDEVICE9 pDevice = GutGetGraphicsDeviceDX9();

	m_iTextureW = w;
	m_iTextureH = h;

	pDevice->CreateTexture(w, h, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &m_pFontTexture, NULL);
	//pDevice->CreateTexture(w, h, 1, 0, D3DFMT_A8L8, D3DPOOL_MANAGED, &m_pFontTexture, NULL);

	return m_pFontTexture ? true : false;
}
Example #19
0
//--------------------------------------------------------------------------------------
void DepthTexture::createTexture(const LPDIRECT3DDEVICE9 device, int width, int height) {
	if(m_isSupported) {
		D3DFORMAT format = m_isINTZ ? FOURCC_INTZ : FOURCC_RAWZ;

		device->CreateTexture(width, height, 1,	D3DUSAGE_DEPTHSTENCIL, format, D3DPOOL_DEFAULT, &m_pTexture, NULL);

		if(m_pTexture) m_pTexture->GetSurfaceLevel(0, &m_pSurface);

		if(!m_isRESZ) {
			NvAPI_D3D9_RegisterResource(m_pTexture);
			NvAPI_D3D9_RegisterResource(m_pSurface);
		}
	}
}
Example #20
0
	bool restore_objects()
	{
		if ( retry_count )
		{
			if ( --retry_count ) return false;

			release_objects();
			if ( lpdev->Reset( &dpp ) != D3D_OK ) return false;
		}

		retry_count = 0;

		LPDIRECT3DSURFACE9 lpbb;
		HRESULT hr;
		if( SUCCEEDED( hr = lpdev->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, & lpbb ) ) ) {
			lpbb->GetDesc( & d3dsd );
			lpbb->Release();
		}

		lpdev->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
		lpdev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
		lpdev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

		lpdev->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);
		lpdev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
		lpdev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);

		lpdev->SetRenderState(D3DRS_LIGHTING, false);
		lpdev->SetRenderState(D3DRS_ZENABLE,  false);
		lpdev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

		lpdev->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA);
		lpdev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		lpdev->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

		lpdev->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);

		if ( lpdev->CreateVertexBuffer( sizeof(d3dvertex) * 4, flags.v_usage, D3DFVF_XYZRHW | D3DFVF_TEX1, (D3DPOOL)flags.v_pool, &lpvbuf, NULL ) != D3D_OK )
			return false;

		update_filtering( 1 );

		lpdev->SetRenderState( D3DRS_DITHERENABLE,   TRUE );

		if ( lpdev->CreateTexture( surface_width, surface_height, 1, flags.t_usage, D3DFMT_X8R8G8B8, (D3DPOOL)flags.t_pool, &lptex, NULL ) != D3D_OK )
			return false;

		return true;
	}
Example #21
0
bool InitDX(HWND hwnd)
{
	g_pD3D=Direct3DCreate9(D3D_SDK_VERSION);
	if(g_pD3D==NULL){
		OutputDebugString("failed -Direct3DCreate9()");
	}

	HRESULT hr;
	D3DDISPLAYMODE d3ddm;
	hr=g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
	if(FAILED(hr)){
		OutputDebugString("InitD3D failed -GetAdapterDisplayMode() failed");
		return false;
	}

	//プレゼンテーションパラメータの設定
	D3DPRESENT_PARAMETERS D3DPP;
	ZeroMemory(&D3DPP,sizeof(D3DPRESENT_PARAMETERS));
	D3DPP.BackBufferCount=1;
	if(ISFULLSCREENED){
		D3DPP.Windowed			 = false;
		D3DPP.BackBufferWidth	 = DEFAULT_CLIANT_WIDTH;
		D3DPP.BackBufferHeight	 = DEFAULT_CLIANT_HEIGHT;
	}
	else{
		D3DPP.Windowed=true;
	}
	D3DPP.BackBufferFormat		 = d3ddm.Format;
	D3DPP.SwapEffect			 = D3DSWAPEFFECT_DISCARD;
	D3DPP.EnableAutoDepthStencil = true;
	D3DPP.AutoDepthStencilFormat = D3DFMT_D16;
	D3DPP.Flags					 = true;
	D3DPP.PresentationInterval	 = D3DPRESENT_INTERVAL_ONE;
   
//デバイスオブジェクトの作成
	if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&D3DPP,&g_pD3DDev))){
		if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&D3DPP,&g_pD3DDev))){
			if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&D3DPP,&g_pD3DDev))){
				OutputDebugString("InitD3D failed -デバイスオブジェクトの作成に失敗 \n");
				return false;
			}
		}
	}

	g_pD3DDev->CreateTexture(SCREEN_WIDTH, SCREEN_HEIGHT, 1,D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&g_tex, NULL);

	return true;
}
Example #22
0
bool ReInitResourceDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	int w,h;
	GutGetWindowSize(w, h);

	float fAspect = (float)h/(float)w;
	g_proj_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, fAspect, g_fCameraNearZ, g_fCameraFarZ);

	g_ImageInfo.m_iWidth = g_texture_width;
	g_ImageInfo.m_iHeight = g_texture_height;
	g_ImageInfo.m_bProcedure = true;

	//D3DFORMAT fmt = D3DFMT_A16B16G16R16F;
	D3DFORMAT fmt = D3DFMT_A32B32G32R32F;

	for ( int i=0; i<TEX_TEXTURES; i++ )
	{
		device->CreateTexture(
			g_texture_width, g_texture_height, 
			1, D3DUSAGE_RENDERTARGET, 
			fmt, D3DPOOL_DEFAULT, 
			&g_pTextures[i], NULL);

		if ( NULL==g_pTextures[i] ) return false;

		g_pTextures[i]->GetSurfaceLevel(0, &g_pSurfaces[i]);

		if ( NULL==g_pSurfaces[i] ) return false;
	}

	device->GetRenderTarget(0, &g_pMainFramebuffer);
	device->GetDepthStencilSurface(&g_pMainDepthbuffer);

	device->SetRenderTarget(0, g_pSurfaces[0]);
	device->SetRenderTarget(1, g_pSurfaces[1]);
	
	device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 1.0f, 0);

	device->SetRenderTarget(0, g_pMainFramebuffer);
	device->SetRenderTarget(1, NULL);

	return true;
}
Example #23
0
int l_Image_Save( lua_State* L )
{
	ImageData* imgData = lua_checkimage( L, 1 );
	const char* fileName = lua_tostring( L, 2 );

	if( !imgData || !fileName )
	{
		lua_pushstring( L, "Wrong parameters to Image_Save functions" );
		lua_error( L );
		return 0;
	}

	LPDIRECT3DTEXTURE9 texture;
	D3DLOCKED_RECT rect;

	D3DFORMAT dxFormat = (D3DFORMAT)imgData->format->d3dformat;

	if( dxFormat == D3DFMT_DXT1 || dxFormat == D3DFMT_DXT3 || dxFormat == D3DFMT_DXT5 )
		dxFormat = D3DFMT_A8R8G8B8;

	g_pd3dDevice->CreateTexture( imgData->width, imgData->height, 1, 0, dxFormat, D3DPOOL_MANAGED, &texture, 0 );
	texture->LockRect(0, &rect, 0, 0 );
	memcpy( rect.pBits, imgData->imgData, imgData->width * imgData->height * imgData->format->bytesPerPixel );
	texture->UnlockRect( 0 );

	if( imgData->format->d3dformat == D3DFMT_DXT1 || imgData->format->d3dformat == D3DFMT_DXT3 || imgData->format->d3dformat == D3DFMT_DXT5 )
	{
		ID3DXBuffer* dxBuffer;
		LPDIRECT3DTEXTURE9 outTexture; 
		D3DXSaveTextureToFileInMemory( &dxBuffer, D3DXIFF_BMP, texture, 0 );
		D3DXCreateTextureFromFileInMemoryEx( g_pd3dDevice, dxBuffer->GetBufferPointer(), dxBuffer->GetBufferSize(), imgData->width, imgData->height, 0, 0, (D3DFORMAT)imgData->format->d3dformat, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, 0, &outTexture );

		D3DXSaveTextureToFile( fileName, extToFormat(fileName), outTexture, 0 );
		outTexture->Release();
	}else
	{
		D3DXSaveTextureToFile( fileName, extToFormat(fileName), texture, 0 );
	}

	texture->Release();

	return 0;
}
Example #24
0
VCNBool VCND3D9EffectCore::Initialize()
{
	if ( !VCNEffectCore::Initialize() )
		return false;

	LPDIRECT3DDEVICE9 device = VCNRenderCore::GetInstance()->Cast<VCND3D9>()->GetD3DDevice();

	const VCNPoint& screenRes = VCNRenderCore::GetInstance()->GetResolution();

	// Create the shadow map render target
	HRESULT hr = device->CreateTexture(screenRes.x, screenRes.y, 1, D3DUSAGE_RENDERTARGET, D3DFMT_G32R32F, D3DPOOL_DEFAULT, &mShadowMapTexture, 0);
	VCN_ASSERT_MSG( SUCCEEDED(hr), _T("Unable to create shadow map render target texture") );

	// Create a state block to capture all
	device->CreateStateBlock( D3DSBT_ALL, &mAllStateBlock );
	
	CreateScreenVertexBuffer(); 

	return true;
}
Example #25
0
static HRESULT CreateChecker(LPDIRECT3DDEVICE9 device, UINT xseg, UINT yseg, DWORD color1, DWORD color2, LPDIRECT3DTEXTURE9* texture)
{
	UINT width = xseg * 50;
	UINT height = xseg * 50;

	D3DLOCKED_RECT rect;
	HRESULT hr = device->CreateTexture(width, height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, texture, NULL);

	if( FAILED(hr) )
		return hr;

	(*texture)->LockRect(0, &rect, NULL, 0);

	UINT xstep = width / xseg;
	UINT ystep = height / yseg;
	UINT a, b;

	xseg = xstep * xseg;
	yseg = ystep * yseg;

	for( UINT i = 0; i < height; ++i )
	{
		for( UINT j = 0; j < width; ++j )
		{
			DWORD* ptr = ((DWORD*)rect.pBits + i * width + j);

			a = i / ystep;
			b = j / xstep;

			if( (a + b) % 2 )
				*ptr = color1;
			else
				*ptr = color2;
		}
	}

	(*texture)->UnlockRect(0);
	return S_OK;
}
Example #26
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	g_projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, 1.0f, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 改變三角形正面的面向
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	// 使用自動normalize功能
	device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

	device->CreateTexture(512, 512, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL);
	device->CreateDepthStencilSurface(512, 512, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &g_pDepthStencil, NULL);

	CGutModel::SetTexturePath("../../textures/");
	g_Model_DX9.ConvertToDX9Model(&g_Model);

	return true;
}
Example #27
0
bool CRenderTarget::CreateTexture(LPDIRECT3DDEVICE9 device, UINT width, UINT height,
                                  D3DFORMAT format, D3DFORMAT depthFormat, UINT levels, DWORD usage)
{
    assert(m_texture == NULL && m_surface == NULL && m_dsurface == NULL);

    m_width = width;
    m_height = height;
    m_format = format;
    m_dformat = depthFormat;
    m_device = device;

    usage |= D3DUSAGE_RENDERTARGET;
    //if (levels == 0)
    //    usage |= D3DUSAGE_AUTOGENMIPMAP;

    if (FAILED(device->CreateTexture(width, height, levels,
                                     usage, format,
                                     D3DPOOL_DEFAULT, &m_texture, NULL)))
    {
        MessageBox(NULL, "Create Texture Error", "ERROR", 0);
        return false;
    }

    if (FAILED(m_texture->GetSurfaceLevel(0, &m_surface)))
    {
        m_texture->Release();
        m_texture = NULL;
        MessageBox(NULL, "Get Surface Error", "ERROR", 0);
        return false;
    }

    CreateDepthSurface(depthFormat);
    InitViewPort();

    m_level = 0;

    return true;
}
static bool ImGui_ImplDX9_CreateFontsTexture()
{
    // Build texture atlas
    ImGuiIO& io = ImGui::GetIO();
    unsigned char* pixels;
    int width, height, bytes_per_pixel;
    io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bytes_per_pixel);

    // Upload texture to graphics system
    g_FontTexture = NULL;
    if (g_pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_FontTexture, NULL) < 0)
        return false;
    D3DLOCKED_RECT tex_locked_rect;
    if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)
        return false;
    for (int y = 0; y < height; y++)
        memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel));
    g_FontTexture->UnlockRect(0);

    // Store our identifier
    io.Fonts->TexID = (void *)g_FontTexture;

    return true;
}
Example #29
0
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int main(int argc, char **argv)
{
#if _WIN32
	char current_path[MAX_PATH + 1];
	GetDirectoryName(current_path, argv[0]);
	SetCurrentDirectoryA(current_path);
#endif

	InitWindow();

	// 描画用インスタンスの生成
	g_renderer = ::EffekseerRendererDX9::Renderer::Create( g_d3d_device, 2000 );
	
	// Specify a distortion function
	// 歪み機能を設定

	// If you'd like to distort background and particles by rendering, it need to specify it.
	// It is a bit heavy
	// もし、描画するごとに背景とパーティクルを歪ませたい場合、設定する必要がある
	// やや重い
	g_renderer->SetDistortingCallback(new DistortingCallback());

	// 背景バッファの生成
	g_d3d_device->CreateTexture(
		640,
		480,
		1,
		D3DUSAGE_RENDERTARGET,
		D3DFMT_A8R8G8B8,
		D3DPOOL_DEFAULT,
		&g_backgroundTexture,
		NULL
		);

	g_backgroundTexture->GetSurfaceLevel(0, &g_backgroundSurface);

	// エフェクト管理用インスタンスの生成
	g_manager = ::Effekseer::Manager::Create( 2000 );

	// 描画用インスタンスから描画機能を設定
	g_manager->SetSpriteRenderer( g_renderer->CreateSpriteRenderer() );
	g_manager->SetRibbonRenderer( g_renderer->CreateRibbonRenderer() );
	g_manager->SetRingRenderer( g_renderer->CreateRingRenderer() );
	g_manager->SetTrackRenderer(g_renderer->CreateTrackRenderer());
	g_manager->SetModelRenderer( g_renderer->CreateModelRenderer() );
	
	// 描画用インスタンスからテクスチャの読込機能を設定
	// 独自拡張可能、現在はファイルから読み込んでいる。
	g_manager->SetTextureLoader( g_renderer->CreateTextureLoader() );
	g_manager->SetModelLoader( g_renderer->CreateModelLoader() );

	// 音再生用インスタンスの生成
	g_sound = ::EffekseerSound::Sound::Create( g_xa2, 16, 16 );

	// 音再生用インスタンスから再生機能を指定
	g_manager->SetSoundPlayer( g_sound->CreateSoundPlayer() );
	
	// 音再生用インスタンスからサウンドデータの読込機能を設定
	// 独自拡張可能、現在はファイルから読み込んでいる。
	g_manager->SetSoundLoader( g_sound->CreateSoundLoader() );

	// 視点位置を確定
	g_position = ::Effekseer::Vector3D(10.0f, 5.0f, 20.0f) * 0.25;

	// 投影行列を設定
	g_renderer->SetProjectionMatrix(
		::Effekseer::Matrix44().PerspectiveFovRH(90.0f / 180.0f * 3.14f, (float)g_window_width / (float)g_window_height, 1.0f, 50.0f));

	// カメラ行列を設定
	g_renderer->SetCameraMatrix(
		::Effekseer::Matrix44().LookAtRH(g_position, ::Effekseer::Vector3D(0.0f, 0.0f, 0.0f), ::Effekseer::Vector3D(0.0f, 1.0f, 0.0f)));

	// エフェクトの読込
	g_effect = Effekseer::Effect::Create(g_manager, (const EFK_CHAR*)L"distortion.efk");

	// エフェクトの再生
	g_handle = g_manager->Play(g_effect, 0, 0, 0);
	
	MainLoop();

	// エフェクトの停止
	g_manager->StopEffect(g_handle);

	// エフェクトの破棄
	ES_SAFE_RELEASE(g_effect);

	// 先にエフェクト管理用インスタンスを破棄
	g_manager->Destroy();

	// 次に音再生用インスタンスを破棄
	g_sound->Destroy();

	// 次に描画用インスタンスを破棄
	g_renderer->Destroy();

	// XAudio2の解放
	if( g_xa2_master != NULL )
	{
		g_xa2_master->DestroyVoice();
		g_xa2_master = NULL;
	}
	ES_SAFE_RELEASE( g_xa2 );

	// バックバッファの破棄
	ES_SAFE_RELEASE( g_backgroundTexture );
	ES_SAFE_RELEASE( g_backgroundSurface );

	// DirectXの解放
	ES_SAFE_RELEASE( g_d3d_device );
	ES_SAFE_RELEASE( g_d3d );

	// COMの終了処理
	CoUninitialize();

	return 0;
}
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object, which is needed to create the D3DDevice.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
    {
        MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ;
        return E_FAIL;
    }

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );

    d3dpp.Windowed = TRUE; // use window mode, not full screen
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE ;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ;

    // Create device
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ;
        return E_FAIL;
    }

    g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE) ;

    // Create texture
    HRESULT hr = g_pd3dDevice->CreateTexture(
                     256,
                     256,
                     1,
                     D3DUSAGE_RENDERTARGET,
                     //d3dpp.BackBufferFormat,
                     D3DFMT_R5G6B5,
                     D3DPOOL_DEFAULT,
                     &g_pRenderTexture,
                     NULL) ;
    if (FAILED(hr))
    {
        MessageBox(NULL, "Create texture failed!", "Error", 0) ;
        return E_FAIL ;
    }

    // Get texture surface
    hr = g_pRenderTexture->GetSurfaceLevel(0, &g_pRenderSurface) ;
    if (FAILED(hr))
    {
        MessageBox(NULL, "Get surface on texture failed!", "Error", 0) ;
        return E_FAIL ;
    }

    // Create teapot
    D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ;

    // Create Cube
    CreateCube() ;

    // Create camera
    g_ModelViewCamera = new Camera() ;

    // Initialize view matrix
    D3DXVECTOR3 eyePt(0.0f, 0.0f, -5.0f) ;
    D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ;
    D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ;
    g_ModelViewCamera->SetViewParams(&eyePt, &lookCenter, &upVec) ;

    g_ModelViewCamera->SetProjParams(D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ;

    return S_OK;
}