⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grass.cpp

📁 windows游戏编程中关于“草”的源码及源程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        else
        {
            m_pSmallFont->DrawText(  2, 40, D3DCOLOR_ARGB(255,255,255,255), 
                               _T("Press F1 for help") );
        }


        // End the scene.
        m_pd3dDevice->EndScene();
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
	
	HRESULT hr;

	hr = D3DXCreateTextureFromFile(m_pd3dDevice, "media\\thingrass0000.tga", &m_pTex);
	hr = D3DXCreateTextureFromFile(m_pd3dDevice, "media\\grass_ground.tga", &m_pGroundTex);

    m_pStatsFont->InitDeviceObjects( m_pd3dDevice );
    m_pSmallFont->InitDeviceObjects( m_pd3dDevice );




    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
    m_pStatsFont->RestoreDeviceObjects();
    m_pSmallFont->RestoreDeviceObjects();

	
 	m_pd3dDevice->SetTexture(0, m_pTex);
 
 	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );

	m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
    m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
 

    m_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, (DWORD)0x000000FF);
    m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

    // Set the transform matrices
 	D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, -20.0f, -10.0f );
    D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f,   0.0f,  -5.0f );
    D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f,   0.0f,  -1.0f );
    

    D3DXMatrixIdentity( &m_matWorld );
    D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec );
    FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
    D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/2, fAspect, 1.0f, 1000.0f );

    m_pd3dDevice->SetTransform( D3DTS_WORLD,      &m_matWorld );
    m_pd3dDevice->SetTransform( D3DTS_VIEW,       &m_matView );
    m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProj );

		
	D3DXMATRIX mat;
	D3DXMatrixIdentity( &m_matInitial);

	// Create grass quads VB
    HRESULT  hr;
	hr = m_pd3dDevice->CreateVertexBuffer( m_iNumQuads*6*sizeof(VERTEX),
                                           D3DUSAGE_WRITEONLY, D3DFVF_VERTEX,
                                           D3DPOOL_MANAGED, &m_pQuadsVB );
    if( FAILED(hr) )
        return hr;

	VERTEX* pVertices = NULL;
	hr = m_pQuadsVB->Lock( 0,  m_iNumQuads*6*sizeof(VERTEX), (BYTE**)&pVertices, 0 );
    if( FAILED(hr) )
        return hr;

     for( DWORD j=0; j< m_iNumQuads; j++ )
	{
		// Generate each quad at random position, orientation, height
		D3DXMATRIX matRandom;
		D3DXMatrixIdentity( &matRandom);
		float scale = 40.0f;
		float angle = ((float)rand()/RAND_MAX - 0.5f) * 8.0f;
		float dx = ((float)rand()/RAND_MAX - 0.5f) * scale*2;
		float dy = ((float)rand()/RAND_MAX - 0.5f) * scale;
		float heightScale = ((float)rand()/RAND_MAX - 0.5f) / 2.0f + 1.0f;

		D3DXMatrixRotationZ( &mat, D3DX_PI * angle);
		D3DXMatrixMultiply( &matRandom, &matRandom, &mat );
		D3DXMatrixTranslation( &mat, dx, dy, 0.0f);			
    	D3DXMatrixMultiply( &matRandom, &matRandom, &mat );
		D3DXMatrixScaling( &mat, 1.0f, 1.0f, heightScale);			
    	D3DXMatrixMultiply( &matRandom, &matRandom, &mat );

		// Apply the transformation to each vertex of the quad
		for( DWORD i=0; i< 6; i++ )
		{
			D3DXVECTOR4 pos, outPos;
			pos.x = g_Vertices[i].x;
			pos.y = g_Vertices[i].y;
			pos.z = g_Vertices[i].z;
			D3DXVec3Transform(&outPos, &(const struct D3DXVECTOR3)pos, &(CONST D3DXMATRIX)matRandom);
			DWORD index = j * 6 + i;
			pVertices[index].x = outPos.x;
			pVertices[index].y = outPos.y;
			pVertices[index].z = outPos.z;
			pVertices[index].tu1 = g_Vertices[i].tu1;
			pVertices[index].tv1 = g_Vertices[i].tv1;
		}
	}

		
    m_pQuadsVB->Unlock();


	// Create ground quad VB
	hr = m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX),
                                           D3DUSAGE_WRITEONLY, D3DFVF_VERTEX,
                                           D3DPOOL_MANAGED, &m_pGroundQuadVB );
    if( FAILED(hr) )
        return hr;

	pVertices = NULL;
	hr = m_pGroundQuadVB->Lock( 0, 4*sizeof(VERTEX), (BYTE**)&pVertices, 0 );
    if( FAILED(hr) )
        return hr;

    for( DWORD i=0; i<4; i++ )
        pVertices[i] = g_GroundVertices[i];

    m_pGroundQuadVB->Unlock();


	// Create vertex shader
    {
        LPD3DXBUFFER pCode;

        // Assemble the vertex shader from the file
        if( FAILED( hr = D3DXAssembleShaderFromFile( "shaders\\grass.vsh", 
                                                     0, NULL, &pCode, NULL ) ) )
            return hr;

        // Create the vertex shader
        hr = m_pd3dDevice->CreateVertexShader( dwVertexDecl, (DWORD*)pCode->GetBufferPointer(),
                                               &m_dwVShader, 0 );
        pCode->Release();
        if( FAILED(hr) )
            return hr;
    }

	// Create pixel shader
    {
        LPD3DXBUFFER pCode;

        // Assemble the pixel shader from the file
         if( FAILED( hr = D3DXAssembleShaderFromFile( "shaders\\grass.psh", 
                                                     0, NULL, &pCode, NULL ) ) )
            return hr;

        // Create the pixel shader
        hr = m_pd3dDevice->CreatePixelShader( (DWORD*)pCode->GetBufferPointer(),
                                              &m_dwPShader);
        pCode->Release();
        if( FAILED(hr) )
            return hr;
    }

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
 	SAFE_RELEASE( m_pQuadsVB );
	SAFE_RELEASE( m_pGroundQuadVB );

	m_pd3dDevice->DeleteVertexShader( m_dwVShader );
	m_pd3dDevice->DeletePixelShader( m_dwPShader );
	m_pStatsFont->InvalidateDeviceObjects();
	m_pSmallFont->InvalidateDeviceObjects();

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
	SAFE_RELEASE( m_pQuadsVB );
	SAFE_RELEASE( m_pGroundQuadVB );

	m_pd3dDevice->SetTexture(0, NULL);
	SAFE_RELEASE( m_pTex );
	SAFE_RELEASE( m_pGroundTex );

    m_pStatsFont->DeleteDeviceObjects();
    m_pSmallFont->DeleteDeviceObjects();
 

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
//       to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
    SAFE_DELETE( m_pStatsFont );
	SAFE_DELETE( m_pSmallFont );


    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                                    LPARAM lParam )
{
	// Trap context menu
    if( WM_CONTEXTMENU == uMsg )
        return 0;


    // Perform commands when keys are released
    if( WM_KEYUP == uMsg )
    {
        switch( wParam )
        {
        case 'V': 
			 m_bVertexShader = !m_bVertexShader;
            break;
         case 'P': 
			 m_bPixelShader = !m_bPixelShader;
            break;
       case VK_F1:
            m_bShowHelp = !m_bShowHelp;
            break;
        }
    }

    return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -