game.cpp

来自「对游戏编程感兴趣的 朋友可以 下载下来看看 对DX讲解的很很详细」· C++ 代码 · 共 545 行 · 第 1/2 页

CPP
545
字号
	//Turn on Depth Buffering
	if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE)))
	{
		LogError("<li>SetRenderState: D3DRS_ZENABLE Failed");
		return false;
	}
	else
	{
		LogInfo("<li>SetRenderState: D3DRS_ZENABLE OK");
	}


	//Set fill state. Possible values: D3DFILL_POINT, D3DFILL_WIREFRAME, D3DFILL_SOLID
	if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID)))
	{
		LogError("<li>SetRenderState: D3DRS_FILLMODE Failed");
		return false;
	}
	else
	{
		LogInfo("<li>SetRenderState: D3DRS_FILLMODE OK");
	}

    return true;
}

bool CGame::InitialiseLights()
{
	LogInfo("<br>Initialise Lights:");

	D3DLIGHT8 d3dLight;

	//Initialize the light structure.
	ZeroMemory(&d3dLight, sizeof(D3DLIGHT8));

	d3dLight.Type = D3DLIGHT_POINT;
	
	d3dLight.Position.x = -10.0f;
	d3dLight.Position.y = 0.0f;
	d3dLight.Position.z = 0.0f;

	d3dLight.Attenuation0 = 1.0f; 
	d3dLight.Attenuation1 = 0.0f; 
	d3dLight.Attenuation2 = 0.0f; 
	d3dLight.Range = 100.0f;	

	d3dLight.Diffuse.r = 1.0f;
	d3dLight.Diffuse.g = 1.0f;
	d3dLight.Diffuse.b = 1.0f;
	
	d3dLight.Ambient.r = 0.0f;
	d3dLight.Ambient.g = 0.0f;
	d3dLight.Ambient.b = 0.0f;
	
	d3dLight.Specular.r = 0.0f;
	d3dLight.Specular.g	= 0.0f;
	d3dLight.Specular.b	= 0.0f;

	d3dLight.Direction = D3DXVECTOR3(-1.0, -1.0, 0.0);

	//Assign the point light to our device in poisition (index) 0
	if(FAILED(m_pD3DDevice->SetLight(0, &d3dLight)))
	{
		LogError("<li>SetLight Failed");
		return false;
	}
	else
	{
		LogInfo("<li>SetLight OK");
	}

	//Enable our point light in position (index) 0
	if(FAILED(m_pD3DDevice->LightEnable(0, TRUE)))
	{
		LogError("<li>LightEnable Failed");
		return false;
	}
	else
	{
		LogInfo("<li>LightEnable OK");
	}

	//Turn on lighting
    if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE)))
	{
		LogError("<li>SetRenderState: D3DRS_LIGHTING Failed");
		return false;
	}
	else
	{
		LogInfo("<li>SetRenderState: D3DRS_LIGHTING OK");
	}

	//Set ambient light level
	if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(60, 60, 60))))	
	{
		LogError("<li>SetRenderState: D3DRS_AMBIENT Failed");
		return false;
	}
	else
	{
		LogInfo("<li>SetRenderState: D3DRS_AMBIENT OK");
	}

	return true;
}

LPDIRECT3DDEVICE8 CGame::GetDevice()
{
	return m_pD3DDevice;
}

void CGame::GameLoop()
{
    //Enter the game loop
    MSG msg; 
    BOOL fMessage;

    PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);

	//Game started, so record time
	m_dwStartTime = timeGetTime();

    while(msg.message != WM_QUIT)
    {
        fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

        if(fMessage)
        {
            //Process message
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            //No message to process, so render the current scene
            Render();
        }
    }

	//Game finished, so record time
	m_dwEndTime = timeGetTime();
}

