Directx11 Study Notes 8-simplest illumination

Source: Internet
Author: User

Labels: C ++ DirectX directx11 Reading Notes

A ground is added to the previous column. This ground is generated by the illumination effect.

Figure:


Note:

Lightshader needs to be rewritten as the light Renderer.

// Define the input layoutD3D11_INPUT_ELEMENT_DESC layout[] ={{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },{ "NORMAL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },};
You will find that the second line is color instead of normal. Because the color of the light is formed by the diffuse reflection of the light transmitted from the external environment.

All the computing of these lights is written in the lightshader. FX file.


//--------------------------------------------------------------------------------------// Constant Buffer Variables//--------------------------------------------------------------------------------------cbuffer ConstantBuffer {matrix World;matrix View;matrix Projection;};cbuffer LightBuffer{    float3 cameraPosition;    float padding;};//--------------------------------------------------------------------------------------struct VS_INPUT{float4 Pos : POSITION;    float3 Normal : NORMAL;};struct VS_OUTPUT{    float4 Pos : SV_POSITION;    float4 Color : COLOR;};//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------VS_OUTPUT VS( VS_INPUT input ){    VS_OUTPUT output = (VS_OUTPUT)0;    float4 worldPosition;    // 改变顶点为四个分量其次坐标.    input.Pos.w = 1.0f;    output.Pos = mul( input.Pos, World );    output.Pos = mul( output.Pos, View );    output.Pos = mul( output.Pos, Projection );     // 世界坐标系中的顶点法向.    float3 N = mul(input.Normal, (float3x3)World);N = normalize(N);//世界坐标系顶点位置float3 P = output.Pos.xyz;//自发射颜色float3 emissive = float3(0.0, 0.0, 0.0);    //计算环境光    float3 ambient =  float3(0.3, 0.3, 0.3);//计算漫反射光    float3 L = normalize(float3(-1.0, -1.0, 1.0));    float diffuseLight = max(dot(N, L), 0);    float3 diffuse =  diffuseLight;     //计算高光    float3 V = normalize(cameraPosition - P);    float3 H = normalize(L + V);    float specularLight = pow(max(dot(N, H), 0), 5.0);     if (diffuseLight <= 0)      specularLight = 0;    float3 specular =  specularLight;output.Color.xyz = emissive + ambient + diffuse + specular;// float3 tt = float3(1.0, 0.0, 0.0);//  output.color.xyz = float3(1.0, 0.0, 0.0);output.Color.w = 1.0f;    return output;}//--------------------------------------------------------------------------------------// Pixel Shader//--------------------------------------------------------------------------------------float4 PS( VS_OUTPUT input ) : SV_Target{    return input.Color;}

It mainly involves vertex input for complex illumination calculation, which is included in longbooks. An extra buffer.

cbuffer LightBuffer{    float3 cameraPosition;    float padding;};
Currently, only the color of the light is stored. The color must be a multiple of 4 to align the float address.

//create the light bufferZeroMemory( &bd, sizeof(bd) );bd.Usage = D3D11_USAGE_DEFAULT;bd.ByteWidth = sizeof(LightBuffer);bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;bd.CPUAccessFlags = 0;hr = device->CreateBuffer( &bd, NULL, &m_lightmaterialBuffer );if( FAILED( hr ) )return hr;

Create a buffer in the light rendering File

//// Update variables//ConstantBuffer cb;cb.mWorld = XMMatrixTranspose( worldMatrix);cb.mView = XMMatrixTranspose( viewMatrix );cb.mProjection = XMMatrixTranspose( projectionMatrix );deviceContext->UpdateSubresource( m_matrixBuffer, 0, NULL, &cb, 0, 0 );deviceContext->VSSetConstantBuffers( 0, 1, &m_matrixBuffer );LightBuffer lb;lb.cameraPosition=cameraPos;deviceContext->UpdateSubresource( m_lightmaterialBuffer, 0, NULL, &lb, 0, 0 );deviceContext->VSSetConstantBuffers( 1, 1, &m_matrixBuffer );

There is a buffer update more than before.

