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

📄 tex.cpp

📁 VC++ DEMO, used for the beginners and the amour
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            g_pTextureArg2 = NULL;
        g_bTex2Changed = false;
    }

    pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, g_dwTextureFactor );

    pd3dDevice->SetTexture( 0, g_pTextureArg0 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
	pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  g_wT0AddrU);
	pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  g_wT0AddrV);
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, g_wT0CArg1 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   g_wT0COp   );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, g_wT0CArg2 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, g_wT0AArg1 );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   g_wT0AOp   );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, g_wT0AArg2 );
	
    if( g_dwMaxTextureBlendStages > 1 )
    {
        pd3dDevice->SetTexture( 1, g_pTextureArg1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
		pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSU,  g_wT1AddrU);
		pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSV,  g_wT1AddrV);
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, g_wT1CArg1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   g_wT1COp   );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, g_wT1CArg2 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, g_wT1AArg1 );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   g_wT1AOp   );
        pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, g_wT1AArg2 );
    }
	
    if( g_dwMaxTextureBlendStages > 2)
    {
        pd3dDevice->SetTexture( 2, g_pTextureArg2 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 2 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
		pd3dDevice->SetTextureStageState( 2, D3DTSS_ADDRESSU,  g_wT2AddrU);
		pd3dDevice->SetTextureStageState( 2, D3DTSS_ADDRESSV,  g_wT2AddrV);
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, g_wT2CArg1 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP,   g_wT2COp   );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, g_wT2CArg2 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG1, g_wT2AArg1 );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,   g_wT2AOp   );
        pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG2, g_wT2AArg2 );
    }

    return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CreateTexture()
// Desc: ..........
//-----------------------------------------------------------------------------

HRESULT CreateTexture( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strTexture,
                               LPDIRECT3DTEXTURE8* ppTexture, D3DFORMAT d3dFormat )
{

    TCHAR strPath[MAX_PATH];
    FindMediaFile( strPath, strTexture );

    return D3DXCreateTextureFromFileEx( pd3dDevice, strPath, 
                D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, d3dFormat, 
                D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, 
                D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, 0, NULL, NULL, ppTexture );
}

//-----------------------------------------------------------------------------
// Name: FindMediaFile()
// Desc: .........
//-----------------------------------------------------------------------------
HRESULT FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
{
    HANDLE file;
    TCHAR strFullPath[1024];
    TCHAR *strShortName;
    DWORD cchPath;

    if( NULL==strFilename || NULL==strPath )
        return E_INVALIDARG;

    cchPath = GetFullPathName(strFilename, sizeof(strFullPath)/sizeof(TCHAR), strFullPath, &strShortName);
    if ((cchPath == 0) || (sizeof(strFullPath)/sizeof(TCHAR) <= cchPath))
        return E_FAIL;

    file = CreateFile( strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
                       OPEN_EXISTING, 0, NULL );
    if( INVALID_HANDLE_VALUE != file )
    {
        _tcscpy( strPath, strFullPath );
        CloseHandle( file );
        return S_OK;
    }
    
    file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL, 
                       OPEN_EXISTING, 0, NULL );
    if( INVALID_HANDLE_VALUE != file )
    {
        _tcscpy( strPath, strShortName );
        CloseHandle( file );
        return S_OK;
    }
	return E_FAIL;
}

//-----------------------------------------------------------------------------
// Name: SetTextureMaps()
// Desc: .........
//-----------------------------------------------------------------------------
void SetTextureMaps( const TCHAR* strTexture0, const TCHAR* strTexture1,
                         const TCHAR* strTexture2 )
{
    _tcscpy( g_strTexture0, strTexture0 );
    _tcscpy( g_strTexture1, strTexture1 );
    _tcscpy( g_strTexture2, strTexture2 );
}
////////////////////////////////////////////////////////////////////
//Construction.
////////////////////////////////////////////////////////////////////

