Directx11学习笔记【十四】 使用最新的Effect框架和SDK

  由于之前一直在看directx11龙书学习,因此sdk一直用的Microsoft DirectX SDK (June 2010) 版本,最近在stackoverflow上问dx11相关问题时,一直被大神吐槽为何还用已经废弃的directx sdk,由于directx sdk现在已经和windows sdk合并到一起了,只能去下windows sdk了。为了方便索性直接换了vs 2015社区版,里面自带了(windows sdk),既然sdk换了最新的,effect框架也要换最新的啊(Effect框架已经被微软开源托管在github上,https://github.com/Microsoft/FX11/wiki)。还有stackoverflow上都推荐DirectXTK,这方面中文资料非常少,只能靠自己慢慢看文档示例学了,等看得差不多了,会考虑用DirectXTK写的。以后教程还是基本跟着龙书走,我会将龙书实现的效果用自己封装的框架写一遍,尤其是一些过时的函数方法我也会换上对应的新方法。

  由于directx sdk集成在windows sdk中了,所以在建项目的时候就不用设置引用目录和库目录的路径了,只需设置链接库就可以了,还是挺方便的。由于换了sdk,一些方法参数之类的都略有不同,我又把之前的基类Dx11DemoBase重新封装了一遍,为了练习把前一个教程HillsDemo又重新写了一遍,体会一下有哪些改动。不同之处都在代码中给出了注释。

  前面教程编译shader用的是两个方法D3DX11CompileFromFile和D3DX11CreateEffectFromMemory,显得比较麻烦,在最新的FX11(Effect框架)中这两个方法已经废弃了,我们可以采用D3DX11CompileEffectFromFile来编译shader,一步到位尤为方便 

 1 //compile shader
 2     ID3DBlob* errorBlob;
 3     DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
 4
 5 #if defined _DEBUG || defined DEBUG
 6     shaderFlags = D3DCOMPILE_DEBUG;
 7 #endif
 8
 9     hr = D3DX11CompileEffectFromFile(L"color.fx", nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, shaderFlags,
10         0, m_pd3dDevice, &m_pFx, &errorBlob);
11     if (FAILED(hr))
12     {
13         MessageBox(nullptr, (LPCWSTR)errorBlob->GetBufferPointer(), L"error", MB_OK);
14         return hr;
15     }
16
17     m_pTechnique = m_pFx->GetTechniqueByName("ColorTech");
18     m_pFxWorldViewProj = m_pFx->GetVariableByName("gWorldViewProj")->AsMatrix();

  下面给出改动后的HillsDemo代码(其实大体差不多),在看新的官方实例时发现代码都是c++11风格(使用nullptr代替NULL,使用c++风格类型转换static_cast/reinterpret_cast,DirectXTK中大量使用智能指针等),因此咱也尽量保持相同的风格以养成写规范代码的好习惯。此外注释我也尽量用英文写,一方面有些用汉语不太好说明,另一方面由于要经常在stackoverflow上提问老外看不懂汉语还得临时改英文挺麻烦的。

  PS(国内有哪些活跃的directx社区网站吗?麻烦知道的告诉一下谢啦,想抓紧时间学一下,感觉才刚起步后面要学的太多了)

  

  Dx11DemoBase.h

 1 #ifndef _DX11DEMOBASE_H_
 2 #define _DX11DEMOBASE_H_
 3
 4 #include <string>
 5 #include <windows.h>
 6 #include <d3d11.h>
 7 #include <d3dcompiler.h>
 8 #include <DirectXMath.h>//don‘t use xnamath.h
 9 #include <directxcolors.h>
10
11 using namespace DirectX;
12
13 class Dx11DemoBase
14 {
15 public:
16     Dx11DemoBase();
17     virtual ~Dx11DemoBase();
18
19     std::wstring  m_mainWndCaption;                    //title
20     float AspectRatio() const;                        //width/height
21
22     bool InitDirect3D(HINSTANCE hInstance, HWND hWnd);
23
24     void ShutDown();                                //release
25
26     virtual bool LoadContent();                        //init concrete content
27     virtual void UnLoadContent();                    //release
28
29     virtual void Update(float dt) = 0;
30     virtual void Render() = 0;
31
32     virtual void OnMouseDown(WPARAM btnState, int x, int y) {}
33     virtual void OnMouseUp(WPARAM btnState, int x, int y) {}
34     virtual void OnMouseMove(WPARAM btnState, int x, int y) {}
35
36 protected:
37     UINT                    m_width;                //window width
38     UINT                    m_height;                //window height
39     HINSTANCE                m_hInstance;
40     HWND                    m_hWnd;
41     D3D_DRIVER_TYPE            m_driverType;
42     D3D_FEATURE_LEVEL        m_featureLevel;
43     ID3D11Device*            m_pd3dDevice;
44     ID3D11DeviceContext*    m_pImmediateContext;
45     IDXGISwapChain*            m_pSwapChain;
46     ID3D11RenderTargetView* m_pRenderTargetView;
47     ID3D11Texture2D*        m_pDepthStencilBuffer;
48     ID3D11DepthStencilView* m_pDepthStencilView;
49 };
50
51 #endif//_DX11DEMOBASE_H_

  Dx11DemoBase.cpp

  1 #include "Dx11DemoBase.h"
  2
  3 Dx11DemoBase::Dx11DemoBase():
  4 m_mainWndCaption(L"Directx11 Application"),
  5 m_driverType(D3D_DRIVER_TYPE_HARDWARE),
  6 m_featureLevel(D3D_FEATURE_LEVEL_11_0),
  7 m_pd3dDevice(nullptr),
  8 m_pImmediateContext(nullptr),
  9 m_pRenderTargetView(nullptr),
 10 m_pDepthStencilBuffer(nullptr),
 11 m_pDepthStencilView(nullptr),
 12 m_pSwapChain(nullptr),
 13 m_hWnd(nullptr),
 14 m_width(800),
 15 m_height(600)
 16 {}
 17
 18 Dx11DemoBase::~Dx11DemoBase()
 19 {
 20     ShutDown();
 21 }
 22
 23 float Dx11DemoBase::AspectRatio() const
 24 {
 25     return static_cast<float>(m_width / m_height);
 26 }
 27
 28 bool Dx11DemoBase::InitDirect3D(HINSTANCE hInstance, HWND hWnd)
 29 {
 30     HRESULT hr = S_OK;
 31     m_hInstance = hInstance;
 32     m_hWnd = hWnd;
 33     RECT rc;
 34     GetClientRect(m_hWnd, &rc);
 35     m_width = rc.right - rc.left;
 36     m_height = rc.bottom - rc.top;
 37
 38     UINT createDeviceFlags = 0;
 39
 40 #ifdef _DEBUG
 41     createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
 42 #endif
 43
 44     D3D_DRIVER_TYPE driverTypes[] =
 45     {
 46         D3D_DRIVER_TYPE_HARDWARE,
 47         D3D_DRIVER_TYPE_WARP,
 48         D3D_DRIVER_TYPE_REFERENCE
 49     };
 50     UINT numDriverTypes = ARRAYSIZE(driverTypes);
 51
 52     D3D_FEATURE_LEVEL featureLevels[] =
 53     {
 54         D3D_FEATURE_LEVEL_11_0,
 55         D3D_FEATURE_LEVEL_10_1,
 56         D3D_FEATURE_LEVEL_10_0
 57     };
 58     UINT numFeatureLevels = ARRAYSIZE(featureLevels);
 59
 60     DXGI_SWAP_CHAIN_DESC sd;
 61     ZeroMemory(&sd, sizeof(sd));
 62     sd.BufferCount = 1;
 63     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
 64     sd.BufferDesc.Width = m_width;
 65     sd.BufferDesc.Height = m_height;
 66     sd.BufferDesc.RefreshRate.Numerator = 60;
 67     sd.BufferDesc.RefreshRate.Denominator = 1;
 68     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
 69     sd.OutputWindow = m_hWnd;
 70     sd.SampleDesc.Count = 1;
 71     sd.SampleDesc.Quality = 0;
 72     sd.Windowed = TRUE;
 73
 74     //create device and swapchain
 75     for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; ++driverTypeIndex)
 76     {
 77         hr = D3D11CreateDeviceAndSwapChain(nullptr, driverTypes[driverTypeIndex], nullptr,
 78             createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &sd, &m_pSwapChain,
 79             &m_pd3dDevice, &m_featureLevel, &m_pImmediateContext);
 80         if (SUCCEEDED(hr))
 81         {
 82             m_driverType = driverTypes[driverTypeIndex];
 83             break;
 84         }
 85     }
 86     if (FAILED(hr))
 87         return hr;
 88
 89     //create render target view
 90     ID3D11Texture2D *pBackBuffer = nullptr;
 91     hr = m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBuffer));
 92     if (FAILED(hr))
 93         return hr;
 94
 95     hr = m_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &m_pRenderTargetView);
 96     pBackBuffer->Release();
 97     if (FAILED(hr))
 98         return hr;
 99
