第十章 AmbientDiffuseSpecularDemo核心代码

来源:互联网 发布:csgo安卓软件 编辑:程序博客网 时间:2024/06/08 03:47

AmbientDiffuseSpecularDemo.cpp

 

//=============================================================================// AmbientDiffuseSpecularDemo.cpp by Frank Luna (C) 2005 All Rights Reserved.//// Demonstrates diffuse, ambient, and specular lighting.//// Controls: Use mouse to orbit and zoom; use the 'W' and 'S' keys to //           alter the height of the camera.//=============================================================================#include "d3dApp.h"#include "DirectInput.h"#include <crtdbg.h>#include "GfxStats.h"#include <list>#include "Vertex.h"class AmbientDiffuseSpecularDemo : public D3DApp{public:AmbientDiffuseSpecularDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);~AmbientDiffuseSpecularDemo();bool checkDeviceCaps();void onLostDevice();void onResetDevice();void updateScene(float dt);void drawScene();// Helper methodsvoid buildFX();void buildViewMtx();void buildProjMtx();private:GfxStats* mGfxStats;ID3DXMesh*   mTeapot;ID3DXEffect* mFX;D3DXHANDLE   mhTech;D3DXHANDLE   mhWVP;D3DXHANDLE   mhWorldInverseTranspose;D3DXHANDLE   mhLightVecW;D3DXHANDLE   mhDiffuseMtrl;D3DXHANDLE   mhDiffuseLight;D3DXHANDLE   mhAmbientMtrl;D3DXHANDLE   mhAmbientLight;D3DXHANDLE   mhSpecularMtrl;D3DXHANDLE   mhSpecularLight;D3DXHANDLE   mhSpecularPower;D3DXHANDLE   mhEyePos;D3DXHANDLE   mhWorld;D3DXVECTOR3 mLightVecW;D3DXCOLOR   mAmbientMtrl;D3DXCOLOR   mAmbientLight;D3DXCOLOR   mDiffuseMtrl;D3DXCOLOR   mDiffuseLight;D3DXCOLOR   mSpecularMtrl;D3DXCOLOR   mSpecularLight;float       mSpecularPower;float mCameraRotationY;float mCameraRadius;float mCameraHeight;D3DXMATRIX mWorld;D3DXMATRIX mView;D3DXMATRIX mProj;};int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,   PSTR cmdLine, int showCmd){// Enable run-time memory check for debug builds.#if defined(DEBUG) | defined(_DEBUG)_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );#endifAmbientDiffuseSpecularDemo app(hInstance, "Ambient, Diffuse, Spec Demo", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);gd3dApp = &app;DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);gDInput = &di;    return gd3dApp->run();}AmbientDiffuseSpecularDemo::AmbientDiffuseSpecularDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP): D3DApp(hInstance, winCaption, devType, requestedVP){if(!checkDeviceCaps()){MessageBox(0, "checkDeviceCaps() Failed", 0, 0);PostQuitMessage(0);}mGfxStats = new GfxStats();mCameraRadius    = 6.0f;mCameraRotationY = 1.2 * D3DX_PI;mCameraHeight    = 3.0f;mLightVecW     = D3DXVECTOR3(0.0, 0.0f, -1.0f);mDiffuseMtrl   = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);mDiffuseLight  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);mAmbientMtrl   = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);mAmbientLight  = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f);mSpecularMtrl  = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);mSpecularLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);mSpecularPower = 8.0f;D3DXMatrixIdentity(&mWorld);HR(D3DXCreateTeapot(gd3dDevice, &mTeapot, 0));buildFX();onResetDevice();InitAllVertexDeclarations();}AmbientDiffuseSpecularDemo::~AmbientDiffuseSpecularDemo(){delete mGfxStats;ReleaseCOM(mTeapot);ReleaseCOM(mFX);DestroyAllVertexDeclarations();}bool AmbientDiffuseSpecularDemo::checkDeviceCaps(){D3DCAPS9 caps;HR(gd3dDevice->GetDeviceCaps(&caps));// Check for vertex shader version 2.0 support.if( caps.VertexShaderVersion < D3DVS_VERSION(2, 0) )return false;// Check for pixel shader version 2.0 support.if( caps.PixelShaderVersion < D3DPS_VERSION(2, 0) )return false;return true;}void AmbientDiffuseSpecularDemo::onLostDevice(){mGfxStats->onLostDevice();HR(mFX->OnLostDevice());}void AmbientDiffuseSpecularDemo::onResetDevice(){mGfxStats->onResetDevice();HR(mFX->OnResetDevice());// The aspect ratio depends on the backbuffer dimensions, which can // possibly change after a reset.  So rebuild the projection matrix.buildProjMtx();}void AmbientDiffuseSpecularDemo::updateScene(float dt){mGfxStats->setVertexCount(mTeapot->GetNumVertices());mGfxStats->setTriCount(mTeapot->GetNumFaces());mGfxStats->update(dt);// Get snapshot of input devices.gDInput->poll();// Check input.if( gDInput->keyDown(DIK_W) ) mCameraHeight   += 25.0f * dt;if( gDInput->keyDown(DIK_S) ) mCameraHeight   -= 25.0f * dt;// Scale down to make mouse less sensitive. mCameraRotationY += gDInput->mouseDX() / 100.0f;mCameraRadius    += gDInput->mouseDY() / 25.0f;// If we rotate over 360 degrees, just roll back to 0if( fabsf(mCameraRotationY) >= 2.0f * D3DX_PI ) mCameraRotationY = 0.0f;// Don't let radius get too small.if( mCameraRadius < 5.0f )mCameraRadius = 5.0f;// The camera position/orientation relative to world space can // change every frame based on input, so we need to rebuild the// view matrix every frame with the latest changes.buildViewMtx();}void AmbientDiffuseSpecularDemo::drawScene(){// Clear the backbuffer and depth buffer.HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));HR(gd3dDevice->BeginScene());// Setup the rendering FXHR(mFX->SetTechnique(mhTech));HR(mFX->SetMatrix(mhWVP, &(mWorld*mView*mProj)));D3DXMATRIX worldInverseTranspose;D3DXMatrixInverse(&worldInverseTranspose, 0, &mWorld);D3DXMatrixTranspose(&worldInverseTranspose, &worldInverseTranspose);HR(mFX->SetMatrix(mhWorldInverseTranspose, &worldInverseTranspose));HR(mFX->SetValue(mhLightVecW, &mLightVecW, sizeof(D3DXVECTOR3)));HR(mFX->SetValue(mhDiffuseMtrl, &mDiffuseMtrl, sizeof(D3DXCOLOR)));HR(mFX->SetValue(mhDiffuseLight, &mDiffuseLight, sizeof(D3DXCOLOR)));HR(mFX->SetValue(mhAmbientMtrl, &mAmbientMtrl, sizeof(D3DXCOLOR)));HR(mFX->SetValue(mhAmbientLight, &mAmbientLight, sizeof(D3DXCOLOR)));HR(mFX->SetValue(mhSpecularLight, &mSpecularLight, sizeof(D3DXCOLOR)));HR(mFX->SetValue(mhSpecularMtrl, &mSpecularMtrl, sizeof(D3DXCOLOR)));HR(mFX->SetFloat(mhSpecularPower, mSpecularPower));HR(mFX->SetMatrix(mhWorld, &mWorld));// Begin passes.UINT numPasses = 0;HR(mFX->Begin(&numPasses, 0));for(UINT i = 0; i < numPasses; ++i){HR(mFX->BeginPass(i));HR(mTeapot->DrawSubset(0));HR(mFX->EndPass());}HR(mFX->End());mGfxStats->display();HR(gd3dDevice->EndScene());// Present the backbuffer.HR(gd3dDevice->Present(0, 0, 0, 0));}void AmbientDiffuseSpecularDemo::buildFX(){// Create the FX from a .fx file.ID3DXBuffer* errors = 0;HR(D3DXCreateEffectFromFile(gd3dDevice, "ambientdiffusespec.fx", 0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors));if( errors )MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);// Obtain handles.mhTech                  = mFX->GetTechniqueByName("AmbientDiffuseSpecTech");mhWVP                   = mFX->GetParameterByName(0, "gWVP");mhWorldInverseTranspose = mFX->GetParameterByName(0, "gWorldInverseTranspose");mhLightVecW             = mFX->GetParameterByName(0, "gLightVecW");mhDiffuseMtrl           = mFX->GetParameterByName(0, "gDiffuseMtrl");mhDiffuseLight          = mFX->GetParameterByName(0, "gDiffuseLight");mhAmbientMtrl           = mFX->GetParameterByName(0, "gAmbientMtrl");mhAmbientLight          = mFX->GetParameterByName(0, "gAmbientLight");mhSpecularMtrl          = mFX->GetParameterByName(0, "gSpecularMtrl");mhSpecularLight         = mFX->GetParameterByName(0, "gSpecularLight");mhSpecularPower         = mFX->GetParameterByName(0, "gSpecularPower");mhEyePos                = mFX->GetParameterByName(0, "gEyePosW");mhWorld                 = mFX->GetParameterByName(0, "gWorld");}void AmbientDiffuseSpecularDemo::buildViewMtx(){float x = mCameraRadius * cosf(mCameraRotationY);float z = mCameraRadius * sinf(mCameraRotationY);D3DXVECTOR3 pos(x, mCameraHeight, z);D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);D3DXMatrixLookAtLH(&mView, &pos, &target, &up);HR(mFX->SetValue(mhEyePos, &pos, sizeof(D3DXVECTOR3)));}void AmbientDiffuseSpecularDemo::buildProjMtx(){float w = (float)md3dPP.BackBufferWidth;float h = (float)md3dPP.BackBufferHeight;D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f, w/h, 1.0f, 5000.0f);}

 

 


 

