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
35 btCollisionShape *collisionShape;
\r
36 btCollisionObject *collisionObject;
\r
38 void set_collision_object(btCollisionObject* co)
\r
40 collisionObject = co;
\r
43 void set_collision_shape(btCollisionShape* cs)
\r
45 collisionShape = cs;
\r
48 void create_texture(void)
\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
55 HRESULT hr = D3DX11CreateShaderResourceViewFromFile(g_pd3dDevice, L"texture.bmp", &loadInfo, NULL, &texture2D_view, NULL);
\r
61 SAFE_RELEASE( g_pIndexBuffer );
\r
62 SAFE_RELEASE( pVB[0] );
\r
63 SAFE_RELEASE( texture2D );
\r
64 SAFE_RELEASE( texture2D_view );
\r
71 if (!collisionObject)
\r
75 ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
\r
77 D3DXMATRIX mWorldViewProjection;
\r
78 D3DXVECTOR3 vLightDir;
\r
83 // Get the projection & view matrix from the camera class
\r
84 mProj = *g_Camera.GetProjMatrix();
\r
85 mView = *g_Camera.GetViewMatrix();
\r
87 // Get the light direction
\r
88 vLightDir = g_LightControl.GetLightDirection();
\r
90 // Per frame cb update
\r
91 D3D11_MAPPED_SUBRESOURCE MappedResource;
\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
101 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerFrameBind, 1, &g_pcbPSPerFrame );
\r
104 ///////////////////////////////////////Modify below//////////////////////////////////////////////////////
\r
108 pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 );
\r
110 //This is where we pass the vertex buffer to DX
\r
111 pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
\r
113 //This is where we pass the index buffer to DX
\r
114 pd3dImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
\r
116 /////////////////////////////////////////////////////////////////////////////////////////////////////////
\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
122 // Set the per object constant data
\r
127 btTransform trans = collisionObject->getWorldTransform();
\r
131 btVector3 origin = trans.getOrigin();
\r
132 btMatrix3x3 btM = trans.getBasis();
\r
134 btScalar* scalar_matrix = new btScalar[16];;
\r
135 trans.getOpenGLMatrix(scalar_matrix);
\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
142 D3DXMATRIXA16 m_scale;
\r
144 D3DXMatrixScaling(&m_scale,sc,sc,sc);
\r
147 D3DXVECTOR3 vCenter( global_shift_x, global_shift_y, global_shift_z);
\r
149 D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x+x_offset, -vCenter.y+y_offset, -vCenter.z+z_offset );
\r
152 D3DXMATRIXA16 m_trans_transpose;
\r
153 D3DXMatrixTranspose(&m_trans_transpose,&m_trans);
\r
155 mWorld = *g_Camera.GetWorldMatrix() ;
\r
156 mProj = *g_Camera.GetProjMatrix();
\r
157 mView = m_trans * *g_Camera.GetViewMatrix();
\r
159 mWorldViewProjection = mView * mProj;
\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
169 pd3dImmediateContext->VSSetConstantBuffers( g_iCBVSPerObjectBind, 1, &g_pcbVSPerObject );
\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
177 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerObjectBind, 1, &g_pcbPSPerObject );
\r
180 SDKMESH_SUBSET* pSubset = NULL;
\r
181 D3D11_PRIMITIVE_TOPOLOGY PrimType;
\r
183 pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );
\r
187 pSubset = g_Mesh11.GetSubset( 0, 0 );
\r
189 pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
\r
191 pd3dImmediateContext->PSSetShaderResources(0,1,&texture2D_view);
\r
193 pd3dImmediateContext->DrawIndexed( (width*3*2+2 + height*width*3*2), 0, ( UINT )pSubset->VertexStart );
\r
196 SAFE_RELEASE(pd3dImmediateContext);
\r
200 void create_buffers(int width_, int height_, bool top_)
\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
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
226 for(int y = 0; y < height; y++)
\r
228 for(int x = 0; x < width; x++)
\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
241 for(int y = 0; y < height; y++)
\r
243 for(int x = 0; x < width; x++)
\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
257 D3D11_SUBRESOURCE_DATA InitData;
\r
258 InitData.pSysMem = vertices;
\r
259 InitData.SysMemPitch = 0;
\r
260 InitData.SysMemSlicePitch = 0;
\r
262 HRESULT hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &pVB[0]);
\r
266 //What is this vertex stride thing all about?
\r
267 Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 );
\r
270 unsigned int* indices = new unsigned int[width*3*2+2 + height*width*3*2];
\r
272 for(int y = 0; y < height-1; y++)
\r
274 for(int x = 0; x < width-1; x++)
\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
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
287 bufferDesc.ByteWidth = sizeof(unsigned int)*(width*3*2+2 + height*width*3*2);
\r
288 bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
\r
290 InitData.pSysMem = indices;
\r
292 hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &g_pIndexBuffer);
\r