100     m_pImmediateContext->OMSetRenderTargets(1, &m_pRenderTargetView, nullptr);
101     /*
102     //create depth stencil texture
103     D3D11_TEXTURE2D_DESC descDepth;
104     descDepth.Width = m_width;
105     descDepth.Height = m_height;
106     descDepth.ArraySize = 1;
107     descDepth.MipLevels = 1;
108     descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
109     descDepth.SampleDesc.Count = 1;
110     descDepth.SampleDesc.Quality = 0;
111     descDepth.Usage = D3D11_USAGE_DEFAULT;
112     descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
113     descDepth.CPUAccessFlags = 0;
114     descDepth.MiscFlags = 0;
115     hr = m_pd3dDevice->CreateTexture2D(&descDepth, nullptr, &m_pDepthStencilBuffer);
116     if (FAILED(hr))
117         return hr;
118
119     //create the depth stencil view
120     D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
121     ZeroMemory(&descDSV, sizeof(descDSV));
122     descDSV.Format = descDepth.Format;
123     descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
124     descDSV.Texture2D.MipSlice = 0;
125     hr = m_pd3dDevice->CreateDepthStencilView(m_pDepthStencilBuffer, &descDSV, &m_pDepthStencilView);
126     if (FAILED(hr))
127         return hr;
128
129     m_pImmediateContext->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);*/
130
131     //setup the viewport
132     D3D11_VIEWPORT vp;
133     vp.Width = static_cast<float>(m_width);
134     vp.Height = static_cast<float>(m_height);
135     vp.MinDepth = 0.0f;
136     vp.MaxDepth = 1.0f;
137     vp.TopLeftX = 0.0f;
138     vp.TopLeftY = 0.0f;
139     m_pImmediateContext->RSSetViewports(1, &vp);
140
141     return LoadContent();
142 }
143
144
145 void Dx11DemoBase::ShutDown()
146 {
147     UnLoadContent();
148     if (m_pImmediateContext) m_pImmediateContext->ClearState();
149
150     if (m_pRenderTargetView) m_pRenderTargetView->Release();
151     if (m_pSwapChain) m_pSwapChain->Release();
152     if (m_pImmediateContext) m_pImmediateContext->Release();
153     if (m_pd3dDevice) m_pd3dDevice->Release();
154 }
155
156 bool Dx11DemoBase::LoadContent()
157 {
158     return true;
159 }
160
161 void Dx11DemoBase::UnLoadContent(){}

  GeometryGenerator类中代码没什么改变不再给出

