• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 505  文章 - 1034  trackbacks - 0
            <2009年3月>
            22232425262728
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234


            子曾經(jīng)曰過(guò):編程無(wú)他,唯手熟爾!

            常用鏈接

            留言簿(94)

            隨筆分類(649)

            隨筆檔案(505)

            相冊(cè)

            BCB

            Crytek

            • crymod
            • Crytek's Offical Modding Portal

            Game Industry

            OGRE

            other

            Programmers

            Qt

            WOW Stuff

            搜索

            •  

            積分與排名

            • 積分 - 911304
            • 排名 - 14

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            術(shù)語(yǔ):
            rotation period 旋轉(zhuǎn)周期
            radians              弧度



            //-----------------------------------------------------------------------------
            // File: Matrices.cpp
            //
            // Desc: Now that we know how to create a device and render some 2D vertices,
            //       this tutorial goes the next step and renders 3D geometry. To deal with
            //       3D geometry we need to introduce the use of 4x4 matrices to transform
            //       the geometry with translations, rotations, scaling, and setting up our
            //       camera.
            //
            //       Geometry is defined in model space. We can move it (translation),
            //       rotate it (rotation), or stretch it (scaling) using a world transform.
            //       The geometry is then said to be in world space. Next, we need to
            //       position the camera, or eye point, somewhere to look at the geometry.
            //       Another transform, via the view matrix, is used, to position and
            //       rotate our view. With the geometry then in view space, our last
            //       transform is the projection transform, which "projects" the 3D scene
            //       into our 2D viewport.
            //
            //       Note that in this tutorial, we are introducing the use of D3DX, which
            //       is a set of helper utilities for D3D. In this case, we are using some
            //       of D3DX's useful matrix initialization functions. To use D3DX, simply
            //       include <d3dx9.h> and link with d3dx9.lib.
            //
            // Copyright (c) Microsoft Corporation. All rights reserved.
            //-----------------------------------------------------------------------------
            #include <Windows.h>
            #include 
            <mmsystem.h>
            #include 
            <d3dx9.h>
            #pragma warning( disable : 
            4996 ) // disable deprecated warning 
            #include <strsafe.h>
            #pragma warning( 
            default : 4996 ) 




            //-----------------------------------------------------------------------------
            // Global variables
            //-----------------------------------------------------------------------------
            LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
            LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
            LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices

            // A structure for our custom vertex type
            struct CUSTOMVERTEX
            {
                FLOAT x, y, z;      
            // The untransformed, 3D position for the vertex
                DWORD color;        // The vertex color
            };

            // Our custom FVF, which describes our custom vertex structure
            #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)




            //-----------------------------------------------------------------------------
            // Name: InitD3D()
            // Desc: Initializes Direct3D
            //-----------------------------------------------------------------------------
            HRESULT InitD3D( HWND hWnd )
            {
                
            // Create the D3D object.
                if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
                    
            return E_FAIL;

                
            // Set up the structure used to create the D3DDevice
                D3DPRESENT_PARAMETERS d3dpp;
                ZeroMemory( 
            &d3dpp, sizeof(d3dpp) );
                d3dpp.Windowed 
            = TRUE;
                d3dpp.SwapEffect 
            = D3DSWAPEFFECT_DISCARD;
                d3dpp.BackBufferFormat 
            = D3DFMT_UNKNOWN;

                
            // Create the D3DDevice
                if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                                  
            &d3dpp, &g_pd3dDevice ) ) )
                {
                    
            return E_FAIL;
                }

                
            // Turn off culling, so we see the front and back of the triangle
                
            // 關(guān)閉剔除,以便三角形的前后都能被我們看到
                g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

                
            // Turn off D3D lighting, since we are providing our own vertex colors
                
            // 關(guān)閉D3D光照,因?yàn)槲覀兲峁┪覀冏约旱捻旤c(diǎn)顏色
                g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

                
            return S_OK;
            }




            //-----------------------------------------------------------------------------
            // Name: InitGeometry()
            // Desc: Creates the scene geometry
            //-----------------------------------------------------------------------------
            HRESULT InitGeometry()
            {
                
            // Initialize three vertices for rendering a triangle
                CUSTOMVERTEX g_Vertices[] =
                {
                    { 
            -1.0f,-1.0f0.0f0xffff0000, },
                    {  
            1.0f,-1.0f0.0f0xff0000ff, },
                    {  
            0.0f1.0f0.0f0xffffffff, },
                };

                
            // Create the vertex buffer.
                if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                                              
            0, D3DFVF_CUSTOMVERTEX,
                                                              D3DPOOL_DEFAULT, 
            &g_pVB, NULL ) ) )
                {
                    
            return E_FAIL;
                }

                
            // Fill the vertex buffer.
                VOID* pVertices;
                
            if( FAILED( g_pVB->Lock( 0sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
                    
            return E_FAIL;
                memcpy( pVertices, g_Vertices, 
            sizeof(g_Vertices) );
                g_pVB
            ->Unlock();

                
            return S_OK;
            }




            //-----------------------------------------------------------------------------
            // Name: Cleanup()
            // Desc: Releases all previously initialized objects
            //-----------------------------------------------------------------------------
            VOID Cleanup()
            {
                
            if( g_pVB != NULL )
                    g_pVB
            ->Release();

                
            if( g_pd3dDevice != NULL )
                    g_pd3dDevice
            ->Release();

                
            if( g_pD3D != NULL )
                    g_pD3D
            ->Release();
            }

            下面是最關(guān)鍵的函數(shù):
            //-----------------------------------------------------------------------------
            // Name: SetupMatrices()
            // Desc: Sets up the world, view, and projection transform matrices.
            //-----------------------------------------------------------------------------
            VOID SetupMatrices()
            {
                
            // For our world matrix, we will just rotate the object about the y-axis.
                
            // 物件只圍繞y軸旋轉(zhuǎn)
                D3DXMATRIXA16 matWorld;

                
            // Set up the rotation matrix to generate 1 full rotation (2*PI radians) every 1000 ms. 
                
            // 配置旋轉(zhuǎn)矩陣為每1000ms一整圈(2*PI 弧度)
                
            // To avoid the loss of precision inherent in very high floating point numbers,
                
            // 為了避免高浮點(diǎn)數(shù)固有的精度損失,
                
            // the system time is modulated by the rotation period before conversion to a radian angle.
                
            // 系統(tǒng)時(shí)間被旋轉(zhuǎn)周期求模,在它被轉(zhuǎn)化成弧度角前
                UINT  iTime  = timeGetTime() % 1000;
                FLOAT fAngle 
            = iTime * (2.0f * D3DX_PI) / 1000.0f;
                D3DXMatrixRotationY( 
            &matWorld, fAngle );
                g_pd3dDevice
            ->SetTransform( D3DTS_WORLD, &matWorld );
            設(shè)置視圖矩陣,看圖:



                // Set up our view matrix. 
                
            // 設(shè)置視圖矩陣
                
            // A view matrix can be defined given an eye point,
                
            // a point to lookat, and a direction for which way is up. Here, we set the
                
            // eye five units back along the z-axis and up three units, look at the
                
            // origin, and define "up" to be in the y-direction.
                D3DXVECTOR3 vEyePt( 0.0f3.0f,-5.0f );
                D3DXVECTOR3 vLookatPt( 
            0.0f0.0f0.0f );
                D3DXVECTOR3 vUpVec( 
            0.0f1.0f0.0f );
                D3DXMATRIXA16 matView;
                D3DXMatrixLookAtLH( 
            &matView, &vEyePt, &vLookatPt, &vUpVec );
                g_pd3dDevice
            ->SetTransform( D3DTS_VIEW, &matView );
            投影矩陣:
                // For the projection matrix, we set up a perspective transform (which
                
            // transforms geometry from 3D view space to 2D viewport space, with
                
            // a perspective divide making objects smaller in the distance). 
                
            // 投影矩陣,我們配置一個(gè)透視轉(zhuǎn)化(把圖形從3D視圖空間轉(zhuǎn)化到2D視口空間,透視劃分使得遠(yuǎn)的物件小)
                
            // To build a perpsective transform, we need the field of view (1/4 pi is common),
                
            // the aspect ratio, and the near and far clipping planes (which define at
                
            // what distances geometry should be no longer be rendered).
                
            // 構(gòu)建透視轉(zhuǎn)化,四個(gè)參數(shù)(1)the field of view (2)the aspect ratio (3)the near clipping plane (4)the far clipping plane
                D3DXMATRIXA16 matProj;
                D3DXMatrixPerspectiveFovLH( 
            &matProj, D3DX_PI/41.0f1.0f100.0f );
                g_pd3dDevice
            ->SetTransform( D3DTS_PROJECTION, &matProj );
            }

            //-----------------------------------------------------------------------------
            // Name: Render()
            // Desc: Draws the scene
            //-----------------------------------------------------------------------------
            VOID Render()
            {
                
            // Clear the backbuffer to a black color
                g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f0 );

                
            // Begin the scene
                if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
                {
                    
            // Setup the world, view, and projection matrices
                    SetupMatrices();

                    
            // Render the vertex buffer contents
                    g_pd3dDevice->SetStreamSource( 0, g_pVB, 0sizeof(CUSTOMVERTEX) );
                    g_pd3dDevice
            ->SetFVF( D3DFVF_CUSTOMVERTEX );
                    g_pd3dDevice
            ->DrawPrimitive( D3DPT_TRIANGLESTRIP, 01 );

                    
            // End the scene
                    g_pd3dDevice->EndScene();
                }

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




            //-----------------------------------------------------------------------------
            // Name: MsgProc()
            // Desc: The window's message handler
            //-----------------------------------------------------------------------------
            LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
            {
                
            switch( msg )
                {
                    
            case WM_DESTROY:
                        Cleanup();
                        PostQuitMessage( 
            0 );
                        
            return 0;
                }

                
            return DefWindowProc( hWnd, msg, wParam, lParam );
            }




            //-----------------------------------------------------------------------------
            // Name: WinMain()
            // Desc: The application's entry point
            //-----------------------------------------------------------------------------
            INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
            {
                
            // Register the window class
                WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L0L,
                                  GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                                  
            "D3D Tutorial", NULL };
                RegisterClassEx( 
            &wc );

                
            // Create the application's window
                HWND hWnd = CreateWindow( "D3D Tutorial""D3D Tutorial 03: Matrices",
                                          WS_OVERLAPPEDWINDOW, 
            100100256256,
                                          NULL, NULL, wc.hInstance, NULL );

                
            // Initialize Direct3D
                if( SUCCEEDED( InitD3D( hWnd ) ) )
                {
                    
            // Create the scene geometry
                    if( SUCCEEDED( InitGeometry() ) )
                    {
                        
            // Show the window
                        ShowWindow( hWnd, SW_SHOWDEFAULT );
                        UpdateWindow( hWnd );

                        
            // Enter the message loop
                        MSG msg;
                        ZeroMemory( 
            &msg, sizeof(msg) );
                        
            while( msg.message!=WM_QUIT )
                        {
                            
            if( PeekMessage( &msg, NULL, 0U0U, PM_REMOVE ) )
                            {
                                TranslateMessage( 
            &msg );
                                DispatchMessage( 
            &msg );
                            }
                            
            else
                                Render();
                        }
                    }
                }

                UnregisterClass( 
            "D3D Tutorial", wc.hInstance );
                
            return 0;
            }
            posted on 2007-02-15 23:05 七星重劍 閱讀(942) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Game Graphics
            久久亚洲国产午夜精品理论片 | 超级碰碰碰碰97久久久久| 99久久香蕉国产线看观香| 欧美亚洲国产精品久久| 久久精品国产亚洲AV香蕉| 亚洲αv久久久噜噜噜噜噜| 久久国产亚洲高清观看| 亚洲国产精品久久久久久| 久久久久亚洲精品无码网址| 久久久精品国产免大香伊| 久久久久亚洲AV成人片| 精品久久久久久久久久久久久久久| 欧美激情精品久久久久久久九九九 | 久久影院综合精品| 久久精品一区二区国产| 中文字幕无码av激情不卡久久| 久久久久久精品无码人妻| 亚洲精品国产美女久久久| 日韩精品久久久久久| 欧美久久亚洲精品| 久久男人Av资源网站无码软件| 久久99精品久久久久久水蜜桃| 久久人人爽人人爽人人片AV麻烦| 久久久国产精品亚洲一区| 久久久精品日本一区二区三区 | 色综合久久夜色精品国产| 国内精品久久九九国产精品| 日本欧美国产精品第一页久久| 99精品久久精品| 人妻无码精品久久亚瑟影视| AAA级久久久精品无码区| 亚洲国产精品一区二区久久hs | 久久国产精品国语对白| 亚洲va中文字幕无码久久| 欧美一级久久久久久久大片 | 人人狠狠综合久久亚洲高清| 国产V综合V亚洲欧美久久| 久久精品天天中文字幕人妻| 女人高潮久久久叫人喷水| 国产精品无码久久四虎| 国产日产久久高清欧美一区|