2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2010 Advanced Micro Devices
\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
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
23 ID3D11Buffer* g_pIndexBuffer;
\r
24 ID3D11Buffer* pVB[1];
\r
28 double x_offset, y_offset, z_offset;
\r
33 ID3D11Texture2D *texture2D;
\r
34 ID3D11ShaderResourceView *texture2D_view;
\r
36 btCollisionShape *collisionShape;
\r
38 //static_cast<btCapsuleShape*>(capsuleShape)
\r
41 btCollisionObject *collisionObject;
\r
43 void set_collision_object(btCollisionObject* co)
\r
45 collisionObject = co;
\r
48 void set_collision_shape(btCollisionShape* cs)
\r
50 collisionShape = cs;
\r
56 SAFE_RELEASE( g_pIndexBuffer );
\r
57 SAFE_RELEASE( pVB[0] );
\r
59 SAFE_RELEASE( texture2D );
\r
60 SAFE_RELEASE( texture2D_view );
\r
63 void create_texture(void)
\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
70 HRESULT hr = D3DX11CreateShaderResourceViewFromFile(g_pd3dDevice, L"texture.bmp", &loadInfo, NULL, &texture2D_view, NULL);
\r
76 if (!collisionObject)
\r
79 ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
\r
81 D3DXMATRIX mWorldViewProjection;
\r
82 D3DXVECTOR3 vLightDir;
\r
87 // Get the projection & view matrix from the camera class
\r
88 mProj = *g_Camera.GetProjMatrix();
\r
89 mView = *g_Camera.GetViewMatrix();
\r
91 // Get the light direction
\r
92 vLightDir = g_LightControl.GetLightDirection();
\r
94 // Per frame cb update
\r
95 D3D11_MAPPED_SUBRESOURCE MappedResource;
\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
105 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerFrameBind, 1, &g_pcbPSPerFrame );
\r
108 ///////////////////////////////////////Modify below//////////////////////////////////////////////////////
\r
112 pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 );
\r
114 //This is where we pass the vertex buffer to DX
\r
115 pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
\r
117 //This is where we pass the index buffer to DX
\r
118 pd3dImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
\r
120 /////////////////////////////////////////////////////////////////////////////////////////////////////////
\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
126 // Set the per object constant data
\r
130 btTransform trans = collisionObject->getWorldTransform();
\r
134 btVector3 origin = trans.getOrigin();
\r
135 btMatrix3x3 btM = trans.getBasis();
\r
137 btScalar* scalar_matrix = new btScalar[16];;
\r
138 trans.getOpenGLMatrix(scalar_matrix);
\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
145 D3DXMATRIXA16 m_scale;
\r
147 D3DXMatrixScaling(&m_scale,sc,sc,sc);
\r
150 D3DXVECTOR3 vCenter( global_shift_x, global_shift_y, global_shift_z);
\r
152 D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x+x_offset, -vCenter.y+y_offset, -vCenter.z+z_offset );
\r
155 D3DXMATRIXA16 m_trans_transpose;
\r
156 D3DXMatrixTranspose(&m_trans_transpose,&m_trans);
\r
158 mWorld = *g_Camera.GetWorldMatrix() ;
\r
159 mProj = *g_Camera.GetProjMatrix();
\r
160 mView = m_trans * *g_Camera.GetViewMatrix();
\r
162 mWorldViewProjection = mView * mProj;
\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
172 pd3dImmediateContext->VSSetConstantBuffers( g_iCBVSPerObjectBind, 1, &g_pcbVSPerObject );
\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
180 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerObjectBind, 1, &g_pcbPSPerObject );
\r
183 SDKMESH_SUBSET* pSubset = NULL;
\r
184 D3D11_PRIMITIVE_TOPOLOGY PrimType;
\r
186 pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );
\r
190 pSubset = g_Mesh11.GetSubset( 0, 0 );
\r
192 pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
\r
194 pd3dImmediateContext->PSSetShaderResources(0,1,&texture2D_view);
\r
196 pd3dImmediateContext->DrawIndexed( (width*3*2+2 + height*width*3*2), 0, ( UINT )pSubset->VertexStart );
\r
199 SAFE_RELEASE(pd3dImmediateContext);
\r
202 void create_buffers(int width_, int height_)
\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
215 vertex_struct *vertices = new vertex_struct[width*height];
\r
217 btCapsuleShape* cs = static_cast<btCapsuleShape*>(collisionShape);
\r
220 float radius = cs->getRadius();
\r
221 float halfHeight = cs->getHalfHeight();
\r
223 for(int y = 0; y < height; y++)
\r
225 for(int x = 0; x < width; x++)
\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
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
241 for(int y = 0; y < height; y++)
\r
243 for(int x = 0; x < width; x++)
\r
245 double coord = sin(x/5.0)*50;
\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
255 D3D11_SUBRESOURCE_DATA InitData;
\r
256 InitData.pSysMem = vertices;
\r
257 InitData.SysMemPitch = 0;
\r
258 InitData.SysMemSlicePitch = 0;
\r
260 HRESULT hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &pVB[0]);
\r
263 //What is this vertex stride thing all about?
\r
264 Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 );
\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
270 for(int y = 0; y < height-1; y++)
\r
272 for(int x = 0; x < width-1; x++)
\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
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
284 bufferDesc.ByteWidth = sizeof(unsigned int)*(width*3*2+2 + height*width*3*2);
\r
285 bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
\r
287 InitData.pSysMem = indices;
\r
289 hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &g_pIndexBuffer);
\r