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

            天行健 君子當自強而不息

            創建游戲內核(8)【OO改良版】

             

            本篇是創建游戲內核(7)【OO改良版】的續篇,關于該內核的細節說明請參閱創建游戲內核(8)


            接口:

            class LIGHT
            {
            public:
                LIGHT();

                D3DLIGHT9* get_light();

                
            void init();

                
            void set_type(D3DLIGHTTYPE type);

                
            void move(float x_pos, float y_pos, float z_pos);
                
            void move_rel(float x_pos, float y_pos, float z_pos);
                
            void get_pos(float* x_pos, float* y_pos, float* z_pos);

                
            void point(float x_from, float y_from, float z_from, float x_at, float y_at, float z_at);
                
            void get_direction(float* x_dir, float* y_dir, float* z_dir);

                
            void set_diffuse(unsigned char red, unsigned char green, unsigned char blue);
                
            void get_diffuse(unsigned char* red, unsigned char* green, unsigned char* blue);

                
            void set_specular(unsigned char red, unsigned char green, unsigned char blue);
                
            void get_specular(unsigned char* red, unsigned char* green, unsigned char* blue);
                
                
            void set_ambient(unsigned char red, unsigned char green, unsigned char blue);
                
            void get_ambient(unsigned char* red, unsigned char* green, unsigned char* blue);

                
            void  set_range(float range);
                
            float get_range();

                
            void  set_falloff(float falloff);
                
            float get_falloff();

                
            void  set_attenuation_0(float attenuation);
                
            float get_attenuation_0();

                
            void  set_attenuation_1(float attenuation);
                
            float get_attenuation_1();    

                
            void  set_attenuation_2(float attenuation);
                
            float get_attenuation_2();

                
            void  set_theta(float theta);
                
            float get_theta();

                
            void  set_phi(float phi);
                
            float get_phi();

            private:
                D3DLIGHT9 m_light;
            };

            typedef LIGHT* LIGHT_PTR;
             

            實現:

            //---------------------------------------------------------------------------
            // Constructor, zero member data.
            //---------------------------------------------------------------------------
            LIGHT::LIGHT()
            {
                ZeroMemory(
            thissizeof(*this));    
            }

            //---------------------------------------------------------------------------
            // Set light type as point light, place light source at origin, and set diffuse 
            // color and ambient color as white, set range and attenuation for light.
            //---------------------------------------------------------------------------
            void LIGHT::init()
            {
                set_type(D3DLIGHT_POINT);
                
                set_diffuse(255, 255, 255);
                set_ambient(255, 255, 255);

                set_range(1000.0);
                set_attenuation_0(1.0);

                move(0.0, 0.0, 0.0);
            }

            //---------------------------------------------------------------------------
            // set light type (D3DLIGHT_POINT, D3DLIGHT_SPOT, D3DLIGHT_DIRECTIONAL).
            //---------------------------------------------------------------------------
            void LIGHT::set_type(D3DLIGHTTYPE type)
            {
                m_light.Type = type;
            }

            //---------------------------------------------------------------------------
            // move light source to specified position.
            //---------------------------------------------------------------------------
            void LIGHT::move(float x_pos, float y_pos, float z_pos)
            {
                m_light.Position.x = x_pos;
                m_light.Position.y = y_pos;
                m_light.Position.z = z_pos;
            }

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

            //---------------------------------------------------------------------------
            // Get current position.
            //---------------------------------------------------------------------------
            void LIGHT::get_pos(float *x_pos, float *y_pos, float *z_pos)
            {
                
            if(x_pos != NULL)
                    *x_pos = m_light.Position.x;

                
            if(y_pos != NULL)
                    *y_pos = m_light.Position.y;

                
            if(z_pos != NULL)
                    *z_pos = m_light.Position.z;
            }

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

                
            // calculate vetor between angles
                m_light.Direction.x = x_at - x_from;
                m_light.Direction.y = y_at - y_from;
                m_light.Direction.z = z_at - z_from;
            }

            //---------------------------------------------------------------------------
            // Get the direction of current light source.
            //---------------------------------------------------------------------------
            void LIGHT::get_direction(float *x_dir, float *y_dir, float *z_dir)
            {
                
            if(x_dir != NULL)
                    *x_dir = m_light.Direction.x;

                
            if(y_dir != NULL)
                    *y_dir = m_light.Direction.y;

                
            if(z_dir != NULL)
                    *z_dir = m_light.Direction.z;
            }

            //---------------------------------------------------------------------------
            // set diffuse color of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_diffuse(unsigned char red, unsigned char green, unsigned char blue)
            {
                m_light.Diffuse.r = red / 255.0f;
                m_light.Diffuse.g = green / 255.0f;
                m_light.Diffuse.b = blue / 255.0f;
            }

            //---------------------------------------------------------------------------
            // Get diffuse color of light source.
            //---------------------------------------------------------------------------
            void LIGHT::get_diffuse(unsigned char* red, unsigned char* green, unsigned char* blue)
            {
                
            if(red != NULL)
                    *red = (unsigned 
            char)(255.0f * m_light.Diffuse.r);

                
            if(green != NULL)
                    *green = (unsigned 
            char)(255.0f * m_light.Diffuse.g);

                
            if(blue != NULL)
                    *blue = (unsigned 
            char)(255.0f * m_light.Diffuse.b);
            }

            //---------------------------------------------------------------------------
            // set specular color of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_specular(unsigned char red, unsigned char green, unsigned char blue)
            {
                m_light.Specular.r = red / 255.0f;
                m_light.Specular.g = green / 255.0f;
                m_light.Specular.b = blue / 255.0f;
            }

            //---------------------------------------------------------------------------
            // Get specular color of light source.
            //---------------------------------------------------------------------------
            void LIGHT::get_specular(unsigned char* red, unsigned char* green, unsigned char* blue)
            {
                
            if(red != NULL)
                    *red = (unsigned 
            char)(255.0f * m_light.Specular.r);

                
            if(green != NULL)
                    *green = (unsigned 
            char)(255.0f * m_light.Specular.g);

                
            if(blue != NULL)
                    *blue = (unsigned 
            char)(255.0f * m_light.Specular.b);
            }

            //---------------------------------------------------------------------------
            // set ambient color of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_ambient(unsigned char red, unsigned char green, unsigned char blue)
            {
                m_light.Ambient.r = red / 255.0f;
                m_light.Ambient.g = green / 255.0f;
                m_light.Ambient.b = blue / 255.0f;
            }

            //---------------------------------------------------------------------------
            // Get ambient color of light source.
            //---------------------------------------------------------------------------
            void LIGHT::get_ambient(unsigned char* red, unsigned char* green, unsigned char* blue)
            {
                
            if(red != NULL)
                    *red = (unsigned 
            char)(255.0f * m_light.Ambient.r);

                
            if(green != NULL)
                    *green = (unsigned 
            char)(255.0f * m_light.Ambient.g);

                
            if(blue != NULL)
                    *blue = (unsigned 
            char)(255.0f * m_light.Ambient.b);
            }

            //---------------------------------------------------------------------------
            // set the range of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_range(float range)
            {
                m_light.Range = range;
            }

            //---------------------------------------------------------------------------
            // Get the range of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_range()
            {
                
            return m_light.Range;
            }

            //---------------------------------------------------------------------------
            // set the fallof of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_falloff(float falloff)
            {
                m_light.Falloff = falloff;
            }

            //---------------------------------------------------------------------------
            // Get the fallof of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_falloff()
            {
                
            return m_light.Falloff;
            }

            //---------------------------------------------------------------------------
            // set attenuation 0 of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_attenuation_0(float attenuation)
            {
                m_light.Attenuation0 = attenuation;
            }

            //---------------------------------------------------------------------------
            // Get attenuation 0 of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_attenuation_0()
            {
                
            return m_light.Attenuation0;
            }

            //---------------------------------------------------------------------------
            // set attenuation 1 of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_attenuation_1(float attenuation)
            {
                m_light.Attenuation1 = attenuation;
            }

            //---------------------------------------------------------------------------
            // Get attenuation 1 of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_attenuation_1()
            {
                
            return m_light.Attenuation1;
            }

            //---------------------------------------------------------------------------
            // set attenuation 2 of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_attenuation_2(float attenuation)
            {
                m_light.Attenuation2 = attenuation;
            }

            //---------------------------------------------------------------------------
            // Get attenuation 2 of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_attenuation_2()
            {
                
            return m_light.Attenuation2;
            }

            //---------------------------------------------------------------------------
            // set angle thera of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_theta(float theta)
            {
                m_light.Theta = theta;
            }

            //---------------------------------------------------------------------------
            // Get angle thera of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_theta()
            {
                
            return m_light.Theta;
            }

            //---------------------------------------------------------------------------
            // set angle phi of light source.
            //---------------------------------------------------------------------------
            void LIGHT::set_phi(float phi)
            {
                m_light.Phi = phi;
            }

            //---------------------------------------------------------------------------
            // Get angle phi of light source.
            //---------------------------------------------------------------------------
            float LIGHT::get_phi()
            {
                
            return m_light.Phi;
            }

            //---------------------------------------------------------------------------
            // Get light source.
            //---------------------------------------------------------------------------
            D3DLIGHT9* LIGHT::get_light()
            {
                
            return &m_light;
            }
             

            測試代碼:
            /*****************************************************************************
            PURPOSE:
                Test for class LIGHT.
            *****************************************************************************/


            #include "core_common.h"
            #include "core_framework.h"
            #include "core_graphics.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)

            class APP : public FRAMEWORK
            {
            public:
                BOOL 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_client_width(g_hwnd), get_client_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, &m_vertex_buffer, NULL);     

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

                    memcpy(_vertex_ptr, _verts, 
            sizeof(_verts));

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

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

                    LIGHT _light;    

                    
            // set light
                    _light.init();
                    _light.set_type(D3DLIGHT_POINT);
                    _light.set_diffuse(128, 128, 0);
                    _light.set_range(1000.0);
                    _light.set_attenuation_0(0.5);

                    _light.move(300.0, 0.0, -600.0);
                    
                    
            // attach light to d3d device and enable _light
                    g_d3d_device->SetLight(0, _light.get_light());    

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

                    
            return TRUE;
                }

                BOOL frame()
                {
                    clear_display_buffer(D3DCOLOR_RGBA(0, 0, 0, 0));

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

                        
            // create and set the world transformation matrix
                        // 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, m_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;
                }

                BOOL shutdown()
                {
                    release_com(m_vertex_buffer);
                    release_com(g_d3d_device);
                    release_com(g_d3d);

                    
            return TRUE;
                }
                
            private:
                IDirect3DVertexBuffer9* m_vertex_buffer;
            };

            //--------------------------------------------------------------------------------
            // Main function, routine entry.
            //--------------------------------------------------------------------------------
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
            {
                APP app;

                
            if(! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
                    
            return -1;
                
                app.run();

                
            return 0;
            }
             

            posted on 2007-10-06 20:01 lovedday 閱讀(251) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            国产精品久久久久久影院| 国产精品99精品久久免费| 久久精品中文字幕一区| 大美女久久久久久j久久| 久久精品国产只有精品66| 亚洲国产成人久久综合区| 久久久久久人妻无码| 91精品国产91热久久久久福利| 精品欧美一区二区三区久久久| 欧美一区二区久久精品| 久久久久人妻精品一区二区三区| segui久久国产精品| 伊人色综合久久天天人手人婷| 国产精品久久久久久久| 日韩久久无码免费毛片软件| 久久精品国产清高在天天线| 亚州日韩精品专区久久久| 亚洲综合久久综合激情久久| 久久久久99这里有精品10| 久久久久久狠狠丁香| 亚洲av成人无码久久精品| 久久久久无码精品| 国产精品激情综合久久| 久久超碰97人人做人人爱| 一本久久免费视频| 亚洲成av人片不卡无码久久| 99久久综合狠狠综合久久| 久久亚洲私人国产精品| 一本色综合久久| 久久99这里只有精品国产| 久久人人爽人爽人人爽av| 久久精品中文字幕有码| 亚洲国产精品久久久久网站| 久久久久久久综合日本亚洲| 精品综合久久久久久888蜜芽| 偷偷做久久久久网站| 国产精品久久久久a影院| 亚洲国产精品狼友中文久久久| 精品免费久久久久国产一区| 久久久久亚洲av成人无码电影| 精品一久久香蕉国产线看播放|