C++ Exercises(十一)

本文介绍如何使用Direct3D创建窗口并渲染一个简单的三角形。包括初始化Direct3D环境、设置呈现参数、创建设备对象及顶点缓冲区,并展示基本的渲染流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif#include <d3d9.h>
None.gif
#pragma warning( disable : 4996 ) // disable deprecated warning 
None.gif#include 
<strsafe.h>
None.gif
#pragma warning( default : 4996 ) 
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
// Global variables
None.gif
//-----------------------------------------------------------------------------
None.gif
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
None.gif
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
// Name: InitD3D()
None.gif
// Desc: Initializes Direct3D
None.gif
//-----------------------------------------------------------------------------
None.gif
HRESULT InitD3D( HWND hWnd )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// Create the D3D object, which is needed to create the D3DDevice.
InBlock.gif
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
InBlock.gif        
return E_FAIL;
InBlock.gif
InBlock.gif    
// Set up the structure used to create the D3DDevice. Most parameters are
InBlock.gif    
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
InBlock.gif    
// window, and then set the SwapEffect to "discard", which is the most
InBlock.gif    
// efficient method of presenting the back buffer to the display.  And 
InBlock.gif    
// we request a back buffer format that matches the current desktop display 
InBlock.gif    
// format.
InBlock.gif
    D3DPRESENT_PARAMETERS d3dpp; 
InBlock.gif    ZeroMemory( 
&d3dpp, sizeof(d3dpp) );
InBlock.gif    d3dpp.Windowed 
= TRUE;
InBlock.gif    d3dpp.SwapEffect 
= D3DSWAPEFFECT_DISCARD;
InBlock.gif    d3dpp.BackBufferFormat 
= D3DFMT_UNKNOWN;
InBlock.gif
InBlock.gif    
// Create the Direct3D device. Here we are using the default adapter (most
InBlock.gif    
// systems only have one, unless they have multiple graphics hardware cards
InBlock.gif    
// installed) and requesting the HAL (which is saying we want the hardware
InBlock.gif    
// device rather than a software one). Software vertex processing is 
InBlock.gif    
// specified since we know it will work on all cards. On cards that support 
InBlock.gif    
// hardware vertex processing, though, we would see a big performance gain 
InBlock.gif    
// by specifying hardware vertex processing.
InBlock.gif
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
InBlock.gif                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
InBlock.gif                                      
&d3dpp, &g_pd3dDevice ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return E_FAIL;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// Device state would normally be set here
InBlock.gif

InBlock.gif    
return S_OK;
ExpandedBlockEnd.gif}

None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
// Name: Cleanup()
None.gif
// Desc: Releases all previously initialized objects
None.gif
//-----------------------------------------------------------------------------
None.gif
VOID Cleanup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
//释放D3D设备对象
InBlock.gif
    if( g_pd3dDevice != NULL) 
InBlock.gif        g_pd3dDevice
->Release();
InBlock.gif
InBlock.gif    
//释放D3D对象
InBlock.gif
    if( g_pD3D != NULL)
InBlock.gif        g_pD3D
->Release();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
// Name: Render()
None.gif
// Desc: Draws the scene
None.gif
//-----------------------------------------------------------------------------
None.gif
VOID Render()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if( NULL == g_pd3dDevice )
InBlock.gif        
return;
InBlock.gif    
// Clear the backbuffer to a blue color
InBlock.gif
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f0 );
InBlock.gif    
// Begin the scene
InBlock.gif
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Rendering of scene objects can happen here
InBlock.gif        
// End the scene
InBlock.gif
        g_pd3dDevice->EndScene();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// Present the backbuffer contents to the display
InBlock.gif
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
ExpandedBlockEnd.gif}

