随笔 - 1,  文章 - 0,  评论 - 0,  阅读 - 15

vertex.h

复制代码
 1 #ifndef __vertexH__
 2 #define __vertexH__
 3 
 4 struct Vertex
 5 {
 6     Vertex(){}
 7     Vertex(float x, float y, float z,
 8         float nx, float ny, float nz,
 9         float u, float v)
10     {
11         _x = x; _y = y; _z = z;
12         _nx = nx; _ny = ny; _nz = nz;
13         _u = u; _v = v;
14     }
15     float _x, _y, _z;
16     float _nx, _ny, _nz;
17     float _u, _v;
18 };
19 
20 #define FVF_VERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
21 
22 #endif
View Code
复制代码

Cube.h

复制代码
 1 #ifndef __CubeH__
 2 #define __CubeH__
 3 
 4 
 5 #include<d3dx9.h>
 6 class Cube
 7 {
 8 public:
 9     Cube(void);
10     Cube(IDirect3DDevice9* device);
11     bool draw(D3DXMATRIX* world,D3DMATERIAL9* mtrl,IDirect3DTexture9* tex);
12     ~Cube(void);
13 private:
14     IDirect3DDevice9*  _device;
15     IDirect3DVertexBuffer9* _vb;
16     IDirect3DIndexBuffer9*  _ib;
17 
18 };
19 
20 #endif
View Code
复制代码

d3dUtility.h

复制代码
 1 //////////////////////////////////////////////////////////////////////////////////////////////////
 2 //
 3 // File: d3dUtility.h
 4 //
 5 // Author: Frank Luna (C) All Rights Reserved
 6 //
 7 // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
 8 //
 9 // Desc: Provides utility functions for simplifying common tasks.
10 //
11 //////////////////////////////////////////////////////////////////////////////////////////////////
12 
13 #ifndef __d3dUtilityH__
14 #define __d3dUtilityH__
15 
16 #include <d3dx9.h>
17 #include <string>
18 
19 namespace d3d
20 {
21     bool InitD3D(
22         HINSTANCE hInstance,       // [in] Application instance.
23         int width, int height,     // [in] Backbuffer dimensions.
24         bool windowed,             // [in] Windowed (true)or full screen (false).
25         D3DDEVTYPE deviceType,     // [in] HAL or REF
26         IDirect3DDevice9** device);// [out]The created device.
27 
28     int EnterMsgLoop(
29         bool (*ptr_display)(float timeDelta));
30 
31     LRESULT CALLBACK WndProc(
32         HWND hwnd,
33         UINT msg,
34         WPARAM wParam,
35         LPARAM lParam);
36 
37     template<class T> void Release(T t)
38     {
39         if( t )
40         {
41             t->Release();
42             t = 0;
43         }
44     }
45 
46     template<class T> void Delete(T t)
47     {
48         if( t )
49         {
50             delete t;
51             t = 0;
52         }
53     }
54 
55     const D3DXCOLOR RED(D3DCOLOR_XRGB(255,0,0));
56     const D3DXCOLOR WHITE(D3DCOLOR_XRGB(255,255,255));
57     const D3DXCOLOR BLACK(D3DCOLOR_XRGB(0,0,0));
58     const D3DXCOLOR GREEN(D3DCOLOR_XRGB(0,255,0));
59     const D3DXCOLOR BLUE(D3DCOLOR_XRGB(0,0,255));
60     const D3DXCOLOR YELLOW(D3DCOLOR_XRGB(255,255,0));
61     const D3DXCOLOR CYAN(D3DCOLOR_XRGB(0,255,255));
62     const D3DXCOLOR MAGENTA(D3DCOLOR_XRGB(255,0,255));
63 
64     D3DMATERIAL9 InitMtrl(D3DXCOLOR a,D3DXCOLOR d,D3DXCOLOR s,D3DXCOLOR e,float p);
65         //5.定义材质常量
66     const D3DMATERIAL9 RED_MTRL=InitMtrl(d3d::RED,d3d::RED,d3d::RED,d3d::BLACK,2.0f);
67     const D3DMATERIAL9 WHITE_MTRL=InitMtrl(WHITE,WHITE,WHITE,BLACK,2.0f);
68     const D3DMATERIAL9 GREEN_MTRL=InitMtrl(GREEN,GREEN,GREEN,BLACK,2.0f);
69     const D3DMATERIAL9 YELLOW_MTRL=InitMtrl(YELLOW,YELLOW,YELLOW,BLACK,2.0f);
70     const D3DMATERIAL9 BLUE_MTRL=InitMtrl(BLUE,BLUE,BLUE,BLACK,2.0f);
71 
72     D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position,D3DXVECTOR3* direction,D3DXCOLOR* color);
73     D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color);
74     D3DLIGHT9 InitPointLight(D3DXVECTOR3* position,D3DXCOLOR* color);
75 }
76 
77 #endif // __d3dUtilityH__
View Code
复制代码

