原英文版地址:http://www.rastertek.com/dx11tut06.html

在本教程中,我将介绍如何使用漫反射照明和Directx11来照亮三维对象。我们将从前面的教程中的代码开始并修改它。
我们将实现的漫射照明类型称为定向照明。定向照明与太阳照亮地球的方式相似。它是一个远离很远的光源,根据它发出光的方向,你可以确定任何物体上的光的数量。然而,与环境照明(另一种照明模式,我们将很快介绍)不同,它不会照亮它不直接接触的表面。
我选择了定向照明作为开始,因为它很容易进行视觉调试。此外,由于它只需要一个方向,因此公式比其他类型的漫射照明(如聚光灯和点光源)更简单。
Directx11中漫反射照明的实现是通过顶点和像素着色来完成的。漫射光只需要方向和一个我们想要照亮的任何多边形的法向量。方向是您定义的单个向量,您可以使用构成多边形的三个顶点来计算任意多边形的法向。在本教程中,我们还将在照明方程中实现漫射光的颜色。

框架
对于本教程,我们将创建一个名为LightClass的新类,它将表示场景中的光源。LightClass实际上只会保持光的方向和颜色。我们还将移除纹理haderClass,并将其替换为LightshaderClass,后者处理模型上的灯光阴影。随着新类的添加,框架现在看起来如下:

我们将通过查看HLSL灯光明暗器来启动代码部分。您将注意到灯光明暗器只是上一教程中纹理明暗器的更新版本。

Light.vs


// Filename: light.vs/
// GLOBALS //
/
cbuffer MatrixBuffer
{matrix worldMatrix;matrix viewMatrix;matrix projectionMatrix;
};

这两个结构现在都有一个3浮点法向量。法向量用于通过使用法向和光方向之间的角度计算光的数量。

//
// TYPEDEFS //
//
struct VertexInputType
{float4 position : POSITION;float2 tex : TEXCOORD0;float3 normal : NORMAL;
};struct PixelInputType
{float4 position : SV_POSITION;float2 tex : TEXCOORD0;float3 normal : NORMAL;
};// Vertex ShaderPixelInputType LightVertexShader(VertexInputType input)
{PixelInputType output;// Change the position vector to be 4 units for proper matrix calculations.input.position.w = 1.0f;// Calculate the position of the vertex against the world, view, and projection matrices.output.position = mul(input.position, worldMatrix);output.position = mul(output.position, viewMatrix);output.position = mul(output.position, projectionMatrix);// Store the texture coordinates for the pixel shader.output.tex = input.tex;
该顶点的法向量在世界空间中计算,然后在作为输入发送到像素着色器之前进行规格化。// Calculate the normal vector against the world matrix only.output.normal = mul(input.normal, (float3x3)worldMatrix);// Normalize the normal vector.output.normal = normalize(output.normal);return output;
}

Light.ps


// Filename: light.ps/
// GLOBALS //
/
Texture2D shaderTexture;
SamplerState SampleType;

我们在光缓冲区中有两个新的全局变量,用来保存漫反射颜色和光的方向。这两个变量将根据新LightClass对象中的值进行设置。

cbuffer LightBuffer
{float4 diffuseColor;float3 lightDirection;float padding;
};//
// TYPEDEFS //
//
struct PixelInputType
{float4 position : SV_POSITION;float2 tex : TEXCOORD0;float3 normal : NORMAL;
};// Pixel Shaderfloat4 LightPixelShader(PixelInputType input) : SV_TARGET
{float4 textureColor;float3 lightDir;float lightIntensity;float4 color;//在此纹理坐标位置使用取样器从纹理中采样像素颜色。
纹理颜色=shadertexture.sample(sampleType,input.tex);
这就是前面讨论过的照明方程现在实现的地方。光强值计算为三角形法向量与光方向向量之间的点积。
//反转灯光方向进行计算。lightDir = -lightDirection;//计算金额在这光的像素。
saturate(点光)(input.normal,lightdir));
与最后的值是一个组合的光散射的纹理的像素值来产生颜色的结果。//根据漫反射颜色和光强度确定漫反射颜色的最终数量。color = saturate(diffuseColor * lightIntensity);// Multiply the texture pixel and the final diffuse color to get the final pixel color result.color = color * textureColor;return color;
}

Lightshaderclass.h
新的lightshaderClass只是先前教程中的纹理shaderClass,它重新编写了一些内容,以合并光源。


// Filename: lightshaderclass.h#ifndef _LIGHTSHADERCLASS_H_
#define _LIGHTSHADERCLASS_H_//
// INCLUDES //
//
#include <d3d11.h>
#include <d3dx10math.h>
#include <d3dx11async.h>
#include <fstream>
using namespace std;// Class name: LightShaderClassclass LightShaderClass
{
private:struct MatrixBufferType{D3DXMATRIX world;D3DXMATRIX view;D3DXMATRIX projection;};
新的lightbuffertype结构将用于保存照明信息。此typedef与像素明暗器中的新typedef相同。请注意,我为大小填充添加了一个额外的浮点,以确保结构是16的倍数。因为没有额外浮点的结构只有28个字节,所以如果我们使用sizeof(lightbuffertype),createBuffer就会失败,因为它需要16的倍数才能成功。struct LightBufferType{D3DXVECTOR4 diffuseColor;D3DXVECTOR3 lightDirection;float padding;  //添加了额外的填充,因此对于CreateBuffer函数要求,结构是16的倍数。};public:LightShaderClass();LightShaderClass(const LightShaderClass&);~LightShaderClass();bool Initialize(ID3D11Device*, HWND);void Shutdown();bool Render(ID3D11DeviceContext*, int, D3DXMATRIX, D3DXMATRIX, D3DXMATRIX, ID3D11ShaderResourceView*, D3DXVECTOR3, D3DXVECTOR4);private:bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);void ShutdownShader();void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);bool SetShaderParameters(ID3D11DeviceContext*, D3DXMATRIX, D3DXMATRIX, D3DXMATRIX, ID3D11ShaderResourceView*, D3DXVECTOR3, D3DXVECTOR4);void RenderShader(ID3D11DeviceContext*, int);private:ID3D11VertexShader* m_vertexShader;ID3D11PixelShader* m_pixelShader;ID3D11InputLayout* m_layout;ID3D11SamplerState* m_sampleState;ID3D11Buffer* m_matrixBuffer;
对于光信息(颜色和方向),有一个新的私有常量缓冲区。此类将使用灯光缓冲区来设置HLSL像素明暗器中的全局灯光变量。ID3D11Buffer* m_lightBuffer;
};#endif

Lightshaderclass.cpp


// Filename: lightshaderclass.cpp#include "lightshaderclass.h"LightShaderClass::LightShaderClass()
{m_vertexShader = 0;m_pixelShader = 0;m_layout = 0;m_sampleState = 0;m_matrixBuffer = 0;
在类构造函数中将light常量缓冲区设置为空。m_lightBuffer = 0;
}LightShaderClass::LightShaderClass(const LightShaderClass& other)
{
}LightShaderClass::~LightShaderClass()
{
}bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
{bool result;
新的light.vs和light.ps hlsl明暗器文件用作输入以初始化灯光明暗器。
//初始化顶点和像素着色器。result = InitializeShader(device, hwnd, L"../Engine/light.vs", L"../Engine/light.ps");if(!result){return false;}return true;
}void LightShaderClass::Shutdown()
{// Shutdown the vertex and pixel shaders as well as the related objects.ShutdownShader();return;
}

渲染函数现在将灯光方向和灯光漫反射颜色作为输入。然后将这些变量发送到setshaderParameters函数中,最后在着色器内部进行设置。

bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, D3DXVECTOR3 lightDirection, D3DXVECTOR4 diffuseColor)
{bool result;// Set the shader parameters that it will use for rendering.result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor);if(!result){return false;}// Now render the prepared buffers with the shader.RenderShader(deviceContext, indexCount);return true;
}bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
{HRESULT result;ID3D10Blob* errorMessage;ID3D10Blob* vertexShaderBuffer;ID3D10Blob* pixelShaderBuffer;
PyGrand布局变量已被更改为具有三个元素而不是两个元素。这样它就可以在布局中容纳一个法向量。D3D11_INPUT_ELEMENT_DESC polygonLayout[3];unsigned int numElements;D3D11_SAMPLER_DESC samplerDesc;D3D11_BUFFER_DESC matrixBufferDesc;
我们还为光常数缓冲区添加了一个新的描述变量。D3D11_BUFFER_DESC lightBufferDesc;// Initialize the pointers this function will use to null.errorMessage = 0;vertexShaderBuffer = 0;pixelShaderBuffer = 0;
加载新的灯光顶点明暗器。// Compile the vertex shader code.result = D3DX11CompileFromFile(vsFilename, NULL, NULL, "LightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &vertexShaderBuffer, &errorMessage, NULL);if(FAILED(result)){// If the shader failed to compile it should have writen something to the error message.if(errorMessage){OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);}// If there was nothing in the error message then it simply could not find the shader file itself.else{MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);}return false;}
加载新的光像素明暗器。// Compile the pixel shader code.result = D3DX11CompileFromFile(psFilename, NULL, NULL, "LightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &pixelShaderBuffer, &errorMessage, NULL);if(FAILED(result)){// If the shader failed to compile it should have writen something to the error message.if(errorMessage){OutputShaderErrorMessage(errorMessage, hwnd, psFilename);}// If there was nothing in the error message then it simply could not find the file itself.else{MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);}return false;}// Create the vertex shader from the buffer.result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);if(FAILED(result)){return false;}// Create the pixel shader from the buffer.result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);if(FAILED(result)){return false;}// Create the vertex input layout description.// This setup needs to match the VertexType stucture in the ModelClass and in the shader.polygonLayout[0].SemanticName = "POSITION";polygonLayout[0].SemanticIndex = 0;polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;polygonLayout[0].InputSlot = 0;polygonLayout[0].AlignedByteOffset = 0;polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;polygonLayout[0].InstanceDataStepRate = 0;polygonLayout[1].SemanticName = "TEXCOORD";polygonLayout[1].SemanticIndex = 0;polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;polygonLayout[1].InputSlot = 0;polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;polygonLayout[1].InstanceDataStepRate = 0;
对明暗器初始化的主要更改之一是在多边形布局中。我们为将用于照明的法向量添加第三个元素。语义名为normal,格式为常规的dxgi_格式_r32g32b32_float,它为法向量的x、y和z处理3个浮点。布局现在将与HLSL顶点明暗器的预期输入相匹配。polygonLayout[2].SemanticName = "NORMAL";polygonLayout[2].SemanticIndex = 0;polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;polygonLayout[2].InputSlot = 0;polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;polygonLayout[2].InstanceDataStepRate = 0;// Get a count of the elements in the layout.numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);// Create the vertex input layout.result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);if(FAILED(result)){return false;}// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.vertexShaderBuffer->Release();vertexShaderBuffer = 0;pixelShaderBuffer->Release();pixelShaderBuffer = 0;// Create a texture sampler state description.samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;samplerDesc.MipLODBias = 0.0f;samplerDesc.MaxAnisotropy = 1;samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;samplerDesc.BorderColor[0] = 0;samplerDesc.BorderColor[1] = 0;samplerDesc.BorderColor[2] = 0;samplerDesc.BorderColor[3] = 0;samplerDesc.MinLOD = 0;samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;// Create the texture sampler state.result = device->CreateSamplerState(&samplerDesc, &m_sampleState);if(FAILED(result)){return false;}// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;matrixBufferDesc.MiscFlags = 0;matrixBufferDesc.StructureByteStride = 0;// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);if(FAILED(result)){return false;}
这里我们设置了光常数缓冲区描述,它将处理漫射光的颜色和光的方向。请注意常量缓冲区的大小,如果它们不是16的倍数,则需要在它们的末尾添加额外的空间,否则CreateBuffer函数将失败。在这种情况下,常量缓冲区是28个字节,填充了4个字节,使其成为32个字节。
//设置像素明暗器中的灯光动态常量缓冲区的描述。
//请注意,如果使用d3d11_bind_constant_buffer或createbuffer失败,bytewidth始终需要是16的倍数。lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;lightBufferDesc.ByteWidth = sizeof(LightBufferType);lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;lightBufferDesc.MiscFlags = 0;lightBufferDesc.StructureByteStride = 0;//创建常量缓冲区指针,以便我们可以从此类中访问顶点着色器常量缓冲区。result = device->CreateBuffer(&lightBufferDesc, NULL, &m_lightBuffer);if(FAILED(result)){return false;}return true;
}void LightShaderClass::ShutdownShader()
{
在shutdownshader函数中释放新的光常数缓冲区。// Release the light constant buffer.if(m_lightBuffer){m_lightBuffer->Release();m_lightBuffer = 0;}// Release the matrix constant buffer.if(m_matrixBuffer){m_matrixBuffer->Release();m_matrixBuffer = 0;}// Release the sampler state.if(m_sampleState){m_sampleState->Release();m_sampleState = 0;}// Release the layout.if(m_layout){m_layout->Release();m_layout = 0;}// Release the pixel shader.if(m_pixelShader){m_pixelShader->Release();m_pixelShader = 0;}// Release the vertex shader.if(m_vertexShader){m_vertexShader->Release();m_vertexShader = 0;}return;
}void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
{char* compileErrors;unsigned long bufferSize, i;ofstream fout;// Get a pointer to the error message text buffer.compileErrors = (char*)(errorMessage->GetBufferPointer());// Get the length of the message.bufferSize = errorMessage->GetBufferSize();// Open a file to write the error message to.fout.open("shader-error.txt");// Write out the error message.for(i=0; i<bufferSize; i++){fout << compileErrors[i];}// Close the file.fout.close();// Release the error message.errorMessage->Release();errorMessage = 0;// Pop a message up on the screen to notify the user to check the text file for compile errors.MessageBox(hwnd, L"Error compiling shader.  Check shader-error.txt for message.", shaderFilename, MB_OK);return;
}