HillsDemo.h

 1 #ifndef _HILLSDEMO_H_
 2 #define _HILLSDEMO_H_
 3
 4 #include "Dx11DemoBase.h"
 5 #include "d3dx11effect.h"
 6 #include "GeometryGenerator.h"
 7
 8
 9 #pragma comment(lib,"d3d11.lib")
10 #pragma comment(lib,"Effects11.lib")
11 #pragma comment(lib,"d3dcompiler.lib")
12 #pragma comment(lib,"winmm.lib")
13 #pragma comment(lib,"comctl32.lib")
14 #pragma comment(lib,"dxguid.lib")
15
16 class HillsDemo : public Dx11DemoBase
17 {
18 public:
19     HillsDemo();
20     ~HillsDemo();
21
22     bool LoadContent() override;
23     void UnLoadContent() override;
24
25     void Update(float dt) override;
26     void Render() override;
27
28     void OnMouseDown(WPARAM btnState, int x, int y) override;
29     void OnMouseUp(WPARAM btnState, int x, int y) override;
30     void OnMouseMove(WPARAM btnState, int x, int y) override;
31 private:
32     ID3D11Buffer*                    m_pVertexBuffer;
33     ID3D11Buffer*                    m_pIndexBuffer;
34     ID3D11InputLayout*               m_pInputLayout;
35
36     ID3DX11Effect*                   m_pFx;
37     ID3DX11EffectTechnique*          m_pTechnique;
38     ID3DX11EffectMatrixVariable*     m_pFxWorldViewProj;
39     XMFLOAT4X4                       m_world;
40     XMFLOAT4X4                       m_view;
41     XMFLOAT4X4                       m_proj;
42
43
44     UINT                             m_gridIndexCount;
45     float                            m_theta;
46     float                            m_phi;
47     float                            m_radius;
48     POINT                            m_lastMousePos;
49
50     float GetHeight(float x, float z)const;
51
52 };
53
54 #endif//_HILLSDEMO_H_

