-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
72 lines (57 loc) · 1.8 KB
/
Texture.cpp
File metadata and controls
72 lines (57 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Texture.h"
#include "NouException.h"
#define STB_IMAGE_IMPLEMENTATION
#include "CImg/stb_image.h"
Texture::Texture(GraphicsD11& gfx, std::string path)
{
int width, height, channels;
this->path = path;
unsigned char* imgdata = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (imgdata == nullptr)
{
throw NouException::BaseException(__LINE__, __FILE__, "Could not find file : " + path);
width = 1;
height = 1;
unsigned char data[] = { 255, 0, 255, 255 };
imgdata = data;
}
if (channels < 3)
{
int none = 3;
}
D3D11_TEXTURE2D_DESC td = {};
td.Width = width;
td.Height = height;
td.MipLevels = 0;
td.ArraySize = 1;
td.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
td.SampleDesc.Count = 1;
td.SampleDesc.Quality = 0;
td.Usage = D3D11_USAGE_DEFAULT;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
td.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = imgdata; //TODO
sd.SysMemPitch = width * sizeof(unsigned char) * 4; //TODO
Microsoft::WRL::ComPtr<ID3D11Texture2D> pTexture;
D3D11_SHADER_RESOURCE_VIEW_DESC vd = {};
vd.Format = td.Format;
vd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
vd.Texture2D.MostDetailedMip = 0;
vd.Texture2D.MipLevels = -1;
HRESULT res = GetDevice(gfx)->CreateTexture2D(&td, nullptr, &pTexture);
CHECK_HR_EXCEPT();
res = GetDevice(gfx)->CreateShaderResourceView(pTexture.Get(), &vd, &pTextureView);
CHECK_HR_EXCEPT();
GetContext(gfx)->UpdateSubresource(pTexture.Get(), 0, 0, sd.pSysMem, sd.SysMemPitch, 0);
GetContext(gfx)->GenerateMips(pTextureView.Get());
stbi_image_free(imgdata);
}
void Texture::Bind(GraphicsD11& gfx) noexcept
{
GetContext(gfx)->PSSetShaderResources(mSlot, 1u, pTextureView.GetAddressOf());
}
void Texture::SetSlot(UINT slot)
{
mSlot = slot;
}