setshaderParameters函数现在接受lightdirection和diff散颜色作为输入。

bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, D3DXVECTOR3 lightDirection, D3DXVECTOR4 diffuseColor)
{HRESULT result;D3D11_MAPPED_SUBRESOURCE mappedResource;unsigned int bufferNumber;MatrixBufferType* dataPtr;LightBufferType* dataPtr2;// Transpose the matrices to prepare them for the shader.D3DXMatrixTranspose(&worldMatrix, &worldMatrix);D3DXMatrixTranspose(&viewMatrix, &viewMatrix);D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix);// Lock the constant buffer so it can be written to.result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);if(FAILED(result)){return false;}// Get a pointer to the data in the constant buffer.dataPtr = (MatrixBufferType*)mappedResource.pData;// Copy the matrices into the constant buffer.dataPtr->world = worldMatrix;dataPtr->view = viewMatrix;dataPtr->projection = projectionMatrix;// Unlock the constant buffer.deviceContext->Unmap(m_matrixBuffer, 0);// Set the position of the constant buffer in the vertex shader.bufferNumber = 0;// Now set the constant buffer in the vertex shader with the updated values.deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);//在像素明暗器中设置明暗器纹理资源。
deviceContext->pssetshaderresources(0、1和纹理);
光常数缓冲区的设置方式与矩阵常数缓冲区相同。我们首先锁定缓冲区并获取指向它的指针。之后,我们使用该指针设置漫射颜色和灯光方向。一旦设置了数据,我们就会解锁缓冲区,然后在像素着色中设置它。注意,我们使用pssetconstantbuffers函数而不是vssetconstantbuffers,因为这是我们正在设置的像素着色缓冲区。
//锁定光常数缓冲区,以便写入。result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);if(FAILED(result)){return false;}// Get a pointer to the data in the constant buffer.dataPtr2 = (LightBufferType*)mappedResource.pData;// Copy the lighting variables into the constant buffer.dataPtr2->diffuseColor = diffuseColor;dataPtr2->lightDirection = lightDirection;dataPtr2->padding = 0.0f;// Unlock the constant buffer.deviceContext->Unmap(m_lightBuffer, 0);// Set the position of the light constant buffer in the pixel shader.bufferNumber = 0;// Finally set the light constant buffer in the pixel shader with the updated values.deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);return true;
}void LightShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
{// Set the vertex input layout.deviceContext->IASetInputLayout(m_layout);// Set the vertex and pixel shaders that will be used to render this triangle.deviceContext->VSSetShader(m_vertexShader, NULL, 0);deviceContext->PSSetShader(m_pixelShader, NULL, 0);// Set the sampler state in the pixel shader.deviceContext->PSSetSamplers(0, 1, &m_sampleState);// Render the triangle.deviceContext->DrawIndexed(indexCount, 0, 0);return;
}

