SharpDX初学者教程第5部分:着色三角形
原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-5-coloring-the-triangle
在第4部分中,我们创建了一个三角形,我们可以在像素着色器中为整个三角形设置单一颜色。在本教程中,我们将看到如何为三角形的每个顶点添加单独的颜色。我们将继续处理上一个教程中的代码,您也可以在此处找到该代码。
最终结果如下:

1.创建顶点结构
请记住,当我们渲染三角形时,我们上传了一个数组Vector3,因为每个顶点只是一个位置。现在我们希望每个顶点都有一个位置和一个颜色。因此,我们首先创建一个VertexPositionColor.cs通过右键单击项目调用的新文件,然后选择Add -> New Item...:

选择“ 代码文件 ” 类型,并为其命名VertexPositionColor.cs,然后单击“ 添加”:

现在使用以下代码创建一个结构:
using SharpDX;
using System.Runtime.InteropServices;
namespace MySharpDXGame
{
    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct VertexPositionColor
    {
        public readonly Vector3 Position;
        public readonly Color4 Color;
        public VertexPositionColor(Vector3 position, Color4 color)
        {
            Position = position;
            Color = color;
        }
    }
}
所以,现在我们有一个不可变的结构,它同时具有Position和Color。我们还将该[StructLayoutAttribute(LayoutKind.Sequential)] 属性添加到结构中。这样可以确保值以与struct中指定的顺序相同的顺序排列在内存中。这很重要,因为我们后来需要告诉GPU。
2.更改顶点
作为下一步,我们需要Game.cs使用新顶点类型的数组替换上一个教程中的顶点数组。我们还为每个顶点选择一种颜色。所以替换这段代码:
private Vector3[] vertices = new Vector3[] { new Vector3(-0.5f, 0.5f, 0.0f), new Vector3(0.5f, 0.5f, 0.0f), new Vector3(0.0f, -0.5f, 0.0f) };
使用此代码:
private VertexPositionColor[] vertices = new VertexPositionColor[]
{
    new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f), SharpDX.Color.Red),
    new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), SharpDX.Color.Green),
    new VertexPositionColor(new Vector3(0.0f, -0.5f, 0.0f), SharpDX.Color.Blue)
};
3.更新VertexBuffer
我们还需要更改我们的顶点缓冲区以包含新的顶点结构而不是Vector3,所以<Vector3>从以下行中删除(在InitializeTriangle()方法中):
triangleVertexBuffer = D3D11.Buffer.Create<Vector3>(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices);
现在编译器会自动推断它是<VertexPositionColor>来自参数的,所以如果我们以后更改它,我们将不需要再次更改。
我们也必须改变在顶点缓冲器中的每个元件的尺寸,所以更换Vector3 用VertexPositionColor 下面的行(在Draw()法):
d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf<Vector3VertexPositionColor>(), 0));
4.更改输入布局
您可能还记得在上一个教程中我们创建了一个输入布局,它描述了顶点数据的结构。在上一个教程中,它看起来像这样:
private D3D11.InputElement[] inputElements = new D3D11.InputElement[]
{
    new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0)
};
由于我们还为每个顶点添加了颜色,我们需要将输入布局更改为以下内容:
private D3D11.InputElement[] inputElements = new D3D11.InputElement[]
{
    new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0),
    new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0, D3D11.InputClassification.PerVertexData, 0)
};
我们添加了另一个InputElement,其中类型是a R32G32B32A32_Float,因为SharpDX.Color4我们在vertex结构中使用的结构包含4个浮点数(以RGBA顺序)。我们还添加了一些其他参数,大多数现在都是无关紧要的,除了第4个,我们为“POSITION”指定0 ,但为“COLOR”元素指定12 。此参数是此数据开始的结构中的偏移量(以字节为单位),并且因为位置首先出现偏移0,并且每个位置Vector3 包含3个浮点数(x,y,z),每个字节数为4个字节= 12字节。因此,颜色数据将在12个字节后找到。
5.更新顶点着色器
打开vertexShader.hlsl文件。首先,我们将更改主要功能参数列表以包含颜色:
float4 main(float4 position : POSITION, float4 color : COLOR) : SV_POSITION
{
   [...]
}
正如您所看到的,顶点着色器现在也会接收颜色,但由于颜色是在像素着色器中设置的,因此我们还需要从此函数返回颜色。要从函数返回多个值,我们需要创建一个结构,其中包含着色器文件顶部的位置和颜色:
struct VSOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};
现在我们将函数返回值更改为此结构,并删除SV_POSITION 语义:
VSOut main(float4 position : POSITION, float4 color : COLOR)
{
    […]
}
而不是仅仅从着色器返回位置,我们将创建一个VSOut 结构,并在其中设置位置和颜色值。所以最终的顶点着色器应如下所示:
struct VSOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};
VSOut main(float4 position : POSITION, float4 color : COLOR)
{
    VSOut output;
    output.position = position;
    output.color = color;
    return output;
}
6.更新像素着色器
在像素着色器(pixelShader.hlsl)中,我们需要将颜色添加为函数参数,然后返回红色而不是返回提供的颜色,因此完整像素着色器应如下所示:
float4 main(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
    return color;
}
7.修复bug
如果您在02-08-2016之前已经按照之前的教程进行操作,那么您可能会遇到错误(在GitHub repo和教程中已修复)。你必须移动这一行 Game.cs:
d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView);
从最后开始InitializeDeviceResources(), 并将其置于第一位Draw(),因为必须在每帧的开头调用它。
8.最终结果
我们现在已经完成了,如果您运行该项目,您现在应该得到以下结果:

和往常一样,GitHub上提供了完整的代码:https://github.com/mrjfalk/SharpDXTutorials/tree/master/BeginnersTutorial-Part5
public class Game : IDisposable
{
private RenderForm renderForm; private const int Width = ;
private const int Height = ; private D3D11.Device d3dDevice;
private D3D11.DeviceContext d3dDeviceContext;
private SwapChain swapChain;
private D3D11.RenderTargetView renderTargetView;
private Viewport viewport; // Shaders
private D3D11.VertexShader vertexShader;
private D3D11.PixelShader pixelShader;
private ShaderSignature inputSignature;
private D3D11.InputLayout inputLayout; private D3D11.InputElement[] inputElements = new D3D11.InputElement[]
{
new D3D11.InputElement("POSITION", , Format.R32G32B32_Float, , , D3D11.InputClassification.PerVertexData, ),
new D3D11.InputElement("COLOR", , Format.R32G32B32A32_Float, , , D3D11.InputClassification.PerVertexData, )
}; // Triangle vertices
private VertexPositionColor[] vertices = new VertexPositionColor[] { new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f), SharpDX.Color.Red), new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), SharpDX.Color.Green), new VertexPositionColor(new Vector3(0.0f, -0.5f, 0.0f), SharpDX.Color.Blue) };
private D3D11.Buffer triangleVertexBuffer; /// <summary>
/// Create and initialize a new game.
/// </summary>
public Game()
{
// Set window properties
renderForm = new RenderForm("My first SharpDX game");
renderForm.ClientSize = new Size(Width, Height);
renderForm.AllowUserResizing = false; InitializeDeviceResources();
InitializeShaders();
InitializeTriangle();
} /// <summary>
/// Start the game.
/// </summary>
public void Run()
{
// Start the render loop
RenderLoop.Run(renderForm, RenderCallback);
} private void RenderCallback()
{
Draw();
} private void InitializeDeviceResources()
{
ModeDescription backBufferDesc = new ModeDescription(Width, Height, new Rational(, ), Format.R8G8B8A8_UNorm); // Descriptor for the swap chain
SwapChainDescription swapChainDesc = new SwapChainDescription()
{
ModeDescription = backBufferDesc,
SampleDescription = new SampleDescription(, ),
Usage = Usage.RenderTargetOutput,
BufferCount = ,
OutputHandle = renderForm.Handle,
IsWindowed = true
}; // Create device and swap chain
D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out d3dDevice, out swapChain);
d3dDeviceContext = d3dDevice.ImmediateContext; viewport = new Viewport(, , Width, Height);
d3dDeviceContext.Rasterizer.SetViewport(viewport); // Create render target view for back buffer
using(D3D11.Texture2D backBuffer = swapChain.GetBackBuffer<D3D11.Texture2D>())
{
renderTargetView = new D3D11.RenderTargetView(d3dDevice, backBuffer);
}
} private void InitializeShaders()
{
// Compile the vertex shader code
using(var vertexShaderByteCode = ShaderBytecode.CompileFromFile("vertexShader.hlsl", "main", "vs_4_0", ShaderFlags.Debug))
{
// Read input signature from shader code
inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode); vertexShader = new D3D11.VertexShader(d3dDevice, vertexShaderByteCode);
} // Compile the pixel shader code
using(var pixelShaderByteCode = ShaderBytecode.CompileFromFile("pixelShader.hlsl", "main", "ps_4_0", ShaderFlags.Debug))
{
pixelShader = new D3D11.PixelShader(d3dDevice, pixelShaderByteCode);
} // Set as current vertex and pixel shaders
d3dDeviceContext.VertexShader.Set(vertexShader);
d3dDeviceContext.PixelShader.Set(pixelShader); d3dDeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; // Create the input layout from the input signature and the input elements
inputLayout = new D3D11.InputLayout(d3dDevice, inputSignature, inputElements); // Set input layout to use
d3dDeviceContext.InputAssembler.InputLayout = inputLayout;
} private void InitializeTriangle()
{
// Create a vertex buffer, and use our array with vertices as data
triangleVertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices);
} /// <summary>
/// Draw the game.
/// </summary>
private void Draw()
{
// Set render targets
d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView); // Clear the screen
d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(, , )); // Set vertex buffer
d3dDeviceContext.InputAssembler.SetVertexBuffers(, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf<VertexPositionColor>(), )); // Draw the triangle
d3dDeviceContext.Draw(vertices.Count(), ); // Swap front and back buffer
swapChain.Present(, PresentFlags.None);
} public void Dispose()
{
inputLayout.Dispose();
inputSignature.Dispose();
triangleVertexBuffer.Dispose();
vertexShader.Dispose();
pixelShader.Dispose();
renderTargetView.Dispose();
swapChain.Dispose();
d3dDevice.Dispose();
d3dDeviceContext.Dispose();
renderForm.Dispose();
}
}
SharpDX初学者教程第5部分:着色三角形的更多相关文章
- SharpDX初学者教程第4部分:绘制三角形
		
