www.pudn.com > snow.rar > snow.cpp


 
#include "d3dUtility.h" 
#include "psystem.h" 
#include "camera.h" 
#include "skybox.h" 
#include "drawMesh.h" 
#include "LensFlare.h" 
#include "PlayMp3.h" 
 
#include "DXUtil.h" 
#include "D3DEnumeration.h" 
#include "D3DSettings.h" 
#include "D3DApp.h" 
#include "D3DFile.h" 
#include "D3DFont.h" 
#include "D3DUtil.h" 
 
#include  
#include  
//python 
#include "python.h" 
 
int SnowNumber = 3000; 
int Snowing = 1; 
float LigthStrong; 
int light = 1; 
// 
// Globals 
// 
 
IDirect3DDevice9*       Device           = NULL;  
LPDIRECT3DCUBETEXTURE9  cubeTex          = NULL; 
D3DMATERIAL9 m_mtrlMirrorMaterial; 
 
DWORD					g_dwMouseX = 0;			 
DWORD					g_dwMouseY = 0; 
HWND mhwnd; 
D3DXMATRIX m_matProj; 
D3DXVECTOR3 m_vSunPos; 
 
const int Width  = 800; 
const int Height = 600; 
 
 
psys::PSystem* Sno      = NULL; 
SkyBox* g_skybox        = NULL; 
DrawMesh* g_drawMesh    = NULL; 
DrawMesh* g_drawMesh1   = NULL; 
CLensFlare m_LensFlare ; 
Camera TheCamera(Camera::LANDOBJECT); 
 
