Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / DX11ClothDemo / cloth.h
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2010 Advanced Micro Devices\r
4 \r
5 This software is provided 'as-is', without any express or implied warranty.\r
6 In no event will the authors be held liable for any damages arising from the use of this software.\r
7 Permission is granted to anyone to use this software for any purpose, \r
8 including commercial applications, and to alter it and redistribute it freely, \r
9 subject to the following restrictions:\r
10 \r
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
13 3. This notice may not be removed or altered from any source distribution.\r
14 */\r
15 \r
16 #ifndef CLOTH_H\r
17 #define CLOTH_H\r
18 \r
19 #include <fstream>\r
20 #include <iostream>\r
21 #include <iomanip>\r
22 \r
23 class piece_of_cloth \r
24 {\r
25 public:\r
26 \r
27 \r
28         void destroy(void)\r
29         {\r
30 \r
31                 if(created)\r
32                 {\r
33                         SAFE_RELEASE(g_pIndexBuffer);\r
34                         SAFE_RELEASE(pVB[0]);\r
35                         SAFE_RELEASE(g_pVB_UAV);\r
36                         SAFE_RELEASE(pVB_staging);\r
37                         SAFE_RELEASE(texture2D_view);\r
38                         if(m_vertexBufferDescriptor) delete [] m_vertexBufferDescriptor;\r
39                         if(cpu_buffer) delete [] cpu_buffer;\r
40                 }\r
41         }\r
42 \r
43         piece_of_cloth()\r
44         {\r
45                 created = false;\r
46                 m_vertexBufferDescriptor = NULL;\r
47                 cpu_buffer = NULL;\r
48         }\r
49         bool created;\r
50 \r
51         ID3D11Buffer* g_pIndexBuffer;\r
52         ID3D11Buffer* pVB[1];\r
53         ID3D11Buffer* pVB_staging;\r
54 \r
55         float* cpu_buffer;\r
56 \r
57         ID3D11UnorderedAccessView* g_pVB_UAV;\r
58         UINT Strides[1];\r
59         UINT Offsets[1];\r
60 \r
61         double x_offset, y_offset, z_offset;\r
62 \r
63 \r
64         ID3D11ShaderResourceView *texture2D_view;\r
65 \r
66         int width;\r
67         int height;\r
68 \r
69         btVertexBufferDescriptor *m_vertexBufferDescriptor;\r
70 \r
71         void create_texture(std::wstring filename)\r
72         {\r
73                 D3DX11_IMAGE_LOAD_INFO loadInfo;\r
74                 ZeroMemory(&loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO) );\r
75                 loadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE;\r
76                 loadInfo.Format = DXGI_FORMAT_BC1_UNORM;\r
77 \r
78                 HRESULT hr = D3DX11CreateShaderResourceViewFromFile(g_pd3dDevice, filename.c_str(), &loadInfo, NULL, &texture2D_view, NULL);\r
79                 hr = hr;\r
80 \r
81         }\r
82 \r
83 \r
84         void draw(void)\r
85         {\r
86 \r
87                 ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();\r
88 \r
89                 D3DXMATRIX mWorldViewProjection;\r
90                 D3DXVECTOR3 vLightDir;\r
91                 D3DXMATRIX mWorld;\r
92                 D3DXMATRIX mView;\r
93                 D3DXMATRIX mProj;\r
94 \r
95                 // Get the projection & view matrix from the camera class\r
96                 mProj = *g_Camera.GetProjMatrix();\r
97                 mView = *g_Camera.GetViewMatrix();\r
98 \r
99                 // Get the light direction\r
100                 vLightDir = g_LightControl.GetLightDirection();\r
101 \r
102                 // Per frame cb update\r
103                 D3D11_MAPPED_SUBRESOURCE MappedResource;\r
104 \r
105                 HRESULT hr;\r
106 #ifndef USE_GPU_COPY\r
107                 pd3dImmediateContext->Map( pVB_staging, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource );\r
108                 memcpy( MappedResource.pData, cpu_buffer, 4*width*height*8 );\r
109                 pd3dImmediateContext->Unmap(pVB_staging, 0 );\r
110                 pd3dImmediateContext->CopyResource(pVB[0], pVB_staging);\r
111 #endif // #ifndef USE_GPU_COPY\r
112 \r
113                 V( pd3dImmediateContext->Map( g_pcbPSPerFrame, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );\r
114                 CB_PS_PER_FRAME* pPerFrame = ( CB_PS_PER_FRAME* )MappedResource.pData;\r
115                 float fAmbient = 0.1f;\r
116                 pPerFrame->m_vLightDirAmbient = D3DXVECTOR4( vLightDir.x, vLightDir.y, vLightDir.z, fAmbient );\r
117                 pd3dImmediateContext->Unmap( g_pcbPSPerFrame, 0 );\r
118 \r
119                 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerFrameBind, 1, &g_pcbPSPerFrame );\r
120 \r
121 \r
122                 ///////////////////////////////////////Modify below//////////////////////////////////////////////////////\r
123 \r
124                 //Get the mesh\r
125                 //IA setup\r
126                 pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 );\r
127 \r
128                 //This is where we pass the vertex buffer to DX\r
129                 pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );\r
130 \r
131                 //This is where we pass the index buffer to DX\r
132                 pd3dImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );\r
133 \r
134                 /////////////////////////////////////////////////////////////////////////////////////////////////////////\r
135                 // Set the shaders\r
136                 pd3dImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );\r
137                 pd3dImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );\r
138                 pd3dImmediateContext->GSSetShader( g_pGeometryShader, NULL, 0);\r
139 \r
140                 // Set the per object constant data\r
141 \r
142                 D3DXMATRIXA16 m_scale;\r
143                 float sc = 10;\r
144                 D3DXMatrixScaling(&m_scale,sc,sc,sc);\r
145 \r
146 \r
147                 D3DXVECTOR3 vCenter( global_shift_x, global_shift_y, global_shift_z);\r
148 \r
149                 D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x+x_offset, -vCenter.y+y_offset, -vCenter.z+z_offset );\r
150 \r
151 \r
152                 mWorld = *g_Camera.GetWorldMatrix();\r
153                 mProj = *g_Camera.GetProjMatrix();\r
154                 mView = *g_Camera.GetViewMatrix();\r
155 \r
156                 mWorldViewProjection = mView * mProj;\r
157 \r
158 \r
159                 // VS Per object\r
160                 V( pd3dImmediateContext->Map( g_pcbVSPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );\r
161                 CB_VS_PER_OBJECT* pVSPerObject = ( CB_VS_PER_OBJECT* )MappedResource.pData;\r
162                 D3DXMatrixTranspose( &pVSPerObject->m_WorldViewProj, &mWorldViewProjection );\r
163                 D3DXMatrixTranspose( &pVSPerObject->m_World, &mWorld );\r
164                 pd3dImmediateContext->Unmap( g_pcbVSPerObject, 0 );\r
165 \r
166                 pd3dImmediateContext->VSSetConstantBuffers( g_iCBVSPerObjectBind, 1, &g_pcbVSPerObject );\r
167 \r
168                 // PS Per object\r
169                 V( pd3dImmediateContext->Map( g_pcbPSPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );\r
170                 CB_PS_PER_OBJECT* pPSPerObject = ( CB_PS_PER_OBJECT* )MappedResource.pData;\r
171                 pPSPerObject->m_vObjectColor = D3DXVECTOR4( 1, 1, 1, 1 );\r
172                 pd3dImmediateContext->Unmap( g_pcbPSPerObject, 0 );\r
173 \r
174                 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerObjectBind, 1, &g_pcbPSPerObject );\r
175 \r
176                 //Render\r
177                 SDKMESH_SUBSET* pSubset = NULL;\r
178                 D3D11_PRIMITIVE_TOPOLOGY PrimType;\r
179 \r
180                 if( g_wireFrame )\r
181                         pd3dImmediateContext->RSSetState(g_pRasterizerStateWF);\r
182                 else    \r
183                         pd3dImmediateContext->RSSetState(g_pRasterizerState);\r
184 \r
185                 pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );\r
186 \r
187                 {\r
188                         // Get the subset\r
189                         pSubset = g_Mesh11.GetSubset( 0, 0 );\r
190 \r
191                         pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);\r
192 \r
193                         pd3dImmediateContext->PSSetShaderResources(0,1,&texture2D_view);\r
194 \r
195                         //pd3dImmediateContext->DrawIndexed( (width*3*2+2 + height*width*3*2), 0, ( UINT )pSubset->VertexStart );\r
196                         pd3dImmediateContext->DrawIndexed( ((height-1)*(width-1)*3*2), 0, ( UINT )pSubset->VertexStart );\r
197                 }\r
198 \r
199                 SAFE_RELEASE(pd3dImmediateContext);\r
200         }\r
201 \r
202         void create_buffers(int width_, int height_)\r
203         {           \r
204                 width = width_;\r
205                 height = height_;\r
206 \r
207                 created = true;\r
208 \r
209 \r
210                 cpu_buffer = new float[width*height*8];\r
211                 memset(cpu_buffer,0,width*height*8*4);\r
212 \r
213                 // Set texture coordinates in output buffers\r
214                 for(int y = 0; y < height; y++)\r
215                 {\r
216                         for(int x = 0; x < width; x++)\r
217                         {\r
218                                 cpu_buffer[y*8*width + x*8 + 6] = x/( (float)(width-1));\r
219                                 cpu_buffer[y*8*width + x*8 + 7] = 1-y/((float)(height-1));\r
220                         }\r
221                 }\r
222 \r
223 \r
224 \r
225                 D3D11_BUFFER_DESC bufferDesc;\r
226                 ZeroMemory(&bufferDesc, sizeof(bufferDesc));\r
227                 bufferDesc.Usage = D3D11_USAGE_DEFAULT;\r
228                 bufferDesc.ByteWidth = sizeof(vertex_struct)*width*height;\r
229                 bufferDesc.StructureByteStride = 0;//sizeof(vertex_struct);\r
230                 bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER | D3D11_BIND_UNORDERED_ACCESS;\r
231                 bufferDesc.MiscFlags = 0;//D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;\r
232                 bufferDesc.CPUAccessFlags = 0;\r
233 \r
234                 vertex_struct *vertices = new vertex_struct[width*height];\r
235                 for(int y = 0; y < height; y++)\r
236                 {\r
237                         for(int x = 0; x < width; x++)\r
238                         {\r
239                                 double coord = sin(x/5.0)*50;\r
240                                 //coord = sin(y/);\r
241 \r
242                                 vertices[y*width+x].Pos = D3DXVECTOR3( (x/((float)(width-1)))*1000, coord, (y/((float)(height-1)))*1000); \r
243                                 vertices[y*width+x].Normal = D3DXVECTOR3(1,0,0);\r
244                                 vertices[y*width+x].Texcoord = D3DXVECTOR2(x/( (float)(width-1)), 1.f-y/((float)(height-1)));\r
245                         }\r
246                 }\r
247 \r
248                 D3D11_SUBRESOURCE_DATA InitData;\r
249                 InitData.pSysMem = vertices;\r
250                 InitData.SysMemPitch = 0;\r
251                 InitData.SysMemSlicePitch = 0;\r
252 \r
253                 HRESULT hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &pVB[0]);\r
254 \r
255 \r
256                 D3D11_UNORDERED_ACCESS_VIEW_DESC uavbuffer_desc;\r
257                 ZeroMemory(&uavbuffer_desc, sizeof(uavbuffer_desc));\r
258                 uavbuffer_desc.Format = DXGI_FORMAT_R32_FLOAT;\r
259                 uavbuffer_desc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;\r
260 \r
261                 uavbuffer_desc.Buffer.NumElements = width*height*sizeof(vertex_struct)/4;\r
262                 hr = g_pd3dDevice->CreateUnorderedAccessView(pVB[0], &uavbuffer_desc, &g_pVB_UAV);\r
263 \r
264 \r
265                 //What is this vertex stride thing all about?\r
266                 Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 );\r
267                 Offsets[0] = 0;\r
268 \r
269 \r
270                 //unsigned int indices[] = {0,1,2, 1,3,2};\r
271                 unsigned int* indices = new unsigned int[(height-1)*(width-1)*3*2];\r
272 \r
273                 for(int y = 0; y < height-1; y++)\r
274                 {\r
275                         for(int x = 0; x < width-1; x++)\r
276                         {\r
277                                 // *3 indices/triangle, *2 triangles/quad\r
278                                 int baseIndex = (x + y*(width-1))*3*2;\r
279                                 indices[baseIndex] = x + y*width;\r
280                                 indices[baseIndex+1] = x+1 + y*width;\r
281                                 indices[baseIndex+2] = x+width + y*width;\r
282 \r
283 \r
284                                 indices[baseIndex+3] = x + 1 +  y*width;\r
285                                 indices[baseIndex+4] = x+(width+1) + y*width;\r
286                                 indices[baseIndex+5] = x+width + y*width;\r
287                         }\r
288                 }\r
289 \r
290 \r
291                 bufferDesc.ByteWidth = sizeof(unsigned int)*((height-1)*(width-1)*3*2);\r
292                 bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;\r
293 \r
294                 InitData.pSysMem = indices;\r
295 \r
296                 hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &g_pIndexBuffer);\r
297 \r
298                 bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;\r
299                 bufferDesc.Usage = D3D11_USAGE_DYNAMIC;\r
300                 bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;\r
301                 bufferDesc.ByteWidth = sizeof(vertex_struct)*width*height;\r
302 \r
303                 hr = g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &pVB_staging);       \r
304 \r
305                 delete [] indices;\r
306                 delete [] vertices;\r
307         }\r
308 \r
309 };\r
310 #endif //CLOTH_H\r
311 \r
312 \r