HillsDemo.cpp

  1 #include "HillsDemo.h"
  2
  3 struct Vertex
  4 {
  5     XMFLOAT3 pos;
  6     XMFLOAT4 color;
  7     Vertex(XMFLOAT3 p, XMFLOAT4 c) : pos(p), color(c) {}
  8 };
  9
 10 HillsDemo::HillsDemo() :m_pInputLayout(nullptr), m_pVertexBuffer(nullptr),m_pFx(nullptr), m_gridIndexCount(0),
 11 m_theta(1.5f*XM_PI), m_phi(0.1f*XM_PI), m_radius(200.0f)
 12 {
 13     XMMATRIX I = XMMatrixIdentity();
 14     XMStoreFloat4x4(&m_world, I);
 15     XMStoreFloat4x4(&m_view, I);
 16     XMStoreFloat4x4(&m_proj, I);
 17 }
 18
 19 HillsDemo::~HillsDemo()
 20 {
 21
 22 }
 23
 24 bool HillsDemo::LoadContent()
 25 {
 26     HRESULT hr;
 27
 28     //create vertex buffer
 29     GeometryGenerator::MeshData grid;
 30     GeometryGenerator geoGen;
 31     geoGen.CreateGrid(160.0f, 160.0f, 50, 50, grid);
 32     m_gridIndexCount = grid.indices.size();
 33
 34     std::vector<Vertex> vertices(grid.vertices.size(), Vertex(XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 0)));
 35     for (UINT i = 0; i < grid.vertices.size(); ++i)
 36     {
 37         XMFLOAT3 p = grid.vertices[i].Position;
 38         p.y = GetHeight(p.x, p.z);
 39
 40         vertices[i].pos = p;
 41
 42         //render vertex with different color according to height
 43         if (p.y < -10.0f)
 44         {
 45             //sandy beach color
 46             vertices[i].color = XMFLOAT4(1.0f, 0.96f, 0.62f, 1.0f);
 47         }
 48         else if (p.y < 5.0f)
 49         {
 50             //dark yellow-green color
 51             vertices[i].color = XMFLOAT4(0.1f, 0.48f, 0.19f, 1.0f);
 52         }
 53         else if (p.y < 12.0f)
 54         {
 55             //light yellow-green color
 56             vertices[i].color = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f);
 57         }
 58         else if (p.y < 20.f)
 59         {
 60             //dark brown color
 61             vertices[i].color = XMFLOAT4(0.45f, 0.39f, 0.34f, 1.0f);
 62         }
 63         else
 64         {
 65             //white snow color
 66             vertices[i].color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
 67         }
 68     }
 69
 70     D3D11_BUFFER_DESC vertexDesc;
 71     ZeroMemory(&vertexDesc, sizeof(vertexDesc));
 72     vertexDesc.Usage = D3D11_USAGE_IMMUTABLE;
 73     vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
 74     vertexDesc.ByteWidth = sizeof(Vertex)* grid.vertices.size();
 75     D3D11_SUBRESOURCE_DATA resourceData;
 76     ZeroMemory(&resourceData, sizeof(resourceData));
 77     resourceData.pSysMem = &vertices[0];
 78     hr = m_pd3dDevice->CreateBuffer(&vertexDesc, &resourceData, &m_pVertexBuffer);
 79     if (FAILED(hr))
 80     {
 81         return false;
 82     }
 83
 84     //set vertex buffer
 85     UINT stride = sizeof(Vertex);
 86     UINT offset = 0;
 87     m_pImmediateContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
 88     //set primitive topology
 89     m_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
 90
 91     //create index buffer
 92     D3D11_BUFFER_DESC indexDesc;
 93     ZeroMemory(&indexDesc, sizeof(indexDesc));
 94     indexDesc.Usage = D3D11_USAGE_IMMUTABLE;
 95     indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
 96     indexDesc.ByteWidth = sizeof(UINT)* m_gridIndexCount;
 97
 98     D3D11_SUBRESOURCE_DATA indexData;
 99     ZeroMemory(&indexData, sizeof(indexData));
