-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexture.cpp
More file actions
130 lines (107 loc) · 3.55 KB
/
Texture.cpp
File metadata and controls
130 lines (107 loc) · 3.55 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "Texture.h"
Texture::Texture()
{
device = NULL;
effect = NULL;
tex_texture = NULL;
srv_texture = NULL;
fxVar_texture = NULL;
texFileName = "NULL";
texVarName = "tex_default";
}
Texture::~Texture()
{
clear();
}
void Texture::clear()
{
SAFE_RELEASE(tex_texture);
SAFE_RELEASE(srv_texture);
//SAFE_RELEASE(fxVar_texture);
}
void Texture::setDeviceAndFx(ID3D10Device* _device, ID3D10Effect* _effect)
{
device = _device;
effect = _effect;
}
void Texture::setTexture(string _resourceFolder, string _texFileName)
{
resourceFolder = _resourceFolder;
texFileName = _texFileName;
}
void Texture::setShaderTargets(string _texVarName)
{
texVarName = _texVarName;
}
void Texture::setDynamic(int _width, int _height)
{
clear();
ZeroMemory(&desc_texture, sizeof(desc_texture));
desc_texture.Width = _width;
desc_texture.Height = _height;
desc_texture.MipLevels = 1;
desc_texture.ArraySize = 1;
desc_texture.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc_texture.SampleDesc.Count = 1;
desc_texture.Usage = D3D10_USAGE_DYNAMIC;
desc_texture.BindFlags = D3D10_BIND_SHADER_RESOURCE;
desc_texture.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
HR(device->CreateTexture2D(&desc_texture, NULL, &tex_texture));
}
void Texture::setPixel(const byte* _source, int _x, int _y)
{
D3D10_MAPPED_TEXTURE2D mappedTex;
tex_texture->Map( D3D10CalcSubresource(0, 0, 1), D3D10_MAP_WRITE_DISCARD, 0, &mappedTex );
UCHAR* pTexels = (UCHAR*)mappedTex.pData;
UINT rowStart = _x * mappedTex.RowPitch;
UINT colStart = _y * 4;
pTexels[rowStart + colStart + 0] = _source[rowStart + colStart + 0]; // Red
pTexels[rowStart + colStart + 1] = _source[rowStart + colStart + 1]; // Green
pTexels[rowStart + colStart + 2] = _source[rowStart + colStart + 2]; // Blue
pTexels[rowStart + colStart + 3] = _source[rowStart + colStart + 3]; // Alpha
tex_texture->Unmap( D3D10CalcSubresource(0, 0, 1) );
}
void Texture::setAllPixels(const byte* _source)
{
D3D10_MAPPED_TEXTURE2D mappedTex;
tex_texture->Map( D3D10CalcSubresource(0, 0, 1), D3D10_MAP_WRITE_DISCARD, 0, &mappedTex );
UCHAR* texels = (UCHAR*)mappedTex.pData;
for( UINT row = 0; row < desc_texture.Height; row++ )
{
UINT rowStart = row * mappedTex.RowPitch;
for( UINT col = 0; col < desc_texture.Width; col++ )
{
UINT colStart = col * 4;
texels[rowStart + colStart + 0] = _source[rowStart + colStart + 0]; //red 1
texels[rowStart + colStart + 1] = _source[rowStart + colStart + 1]; //green 0
texels[rowStart + colStart + 2] = _source[rowStart + colStart + 2]; //blue 3
texels[rowStart + colStart + 3] = _source[rowStart + colStart + 3]; //alpha 2
}
}
tex_texture->Unmap( D3D10CalcSubresource(0, 0, 1) );
}
void Texture::initTexture()
{
if(tex_texture != NULL)
{
D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = desc_texture.Format;
srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc_texture.MipLevels;
srvDesc.Texture2D.MostDetailedMip = desc_texture.MipLevels -1;
HR(device->CreateShaderResourceView(tex_texture, &srvDesc, &srv_texture));
}
else
{
wstring wstrTemp(texFileName.length(), L' ');
copy(texFileName.begin(), texFileName.end(), wstrTemp.begin());
HR(D3DX10CreateShaderResourceViewFromFile(device, wstrTemp.c_str(), NULL, NULL, &srv_texture, NULL));
}
}
void Texture::useTexture()
{
wstring wstrTemp(texVarName.length(), L' ');
copy(texFileName.begin(), texFileName.end(), wstrTemp.begin());
fxVar_texture = effect->GetVariableByName(texVarName.c_str())->AsShaderResource();
HR(fxVar_texture->SetResource(srv_texture));
}