Modelclass.h
modelClass已进行了轻微修改,以处理照明组件。


// Filename: modelclass.h#ifndef _MODELCLASS_H_
#define _MODELCLASS_H_//
// INCLUDES //
//
#include <d3d11.h>
#include <d3dx10math.h>///
// MY CLASS INCLUDES //
///
#include "textureclass.h"// Class name: ModelClassclass ModelClass
{
private:
VertexType结构现在有一个标准向量来适应照明。struct VertexType{D3DXVECTOR3 position;D3DXVECTOR2 texture;D3DXVECTOR3 normal;};public:ModelClass();ModelClass(const ModelClass&);~ModelClass();bool Initialize(ID3D11Device*, WCHAR*);void Shutdown();void Render(ID3D11DeviceContext*);int GetIndexCount();ID3D11ShaderResourceView* GetTexture();private:bool InitializeBuffers(ID3D11Device*);void ShutdownBuffers();void RenderBuffers(ID3D11DeviceContext*);bool LoadTexture(ID3D11Device*, WCHAR*);void ReleaseTexture();private:ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;int m_vertexCount, m_indexCount;TextureClass* m_Texture;
};#endif

Modelclass.cpp


// Filename: modelclass.cpp#include "modelclass.h"ModelClass::ModelClass()
{m_vertexBuffer = 0;m_indexBuffer = 0;m_Texture = 0;
}ModelClass::ModelClass(const ModelClass& other)
{
}ModelClass::~ModelClass()
{
}bool ModelClass::Initialize(ID3D11Device* device, WCHAR* textureFilename)
{bool result;// Initialize the vertex and index buffers.result = InitializeBuffers(device);if(!result){return false;}// Load the texture for this model.result = LoadTexture(device, textureFilename);if(!result){return false;}return true;
}void ModelClass::Shutdown()
{// Release the model texture.ReleaseTexture();// Shutdown the vertex and index buffers.ShutdownBuffers();return;
}void ModelClass::Render(ID3D11DeviceContext* deviceContext)
{
//将顶点缓冲区和索引缓冲区放置在图形管道上,以准备绘制。RenderBuffers(deviceContext);return;
}int ModelClass::GetIndexCount()
{return m_indexCount;
}ID3D11ShaderResourceView* ModelClass::GetTexture()
{return m_Texture->GetTexture();
}bool ModelClass::InitializeBuffers(ID3D11Device* device)
{VertexType* vertices;unsigned long* indices;D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;D3D11_SUBRESOURCE_DATA vertexData, indexData;HRESULT result;// Set the number of vertices in the vertex array.m_vertexCount = 3;// Set the number of indices in the index array.m_indexCount = 3;// Create the vertex array.vertices = new VertexType[m_vertexCount];if(!vertices){return false;}// Create the index array.indices = new unsigned long[m_indexCount];if(!indices){return false;}
对initializeBuffers函数的唯一更改是在顶点设置中。现在每个顶点都有与之关联的法线用于照明计算。法向是一条与多边形面垂直的线,这样可以计算出面指向的确切方向。为了简单起见,我通过将每个z分量设置为-1.0f来设置z轴上每个顶点的法向,使法向指向查看器。
//用数据加载顶点数组。vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);  // Bottom left.vertices[0].texture = D3DXVECTOR2(0.0f, 1.0f);vertices[0].normal = D3DXVECTOR3(0.0f, 0.0f, -1.0f);vertices[1].position = D3DXVECTOR3(0.0f, 1.0f, 0.0f);  // Top middle.vertices[1].texture = D3DXVECTOR2(0.5f, 0.0f);vertices[1].normal = D3DXVECTOR3(0.0f, 0.0f, -1.0f);vertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);  // Bottom right.vertices[2].texture = D3DXVECTOR2(1.0f, 1.0f);vertices[2].normal = D3DXVECTOR3(0.0f, 0.0f, -1.0f);// Load the index array with data.indices[0] = 0;  // Bottom left.indices[1] = 1;  // Top middle.indices[2] = 2;  // Bottom right.// Set up the description of the static vertex buffer.vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;vertexBufferDesc.CPUAccessFlags = 0;vertexBufferDesc.MiscFlags = 0;vertexBufferDesc.StructureByteStride = 0;// Give the subresource structure a pointer to the vertex data.vertexData.pSysMem = vertices;vertexData.SysMemPitch = 0;vertexData.SysMemSlicePitch = 0;// Now create the vertex buffer.result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);if(FAILED(result)){return false;}// Set up the description of the static index buffer.indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;indexBufferDesc.CPUAccessFlags = 0;indexBufferDesc.MiscFlags = 0;indexBufferDesc.StructureByteStride = 0;// Give the subresource structure a pointer to the index data.indexData.pSysMem = indices;indexData.SysMemPitch = 0;indexData.SysMemSlicePitch = 0;// Create the index buffer.result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);if(FAILED(result)){return false;}// Release the arrays now that the vertex and index buffers have been created and loaded.delete [] vertices;vertices = 0;delete [] indices;indices = 0;return true;
}void ModelClass::ShutdownBuffers()
{// Release the index buffer.if(m_indexBuffer){m_indexBuffer->Release();m_indexBuffer = 0;}// Release the vertex buffer.if(m_vertexBuffer){m_vertexBuffer->Release();m_vertexBuffer = 0;}return;
}void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
{unsigned int stride;unsigned int offset;// Set vertex buffer stride and offset.stride = sizeof(VertexType); offset = 0;// Set the vertex buffer to active in the input assembler so it can be rendered.deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);// Set the index buffer to active in the input assembler so it can be rendered.deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);return;
}bool ModelClass::LoadTexture(ID3D11Device* device, WCHAR* filename)
{bool result;// Create the texture object.m_Texture = new TextureClass;if(!m_Texture){return false;}// Initialize the texture object.result = m_Texture->Initialize(device, filename);if(!result){return false;}return true;
}void ModelClass::ReleaseTexture()
{// Release the texture object.if(m_Texture){m_Texture->Shutdown();delete m_Texture;m_Texture = 0;}return;
}

