zoukankan      html  css  js  c++  java
  • Scenario 2: Drawing Two Triangles with Indexing

      1 //-----------------------------------------------------------------------------
      2 // File: Vertices.cpp
      3 //
      4 // Desc: In this tutorial, we are rendering some vertices. This introduces the
      5 //       concept of the vertex buffer, a Direct3D object used to store
      6 //       vertices. Vertices can be defined any way we want by defining a
      7 //       custom structure and a custom FVF (flexible vertex format). In this
      8 //       tutorial, we are using vertices that are transformed (meaning they
      9 //       are already in 2D window coordinates) and lit (meaning we are not
     10 //       using Direct3D lighting, but are supplying our own colors).
     11 //
     12 // Copyright (c) Microsoft Corporation. All rights reserved.
     13 //-----------------------------------------------------------------------------
     14 #include <d3dx9.h>
     15 #pragma warning( disable : 4996 ) // disable deprecated warning 
     16 #include <strsafe.h>
     17 #pragma warning( default : 4996 )
     18 
     19 
     20 
     21 
     22 //-----------------------------------------------------------------------------
     23 // Global variables
     24 //-----------------------------------------------------------------------------
     25 LPDIRECT3D9             g_pD3D = NULL; // Used to create the D3DDevice
     26 LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
     27 LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
     28 LPDIRECT3DINDEXBUFFER9  g_pIB = NULL;
     29 
     30 // A structure for our custom vertex type
     31 struct CUSTOMVERTEX
     32 {
     33     FLOAT x, y, z, rhw; // The transformed position for the vertex
     34     DWORD color;        // The vertex color
     35 };
     36 
     37 // Our custom FVF, which describes our custom vertex structure
     38 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
     39 
     40 
     41 
     42 
     43 //-----------------------------------------------------------------------------
     44 // Name: InitD3D()
     45 // Desc: Initializes Direct3D
     46 //-----------------------------------------------------------------------------
     47 HRESULT InitD3D( HWND hWnd )
     48 {
     49     // Create the D3D object.
     50     if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
     51         return E_FAIL;
     52 
     53     // Set up the structure used to create the D3DDevice
     54     D3DPRESENT_PARAMETERS d3dpp;
     55     ZeroMemory( &d3dpp, sizeof( d3dpp ) );
     56     d3dpp.Windowed = TRUE;
     57     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
     58     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
     59 
     60     // Create the D3DDevice
     61     if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
     62         D3DCREATE_SOFTWARE_VERTEXPROCESSING,
     63         &d3dpp, &g_pd3dDevice ) ) )
     64     {
     65         return E_FAIL;
     66     }
     67 
     68     // Device state would normally be set here
     69 
     70     return S_OK;
     71 }
     72 
     73 
     74 
     75 
     76 //-----------------------------------------------------------------------------
     77 // Name: InitVB()
     78 // Desc: Creates a vertex buffer and fills it with our vertices. The vertex
     79 //       buffer is basically just a chuck of memory that holds vertices. After
     80 //       creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
     81 //       also uses index buffers. The special thing about vertex and index
     82 //       buffers is that they can be created in device memory, allowing some
     83 //       cards to process them in hardware, resulting in a dramatic
     84 //       performance gain.
     85 //-----------------------------------------------------------------------------
     86 HRESULT InitVB()
     87 {
     88     // rhw空间下的坐标为:原点在窗口左上角,Y轴向下,X轴向右,Z轴不变
     89     CUSTOMVERTEX vertices[] =
     90     {
     91         {  50.0f150.0f0.5f1.0f0xff00ffff, },
     92         {  50.0f,  50.0f0.5f1.0f0xffff0000, }, // x, y, z, rhw, color
     93         { 150.0f,  50.0f0.5f1.0f0xff00ff00, },
     94         { 150.0f150.0f0.5f1.0f0xff00ffff, },
     95     };
     96 
     97     // Create the vertex buffer. Here we are allocating enough memory
     98     // (from the default pool) to hold all our 3 custom vertices. We also
     99     // specify the FVF, so the vertex buffer knows what data it contains.
    100     if( FAILED( g_pd3dDevice->CreateVertexBuffer( sizeof( vertices ),
    101         0, D3DFVF_CUSTOMVERTEX,
    102         D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    103     {
    104         return E_FAIL;
    105     }
    106 
    107     // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
    108     // gain access to the vertices. This mechanism is required becuase vertex
    109     // buffers may be in device memory.
    110     VOID* pVertices;
    111     if( FAILED( g_pVB->Lock( 0sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
    112         return E_FAIL;
    113     memcpy( pVertices, vertices, sizeof( vertices ) );
    114     g_pVB->Unlock();
    115 
    116     return S_OK;
    117 }
    118 
    119 
    120 HRESULT InitIB()
    121 {
    122     int fIB[] = {0,1,2,3,0,2};
    123     if (FAILED(g_pd3dDevice->CreateIndexBuffer(sizeof(fIB),D3DUSAGE_DYNAMIC,D3DFMT_INDEX32,
    124         D3DPOOL_DEFAULT,&g_pIB,NULL)))
    125     {
    126         return E_FAIL;
    127     }
    128 
    129     void *pIndexs;
    130     if (FAILED(g_pIB->Lock(0sizeof(fIB),&pIndexs,D3DLOCK_DISCARD)))
    131     {
    132         return E_FAIL;
    133     }
    134     memcpy(pIndexs, fIB, sizeof(fIB));
    135     g_pIB->Unlock();
    136 
    137     return S_OK;
    138 }
    139 
    140 //-----------------------------------------------------------------------------
    141 // Name: Cleanup()
    142 // Desc: Releases all previously initialized objects
    143 //-----------------------------------------------------------------------------
    144 VOID Cleanup()
    145 {
    146     if( g_pVB != NULL )
    147         g_pVB->Release();
    148 
    149     if( g_pd3dDevice != NULL )
    150         g_pd3dDevice->Release();
    151 
    152     if( g_pD3D != NULL )
    153         g_pD3D->Release();
    154 }
    155 
    156 
    157 
    158 
    159 //-----------------------------------------------------------------------------
    160 // Name: Render()
    161 // Desc: Draws the scene
    162 //-----------------------------------------------------------------------------
    163 VOID Render()
    164 {
    165     // Clear the backbuffer to a blue color
    166     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 00255 ), 1.0f0 );
    167 
    168     // Begin the scene
    169     if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    170     {
    171         // Draw the triangles in the vertex buffer. This is broken into a few
    172         // steps. We are passing the vertices down a "stream", so first we need
    173         // to specify the source of that stream, which is our vertex buffer. Then
    174         // we need to let D3D know what vertex shader to use. Full, custom vertex
    175         // shaders are an advanced topic, but in most cases the vertex shader is
    176         // just the FVF, so that D3D knows what type of vertices we are dealing
    177         // with. Finally, we call DrawPrimitive() which does the actual rendering
    178         // of our geometry (in this case, just one triangle).
    179         g_pd3dDevice->SetStreamSource( 0, g_pVB, 0sizeof( CUSTOMVERTEX ) );
    180         g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    181         g_pd3dDevice->SetIndices(g_pIB);
    182         g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 00,4,0,2 );
    183 
    184         // End the scene
    185         g_pd3dDevice->EndScene();
    186     }
    187 
    188     // Present the backbuffer contents to the display
    189     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
    190 }
    191 
    192 
    193 
    194 
    195 //-----------------------------------------------------------------------------
    196 // Name: MsgProc()
    197 // Desc: The window's message handler
    198 //-----------------------------------------------------------------------------
    199 LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    200 {
    201     switch( msg )
    202     {
    203     case WM_DESTROY:
    204         Cleanup();
    205         PostQuitMessage( 0 );
    206         return 0;
    207     }
    208 
    209     return DefWindowProc( hWnd, msg, wParam, lParam );
    210 }
    211 
    212 
    213 
    214 
    215 //-----------------------------------------------------------------------------
    216 // Name: wWinMain()
    217 // Desc: The application's entry point
    218 //-----------------------------------------------------------------------------
    219 INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
    220 {
    221     // Register the window class
    222     WNDCLASSEX wc =
    223     {
    224         sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L0L,
    225         GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
    226         L"D3D Tutorial", NULL
    227     };
    228     RegisterClassEx( &wc );
    229 
    230     // Create the application's window
    231     HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 02: Vertices",
    232         WS_OVERLAPPEDWINDOW, 100100300300,
    233         NULL, NULL, wc.hInstance, NULL );
    234 
    235     // Initialize Direct3D
    236     if( SUCCEEDED( InitD3D( hWnd ) ) )
    237     {
    238         // Create the vertex buffer
    239         if( SUCCEEDED( InitVB() ) && SUCCEEDED(InitIB()))
    240         {
    241             // Show the window
    242             ShowWindow( hWnd, SW_SHOWDEFAULT );
    243             UpdateWindow( hWnd );
    244 
    245             // Enter the message loop
    246             MSG msg;
    247             ZeroMemory( &msg, sizeof( msg ) );
    248             while( msg.message != WM_QUIT )
    249             {
    250                 if( PeekMessage( &msg, NULL, 0U0U, PM_REMOVE ) )
    251                 {
    252                     TranslateMessage( &msg );
    253                     DispatchMessage( &msg );
    254                 }
    255                 else
    256                     Render();
    257             }
    258         }
    259     }
    260 
    261     UnregisterClass( L"D3D Tutorial", wc.hInstance );
    262     return 0;
    263 }
    264 

     结果:

  • 相关阅读:
    JS实现双击内容变为可编辑状态
    DataTables 1.10.x与1.9.x参数名对照表
    div里 datapicker显示异常的情况之一
    javascript 添加行,删除行,datepicker获取当前日期和上一个月日期并设置格式,笔记
    jQuery 实现添加表格行,删除行,调用日期控件
    c# webbrowser控件内核版本强制修改
    【.net】获取网页CDM的下载链接的地址
    MySQL 密码增强插件
    跟我一起学extjs5(42--单个模块的数据新增方式)
    给 Android 开发人员的 RxJava 具体解释
  • 原文地址:https://www.cnblogs.com/lancidie/p/1621655.html
Copyright © 2011-2022 走看看