None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
// Name: MsgProc()
None.gif
// Desc: The window's message handler
None.gif
//-----------------------------------------------------------------------------
None.gif
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
switch( msg )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
case WM_DESTROY:
InBlock.gif            Cleanup();
InBlock.gif            PostQuitMessage( 
0 );
InBlock.gif            
return 0;
InBlock.gif        
case WM_PAINT:
InBlock.gif            Render();
InBlock.gif            ValidateRect( hWnd, NULL );
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return DefWindowProc( hWnd, msg, wParam, lParam );
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//-----------------------------------------------------------------------------
None.gif
// Name: WinMain()
None.gif
// Desc: The application's entry point
None.gif
//-----------------------------------------------------------------------------
None.gif
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// Register the window class
ExpandedSubBlockStart.gifContractedSubBlock.gif
    WNDCLASSEX wc = dot.gifsizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L0L
InBlock.gif                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
ExpandedSubBlockEnd.gif                      
"D3D Tutorial", NULL }
;
InBlock.gif    RegisterClassEx( 
&wc );
InBlock.gif    
// Create the application's window
InBlock.gif
    HWND hWnd = CreateWindow( "D3D Tutorial""D3D Tutorial 01: CreateDevice"
InBlock.gif                              WS_OVERLAPPEDWINDOW, 
100100300300,
InBlock.gif                              NULL, NULL, wc.hInstance, NULL );
InBlock.gif    
// Initialize Direct3D
InBlock.gif
    if( SUCCEEDED( InitD3D( hWnd ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif//初始化成功
InBlock.gif        
// Show the window
InBlock.gif
        ShowWindow( hWnd, SW_SHOWDEFAULT );
InBlock.gif        UpdateWindow( hWnd );
InBlock.gif        
// Enter the message loop
InBlock.gif
        MSG msg; 
InBlock.gif        
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{//消息处理
InBlock.gif
                if(msg.message==WM_QUIT)
InBlock.gif                    
break;
InBlock.gif                TranslateMessage(
&msg);
InBlock.gif                DispatchMessage(
&msg);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{//空闲时绘制
InBlock.gif
                Render();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    UnregisterClass( 
"D3D Tutorial", wc.hInstance );
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif

2

None.gif
None.gif#include 
<d3d9.h>
None.gif
#pragma warning( disable : 4996 ) // disable deprecated warning 
None.gif#include 
<strsafe.h>
None.gif
#pragma warning( default : 4996 ) 
None.gif
None.gifLPDIRECT3D9             g_pD3D       
= NULL; // Used to create the D3DDevice
None.gif
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
None.gif
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices 顶点缓冲区
None.gif
None.gif
// A structure for our custom vertex type
None.gif
struct CUSTOMVERTEX
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    FLOAT x, y, z, rhw; 
// The transformed position for the vertex
InBlock.gif
    DWORD color;        // The vertex color
ExpandedBlockEnd.gif
}
;
None.gif
None.gif
// Our custom FVF, which describes our custom vertex structure
None.gif
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
None.gif
None.gifHRESULT InitD3D( HWND hWnd )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// Create the D3D object.
InBlock.gif
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
InBlock.gif        
return E_FAIL;
InBlock.gif
InBlock.gif    
// Set up the structure used to create the D3DDevice
InBlock.gif
    D3DPRESENT_PARAMETERS d3dpp;
InBlock.gif    ZeroMemory( 
&d3dpp, sizeof(d3dpp) );
InBlock.gif    d3dpp.Windowed 
= TRUE;
InBlock.gif    d3dpp.SwapEffect 
= D3DSWAPEFFECT_DISCARD;
InBlock.gif    d3dpp.BackBufferFormat 
= D3DFMT_UNKNOWN;
InBlock.gif
InBlock.gif    
// Create the D3DDevice
InBlock.gif
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
InBlock.gif                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
InBlock.gif                                      
&d3dpp, &g_pd3dDevice ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return E_FAIL;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Device state would normally be set here
InBlock.gif

InBlock.gif    
return S_OK;
ExpandedBlockEnd.gif}

None.gif
None.gifHRESULT InitVB()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// Initialize three vertices for rendering a triangle
InBlock.gif
    CUSTOMVERTEX vertices[] =
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif150.0f,  50.0f0.5f1.0f0xffff0000, }// x, y, z, rhw, color
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif250.0f250.0f0.5f1.0f0xff00ff00, },
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{  50.0f250.0f0.5f1.0f0xff00ffff, },
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
InBlock.gif    
// Create the vertex buffer. Here we are allocating enough memory
InBlock.gif    
// (from the default pool) to hold all our 3 custom vertices. We also
InBlock.gif    
// specify the FVF, so the vertex buffer knows what data it contains.
InBlock.gif
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
InBlock.gif                                                  
0, D3DFVF_CUSTOMVERTEX,
InBlock.gif                                                  D3DPOOL_DEFAULT, 
&g_pVB, NULL ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return E_FAIL;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
InBlock.gif    
// gain access to the vertices. This mechanism is required becuase vertex
InBlock.gif    
// buffers may be in device memory.
InBlock.gif
    VOID* pVertices;
InBlock.gif    
if( FAILED( g_pVB->Lock( 0sizeof(vertices), (void**)&pVertices, 0 ) ) )
InBlock.gif        
return E_FAIL;
InBlock.gif    memcpy( pVertices, vertices, 
sizeof(vertices) );
InBlock.gif    g_pVB
->Unlock();
InBlock.gif
InBlock.gif    
return S_OK;
ExpandedBlockEnd.gif}