With the illumination effect file, the illumination Renderer has a different bearer.

Here is a quadrilateral.

Planemodel. h

#pragma  once#include "XComm.h"class PlaneModel{protected:struct SimpleVertexN  {  XMFLOAT3 Pos;  XMFLOAT3 Normal;  };public://顶点缓冲和顶点索引缓冲ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;int m_vertexCount, m_indexCount;public:PlaneModel():m_vertexCount(0),m_indexCount(0){};bool init(ID3D11Device*);void close();void render(ID3D11DeviceContext*);};

Basically, the method of the cube is the same as that of the previous one, except that the color is changed to normal.

Planemodel. cpp

#include "PlaneModel.h"bool PlaneModel::init(ID3D11Device* device){SimpleVertexN* vertices;unsigned long* indices;D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;D3D11_SUBRESOURCE_DATA vertexData, indexData;HRESULT result;//首先,我们创建2个临时缓冲存放顶点和索引数据,以便后面使用。. // 设置顶点缓冲大小为4,一个平面.m_vertexCount = 4;// 设置索引缓冲大小.,两个三角形m_indexCount = 6;// 创建顶点临时缓冲.vertices = new SimpleVertexN[m_vertexCount];if(!vertices){return false;}// 创建索引缓冲.indices = new unsigned long[m_indexCount];if(!indices){return false;}//创建顺时针方向的三角形,左手规则// 设置顶点数据.vertices[0].Pos = XMFLOAT3(-50.0f, -3.0f, -50.0f);  vertices[0].Normal = XMFLOAT3(0.0f, 1.0f, 0.0f);vertices[1].Pos = XMFLOAT3(-50.0f,-3.0f, 50.0f);  vertices[1].Normal = XMFLOAT3(0.0f, 1.0f, 0.0f);vertices[2].Pos = XMFLOAT3(50.0f, -3.0f, 50.0f);  vertices[2].Normal=  XMFLOAT3(0.0f, 1.0f, 0.0f);vertices[3].Pos = XMFLOAT3(50.0f, -3.0f, -50.0f);  vertices[3].Normal =  XMFLOAT3(0.0f, 1.0f, 0.0f);// 设置索引缓冲数据.indices[0] = 0;  // 前面indices[1] = 1; indices[2] = 2;  indices[3] = 0; indices[4] = 2; indices[5] = 3;  // 设置顶点缓冲描述vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;vertexBufferDesc.ByteWidth = sizeof(SimpleVertexN) * m_vertexCount;vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;vertexBufferDesc.CPUAccessFlags = 0;vertexBufferDesc.MiscFlags = 0;vertexBufferDesc.StructureByteStride = 0;// 指向保存顶点数据的临时缓冲.vertexData.pSysMem = vertices;vertexData.SysMemPitch = 0;vertexData.SysMemSlicePitch = 0;// 创建顶点缓冲.result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);if(FAILED(result)){HR(result);return false;}// 设置索引缓冲描述.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;// 指向存临时索引缓冲.indexData.pSysMem = indices;indexData.SysMemPitch = 0;indexData.SysMemSlicePitch = 0;// 创建索引缓冲.result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);if(FAILED(result)){HR(result);return false;}// 释放临时缓冲.delete [] vertices;vertices = 0;delete [] indices;indices = 0;}void PlaneModel::close(){// 释放顶点缓冲.if(m_indexBuffer){m_indexBuffer->Release();m_indexBuffer = 0;}// 释放索引缓冲if(m_vertexBuffer){m_vertexBuffer->Release();m_vertexBuffer = 0;}}void PlaneModel::render(ID3D11DeviceContext* deviceContext){unsigned int stride;unsigned int offset;// 设置顶点缓冲跨度和偏移.stride = sizeof(SimpleVertexN); offset = 0;//在input assemberl阶段绑定顶点缓冲,以便能够被渲染deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);//在input assemberl阶段绑定索引缓冲,以便能够被渲染deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);// 设置体元语义,渲染三角形列表.//注意这里和XModel不一样deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);}


Directx11 Study Notes 8-simplest illumination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.