ambientdiffusespec.fx

 

//=============================================================================// ambientdiffusespec.fx by Frank Luna (C) 2004 All Rights Reserved.//// Does basic ambient, diffuse, and specular lighting.//=============================================================================uniform extern float4x4 gWorld;uniform extern float4x4 gWorldInverseTranspose;uniform extern float4x4 gWVP;uniform extern float4 gAmbientMtrl;uniform extern float4 gAmbientLight;uniform extern float4 gDiffuseMtrl;uniform extern float4 gDiffuseLight;uniform extern float4 gSpecularMtrl;uniform extern float4 gSpecularLight;uniform extern float  gSpecularPower;uniform extern float3 gLightVecW;uniform extern float3 gEyePosW; struct OutputVS{    float4 posH  : POSITION0;    float4 color : COLOR0;};OutputVS AmbientDiffuseSpecVS(float3 posL : POSITION0, float3 normalL : NORMAL0){    // Zero out our output.OutputVS outVS = (OutputVS)0;// Transform normal to world space.float3 normalW = mul(float4(normalL, 0.0f), gWorldInverseTranspose).xyz;normalW = normalize(normalW);// Transform vertex position to world space.float3 posW  = mul(float4(posL, 1.0f), gWorld).xyz;//=======================================================// Compute the color: Equation 10.3.// Compute the vector from the vertex to the eye position.float3 toEye = normalize(gEyePosW - posW);// Compute the reflection vector.float3 r = reflect(-gLightVecW, normalW);// Determine how much (if any) specular light makes it into the eye.float t  = pow(max(dot(r, toEye), 0.0f), gSpecularPower);// Determine the diffuse light intensity that strikes the vertex.float s = max(dot(gLightVecW, normalW), 0.0f);// Compute the ambient, diffuse and specular terms separatly. float3 spec = t*(gSpecularMtrl*gSpecularLight).rgb;float3 diffuse = s*(gDiffuseMtrl*gDiffuseLight).rgb;float3 ambient = gAmbientMtrl*gAmbientLight;// Sum all the terms together and copy over the diffuse alpha.outVS.color.rgb = ambient + diffuse + spec;outVS.color.a   = gDiffuseMtrl.a;//=======================================================// Transform to homogeneous clip space.outVS.posH = mul(float4(posL, 1.0f), gWVP);// Done--return the output.    return outVS;}float4 AmbientDiffuseSpecPS(float4 c : COLOR0) : COLOR{    return c;}technique AmbientDiffuseSpecTech{    pass P0    {        // Specify the vertex and pixel shader associated with this pass.        vertexShader = compile vs_2_0 AmbientDiffuseSpecVS();        pixelShader  = compile ps_2_0 AmbientDiffuseSpecPS();    }}


 

原创粉丝点击