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