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

📄 copy of 1steps.cpp

📁 Beginning Direct3D Game Programming源代码Part1chapter6
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
    D3DXMatrixRotationY( &matRotateY, -m_pObjects[0].vR.x );
	D3DXMatrixRotationX( &matRotateX, -m_pObjects[0].vR.y );
	D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[0].vR.z );

	D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
	D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
	D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld ); 

    m_pObjects[0].matLocal = matWorld;

	//**********************************************************
	// red object
	//**********************************************************
    if (m_bKey['D'])	m_pObjects[1].vR.z -= m_fTimeElapsed;
    if (m_bKey['A'])	m_pObjects[1].vR.z += m_fTimeElapsed;
	if (m_bKey['S'])	m_pObjects[1].vR.y -= m_fTimeElapsed;
    if (m_bKey['W'])	m_pObjects[1].vR.y += m_fTimeElapsed;

	D3DXMatrixTranslation(&matWorld, m_pObjects[1].vLoc.x, m_pObjects[0].vLoc.y, m_pObjects[0].vLoc.z);
    
	D3DXMatrixRotationY( &matRotateY, -m_pObjects[1].vR.x );
	D3DXMatrixRotationX( &matRotateX, -m_pObjects[1].vR.y );
	D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[1].vR.z );
	D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
	D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
	D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld ); 

    m_pObjects[1].matLocal = matWorld;

	//************************************************************
	// camera stuff
	//************************************************************
    D3DXVECTOR3 vTrans(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 vRot(0.0f, 0.0f, 0.0f);

	// Process keyboard input
    if (m_bKey[VK_HOME])		vTrans.z -= 0.2f; // Move Forward
    if (m_bKey[VK_END])			vTrans.z += 0.2f; // Move Backward
    if (m_bKey[VK_NUMPAD4])		vTrans.x -= 0.1f; // Slide Left
    if (m_bKey[VK_NUMPAD6])		vTrans.x += 0.1f; // Slide Right
    if (m_bKey[VK_NUMPAD8])     vTrans.y -= 0.1f; // Slide Down
    if (m_bKey[VK_NUMPAD2])     vTrans.y += 0.1f; // Slide Up

	if (m_bKey[VK_UP])			vRot.y -= 0.1f; // Pitch Up
    if (m_bKey[VK_DOWN])		vRot.y += 0.1f; // Pitch Down
    if (m_bKey[VK_LEFT])	    vRot.x -= 0.1f; // Turn Left
    if (m_bKey[VK_RIGHT])	    vRot.x += 0.1f; // Turn Right
    if (m_bKey['C'])		    vRot.z -= 0.1f; // Rotate Left
    if (m_bKey['X'])		    vRot.z += 0.1f; // Rotate Right
	
	// turn cfSmooth to 0.98f
	const FLOAT cfSmooth = 0.9f;

	// transform and rotation velocity
    m_pvVelocity = m_pvVelocity * cfSmooth + vTrans;
    m_pvAngularVelocity = m_pvAngularVelocity * cfSmooth + vRot;

	// transform and rotation value
    vTrans = m_pvVelocity * m_fTimeElapsed * m_fSpeed;
    vRot = m_pvAngularVelocity* m_fTimeElapsed * m_fAngularSpeed;

    // Update position and view matricies
    D3DXMATRIX matT, matR;
    D3DXQUATERNION qR;

	D3DXMatrixTranslation(&matT, vTrans.x, vTrans.y, vTrans.z);
	D3DXMatrixMultiply(&m_matPosition, &matT, &m_matPosition);

    QuaternionFromAngles(qR.x,qR.y,qR.z,qR.w,vRot.z, vRot.x, vRot.y);
//	D3DXMatrixRotationQuaternion (&matR, &qR);
	D3DMath_MatrixFromQuaternion(matR, qR.x,qR.y,qR.z,qR.w);
	D3DXMatrixMultiply(&m_matPosition, &matR, &m_matPosition);
	D3DXMatrixInverse(&m_matView, NULL, &m_matPosition);

/*	
	FLOAT fSpeed        = 3.0f * m_fTimeElapsed;
    FLOAT fAngularSpeed = 0.01f;

    // De-accelerate the camera movement (for smooth motion)
    m_vVelocity      *= 0.9f;
    m_fYawVelocity   *= 0.9f;
    m_fPitchVelocity *= 0.9f;
    m_fRollVelocity  *= 0.9f;

    // Process keyboard input
    if(m_bKey[VK_NUMPAD6]) m_vVelocity.x    += fSpeed; // Slide Right
    if(m_bKey[VK_NUMPAD4]) m_vVelocity.x    -= fSpeed; // Slide Left
    if(m_bKey[VK_NUMPAD8]) m_vVelocity.y    += fSpeed; // Slide Up
    if(m_bKey[VK_NUMPAD2]) m_vVelocity.y    -= fSpeed; // Slide Down
    if(m_bKey[VK_HOME])    m_vVelocity.z    += fSpeed; // Move Forward
    if(m_bKey[VK_END])     m_vVelocity.z    -= fSpeed; // Move Backward
    if(m_bKey[VK_RIGHT])   m_fYawVelocity   -= fSpeed; // Turn Right
    if(m_bKey[VK_LEFT])    m_fYawVelocity   += fSpeed; // Turn Left
    if(m_bKey[VK_DOWN])    m_fPitchVelocity -= fSpeed; // Turn Down
    if(m_bKey[VK_UP])      m_fPitchVelocity += fSpeed; // Turn Up
	if(m_bKey['C'])		   m_fRollVelocity  += fSpeed; // Rotate
    if(m_bKey['X'])        m_fRollVelocity  -= fSpeed; // Rotate

    // Update the position vector
    D3DXVECTOR3 vT = m_vVelocity * fSpeed;
    D3DXVec3TransformNormal( &vT, &vT, &m_matOrientation );
    m_vPosition += vT;

    // Update the yaw-pitch-rotation floats
    m_fYaw   += fAngularSpeed * m_fYawVelocity;
    m_fPitch += fAngularSpeed * m_fPitchVelocity;
    m_fRoll += fAngularSpeed * m_fRollVelocity;


    // Set the view matrix
    D3DXMATRIX matR;
    D3DXQUATERNION qR;
    QuaternionFromAngles(qR.x,qR.y,qR.z,qR.w,m_fRoll, m_fYaw, m_fPitch);
//	nD3DXMatrixRotationQuaternio(&matR, &qR);
//	D3DXMatrixMultiply(&m_matOrientation, &matR, &m_matOrientation);
    D3DXMatrixAffineTransformation( &m_matOrientation, 1.25f, NULL, &qR, &m_vPosition );
    D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );
  */  
	m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );

   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
