-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicCubeMapping.cpp
More file actions
269 lines (221 loc) · 7.53 KB
/
DynamicCubeMapping.cpp
File metadata and controls
269 lines (221 loc) · 7.53 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "Defines.h"
#include "DynamicCubeMapping.h"
DynamicCubeMapping::DynamicCubeMapping()
{
//cubeTex = 0;
cubeSRV = 0;
for (int i = 0; i < 6; ++i)
{
dynamicCubeMapRTV[i] = 0;
}
dynamicCubeMapDSV = 0;
}
DynamicCubeMapping::~DynamicCubeMapping()
{
}
// this function works
bool DynamicCubeMapping::BuildCubeFaceCameras(float x, float y, float z, Camera cubeCams[6])
{
// Generate the cube map about the given position
XMFLOAT3 center(x, y, z);
XMFLOAT3 targets[6] =
{
XMFLOAT3(x + 1.0f, y, z), // +X
XMFLOAT3(x - 1.0f, y, z), // -X
XMFLOAT3(x, y + 1.0f, z), // +Y
XMFLOAT3(x, y - 1.0f, z), // -Y
XMFLOAT3(x, y, z + 1.0f), // +Z
XMFLOAT3(x, y, z - 1.0f) // -Z
};
/*
Use world up vector (0,1,0) for all directions except +Y/-Y.
In these cases, we are looking down +Y or -Y.
*/
XMFLOAT3 ups[6] =
{
XMFLOAT3(0.0f, 1.0f, 0.0f), // +X
XMFLOAT3(0.0f, 1.0f, 0.0f), // -X
XMFLOAT3(0.0f, 0.0f, -1.0f), // +Y
XMFLOAT3(0.0f, 0.0f, +1.0f), // -Y
XMFLOAT3(0.0f, 1.0f, 0.0f), // +Z
XMFLOAT3(0.0f, 1.0f, 0.0f) // -Z
};
// Use sphere worldspace and cubemap view and projection
for (int i = 0; i < 6; ++i) {
cubeCams[i].SetPos(XMFLOAT3(center.x, center.y, center.z));
cubeCams[i].SetTargetPosition(targets[i].x, targets[i].y, targets[i].z);
cubeCams[i].SetUpvector(ups[i].x, ups[i].y, ups[i].z);
cubeCams[i].SetViewMatrixForCube(XMVECTOR(XMVectorSet(center.x, center.y, center.z, 0.0f)), XMVECTOR(XMVectorSet(targets[i].x, targets[i].y, targets[i].z, 0.0f)), XMVECTOR(XMVectorSet(ups[i].x, ups[i].y, ups[i].z, 0.0f)));
}
//should work?
return true;
}
//this function works
bool DynamicCubeMapping::BuildCubeMapViews(ID3D11Device* device/*, const wchar_t* texPath*/)
{
D3D11_TEXTURE2D_DESC texDesc;
texDesc.Width = cubeMapSize;
texDesc.Height = cubeMapSize;
texDesc.MipLevels = 0;
texDesc.ArraySize = 6;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
ID3D11Texture2D* cubeTex = 0;
HRESULT hr =(device->CreateTexture2D(&texDesc, nullptr, &cubeTex));
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE TEX2D FOR CUBEMAP" << std::endl;
return false;
}
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
rtvDesc.Format = texDesc.Format;
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
rtvDesc.Texture2DArray.ArraySize = 1;
rtvDesc.Texture2DArray.MipSlice = 0;
//for each cubeMapFace
for (int i = 0; i < 6; ++i)
{
rtvDesc.Texture2DArray.FirstArraySlice = i;
hr = (device->CreateRenderTargetView(cubeTex, &rtvDesc, &dynamicCubeMapRTV[i]));
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE RTV FOR CUBEMAP" << std::endl;
return false;
}
}
//D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
//ZeroMemory(&rtvDesc, sizeof(rtvDesc));
//rtvDesc.Format = texDesc.Format; // Same format as texture
//rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; // using it as texture2D
//rtvDesc.Texture2D.MipSlice = 0;
//
// for (int i = 0; i < 8; i++)
// {
// hr = device->CreateRenderTargetView(cubeTex, &rtvDesc, &dynamicCubeMapRTV[i]);
// if (FAILED(hr))
// {
// std::cerr << "ERROR! FAILED TO CREATE RTV GBUFFER!" << std::endl;
// return false;
// }
// }
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = texDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
srvDesc.TextureCube.MostDetailedMip = 0;
srvDesc.TextureCube.MipLevels = -1;
hr = device->CreateShaderResourceView(cubeTex, &srvDesc, &cubeSRV);
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE CUBE SRV!" << std::endl;
return false;
}
cubeTex->Release();
cubeTex = nullptr;
D3D11_TEXTURE2D_DESC depthTexDesc;
depthTexDesc.Width = cubeMapSize;
depthTexDesc.Height = cubeMapSize;
depthTexDesc.MipLevels = 1;
depthTexDesc.ArraySize = 1;
depthTexDesc.SampleDesc.Count = 1;
depthTexDesc.SampleDesc.Quality = 0;
depthTexDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthTexDesc.Usage = D3D11_USAGE_DEFAULT;
depthTexDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthTexDesc.CPUAccessFlags = 0;
depthTexDesc.MiscFlags = 0;
ID3D11Texture2D* depthTex = 0;
hr = (device->CreateTexture2D(&depthTexDesc, 0, &depthTex));
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE TEXTURE2D FOR CUBEMAP!" << std::endl;
return false;
}
// Create the depth stencil view for the entire cube
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
dsvDesc.Format = depthTexDesc.Format;
dsvDesc.Flags = 0;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;
hr = (device->CreateDepthStencilView(depthTex, &dsvDesc, &dynamicCubeMapDSV));
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE DSVIEW FOR CUBEMAP" << std::endl;
return false;
}
depthTex->Release();
depthTex = nullptr;
//
// Viewport for drawing into cubemap.
//
ZeroMemory(&cubeMapViewport, sizeof(D3D11_VIEWPORT));
cubeMapViewport.TopLeftX = 0.0f;
cubeMapViewport.TopLeftY = 0.0f;
cubeMapViewport.Width = (float)cubeMapSize;
cubeMapViewport.Height = (float)cubeMapSize;
cubeMapViewport.MinDepth = 0.0f;
cubeMapViewport.MaxDepth = 1.0f;
return true;
}
bool DynamicCubeMapping::RenderDynamicCubeMapping(ID3D11Device* device, ID3D11DeviceContext* context,/* Particles* particles, RenderParticles* particleRenderer,*/ RenderModels* modelRenderer, const std::vector<Model*>& models, Camera* cubeCameras)
{
ID3D11ShaderResourceView* const nullsrv[8] = { nullptr };
context->PSSetShaderResources(0, 8, nullsrv);
// context->PSSetShaderResources(0, gBuffer.NROFBUFFERS, gBuffer.gBufferSrv);
//context->RSSetViewports(NULL, NULL);
//context->RSSetViewports(1, &cubeMapViewport);
float backGroundColor[4] = { 0.2f, 0.2f, 0.2f, 0 }; //background on sphere
XMMATRIX cubeProjectionMatrix = XMMatrixPerspectiveFovLH(0.5f * XM_PI, 1.0f, 0.1f, 1000.0f);
//loop for "mirror" stuff on the sphere
for (int i = 0; i < 6; i++)
{
context->ClearRenderTargetView(dynamicCubeMapRTV[i], backGroundColor);
context->ClearDepthStencilView(dynamicCubeMapDSV, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
context->OMSetRenderTargets(1, dynamicCubeMapRTV, dynamicCubeMapDSV);
//context->OMSetRenderTargets(1, &dynamicCubeMapRTV[i], dynamicCubeMapDSV); this dont work. Gör om till [8] och använd hela varje iteration
//draw scene "on sphere"
XMMATRIX views;
views = cubeCameras[i].GetViewMatrix();
for (auto& model : models) //används detta för icke frustum, fixa en annan loop för frustum (
{
//modelRenderer->RenderOnCubeMap(device, context, model, false, cubeCameras[i]);
}
}
//context->GenerateMips(cubeSRV);
return true;
}
void DynamicCubeMapping::ShutDown()
{
if (cubeSRV)
{
cubeSRV->Release();
cubeSRV = nullptr;
}
//cubeTex->Release();
//delete cubeTex;
for (int j = 0; j < 6; j++)
{
if (dynamicCubeMapRTV[j])
{
dynamicCubeMapRTV[j]->Release();
dynamicCubeMapRTV[j] = nullptr;
}
}
if (dynamicCubeMapDSV)
{
dynamicCubeMapDSV->Release();
dynamicCubeMapDSV = nullptr;
}
}
D3D11_VIEWPORT& DynamicCubeMapping::GetViewPort()
{
return this->cubeMapViewport;
}
ID3D11ShaderResourceView* DynamicCubeMapping::GetCubeSRV()
{
return this->cubeSRV;
}