None.gifVOID Cleanup()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if( g_pVB != NULL )        
InBlock.gif        g_pVB
->Release();
InBlock.gif
InBlock.gif    
if( g_pd3dDevice != NULL ) 
InBlock.gif        g_pd3dDevice
->Release();
InBlock.gif
InBlock.gif    
if( g_pD3D != NULL )       
InBlock.gif        g_pD3D
->Release();
ExpandedBlockEnd.gif}

None.gif
None.gifVOID Render()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// Clear the backbuffer to a blue color
InBlock.gif
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f0 );
InBlock.gif
InBlock.gif    
// Begin the scene
InBlock.gif
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Draw the triangles in the vertex buffer.
InBlock.gif
        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0sizeof(CUSTOMVERTEX) );
InBlock.gif        g_pd3dDevice
->SetFVF( D3DFVF_CUSTOMVERTEX );
InBlock.gif        g_pd3dDevice
->DrawPrimitive( D3DPT_TRIANGLELIST, 01 );
InBlock.gif
InBlock.gif        
// End the scene
InBlock.gif
        g_pd3dDevice->EndScene();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Present the backbuffer contents to the display
InBlock.gif
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
ExpandedBlockEnd.gif}

None.gif
None.gifLRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
switch( msg )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
case WM_DESTROY:
InBlock.gif            Cleanup();
InBlock.gif            PostQuitMessage( 
0 );
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return DefWindowProc( hWnd, msg, wParam, lParam );
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gifINT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// Register the window class
ExpandedSubBlockStart.gifContractedSubBlock.gif
    WNDCLASSEX wc = dot.gifsizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L0L
InBlock.gif                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
ExpandedSubBlockEnd.gif                      
"D3D Tutorial", NULL }
;
InBlock.gif    RegisterClassEx( 
&wc );
InBlock.gif    
// Create the application's window
InBlock.gif
    HWND hWnd = CreateWindow( "D3D Tutorial""D3D Tutorial 01: CreateDevice"
InBlock.gif                              WS_OVERLAPPEDWINDOW, 
100100300300,
InBlock.gif                              NULL, NULL, wc.hInstance, NULL );
InBlock.gif   
// Initialize Direct3D
InBlock.gif
    if( SUCCEEDED( InitD3D( hWnd ) ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif//初始化成功
InBlock.gif        
// Create the vertex buffer
InBlock.gif
        if( SUCCEEDED( InitVB() ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Show the window
InBlock.gif
            ShowWindow( hWnd, SW_SHOWDEFAULT );
InBlock.gif            UpdateWindow( hWnd );
InBlock.gif            
// Enter the message loop
InBlock.gif
            MSG msg; 
InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//消息处理
InBlock.gif
                    if(msg.message==WM_QUIT)
InBlock.gif                        
break;
InBlock.gif                    TranslateMessage(
&msg);
InBlock.gif                    DispatchMessage(
&msg);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{//空闲时绘制
InBlock.gif
                    Render();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    UnregisterClass( 
"D3D Tutorial", wc.hInstance );
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值