两天学会DirectX 3D之第二天
提要
前几天非常easy地跑了一个DirectX 9 程序,以为DirectX就那么绘制,事实证明有点Naive了。
之前的那个程序最多也就是个固定流水线的东西。
可是今天要用DirectX11来写一个小的框架。
龙书就不要看了,看Introduction to 3D GAME PROGRAMMING WITH DIRECTX®11
几个重要的类
ID3D11Device : 一个虚拟适配器。它被用于执行渲染和创建资源。
ID3D11DeviceContext: represents a device context which generates rendering commands.
ID3D11RenderTargetView: identifies the render-target subresources that can be accessed during rendering.
ID3D11InputLayout: An input-layout interface holds a definition of how to feed vertex data that is laid out in memory into the input-assembler stage of the graphics pipeline.
渲染流水线(翻译自微软文档)
DirectX编程流水线是为了实时游戏应用设计的,上图显示了由输入到输出的各个阶段的数据流向。相对于DirectX10 的图形流水线。DirectX11加入了一些额外的Stage来支持一些新的特性。
你能够使用DirectX 11API来配置全部的Stage,通过HLSL语言来设置就能够了。这样整个流水线就拥有非常大的可扩展性和适应性了。
以下列出每一个阶段所做的事情.
Input-Assembler Stage : 提供渲染时的数据(三角形,线。点)。
Vertex-Shader Stage:处理顶点,通常的操作有:Transformmation,蒙皮,光照计算。通常一个VertexShader输入是一组顶点,输出也是一组顶点。
Geometry-Shader Statge:这个阶段会处理全部的图元,输入是完整的图元(三角形就是三个顶点,线段就是两个顶点,还有就是单个的点)。
另外。每一个图元能够包括邻接图元的信息。另外这个阶段还能够对图元进行一定的简化和精细化。
给定一个图元,geometry shader能够丢弃这个图元,或者能够生成新的一个或者多个的图元。
Stream-Output Stage: 从上一个阶段流下来的数据,能够将图元信息从流水线放入到存储中。或者放到Rasterizer阶段。被放到内存中的数据能够再次放到流水线中。或者被CPU读取。
Rasterizer Stage:裁剪图元,为pixel shader准备图元,并准备如何调用pixel shader.
Pixel-Shader Stage: 收到经过插值的结果,生成终于的图像。
Output-Merger Stage :合并各种类型的输出信息(pixel shader 值。深度信息,Stencil值)。并和当前render target的深度/Stencil 缓存。得到最后的流水线结果。
Tessellation stage:这个阶段由Hull-shader, tessellator还有domain-shader组成,它主要是将高阶表面转换成一系列的三角行。然后放到流水线中。
Direct3D 11 可编程流水线也能够用快速的计算任务。一个compute shader能够将Direct3D11扩展用于通用GPU计算。
用Shader绘制一个三角形
Direct3D。我们须要完毕以下几个步骤:
1.定义我们须要检查的设备类型(device types)和特征级别(feature levels)
2.创建Direct3D设备。渲染设备(context)和交换链(swap chain)。
3.创建渲染目标(render target)。
4.设置视口(viewport)
5.開始渲染
6.渲染模型
7.清屏幕
代码清单
// include the basic windows header files and the Direct3D header files
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include "assimpmodel.h" // include the Direct3D Library file
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib") // define the screen resolution
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600 // global declarations
IDXGISwapChain *swapchain; // the pointer to the swap chain interface
ID3D11Device *dev; // the pointer to our Direct3D device interface
ID3D11DeviceContext *devcon; // the pointer to our Direct3D device context
ID3D11RenderTargetView *backbuffer; // the pointer to our back buffer
ID3D11InputLayout *pLayout; // the pointer to the input layout
ID3D11VertexShader *pVS; // the pointer to the vertex shader
ID3D11PixelShader *pPS; // the pointer to the pixel shader
ID3D11Buffer *pVBuffer; // the pointer to the vertex buffer // a struct to define a single vertex
struct VERTEX{ D3DXVECTOR3 position; D3DXCOLOR Color; }; // function prototypes
void InitD3D(HWND hWnd); // sets up and initializes Direct3D
void RenderFrame(void); // renders a single frame
void CleanD3D(void); // closes Direct3D and releases memory
void InitGraphics(void); // creates the shape to render
void InitPipeline(void); // loads and prepares the shaders // the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc; ZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass"; RegisterClassEx(&wc); RECT wr = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); hWnd = CreateWindowEx(NULL,
"WindowClass",
"Triangle",
WS_OVERLAPPEDWINDOW,
300,
300,
wr.right - wr.left,
wr.bottom - wr.top,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hWnd, nCmdShow); // set up and initialize Direct3D
InitD3D(hWnd); // enter the main loop: MSG msg; while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg); if (msg.message == WM_QUIT)
break;
} RenderFrame();
} // clean up DirectX and COM
CleanD3D(); return msg.wParam;
} // this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
} return DefWindowProc(hWnd, message, wParam, lParam);
} // this function initializes and prepares Direct3D for use
void InitD3D(HWND hWnd)
{
// create a struct to hold information about the swap chain
DXGI_SWAP_CHAIN_DESC scd; // clear out the struct for use
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); // fill the swap chain description struct
scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = 4; // how many multisamples
scd.Windowed = TRUE; // windowed/full-screen mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching // create a device, device context and swap chain using the information in the scd struct
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapchain,
&dev,
NULL,
&devcon); // get the address of the back buffer
ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); // use the back buffer address to create the render target
dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
pBackBuffer->Release(); // set the render target as the back buffer
devcon->OMSetRenderTargets(1, &backbuffer, NULL); // Set the viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = SCREEN_WIDTH;
viewport.Height = SCREEN_HEIGHT; devcon->RSSetViewports(1, &viewport); InitPipeline();
InitGraphics();
} // this is the function used to render a single frame
void RenderFrame(void)
{
// clear the back buffer to a deep blue
devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f)); // select which vertex buffer to display
UINT stride = sizeof(VERTEX);
UINT offset = 0;
devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset); // select which primtive type we are using
devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // draw the vertex buffer to the back buffer
devcon->Draw(3, 0); // switch the back buffer and the front buffer
swapchain->Present(0, 0);
} // this is the function that cleans up Direct3D and COM
void CleanD3D(void)
{
swapchain->SetFullscreenState(FALSE, NULL); // switch to windowed mode // close and release all existing COM objects
pLayout->Release();
pVS->Release();
pPS->Release();
pVBuffer->Release();
swapchain->Release();
backbuffer->Release();
dev->Release();
devcon->Release();
} // this is the function that creates the shape to render
void InitGraphics()
{
//Assimpmodel *model = new Assimpmodel();
//model->Initialize(dev);
// create a triangle using the VERTEX struct
VERTEX OurVertices[] =
{
{ D3DXVECTOR3(0.0f, 0.5f, 0.0f), D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
{ D3DXVECTOR3(0.45f, -0.5, 0.0f), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) },
{ D3DXVECTOR3(-0.45f, -0.5f, 0.0f), D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) }
}; // create the vertex buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D11_USAGE_DYNAMIC; // write access access by CPU and GPU
bd.ByteWidth = sizeof(VERTEX)* 3; // size is the VERTEX struct * 3
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; // use as a vertex buffer
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // allow CPU to write in buffer dev->CreateBuffer(&bd, NULL, &pVBuffer); // create the buffer // copy the vertices into the buffer
D3D11_MAPPED_SUBRESOURCE ms;
devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer
memcpy(ms.pData, OurVertices, sizeof(OurVertices)); // copy the data
devcon->Unmap(pVBuffer, NULL); // unmap the buffer
} // this function loads and prepares the shaders
void InitPipeline()
{
// load and compile the two shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile("shaders.shader", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile("shaders.shader", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0); // encapsulate both shaders into shader objects
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS); // set the shader objects
devcon->VSSetShader(pVS, 0, 0);
devcon->PSSetShader(pPS, 0, 0); // create the input layout object
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
}; dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devcon->IASetInputLayout(pLayout);
}
shader.shader
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
}; VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
VOut output; output.position = position;
output.color = color; return output;
} float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
代码就不具体说了,具体能够看參考的链接。
结果例如以下
封装出一个简单的框架
将全部的代码都写在main.cpp里肯定不是太好,最好按功能抽象出各种类型。以下是參照教程写的一个框架。
简单的一个框架。还是渲染一个小小的三角形。可是是抽了非常多个类出来。比方相机,输入之类的,方面后面扩展。
当然还是一个比較槽的框架哈。
代码直接看github吧。
參考
Graphics Pipeline - https://msdn.microsoft.com/en-us/library/windows/desktop/ff476882(v=vs.85).aspx
《Introduction to 3D GAME PROGRAMMING WITH DIRECTX®11》
DirectX 11 Tutorials - http://www.rastertek.com/tutdx11.html
http://www.directxtutorial.com/default.aspx
两天学会DirectX 3D之第二天的更多相关文章
- 两天学会DirectX 3D之入门
		