Lightclass.h
现在我们来看看新的光课程,它非常简单。其目的只是为了保持灯光的方向和颜色。


// Filename: lightclass.h#ifndef _LIGHTCLASS_H_
#define _LIGHTCLASS_H_//
// INCLUDES //
//
#include <d3dx10math.h>// Class name: LightClassclass LightClass
{
public:LightClass();LightClass(const LightClass&);~LightClass();void SetDiffuseColor(float, float, float, float);void SetDirection(float, float, float);D3DXVECTOR4 GetDiffuseColor();D3DXVECTOR3 GetDirection();private:D3DXVECTOR4 m_diffuseColor;D3DXVECTOR3 m_direction;
};#endif

Lightclass.cpp


// Filename: lightclass.cpp#include "lightclass.h"LightClass::LightClass()
{
}LightClass::LightClass(const LightClass& other)
{
}LightClass::~LightClass()
{
}void LightClass::SetDiffuseColor(float red, float green, float blue, float alpha)
{m_diffuseColor = D3DXVECTOR4(red, green, blue, alpha);return;
}void LightClass::SetDirection(float x, float y, float z)
{m_direction = D3DXVECTOR3(x, y, z);return;
}D3DXVECTOR4 LightClass::GetDiffuseColor()
{return m_diffuseColor;
}D3DXVECTOR3 LightClass::GetDirection()
{return m_direction;
}

