Execute.cpp
#include "stdafx.h"
#include "Execute.h"
#include "graphics.h"
Execute::Execute()
{
graphics = new Graphics;
graphics->Initialize();
graphics->CreateBackBuffer(static_cast<uint>(Settings::Get().GetWidth()),
static_cast<uint>(Settings::Get().GetHeight()));
{
vertics = new vertexTexture[4];
vertics[0].position = D3DXVECTOR3(-0.5f, -0.5f, 0.0f);
vertics[0].uv = D3DXVECTOR2(0.0f, 4.0f);
vertics[1].position = D3DXVECTOR3(-0.5f, +0.5f, 0.0f);
vertics[1].uv = D3DXVECTOR2(0.0f, 0.0f);
vertics[2].position = D3DXVECTOR3(+0.5f, -0.5f, 0.0f);
vertics[2].uv = D3DXVECTOR2(4.0f, 4.0f);
vertics[3].position = D3DXVECTOR3(+0.5f, +0.5f, 0.0f);
vertics[3].uv = D3DXVECTOR2(4.0f, 0.0f);
}
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc.ByteWidth = sizeof(vertexTexture) * 4;
D3D11_SUBRESOURCE_DATA sub_data;
ZeroMemory(&sub_data, sizeof(D3D11_SUBRESOURCE_DATA));
sub_data.pSysMem = vertics;
auto hr = graphics->GetDevice()->CreateBuffer
(
&desc,
&sub_data,
&vertex_buffer
);
assert(SUCCEEDED(hr));
}
{
indices = new uint[6]{0, 1, 2, 2, 1, 3};
}
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
desc.ByteWidth = sizeof(uint) * 6;
D3D11_SUBRESOURCE_DATA sub_data;
ZeroMemory(&sub_data, sizeof(D3D11_SUBRESOURCE_DATA));
sub_data.pSysMem = indices;
auto hr = graphics->GetDevice()->CreateBuffer(&desc, &sub_data, &index_buffer);
assert(SUCCEEDED(hr));
}
{
auto hr = D3DX11CompileFromFileA
(
"Texture.hlsl",
nullptr,
nullptr,
"VS",
"vs_5_0",
0,
0,
nullptr,
&vs_blob,
nullptr,
nullptr
);
assert(SUCCEEDED(hr));
hr = graphics->GetDevice()->CreateVertexShader
(
vs_blob->GetBufferPointer(),
vs_blob->GetBufferSize(),
nullptr,
&vertex_shader
);
assert(SUCCEEDED(hr));
}
{
D3D11_INPUT_ELEMENT_DESC layout_desc[]
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
auto hr = graphics->GetDevice()->CreateInputLayout
(
layout_desc,
2,
vs_blob->GetBufferPointer(),
vs_blob->GetBufferSize(),
&input_layout
);
assert(SUCCEEDED(hr));
}
{
auto hr = D3DX11CompileFromFileA
(
"Texture.hlsl",
nullptr,
nullptr,
"PS",
"ps_5_0",
0,
0,
nullptr,
&ps_blob,
nullptr,
nullptr
);
assert(SUCCEEDED(hr));
hr = graphics->GetDevice()->CreatePixelShader
(
ps_blob->GetBufferPointer(),
ps_blob->GetBufferSize(),
nullptr,
&pixel_shader
);
assert(SUCCEEDED(hr));
}
{
D3DXMatrixIdentity(&world);
D3DXMatrixIdentity(&view);
D3DXMatrixIdentity(&projection);
D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, 0, 1), &D3DXVECTOR3(0, 1, 0));
D3DXMatrixOrthoLH
(
&projection,
Settings::Get().GetWidth(),
Settings::Get().GetHeight(),
0,
1
);
D3DXMATRIX S;
D3DXMatrixScaling(&S, 500, 500, 1);
D3DXMATRIX T;
D3DXMatrixTranslation(&T, 0, 0, 0);
D3DXMATRIX R;
D3DXMatrixRotationZ(&R, static_cast<float>(D3DXToRadian(45)));
world = S /** R*/ * T;
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
desc.ByteWidth = sizeof(TRANSFORM_DATA);
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
auto hr = graphics->GetDevice()->CreateBuffer
(
&desc,
nullptr,
&gpu_buffer
);
assert(SUCCEEDED(hr));
}
{
D3D11_RASTERIZER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_RASTERIZER_DESC));
desc.FillMode = D3D11_FILL_SOLID;
desc.CullMode = D3D11_CULL_BACK;
desc.FrontCounterClockwise = false;
auto hr = graphics->GetDevice()->CreateRasterizerState(&desc, &rasterizer_state);
assert(SUCCEEDED(hr));
}
{
auto hr = D3DX11CreateShaderResourceViewFromFileA
(
graphics->GetDevice(), "Nobita.Png", nullptr, nullptr, &shader_resource[0], nullptr
);
assert(SUCCEEDED(hr));
hr = D3DX11CreateShaderResourceViewFromFileA
(
graphics->GetDevice(), "Dora.Png", nullptr, nullptr, &shader_resource[1], nullptr
);
assert(SUCCEEDED(hr));
hr = D3DX11CreateShaderResourceViewFromFileA
(
graphics->GetDevice(), "3.Png", nullptr, nullptr, &shader_resource[2], nullptr
);
assert(SUCCEEDED(hr));
hr = D3DX11CreateShaderResourceViewFromFileA
(
graphics->GetDevice(), "4.Png", nullptr, nullptr, &shader_resource[3], nullptr
);
assert(SUCCEEDED(hr));
}
{
D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_SAMPLER_DESC));
desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
desc.BorderColor[0] = 1;
desc.BorderColor[1] = 0;
desc.BorderColor[2] = 0;
desc.BorderColor[3] = 1;
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
desc.MaxAnisotropy = 16;
desc.MaxLOD = std::numeric_limits<float>::max();
desc.MinLOD = std::numeric_limits<float>::min();
desc.MipLODBias = 0.0f;
auto hr = graphics->GetDevice()->CreateSamplerState(&desc, &sampler_state);
assert(SUCCEEDED(hr));
}
}
}
Execute::~Execute()
{
SAFE_RELEASE(sampler_state);
SAFE_RELEASE(shader_resource[3]);
SAFE_RELEASE(shader_resource[2]);
SAFE_RELEASE(shader_resource[1]);
SAFE_RELEASE(shader_resource[0]);
SAFE_RELEASE(rasterizer_state);
SAFE_RELEASE(gpu_buffer);
SAFE_RELEASE(pixel_shader);
SAFE_RELEASE(ps_blob);
SAFE_RELEASE(input_layout);
SAFE_RELEASE(vertex_shader);
SAFE_RELEASE(vs_blob);
SAFE_RELEASE(index_buffer);
SAFE_DELETE_ARRAY(indices);
SAFE_RELEASE(vertex_buffer);
SAFE_DELETE_ARRAY(vertics);
SAFE_DELETE(graphics);
}
void Execute::Update()
{
D3DXMatrixTranspose(&cpu_buffer.world, &world);
D3DXMatrixTranspose(&cpu_buffer.view, &view);
D3DXMatrixTranspose(&cpu_buffer.projection, &projection);
D3D11_MAPPED_SUBRESOURCE mapped_subresource;
graphics->GetDeviceContext()->Map
(
gpu_buffer,
0,
D3D11_MAP_WRITE_DISCARD,
0,
&mapped_subresource
);
memcpy(mapped_subresource.pData, &cpu_buffer, sizeof(TRANSFORM_DATA));
graphics->GetDeviceContext()->Unmap
(
gpu_buffer,
0
);
}
void Execute::Render()
{
uint stride = sizeof(vertexTexture);
uint offset = 0;
graphics->Begin();
{
graphics->GetDeviceContext()->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset);
graphics->GetDeviceContext()->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R32_UINT, 0);
graphics->GetDeviceContext()->IASetInputLayout(input_layout);
graphics->GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
graphics->GetDeviceContext()->VSSetShader(vertex_shader, nullptr, 0);
graphics->GetDeviceContext()->VSSetConstantBuffers(0, 1, &gpu_buffer);
graphics->GetDeviceContext()->RSSetState(rasterizer_state);
graphics->GetDeviceContext()->PSSetShader(pixel_shader, nullptr, 0);
graphics->GetDeviceContext()->PSSetShaderResources(0, 4, shader_resource);
graphics->GetDeviceContext()->PSSetSamplers(0, 1, &sampler_state);
graphics->GetDeviceContext()->DrawIndexed(6, 0, 0);
}
graphics->End();
}
Texture.hlsl
struct VertexInput
{
float4 position : POSITION0;
float2 uv : TEXCOORD0;
};
struct PixelInput
{
float4 position : SV_POSITION0;
float2 uv : TEXCOORD0;
};
cbuffer TransformBuffer : register(b0)
{
matrix world;
matrix view;
matrix proj;
};
PixelInput VS(VertexInput input)
{
PixelInput output;
output.position = mul(input.position, world);
output.position = mul(output.position, view);
output.position = mul(output.position, proj);
output.uv = input.uv;
return output;
};
Texture2D source_texture1 : register(t0);
Texture2D source_texture2 : register(t1);
Texture2D source_texture3 : register(t2);
Texture2D source_texture4 : register(t3);
SamplerState samp : register(s0);
float4 PS(PixelInput input) : SV_Target
{
float4 color = 0.0f;
if(input.uv.x < 1.0f)
color = source_texture1.Sample(samp, input.uv);
else if (input.uv.x < 2.0f)
color = source_texture2.Sample(samp, float2(input.uv.x - 1.0f, input.uv.y));
else if (input.uv.x < 3.0f)
color = source_texture3.Sample(samp, float2(input.uv.x - 2.0f, input.uv.y));
else
color = source_texture4.Sample(samp, float2(input.uv.x - 3.0f, input.uv.y));
clip(color.a - 0.9f);
return color;
};
출력 결과
보다시피 Sampler State에 Address로 CLAMP를 주어서 나머지 픽셀들이 아래쪽으로 쭉 늘어지게 된다.

'DirectX11 2D > 과제' 카테고리의 다른 글
| <DirectX11 2D 과제> 원 출력(수정) (0) | 2021.11.04 |
|---|---|
| <DirectX11 2D 과제> 별모양 출력 (0) | 2021.09.02 |
| <DirectX11 2D 과제> 육각형 출력 (0) | 2021.09.01 |
| <DirectX11 2D 과제> 오각형 출력 (0) | 2021.09.01 |