예제 #1
0
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
	// Init the font
    m_pFont->InitDeviceObjects( m_pd3dDevice );

    // Load texture map. Note that this is a special textures, which has a
    // height field stored in the alpha channel
    if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("emboss1.tga"), 
                                       &m_pEmbossTexture, D3DFMT_A8R8G8B8 ) ) )
        return D3DAPPERR_MEDIANOTFOUND;

    // Load geometry
    if( FAILED( m_pObject->Create( m_pd3dDevice, _T("tiger.x") ) ) )
        return D3DAPPERR_MEDIANOTFOUND;

    // Set attributes for the geometry
    m_pObject->SetFVF( m_pd3dDevice, D3DFVF_EMBOSSVERTEX );
    m_pObject->UseMeshMaterials( FALSE );

    // Compute the object's tangents and binormals, whaich are needed for the 
    // emboss-tecnhique's texture-coordinate shifting calculations
    ComputeTangentsAndBinormals();

    return S_OK;
}
예제 #2
0
LPDIRECT3DTEXTURE9 CSpeedTreeWrapper::LoadTexture(const char* pFilename)
{
    LPDIRECT3DTEXTURE9 pTexture = NULL;

	if (D3DUtil_CreateTexture(m_pDx, const_cast<char*>(pFilename), &pTexture) != S_OK)
		fprintf(stderr, "Warning: failed to load texture [%s]\n", pFilename);

    return pTexture;
}
예제 #3
0
//-------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: 디바이스가 생성된후의 초기화
//       프레임버퍼 포맷과 디바이스 종류가 변한뒤에 호출
//       여기서 확보한 메모리는 DeleteDeviceObjects()에서 해제
//-------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    HRESULT hr;
    LPDIRECT3DTEXTURE9	pHeightTexture;
    D3DSURFACE_DESC desc;

	// 법선맵 생성
    D3DUtil_CreateTexture( m_pd3dDevice,// 높이맵 읽기
		_T("height.bmp"), &pHeightTexture );
    pHeightTexture->GetLevelDesc(0,&desc);// 텍스처 정보 얻기
    D3DXCreateTexture(m_pd3dDevice, desc.Width, desc.Height, 0, 0, 
        D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &m_pNormalMap);// 텍스처 생성
    D3DXComputeNormalMap(m_pNormalMap,	// 법선맵 생성
        pHeightTexture, NULL, 0, D3DX_CHANNEL_RED, 1.0f);
    SAFE_RELEASE( pHeightTexture );		// 필요없어진 리소스 해제

	// 정점선언 오브젝트 생성
	if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration(
										decl, &m_pDecl )))
		return DXTRACE_ERR ("CreateVertexDeclaration", hr);
	
	// 주전자 읽기
	if(FAILED(hr=m_pMesh  ->Create( m_pd3dDevice, _T("t-pot.x"))))
        return DXTRACE_ERR( "Load Object", hr );
        
	// 지형 읽기
	if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
        return DXTRACE_ERR( "Load Ground", hr );
        
	// 셰이더 읽기
    LPD3DXBUFFER pErr=NULL;
    if( FAILED( hr = D3DXCreateEffectFromFile(
                m_pd3dDevice, "hlsl.fx", NULL, NULL, 
                0 , NULL, &m_pEffect, &pErr ))){
        // 셰이더 읽기 실패
        MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
                    , "ERROR", MB_OK);
    }else{
		m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
		m_hmWVP      = m_pEffect->GetParameterByName( NULL, "mWVP" );
		m_hvLightDir = m_pEffect->GetParameterByName( NULL, "vLightDir" );
		m_hvColor    = m_pEffect->GetParameterByName( NULL, "vColor" );
		m_hvEyePos   = m_pEffect->GetParameterByName( NULL, "vEyePos" );
		m_htDecaleTex= m_pEffect->GetParameterByName( NULL, "DecaleTex" );
		m_htNormalMap= m_pEffect->GetParameterByName( NULL, "NormalMap" );
    }
    SAFE_RELEASE(pErr);

	// 폰트
    m_pFont->InitDeviceObjects( m_pd3dDevice );

    return S_OK;
}
예제 #4
0
bool CSpeedTreeForestDirectX9::InitGraphics(void)
{
	bool bSuccess = false;

	// load the composite map
	if (D3DUtil_CreateTexture(m_pDx, c_pCompositeMapFilename, &m_texCompositeMap) != S_OK)
		MessageBox(NULL, "Failed to create leaf texture.", "File Error", MB_ICONEXCLAMATION);
	else
	{
		// init the vertex shaders, if needed
		bSuccess = InitVertexShaders( );
	}

	return bSuccess;
}
예제 #5
0
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    m_pFont->InitDeviceObjects( m_pd3dDevice );

    // Load the texture for the background image
    if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"),
                                       &m_pBackgroundTexture ) ) )
        return E_FAIL;

    // Create the bump map texture
    if( FAILED( CreateBumpMap( 256, 256 ) ) )
       return E_FAIL;

    // Create a square for rendering the background
    if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(BACKGROUNDVERTEX),
                                                  D3DUSAGE_WRITEONLY, D3DFVF_BACKGROUNDVERTEX,
                                                  D3DPOOL_MANAGED, &m_pBackgroundVB ) ) )
        return E_FAIL;

    // Create a square for rendering the lens
    if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(BUMPVERTEX),
                                                  D3DUSAGE_WRITEONLY, D3DFVF_BUMPVERTEX,
                                                  D3DPOOL_MANAGED, &m_pLensVB ) ) )
        return E_FAIL;

    BUMPVERTEX* vLens;
    m_pLensVB->Lock( 0, 0, (BYTE**)&vLens, 0 );
    vLens[0].p = D3DXVECTOR3(-256.0f,-256.0f, 0.0f );
    vLens[1].p = D3DXVECTOR3(-256.0f, 256.0f, 0.0f );
    vLens[2].p = D3DXVECTOR3( 256.0f,-256.0f, 0.0f );
    vLens[3].p = D3DXVECTOR3( 256.0f, 256.0f, 0.0f );
    vLens[0].tu1 = 0.0f; vLens[0].tv1 = 1.0f;
    vLens[1].tu1 = 0.0f; vLens[1].tv1 = 0.0f;
    vLens[2].tu1 = 1.0f; vLens[2].tv1 = 1.0f;
    vLens[3].tu1 = 1.0f; vLens[3].tv1 = 0.0f;
    m_pLensVB->Unlock();

    m_bDeviceValidationFailed = FALSE;

    return S_OK;
}
예제 #6
0
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    // Load the file objects
    if( FAILED( m_pShinyTeapot->Create( m_pd3dDevice, _T("teapot.x") ) ) )
        return D3DAPPERR_MEDIANOTFOUND;
    if( FAILED( m_pSkyBox->Create( m_pd3dDevice, _T("lobby_skybox.x") ) ) )
        return D3DAPPERR_MEDIANOTFOUND;
    if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("spheremap.bmp"), &m_pSphereMap ) ) )
        return D3DAPPERR_MEDIANOTFOUND;

    // Set mesh properties
    m_pShinyTeapot->SetFVF( m_pd3dDevice, D3DFVF_ENVMAPVERTEX );

    // Restore the device-dependent objects
    m_pFont->InitDeviceObjects( m_pd3dDevice );

    // Create Effect object
    if( FAILED( D3DXCreateEffect( m_pd3dDevice, g_szEffect, g_cchEffect, &m_pEffect, NULL ) ) )
        return E_FAIL;

    return S_OK;
}
예제 #7
0
//-----------------------------------------------------------------------------
// Name: SetTextureStageStatesForRendering()
// Desc: Sets up the texture stage, as per the global variables defining each
//       stage.
//-----------------------------------------------------------------------------
HRESULT SetTextureStageStatesForRendering( LPDIRECT3DDEVICE9 pd3dDevice )
{
    // If new textures were selected, restore them now
    if( g_bTexturesChanged )
    {
        SAFE_RELEASE( g_pFloorTexture );
        SAFE_RELEASE( g_pTexture0 );
        SAFE_RELEASE( g_pTexture1 );
        SAFE_RELEASE( g_pTexture2 );
		SAFE_RELEASE( g_pTexture3 );
        SAFE_RELEASE( g_pTexture4 );
        if( FAILED( D3DUtil_CreateTexture( pd3dDevice, _T("floor.bmp"), &g_pFloorTexture ) ) )
            g_pFloorTexture = NULL;
        if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture0, &g_pTexture0 ) ) )
            g_pTexture0 = NULL;
        if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture1, &g_pTexture1 ) ) )
            g_pTexture1 = NULL;
        if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture2, &g_pTexture2 ) ) )
            g_pTexture2 = NULL;
		if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture3, &g_pTexture3 ) ) )
            g_pTexture3 = NULL;
		if( FAILED( D3DUtil_CreateTexture( pd3dDevice, g_strTexture4, &g_pTexture4 ) ) )
            g_pTexture4 = NULL;
        g_bTexturesChanged = FALSE;
    }

    pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, g_dwTextureFactor );

    pd3dDevice->SetTexture( 0, g_pTexture0 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
    pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, g_wT0CArg1 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   g_wT0COp   );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, g_wT0CArg2 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG0, g_wT0CArg0 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, g_wT0AArg1 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   g_wT0AOp   );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, g_wT0AArg2 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG0, g_wT0AArg0 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_RESULTARG, g_wT0RArg  );

    if( g_dwMaxTextureBlendStages > 1 )
    {
        pd3dDevice->SetTexture( 1, g_pTexture1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
        pd3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, g_wT1CArg1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   g_wT1COp   );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, g_wT1CArg2 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG0, g_wT1CArg0 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, g_wT1AArg1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   g_wT1AOp   );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, g_wT1AArg2 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG0, g_wT1AArg0 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_RESULTARG, g_wT1RArg  );
    }

    if( g_dwMaxTextureBlendStages > 2)
    {
        pd3dDevice->SetTexture( 2, g_pTexture2 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );
        pd3dDevice->SetSamplerState( 2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, g_wT2CArg1 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP,   g_wT2COp   );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, g_wT2CArg2 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG0, g_wT2CArg0 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG1, g_wT2AArg1 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,   g_wT2AOp   );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG2, g_wT2AArg2 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG2, g_wT2AArg0 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_RESULTARG, g_wT2RArg  );
    }

	if( g_dwMaxTextureBlendStages > 3)
    {
        pd3dDevice->SetTexture( 3, g_pTexture3 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_TEXCOORDINDEX, 0 );
        pd3dDevice->SetSamplerState( 3, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_COLORARG1, g_wT3CArg1 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_COLOROP,   g_wT3COp   );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_COLORARG2, g_wT3CArg2 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_COLORARG0, g_wT3CArg0 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_ALPHAARG1, g_wT3AArg1 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_ALPHAOP,   g_wT3AOp   );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_ALPHAARG2, g_wT3AArg2 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_ALPHAARG2, g_wT3AArg0 );
        pd3dDevice->SetTextureStageState( 3, D3DTSS_RESULTARG, g_wT3RArg  );
    }

	if( g_dwMaxTextureBlendStages > 4)
    {
        pd3dDevice->SetTexture( 4, g_pTexture4 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_TEXCOORDINDEX, 0 );
        pd3dDevice->SetSamplerState( 4, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_COLORARG1, g_wT4CArg1 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_COLOROP,   g_wT4COp   );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_COLORARG2, g_wT4CArg2 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_COLORARG0, g_wT4CArg0 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_ALPHAARG1, g_wT4AArg1 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_ALPHAOP,   g_wT4AOp   );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_ALPHAARG2, g_wT4AArg2 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_ALPHAARG2, g_wT4AArg0 );
        pd3dDevice->SetTextureStageState( 4, D3DTSS_RESULTARG, g_wT4RArg  );
    }

    return S_OK;
}
예제 #8
0
/********************Public*Routine****************************************\
* CPlane
* 
\**************************************************************************/
CHall::CPlane::CPlane(
                  IDirect3DDevice9* pDevice,
                  TCHAR *achName, 
                  LPCTSTR lpResourceName,
                  float fU, 
                  float fV, 
                  D3DXMATRIX& M,
                  float a,
                  HRESULT *phr)
    : m_du( 0.f)
    , m_fU( fU )
    , m_fV( fV )
    , m_fSpeed( 0.f )
    , m_pTexture( NULL )
{
    HRESULT hr = S_OK;

    TCHAR achTexturePath[MAX_PATH];

    D3DXVECTOR3 vLB( M(0,0), M(1,0), M(2,0));
    D3DXVECTOR3 vLT( M(0,1), M(1,1), M(2,1));
    D3DXVECTOR3 vRT( M(0,2), M(1,2), M(2,2));
    D3DXVECTOR3 vRB( M(0,3), M(1,3), M(2,3));
    D3DXVECTOR3 V;

    try
    {
        if( !achName )
            throw E_POINTER;

        hr = FindMediaFile( achTexturePath,
            sizeof(TCHAR)*MAX_PATH,
            achName,
            lpResourceName,
            RT_BITMAP);
        if( FAILED(hr))
        {
            TCHAR achMsg[MAX_PATH];
            _stprintf( achMsg, TEXT("Cannot find media file '%s'. ")
                       TEXT("Make sure you have valid installation of DirectX SDK"), 
                       achName);
            ::MessageBox( NULL, achMsg, TEXT("Error"), MB_OK);
            if( phr )
            {
                *phr = hr;
                return;
            }
        }

        if( !pDevice )
            throw E_POINTER;

        if( a<0.01f || a>0.5f )
            throw E_INVALIDARG;

        D3DXVECTOR3 vecDiff = vLB - vLT;
        if( D3DXVec3LengthSq( &vecDiff )< 0.001f )
            throw E_INVALIDARG;

        if( m_fU < 0.001f || m_fV < 0.001f )
            throw E_INVALIDARG;

        CHECK_HR(
            hr = DXUtil_FindMediaFileCb( achTexturePath, sizeof(TCHAR)*MAX_PATH, achName ),
            DbgMsg("CPlane::CPlane: cannot find bitmap file %s in SDK media folder", achTexturePath));

        // load texture
        ASSERT( g_pEnvFormat );
        CHECK_HR(
            hr = D3DUtil_CreateTexture( pDevice, achTexturePath, &m_pTexture, *g_pEnvFormat ),
            DbgMsg("CPlane::CPlane: failed to create the texture, hr = 0x%08x", hr));

        // initialize geometry

        // POSITION

        // corners
        
        memcpy( &(m_V[0].Pos), &vLB, sizeof(D3DVECTOR));
        memcpy( &(m_V[1].Pos), &vLT, sizeof(D3DVECTOR));
        memcpy( &(m_V[2].Pos), &vRB, sizeof(D3DVECTOR));
        memcpy( &(m_V[3].Pos), &vRT, sizeof(D3DVECTOR));

        // TEXTURE COORD
        m_V[0].tu = 0.f;    m_V[0].tv = fV;
        m_V[1].tu = 0.f;    m_V[1].tv = 0.f;
        m_V[2].tu =  fU;    m_V[2].tv = fV;
        m_V[3].tu =  fU;    m_V[3].tv = 0.f;

        // corners are transparent, middle vertices are opaque
        m_V[0].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF);
        m_V[1].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF);
        m_V[2].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF);
        m_V[3].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF);

    }
    catch( HRESULT hr1 )
    {
        RELEASE( m_pTexture );
        hr = hr1;
    }

    if( phr )
    {
        *phr = hr;
    }
}
예제 #9
0
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: This creates all device-dependent managed objects, such as managed
//       textures and managed vertex buffers.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    // Initialize the font's internal textures
    m_pFont->InitDeviceObjects( m_pd3dDevice );

    // Create the tree textures
    for( DWORD i=0; i<NUMTREETEXTURES; i++ )
    {
        if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, g_strTreeTextures[i],
                                           &m_pTreeTextures[i] ) ) )
            return D3DAPPERR_MEDIANOTFOUND;
    }

    // Create a quad for rendering each tree
    if( FAILED( m_pd3dDevice->CreateVertexBuffer( NUM_TREES*4*sizeof(TREEVERTEX),
                                                  D3DUSAGE_WRITEONLY, D3DFVF_TREEVERTEX,
                                                  D3DPOOL_MANAGED, &m_pTreeVB ) ) )
    {
        return E_FAIL;
    }

    // Copy tree mesh data into vertexbuffer
    TREEVERTEX* v;
    m_pTreeVB->Lock( 0, 0, (BYTE**)&v, 0 );

    INT iTree;
    DWORD dwOffset = 0;
    for( iTree = 0; iTree < NUM_TREES; iTree++ )
    {
        memcpy( &v[dwOffset], m_Trees[iTree].v, 4*sizeof(TREEVERTEX) );
        m_Trees[iTree].dwOffset = dwOffset;
        dwOffset += 4;
    }

    m_pTreeVB->Unlock();

    // Load the skybox
    if( FAILED( m_pSkyBox->Create( m_pd3dDevice, _T("SkyBox2.x") ) ) )
        return D3DAPPERR_MEDIANOTFOUND;

    // Load the terrain
    if( FAILED( m_pTerrain->Create( m_pd3dDevice, _T("SeaFloor.x") ) ) )
        return D3DAPPERR_MEDIANOTFOUND;

    // Add some "hilliness" to the terrain
    LPDIRECT3DVERTEXBUFFER8 pVB;
    if( SUCCEEDED( m_pTerrain->GetSysMemMesh()->GetVertexBuffer( &pVB ) ) )
    {
        struct VERTEX { FLOAT x,y,z,tu,tv; };
        VERTEX* pVertices;
        DWORD   dwNumVertices = m_pTerrain->GetSysMemMesh()->GetNumVertices();

        pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 );

        for( DWORD i=0; i<dwNumVertices; i++ )
            pVertices[i].y = HeightField( pVertices[i].x, pVertices[i].z );

        pVB->Unlock();
        pVB->Release();
    }

    return S_OK;
}