DirectX11 공부 6일차(Wrapping Class - Viewport, ShaderResource, SamplerState)
06 Jun 2021
Reading time ~2 minutes
Wrapping Class - Viewport
이젠 Viewport 뤠핑 할 순서이다.
딱히 특이한건 없고 그냥 클래스 생성 뿐이라 따로 설명은 없을거 같다….
D3D11_Viewport.h
#pragma once
class D3D11_Viewport final
{
public:
D3D11_Viewport
(
const float& x = 0.0f,
const float& y = 0.0f,
const float& width = 0.0f,
const float& height = 0.0f,
const float& min_depth = 0.0f,
const float& map_depth = 1.0f
);
D3D11_Viewport(const D3D11_Viewport& rhs);
~D3D11_Viewport() = default;
bool operator==(const D3D11_Viewport& rhs) const { return x == rhs.x && y == rhs.y && width == rhs.width && height == rhs.height && min_depth == rhs.min_depth && max_depth && rhs.max_depth; }
bool operator!=(const D3D11_Viewport& rhs) const { return !(*this == rhs); }
bool IsDefined() const { return x != 0.0f || y != 0.0f || width != 0.0f || height != 0.0f || min_depth != 0.0 || max_depth != 0.0f; }
float AspecRatio() const { return width / height; }
public:
static const D3D11_Viewport Undefined_viewport;
public:
float x = 0.0f;
float y = 0.0f;
float width = 0.0f;
float height = 0.0f;
float min_depth = 0.0f;
float max_depth = 0.0f;
};
D3D11_Viewport.cpp
#include "stdafx.h"
#include "D3D11_Viewport.h"
const D3D11_Viewport D3D11_Viewport::Undefined_viewport(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
D3D11_Viewport::D3D11_Viewport(const float& x, const float& y, const float& width, const float& height, const float& min_depth, const float& map_depth)
: x(x)
, y(y)
, width(width)
, height(height)
, min_depth(min_depth)
, max_depth(max_depth)
{
}
D3D11_Viewport::D3D11_Viewport(const D3D11_Viewport& rhs)
: x(rhs.x)
, y(rhs.y)
, width(rhs.width)
, height(rhs.height)
, min_depth(rhs.min_depth)
, max_depth(rhs.max_depth)
{
}
변경하는 부분은 따로 적진 않겠다. 바뀐게 많이 없다.
대신 이것을 사용한 곳은 처음에 만드는 곳이니까 Graphics에 넣어라
Wrapping Class - ShaderResource
이젠 ShaderResource 뤠핑 할 순서이다.
여기도 설명할게 없다.
D3D11_Texture.h
#pragma once
class D3D11_Texture final
{
public:
D3D11_Texture(class Graphics* graphics);
~D3D11_Texture();
ID3D11ShaderResourceView* GetResource() const { return shader_resource; }
void Create(const std::string path);
void Clear();
private:
ID3D11Device* device = nullptr;
ID3D11ShaderResourceView* shader_resource = nullptr;
};
D3D11_Texture.cpp
#include "stdafx.h"
#include "D3D11_Texture.h"
D3D11_Texture::D3D11_Texture(Graphics* graphics)
{
device = graphics->GetDevice();
}
D3D11_Texture::~D3D11_Texture()
{
Clear();
}
void D3D11_Texture::Create(const std::string path)
{
HRESULT hr = D3DX11CreateShaderResourceViewFromFileA
(
device,
path.c_str(),
nullptr,
nullptr,
&shader_resource,
nullptr
);
assert(SUCCEEDED(hr));
}
void D3D11_Texture::Clear()
{
SAFE_RELEASE(shader_resource);
}
Wrapping Class - SamplerState
이젠 SamplerState 뤠핑 할 순서이다.
여기도 설명할게 없다.
D3D11_SamplerState.h
#pragma once
class D3D11_SamplerState
{
public:
D3D11_SamplerState(Graphics* graphics);
~D3D11_SamplerState();
ID3D11SamplerState* GetResource() const { return state; }
void Create
(
const D3D11_FILTER& filter,
const D3D11_TEXTURE_ADDRESS_MODE& address_mode
);
void Clear();
private:
ID3D11Device* device = nullptr;
ID3D11SamplerState* state = nullptr;
};
Wrapping Class - D3D11_SamplerState.cpp
#include "stdafx.h"
#include "D3D11_SamplerState.h"
D3D11_SamplerState::D3D11_SamplerState(Graphics* graphics)
{
device = graphics->GetDevice();
}
D3D11_SamplerState::~D3D11_SamplerState()
{
Clear();
}
void D3D11_SamplerState::Create(const D3D11_FILTER& filter, const D3D11_TEXTURE_ADDRESS_MODE& address_mode)
{
D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_SAMPLER_DESC));
desc.Filter = filter;
desc.AddressU = address_mode;
desc.AddressV = address_mode;
desc.AddressW = address_mode;
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
desc.MaxAnisotropy = 16;
desc.BorderColor[0] = 0.0f;
desc.BorderColor[1] = 0.0f;
desc.BorderColor[2] = 0.0f;
desc.BorderColor[3] = 0.0f;
desc.MinLOD = std::numeric_limits<float>::min();
desc.MaxLOD = std::numeric_limits<float>::max();
desc.MipLODBias = 0;
HRESULT hr = device->CreateSamplerState(&desc, &state);
assert(SUCCEEDED(hr));
}
void D3D11_SamplerState::Clear()
{
SAFE_RELEASE(state);
}