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

📄 blois.cpp

📁 著名的GPU Gems中的自然效果模拟-水效果的演示程序及全部实现源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RenderText()
{
    D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
    TCHAR szMsg[MAX_PATH] = TEXT("");

    // Output display stats
    FLOAT fNextLine = 40.0f; 

    lstrcpy( szMsg, m_strDeviceStats );
    fNextLine -= 20.0f;
    m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

    lstrcpy( szMsg, m_strFrameStats );
    fNextLine -= 20.0f;
    m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

    // Output statistics & help
    fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 


    lstrcpy( szMsg, TEXT("Spacebar toggles help") );
    fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

	if( m_bShowHelp )
	{
		lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
		fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'F' - Environment map height") );
		fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'G' - Environment map radius") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'P' - Texture wave angle deviation") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'O' - Geometric wave angle deviation") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'N' - Texture Noise") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'Y' - Texture scaling") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'U' - Texture wave height") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'K' - Geometric wave choppiness") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'J' - Geometric wave height") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'H' - Water level") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("Parameters - Shift goes up, non-shift goes down") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'B' - Show bump map") );
		fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'r' - Reinitializes system with current settings") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("'R' - Resets system settings to default and re-init") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("Toggles:") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("Left/right arrow - slide left/right") );
		fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("Up/down arrow - move up/down") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("W/S - move forward/backward") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("A/Z - pitch up/down") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("E/Q - rotate scene right/left") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );

		lstrcpy( szMsg, TEXT("Movement - Shift is faster:") );
		fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );


	}

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Overrrides the main WndProc, so the sample can do custom message
//       handling (e.g. processing mouse, keyboard, or menu commands).
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
                                    LPARAM lParam )
{
    switch( msg )
    {
        case WM_KEYDOWN:
            m_bKey[wParam] = TRUE;
            break;

        case WM_KEYUP:
            m_bKey[wParam] = FALSE;
            break;
        case WM_PAINT:
        {
            if( m_bLoadingApp )
            {
                // Draw on the window tell the user that the app is loading
                // TODO: change as needed
                HDC hDC = GetDC( hWnd );
                TCHAR strMsg[MAX_PATH];
                wsprintf( strMsg, TEXT("Loading... Please wait") );
                RECT rct;
                GetClientRect( hWnd, &rct );
                DrawText( hDC, strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
                ReleaseDC( hWnd, hDC );
            }
            break;
        }

    }

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




//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Invalidates device objects.  Paired with RestoreDeviceObjects()
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
    // TODO: Cleanup any objects created in RestoreDeviceObjects()
    m_pFont->InvalidateDeviceObjects();
    m_CompCosinesEff->OnLostDevice();
	m_WaterEff->OnLostDevice();

	SAFE_RELEASE(m_BumpTex);
	SAFE_RELEASE(m_BumpSurf);
	SAFE_RELEASE(m_BumpRender);

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Paired with InitDeviceObjects()
//       Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.  
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
    // TODO: Cleanup any objects created in InitDeviceObjects()
    m_pFont->DeleteDeviceObjects();

	SAFE_RELEASE(m_CompCosinesEff);
	SAFE_RELEASE(m_WaterEff);
	SAFE_RELEASE(m_CosineLUT);
	SAFE_RELEASE(m_BiasNoiseMap);
	SAFE_RELEASE(m_EnvMap);
	SAFE_RELEASE(m_LandTex);

	SAFE_RELEASE(m_WaterMesh);
	SAFE_RELEASE(m_LandMesh);
	SAFE_RELEASE(m_PillarsMesh);

	SAFE_RELEASE(m_BumpVBuffer);

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Paired with OneTimeSceneInit()
//       Called before the app exits, this function gives the app the chance
//       to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
    // TODO: Perform any final cleanup needed
    // Cleanup D3D font
    SAFE_DELETE( m_pFont );

    return S_OK;
}

HRESULT CMyD3DApplication::CreateCosineLUT()
{
	HRESULT hr = D3DXCreateTexture(m_pd3dDevice,
		kBumpTexSize,
		1,
		1,
		0,
		D3DFMT_A8R8G8B8,
		D3DPOOL_MANAGED,
		&m_CosineLUT);

	if( FAILED(hr) )
		return hr;

	D3DLOCKED_RECT lockedRect;
	hr = m_CosineLUT->LockRect(0, &lockedRect, NULL, 0);
	if( FAILED(hr) )
		return hr;

	unsigned long* pDat = (unsigned long*)lockedRect.pBits;
	int i;
	for( i = 0; i < kBumpTexSize; i++ )
	{
		float dist = float(i) / float(kBumpTexSize-1) * 2.f * D3DX_PI;
		float c = float(cos(dist));
		float s = float(sin(dist));
		s *= 0.5f;
		s += 0.5f;
		s = float(pow(s, m_TexState.m_Chop));
		c *= s;
		unsigned char cosDist = (unsigned char)((c * 0.5 + 0.5) * 255.999f);
		pDat[i] = (0xff << 24)
					| (cosDist << 16)
					| (cosDist << 8)
					| 0xff;
	}

	m_CosineLUT->UnlockRect(0);

	return S_OK;
}

HRESULT CMyD3DApplication::CreateBiasNoiseMap()
{
	HRESULT hr = D3DXCreateTexture(m_pd3dDevice,
		kBumpTexSize,
		kBumpTexSize,
		1,
		0,
		D3DFMT_A8R8G8B8,
		D3DPOOL_MANAGED,
		&m_BiasNoiseMap);

	if( FAILED(hr) )
		return hr;

	D3DLOCKED_RECT lockedRect;
	hr = m_BiasNoiseMap->LockRect(0, &lockedRect, NULL, 0);
	if( FAILED(hr) )
		return hr;

	unsigned long* pDat = (unsigned long*)lockedRect.pBits;

	const int size = kBumpTexSize;
	int i;
	for( i = 0; i < size; i++ )
	{
		int j;
		for( j = 0; j < size; j++ )
		{
			float x = RandZeroToOne();
			float y = RandZeroToOne();

			unsigned char r = (unsigned char)(x * 255.999f);
			unsigned char g = (unsigned char)(y * 255.999f);
	
			pDat[j] = (0xff << 24)
				| (r << 16)
				| (g << 8)
				| 0xff;
		}

		pDat += lockedRect.Pitch / 4;
	}

	m_BiasNoiseMap->UnlockRect(0);

	return S_OK;
}

void CMyD3DApplication::InitTexWaves()
{
	int i;
	for( i = 0; i < kNumTexWaves; i++ )
		InitTexWave(i);
}

void CMyD3DApplication::UpdateTexWaves(FLOAT dt)
{
	int i;
	for( i = 0; i < kNumTexWaves; i++ )
		UpdateTexWave(i, dt);
}

void CMyD3DApplication::UpdateTexWave(int i, FLOAT dt)
{
	if( i == m_TexState.m_TransIdx )
	{
		m_TexWaves[i].m_Fade += m_TexState.m_TransDel * dt;
		if( m_TexWaves[i].m_Fade < 0 )
		{
			// This wave is faded out. Re-init and fade it back up.
			InitTexWave(i);
			m_TexWaves[i].m_Fade = 0;
			m_TexState.m_TransDel = -m_TexState.m_TransDel;
		}
		else if( m_TexWaves[i].m_Fade > 1.f )
		{
			// This wave is faded back up. Start fading another down.
			m_TexWaves[i].m_Fade = 1.f;
			m_TexState.m_TransDel = -m_TexState.m_TransDel;
			if( ++m_TexState.m_TransIdx >= kNumTexWaves )
				m_TexState.m_TransIdx = 0;
		}
	}
	m_TexWaves[i].m_Phase -= dt * m_TexWaves[i].m_Speed;
	m_TexWaves[i].m_Phase -= int(m_TexWaves[i].m_Phase);
}

void CMyD3DApplication::InitTexWave(int i)
{
	float rads = RandMinusOneToOne() * m_TexState.m_AngleDeviation * D3DX_PI / 180.f;
	float dx = float(sin(rads));
	float dy = float(cos(rads));


	float tx = dx;
	dx = m_TexState.m_WindDir.y * dx - m_TexState.m_WindDir.x * dy;
	dy = m_TexState.m_WindDir.x * tx + m_TexState.m_WindDir.y * dy;

	float maxLen = m_TexState.m_MaxLength * kBumpTexSize / m_TexState.m_RippleScale;
	float minLen = m_TexState.m_MinLength * kBumpTexSize / m_TexState.m_RippleScale;
	float len = float(i) / float(kNumTexWaves-1) * (maxLen - minLen) + minLen;

	float reps = float(kBumpTexSize) / len;

	dx *= reps;
	dy *= reps;
	dx = float(int(dx >= 0 ? dx + 0.5f : dx - 0.5f));
	dy = float(int(dy >= 0 ? dy + 0.5f : dy - 0.5f));

	m_TexWaves[i].m_RotScale.x = dx;
	m_TexWaves[i].m_RotScale.y = dy;

	float effK = float(1.0 / sqrt(dx*dx + dy*dy));
	m_TexWaves[i].m_Len = float(kBumpTexSize) * effK;
	m_TexWaves[i].m_Freq = D3DX_PI * 2.f / m_TexWaves[i].m_Len;
	m_TexWaves[i].m_Amp = m_TexWaves[i].m_Len * m_TexState.m_AmpOverLen;
	m_TexWaves[i].m_Phase = RandZeroToOne();
	
	m_TexWaves[i].m_Dir.x = dx * effK;
	m_TexWaves[i].m_Dir.y = dy * effK;

	m_TexWaves[i].m_Fade = 1.f;

	float speed = float( 1.0 / sqrt(m_TexWaves[i].m_Len / (2.f * D3DX_PI * kGravConst)) ) / 3.f;
	speed *= 1.f + RandMinusOneToOne() * m_TexState.m_SpeedDeviation;
	m_TexWaves[i].m_Speed = speed;
}

void CMyD3DApplication::InitTexState()
{
	m_TexState.m_Noise = 0.2f;
	m_TexState.m_Chop = 1.f;
	m_TexState.m_AngleDeviation = 15.f;
	m_TexState.m_WindDir.x = 0;
	m_TexState.m_WindDir.y = 1.f;
	m_TexState.m_MaxLength = 10.f;
	m_TexState.m_MinLength = 1.f;
	m_TexState.m_AmpOverLen = 0.1f;
	m_TexState.m_RippleScale = 25.f;
	m_TexState.m_SpeedDeviation = 0.1f;

	m_TexState.m_TransIdx = 0;
	m_TexState.m_TransDel = -1.f / 5.f;
}

void CMyD3DApplication::GetCompCosineEffParams()

⌨️ 快捷键说明

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