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 class piece_of_cloth
\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
46 m_vertexBufferDescriptor = NULL;
\r
51 ID3D11Buffer* g_pIndexBuffer;
\r
52 ID3D11Buffer* pVB[1];
\r
53 ID3D11Buffer* pVB_staging;
\r
57 ID3D11UnorderedAccessView* g_pVB_UAV;
\r
61 double x_offset, y_offset, z_offset;
\r
64 ID3D11ShaderResourceView *texture2D_view;
\r
69 btVertexBufferDescriptor *m_vertexBufferDescriptor;
\r
71 void create_texture(std::wstring filename)
\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
78 HRESULT hr = D3DX11CreateShaderResourceViewFromFile(g_pd3dDevice, filename.c_str(), &loadInfo, NULL, &texture2D_view, NULL);
\r
87 ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext();
\r
89 D3DXMATRIX mWorldViewProjection;
\r
90 D3DXVECTOR3 vLightDir;
\r
95 // Get the projection & view matrix from the camera class
\r
96 mProj = *g_Camera.GetProjMatrix();
\r
97 mView = *g_Camera.GetViewMatrix();
\r
99 // Get the light direction
\r
100 vLightDir = g_LightControl.GetLightDirection();
\r
102 // Per frame cb update
\r
103 D3D11_MAPPED_SUBRESOURCE MappedResource;
\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
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
119 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerFrameBind, 1, &g_pcbPSPerFrame );
\r
122 ///////////////////////////////////////Modify below//////////////////////////////////////////////////////
\r
126 pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 );
\r
128 //This is where we pass the vertex buffer to DX
\r
129 pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
\r
131 //This is where we pass the index buffer to DX
\r
132 pd3dImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
\r
134 /////////////////////////////////////////////////////////////////////////////////////////////////////////
\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
140 // Set the per object constant data
\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 mWorld = *g_Camera.GetWorldMatrix();
\r
153 mProj = *g_Camera.GetProjMatrix();
\r
154 mView = *g_Camera.GetViewMatrix();
\r
156 mWorldViewProjection = mView * mProj;
\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
166 pd3dImmediateContext->VSSetConstantBuffers( g_iCBVSPerObjectBind, 1, &g_pcbVSPerObject );
\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
174 pd3dImmediateContext->PSSetConstantBuffers( g_iCBPSPerObjectBind, 1, &g_pcbPSPerObject );
\r
177 SDKMESH_SUBSET* pSubset = NULL;
\r
178 D3D11_PRIMITIVE_TOPOLOGY PrimType;
\r
181 pd3dImmediateContext->RSSetState(g_pRasterizerStateWF);
\r
183 pd3dImmediateContext->RSSetState(g_pRasterizerState);
\r
185 pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear );
\r
189 pSubset = g_Mesh11.GetSubset( 0, 0 );
\r
191 pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
\r
193 pd3dImmediateContext->PSSetShaderResources(0,1,&texture2D_view);
\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
199 SAFE_RELEASE(pd3dImmediateContext);
\r
202 void create_buffers(int width_, int height_)
\r
210 cpu_buffer = new float[width*height*8];
\r
211 memset(cpu_buffer,0,width*height*8*4);
\r
213 // Set texture coordinates in output buffers
\r
214 for(int y = 0; y < height; y++)
\r
216 for(int x = 0; x < width; x++)
\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
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
234 vertex_struct *vertices = new vertex_struct[width*height];
\r
235 for(int y = 0; y < height; y++)
\r
237 for(int x = 0; x < width; x++)
\r
239 double coord = sin(x/5.0)*50;
\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
248 D3D11_SUBRESOURCE_DATA InitData;
\r
249 InitData.pSysMem = vertices;
\r
250 InitData.SysMemPitch = 0;
\r
251 InitData.SysMemSlicePitch = 0;
\r
253 HRESULT hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &pVB[0]);
\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
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
265 //What is this vertex stride thing all about?
\r
266 Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 );
\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
273 for(int y = 0; y < height-1; y++)
\r
275 for(int x = 0; x < width-1; x++)
\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
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
291 bufferDesc.ByteWidth = sizeof(unsigned int)*((height-1)*(width-1)*3*2);
\r
292 bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
\r
294 InitData.pSysMem = indices;
\r
296 hr = g_pd3dDevice->CreateBuffer(&bufferDesc, &InitData, &g_pIndexBuffer);
\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
303 hr = g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &pVB_staging);
\r
306 delete [] vertices;
\r