原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-4-drawing-a-triangle 现在我们有了一个Direct3 ...
 - SharpDX初学者教程第3部分:初始化DirectX
		
原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-3-initializing-directx 在这部分中,我们将初始化D ...
 - SharpDX初学者教程第1部分:在Visual Studio 2013中设置SharpDX项目
		
原文 http://www.johanfalk.eu/blog/sharpdx-tutorial-part-1-setting-up-a-sharpdx-project-in-visual-studi ...
 - SharpDX初学者教程第2部分:创建窗口
		
原文 http://www.johanfalk.eu/blog/sharpdx-tutorial-part-2-creating-a-window 在第二篇教程中,我们将介绍如何创建一个稍后将呈现的简 ...
 - WebGL简易教程(三):绘制一个三角形(缓冲区对象)
		
目录 1. 概述 2. 示例:绘制三角形 1) HelloTriangle.html 2) HelloTriangle.js 3) 缓冲区对象 (1) 创建缓冲区对象(gl.createBuffer( ...
 - Go GraphQL初学者教程
		
Go GraphQL初学者教程 https://tutorialedge.net/golang/go-graphql-beginners-tutorial/ https://tutorialedge. ...
 - 《SLAM for Dummies》中文版《SLAM初学者教程》
		
SLAM for Dummies SLAM初学者教程A Tutorial Approach to Simultaneous Localization and Mapping 一本关于实时定位及绘图 ...
 - SLAM for Dummies  SLAM初学者教程 中文翻译 1到4章
		
SLAM for Dummies SLAM初学者教程A Tutorial Approach to Simultaneous Localization and Mapping 一本关于实时定位及绘图 ...
 - PANDAS 数据分析初学者教程
		
Pandas 初学者教程 2018-05-19 六尺巷人 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重要的Python包.它不仅提供了很多方法,使得数据处理非常 ...
 
随机推荐
- 理解es6箭头函数
			
箭头函数知识点很少,但是要理解清楚,不然看代码会很不适应的. 1. 最简单的写法 x => x*x 可以理解为 我的x要被转化为x*x,所以实际相当于下边的这个 function (x){ re ...
 - String--在内存中的表现
			
创建字符串的方法有两种: Stringstr1=”直接赋值法” Stringstr2=new String(“通过new关键字的方法来创建”); 在执行String str1=” ...
 - vue打包之部署在非根路径下的三两事
			
首先,感叹一下,2019年已经过去一半,想想自己做了些什么,好像也没做什么. 今天试着配一个nginx,以前的nginx都是指向的/根路径,今天的nginx指向的非/根路径,遇到许多问题的,总结总结. ...
 - 下载android4.4.2源代码全过程(附已下载的源代码)
			
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/maosidiaoxian/article/details/36625727 今天在下载andriod ...
 - Docker Tomcat部署
			
1.下载tomcat镜像 docker pull tomcat 2.上传项目 /etc/tomcat/webapps/ 3.启动tomcat容器 docker run -d --name tomcat ...
 - CentOS 7 SVN搭建 (YUM安装)
			
1.检查已安装版本 rpm -qa subversion卸载旧版本SVNyum remove subversion2.安装SVNyum install subversion 3.验证安装svnserv ...
 - 关系数据库理论 ch.6
			
6.1 问题的提出 关系模式是一个5元组 R U,D,DOM,F U 属性 D 域 DOM 属性到域的映射 F 依赖 在本章中将关系模式看作 三元组 R U,D 属性-依赖 1NF 每一个分量是不可分 ...
 - hdu 4512 (LCIS)
			
好久没写解题报告了,最近几周好忙...感觉是我进大学以来最忙的一段时间了,要给新生准备下周三比赛的题目,下周五要去南京赛区,回来之后马上就要期中考试了...不想挂科 额,很早之前就看过关于LCIS ...
 - jQuery 图片上传
			
1. 概述 1.1 说明 在一些前后端不分离的项目中,经常有一些需要把文件或者图片上传的功能,故记录此代码以便后期使用. 1.2 要求 1.上传,10M以内,限bmp,jpg,png,jpeg等图片格 ...
 - DIV元素垂直居中的分析与实现
			
首先,对于需要垂直居中的元素常规居中处理 margin: 0 auto; 接下来要设置div的祖先元素的宽高为100%, 在默认的设置中他们都为0: html, body { width: %; he ...