// 
// Framework Functions 
// 
bool Setup() 
{ 
	// seed random number generator 
	srand((unsigned int)time(0)); 
    HRESULT hr; 
	// 
	//Lua Setup 
	// 
 
	// 
	//python setup 
	// 
	Py_Initialize(); 
	PyObject *pName = PyString_FromString("snow1"); 
	PyObject *pModule = PyImport_Import(pName); 
	if(!pModule) 
	{ 
		::MessageBox(0, "Python - FAILED", 0, 0); 
		return 0; 
	}	 
	//snow 
	PyObject *pDict = PyModule_GetDict(pModule); 
    PyObject *pFunc = PyDict_GetItemString ( pDict, "SonwNumber" ); 
 
	PyObject * pParams = PyTuple_New ( 1 ); 
	PyObject * pCurrParam; 
	 
	pCurrParam = PyInt_FromLong ( Snowing ); 
	PyTuple_SetItem ( pParams, 0, pCurrParam ); 
 
 
	PyObject * pNumber = PyObject_CallObject ( pFunc, pParams); 
	SnowNumber = PyInt_AsLong ( pNumber ); 
	//light 
	pDict = PyModule_GetDict(pModule); 
    pFunc = PyDict_GetItemString ( pDict, "LightColor" ); 
 
	pParams = PyTuple_New ( 1 ); 
	pCurrParam; 
	 
	pCurrParam = PyInt_FromLong ( light ); 
	PyTuple_SetItem ( pParams, 0, pCurrParam ); 
 
 
	pNumber = PyObject_CallObject ( pFunc, pParams); 
	int temp = PyInt_AsLong ( pNumber ); 
	LigthStrong = temp/10.0; 
	//end 
	Py_XDECREF ( pNumber ); 
    Py_XDECREF ( pParams ); 
    Py_XDECREF ( pFunc ); 
    Py_XDECREF ( pDict ); 
    Py_XDECREF ( pModule ); 
    Py_XDECREF ( pName ); 
	Py_Finalize(); 
 
 
	// 
	// Create Snow System. 
	// 
	 
	d3d::BoundingBox boundingBox; 
	boundingBox._min = D3DXVECTOR3(-100.0f, -80.0f, -100.0f); 
	boundingBox._max = D3DXVECTOR3( 100.0f,  50.0f,  100.0f); 
	Sno = new psys::Snow(&boundingBox, SnowNumber); 
	Sno->init(Device, "SnowSRC\\snowflake.dds"); 
	// 
	// Create skybox. 
	// 
	g_skybox = new SkyBox(); 
    g_skybox->Init(Device); 
	V(D3DXCreateCubeTextureFromFileEx(Device,"SnowSRC\\CloudyHillsCubemap.dds", 
	    D3DX_DEFAULT,0,0,D3DFMT_R8G8B8, 
		D3DPOOL_MANAGED,D3DX_FILTER_LINEAR, 
		D3DX_FILTER_LINEAR,0,NULL,NULL,&cubeTex)); 
	 
	g_skybox->SetCubeMap(cubeTex); 
 
	// 
	// Create basic scene. 
	// 
	d3d::DrawBasicScene(Device, 1.0f); 
	// 
	//Create Mesh 
	// 
    g_drawMesh = new DrawMesh(); 
    g_drawMesh->InitGeometry(Device,"SnowSRC\\snowAccumulation_tree.x", 
	"SnowSRC\\snow.tga"); 
    g_drawMesh1 = new DrawMesh(); 
    g_drawMesh1->InitGeometry(Device,"SnowSRC\\aster1.x", 
	"SnowSRC\\snow.tga"); 
	// 
	//set light 
	// 
	D3DLIGHT9 light; 
	::ZeroMemory(&light, sizeof(light)); 
	D3DXVECTOR3 position(-1.707f, 20.707f, 20.707f); 
	D3DXCOLOR color(1.5f*LigthStrong, 1.5f*LigthStrong, 1.16f*LigthStrong, 2.0f); 
 
	light.Type      = D3DLIGHT_POINT; 
	light.Ambient   = color * 0.4f; 
	light.Diffuse   = color; 
	light.Specular  = color ;//* 0.8f; 
	light.Position  = position; 
	light.Range        = 40.0f; 
	light.Falloff      = 1.0f; 
	light.Attenuation0 = 1.0f; 
	light.Attenuation1 = 0.0f; 
	light.Attenuation2 = 0.0f; 
	Device->SetLight(0, &light);	 
    Device->SetRenderState( D3DRS_LIGHTING, TRUE); 
	// 
	// LensFlare 
	// 
	m_vSunPos = D3DXVECTOR3(50, 50,10); 
    m_LensFlare.RestoreDeviceObjects(Device); 
 
	LPDIRECT3DTEXTURE9 tex1 = m_LensFlare.AddTexture("SnowSRC\\snowflake.dds"); 
	LPDIRECT3DTEXTURE9 tex2 = m_LensFlare.AddTexture("SnowSRC\\snowflake.dds"); 
	LPDIRECT3DTEXTURE9 tex3 = m_LensFlare.AddTexture("SnowSRC\\snowflake.dds"); 
 
	m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.10f,  0.8f, D3DXCOLOR(0.7f, 0.5f, 0.0f, 0.2f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.01f,  0.7f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.7f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f,  0.6f, D3DXCOLOR(1.0f, 1.0f, 0.0f, 0.5f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.13f,  0.5f, D3DXCOLOR(1.0f, 1.0f, 0.0f, 0.5f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f,  0.4f, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.05f,  0.1f, D3DXCOLOR(1.0f, 1.0f, 0.5f, 0.5f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, -0.2f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.09f, -0.3f, D3DXCOLOR(1.0f, 1.0f, 0.6f, 0.5f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.13f, -0.4f, D3DXCOLOR(1.0f, 0.7f, 0.0f, 0.3f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.13f, -0.7f, D3DXCOLOR(1.0f, 0.5f, 0.0f, 0.2f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.10f, -1.0f, D3DXCOLOR(1.0f, 0.7f, 0.0f, 0.4f))); 
	m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.07f, -1.3f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.7f))); 
	// 
	// Set projection matrix. 
	// 
 
	D3DXMATRIX proj; 
	D3DXMatrixPerspectiveFovLH( 
			&proj, 
			D3DX_PI / 4.0f, // 45 - degree 
			(float)Width / (float)Height, 
			1.0f, 
			5000.0f); 
	Device->SetTransform(D3DTS_PROJECTION, &proj); 
	m_matProj = proj; 
	//python 
	 
	return true; 
} 
 
void Cleanup() 
{    
    g_drawMesh->Cleanup(); 
	g_skybox->Destroy(); 
	d3d::Delete( Sno ); 
	SAFE_DELETE(g_skybox); 
    SAFE_DELETE(g_drawMesh); 
	SAFE_DELETE(g_drawMesh1); 
	d3d::DrawBasicScene(0, 1.0f); 
	m_LensFlare.InvalidateDeviceObjects(); 
	vCleanup(); 
} 
 
bool Display(float timeDelta) 
{ 
	if( Device ) 
	{    
		// 
		// Update the scene: 
		//  
	    D3DXMATRIXA16 mView; 
		// 
		// mousedispose 
		// 
		POINT	pt; 
	    float	fDelta = 0.001f;	 
		GetCursorPos( &pt ); 
	    int dx = (pt.x - g_dwMouseX);	 
	    int dy = (pt.y - g_dwMouseY);	 
		TheCamera.pitch(dy * timeDelta*0.018f);	 
		TheCamera.yaw  (dx * timeDelta*0.018f); 
	 
		SetCursor( NULL );	 
	    RECT	rc; 
		GetClientRect( mhwnd, &rc ); 
		pt.x = (rc.right - rc.left) / 2; 
		pt.y = (rc.bottom - rc.top) / 2; 
		ClientToScreen( mhwnd, &pt ); 
		SetCursorPos( pt.x, pt.y ); 
		g_dwMouseX = pt.x; 
		g_dwMouseY = pt.y; 
		// 
		//keyboard 
		// 
		//if( ::GetAsyncKeyState('W') & 0x8000f ) 
		//	TheCamera.walk(10.0f * timeDelta); 
 
		//if( ::GetAsyncKeyState('S') & 0x8000f ) 
		//	TheCamera.walk(-10.0f * timeDelta); 
 
		//if( ::GetAsyncKeyState('A') & 0x8000f ) 
		//	TheCamera.strafe(-10.0f * timeDelta); 
 
		//if( ::GetAsyncKeyState('D') & 0x8000f ) 
		//	TheCamera.strafe(10.0f * timeDelta); 
		// 
		//SetTransform 
		// 
		D3DXMATRIX V; 
		TheCamera.getViewMatrix(&V); 
		Device->SetTransform(D3DTS_VIEW, &V); 
		Sno->update(timeDelta); 
		// 
		// Draw the scene: 
		// 
		Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xfffffff, 1.0f, 0); 
		Device->BeginScene();      
 
		// 
		// turn on the light & set normal 
		//	 
		Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); 
		Device->SetRenderState(D3DRS_SPECULARENABLE, true); 
		Device->SetRenderState( D3DRS_LIGHTING, true); 
		Device->LightEnable(0, true);	 
		// 
		// DrawBasic 
		// 
		D3DXMATRIX I; 
		D3DXMatrixIdentity(&I); 
		Device->SetTransform(D3DTS_WORLD, &I); 
         
		// 
		// render snow 
		// 
 
		Device->SetTransform(D3DTS_WORLD, &I); 
		Sno->render();	 
		// 
		//Draw mesh 
		// 
		//tree1	    
		D3DXMATRIX mWorld; 
		D3DXMATRIX mTrans,mRot,mTrans2,mScale; 
       	D3DXMatrixTranslation(&mTrans,-3,-3,-3); 
		D3DXMatrixScaling(&mScale,1,1,1); 
		mWorld = mScale*mTrans; 
		Device->SetTransform(D3DTS_WORLD, &mWorld); 
		g_drawMesh->Render(Device); 
		//land 
		D3DXMatrixTranslation(&mTrans,-3,-13,-3); 
		D3DXMatrixScaling(&mScale,18,14,18); 
		mWorld = mScale*mTrans; 
		Device->SetTransform(D3DTS_WORLD, &mWorld); 
		g_drawMesh1->Render(Device); 
		//tree2 
		D3DXMatrixTranslation(&mTrans,-12,-4,-3); 
		D3DXMatrixScaling(&mScale,0.7f,0.65f,0.7f); 
		mWorld = mScale*mTrans; 
		Device->SetTransform(D3DTS_WORLD, &mWorld); 
		g_drawMesh->Render(Device); 
		//tree3 
		D3DXMatrixTranslation(&mTrans,-9,-5,-10); 
		D3DXMatrixScaling(&mScale,0.7f,0.77f,0.7f); 
		mWorld = mScale*mTrans; 
		Device->SetTransform(D3DTS_WORLD, &mWorld); 
		g_drawMesh->Render(Device); 
		// 
		// LensFlare 
		// 
						 
 
		D3DXMatrixRotationY(&mRot,D3DX_PI / 4.0f); 
		D3DXMatrixTranslation(&mTrans,0,0,0); 
		D3DXMatrixTranslation(&mTrans2,-30,50,130); 
		mWorld = mTrans*mScale*mTrans2; 
		Device->SetTransform(D3DTS_WORLD, &mWorld); 
		Device->SetRenderState(D3DRS_ZENABLE, true); 
		m_LensFlare.Render(TheCamera, m_matProj, m_vSunPos,  
	    250, 185, false); 
		Device->SetRenderState( D3DRS_ALPHABLENDENABLE, false); 
		// 
		//turn off light 
		// 
	    Device->SetRenderState(D3DRS_NORMALIZENORMALS, false); 
	    Device->SetRenderState(D3DRS_SPECULARENABLE, false); 
     	Device->LightEnable(0, false); 
		// 
		// Draw the skybox at last 
		//	 
		D3DXMatrixRotationY(&mRot,D3DX_PI / -2.0f); 
		mWorld = mRot; 
		mWorld = mRot*V;	 
    	g_skybox->Render(Device,mWorld); 
 
 
		// 
		// End the scene 
		// 
		Device->EndScene(); 
		Device->Present(0, 0, 0, 0); 
	} 
	return true; 
} 
 
 
// 
// WndProc 
// 
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
	mhwnd=hwnd; 
	switch( msg ) 
	{ 
	case WM_DESTROY: 
		::PostQuitMessage(0); 
		break; 
		 
	case WM_KEYDOWN: 
		if( wParam == VK_ESCAPE ) 
			::DestroyWindow(hwnd); 
		break; 
	} 
	return ::DefWindowProc(hwnd, msg, wParam, lParam); 
} 
 
// 
// WinMain 
// 
int WINAPI WinMain(HINSTANCE hinstance, 
				   HINSTANCE prevInstance,  
				   PSTR cmdLine, 
				   int showCmd) 
{ 
	bool		bRet;  
	bRet = bPlayTitleMusic(); 
 
	if(!d3d::InitD3D(hinstance, 
		Width, Height, true, D3DDEVTYPE_HAL, &Device)) 
	{ 
		::MessageBox(0, "InitD3D() - FAILED", 0, 0); 
		return 0; 
	} 
		 
	if(!Setup()) 
	{ 
		::MessageBox(0, "Setup() - FAILED", 0, 0); 
		return 0; 
	} 
 
	d3d::EnterMsgLoop( Display ); 
	if( bRet == 0 ) { 
		MessageBox( mhwnd, "Initialization Failure",  
					"Failed to initialize DirectShow",  
					MB_ICONEXCLAMATION|MB_OK ); 
		// The program failed, exit 
		exit(1); 
	} 
	Cleanup(); 
	Device->Release(); 
 
	return 0; 
}