Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / DX11ClothDemo / cap.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 CAP_H\r
17 #define CAP_H\r
18 \r
19 class cap \r
20 {\r
21         public:\r
22 \r
23         ID3D11Buffer* g_pIndexBuffer;\r
24         ID3D11Buffer* pVB[1];\r
25         UINT Strides[1];\r
26         UINT Offsets[1];\r
27 \r
28         double x_offset, y_offset, z_offset;\r
29         \r
30         int width,height;\r
31         bool top;\r
32 \r
33         ID3D11Texture2D *texture2D;\r
34         ID3D11ShaderResourceView *texture2D_view;\r
35         btCollisionShape *collisionShape;\r
36         btCollisionObject *collisionObject;\r
37 \r
38         void set_collision_object(btCollisionObject* co)\r
39         {\r
40                 collisionObject = co;\r
41         }\r
42 \r
43         void set_collision_shape(btCollisionShape* cs)\r
44         {\r
45                 collisionShape = cs;\r
46         }\r
47 \r
48         void create_texture(void)\r
49         {\r
50                 D3DX11_IMAGE_LOAD_INFO loadInfo;\r
51                 ZeroMemory(&loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO) );\r
52                 loadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE;\r
53                 loadInfo.Format = DXGI_FORMAT_BC1_UNORM;\r
54 \r
55                 HRESULT hr = D3DX11CreateShaderResourceViewFromFile(g_pd3dDevice, L"texture.bmp", &loadInfo, NULL, &texture2D_view, NULL);\r
56                 hr = hr;\r
57         }\r
58 \r
59         void destroy()\r
60         {\r
61                 SAFE_RELEASE( g_pIndexBuffer );\r
62                 SAFE_RELEASE( pVB[0] );\r
63                 SAFE_RELEASE( texture2D );\r
64                 SAFE_RELEASE( texture2D_view );\r
65         }\r
66 \r
67         void draw(void)\r
68         {\r
69                 \r
70 \r
71                 if (!collisionObject)\r
72                         return;\r
73 \r
74 \r
75                 ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();\r
76 \r
77                 D3DXMATRIX mWorldViewProjection;\r
78                 D3DXVECTOR3 vLightDir;\r
79                 D3DXMATRIX mWorld;\r
80                 D3DXMATRIX mView;\r
81                 D3DXMATRIX mProj;\r
82 \r
83                 // Get the projection & view matrix from the camera class\r
84                 mProj = *g_Camera.GetProjMatrix();\r
85                 mView = *g_Camera.GetViewMatrix();\r
86 \r
87                 // Get the light direction\r
88                 vLightDir = g_LightControl.GetLightDirection();\r
89 \r
90                 // Per frame cb update\r
91                 D3D11_MAPPED_SUBRESOURCE MappedResource;\r
92 \r
93                 HRESULT hr;\r
94 \r
95                 V( pd3dImmediateContext->Map( g_pcbPSPerFrame, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );\r
96                 CB_PS_PER_FRAME* pPerFrame = ( CB_PS_PER_FRAME* )MappedResource.pData;\r
97                 float fAmbient = 0.1f;\r
98                 pPerFrame->m_vLightDirAmbient = D3DXVECTOR4( vLightDir.x, vLightDir.y, vLightDir.z, fAmbient );\r
99                 pd3dImmediateContext->Unmap( g_pcbPSPerFrame, 0 );\r
100 \r
101                 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerFrameBind, 1, &g_pcbPSPerFrame );\r
102 \r
103 \r
104                 ///////////////////////////////////////Modify below//////////////////////////////////////////////////////\r
105 \r
106                 //Get the mesh\r
107                 //IA setup\r
108                 pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 );\r
109 \r
110                 //This is where we pass the vertex buffer to DX\r
111                 pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );\r
112 \r
113                 //This is where we pass the index buffer to DX\r
114                 pd3dImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );\r
115 \r
116                 /////////////////////////////////////////////////////////////////////////////////////////////////////////\r
117                 // Set the shaders\r
118                 pd3dImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );\r
119                 pd3dImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );\r
120                 pd3dImmediateContext->GSSetShader( g_pGeometryShader, NULL, 0);\r
121 \r
122                 // Set the per object constant data\r
123 \r
124         \r
125 \r
126                 \r
127                 btTransform trans = collisionObject->getWorldTransform();\r
128 \r
129                 \r
130             \r
131                 btVector3 origin = trans.getOrigin();\r
132                 btMatrix3x3 btM = trans.getBasis();\r
133                 \r
134                 btScalar* scalar_matrix = new btScalar[16];;\r
135                 trans.getOpenGLMatrix(scalar_matrix);\r
136                 \r
137                 D3DXMATRIXA16 m_trans(scalar_matrix[0],scalar_matrix[1],scalar_matrix[2],scalar_matrix[3],\r
138                                                           scalar_matrix[4],scalar_matrix[5],scalar_matrix[6],scalar_matrix[7],\r
139                                                           scalar_matrix[8],scalar_matrix[9],scalar_matrix[10],scalar_matrix[11],\r
140                                                           scalar_matrix[12],scalar_matrix[13],scalar_matrix[14],scalar_matrix[15]);\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                 D3DXMATRIXA16 m_trans_transpose;\r
153                 D3DXMatrixTranspose(&m_trans_transpose,&m_trans);\r
154                 \r
155                 mWorld = *g_Camera.GetWorldMatrix() ;\r
156                 mProj = *g_Camera.GetProjMatrix();\r
157                 mView = m_trans * *g_Camera.GetViewMatrix();\r
158 \r
159                 mWorldViewProjection = mView * mProj;\r
160 \r
161 \r
162                 // VS Per object\r
163                 V( pd3dImmediateContext->Map( g_pcbVSPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );\r
164                 CB_VS_PER_OBJECT* pVSPerObject = ( CB_VS_PER_OBJECT* )MappedResource.pData;\r
165                 D3DXMatrixTranspose( &pVSPerObject->m_WorldViewProj, &mWorldViewProjection );\r
166                 D3DXMatrixTranspose( &pVSPerObject->m_World, &mWorld );\r
167                 pd3dImmediateContext->Unmap( g_pcbVSPerObject, 0 );\r
168 \r
169                 pd3dImmediateContext->VSSetConstantBuffers( g_iCBVSPerObjectBind, 1, &g_pcbVSPerObject );\r
170 \r
171                 // PS Per object\r
172                 V( pd3dImmediateContext->Map( g_pcbPSPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );\r
173                 CB_PS_PER_OBJECT* pPSPerObject = ( CB_PS_PER_OBJECT* )MappedResource.pData;\r
174                 pPSPerObject->m_vObjectColor = D3DXVECTOR4( 1, 1, 1, 1 );\r
175                 pd3dImmediateContext->Unmap( g_pcbPSPerObject, 0 );\r
176 \r
177                 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerObjectBind, 1, &g_pcbPSPerObject );\r
178 \r
179                 //Render\r
180                 SDKMESH_SUBSET* pSubset = NULL;\r
181                 D3D11_PRIMITIVE_TOPOLOGY PrimType;\r
182 \r
183                 pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );\r
184 \r
185                 {\r
186                         // Get the subset\r
187                         pSubset = g_Mesh11.GetSubset( 0, 0 );\r
188 \r
189                         pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);\r
190 \r
191                         pd3dImmediateContext->PSSetShaderResources(0,1,&texture2D_view);\r
192 \r
193                         pd3dImmediateContext->DrawIndexed( (width*3*2+2 + height*width*3*2), 0, ( UINT )pSubset->VertexStart );\r
194                 }\r
195 \r
196                 SAFE_RELEASE(pd3dImmediateContext);\r
197         }\r
198 \r
199 \r
200         void create_buffers(int width_, int height_, bool top_)\r
201         {\r
202 \r
203                 top = top_;\r
204                 width = width_;\r
205                 height = height_;\r
206 \r
207                 D3D11_BUFFER_DESC bufferDesc;\r
208                 bufferDesc.Usage = D3D11_USAGE_DEFAULT;\r
209                 bufferDesc.ByteWidth = sizeof(vertex_struct)*width*height;\r
210                 bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;\r
211                 bufferDesc.CPUAccessFlags = 0;\r
212                 bufferDesc.MiscFlags = 0;\r
213 \r
214 \r
215                 vertex_struct *vertices = new vertex_struct[width*height];\r
216 \r
217                 btCapsuleShape* cs = static_cast<btCapsuleShape*>(collisionShape);\r
218                 if (cs)\r
219                 {\r
220                         float radius = cs->getRadius();\r
221                         float halfHeight = cs->getHalfHeight();\r
222                         \r
223 \r
224                         if (top)\r
225                         {\r
226                                 for(int y = 0; y < height; y++)\r
227                                 {\r
228                                         for(int x = 0; x < width; x++)\r
229                                         {       \r
230                                                 float X =  (x/((float)(width-1)))*3.14159;\r
231                                                 float Y =  (y/((float)(height-1)))*3.14159;\r
232                                                 float z_coord = radius*cos(X)*sin(Y);\r
233                                                 float y_coord = radius*sin(X)*sin(Y) + halfHeight;\r
234                                                 float x_coord = radius*cos(Y);\r
235                                                 vertices[y*width+x].Pos = D3DXVECTOR3(x_coord, y_coord, z_coord); \r
236                                                 vertices[y*width+x].Normal = D3DXVECTOR3(x_coord,y_coord-halfHeight,z_coord);\r
237                                                 vertices[y*width+x].Texcoord = D3DXVECTOR2(x/( (float)(width-1)), y/((float)(height-1)));\r
238                                         }\r
239                                 }\r
240                         } else  {\r
241                                 for(int y = 0; y < height; y++)\r
242                                 {\r
243                                         for(int x = 0; x < width; x++)\r
244                                         {       \r
245                                                 float X =  (x/((float)(width-1)))*3.14159;\r
246                                                 float Y =  (y/((float)(height-1)))*3.14159;\r
247                                                 float z_coord = radius*cos(X)*sin(Y);\r
248                                                 float y_coord = -radius*sin(X)*sin(Y) - halfHeight;\r
249                                                 float x_coord = radius*cos(Y);\r
250                                                 vertices[y*width+x].Pos = D3DXVECTOR3(x_coord, y_coord, z_coord); \r
251                                                 vertices[y*width+x].Normal = D3DXVECTOR3(x_coord,y_coord+halfHeight,z_coord);\r
252                                                 vertices[y*width+x].Texcoord = D3DXVECTOR2(x/( (float)(width-1)), y/((float)(height-1)));\r
253                                         }\r
254                                 }\r
255                         }\r
256 \r
257                         D3D11_SUBRESOURCE_DATA InitData;\r
258                         InitData.pSysMem = vertices;\r
259                         InitData.SysMemPitch = 0;\r
260                         InitData.SysMemSlicePitch = 0;\r
261 \r
262                         HRESULT hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &pVB[0]);\r
263                     \r
264                         \r
265                         \r
266                         //What is this vertex stride thing all about?\r
267                         Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 );\r
268                         Offsets[0] = 0;\r
269 \r
270                         unsigned int* indices = new unsigned int[width*3*2+2 + height*width*3*2];\r
271 \r
272                         for(int y = 0; y < height-1; y++)\r
273                         {\r
274                                 for(int x = 0; x < width-1; x++)\r
275                                 {\r
276                                         indices[x*3*2 + y*width*3*2] = x + y*width;\r
277                                         indices[x*3*2+1 + y*width*3*2] = x+1 + y*width;\r
278                                         indices[x*3*2+2 + y*width*3*2] = x+width + y*width;\r
279 \r
280                                         indices[x*3*2 + 3 + y*width*3*2] = x + 1 +  y*width;\r
281                                         indices[x*3*2 + 4 + y*width*3*2] = x+(width+1) + y*width;\r
282                                         indices[x*3*2 + 5 + y*width*3*2] = x+width + y*width;\r
283 \r
284                                 }\r
285                         }\r
286 \r
287                         bufferDesc.ByteWidth = sizeof(unsigned int)*(width*3*2+2 + height*width*3*2);\r
288                         bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;\r
289 \r
290                         InitData.pSysMem = indices;\r
291 \r
292                         hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &g_pIndexBuffer);\r
293                         hr = hr;\r
294                 }\r
295         }\r
296 };\r
297 \r
298 #endif CAP_H\r