Cube.cpp

复制代码
  1 #include "Cube.h"
  2 #include "vertex.h"
  3 
  4 Cube::Cube(void)
  5 {
  6 }
  7 Cube::Cube(IDirect3DDevice9* device)
  8 {
  9     _device = device;
 10         _device->CreateVertexBuffer(24 * sizeof(Vertex),//24个顶点,6个面*4个点
 11         D3DUSAGE_WRITEONLY,
 12         FVF_VERTEX,
 13         D3DPOOL_MANAGED,
 14         &_vb,
 15         0);
 16     Vertex* v;
 17     _vb->Lock(0,0,(void**)&v,0);
 18 
 19     // fill in the front face vertex data
 20     v[0] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);//顶点坐标,法线坐标,纹理坐标
 21     v[1] = Vertex(-1.0f,  1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
 22     v[2] = Vertex( 1.0f,  1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
 23     v[3] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
 24 
 25     // fill in the back face vertex data
 26     v[4] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
 27     v[5] = Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
 28     v[6] = Vertex( 1.0f,  1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
 29     v[7] = Vertex(-1.0f,  1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
 30 
 31     // fill in the top face vertex data
 32     v[8]  = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
 33     v[9]  = Vertex(-1.0f, 1.0f,  1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
 34     v[10] = Vertex( 1.0f, 1.0f,  1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
 35     v[11] = Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
 36 
 37     // fill in the bottom face vertex data
 38     v[12] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);
 39     v[13] = Vertex( 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f);
 40     v[14] = Vertex( 1.0f, -1.0f,  1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f);
 41     v[15] = Vertex(-1.0f, -1.0f,  1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
 42 
 43     // fill in the left face vertex data
 44     v[16] = Vertex(-1.0f, -1.0f,  1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
 45     v[17] = Vertex(-1.0f,  1.0f,  1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
 46     v[18] = Vertex(-1.0f,  1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
 47     v[19] = Vertex(-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
 48 
 49     // fill in the right face vertex data
 50     v[20] = Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
 51     v[21] = Vertex( 1.0f,  1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
 52     v[22] = Vertex( 1.0f,  1.0f,  1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
 53     v[23] = Vertex( 1.0f, -1.0f,  1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
 54 
 55     _vb->Unlock();
 56 //创建索引缓存
 57     _device->CreateIndexBuffer(36*sizeof(WORD),//每个面两个三角形,6个面*2个三角形*3个顶点
 58         D3DUSAGE_WRITEONLY,
 59         D3DFMT_INDEX16,
 60         D3DPOOL_MANAGED,
 61         &_ib,
 62         0);
 63     WORD* i=0;
 64     _ib->Lock(0,0,(void**)&i,0);
 65 
 66     // fill in the front face index data
 67     i[0] = 0; i[1] = 1; i[2] = 2;
 68     i[3] = 0; i[4] = 2; i[5] = 3;
 69 
 70     // fill in the back face index data
 71     i[6] = 4; i[7]  = 5; i[8]  = 6;
 72     i[9] = 4; i[10] = 6; i[11] = 7;
 73 
 74     // fill in the top face index data
 75     i[12] = 8; i[13] =  9; i[14] = 10;
 76     i[15] = 8; i[16] = 10; i[17] = 11;
 77 
 78     // fill in the bottom face index data
 79     i[18] = 12; i[19] = 13; i[20] = 14;
 80     i[21] = 12; i[22] = 14; i[23] = 15;
 81 
 82     // fill in the left face index data
 83     i[24] = 16; i[25] = 17; i[26] = 18;
 84     i[27] = 16; i[28] = 18; i[29] = 19;
 85 
 86     // fill in the right face index data
 87     i[30] = 20; i[31] = 21; i[32] = 22;
 88     i[33] = 20; i[34] = 22; i[35] = 23;
 89 
 90     _ib->Unlock();
 91 }
 92 
 93 
 94 Cube::~Cube(void)
 95 {
 96 }
 97 
 98 
 99 bool Cube::draw(D3DXMATRIX* world, D3DMATERIAL9* mtrl, IDirect3DTexture9* tex)
100 {    if(world)
101         _device->SetTransform(D3DTS_WORLD, world);
102 if(mtrl)
103         _device->SetMaterial(mtrl);
104     if(tex)
105         _device->SetTexture(0,tex);
106     _device->SetStreamSource(0,_vb,0,sizeof(Vertex));
107     _device->SetIndices(_ib);
108     _device->SetFVF(FVF_VERTEX);
109     _device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,24,0,12);
110     return true;
111 }
View Code
复制代码

d3dInit.cpp

复制代码
  1 //////////////////////////////////////////////////////////////////////////////////////////////////
  2 //
  3 // File: d3dinit.cpp
  4 //
  5 // Author: Frank Luna (C) All Rights Reserved
  6 //
  7 // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
  8 //
  9 // Desc: Demonstrates how to initialize Direct3D, how to use the book's framework
 10 //       functions, and how to clear the screen to black.  Note that the Direct3D
 11 //       initialization code is in the d3dUtility.h/.cpp files.
 12 //
 13 //////////////////////////////////////////////////////////////////////////////////////////////////
 14 
 15 #include "d3dUtility.h"
 16 #pragma  comment(lib, "d3d9.lib")
 17 #pragma  comment(lib, "d3dx9.lib")
 18 #pragma  comment(lib, "winmm.lib")
 19 //
 20 // Globals
 21 //
 22 #include "Cube.h"
 23 IDirect3DDevice9* Device = 0;
 24 
 25 //
 26 // Framework Functions
 27 //
 28 const int Width=640;
 29 const int Height=480;
 30 
 31 float angle=0.0f;
 32 
 33 Cube* Box = 0;
 34 
 35 IDirect3DTexture9* Tex = 0;
 36 
 37 bool Setup()
 38 {
 39     Box = new Cube(Device);
 40     
 41     
 42         //灯光
 43     D3DLIGHT9 light;
 44     ::ZeroMemory(&light,sizeof(light));
 45     light.Type = D3DLIGHT_DIRECTIONAL;
 46     light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);
 47     light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
 48     light.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
 49     light.Direction = D3DXVECTOR3(1.0f,-1.0f,0.0f);
 50     Device->SetLight(0,&light);
 51     Device->LightEnable(0,true);
 52 
 53     Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
 54     Device->SetRenderState(D3DRS_SPECULARENABLE, true);
 55     
 56 
 57     D3DXCreateTextureFromFile(
 58             Device,
 59         L"crate.jpg",
 60         &Tex);
 61     Device->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
 62     Device->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
 63     Device->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
 64         //摄像机的位置和角度
 65     D3DXVECTOR3 position(0.0f,2.0f,-5.0f);
 66     D3DXVECTOR3 target(0.0f,0.0f,0.0f);
 67     D3DXVECTOR3 up(0.0f,1.0f,0.0f);
 68     D3DXMATRIX V;
 69     D3DXMatrixLookAtLH(&V,&position,&target,&up);
 70     Device->SetTransform(D3DTS_VIEW,&V);
 71 
 72     //投影
 73     D3DXMATRIX proj;
 74     D3DXMatrixPerspectiveFovLH(
 75         &proj,
 76         D3DX_PI*0.5f,
 77         (float)Width/(float)Height,
 78         1.0f,
 79         1000.0f);
 80     Device->SetTransform(D3DTS_PROJECTION,&proj);
 81     //Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
 82     
 83     
 84     //Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
 85 
 86     return true;
 87 }
 88 
 89 void Cleanup()
 90 {
 91         d3d::Delete<Cube*>(Box);
 92     d3d::Release<IDirect3DTexture9*>(Tex);
 93 }
 94 
 95 bool Display(float timeDelta)
 96 {
 97     if( Device ) // Only use Device methods if we have a valid device.
 98     {
 99     D3DXMATRIX yRot;
100     angle+=0.001f;
101     D3DXMatrixRotationY(&yRot,angle);
102     
103     
104     Device->SetTransform(D3DTS_WORLD, &yRot);
105 
106     
107         Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
108       
109         Device->BeginScene();
110         Device->SetMaterial(&d3d::WHITE_MTRL);
111         Device->SetTexture(0,Tex);
112         Box->draw(0,0,0);
113         Device->EndScene();
114 
115         Device->Present(0, 0, 0, 0);
116     }
117     return true;
118 }
119 
120 //
121 // WndProc
122 //
123 LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
124 {
125     switch( msg )
126     {
127     case WM_DESTROY:
128         ::PostQuitMessage(0);
129         break;
130 
131     case WM_KEYDOWN:
132         if( wParam == VK_ESCAPE )
133             ::DestroyWindow(hwnd);
134         break;
135     }
136     return ::DefWindowProc(hwnd, msg, wParam, lParam);
137 }
138 
139 //
140 // WinMain
141 //
142 int WINAPI WinMain(HINSTANCE hinstance,
143                    HINSTANCE prevInstance,
144                    PSTR cmdLine,
145                    int showCmd)
146 {
147     if(!d3d::InitD3D(hinstance,
148         640, 480, true, D3DDEVTYPE_HAL, &Device))
149     {
150         ::MessageBox(0, L"InitD3D() - FAILED", 0, 0);
151         return 0;
152     }
153 
154     if(!Setup())
155     {
156         ::MessageBox(0, L"Setup() - FAILED", 0, 0);
157         return 0;
158     }
159 
160     d3d::EnterMsgLoop( Display );
161 
162     Cleanup();
163 
164     Device->Release();
165 
166     return 0;
167 }
View Code
复制代码

d3dUtility.cpp

复制代码
  1 //////////////////////////////////////////////////////////////////////////////////////////////////
  2 //
  3 // File: d3dUtility.cpp
  4 //
  5 // Author: Frank Luna (C) All Rights Reserved
  6 //
  7 // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
  8 //
  9 // Desc: Provides utility functions for simplifying common tasks.
 10 //
 11 //////////////////////////////////////////////////////////////////////////////////////////////////
 12 
 13 #include "d3dUtility.h"
 14 
 15 bool d3d::InitD3D(
 16     HINSTANCE hInstance,
 17     int width, int height,
 18     bool windowed,
 19     D3DDEVTYPE deviceType,
 20     IDirect3DDevice9** device)
 21 {
 22     //
 23     // Create the main application window.
 24     //
 25 
 26     WNDCLASS wc;
 27 
 28     wc.style         = CS_HREDRAW | CS_VREDRAW;
 29     wc.lpfnWndProc   = (WNDPROC)d3d::WndProc;
 30     wc.cbClsExtra    = 0;
 31     wc.cbWndExtra    = 0;
 32     wc.hInstance     = hInstance;
 33     wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
 34     wc.hCursor       = LoadCursor(0, IDC_ARROW);
 35     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 36     wc.lpszMenuName  = 0;
 37     wc.lpszClassName = L"Direct3D9App";
 38 
 39     if( !RegisterClass(&wc) )
 40     {
 41         ::MessageBox(0, L"RegisterClass() - FAILED", 0, 0);
 42         return false;
 43     }
 44 
 45     HWND hwnd = 0;
 46     hwnd = ::CreateWindow(L"Direct3D9App", L"Direct3D9App",
 47         WS_EX_TOPMOST,
 48         0, 0, width, height,
 49         0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/);
 50 
 51     if( !hwnd )
 52     {
 53         ::MessageBox(0, L"CreateWindow() - FAILED", 0, 0);
 54         return false;
 55     }
 56 
 57     ::MoveWindow(hwnd,2250,80,640, 480,true);
 58     ::ShowWindow(hwnd, SW_SHOW);
 59     ::UpdateWindow(hwnd);
 60 
 61     //
 62     // Init D3D:
 63     //
 64 
 65     HRESULT hr = 0;
 66 
 67     // Step 1: Create the IDirect3D9 object.
 68 
 69     IDirect3D9* d3d9 = 0;
 70     d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
 71 
 72     if( !d3d9 )
 73     {
 74         ::MessageBox(0, L"Direct3DCreate9() - FAILED", 0, 0);
 75         return false;
 76     }
 77 
 78     // Step 2: Check for hardware vp.
 79 
 80     D3DCAPS9 caps;
 81     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);
 82 
 83     int vp = 0;
 84     if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
 85         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
 86     else
 87         vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
 88 
 89     // Step 3: Fill out the D3DPRESENT_PARAMETERS structure.
 90 
 91     D3DPRESENT_PARAMETERS d3dpp;
 92     d3dpp.BackBufferWidth            = width;
 93     d3dpp.BackBufferHeight           = height;
 94     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
 95     d3dpp.BackBufferCount            = 1;
 96     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
 97     d3dpp.MultiSampleQuality         = 0;
 98     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
 99     d3dpp.hDeviceWindow              = hwnd;
100     d3dpp.Windowed                   = windowed;
101     d3dpp.EnableAutoDepthStencil     = true;
102     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
103     d3dpp.Flags                      = 0;
104     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
105     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
106 
107     // Step 4: Create the device.
108 
109     hr = d3d9->CreateDevice(
110         D3DADAPTER_DEFAULT, // primary adapter
111         deviceType,         // device type
112         hwnd,               // window associated with device
113         vp,                 // vertex processing
114         &d3dpp,             // present parameters
115         device);            // return created device
116 
117     if( FAILED(hr) )
118     {
119         // try again using a 16-bit depth buffer
120         d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
121 
122         hr = d3d9->CreateDevice(
123             D3DADAPTER_DEFAULT,
124             deviceType,
125             hwnd,
126             vp,
127             &d3dpp,
128             device);
129 
130         if( FAILED(hr) )
131         {
132             d3d9->Release(); // done with d3d9 object
133             ::MessageBox(0, L"CreateDevice() - FAILED", 0, 0);
134             return false;
135         }
136     }
137 
138     d3d9->Release(); // done with d3d9 object
139 
140     return true;
141 }
142 
143 int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )
144 {
145     MSG msg;
146     ::ZeroMemory(&msg, sizeof(MSG));
147 
148     static float lastTime = (float)timeGetTime();
149 
150     while(msg.message != WM_QUIT)
151     {
152         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
153         {
154             ::TranslateMessage(&msg);
155             ::DispatchMessage(&msg);
156         }
157         else
158         {
159             float currTime  = (float)timeGetTime();
160             float timeDelta = (currTime - lastTime)*0.001f;
161 
162             ptr_display(timeDelta);
163 
164             lastTime = currTime;
165         }
166     }
167     return msg.wParam;
168 }
169 
170 //4.定义材质函数,具体的函数实现在d3dUtility.cpp源文件中
171 D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a,D3DXCOLOR d,D3DXCOLOR s,D3DXCOLOR e,float p)
172     {
173         D3DMATERIAL9 mtrl;
174       //剪切聚光灯.cpp文件的相应材质定义语句
175       /*Mtrls[0].Ambient=d3d::RED;
176     Mtrls[0].Diffuse=d3d::RED;
177     Mtrls[0].Specular=d3d::RED;
178     Mtrls[0].Power=2.0f;
179     Mtrls[0].Emissive=d3d::BLACK;*/
180       //要做相应修改,数组改成局部变量,后面的符号常量,在这里改成参数
181         mtrl.Ambient=a;
182         mtrl.Diffuse=d;
183         mtrl.Specular=s;
184         mtrl.Power=p;
185         mtrl.Emissive=e;
186         return mtrl;
187     }
188 
189 D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position,D3DXVECTOR3* direction,D3DXCOLOR *color)
190 {
191     D3DLIGHT9 light;
192     ::ZeroMemory(&light,sizeof(light));
193     light.Type=D3DLIGHT_SPOT;
194     light.Ambient=*color*0.0f;
195     light.Diffuse=*color;
196     light.Specular=*color*0.6f;
197     light.Position=*position;
198     light.Direction=*direction;
199     light.Range=1000.0f;
200     light.Falloff=1.0f;
201     light.Attenuation0=1.0f;
202     light.Attenuation1=0.0f;
203     light.Attenuation2=0.0f;
204     light.Theta=0.4f;
205     light.Phi=0.9f;
206 
207     return light;
208 }
209 
210 
211 D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR *color)
212     {
213     D3DLIGHT9 light;
214     ::ZeroMemory(&light,sizeof(light));//用Windows函数先清零
215     light.Type=D3DLIGHT_DIRECTIONAL;
216     light.Ambient=*color*0.6f;//环境光,就使用方法给它乘以0.6
217     light.Diffuse=*color;//漫射光
218     light.Specular=*color*0.6f;//镜面光,高亮度数是0.6倍
219     light.Direction=*direction;
220     
221     return light;
222     }
223 
224 D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position,D3DXCOLOR *color)
225     {
226     D3DLIGHT9 light;
227     ::ZeroMemory(&light,sizeof(light));//用Windows函数先清零
228     light.Type=D3DLIGHT_POINT;
229     light.Ambient=*color*0.6f;//没有环境光,就使用方法给它乘以0.6
230     light.Diffuse=*color;//漫射光
231     light.Specular=*color*0.6f;//镜面光,高亮度数是0.6倍
232     light.Position=*position;
233     light.Range=1000.0f;
234     light.Falloff=1.0f;//衰减率
235     light.Attenuation0=1.0f;
236     light.Attenuation1=0.0f;
237     light.Attenuation2=0.0f;
238     
239 
240     return light;
241     }
View Code
复制代码
posted on   次次上岸盈  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示