• <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)而不息

            創(chuàng)建游戲內(nèi)核(8)【C風(fēng)格版】

             

            本篇是創(chuàng)建游戲內(nèi)核(7)【C風(fēng)格版】的續(xù)篇,關(guān)于該內(nèi)核的細(xì)節(jié)說明請(qǐng)參考創(chuàng)建游戲內(nèi)核(8)

             

            接口:

            void move_light(D3DLIGHT9_PTR light, 
                            
            float x_pos, float y_pos, float z_pos);

            void move_light_rel(D3DLIGHT9_PTR light, 
                                
            float x_pos, float y_pos, float z_pos);

            void get_light_pos(const D3DLIGHT9_PTR light, 
                               float_ref x_pos, float_ref y_pos, float_ref z_pos);

            void point_light(D3DLIGHT9_PTR light,
                             
            float x_from, float y_from, float z_from,
                             
            float x_at,   float y_at,   float z_at);

            void get_light_direction(const D3DLIGHT9_PTR light, 
                                     float_ref x_dir, float_ref y_dir, float_ref z_dir);

            void set_light_diffuse(D3DLIGHT9_PTR light, 
                                   uchar red, uchar green, uchar blue);

            void get_light_diffuse(const D3DLIGHT9_PTR light, 
                                   uchar_ref red, uchar_ref green, uchar_ref blue);

            void set_light_specular(D3DLIGHT9_PTR light,
                                   uchar red, uchar green, uchar blue);

            void get_light_specular(const D3DLIGHT9_PTR light,
                                    uchar_ref red, uchar_ref green, uchar_ref blue);

            void set_light_ambient(D3DLIGHT9_PTR light,
                                   uchar red, uchar green, uchar blue);

            void get_light_ambient(const D3DLIGHT9_PTR light,
                                   uchar_ref red, uchar_ref green, uchar_ref blue);

            實(shí)現(xiàn):
            //---------------------------------------------------------------------------
            // move light source to specified position.
            //---------------------------------------------------------------------------
            void move_light(D3DLIGHT9_PTR light, 
                            
            float x_pos, float y_pos, float z_pos)
            {
                light->Position.x = x_pos;
                light->Position.y = y_pos;
                light->Position.z = z_pos;
            }

            //---------------------------------------------------------------------------
            // move light source to specified position which is relative to current position.
            //---------------------------------------------------------------------------
            void move_light_rel(D3DLIGHT9_PTR light, 
                                
            float x_pos, float y_pos, float z_pos)
            {
                light->Position.x += x_pos;
                light->Position.y += y_pos;
                light->Position.z += z_pos;
            }

            //---------------------------------------------------------------------------
            // Get current position.
            //---------------------------------------------------------------------------
            void get_light_pos(const D3DLIGHT9_PTR light, 
                               float_ref x_pos, float_ref y_pos, float_ref z_pos)
            {    
                x_pos = light->Position.x;
                y_pos = light->Position.y;    
                z_pos = light->Position.z;
            }

            //---------------------------------------------------------------------------
            // move light source to specified position and pointer it to specified direction.
            //---------------------------------------------------------------------------
            void point_light(D3DLIGHT9_PTR light,
                             
            float x_from, float y_from, float z_from,
                             
            float x_at,   float y_at,   float z_at)
            {
                
            // move the light
                move_light(light, x_from, y_from, z_from);

                
            // calculate vector between angles
                light->Direction.x = x_at - x_from;
                light->Direction.y = y_at - y_from;
                light->Direction.z = z_at - z_from;
            }

            //---------------------------------------------------------------------------
            // Get the direction of current light source.
            //---------------------------------------------------------------------------
            void get_light_direction(const D3DLIGHT9_PTR light, 
                                     float_ref x_dir, float_ref y_dir, float_ref z_dir)
            {    
                x_dir = light->Direction.x;    
                y_dir = light->Direction.y;   
                z_dir = light->Direction.z;        
            }

            //---------------------------------------------------------------------------
            // set diffuse color of light source.
            //---------------------------------------------------------------------------
            void set_light_diffuse(D3DLIGHT9_PTR light, 
                                   uchar red, uchar green, uchar blue)
            {
                light->Diffuse.r = red / 255.0f;
                light->Diffuse.g = green / 255.0f;
                light->Diffuse.b = blue / 255.0f;
            }

            //---------------------------------------------------------------------------
            // Get diffuse color of light source.
            //---------------------------------------------------------------------------
            void get_light_diffuse(const D3DLIGHT9_PTR light, 
                                   uchar_ref red, uchar_ref green, uchar_ref blue)
            {    
                red   = (uchar)(255.0f * light->Diffuse.r);   
                green = (uchar)(255.0f * light->Diffuse.g);    
                blue  = (uchar)(255.0f * light->Diffuse.b);
            }

            //---------------------------------------------------------------------------
            // set specular color of light source.
            //---------------------------------------------------------------------------
            void set_light_specular(D3DLIGHT9_PTR light,
                                    uchar red, uchar green, uchar blue)
            {
                light->Specular.r = red / 255.0f;
                light->Specular.g = green / 255.0f;
                light->Specular.b = blue / 255.0f;
            }

            //---------------------------------------------------------------------------
            // Get specular color of light source.
            //---------------------------------------------------------------------------
            void get_light_specular(const D3DLIGHT9_PTR light,
                                    uchar_ref red, uchar_ref green, uchar_ref blue)
            {    
                red   = (uchar)(255.0f * light->Specular.r);   
                green = (uchar)(255.0f * light->Specular.g);    
                blue  = (uchar)(255.0f * light->Specular.b);
            }

            //---------------------------------------------------------------------------
            // set ambient color of light source.
            //---------------------------------------------------------------------------
            void set_light_ambient(D3DLIGHT9_PTR light,
                                   uchar red, uchar green, uchar blue)
            {
                light->Ambient.r = red / 255.0f;
                light->Ambient.g = green / 255.0f;
                light->Ambient.b = blue / 255.0f;
            }

            //---------------------------------------------------------------------------
            // Get ambient color of light source.
            //---------------------------------------------------------------------------
            void get_light_ambient(const D3DLIGHT9_PTR light,
                                   uchar_ref red, uchar_ref green, uchar_ref blue)
            {    
                red   = (uchar)(255.0f * light->Ambient.r);   
                green = (uchar)(255.0f * light->Ambient.g);    
                blue  = (uchar)(255.0f * light->Ambient.b);
            }

            測試代碼:
            /***********************************************************************************
            PURPOSE:
                Test D3D light function.
            ***********************************************************************************/


            #include "core_common.h"
            #include "core_framework.h"
            #include "core_graphics.h"
            #include "core_tool.h"

            // The 3D vertex format and descriptor
            typedef struct
            {
                
            float x, y, z;      // 3D coordinates    
                float nx, ny, nz;   // normals
                D3DCOLOR diffuse;   // color
            } VERTEX;

            #define VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)

            IDirect3DVertexBuffer9* g_vertex_buffer;

            //--------------------------------------------------------------------------------
            // Initialize data for game.
            //--------------------------------------------------------------------------------
            BOOL game_init()
            {
                
            // initialize vertex data
                VERTEX verts[] = {
                    { -100.0f,  100.0f, -100.0f, 0.0f,0.0f,-1.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    {  100.0f,  100.0f, -100.0f, 0.0f,0.0f,-1.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    { -100.0f, -100.0f, -100.0f, 0.0f,0.0f,-1.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    {  100.0f, -100.0f, -100.0f, 0.0f,0.0f,-1.0f, D3DCOLOR_RGBA(255,255,255,255) },

                    {  100.0f,  100.0f, -100.0f, 1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    {  100.0f,  100.0f,  100.0f, 1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    {  100.0f, -100.0f, -100.0f, 1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    {  100.0f, -100.0f,  100.0f, 1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },

                    {  100.0f,  100.0f,  100.0f, 0.0f,0.0f,1.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    { -100.0f,  100.0f,  100.0f, 0.0f,0.0f,1.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    {  100.0f, -100.0f,  100.0f, 0.0f,0.0f,1.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    { -100.0f, -100.0f,  100.0f, 0.0f,0.0f,1.0f, D3DCOLOR_RGBA(255,255,255,255) },

                    { -100.0f,  100.0f,  100.0f, -1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    { -100.0f,  100.0f, -100.0f, -1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    { -100.0f, -100.0f,  100.0f, -1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) },
                    { -100.0f, -100.0f, -100.0f, -1.0f,0.0f,0.0f, D3DCOLOR_RGBA(255,255,255,255) }
                }; 

                
            // Create Direct3D and Direct3DDevice object
                if(! create_display(g_hwnd, get_window_width(g_hwnd), get_window_height(g_hwnd), 16, TRUE, FALSE))
                    
            return FALSE;

                
            // builds a left-handed perspective projection matrix based on a field of view
                set_perspective(D3DX_PI/4.0, 1.33333f, 1.0f, 1000.0f);

                D3DXMATRIX mat_view;

                
            // create and set the view matrix
                D3DXMatrixLookAtLH(&mat_view, 
                                   &D3DXVECTOR3(0.0f, 0.0f, -500.0f),
                                   &D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
                                   &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

                g_d3d_device->SetTransform(D3DTS_VIEW, &mat_view);

                BYTE* vertex_ptr;

                
            // create the vertex buffer and set data
                g_d3d_device->CreateVertexBuffer(sizeof(verts), 0, VERTEX_FVF, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);     

                
            // locks a range of vertex data and obtains a pointer to the vertex buffer memory
                g_vertex_buffer->Lock(0, 0, (void**)&vertex_ptr, 0);

                memcpy(vertex_ptr, verts, 
            sizeof(verts));

                
            // unlocks vertex data
                g_vertex_buffer->Unlock();

                
            // enable d3d lighting
                g_d3d_device->SetRenderState(D3DRS_LIGHTING, TRUE); 

                D3DLIGHT9 light;    

                memset(&light, 0, 
            sizeof(light));

                
            // set light property           
                
                light.Type         = D3DLIGHT_POINT;
                light.Range        = 1000.0f;
                light.Attenuation0 = 0.5;
                
                set_light_diffuse(&light, 128, 128, 0);
                move_light(&light, 300.0, 0.0, -600.0);

                
            // attach light to d3d device and enable light
                g_d3d_device->SetLight(0, &light);    

                
            // enale this light
                g_d3d_device->LightEnable(0, TRUE);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Render every game frame.
            //--------------------------------------------------------------------------------
            BOOL game_frame()
            {
                clear_display_buffer(D3DCOLOR_RGBA(0, 0, 0, 0));

                
            if(SUCCEEDED(g_d3d_device->BeginScene()))
                {
                    D3DXMATRIX mat_world;

                    
            // rotate object along y-axis
                    D3DXMatrixRotationY(&mat_world, (float) (timeGetTime() / 1000.0));
                    g_d3d_device->SetTransform(D3DTS_WORLD, &mat_world);

                    
            // set the vertex stream, shader, and texture.

                    // binds a vertex buffer to a device data stream
                    g_d3d_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(VERTEX));

                    
            // set the current vertex stream declation
                    g_d3d_device->SetFVF(VERTEX_FVF);

                    
            // renders a sequence of noindexed, geometric primitives of the specified type from the current set
                    // of data input stream.
                    for(short i = 0; i < 4; i++)
                        g_d3d_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2);
                    
                    g_d3d_device->EndScene();
                }

                present_display();

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Release all game resources.
            //--------------------------------------------------------------------------------
            BOOL game_shutdown()
            {
                release_com(g_vertex_buffer);
                
                destroy_display();

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Main function, routine entry.
            //--------------------------------------------------------------------------------
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
            {    
                DWORD client_width  = 640;
                DWORD client_height = 480;
                DWORD x_pos = (get_screen_width()  - client_width) / 2;
                DWORD y_pos = (get_screen_height() - client_height) / 4;

                
            if(! build_window(inst, "light class", "light test", WS_OVERLAPPEDWINDOW,
                                  x_pos, y_pos, client_width, client_height))
                {
                    
            return -1;
                }
                
                run_game(game_init, game_frame, game_shutdown);
                
                
            return 0;
            }
             

            posted on 2007-10-25 22:01 lovedday 閱讀(278) 評(píng)論(0)  編輯 收藏 引用


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


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

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

            搜索

            最新評(píng)論

            av午夜福利一片免费看久久| 精品一区二区久久| 久久SE精品一区二区| 合区精品久久久中文字幕一区| 麻豆国内精品久久久久久| 国产精品久久久久免费a∨| 亚洲AV无码久久精品狠狠爱浪潮| 国产午夜福利精品久久2021| 精品久久久久中文字| 亚洲中文字幕无码一久久区| 66精品综合久久久久久久| 思思久久99热只有频精品66 | 伊人久久综在合线亚洲2019 | 欧美久久综合性欧美| 亚洲人成网站999久久久综合| 99久久精品费精品国产一区二区| 久久久精品视频免费观看| 久久精品国产亚洲av高清漫画| 国产精品美女久久久久av爽| 嫩草伊人久久精品少妇AV| 久久丝袜精品中文字幕| 99久久精品国产毛片| 久久66热人妻偷产精品9| 奇米影视7777久久精品人人爽 | 久久精品国产秦先生| 狠狠色噜噜色狠狠狠综合久久| 久久99热这里只有精品国产| 狠狠色丁香久久婷婷综| 奇米综合四色77777久久| 综合人妻久久一区二区精品| 久久人人爽人人爽人人片AV麻豆| 国内精品久久久久久久久| 伊人热人久久中文字幕| 亚洲国产精品久久66| 久久中文娱乐网| 色综合久久天天综合| 国产69精品久久久久777| 久久久久99精品成人片直播| 精品乱码久久久久久久| 亚洲精品无码久久久久sm| 精品久久亚洲中文无码|