Graphicsclass.h


// Filename: graphicsclass.h#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_///
// MY CLASS INCLUDES //
///
#include "d3dclass.h"
#include "cameraclass.h"
#include "modelclass.h"
graphicsClass现在有两个新的包含用于lightshaderClass和lightClass。
#include "lightshaderclass.h"
#include "lightclass.h"/
// GLOBALS //
/
const bool FULL_SCREEN = true;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;// Class name: GraphicsClassclass GraphicsClass
{
public:GraphicsClass();GraphicsClass(const GraphicsClass&);~GraphicsClass();bool Initialize(int, int, HWND);void Shutdown();bool Frame();private:
Render now takes a float value as input.bool Render(float);private:D3DClass* m_D3D;CameraClass* m_Camera;ModelClass* m_Model;
灯光明暗器和灯光对象有两个新的私有变量。LightShaderClass* m_LightShader;LightClass* m_Light;
};#endif

Graphicsclass.cpp


// Filename: graphicsclass.cpp#include "graphicsclass.h"GraphicsClass::GraphicsClass()
{m_D3D = 0;m_Camera = 0;m_Model = 0;
在类构造函数中,灯光明暗器和灯光对象设置为空。m_LightShader = 0;m_Light = 0;
}GraphicsClass::GraphicsClass(const GraphicsClass& other)
{
}GraphicsClass::~GraphicsClass()
{
}bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{bool result;// Create the Direct3D object.m_D3D = new D3DClass;if(!m_D3D){return false;}// Initialize the Direct3D object.result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);if(!result){MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);return false;}// Create the camera object.m_Camera = new CameraClass;if(!m_Camera){return false;}// Set the initial position of the camera.m_Camera->SetPosition(0.0f, 0.0f, -10.0f);// Create the model object.m_Model = new ModelClass;if(!m_Model){return false;}// Initialize the model object.result = m_Model->Initialize(m_D3D->GetDevice(), L"../Engine/data/seafloor.dds");if(!result){MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);return false;}
新的灯光明暗器对象在此处创建和初始化。// Create the light shader object.m_LightShader = new LightShaderClass;if(!m_LightShader){return false;}// Initialize the light shader object.result = m_LightShader->Initialize(m_D3D->GetDevice(), hwnd);if(!result){MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);return false;}
The new light object is created here.// Create the light object.m_Light = new LightClass;if(!m_Light){return false;}
灯光颜色设置为紫色,灯光方向设置为指向正Z轴。// Initialize the light object.m_Light->SetDiffuseColor(1.0f, 0.0f, 1.0f, 1.0f);m_Light->SetDirection(0.0f, 0.0f, 1.0f);return true;
}

