• <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>

            天行健 君子當(dāng)自強(qiáng)而不息

            高級紋理映射技術(shù)(8)

            三維場景中的物體不僅受光照影響,而且受周圍環(huán)境的影響,可以映射出周圍環(huán)境的圖像,環(huán)境映射就是模擬物體光滑表面映射周圍環(huán)境的一種技術(shù)。實際上,就是將一幅包含物體周圍環(huán)境場景的紋理貼到物體表面上,這樣就可以在一定程度上模擬出物體對周圍環(huán)境的映射,而無須使用像光線跟蹤算法這種復(fù)雜的計算技術(shù)。

            在圖形工業(yè)中有多種環(huán)境映射,下面介紹兩種最常用的環(huán)境映射:立方體環(huán)境映射和球形環(huán)境映射。

             

            立方體環(huán)境映射

            立方體環(huán)境映射圖有時又稱為立方體映射圖,是一組包含物體周圍環(huán)境圖像的紋理貼圖,好像物體在立方體的中心。立方體環(huán)境圖的每個面覆蓋水平和垂直方向上各90度視角區(qū)域,一共6個面,各面的方向如下圖所示:

            立方體環(huán)境映射圖的6個面布局如下圖所示:

             

            球形環(huán)境映射

            球形環(huán)境映射圖(或稱為球形映射圖)和立方體環(huán)境映射圖類似,也是一幅包含周圍場景圖像的特殊紋理。和立方體環(huán)境映射圖不同的是,球形環(huán)境映射圖不直接代表物體周圍的環(huán)境。球形映射圖就好像通過魚眼(fish-eye)凸透鏡觀察到的景象一樣,是一個物體周圍環(huán)境360度全方位視域的三維表現(xiàn)。

            首先使用一張球形的背景圖作為環(huán)境映射紋理,如下所示:

            接著在回調(diào)函數(shù)OnCreateDevice()中加載紋理和網(wǎng)格模型:

            V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L"spheremap.bmp", &g_env_texture));
            ID3DXBuffer* material_buffer;
            V_RETURN(D3DXLoadMeshFromXW(L"teapot.x", D3DXMESH_MANAGED, pd3dDevice, NULL, &material_buffer, NULL, 
            &g_num_materials, &g_mesh));
            D3DXMATERIAL* xmaterials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();
            g_mesh_materials = new D3DMATERIAL9[g_num_materials];
            for(DWORD i = 0; i < g_num_materials; i++)
            {
            g_mesh_materials[i] = xmaterials[i].MatD3D;
            	// .x file do not save ambient data, so set it here.
            g_mesh_materials[i].Ambient = g_mesh_materials[i].Diffuse;
            }
            material_buffer->Release();

            該網(wǎng)格模型的視覺效果如下所示:

            接著在回調(diào)函數(shù)OnResetDevice()中自動生成紋理坐標(biāo),并設(shè)置紋理階段顏色混合方法:

            // set texture color blend method, disalbe alpha blend.

            pd3dDevice->SetTexture(0, g_env_texture);
            pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
            pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
            pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
            pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

            pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_SPHEREMAP);
            pd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);

            最后在回調(diào)函數(shù)OnFrameRender()中渲染茶壺:

            // Clear the render target and the zbuffer 
            V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );
            // Render the scene
            if( SUCCEEDED( pd3dDevice->BeginScene() ) )
            {
            for(DWORD i = 0; i < g_num_materials; i++)
            {
            pd3dDevice->SetMaterial(&g_mesh_materials[i]);
            g_mesh->DrawSubset(i);
            }
            	RenderText();
            	V(g_button_dlg.OnRender(fElapsedTime));
                V( pd3dDevice->EndScene() );
            }

            運行效果圖:

            球形環(huán)境紋理映射的實現(xiàn)比較簡單,和普通紋理映射基本相同,唯一的區(qū)別是使用自動生成的球形紋理坐標(biāo)。

             

            主程序:

            #include "dxstdafx.h"
            #include 
            "resource.h"

            #pragma warning(disable : 
            4127 4995 4996)

            #define release_com(p)    do { if(p) { (p)->Release(); (p) = NULL; } } while(0)

            #define IDC_TOGGLE_FULLSCREEN        1
            #define IDC_TOGGLE_REF                2
            #define IDC_CHANGE_DEVICE            3

            const D3DXCOLOR FONT_COLOR(1.0f0.5f0.25f1.0f);

            ID3DXFont
            *                    g_font;
            ID3DXSprite
            *                g_text_sprite;
            bool                        g_show_help;

            CDXUTDialogResourceManager    g_dlg_resource_manager;
            CD3DSettingsDlg                g_settings_dlg;
            CDXUTDialog                    g_button_dlg;

            ID3DXMesh
            *                    g_mesh;
            D3DMATERIAL9
            *                g_mesh_materials;
            DWORD                        g_num_materials;
            IDirect3DTexture9
            *            g_env_texture;

            //--------------------------------------------------------------------------------------
            // Rejects any devices that aren't acceptable by returning false
            //--------------------------------------------------------------------------------------
            bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
                                              D3DFORMAT BackBufferFormat, 
            bool bWindowed, void* pUserContext )
            {
                
            // Typically want to skip backbuffer formats that don't support alpha blending

                IDirect3D9
            * pD3D = DXUTGetD3DObject(); 

                
            if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, 
                                D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
                    
            return false;

                
            return true;
            }


            //--------------------------------------------------------------------------------------
            // Before a device is created, modify the device settings as needed.
            //--------------------------------------------------------------------------------------
            bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
            {
                
            // If video card does not support hardware vertex processing, then uses sofaware vertex processing.
                if((pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0)
                    pDeviceSettings
            ->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

                
            static bool is_first_time = true;

                
            if(is_first_time)
                {
                    is_first_time 
            = false;

                    
            // if using reference device, then pop a warning message box.
                    if(pDeviceSettings->DeviceType == D3DDEVTYPE_REF)
                        DXUTDisplaySwitchingToREFWarning();
                }

                
            return true;
            }


            //--------------------------------------------------------------------------------------
            // Create any D3DPOOL_MANAGED resources here 
            //--------------------------------------------------------------------------------------
            HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, 
                                             
            const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
                                             
            void* pUserContext )
            {
                HRESULT    hr;

                V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice));
                V_RETURN(g_settings_dlg.OnCreateDevice(pd3dDevice));

                D3DXCreateFont(pd3dDevice, 
            180, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
                               DEFAULT_PITCH 
            | FF_DONTCARE, L"Arial"&g_font);

                V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L
            "spheremap.bmp"&g_env_texture));
                
                ID3DXBuffer
            * material_buffer;

                V_RETURN(D3DXLoadMeshFromXW(L
            "teapot.x", D3DXMESH_MANAGED, pd3dDevice, NULL, &material_buffer, NULL, 
                                            
            &g_num_materials, &g_mesh));

                D3DXMATERIAL
            * xmaterials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();
                g_mesh_materials 
            = new D3DMATERIAL9[g_num_materials];

                
            for(DWORD i = 0; i < g_num_materials; i++)
                {
                    g_mesh_materials[i] 
            = xmaterials[i].MatD3D;

                    
            // .x file do not save ambient data, so set it here.
                    g_mesh_materials[i].Ambient = g_mesh_materials[i].Diffuse; 
                }

                material_buffer
            ->Release();

                
            return S_OK;
            }


            //--------------------------------------------------------------------------------------
            // Create any D3DPOOL_DEFAULT resources here 
            //--------------------------------------------------------------------------------------
            HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
                                            
            const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
                                            
            void* pUserContext )
            {
                HRESULT hr;

                V_RETURN(g_dlg_resource_manager.OnResetDevice());
                V_RETURN(g_settings_dlg.OnResetDevice());
                V_RETURN(g_font
            ->OnResetDevice());
                V_RETURN(D3DXCreateSprite(pd3dDevice, 
            &g_text_sprite));

                
            // set dialog position and size

                g_button_dlg.SetLocation(pBackBufferSurfaceDesc
            ->Width - 1700);
                g_button_dlg.SetSize(
            170170);

                
            // setup view matrix

                D3DXMATRIX mat_view;
                D3DXVECTOR3 eye(
            0.0f300.0f220.0f);
                D3DXVECTOR3  at(
            0.0f0.0f,  50.0f);
                D3DXVECTOR3  up(
            0.0f0.0f,  1.0f);

                D3DXMatrixLookAtLH(
            &mat_view, &eye, &at, &up);
                pd3dDevice
            ->SetTransform(D3DTS_VIEW, &mat_view);

                
            // set projection matrix
                D3DXMATRIX mat_proj;
                
            float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;
                D3DXMatrixPerspectiveFovLH(
            &mat_proj, D3DX_PI/4, aspect, 1.0f1000.0f);
                pd3dDevice
            ->SetTransform(D3DTS_PROJECTION, &mat_proj);

                
            // setup material

                D3DMATERIAL9 material;
                ZeroMemory(
            &material, sizeof(D3DMATERIAL9));

                material.Diffuse.r 
            = material.Ambient.r = 1.0f;
                material.Diffuse.g 
            = material.Ambient.g = 1.0f;
                material.Diffuse.b 
            = material.Ambient.b = 1.0f;
                material.Diffuse.a 
            = material.Ambient.a = 1.0f;

                pd3dDevice
            ->SetMaterial(&material);

                
            // setup light

                D3DLIGHT9 light;
                ZeroMemory(
            &light, sizeof(D3DLIGHT9));

                light.Type       
            = D3DLIGHT_DIRECTIONAL;
                light.Diffuse.r  
            = 1.0f;
                light.Diffuse.g  
            = 1.0f;
                light.Diffuse.b  
            = 1.0f;
                light.Range      
            = 1000.0f;

                D3DXVECTOR3 light_dir(
            10.0f-10.0f-10.0f);        
                D3DXVec3Normalize((D3DXVECTOR3
            *&light.Direction, &light_dir);
                
                pd3dDevice
            ->SetLight(0&light);
                pd3dDevice
            ->LightEnable(0, TRUE);  

                pd3dDevice
            ->SetRenderState(D3DRS_AMBIENT, 0x00FF8080);

                
            // set texture color blend method, disalbe alpha blend.

                pd3dDevice
            ->SetTexture(0, g_env_texture);    
                pd3dDevice
            ->SetTextureStageState(0, D3DTSS_COLORARG1,        D3DTA_TEXTURE);    
                pd3dDevice
            ->SetTextureStageState(0, D3DTSS_COLORARG2,        D3DTA_DIFFUSE);    
                pd3dDevice
            ->SetTextureStageState(0, D3DTSS_COLOROP,            D3DTOP_MODULATE);    
                pd3dDevice
            ->SetTextureStageState(0, D3DTSS_ALPHAOP,            D3DTOP_DISABLE);
                
                pd3dDevice
            ->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX,            D3DTSS_TCI_SPHEREMAP);    
                pd3dDevice
            ->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS,    D3DTTFF_COUNT2);

                
            return S_OK;
            }

            //--------------------------------------------------------------------------------------
            // Release resources created in the OnResetDevice callback here 
            //--------------------------------------------------------------------------------------
            void CALLBACK OnLostDevice( void* pUserContext )
            {
                g_dlg_resource_manager.OnLostDevice();
                g_settings_dlg.OnLostDevice();
                g_font
            ->OnLostDevice();

                release_com(g_text_sprite);
            }


            //--------------------------------------------------------------------------------------
            // Release resources created in the OnCreateDevice callback here
            //--------------------------------------------------------------------------------------
            void CALLBACK OnDestroyDevice( void* pUserContext )
            {
                g_dlg_resource_manager.OnDestroyDevice();
                g_settings_dlg.OnDestroyDevice();    

                delete[] g_mesh_materials;
                g_mesh_materials 
            = NULL;

                release_com(g_font);
                release_com(g_mesh);
                release_com(g_env_texture);
            }

            //--------------------------------------------------------------------------------------
            // Handle updates to the scene
            //--------------------------------------------------------------------------------------
            void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
            {
            }

            //--------------------------------------------------------------------------------------
            // Render the helper information
            //--------------------------------------------------------------------------------------
            void RenderText()
            {
                CDXUTTextHelper text_helper(g_font, g_text_sprite, 
            20);
                
                text_helper.Begin();

                
            // show frame and device states
                text_helper.SetInsertionPos(55);
                text_helper.SetForegroundColor(FONT_COLOR);
                text_helper.DrawTextLine( DXUTGetFrameStats(
            true) );
                text_helper.DrawTextLine( DXUTGetDeviceStats() );

                
            // show helper information
                
                
            const D3DSURFACE_DESC* surface_desc = DXUTGetBackBufferSurfaceDesc();

                
            if(g_show_help)
                {
                    text_helper.SetInsertionPos(
            10, surface_desc->Height - 18 * 5);
                    text_helper.SetForegroundColor(FONT_COLOR);
                    text_helper.DrawTextLine(L
            "Controls (F1 to hide):");
                    
                    text_helper.SetInsertionPos(
            40, surface_desc->Height - 18 * 4);
                    text_helper.DrawTextLine(L
            "Quit: ESC");
                }
                
            else
                {
                    text_helper.SetInsertionPos(
            10, surface_desc->Height - 15 * 4);
                    text_helper.SetForegroundColor( D3DXCOLOR(
            1.0f1.0f1.0f1.0f) );
                    text_helper.DrawTextLine(L
            "Press F1 for help");
                }

                text_helper.End();
            }

            //--------------------------------------------------------------------------------------
            // Render the scene 
            //--------------------------------------------------------------------------------------
            void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
            {
                HRESULT hr;

                
            if(g_settings_dlg.IsActive())
                {
                    g_settings_dlg.OnRender(fElapsedTime);
                    
            return;
                }

                
            float time = timeGetTime() / 1000.0f;

                D3DXMATRIX mat_world;    
                D3DXMatrixRotationYawPitchRoll(
            &mat_world, time, time, time);
                pd3dDevice
            ->SetTransform(D3DTS_WORLD, &mat_world);

                
            // Clear the render target and the zbuffer 
                V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0000), 1.0f0) );

                
            // Render the scene
                if( SUCCEEDED( pd3dDevice->BeginScene() ) )
                {
                    
            for(DWORD i = 0; i < g_num_materials; i++)
                    {
                        pd3dDevice
            ->SetMaterial(&g_mesh_materials[i]);
                        g_mesh
            ->DrawSubset(i);
                    }

                    RenderText();

                    V(g_button_dlg.OnRender(fElapsedTime));

                    V( pd3dDevice
            ->EndScene() );
                }
            }


            //--------------------------------------------------------------------------------------
            // Handle messages to the application 
            //--------------------------------------------------------------------------------------
            LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
                                      
            bool* pbNoFurtherProcessing, void* pUserContext )
            {
                
            *pbNoFurtherProcessing = g_dlg_resource_manager.MsgProc(hWnd, uMsg, wParam, lParam);
                
            if(*pbNoFurtherProcessing)
                    
            return 0;

                
            if(g_settings_dlg.IsActive())
                {
                    g_settings_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
                    
            return 0;
                }

                
            *pbNoFurtherProcessing = g_button_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
                
            if(*pbNoFurtherProcessing)
                    
            return 0;

                
            return 0;
            }


            //--------------------------------------------------------------------------------------
            // Handle keybaord event
            //--------------------------------------------------------------------------------------
            void CALLBACK OnKeyboardProc(UINT charater, bool is_key_down, bool is_alt_down, void* user_context)
            {
                
            if(is_key_down)
                {
                    
            switch(charater)
                    {
                    
            case VK_F1:
                        g_show_help 
            = !g_show_help;
                        
            break;
                    }
                }
            }

            //--------------------------------------------------------------------------------------
            // Handle events for controls
            //--------------------------------------------------------------------------------------
            void CALLBACK OnGUIEvent(UINT eventint control_id, CDXUTControl* control, void* user_context)
            {
                
            switch(control_id)
                {
                
            case IDC_TOGGLE_FULLSCREEN:
                    DXUTToggleFullScreen();
                    
            break;

                
            case IDC_TOGGLE_REF:
                    DXUTToggleREF();
                    
            break;

                
            case IDC_CHANGE_DEVICE:
                    g_settings_dlg.SetActive(
            true);
                    
            break;
                }
            }

            //--------------------------------------------------------------------------------------
            // Initialize dialogs
            //--------------------------------------------------------------------------------------
            void InitDialogs()
            {
                g_settings_dlg.Init(
            &g_dlg_resource_manager);
                g_button_dlg.Init(
            &g_dlg_resource_manager);

                g_button_dlg.SetCallback(OnGUIEvent);

                
            int x = 35, y = 10, width = 125, height = 22;

                g_button_dlg.AddButton(IDC_TOGGLE_FULLSCREEN, L
            "Toggle full screen", x, y,         width, height);
                g_button_dlg.AddButton(IDC_TOGGLE_REF,          L
            "Toggle REF (F3)",     x, y += 24, width, height);
                g_button_dlg.AddButton(IDC_CHANGE_DEVICE,      L
            "Change device (F2)", x, y += 24, width, height, VK_F2);    
            }

            //--------------------------------------------------------------------------------------
            // Initialize everything and go into a render loop
            //--------------------------------------------------------------------------------------
            INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
            {
                
            // Enable run-time memory check for debug builds.
            #if defined(DEBUG) | defined(_DEBUG)
                _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF 
            | _CRTDBG_LEAK_CHECK_DF );
            #endif

                
            // Set the callback functions
                DXUTSetCallbackDeviceCreated( OnCreateDevice );
                DXUTSetCallbackDeviceReset( OnResetDevice );
                DXUTSetCallbackDeviceLost( OnLostDevice );
                DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
                DXUTSetCallbackMsgProc( MsgProc );
                DXUTSetCallbackFrameRender( OnFrameRender );
                DXUTSetCallbackFrameMove( OnFrameMove );
                DXUTSetCallbackKeyboard(OnKeyboardProc);
               
                
            // TODO: Perform any application-level initialization here
                InitDialogs();

                
            // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
                DXUTInit( truetruetrue ); // Parse the command line, handle the default hotkeys, and show msgboxes
                DXUTSetCursorSettings( truetrue ); // Show the cursor and clip it when in full screen
                DXUTCreateWindow( L"Spherical Environment Mapping" );
                DXUTCreateDevice( D3DADAPTER_DEFAULT, 
            true640480, IsDeviceAcceptable, ModifyDeviceSettings );

                
            // Start the render loop
                DXUTMainLoop();

                
            // TODO: Perform any application-level cleanup here

                
            return DXUTGetExitCode();
            }

             

            下載示例工程


            posted on 2008-05-21 14:35 lovedday 閱讀(2428) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評論

            久久91精品久久91综合| 香蕉久久AⅤ一区二区三区| 国产精品美女久久久m| 国产精品亚洲美女久久久| 香蕉99久久国产综合精品宅男自| 久久精品国产2020| 99久久精品免费看国产| 久久综合鬼色88久久精品综合自在自线噜噜| 久久久国产精华液| 国产精品日韩深夜福利久久| 久久久久亚洲AV无码观看| 久久精品国产影库免费看| 狠狠综合久久AV一区二区三区| 久久久九九有精品国产| 久久中文字幕人妻熟av女| 99久久免费国产精品| 久久香蕉超碰97国产精品| 久久人人青草97香蕉| 亚洲一区二区三区日本久久九| 无码人妻久久一区二区三区| 久久综合久久性久99毛片| A级毛片无码久久精品免费| 欧美精品久久久久久久自慰| 久久天天婷婷五月俺也去| 久久本道久久综合伊人| 亚洲午夜久久久精品影院 | 久久99精品久久久久久秒播| 久久综合给久久狠狠97色| 国产精品久久久香蕉| 中文字幕无码久久精品青草| 久久综合九色综合久99| 久久精品国产欧美日韩| 四虎国产永久免费久久| AV色综合久久天堂AV色综合在| 久久无码AV中文出轨人妻| 国产69精品久久久久99| 久久精品成人免费看| 国产精品一区二区久久| 精品久久久久久亚洲| 久久亚洲国产中v天仙www| 久久这里只精品国产99热|