100     indexData.pSysMem = &grid.indices[0];
101     hr = m_pd3dDevice->CreateBuffer(&indexDesc, &indexData, &m_pIndexBuffer);
102     if (FAILED(hr))
103     {
104         return false;
105     }
106
107     //set index buffer
108     m_pImmediateContext->IASetIndexBuffer(m_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
109
110     //compile shader
111     ID3DBlob* errorBlob;
112     DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
113
114 #if defined _DEBUG || defined DEBUG
115     shaderFlags = D3DCOMPILE_DEBUG;
116 #endif
117
118     hr = D3DX11CompileEffectFromFile(L"color.fx", nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, shaderFlags,
119         0, m_pd3dDevice, &m_pFx, &errorBlob);
120     if (FAILED(hr))
121     {
122         MessageBox(nullptr, (LPCWSTR)errorBlob->GetBufferPointer(), L"error", MB_OK);
123         return hr;
124     }
125
126     m_pTechnique = m_pFx->GetTechniqueByName("ColorTech");
127     m_pFxWorldViewProj = m_pFx->GetVariableByName("gWorldViewProj")->AsMatrix();
128
129
130     //define the input layout
131     D3D11_INPUT_ELEMENT_DESC colorLayout[] =
132     {
133         { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
134         { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
135     };
136
137     UINT numLayoutElements = ARRAYSIZE(colorLayout);
138     D3DX11_PASS_DESC passDesc;
139     m_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);
140
141     //create the input layout
142     hr = m_pd3dDevice->CreateInputLayout(colorLayout, numLayoutElements, passDesc.pIAInputSignature,
143         passDesc.IAInputSignatureSize, &m_pInputLayout);
144     if (FAILED(hr))
145         return hr;
146
147     //set input layout
148     m_pImmediateContext->IASetInputLayout(m_pInputLayout);
149
150     return true;
151 }
152
153 void HillsDemo::UnLoadContent()
154 {
155     if (m_pVertexBuffer) m_pVertexBuffer->Release();
156     if (m_pIndexBuffer)    m_pIndexBuffer->Release();
157     if (m_pInputLayout)    m_pInputLayout->Release();
158     if (m_pTechnique) m_pTechnique->Release();
159 }
160
161 void HillsDemo::Update(float dt)
162 {
163     float x = m_radius*sinf(m_phi)*cosf(m_theta);
164     float z = m_radius*sinf(m_phi)*sinf(m_theta);
165     float y = m_radius*cosf(m_phi);
166
167     XMVECTOR pos = XMVectorSet(x, y, z, 1.0f);
168     XMVECTOR target = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
169     XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
170
171     XMMATRIX V = XMMatrixLookAtLH(pos, target, up);
172     XMStoreFloat4x4(&m_view, V);
173     XMMATRIX T = XMMatrixPerspectiveFovLH(XM_PIDIV4, m_width / static_cast<float>(m_height),
174         1.0f, 1000.0f);
175     XMStoreFloat4x4(&m_proj, T);
176 }
177
178 void HillsDemo::Render()
179 {
180     if (m_pImmediateContext == 0)
181         return;
182     float clearColor[4] = { 0.0f, 0.0f, 1.0f, 1.0f };//background color
183     m_pImmediateContext->ClearRenderTargetView(m_pRenderTargetView, clearColor);
184
185     //set constant buffer
186     XMMATRIX world = XMLoadFloat4x4(&m_world);
187     XMMATRIX view = XMLoadFloat4x4(&m_view);
188     XMMATRIX proj = XMLoadFloat4x4(&m_proj);
189     XMMATRIX worldViewProj = world*view*proj;
190
191
192     D3DX11_TECHNIQUE_DESC techDesc;
193     m_pTechnique->GetDesc(&techDesc);
194     for (UINT i = 0; i < techDesc.Passes; ++i)
195     {
196         m_pFxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));
197         m_pTechnique->GetPassByIndex(i)->Apply(0, m_pImmediateContext);
198         m_pImmediateContext->DrawIndexed(m_gridIndexCount, 0, 0);
199     }
200
201     m_pSwapChain->Present(0, 0);
202 }
203
204 void HillsDemo::OnMouseDown(WPARAM btnState, int x, int y)
205 {
206     m_lastMousePos.x = x;
207     m_lastMousePos.y = y;
208     SetCapture(m_hWnd);
209 }
210
211 void HillsDemo::OnMouseUp(WPARAM btnState, int x, int y)
212 {
213     ReleaseCapture();
214 }
215
216 //restrict the number
217 template<typename T>
218 static T Clamp(const T& x, const T& low, const T& high)
219 {
220     return x < low ? low : (x > high ? high : x);
221 }
222
223 void HillsDemo::OnMouseMove(WPARAM btnState, int x, int y)
224 {
225     if ((btnState & MK_LBUTTON) != 0)
226     {
227         // make each pixel correspond to a quarter of a degree.
228         float dx = XMConvertToRadians(0.25f*static_cast<float>(x - m_lastMousePos.x));
229         float dy = XMConvertToRadians(0.25f*static_cast<float>(y - m_lastMousePos.y));
230
231         // update angles based on input to orbit camera around box.
232         m_theta += dx;
233         m_phi += dy;
234
235         // restrict the angle mPhi.
236         m_phi = Clamp(m_phi, 0.1f, XM_PI - 0.1f);
237     }
238     else if ((btnState & MK_RBUTTON) != 0)
239     {
240         // make each pixel correspond to 0.2 unit in the scene.
241         float dx = 0.2f*static_cast<float>(x - m_lastMousePos.x);
242         float dy = 0.2f*static_cast<float>(y - m_lastMousePos.y);
243
244         // update the camera radius based on input.
245         m_radius += dx - dy;
246
247         // restrict the radius.
248         m_radius = Clamp(m_radius, 50.0f, 500.0f);
249     }
250
251     m_lastMousePos.x = x;
252     m_lastMousePos.y = y;
253 }
254
255 float HillsDemo::GetHeight(float x, float z) const
256 {
257     return 0.3f*(z*sinf(0.1f*x) + x*cosf(0.1f*z));
258 }