void GraphicsClass::Shutdown()
{
shutdown函数释放新的灯光和灯光明暗器对象。// Release the light object.if(m_Light){delete m_Light;m_Light = 0;}// Release the light shader object.if(m_LightShader){m_LightShader->Shutdown();delete m_LightShader;m_LightShader = 0;}// Release the model object.if(m_Model){m_Model->Shutdown();delete m_Model;m_Model = 0;}// Release the camera object.if(m_Camera){delete m_Camera;m_Camera = 0;}// Release the D3D object.if(m_D3D){m_D3D->Shutdown();delete m_D3D;m_D3D = 0;}return;
}bool GraphicsClass::Frame()
{bool result;
我们添加了一个新的静态变量来保存一个更新的旋转值,每个帧都将被传递到渲染函数中。static float rotation = 0.0f;// Update the rotation variable each frame.rotation += (float)D3DX_PI * 0.01f;if(rotation > 360.0f){rotation -= 360.0f;}// Render the graphics scene.result = Render(rotation);if(!result){return false;}return true;
}bool GraphicsClass::Render(float rotation)
{D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;bool result;// Clear the buffers to begin the scene.m_D3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);// Generate the view matrix based on the camera's position.m_Camera->Render();// Get the world, view, and projection matrices from the camera and d3d objects.m_Camera->GetViewMatrix(viewMatrix);m_D3D->GetWorldMatrix(worldMatrix);m_D3D->GetProjectionMatrix(projectionMatrix);
在这里,我们用旋转值旋转世界矩阵,这样当我们使用这个更新的世界矩阵渲染三角形时,它将通过旋转量旋转三角形。
//按旋转值旋转世界矩阵,使三角形旋转。D3DXMatrixRotationY(&worldMatrix, rotation);// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.m_Model->Render(m_D3D->GetDeviceContext());
这里调用灯光明暗器来渲染三角形。新的灯光对象用于将漫射灯光颜色和灯光方向发送到渲染函数中,以便明暗器可以访问这些值。
//使用灯光明暗器渲染模型。result = m_LightShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(), m_Light->GetDirection(), m_Light->GetDiffuseColor());if(!result){return false;}// Present the rendered scene to the screen.m_D3D->EndScene();return true;
}

总结
通过对代码的一些更改,我们能够实现一些基本的方向性照明。确保您了解法向量的工作原理以及它们为什么对计算多边形面上的照明很重要。请注意,旋转三角形的背面不会亮起,因为我们在D3D类中启用了背面剔除。

做练习
1。重新编译该项目,并确保您得到一个旋转纹理三角形,正由一个紫色的灯光照亮。按Escape退出。
2。在像素明暗器中注释掉“color=color*texturecolor;”,这样明暗器就不再使用了,您应该看到没有纹理的照明效果。
三。在graphicsClass的m_light->setDiff散颜色代码行中将灯光颜色更改为绿色。
4。更改灯光的方向,使其沿正X轴和负X轴向下。您可能还需要更改旋转速度。