环境配置以及背景知识 环境 Windows 8.1 64bit VS2013 Microsoft DirectX SDK (June 2010) NVDIA Geforce GT755 环境的配置參考 ...
 - 两周“学会”bootstrap搭建一个移动站点
		
一直想着用bootstrap搭建网站,它的自适应.元素封装完善.现成的Glyphicons字体图标,省去很多的css.js.ui的工作,可以快速搭建一个客户需要的站点.ytkah自己有一些div+cs ...
 - 【公众号系列】两分钟学会SAP F1技巧
		
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[公众号系列]两分钟学会SAP F1技巧 写 ...
 - DirectX 3D 之C#开发
		
C#下进行directX的3D开发,一个旋转的4棱锥的例子. 建议看两个文档<Managed DirectX 9图形和游戏编程简略中文文档>和<Managed DirectX 9 S ...
 - Directx 3D编程实例:绘制可变速旋转的三角形
		
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要. 写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博 ...
 - Directx 3D编程实例:随机绘制的立体图案旋转
		
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要. 写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博 ...
 - Directx 3D编程实例:绘制3DMesh
		
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...
 - Directx 3D编程实例:多个3D球的综合Directx实例
		
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...
 - 武汉新芯:定位存储器制造,两年后或推3D NAND
		
武汉新芯集成电路制造公司(XMC)2006成立,2012年底起独立经营,是国有制企业.为了区别于本土的制造巨头SMIC(中芯国际)和华力微电子(HLMC)等,XMC将立足存储器制造.近日,武汉新芯董事 ...
 
