[DirectX11] 017 Texture Load
목차
Texture Load
컴퓨터에 저장된 Texture 불러오기
Texture Load 코드
Texture.h
더보기
#pragma once
#include "Systems/IExecute.h"
class TextureLoadDemo : public IExecute
{
public:
virtual void Initialize() override;
virtual void Ready() override {}
virtual void Destroy() override;
virtual void Update() override;
virtual void PreRender() override {}
virtual void Render() override;
virtual void PostRender() override {}
virtual void ResizeScreen() override {}
private:
void LoadTexture(wstring file);
private:
Shader* shader;
VertexTexture vertices[6];
ID3D11Buffer* vertexBuffer;
UINT indices[6] = { 0, 1, 2, 2, 1, 3 };
ID3D11Buffer* indexBuffer;
Matrix world;
//ID3D11ShaderResourceView* srv;
Texture* texture = NULL;
};
Texture.cpp
더보기
#include "stdafx.h"
#include "TextureLoadDemo.h"
void TextureLoadDemo::Initialize()
{
Context::Get()->GetCamera()->Position(0, 0, -5.0f);
shader = new Shader(L"16_Texture.fx");
vertices[0].Position = Vector3(-0.5f, -0.5f, 0.0f);
vertices[1].Position = Vector3(-0.5f, +0.5f, 0.0f);
vertices[2].Position = Vector3(+0.5f, -0.5f, 0.0f);
vertices[3].Position = Vector3(+0.5f, +0.5f, 0.0f);
vertices[0].Uv = Vector2(0, 1);
vertices[1].Uv = Vector2(0, 0);
vertices[2].Uv = Vector2(1, 1);
vertices[3].Uv = Vector2(1, 0);
//Create Vertex Buffer
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.ByteWidth = sizeof(VertexTexture) * 4;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA subResource = { 0 };
subResource.pSysMem = vertices;
Check(D3D::GetDevice()->CreateBuffer(&desc, &subResource, &vertexBuffer));
}
//Create Index Buffer
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.ByteWidth = sizeof(UINT) * 6;
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA subResource = { 0 };
subResource.pSysMem = indices;
Check(D3D::GetDevice()->CreateBuffer(&desc, &subResource, &indexBuffer));
}
D3DXMatrixIdentity(&world);
}
void TextureLoadDemo::Destroy()
{
SafeDelete(shader);
SafeRelease(vertexBuffer);
SafeRelease(indexBuffer);
//SafeRelease(srv);
SafeDelete(texture);
}
void TextureLoadDemo::Update()
{
if (ImGui::Button("Open") == true)
{
function<void(wstring)> f = bind(&TextureLoadDemo::LoadTexture, this, placeholders::_1);
D3DDesc desc = D3D::GetDesc();
Path::OpenFileDialog(L"", Path::ImageFilter, L"../../_Texture/", f, desc.Handle);
}
}
void TextureLoadDemo::Render()
{
shader->AsMatrix("World")->SetMatrix(world);
shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
if(texture != NULL)
shader->AsSRV("Map")->SetResource(texture->SRV());
UINT stride = sizeof(VertexTexture);
UINT offset = 0;
D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
D3D::GetDC()->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
shader->DrawIndexed(0, 0, 6);
}
void TextureLoadDemo::LoadTexture(wstring file)
{
//D3DDesc desc = D3D::GetDesc();
//MessageBox(desc.Handle, file.c_str(), L"", MB_OK);
SafeDelete(texture);
texture = new Texture(file);
}
void TextureLoadDemo::Update()
{
if (ImGui::Button("Open") == true)
{
function<void(wstring)> f = bind(&TextureLoadDemo::LoadTexture, this, placeholders::_1);
D3DDesc desc = D3D::GetDesc();
Path::OpenFileDialog(L"", Path::ImageFilter, L"../../_Texture/", f, desc.Handle);
}
}
if(texture != NULL)
shader->AsSRV("Map")->SetResource(texture->SRV());
참고사항
D3D.h
struct D3DDesc
{
wstring AppName;
HINSTANCE Instance;
HWND Handle;
float Width;
float Height;
bool bVsync;
bool bFullScreen;
D3DXCOLOR Background;
};
Main.cpp
- 특이사항 없음. 생략
실행화면
'⭐ DirectX > DirectX11 3D' 카테고리의 다른 글
[DirectX11] 019 Heightmap (0) | 2023.01.16 |
---|---|
[DirectX11] 018 Texture Sampler (0) | 2023.01.16 |
[DirectX11] 016 Texture (0) | 2023.01.14 |
[DirectX11] 015 Cube (0) | 2023.01.13 |
[DirectX11] 013~014 Camera (0) | 2023.01.11 |
댓글
이 글 공유하기
다른 글
-
[DirectX11] 019 Heightmap
[DirectX11] 019 Heightmap
2023.01.16 -
[DirectX11] 018 Texture Sampler
[DirectX11] 018 Texture Sampler
2023.01.16 -
[DirectX11] 016 Texture
[DirectX11] 016 Texture
2023.01.14 -
[DirectX11] 015 Cube
[DirectX11] 015 Cube
2023.01.13