void CGame::Render()
{
	D3DXMATRIX matSun, matRotateSunAboutAxis, matScaleSun;
	D3DXMATRIX matMoon, matMoveMoon, matScaleMoon, matRotateMoonAboutAxis, matRotateMoonAboutEarth;
	D3DXMATRIX matEarth, matMoveEarth, matEarthRoll, matRotateEarthAboutAxis, matRotateEarthAboutSun;
	D3DXMATRIX matRotateCylinder, matCylinder, matMoveCylinder;
	D3DXMATRIX matRotateCone, matCone, matMoveCone;
	D3DXMATRIX matRotateCylinder2, matCylinder2, matMoveCylinder2;
	D3DXMATRIX matRotateCone2, matCone2, matMoveCone2;

	if(m_pD3DDevice == NULL)
    {
        return;
    }

    //Clear the back buffer and depth buffer
    m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
    //Begin the scene
    m_pD3DDevice->BeginScene();
    
	//Setup camera and perspective
	SetupCamera();
	
	//Speed of rotations
	float r1Day = -200;
	float r1Year = r1Day * 365.0f;
	float r1Month = r1Year / 12.0f;

	//Create the rotation transformation matrices for the earth
	D3DXMatrixRotationY(&matRotateEarthAboutAxis, timeGetTime()/r1Day);
	D3DXMatrixRotationY(&matRotateEarthAboutSun, timeGetTime()/r1Year);
	D3DXMatrixTranslation(&matMoveEarth, 20.0f, 0.0f, 0.0f);
	D3DXMatrixRotationYawPitchRoll(&matEarthRoll, 0.0f, 0.0f, D3DXToRadian(23.44f));
	D3DXMatrixMultiply(&matEarth, &matRotateEarthAboutAxis, &matEarthRoll);
	D3DXMatrixMultiply(&matEarth, &matEarth, &matMoveEarth);
	D3DXMatrixMultiply(&matEarth, &matEarth, &matRotateEarthAboutSun);
	

	//Create the rotation transformation matrices for the moon
	D3DXMatrixScaling(&matScaleMoon, 0.25f, 0.25f, 0.25f);
	D3DXMatrixTranslation(&matMoveMoon, 4.0f, 0.0f, 0.0f);
	D3DXMatrixRotationY(&matRotateMoonAboutAxis, timeGetTime()/r1Month);
	D3DXMatrixRotationY(&matRotateMoonAboutEarth, timeGetTime()/r1Month);
	D3DXMatrixMultiply(&matMoon, &matScaleMoon, &matRotateMoonAboutAxis);
	D3DXMatrixMultiply(&matMoon, &matMoon, &matMoveMoon);
	D3DXMatrixMultiply(&matMoon, &matMoon, &matRotateMoonAboutEarth);
	D3DXMatrixMultiply(&matMoon, &matMoon, &matMoveEarth);
	D3DXMatrixMultiply(&matMoon, &matMoon, &matRotateEarthAboutSun);

	//Create the rotation transformation matrices for the sun
	D3DXMatrixRotationY(&matRotateSunAboutAxis, timeGetTime()/(r1Day * 25.0f));
	D3DXMatrixScaling(&matScaleSun, 8.0f, 8.0f, 8.0f);
	D3DXMatrixMultiply(&matSun, &matScaleSun, &matRotateSunAboutAxis);

	//Create the rotation transformation matrices for the 1st cylinder
	D3DXMatrixTranslation(&matMoveCylinder, -15.0f, 13.0f, 0.0f);
	D3DXMatrixRotationX(&matRotateCylinder, timeGetTime()/-800.0f);
	D3DXMatrixMultiply(&matCylinder, &matRotateCylinder, &matMoveCylinder);

	//Create the rotation transformation matrices for the 1st cone
	D3DXMatrixTranslation(&matMoveCone, 15.0f, 13.0f, 0.0f);
	D3DXMatrixRotationX(&matRotateCone, timeGetTime()/800.0f);
	D3DXMatrixMultiply(&matCone, &matRotateCone, &matMoveCone);

	//Create the rotation transformation matrices for the 2nd cylinder
	D3DXMatrixTranslation(&matMoveCylinder2, 15.0f, -13.0f, 0.0f);
	D3DXMatrixRotationZ(&matRotateCylinder2, timeGetTime()/800.0f);
	D3DXMatrixMultiply(&matCylinder2, &matRotateCylinder2, &matMoveCylinder2);

	//Create the rotation transformation matrices for the 2nd cone
	D3DXMatrixTranslation(&matMoveCone2, -15.0f, -13.0f, 0.0f);
	D3DXMatrixRotationZ(&matRotateCone2, timeGetTime()/-800.0f);
	D3DXMatrixMultiply(&matCone2, &matRotateCone2, &matMoveCone2);

	//Render our objects
	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matCylinder);
	m_dwTotalPolygons += m_pCylinder1->Render();

	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matCone);
	m_dwTotalPolygons += m_pCone1->Render();

	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matCylinder2);
	m_dwTotalPolygons += m_pCylinder2->Render();

	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matCone2);
	m_dwTotalPolygons += m_pCone2->Render();

	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matSun);
	m_dwTotalPolygons += m_pSphere3->Render();

	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matEarth);
	m_dwTotalPolygons += m_pSphere1->Render();
	
	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matMoon);
	m_dwTotalPolygons += m_pSphere2->Render();		

    //End the scene
    m_pD3DDevice->EndScene();
    
    //Filp the back and front buffers so that whatever has been rendered on the back buffer
    //will now be visible on screen (front buffer).
    m_pD3DDevice->Present(NULL, NULL, NULL, NULL);

	//Count Frames
	m_dwFrames++;
}

void CGame::SetupCamera()
{
	//Here we will setup the camera.
	//The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"
	//We have set the following:
	//Camera Position:	(0, 15, -50)
	//Look at Position: (0, 0, 0)
	//Up direction:		Y-Axis.
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 15.0f, -50.0f),		//Camera Position
                                 &D3DXVECTOR3(0.0f, 0.0f, 0.0f),		//Look At Position
                                 &D3DXVECTOR3(0.0f, 1.0f, 0.0f));		//Up Direction
    m_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);

	//Here we specify the field of view, aspect ration and near and far clipping planes.
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.25f, 1.0f, 2000.0f);
    m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}

⌨️ 快捷键说明

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