随机推荐
- Linux内核部件分析  原子性操作atomic_t
			
在任何处理器平台下,都会有一些原子性操作,供操作系统使用,我们这里只讲x86下面的.在单处理器情况下,每条指令的执行都是原子性的,但在多处理器情况下,只有那些单独的读操作或写操作才是原子性的.为了弥补 ...
 - 【DB2】NVL2函数
			
语法: NVL2(表达式1,表达式2,表达式3) 如果表达式1为空,返回值为表达式3的值.如果表达式1不为空,返回值为表达式2的值. 例子: SELECT ID, NVL2(SEX,'非空','空值' ...
 - 一个完全摆脱findViewById的自动绑定库
			
代码地址如下:http://www.demodashi.com/demo/13504.html 问题 先来看一个正常的写法: <?xml version="1.0" enco ...
 - android实现超酷的腾讯视频首页和垂直水平网格瀑布流一揽子效果
			
代码地址如下:http://www.demodashi.com/demo/13381.html 先来一波demo截图 实现ListView.GridView.瀑布流 1.导入RecyclerView的 ...
 - oracle 查询 函数练习2
			
/*以下代码是对emp表/dept表/salgrade表进行显示宽度设置 */col empno for 9999;col ename for a10;col job for a10;col mgr ...
 - 修改配置nginx,限制无良爬虫频率
			
配置如下: #全局配置 limit_req_zone $anti_spider zone=anti_spider:10m rate=15r/m; #某个server中 limit_req zone=a ...
 - 贪吃蛇easyx版本
			
这学期学了图形交互学,三个星期下来,突然意识到已经可以用c++写一个贪吃蛇了. 于是就用了两天写了这个小游戏. 其中一天写了核心代码,半天找核心代码中的bug,还有半天进行了界面及操作的优化. 但是有 ...
 - rsync工具介绍
			
rsync工具介绍 http://man.linuxde.net/rsync rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法” ...
 - ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程)
			
ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程) ufldl出了新教程,感觉比之前的好,从基础讲起,系统清晰,又有编程实践. 在dee ...
 - .NET面试题(二)
			
基础方面: 1.简单的描述一下C# 中重写,重载,隐藏的概念 C#中重写(overide)是子类继承父类后,对父类中的方法进行行为改写. 重载是指方法名相同,参数不同 重写和隐藏的定义: 重写:基类方 ...