初学DirectX11, 留个纪恋。
以前学的是openGL, 最近才开始学DirectX11,写了个很垃圾的代码,怀念以前的glPushMatrix(), glPopMatrix(), glBegin(), glEnd(), 多简单啊,可惜在openGL4后面的版本中放弃了这些经典的函数,改成了跟directX差不多的自定义管线, 我觉得openGL已经被改的面目全非了,可能是openGL慢的缘故吧。openGL4.3的VAO,VBO还是不能理解。
写了个垃圾的程序,许多个几何体,自己写函数实现了以前glPushMatrix(),glPopMatrix()的功能。界面还是Qt写的,毕竟方便,调用API函数代码量太大。DirectX的代码真多啊,以后分装成一个类库试试。
贴一下主要的代码吧。

几何体顶点过多变成球的时候,运行很卡,我的代码效率海水很差啊。
#ifndef DWIDGET_H
#define DWIDGET_H
#include "xBasic.h"
#include <d3d11.h>
#include <DirectXMath.h>
#include "DPainter.h"
#include <QWidget>
#include <stack> class DPainter; class DWidget : public QWidget
{
Q_OBJECT public:
DWidget(QWidget *parent = nullptr);
~DWidget();
void rotateX();
void rotateY();
void rotateZ(); protected:
virtual void paintEvent(QPaintEvent *event);
virtual void resizeEvent(QResizeEvent *event);
void mousePressEvent(QMouseEvent *event);
virtual QPaintEngine *paintEngine() const { return nullptr; } private:
void updateWVP();
void initializeD3D();
void pushWorldMatrix();
void multipyWorldMatrix(DirectX::XMMATRIX *matrix);
void popWorldMatrix();
void worldMatrixIdentity();
void setCurrentWorldMatrix(DirectX::XMMATRIX *matrix); std::stack<XMFLOAT4X4> worldStack;
DPainter *d3dPainter;
HWND hwnd; ID3D11Device *device;
ID3D11DeviceContext *context;
IDXGISwapChain *swapChain;
ID3D11RenderTargetView *renderTargetView;
ID3D11VertexShader *VS;
ID3D11PixelShader *PS;
ID3D10Blob *VS_Buffer;
ID3D10Blob *PS_Buffer;
ID3D11Buffer *constantBuffer;
ID3D11InputLayout *vertexLayout;
ID3D11Buffer *worldBuffer;
ID3D11Buffer *viewBuffer;
ID3D11Buffer *projectionBuffer;
ID3D11DepthStencilView *depthStencilView;
ID3D11Texture2D *depthStencilBuffer;
ID3D11DepthStencilState *depthStencilState; XMFLOAT4X4 world;
XMFLOAT4X4 view;
XMFLOAT4X4 projection;
XMFLOAT4 eye;
XMFLOAT4 target;
XMFLOAT4 up;
CPerObject WVP; const D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", , DXGI_FORMAT_R32G32B32_FLOAT, , , D3D11_INPUT_PER_VERTEX_DATA, },
{ "COLOR", , DXGI_FORMAT_R32G32B32A32_FLOAT, , , D3D11_INPUT_PER_VERTEX_DATA, },
};
int numElements = ;
}; #endif // DWIDGET_H
// cpp文件
#include "DWidget.h"
#include <qevent.h>
#include "GeometryProvider.h" ID3D11Buffer *boolBuffer;
ID3D11DepthStencilView* pDSV;
struct ABCD
{
bool a;
}isT; ID3D11Buffer *cylinderVertexBuffer;
ID3D11Buffer *cylinderIndexBuffer; DWidget::DWidget(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_PaintOnScreen, true);
setAttribute(Qt::WA_NativeWindow, true);
setAutoFillBackground(false);
setFocusPolicy(Qt::StrongFocus);
hwnd = (HWND)winId();
initializeD3D();
depthStencilBuffer = nullptr;
depthStencilView = nullptr;
d3dPainter = new DPainter;
d3dPainter->context = context;
d3dPainter->device = device;
d3dPainter->createSphere(, );
} DWidget::~DWidget()
{
context->Release();
device->Release();
} void DWidget::rotateX()
{
XMMATRIX tmpWord;// = XMLoadFloat4x4(&world);
XMVECTOR anx = XMVectorSet(, , , );
tmpWord = XMMatrixRotationAxis(anx, 0.05);
multipyWorldMatrix(&tmpWord);
update();
} void DWidget::rotateY()
{
XMMATRIX tmpWord;// = XMLoadFloat4x4(&world);
XMVECTOR anx = XMVectorSet(, , , );
tmpWord = XMMatrixRotationAxis(anx, 0.05);
multipyWorldMatrix(&tmpWord);
update();
} void DWidget::rotateZ()
{
XMMATRIX tmpWord;
XMVECTOR anx = XMVectorSet(, , , );
tmpWord = XMMatrixRotationAxis(anx, 0.01);
multipyWorldMatrix(&tmpWord);
update();
} void DWidget::paintEvent(QPaintEvent * event)
{
FLOAT bgColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
context->ClearRenderTargetView(renderTargetView, bgColor);
context->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0.0f); {
pushWorldMatrix();
multipyWorldMatrix(&XMMatrixScaling(0.14, 0.14, 0.14));
multipyWorldMatrix(&XMMatrixTranslation(-, -, -)); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Vertex);
UINT offset = ;
context->IASetVertexBuffers(, , &cylinderVertexBuffer, &stride, &offset);
context->IASetIndexBuffer(cylinderIndexBuffer, DXGI_FORMAT_R32_UINT, );
context->DrawIndexed(, , ); {
pushWorldMatrix();
multipyWorldMatrix(&XMMatrixTranslation(, , ));
multipyWorldMatrix(&XMMatrixScaling(0.1, 0.1, 0.1)); d3dPainter->drawSphere(); popWorldMatrix();
} for (int i = ; i < ; i++)
{
for (int j = ; j <= ; j++)
{
for (int k = ; k < ; k++)
{
pushWorldMatrix();
multipyWorldMatrix(&XMMatrixTranslation(i, j, k));
multipyWorldMatrix(&XMMatrixScaling(0.1, 0.1, 0.1));
d3dPainter->drawSphere();
popWorldMatrix(); }
}
} popWorldMatrix();
}//d3dPainter->drawSphere(); swapChain->Present(, ); } void DWidget::updateWVP()
{
XMStoreFloat4x4(&WVP.view, XMMatrixTranspose(XMLoadFloat4x4(&view)));
XMStoreFloat4x4(&WVP.world, XMMatrixTranspose(XMLoadFloat4x4(&world)));
XMStoreFloat4x4(&WVP.projection, XMMatrixTranspose(XMLoadFloat4x4(&projection)));
context->UpdateSubresource(constantBuffer, , NULL, &WVP, , );
context->VSSetConstantBuffers(, , &constantBuffer);
::isT.a = ;
} void DWidget::resizeEvent(QResizeEvent * event)
{
int width = this->width();
int height = this->height();
::xBasicChangeSize(device, context, swapChain, &renderTargetView, width, height); D3D11_TEXTURE2D_DESC depthStencilBufferDesc;
ZeroMemory(&depthStencilBufferDesc, sizeof(D3D11_TEXTURE2D_DESC));
depthStencilBufferDesc.Width = width;
depthStencilBufferDesc.Height = height;
depthStencilBufferDesc.MipLevels = ;
depthStencilBufferDesc.ArraySize = ;
depthStencilBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilBufferDesc.SampleDesc.Count = ;
depthStencilBufferDesc.SampleDesc.Quality = ;
depthStencilBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilBufferDesc.CPUAccessFlags = ;
depthStencilBufferDesc.MiscFlags = ; device->CreateTexture2D(&depthStencilBufferDesc, NULL, &depthStencilBuffer); D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; device->CreateDepthStencilState(&depthStencilDesc, &depthStencilState); context->OMSetDepthStencilState(depthStencilState, ); D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory(&depthStencilViewDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = ; device->CreateDepthStencilView(depthStencilBuffer, &depthStencilViewDesc, &depthStencilView);
context->OMSetRenderTargets(, &renderTargetView, depthStencilView); XMMATRIX tmpProjection = XMMatrixPerspectiveFovLH(XM_PIDIV2 / , (float)width / (float)height, 0.01f, 100.0f);
XMStoreFloat4x4(&projection, tmpProjection);
} void DWidget::mousePressEvent(QMouseEvent * event)
{
} void DWidget::initializeD3D()
{
::xBasicInitializeDirectX11(hwnd, &swapChain, &device, &context, &renderTargetView);
::xBasicCreateShader(device, context, L"Effects.fx", &VS_Buffer, &PS_Buffer, &VS, &PS);
::xBasicCreateBuffer(device, D3D11_BIND_CONSTANT_BUFFER, sizeof(CPerObject), &WVP, &constantBuffer);
::xBasicCreateBuffer(device, D3D11_BIND_CONSTANT_BUFFER, , &::isT, &::boolBuffer); XMMATRIX tmpWorld = XMMatrixScaling(0.1, 0.1, 0.1) *XMMatrixTranslation( * , * , );;
XMStoreFloat4x4(&world, tmpWorld); XMVECTOR eye = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
XMVECTOR at = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMStoreFloat4x4(&view, XMMatrixLookAtLH(eye, at, up)); //Create the Input Layout
device->CreateInputLayout(layout, , VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertexLayout); //Set the Input Layout
context->IASetInputLayout(vertexLayout); GeometryProvider provider;
std::vector<Vertex> vertices;
std::vector<UINT> indices;
GeometryProvider::MeshData data;
provider.createCylinder(data, );
for (size_t i = ; i < data.vertices.size(); i++)
{
Vertex tmpV;
tmpV.pos = data.vertices[i].position;
tmpV.color = XMFLOAT4(rand() % , rand() % , rand() % , );
vertices.push_back(tmpV);
} for (size_t i = ; i < data.indices.size(); i++)
{
indices.push_back(data.indices[i]);
} ::xBasicCreateBuffer(device, D3D11_BIND_VERTEX_BUFFER, vertices.size() * sizeof(Vertex), &vertices[], &cylinderVertexBuffer);
::xBasicCreateBuffer(device, D3D11_BIND_INDEX_BUFFER, indices.size() * sizeof(UINT), &indices[], &cylinderIndexBuffer);
} void DWidget::pushWorldMatrix()
{
if (worldStack.empty())
{
worldMatrixIdentity();
}
worldStack.push(worldStack.top());
} void DWidget::multipyWorldMatrix(DirectX::XMMATRIX * matrix)
{
if (worldStack.empty())
{
worldMatrixIdentity();
} XMMATRIX aMatrix = XMLoadFloat4x4(&worldStack.top());
XMMATRIX ansMatrix = *matrix * aMatrix;
XMStoreFloat4x4(&worldStack.top(), ansMatrix); world = worldStack.top();
updateWVP();
} void DWidget::popWorldMatrix()
{
worldStack.pop(); world = worldStack.top(); updateWVP();
} void DWidget::worldMatrixIdentity()
{
while (!worldStack.empty())
{
worldStack.pop();
} worldStack.push(XMFLOAT4X4()); XMStoreFloat4x4(&worldStack.top(), XMMatrixIdentity()); world = worldStack.top();
updateWVP();
} void DWidget::setCurrentWorldMatrix(DirectX::XMMATRIX * matrix)
{
if (worldStack.empty())
{
worldMatrixIdentity();
} XMMATRIX aMatrix = XMLoadFloat4x4(&worldStack.top());
XMMATRIX ansMatrix = *matrix * aMatrix;
XMStoreFloat4x4(&worldStack.top(), ansMatrix); world = worldStack.top();
updateWVP();
}
有些很奇葩的写法就不管了。
初学DirectX11, 留个纪恋。的更多相关文章
- python初学代码留个纪念
最简单的代码 if else if else 1.python中else if 用 elif表示 2.注释: 单行注释:##### 多行注释:''' ------''',"&q ...
- 3500常用汉字与标点符号(已排除不支持GB2312的)
.?!,.::“”‘’…()<>〈〉[].,:;!?-'_"'()[]<>|&~;+-*/=<>0123456789ABCEFGHIJKLMNOP ...
- [商业_法务] 2、注册公司起名很费劲,用C++怒写个随机名字生成器
前言 博主最近在注册公司,由于之前听说过注册公司的名字很难通过,于是便直接找代理去帮忙跑趟,为确保万无一失,还自己绞尽脑汁想了几个很奇葩的名字(噬菌体.云木.灌木.杏仁...). 但是不幸的是那些奇葩 ...
- Python抓取百度汉字笔画的gif
偶然发现百度汉语里面,有一笔一划的汉字顺序: 觉得这个动态的图片,等以后娃长大了,可以用这个教写字.然后就去找找常用汉字,现代汉语常用字表 .拿到这里面的汉字,做两个数组出来,一共是 ...
- Java随机产生中文昵称
有时候我们注册一个网站第一次登陆系统会产生一个随机昵称供用户选择,在项目测试阶段遇到了这个问题,因为注册时没有让用户填写昵称,于是找了两种产生随机中文昵称的方法: 代码如下 package com.u ...
- [转]TrueType(TTF)字体文件裁剪(支持简体中文,繁体中文TTF字体裁剪)
原文入口: TTF字体文件裁剪(支持简体中文,繁体中文TTF字体裁剪) 对于TrueType(TTF)字体格式的介绍可以看: https://www.cnblogs.com/slysky/p/1131 ...
- RenderDoc图形调试器详细使用教程(基于DirectX11)
前言 由于最近Visual Studio的图形调试器老是抽风,不得不寻找一个替代品了. 对于图形程序开发者来说,学会使用RenderDoc图形调试器可以帮助你全面了解渲染管线绑定的资源和运行状态,从而 ...
- 【转】Directx11 HelloWorld之HLSL的Effect框架的使用
最近尝试用了下Directx下的Effect框架,作为一初学者初学者,说下为什么我们要使用Effect框架及其好处吧. 首先Effect最大好处的就是简单,使得编写Shader绘制的程序工作量大大下降 ...
- DirectX11 With Windows SDK--25 法线贴图
前言 在很早之前的纹理映射中,纹理存放的元素是像素的颜色,通过纹理坐标映射到目标像素以获取其颜色.但是我们的法向量依然只是定义在顶点上,对于三角形面内一点的法向量,也只是通过比较简单的插值法计算出相应 ...
随机推荐
- css实现单行,多行文本溢出显示省略号……
1.单行文本溢出显示省略号我们可以直接用text-overflow: ellipsis 实现方法: <style> .div_text{width: 300px; padding:10px ...
- 套用JQuery EasyUI列表显示数据、分页、查询
声明,本博客从csdn搬到cnblogs博客园了,以前的csdn不再更新,朋友们可以到这儿来找我的文章,更多的文章会发表,谢谢关注! 有时候闲的无聊,看到extjs那么肥大,真想把自己的项目改了,最近 ...
- 【需求设计1】VIP积分系统无聊YY
RT,想到什么就写什么呗,这是最简单的方式,顺便给自己做一个记录,反正自己记忆力也不太好.本文是仿陆金所的积分系统,自己YY的一套东西. 首先我想做一个VIP兑换投资卷的功能: 我们先来确定一些我知道 ...
- CSS 3学习——transform 2D转换
首先声明一点,transform属性不为none的元素是它的定位子元素(绝对定位和固定定位)的包含块,而且对内创建一个新的层叠上下文. 注意:可以通过 transform-box 属性指定元素的那个盒 ...
- 海康网络摄像机YV12转换为BGR,由opencv Mat显示 (转)
我使用的是海康DS-2CD852MF-E, 200万,网络摄像机,已经比较老了,不过SDK在海康官网下载的,开发流程都差不多. 海康摄像机回调解码后的视频数据格式为YV12,顺便说一下YV12的数据格 ...
- BPM体系文件管理解决方案分享
一.方案概述 企业管理在很大程度上是通过文件化的形式表现出来,体系文件管理是管理体系存在的基础和证据,是规范企业管理活动和全体人员行为,达到管理目标的管理依据.对与公司质量.环境.职业健康安全等体系有 ...
- Linux下编译安装Vim8.0
什么是Vim? Vim 是经典的 UNIX 编辑器 Vi 的深度改良版本.它增加了许多功能,包括:多级撤销.格式高亮.命令行历史.在线帮助.拼写检查.文件名补完.块操作.脚本支持,等等.除了字符界面版 ...
- Linux设备管理(一)_kobject, kset,ktype分析
Linux内核大量使用面向对象的设计思想,通过追踪源码,我们甚至可以使用面向对象语言常用的UML类图来分析Linux设备管理的"类"之间的关系.这里以4.8.5内核为例从kobje ...
- VS2012+EF6+Mysql配置心路历程
为了学习ORM,选择了EntityFramework,经历了三天两夜的煎熬,N多次错误,在群里高手的帮助下,终于成功,现在将我的心路历程记录下来,一是让自己有个记录,另外就是让其它人少走些弯路. 我的 ...
- [PHP源码阅读]strtolower和strtoupper函数
字符串的操作函数中,字符串的大小写转换也算是比较常用的函数,其底层实现也比较简单,下面来一探究竟. 我在github上有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注 ...