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

📄 water.cpp

📁 在GPU上实现数值模拟技术(线性方程组)的通用架构
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//--------------------------------------------------------------------------------------
// File: Water.cpp
//
// Simple Shader 2.0 clFramework demo
//
// Copyright (c) Jens Kr黦er All rights reserved.
//--------------------------------------------------------------------------------------
#include "water.h"

void SetCurrentDir(LPSTR cmdLine) {
	LPWSTR wcLine;
	wcLine = GetCommandLine();

	char *cline = new char[int(_tcslen(wcLine))+1];

	BOOL bConvProblem;
	WideCharToMultiByte( CP_ACP, 0, wcLine, -1, cline, int(_tcslen(wcLine))+1,"_",&bConvProblem );

	char strAppDir[_MAX_PATH];
	strncpy(strAppDir,cline,strlen(cline)-strlen(cmdLine));
	strAppDir[strlen(cline)-strlen(cmdLine)] = 0;
	
	unsigned int end=0;
	for (unsigned int i = 0;i<strlen(strAppDir);i++) if (strAppDir[i] == '\\') {
		end = i;
	}

	strncpy(strAppDir,strAppDir,end);
	strAppDir[end] = 0;	strupr(strAppDir);
	if( !_chdrive( strAppDir[0]-'A'+1 ) ) _chdir(strAppDir+2);

	delete [] cline;
}

//--------------------------------------------------------------------------------------
// Entry point to the program. Initializes everything and goes into a message processing 
// loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR cmdLine, int )
{
	// This is for "cheap screensaver" support
	if (strstr(cmdLine,"/p") != 0) {
		exit(0);
	}
	if (strstr(cmdLine,"/c") != 0) {
		MessageBoxA(NULL,"Water Screensaver\n(c) 2004 by Jens Kr黦er\n http://wwwcg.in.tum.de/\nHit ESC to exit the Screensaver\nHit F1 for help ","Implict Water Screensaver",MB_OK);
		exit(0);
	}
	if (strstr(cmdLine,"/s") != 0) {
		SetCurrentDir(cmdLine);
		g_bStartWindowed = false;
		g_bShowUI = false;
		g_bRainMode = true;
		g_bScreesaverMode = true;
	} else g_bScreesaverMode = false;
	// end screensaver


	if (strlen(cmdLine) != 0 && atoi(cmdLine) != 0 ) g_size = atoi(cmdLine);
	
	// Set the callback functions. These functions allow the sample framework to notify
    // the application about device changes, user input, and windows messages.  The 
    // callbacks are optional so you need only set callbacks for events you're interested 
    // in. However, if you don't handle the device reset/lost callbacks then the sample 
    // framework won't be able to reset your device since the application must first 
    // release all device resources before resetting.  Likewise, if you don't handle the 
    // device created/destroyed callbacks then the sample framework won't be able to 
    // recreate your device resources.
    DXUTSetCallbackDeviceCreated( OnCreateDevice );
    DXUTSetCallbackDeviceReset( OnResetDevice );
    DXUTSetCallbackDeviceLost( OnLostDevice );
    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
    DXUTSetCallbackMsgProc( MsgProc );
    DXUTSetCallbackKeyboard( KeyboardProc );
    DXUTSetCallbackFrameRender( OnFrameRender );
    DXUTSetCallbackFrameMove( OnFrameMove );
	DXUTSetCallbackMouse( OnMouseEvent, true);

    // Show the cursor and clip it when in full screen
    DXUTSetCursorSettings( g_bScreesaverMode, true );

    InitApp();

    // Initialize the sample framework and create the desired Win32 window and Direct3D 
    // device for the application. Calling each of these functions is optional, but they
    // allow you to set several options which control the behavior of the framework.
    DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes

    DXUTCreateWindow( g_strTitle );
    DXUTCreateDevice( D3DADAPTER_DEFAULT, g_bStartWindowed, MIN(600,(g_iGridSizeX > 64) ? g_iGridSizeX*2 : 128),
		                                        MIN(600,(g_iGridSizeY > 64) ? g_iGridSizeY*2 : 128),
												IsDeviceAcceptable, ModifyDeviceSettings );

    // Pass control to the sample framework for handling the message pump and 
    // dispatching render calls. The sample framework will call your FrameMove 
    // and FrameRender callback when there is idle time between handling window messages.
    DXUTMainLoop();

    // Perform any application-level cleanup here. Direct3D device resources are released within the
    // appropriate callback functions and therefore don't require any cleanup code here.

    return DXUTGetExitCode();
}