//       rendering. This function sets up render states, clears the
//       viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
    D3DMATERIAL8 mtrl;

    // Clear the viewport | z-buffer
    m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0x00000000, 1.0f, 0L );

    // Begin the scene
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {

		// yellow object
		// Set the color for the object
		D3DUtil_InitMaterial( mtrl, m_pObjects[0].fR, m_pObjects[0].fG, m_pObjects[0].fB );
		m_pd3dDevice->SetMaterial( &mtrl );

        // Apply the object's local matrix
        m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[0].matLocal );

		m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
		m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
        m_pd3dDevice->SetIndices( m_pIB, 0 );
		m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 
											0, 
											16,  // number of vertices
                                            0, 
											10); // number of primitives

		// red object
		// Set the color for the object
		D3DUtil_InitMaterial( mtrl, m_pObjects[1].fR, m_pObjects[1].fG, m_pObjects[1].fB );
		m_pd3dDevice->SetMaterial( &mtrl );

        // Apply the object's local matrix
        m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[1].matLocal );

		m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
		m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
        m_pd3dDevice->SetIndices( m_pIB, 0 );
		m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 
											0, 
											16,  // number of vertices
                                            0, 
											10); // number of primitives


        // Output statistics
        m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
        m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );

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

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    m_pFont->InitDeviceObjects( m_pd3dDevice );
   return S_OK;
}

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

    D3DMATERIAL8 mtrl;
    D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
    m_pd3dDevice->SetMaterial( &mtrl );

    // Set the transform matrices
    D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, 2.0f );
    D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
    D3DXMATRIX  /*matWorld, matView,*/ matProj;

//    D3DXMatrixTranslation(&m_matView, 0.0f, 0.0f, -20.0f);
//    D3DXMatrixTranslation(&m_matPosition, 0.0f, 0.0f, -5.0f);
    
//    D3DXMatrixIdentity( &matWorld );
    D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec );
    FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );

//    m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
    m_pd3dDevice->SetTransform( D3DTS_VIEW,       &m_matView );
    m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

    // Set up the lights
    if( m_d3dCaps.VertexProcessingCaps & D3DVTXPCAPS_DIRECTIONALLIGHTS )
    {
        D3DLIGHT8 light;
        D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.5f, -1.0f, 0.3f );
        light.Ambient.r = 0.1f;
        light.Ambient.g = 0.1f;
        light.Ambient.b = 0.1f;
        m_pd3dDevice->SetLight( 0, &light );
        m_pd3dDevice->LightEnable( 0, TRUE );
        m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

        D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.5f, 1.0f, 1.0f );
        light.Ambient.r = 0.2f;
        light.Ambient.g = 0.2f;
        light.Ambient.b = 0.2f;
        m_pd3dDevice->SetLight( 1, &light );
        m_pd3dDevice->LightEnable( 1, TRUE );
        m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
    }
	else
	{
	    m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x0c0c0c0c);
	}

	// fill the vertex buffer
	m_dwSizeofVertices = sizeof(m_pvObjectVertices);

    // Create the vertex buffer
    if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_MANAGED, &m_pVB ) ) )
        return E_FAIL;

    VOID* pVertices;
    if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, m_pvObjectVertices, m_dwSizeofVertices);
    m_pVB->Unlock();

	// fill the Index buffer
	m_dwSizeofIndices = sizeof(m_pwObjectIndices);

	// Create the index buffer
	if( FAILED( m_pd3dDevice->CreateIndexBuffer( m_dwSizeofIndices,
	                                             0, D3DFMT_INDEX16,
		                                         D3DPOOL_MANAGED, &m_pIB ) ) )
		return E_FAIL;

	VOID* pIndices;
	if( FAILED( m_pIB->Lock( 0, m_dwSizeofIndices, (BYTE**)&pIndices, 0 ) ) )
	   return E_FAIL;
	memcpy( pIndices, m_pwObjectIndices, m_dwSizeofIndices );
	m_pIB->Unlock();

  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()
{
    m_pFont->InvalidateDeviceObjects();
    SAFE_RELEASE( m_pVB );
    SAFE_RELEASE( m_pIB );
   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()
{
    m_pFont->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()
{
   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
//       for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, 
                                          D3DFORMAT Format )
{

    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 )
{
    // Record key presses
    if( WM_KEYDOWN == uMsg )
    {
        m_bKey[wParam] = 1;
    }

    // Perform commands when keys are rleased
    if( WM_KEYUP == uMsg )
    {
        m_bKey[wParam] = 0;

        switch( wParam )
        {
            case VK_F1:
//                m_bDrawHelp = !m_bDrawHelp;
                return 1;
        }
    }

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

⌨️ 快捷键说明

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