(如果要看其他课的中文翻译版,请到我博客目录查找,我会抽时间把后续的课目都翻译出来,这取决于我有空闲时间。)
时间仓促,只是粗略翻译,可能有多处失误,请谅解。朋友如有发现哪里有错误,欢迎指正,联·系w新licheng16886

DirectX 11 Tutorial 6 中文翻译版教程: 漫射照明相关推荐

  1. DirectX 11 Tutorial 4 中文翻译版教程: 缓存区、着色器和HLSL

    原英文版地址:http://www.rastertek.com/dx11tut04.html 本教程将介绍如何在Directx11中编写顶点和像素遮影器.它还将介绍如何在Directx11中使用顶点和 ...

  2. DirectX 11 Tutorial 3 中文翻译版教程: 初始化DirectX 11

    原英文版地址:http://www.rastertek.com/dx11tut03.html 本教程将首次介绍如何使用Directx11.我们将讨论如何初始化和关闭Direct3D以及如何渲染到窗口. ...

  3. DirectX 11 Tutorial 2 中文翻译版教程:创建框架和窗口

    原英文版地址:http://www.rastertek.com/dx11tut02.html 在开始使用Directx11编码之前,我建议构建一个简单的代码框架.这个框架将处理基本的Windows功能 ...

  4. 7系列FPGA数据手册:概述------中文翻译版

    7系列FPGA数据手册:概述------中文翻译版 总体介绍 7系列FPGA功能摘要 Spartan-7系列FPGA功能摘要 Artix-7系列FPGA功能摘要 Kintex-7系列FPGA功能摘要 ...

  5. GeoPandas官方快速入门文档(中文翻译版)

    GeoPandas官方快速入门文档(中文翻译版) 原文链接>>Introduction to GeoPandas 点这里可以一键跑通>>GeoPandas 简介 GeoPand ...

  6. ElasticSearch最新权威指南中文翻译版!

    ElasticSearch最新权威指南中文翻译版! GitHub源码地址:  https://github.com/wjw465150/Elasticsearch 阅读的GitPage地址:  htt ...

  7. 苹果商店应用审核指南(2014年中文翻译版)

    前言 非常高兴您能付出宝贵的时间和精力来开发iOS应用.成千上万的开发者的经历表明,无论从专业技术还是经济收入上,开发iOS应用都是个有意义的事情,我们将帮助你更快成功,成为其中的一员.这篇App S ...

  8. 【Learncpp中文翻译版】【1.9、1.10、1.11】

    文章目录 1.9 - Introduction to literals and operators 文字 运算符 链接运算符 返回值和副作用 1.10 - Introduction to expres ...

  9. pinia中文文档 指导文档中文翻译版 pinia指导中文翻译

    Pinia 指导文档 中 文 翻 译 版 翻译者:jcLee95 Pinia 指导手册中文翻译地址(本文): https://blog.csdn.net/qq_28550263/article/det ...

最新文章

  1. Linux系统JDK安装和配置
  2. vecm模型怎么写系数_经典传染病的SIR模型(基于MATLAB)
  3. iphone手机备忘录迁移_如何在iPhone和iPad上使用语音备忘录
  4. Ubuntu 装机软件
  5. Android学习笔记(二)基础知识(1)
  6. Magento教程 4:主机环境准备
  7. js中常用的日期处理函数
  8. 滚动电梯卡延期复制教学
  9. Linux安装vim命令
  10. 传谷歌金山秘密合作研发Office软件
  11. 项目经理之初为项目经理
  12. Revit API 2018调试闪退
  13. FFmpeg数据结构AVFrame
  14. 深圳市“数据分析”岗位招聘分析—基于拉勾网
  15. MongoDB 更新数组中的元素
  16. Python学习之-分支语句
  17. CSS3 浏览器内核识别码
  18. Java完成迪迦奥特曼打小怪兽
  19. Jsp格式化显示Date类型数据
  20. NOIP201002接水问题 题解

热门文章

  1. html设置打开页面后自动关闭,Win7系统打开IE浏览器后页面自动关闭的四种解决方法...
  2. tkinter库绘图实例13:手绘画板效果
  3. Spring 的表头
  4. 建立自己的3389肉鸡(基础级)
  5. iPad2、MSAA和alpha混合几何 Bug
  6. minicom指令_minicom 使用
  7. stm32 例程中lcd颜色初始化显示
  8. 函数返回vector类型
  9. inputStream类型
  10. sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)