//--------------------------------------------------------------------------------------
// Initialize the app 
//--------------------------------------------------------------------------------------
void InitApp()
{
	// Initialize members
	g_iGridSizeX	= g_size;
	g_iGridSizeY	= g_size;

	g_bWireFrame				= false;
	g_pMainShader				= NULL;
	g_pVB						= NULL;
	g_pVBGrid					= NULL;
	g_pIBGrid					= NULL;

	g_cluUCurrent				= NULL;
	g_cluULast					= NULL;
	g_cluRHS					= NULL;
#ifdef useUpacked 
	g_cluUNext					= NULL;
#else
	g_clRHS						= NULL;
	g_clUNext					= NULL;
#endif
	g_fDt						= 0;
	g_fDX						= 0;
	g_fDY						= 0;

	g_fMouseX					= 0;
	g_fMouseY					= 0;
	g_bMouseLDown				= false;
	g_bIsGUIEvent				= false;

    g_pCubePoolView             = NULL;

	g_fC						= 0.3f;

	g_iRainCounter				= 0;
	g_iRainDelay				= 10;

	g_bTimeclPerformance		= false;

	// Initialize UI
    g_HUD.SetCallback( OnGUIEvent ); int iY = 10; 
    g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
    g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22 );
    g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22 );
    g_HUD.AddButton( IDC_TOGGLEUI, L"Hide UI (u)", 35, iY += 24, 125, 22 );

	g_WaterUI.SetCallback( OnGUIEvent ); iY = 2;
	g_WaterUI.AddStatic( IDC_STATIC_VIS, _T("Velocity"),50, iY += 24, 100, 22);
	g_WaterUI.GetStatic( IDC_STATIC_VIS )->GetElement(0)->dwTextFormat = DT_LEFT|DT_TOP|DT_WORDBREAK;
    g_WaterUI.AddSlider( IDC_SLIDER_VIS, 50, iY += 24, 100, 22 );
    g_WaterUI.GetSlider( IDC_SLIDER_VIS )->SetRange( 10, 100 );
    g_WaterUI.GetSlider( IDC_SLIDER_VIS )->SetValue( int(g_fC*100) );
	g_WaterUI.AddCheckBox( IDC_CBOX_RAIN, _T("Rain"), 50, iY += 50, 125, 22,g_bRainMode );
    g_WaterUI.AddSlider( IDC_SLIDER_RAIN, 50, iY += 24, 100, 22 );
    g_WaterUI.GetSlider( IDC_SLIDER_RAIN )->SetRange( 1, 100 );
	g_WaterUI.GetSlider( IDC_SLIDER_RAIN )->SetEnabled(g_bRainMode);
    g_WaterUI.GetSlider( IDC_SLIDER_RAIN )->SetValue( 100/g_iRainDelay );
}

HRESULT createCubeMap(IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, _TCHAR *face) {
	if (FAILED(DirectXUtils::checkLoadCubemap(_T("textures"), &g_pCubePoolView,pBackBufferSurfaceDesc->Format,pd3dDevice,512,face))) {
		MessageBox(NULL, _T("Unable to load pool cubemap"), _T("Error"),MB_OK);
		return E_FAIL;
	}
	return S_OK;
}


//--------------------------------------------------------------------------------------
// Called during device initialization, this code checks the device for some 
// minimum set of capabilities, and rejects those that don't pass by returning false.
//--------------------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
                                  D3DFORMAT BackBufferFormat, bool bWindowed )
{
    IDirect3D9* pD3D = DXUTGetD3DObject(); 

   if(	pCaps->PixelShaderVersion >= D3DPS_VERSION(2,0) &&
		pCaps->VertexShaderVersion >= D3DVS_VERSION(2,0) &&
		DirectXUtils::isTextureFormatOk( D3DFMT_R32F, AdapterFormat, pD3D) &&
		DirectXUtils::isTextureFormatOk( D3DFMT_A32B32G32R32F, AdapterFormat, pD3D))
        return true;
    else
        return false;
	
}

void ClearSimulation() {
	g_cluULast->clear();
#ifdef useUpacked
	g_cluUNext->clear();
#else
	g_clUNext->clear();
#endif
	g_cluUCurrent->clear();
}

void CALLBACK OnMouseEvent( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos ) {
	g_fMouseX = float(xPos)/g_iViewWidth;
	g_fMouseY = float(yPos)/g_iViewHeight;

	if (bRightButtonDown) ClearSimulation();

	g_bMouseLDown = bLeftButtonDown;
}

//--------------------------------------------------------------------------------------
// This callback function is called immediately before a device is created to allow the 
// application to modify the device settings. The supplied pDeviceSettings parameter 
// contains the settings that the framework has selected for the new device, and the 
// application can make any desired changes directly to this structure.  Note however that 
// the sample framework will not correct invalid device settings so care must be taken 
// to return valid device settings, otherwise IDirect3D9::CreateDevice() will fail.  
//--------------------------------------------------------------------------------------
void CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps )
{
    // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
    // then switch to SWVP.
    if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
         pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
    {
        pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    }
    else
    {
        pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    }

    // This application is designed to work on a pure device by not using 
    // IDirect3D9::Get*() methods, so create a pure device if supported and using HWVP.
    if ((pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 && 
        (pDeviceSettings->BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING) != 0 )
        pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;

    // Debugging vertex shaders requires either REF or software vertex processing 
    // and debugging pixel shaders requires REF.  
#ifdef DEBUG_VS
    if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
    {
        pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
        pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
        pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    }
#endif
#ifdef DEBUG_PS
    pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
#endif
}


//--------------------------------------------------------------------------------------
// This callback function will be called immediately after the Direct3D device has been 
// created, which will happen during application initialization and windowed/full screen 
// toggles. This is the best location to create D3DPOOL_MANAGED resources since these 
// resources need to be reloaded whenever the device is destroyed. Resources created  
// here should be released in the OnDestroyDevice callback. 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
{
    HRESULT hr;

	g_pd3dDevice = pd3dDevice;
	g_pBackBufferSurfaceDesc = pBackBufferSurfaceDesc;

    // Initialize the font

⌨️ 快捷键说明

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