-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
94 lines (77 loc) · 2.74 KB
/
Texture.cpp
File metadata and controls
94 lines (77 loc) · 2.74 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Defines.h"
#include "Texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Texture::Texture(ID3D11Device* device, std::string path)
{
int imgWidth, imgHeight;
unsigned char* image = stbi_load((path).c_str(), &imgWidth, &imgHeight, nullptr, STBI_rgb_alpha);
// Desc from https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-textures-create
D3D11_TEXTURE2D_DESC textureDesc = {};
textureDesc.Width = imgWidth;
textureDesc.Height = imgHeight;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.MiscFlags = 0;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_IMMUTABLE; // GPU READ
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA data = {};
data.pSysMem = image;
data.SysMemPitch = imgWidth * STBI_rgb_alpha;
ID3D11Texture2D* img;
if (path != "Models/asdf/330672.jpg")
{
HRESULT hr = device->CreateTexture2D(&textureDesc, &data, &img);
if (FAILED(hr))
{
std::cout << "FAILED TO CREATE TEXTURE 2D" << std::endl;
return;
}
hr = device->CreateShaderResourceView(img, nullptr, &this->texture);
if (FAILED(hr))
{
std::cout << "FAILED TO CREATE SHADER RESOURCE VIEW" << std::endl;
return;
}
img->Release();
}
stbi_image_free(image);
img = nullptr;
}
bool Texture::makeTexture(std::string file, ID3D11Device* device, ID3D11ShaderResourceView*& texSRV)
{
ID3D11Texture2D* tex;
struct stat buffer;
int width;
int height;
int channels;
unsigned char* texData = stbi_load(file.c_str(), &width, &height, &channels, 4);
D3D11_TEXTURE2D_DESC desc;
desc.Width = width;
desc.Height = height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = (void*)texData;
data.SysMemPitch = width * 4;
data.SysMemSlicePitch = width * height * 4;
if (FAILED(device->CreateTexture2D(&desc, &data, &tex))) {
printf("cannot create texture");
return false;
}
HRESULT hr = device->CreateShaderResourceView(tex, nullptr, &texSRV);
delete[] texData;
tex->Release();
return !FAILED(hr);
}