main.cpp

 1 #include <windows.h>
 2 #include <windowsx.h>
 3 #include <memory>
 4 #include "HillsDemo.h"
 5 using namespace std;
 6
 7 std::shared_ptr<Dx11DemoBase> demo = make_shared<HillsDemo>();
 8
 9 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
10
11 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
12 {
13     UNREFERENCED_PARAMETER(hPrevInstance);
14     UNREFERENCED_PARAMETER(lpCmdLine);
15
16     WNDCLASSEX wcex;
17     wcex.cbClsExtra = 0;
18     wcex.cbSize = sizeof(wcex);
19     wcex.cbWndExtra = 0;
20     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
21     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
22     wcex.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
23     wcex.hIconSm = wcex.hIcon;
24     wcex.hInstance = hInstance;
25     wcex.lpfnWndProc = WndProc;
26     wcex.lpszClassName = L"HillsDemo";
27     wcex.lpszMenuName = nullptr;
28     wcex.style = CS_HREDRAW | CS_VREDRAW;
29
30     if (!RegisterClassEx(&wcex))
31         return 0;
32
33
34     RECT rc = { 0, 0, 800, 600 };
35     AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
36
37     HWND hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"HillsDemo", L"HillsDemo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
38         CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance, nullptr);
39
40     if (!hwnd)
41         return 0;
42
43     ShowWindow(hwnd, nShowCmd);
44
45
46     bool result = demo->InitDirect3D(hInstance, hwnd);
47     if (!result)
48         return 0;
49
50     MSG msg;
51     ZeroMemory(&msg, sizeof(msg));
52     while (msg.message != WM_QUIT)
53     {
54         if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
55         {
56             TranslateMessage(&msg);
57             DispatchMessage(&msg);
58         }
59         demo->Update(0.0f);
60         demo->Render();
61     }
62     demo->ShutDown();
63     return static_cast<int>(msg.wParam);
64 }
65
66 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
67 {
68     PAINTSTRUCT paintStruct;
69     HDC hdc;
70     switch (message)
71     {
72     case WM_PAINT:
73         hdc = BeginPaint(hWnd, &paintStruct);
74         EndPaint(hWnd, &paintStruct);
75         break;
76     case WM_DESTROY:
77         PostQuitMessage(0);
78         break;
79     case WM_LBUTTONDOWN:
80     case WM_MBUTTONDOWN:
81     case WM_RBUTTONDOWN:
82         demo->OnMouseDown(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
83         return 0;
84     case WM_LBUTTONUP:
85     case WM_MBUTTONUP:
86     case WM_RBUTTONUP:
87         demo->OnMouseUp(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
88         return 0;
89     case WM_MOUSEMOVE:
90         demo->OnMouseMove(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
91         return 0;
92     default:
93         return DefWindowProc(hWnd, message, wParam, lParam);
94     }
95     return 0;
96 }

好了代码就这些了,最后让我们看张图缓解下疲劳吧

时间: 2024-10-09 22:04:34

Directx11学习笔记【十四】 使用最新的Effect框架和SDK的相关文章

Swift学习笔记十四:构造(Initialization)

类和结构体在实例创建时,必须为所有存储型属性设置合适的初始值.存储型属性的值不能处于一个未知的状态. 你可以在构造器中为存储型属性赋初值,也可以在定义属性时为其设置默认值.以下章节将详细介绍这两种方法. 注意: 当你为存储型属性设置默认值或者在构造器中为其赋值时,它们的值是被直接设置的,不会触发任何属性观测器(property observers). 一.基本语法 class Human{ var name :String init(){ name = "human" } init(n

laravel3学习笔记(十四)

原作者博客:ieqi.net ==================================================================================================== 运行时配置 在 Laravel3 中很多地方我们都可以看到“约定大于配置”的影子,我本人也很喜欢这种工程哲学尤其是在框架领域,当然这并不能代替所有的配置.我们知道 Laravel3 中,主要配置都写在 application/config 文件夹下,在应用逻辑中,往往

Directx11学习笔记【四】 封装一个简单的Dx11DemoBase

根据前面两个笔记的内容,我们来封装一个简单的基类,方便以后的使用. 代码和前面类似,没有什么新的内容,直接看代码吧(由于代码上次都注释了,这次代码就没怎么写注释o(╯□╰)o) Dx11DemoBase.h Dx11DemoBase.h #pragma once #include <d3d11.h> #include <D3DX11.h> #include <DxErr.h> class Dx11DemoBase { public: Dx11DemoBase(); vi

【转】angular学习笔记(十四)-$watch(1)

本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: 需要被检测的对象,可以是以下任意一种: 1. 某个数据,监测这个数据的值是否发生变化 2. 一条angular表达式,监测表达式的结果是否发生变化 3. 函数(),监测函数的返回值是否发生变化 注意,以上三种,无论是哪种,都应该是字符串格式,并且都是在$scope作用域下执行的. 4.函数,非字符

Android学习笔记十四.深入理解fragment(二) 之《图书详情》实战

深入理解fragment(二) 之<图书详情>实战 通过上一篇博文<深入理解fragment一>,我们学习了Android-Fragment的核心知识点.现在在此基础上,利用Fragment技术开发一款适用于大屏幕手机/平板的查找图书详情的应用软件.该项目主要在于两方面,一是Activity.Fragment的源码实现:二是,布局界面资源文件的实现. 1.res/../BookListFragment.java: 自定义类,继承于ListFragment,无需实现OnCreateV

JavaScript权威设计--Window对象之Iframe(简要学习笔记十四)

1.Window对象属性的文档元素(id) 如果在HTML文档中用id属性来为元素命名,并且如果Window对象没有此名字的属性,Window对象会赋予一个属性,它的名字是id属性的值,而他们的值指向表示文档元素的HTMLElement对象. Window对象是以全局对象的形式存在于作用域链的最上层,这就意味着HTML文档中使用的id属性会成为可以被脚本访问的全局变量. 如: <button id="but"/> 就可以通过全局变量but来引用此元素. 2.多窗体窗口(if

yii2源码学习笔记(十四)

Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php 1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 namespace yii\base; 9 10 use Yii; 11 us

Oracle学习笔记十四 内置程序包

扩展数据库的功能 为 PL/SQL 提供对 SQL 功能的访问 用户 SYS 拥有所有程序包 是公有同义词 可以由任何用户访问 一些内置程序包 程序包名称 说明 STANDARD和DBMS_STANDARD 定义和扩展PL/SQL语言环境 DBMS_LOB 提供对 LOB数据类型进行操作的功能 DBMS_OUTPUT 处理PL/SQL块和子程序输出调试信息 DBMS_RANDOM 提供随机数生成器 DBMS_SQL 允许用户使用动态 SQL DBMS_XMLDOM 用DOM模型读写XML类型的数

Java中执行存储过程和函数(web基础学习笔记十四)

一.概述 如果想要执行存储过程,我们应该使用 CallableStatement 接口. CallableStatement 接口继承自PreparedStatement 接口.所以CallableStatement 接口包含有Statement 接口和PreparedStatement 接口定义的全部方法,但是并不是所有的方法我们都要使用,主要使用的方法有这样几个: CallableStatement 常用方法: 返回类型 方法签名 说明 boolean execute() 执行 SQL 语句