bool CreateDemoDialog( ProcessInfo_t * ProcessInfoPtr )
{
	WNDCLASS wc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = DLGWINDOWEXTRA;
	wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hInstance = ProcessInfoPtr->hInst;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = ProcessInfoPtr->szClassName.c_str();
	wc.lpszMenuName = NULL;
	if ( !RegisterClass(&wc) ) return false;

	HWND hWnd( CreateDialog(wc.hInstance,MAKEINTRESOURCE(IDD_FORMVIEW),GetDesktopWindow(),NULL) );

	if ( hWnd )
	{	
		Initialise3DEntironment( GetDlgItem(hWnd,IDC_VIEW3D) );

		SetWindowText( hWnd,ProcessInfoPtr->szDlgTitle.c_str() );
		ShowWindow(hWnd,ProcessInfoPtr->nCmdShow);
		UpdateWindow( hWnd );
		Initialise3DElement();
		InitDialogUIControls( hWnd);

		return true;
	}
	return false;
}
LRESULT CALLBACK WndProc( HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
	switch( Message )
	{
		HANDLE_MSG(hWnd,WM_DESTROY,Main_OnDestroy);
		HANDLE_MSG(hWnd,WM_COMMAND,Main_OnCommand);
	default:
		return DefWindowProc( hWnd,Message,wParam,lParam);
	}
	return 0;

}
HRESULT Initialise3DEntironment( HWND hWnd )
{
	RECT rcClientRect;
	WORD Width,Height;
	GetClientRect(hWnd,&rcClientRect);
	Width = rcClientRect.right - rcClientRect.left;
	Height = rcClientRect.bottom - rcClientRect.top;
	g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
	
	D3DDISPLAYMODE d3ddm;
	g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
	
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp, &g_pd3dDevice);
		
	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
	g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	
	D3DXMATRIX matWorld;
	D3DXMatrixIdentity(&matWorld);
	g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
	
	D3DCAPS8 d3dCaps;
	g_pd3dDevice->GetDeviceCaps(&d3dCaps);
	g_dwMaxTextureBlendStages = d3dCaps.MaxTextureBlendStages;

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0, 0, -15),
		&D3DXVECTOR3(0, 0, 0),
		&D3DXVECTOR3(0, 1, 0));
	g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
	
	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, (float)Width/Height, 1.0f, 100.0f);
	g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
	return S_OK;
}
HRESULT Initialise3DElement()
{
	// 建立表面VB
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4 * sizeof(FACEVERTEX),
        0, D3DFVF_FACEVERTEX, D3DPOOL_MANAGED, &g_pVB ) ) )
    {
        return E_FAIL;
    }
	
	Main_OnChangeAddrMax();
	Main_OnChangeTex();
	return S_OK;
}
HRESULT Render()
{
	HRESULT hr;
	D3DXMATRIX matWorld;
	g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
		D3DCOLOR_RGBA(0,64,128,255), 1.0f, 0);
	
    // Begin the scene
    if( FAILED( g_pd3dDevice->BeginScene() ) )
        return E_FAIL;
	D3DXMatrixIdentity(&matWorld);
	D3DXMatrixRotationY(&matWorld,timeGetTime()/1000.0f);
	g_pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld);
    // Setup the texture stages' state
    SetTextureStageStatesForRendering( g_pd3dDevice );
	
    // Validate the device. This checks to see if the texture stage states we
    // set up are valid for the device. If so, render the object.
    DWORD dwNumPasses;
    if( SUCCEEDED( hr = g_pd3dDevice->ValidateDevice( &dwNumPasses ) ) )
    {
        // Render the multi-textured object
        g_pd3dDevice->SetVertexShader( D3DFVF_FACEVERTEX );
        g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(FACEVERTEX) );
        g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
    }
	g_pd3dDevice->EndScene();	
	g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
	return S_OK;
}

void Shutdown()
{
	// 释放VB
	SAFE_RELEASE(g_pVB);
	SAFE_RELEASE(g_pTextureArg0);
	SAFE_RELEASE(g_pTextureArg1);
	SAFE_RELEASE(g_pTextureArg2);
	SAFE_RELEASE(g_pd3dDevice);
	SAFE_RELEASE(g_pD3D);
}


⌨️ 快捷键说明

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