void IndexBuffer::restore() { // if dynamic is requested or if we are in reference mode for debugging shaders if(m_isDynamic || gRenderer.getDeviceReference()) { if( FAILED( pD3DDevice->CreateIndexBuffer( m_count * sizeof(RenderTri), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_dxBuffer, NULL) ) ) { m_dxBuffer = NULL; } } else { if( FAILED( pD3DDevice->CreateIndexBuffer( m_count * sizeof(RenderTri), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_dxBuffer, NULL) ) ) { m_dxBuffer = NULL; } } m_bufferLocked = false; m_dataEmpty = true; }
LptaD3DDynamicBuffer::LptaD3DDynamicBuffer( LPDIRECT3DDEVICE9 d3ddev, lpta::VERTEX_TYPE vertexType, unsigned int maxVertices, unsigned int maxIndices, lpta::LptaSkin::SKIN_ID skinId) : vertexBuffer(nullptr), indexBuffer(nullptr), vertexType(vertexType), maxVertices(maxVertices), maxIndices(maxIndices), numVertices(0), numIndices(0), skinId(skinId) { try { { HRESULT result = d3ddev->CreateVertexBuffer(ToStride(vertexType) * maxVertices, USAGE, 0, D3DPOOL_DEFAULT, &vertexBuffer, nullptr); if (FAILED(result)) { throw std::runtime_error("failed to create vertex buffer"); } } { HRESULT result = d3ddev->CreateIndexBuffer(sizeof(DWORD) * maxIndices, USAGE, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, nullptr); if (FAILED(result)) { throw std::runtime_error("failed to create index buffer"); } } } catch (std::runtime_error) { if (nullptr != vertexBuffer) { vertexBuffer->Release(); } if (nullptr != indexBuffer) { indexBuffer->Release(); } throw D3DBufferAllocationFailure(); } }
LPDIRECT3DINDEXBUFFER9 CreateIndexBuffer( LPDIRECT3DDEVICE9 device, OvByte* buffer, OvSize bufsize ) { if ( device && buffer && bufsize ) { LPDIRECT3DINDEXBUFFER9 streamBuffer = NULL; HRESULT hr = device->CreateIndexBuffer ( bufsize , 0 , D3DFMT_INDEX16 , D3DPOOL_MANAGED , &streamBuffer , NULL ); if ( SUCCEEDED(hr) && streamBuffer ) { void* copyDest = NULL; if ( SUCCEEDED(streamBuffer->Lock( 0, bufsize, ©Dest, 0)) && copyDest) { memcpy( copyDest, buffer, bufsize ); streamBuffer->Unlock(); return streamBuffer; } } } return NULL; }
//----[ createIndexBuffer ]-------------------------------------------------- bool EvidyonMapRenderer::createIndexBuffer(LPDIRECT3DDEVICE9 d3d_device) { if (FAILED(d3d_device->CreateIndexBuffer( sizeof(MapIndex) * 3 * TRIANGLES_PER_QUAD * MAX_VISIBLE_QUADS, D3DUSAGE_WRITEONLY, D3DFMT_EVIDYON_MAPINDEX, D3DPOOL_DEFAULT, &ib_, NULL))) return false; MapIndex* indices; if (FAILED(ib_->Lock(0, 0, (LPVOID*)&indices, 0))) return false; // 2 0 1 // 2 1 4 // 2 4 3 // 2 3 0 MapIndex base_index = 0; for (int quad = 0; quad < MAX_VISIBLE_QUADS; ++quad, base_index += VERTICES_PER_QUAD) { const MapIndex center_index = base_index + 2; const MapIndex offsets[] = { base_index + 1, base_index + 4, base_index + 3 }; *(indices++) = center_index; *(indices++) = base_index; *(indices++) = offsets[0]; *(indices++) = center_index; *(indices++) = offsets[0]; *(indices++) = offsets[1]; *(indices++) = center_index; *(indices++) = offsets[1]; *(indices++) = offsets[2]; *(indices++) = center_index; *(indices++) = offsets[2]; *(indices++) = base_index; } ib_->Unlock(); return true; }
//頂点バッファをインデックスバッファを生成 void Vertices::CreateBuffer(LPDIRECT3DDEVICE9 pD3DDevice, DWORD Usage, D3DPOOL Pool) { Release(); //頂点バッファとインデックスバッファを解放 //頂点バッファを作成 if (vectorD3DVERTEX.size()) { pD3DDevice->CreateVertexBuffer(vectorD3DVERTEX.size() * sizeof(D3DVERTEX), Usage, D3DVERTEX::FVF, Pool, &pVertexBuffer, NULL); if (pVertexBuffer) //頂点バッファの作成に成功した場合 { LPVOID pBuffer; //バッファの内容を示すポインタ pVertexBuffer->Lock(0, 0, &pBuffer, 0); //バッファをロック memcpy(pBuffer, &vectorD3DVERTEX.at(0), vectorD3DVERTEX.size() * sizeof(D3DVERTEX)); //頂点配列の内容をバッファへコピー pVertexBuffer->Unlock(); //バッファのロック解除 } } //インデックスバッファを作成 if (vectorIndex.size()) { pD3DDevice->CreateIndexBuffer(vectorIndex.size() * sizeof(WORD), Usage, D3DFMT_INDEX16, Pool, &pIndexBuffer, NULL); if (pIndexBuffer) //インデックスバッファの作成に成功した場合 { LPVOID pBuffer; //バッファの内容を示すポインタ pIndexBuffer->Lock(0, 0, &pBuffer, 0); //バッファをロック memcpy(pBuffer, &vectorIndex.at(0), vectorIndex.size() * sizeof(WORD)); //インデックス配列の内容をバッファへコピー pIndexBuffer->Unlock(); //バッファのロック解除 } } return; }
//--------------------------------------------------------------------- D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer(HardwareIndexBuffer::IndexType idxType, size_t numIndexes, HardwareBuffer::Usage usage, LPDIRECT3DDEVICE9 pDev, bool useSystemMemory, bool useShadowBuffer) : HardwareIndexBuffer(idxType, numIndexes, usage, useSystemMemory, useShadowBuffer) { #if OGRE_D3D_MANAGE_BUFFERS mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : // If not system mem, use managed pool UNLESS buffer is discardable // if discardable, keeping the software backing is expensive (usage & HardwareBuffer::HBU_DISCARDABLE)? D3DPOOL_DEFAULT : D3DPOOL_MANAGED; #else mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT; #endif // Create the Index buffer HRESULT hr = pDev->CreateIndexBuffer( static_cast<UINT>(mSizeInBytes), D3D9Mappings::get(mUsage), D3D9Mappings::get(mIndexType), mD3DPool, &mlpD3DBuffer, NULL ); if (FAILED(hr)) { String msg = DXGetErrorDescription9(hr); OGRE_EXCEPT(hr, "Cannot create D3D9 Index buffer: " + msg, "D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer"); } }
//--------------------------------------------------------------------- bool D3D9HardwareIndexBuffer::recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev) { if (mD3DPool == D3DPOOL_DEFAULT) { // Create the Index buffer HRESULT hr = pDev->CreateIndexBuffer( static_cast<UINT>(mSizeInBytes), D3D9Mappings::get(mUsage), D3D9Mappings::get(mIndexType), mD3DPool, &mlpD3DBuffer, NULL ); if (FAILED(hr)) { String msg = DXGetErrorDescription9(hr); OGRE_EXCEPT(hr, "Cannot create D3D9 Index buffer: " + msg, "D3D9HardwareIndexBuffer::D3D9HardwareIndexBuffer"); } return true; } return false; }
bool IndexBuffer::create(LPDIRECT3DDEVICE9 _pD3DDevice, int _iIndices, bool _bDynamic) { m_iIndices = _iIndices; m_bDynamic = _bDynamic; HRESULT hResult = _pD3DDevice->CreateIndexBuffer(m_iIndices * sizeof(short), D3DUSAGE_WRITEONLY | (_bDynamic ? D3DUSAGE_DYNAMIC : 0), D3DFMT_INDEX16, _bDynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &m_pIndexBuffer, NULL); if (FAILED(hResult)) { Log::instance()->logError("CreateIndexBuffer error (%08X)", hResult); return false; } void* pIndices; hResult = m_pIndexBuffer->Lock(0, m_iIndices * sizeof(short), &pIndices, (true == m_bDynamic) ? D3DLOCK_DISCARD : 0); if (FAILED(hResult)) { Log::instance()->logError("Lock error (%08X)", hResult); return false; } memset(pIndices, 0, m_iIndices * sizeof(short)); m_pIndexBuffer->Unlock(); return true; }
/**----------------------------------------------------------------------------- * 인덱스 버퍼 초기화 *------------------------------------------------------------------------------ */ HRESULT InitIB() { if( FAILED( g_pd3dDevice->CreateIndexBuffer( (g_cxHeight-1)*(g_czHeight-1)*2 * sizeof(MYINDEX), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL ) ) ) { return E_FAIL; } MYINDEX i; MYINDEX* pI; if( FAILED( g_pIB->Lock( 0, (g_cxHeight-1)*(g_czHeight-1)*2 * sizeof(MYINDEX), (void**)&pI, 0 ) ) ) return E_FAIL; for ( DWORD z = 0; z < g_czHeight - 1; z++ ) { for ( DWORD x = 0; x < g_cxHeight - 1; x++ ) { i._0 = static_cast<WORD>( z*g_cxHeight + x ); i._1 = static_cast<WORD>( z*g_cxHeight + x + 1 ); i._2 = static_cast<WORD>( ( z + 1 )*g_cxHeight + x ); *pI++ = i; i._0 = static_cast<WORD>( ( z + 1 )*g_cxHeight + x ); i._1 = static_cast<WORD>( z*g_cxHeight + x + 1 ); i._2 = static_cast<WORD>( ( z + 1 )*g_cxHeight + x + 1 ); *pI++ = i; } } g_pIB->Unlock(); return S_OK; }
// Creates a DirectX specific sub-mesh from an imported sub-mesh bool CMesh::CreateSubMeshDX ( const SSubMesh& subMesh, SSubMeshDX* subMeshDX ) { // Copy node and material subMeshDX->node = subMesh.node; subMeshDX->material = subMesh.material; // Create the vertex buffer unsigned int bufferSize = subMesh.numVertices * subMesh.vertexSize; if (FAILED(g_pd3dDevice->CreateVertexBuffer( bufferSize, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &subMeshDX->vertexBuffer, NULL ))) { return false; } subMeshDX->numVertices = subMesh.numVertices; subMeshDX->vertexSize = subMesh.vertexSize; // "Lock" the vertex buffer so we can write to it void* bufferData; if (FAILED(subMeshDX->vertexBuffer->Lock( 0, bufferSize, (void**)&bufferData, 0 ))) { return false; } // Copy the vertex data memcpy( bufferData, subMesh.vertices, bufferSize ); // Unlock the vertex buffer again so it can be used for rendering subMeshDX->vertexBuffer->Unlock(); // Create the index buffer - assuming 16-bit (WORD) index data bufferSize = subMesh.numFaces * 3 * sizeof(WORD); if (FAILED(g_pd3dDevice->CreateIndexBuffer( bufferSize, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &subMeshDX->indexBuffer, NULL ))) { return false; } subMeshDX->numIndices = subMesh.numFaces * 3; // "Lock" the index buffer so we can write to it if (FAILED(subMeshDX->indexBuffer->Lock( 0, bufferSize, (void**)&bufferData, 0 ))) { return false; } // Copy the index data memcpy( bufferData, subMesh.faces, bufferSize ); // Unlock the index buffer again so it can be used for rendering subMeshDX->indexBuffer->Unlock(); return true; }
IndexBufferImpl::IndexBufferImpl(uint length, uint fmt, const void* data, LPDIRECT3DDEVICE9 device) { D3DFORMAT table[] = {D3DFMT_INDEX16, D3DFMT_INDEX32}; assert(("Unknown format", fmt <= 1)); if (FAILED(device->CreateIndexBuffer(length, 0, table[fmt], D3DPOOL_DEFAULT, &ib, 0))) throw Exception("Failed to allocate VertexBuffer"); if (data != nullptr) upload(data, 0, length); }
HRESULT HookIDirect3DDevice9::CreateIndexBuffer(LPVOID _this, UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DIndexBuffer9** ppIndexBuffer, HANDLE* pSharedHandle) { LOG_API(); return pD3Dev->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, pSharedHandle); }
/**----------------------------------------------------------------------------- * 인덱스 버퍼 초기화 *------------------------------------------------------------------------------ */ HRESULT InitIB() { if( FAILED( g_pd3dDevice->CreateIndexBuffer( (g_cxHeight-1)*(g_czHeight-1)*2 * sizeof(MYINDEX), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL ) ) ) { return E_FAIL; } g_nTriangles = 0; return S_OK; }
/************************************************************************ * Objects such as vertex buffer, index buffer, fonts are initialized here ************************************************************************/ bool ObjectInit(HWND hwnd) { PlaySound(_T("阿兰 - 浴火重生.wav"), nullptr, SND_ASYNC | SND_FILENAME | SND_LOOP); srand(unsigned(time(NULL))); D3DXCreateFont(gPD3DDevice, 40, 0, 0, 0, 0, 0, 0, 0, 0, _T("楷体"), &gPFont); ////////////////////////////////////////////////////////////////////////// // Fill in the vertex struct ////////////////////////////////////////////////////////////////////////// CUSTOM_VERTEX vertices[17]; vertices[0].x = 400.0f; vertices[0].y = 300.0f; vertices[0].z = 0.0f; vertices[0].rhw = 1.0f; vertices[0].color = D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256); for (int i = 1; i < 17; i++) { vertices[i].x = gRadius*cos(PI*(i - 1)/ 8) + vertices[0].x; vertices[i].y = gRadius*sin(PI*(i - 1)/ 8) + vertices[0].y; vertices[i].z = 0.0f; vertices[i].rhw = 1.0f; vertices[i].color = D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256); } gPD3DDevice->CreateVertexBuffer(17 * sizeof(CUSTOM_VERTEX), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &gPVertexBuffer, nullptr); void* pVertices{0}; gPVertexBuffer->Lock(0, 17 * sizeof(CUSTOM_VERTEX), (void**)&pVertices, 0); memcpy(pVertices, vertices, 17 * sizeof(CUSTOM_VERTEX)); gPVertexBuffer->Unlock(); ////////////////////////////////////////////////////////////////////////// // Fill in the index struct ////////////////////////////////////////////////////////////////////////// int indexes[48]; for (int i = 0; i < 16; i++) { indexes[i * 3] = 0; indexes[i * 3 + 1] = i + 1; indexes[i * 3 + 2] = i + 2; } indexes[47] = 1; gPD3DDevice->CreateIndexBuffer(48 * sizeof(int), 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &gPIndexBuffer, nullptr); int* pIndexes{0}; gPIndexBuffer->Lock(0, 0, (void**)&pIndexes, 0); memcpy(pIndexes, indexes, 48 * sizeof(int)); gPIndexBuffer->Unlock(); return true; }
void cSubDivSurf::Draw( matrix4& mat ) { LPDIRECT3DDEVICE9 lpDevice = Graphics()->GetDevice(); Graphics()->SetWorldMatrix( mat ); HRESULT hr; // The index buffer LPDIRECT3DINDEXBUFFER9 pIndexBuffer = 0; // Create the index buffer lpDevice->CreateIndexBuffer( m_nTris * 3 * sizeof( WORD ), // Size in bytes of buffer D3DUSAGE_WRITEONLY, // Will only be writing to the buffer D3DFMT_INDEX16, // 16 bit indices D3DPOOL_DEFAULT, // Default memory pooling &pIndexBuffer, // Address of the buffer pointer NULL ); // Reserved. set to NULL // Pointer to the index buffer data WORD* pData = 0; // Lock the index buffer pIndexBuffer->Lock( 0, 0, (void**)&pData, 0 ); // Copy the index data into the index buffer CopyMemory( pData, m_d3dTriList, m_nTris * 3 * sizeof( WORD ) ); // Unlock the index buffer pIndexBuffer->Unlock(); // Tell Direct3D to use the index buffer lpDevice->SetIndices( pIndexBuffer ); // Attach the vertex buffer to rendering stream 0 lpDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof( sVertex ) ); // Draw the primitive hr = lpDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, m_nVerts, 0, m_nTris ); if( FAILED( hr ) ) { DP0("[cSubDivSurf::Draw]: DrawIndexedPrimitive failed!\n"); } pIndexBuffer->Release(); }
// this is the function that puts the 3D models into video RAM void init_graphics(void) { // create a vertex buffer interface called v_buffer d3ddev->CreateVertexBuffer(g_Sim.numPoints()*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL); d3ddev->CreateVertexBuffer(5*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_MANAGED, &v_bordbuffer, NULL); d3ddev->CreateIndexBuffer(g_Sim.numConstraints() * 2 * sizeof(short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &i_buffer, NULL); HRESULT hr = D3DXCreateFont(d3ddev, 17, 0, FW_NORMAL, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Courier New"), &font ); CUSTOMVERTEX* verts; v_bordbuffer->Lock(0, 0, (void**)&verts, 0); verts[0].X = MINX; verts[0].Y = MINY; verts[1].X = MINX; verts[1].Y = MAXY; verts[2].X = MAXX; verts[2].Y = MAXY; verts[3].X = MAXX; verts[3].Y = MINY; verts[4].X = MINX; verts[4].Y = MINY; verts[0].Z = verts[1].Z = verts[2].Z = verts[3].Z = verts[4].Z = 0.0f; verts[0].COLOR = verts[1].COLOR = verts[2].COLOR = verts[3].COLOR = verts[4].COLOR = D3DCOLOR_XRGB(127, 127, 127); v_bordbuffer->Unlock(); short* indices; i_buffer->Lock(0, 0, (void**)&indices, 0); for (unsigned i = 0; i < g_Sim.numConstraints(); i++) { const std::pair<unsigned, unsigned>& p = g_Sim.constraint(i); indices[2 * i] = p.first; indices[2 * i + 1] = p.second; } i_buffer->Unlock(); }
D3DIndexBuffer::D3DIndexBuffer(EIndexType type,uint numIndicies,EUsageType usage,bool useSystemMem, LPDIRECT3DDEVICE9 lpDev,uint id,bool useVirtualBuffer): IHardwareIndexBuffer(type,numIndicies,usage,id,useVirtualBuffer),m_lpDev(lpDev) { m_pool=useSystemMem ? D3DPOOL_SYSTEMMEM : ((usage & EUT_Discardable ) ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED); HRESULT hr=lpDev->CreateIndexBuffer(m_sizeInBytes, D3DEnums::Map(usage),D3DEnums::Map(type),m_pool,&m_d3dBuff,0); if(FAILED(hr)) { gVideoLoggerSystem.log(mT("D3DIndexBuffer() - Faield to create index buffer"),ELL_WARNING); } }
BOOL FSCreateDynamicIndexBuffer(RENDEROBJECT *renderObject, int numIndices) { LastError = lpD3DDevice->CreateIndexBuffer( numIndices * 3 * sizeof(WORD), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, (LPDIRECT3DINDEXBUFFER9*)&renderObject->lpIndexBuffer, NULL ); if (FAILED(LastError)) { DebugPrintf("can't create vertex buffer\n"); return FALSE; } return TRUE; }
//=============================================== //頂点情報のコンバート //=============================================== //[input] // pD3DX9:Direct3Dデバイス //[return] // HREULT値 //=============================================== bool CXMesh::ConvertVertex(LPDIRECT3DDEVICE9 pD3DX9) { LPD3DXBUFFER pD3DXMtrlBuffer = NULL; /*Vertex Bufferにコピーする*/ D3DVERTEX* pSrc; D3DVERTEX* pDest; LPDIRECT3DINDEXBUFFER9 pSrcIndex; WORD* pISrc; WORD* pIDest; /*VertexBuffer情報取得*/ LPDIRECT3DVERTEXBUFFER9 pVB; MeshData.pMesh->GetVertexBuffer(&pVB); D3DVERTEXBUFFER_DESC Desc; pVB->GetDesc( &Desc ); DWORD nMeshVertices = MeshData.pMesh->GetNumVertices(); DWORD nMeshFaces = MeshData.pMesh->GetNumFaces(); /*頂点バッファを作成*/ pD3DX9->CreateVertexBuffer( Desc.Size, 0, MeshData.pMesh->GetFVF(), D3DPOOL_MANAGED, &m_pMeshVB, NULL ); /*インデックスバッファを作成*/ pD3DX9->CreateIndexBuffer( nMeshFaces * 3 * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pMeshIndex, NULL ); /*頂点バッファをコピー*/ pVB->Lock(0,0,(void**)&pSrc,0); m_pMeshVB->Lock(0,0,(void**)&pDest,0); CopyMemory( pDest, pSrc, Desc.Size ); pVB->Unlock(); pVB->Release(); m_pMeshVB->Unlock(); /*インデックスのコピー*/ MeshData.pMesh->GetIndexBuffer( &pSrcIndex ); pSrcIndex->Lock( 0, 0, (void**)&pISrc, 0 ); m_pMeshIndex->Lock( 0, 0, (void**)&pIDest, 0 ); CopyMemory( pIDest, pISrc, nMeshFaces * 3 * sizeof( WORD ) ); pSrcIndex->Unlock(); m_pMeshIndex->Unlock(); pSrcIndex->Release(); return true; }
void LoadMesh(const PMDLoader::DATA *_data) { LPDIRECT3DDEVICE9 device = DirectX9::Instance().Device; HRESULT hr; // 頂点バッファの作成 m_MaxVertex = _data->vert_count.u; hr = device->CreateVertexBuffer( sizeof(Vertex3D::VERTEX) * _data->vert_count.u, D3DUSAGE_WRITEONLY, // 頂点バッファの使用方法 Vertex3D::FVF, // 使用する頂点フォーマット D3DPOOL_MANAGED, // バッファを保持するメモリクエストを指定 &m_VertexBuffer, // 頂点のバッファの先頭アドレス NULL ); Vertex3D::VERTEX *vtx; hr = m_VertexBuffer->Lock(0,0,(void**)&vtx,0); for( unsigned int i = 0; i < _data->vert_count.u ; i++) { vtx[i].pos.x = _data->vertex[i].pos[0].f; vtx[i].pos.y = _data->vertex[i].pos[1].f; vtx[i].pos.z = -_data->vertex[i].pos[2].f; vtx[i].nor.x = _data->vertex[i].normal_vec[0].f; vtx[i].nor.y = _data->vertex[i].normal_vec[1].f; vtx[i].nor.z = -_data->vertex[i].normal_vec[2].f; vtx[i].tex.x = _data->vertex[i].uv[0].f; vtx[i].tex.y = _data->vertex[i].uv[1].f; } hr = m_VertexBuffer->Unlock(); // インデックスバッファの作成 m_MaxIndex = _data->face_vert_count.u; hr = device->CreateIndexBuffer( sizeof( WORD ) * m_MaxIndex, // バッファサイズ D3DUSAGE_WRITEONLY, // 頂点バッファの使用方法 D3DFMT_INDEX16, // 使用する頂点フォーマット D3DPOOL_MANAGED, // バッファを保持するメモリクエストを指定 &m_IndexBuffer, // 頂点インデックスのバッファの先頭アドレス NULL ); WORD *idx; hr = m_IndexBuffer->Lock(0,0,(void**)&idx,0); for( unsigned int i=0; i < _data->face_vert_count.u ; i++) { idx[i] = _data->face_vert_index[i].u; } hr = m_IndexBuffer->Unlock(); }
//--------------------------------------------------------------------- bool CD3D9HardwareIndexBuffer::recreateIfDefaultPool() { LPDIRECT3DDEVICE9 pDev = GetD3D9RenderSystem().GetD3D9Device(); if (mD3DPool == D3DPOOL_DEFAULT) { // Create the Index buffer return D3D9HR( pDev->CreateIndexBuffer( static_cast<UINT>(m_uBufferSize), UsageForD3D9(m_Usage), (D3DFORMAT)IndexTypeForD3D9(m_IndexType), mD3DPool, &mlpD3DBuffer, NULL) ); } return false; }
//----------------------------------------------------------------------------- // Name: create // Desc: Initializes the buffers for this layer //----------------------------------------------------------------------------- bool GUILayer::create(LPDIRECT3DDEVICE9 d3dDevice, size_t vertexCapacity, size_t triangleCapacity) { // Validate parameters if (!d3dDevice || !vertexCapacity || !triangleCapacity) return false; // Make sure the class is clean destroy(); // Create a vertex buffer if (FAILED(d3dDevice->CreateVertexBuffer( sizeof(GUIVertex) * vertexCapacity, D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, GUIVERTEX_D3DBUFFERFORMAT, D3DPOOL_DEFAULT, &myVB, 0))) { return false; } // Create an index buffer if (FAILED(d3dDevice->CreateIndexBuffer( sizeof(GUIIndex) * triangleCapacity * 3, D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, GUIINDEX_D3DBUFFERFORMAT, D3DPOOL_DEFAULT, &myIB, 0))) { // Clear out the VB myVB->Release(); // Failed return false; } // Save the values internally myVertexCapacity = vertexCapacity; myTriangleCapacity = triangleCapacity; // Store the device pointer internally (d3d_device_ = d3dDevice)->AddRef(); // Set up the default viewport setFullscreenViewport(0.0f, 1.0f); // Success return true; }
void ModelMesh::PreRender( LPDIRECT3DDEVICE9 device ) { if( !IsLoaded() ) return; // 顶点缓存 if( FAILED( device->CreateVertexBuffer( m_vertex_size * sizeof( ModelVertexStruct ), D3DUSAGE_SOFTWAREPROCESSING, ModelVertexStruct::FVF, D3DPOOL_DEFAULT, &m_vb, NULL ) ) ) return; ModelVertexStruct* verticies = NULL; m_vb->Lock( 0, 0, (void **) &verticies, 0 ); memcpy( verticies, m_vertex_arr, m_vertex_size * sizeof( ModelVertexStruct ) ); m_vb->Unlock(); // 索引缓存 if( FAILED( device->CreateIndexBuffer( m_index_size * sizeof(WORD), D3DUSAGE_SOFTWAREPROCESSING, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_ib, NULL ) ) ) return; WORD* indices = NULL; m_ib->Lock( 0, 0, (void **) &indices, 0 ); memcpy( indices, m_index_arr, m_index_size * sizeof(WORD) ); m_ib->Unlock(); return; }
///Create Vertex Buffer HRESULT initVB(){ CUSTOMVERTEX vertices[9]; vertices[0].x = 300; vertices[0].y = 250; vertices[0].z = 0.5f; vertices[0].rhw = 1.0f; vertices[0].color = 0xffff0000; for(int i=0; i<8; ++i){ vertices[i+1].x = (float)(200*sin(i*3.14159/4.0)) + 300; vertices[i+1].y = -(float)(200*cos(i*3.14159/4.0)) + 250; vertices[i+1].z = 0.5f; vertices[i+1].rhw = 1.0f; vertices[i+1].color = 0xff00ff00; } WORD indices[] = { 0,1,2, 0,2,3, 0,3,4, 0,4,5, 0,5,6, 0,6,7, 0,7,8, 0,8,1 }; //Vertex Buffer if(FAILED(g_pDevice->CreateVertexBuffer(9 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL))) return E_FAIL; VOID* pVertices; if(FAILED(g_pVB->Lock(0, sizeof(vertices), (void**)&pVertices, 0))) return E_FAIL; memcpy(pVertices, vertices, sizeof(vertices)); g_pVB->Unlock(); ///Index Buffer if(FAILED(g_pDevice->CreateIndexBuffer(24* sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL))) return E_FAIL; VOID* pIndices; if(FAILED(g_pIB->Lock(0, sizeof(indices), (void**)&pIndices, 0))) return E_FAIL; memcpy(pIndices, indices, sizeof(indices)); g_pIB->Unlock(); return S_OK; }
HRESULT init_index_buffer() { CUSTOMINDEX indices[] = { { 0, 1, 2 }, { 0, 2, 3 }, /// 윗면 { 4, 6, 5 }, { 4, 7, 6 }, /// 아랫면 { 0, 3, 7 }, { 0, 7, 4 }, /// 왼면 { 1, 5, 6 }, { 1, 6, 2 }, /// 오른면 { 3, 2, 6 }, { 3, 6, 7 }, /// 앞면 { 0, 4, 5 }, { 0, 5, 1 } /// 뒷면 }; if (FAILED( g_pd3dDevice->CreateIndexBuffer( 12 * sizeof(CUSTOMINDEX), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIndexBuff, NULL ))) return E_FAIL; VOID* pIndices; if (FAILED( g_pIndexBuff->Lock( 0, sizeof(indices), (void**)&pIndices, 0 ))) return E_FAIL; memcpy( pIndices, indices, sizeof(indices) ); g_pIndexBuff->Unlock(); return S_OK; }
HRESULT CTile::CreateIB(LPDIRECT3DDEVICE9 _device) { if( FAILED(_device->CreateIndexBuffer( sizeof(WORD) * 6, 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIB, 0 ) ) ) { MessageBox(NULL, L"CTile::CreateIB Failed!!", NULL, MB_OK); return E_FAIL; } WORD* indices = 0; if( FAILED( m_pIB->Lock(0, sizeof(WORD)*6, (void**)&indices, 0 ) ) ){ MessageBox(NULL, L"CTile::IB Lock Failed!!", NULL, MB_OK); return E_FAIL; } indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; m_pIB->Unlock(); return S_OK; }
int CBgDisp2::Create3DBuffers( LPDIRECT3DDEVICE9 pd3dDevice ) { HRESULT hr; DWORD elemsize, fvf; if( multitexok == 0 ){ elemsize = sizeof( D3DTLVERTEX ); fvf = D3DFVF_TLVERTEX; }else{ elemsize = sizeof( D3DTLVERTEX2 ); fvf = D3DFVF_TLVERTEX2; } hr = pd3dDevice->CreateVertexBuffer( BGTLVNUM * elemsize, D3DUSAGE_WRITEONLY, fvf, D3DPOOL_MANAGED, &m_VB, NULL ); if( FAILED(hr) ){ DbgOut( "bgdisp2 : Create3DBuffers : CreateVertexBuffer error !!!\n" ); _ASSERT( 0 ); return 1; } hr = pd3dDevice->CreateIndexBuffer( BGINDEXNUM * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_IB, NULL ); if( FAILED(hr) ){ DbgOut( "bgdisp2 : Create3DBuffers : CreateIndexBuffer error !!!\n" ); _ASSERT( 0 ); return 1; } return 0; }
bool PrimitiveTriangleListID3D::createOptimizedVB( LPDIRECT3DDEVICE9 d3dd, int triangleCount, const word * pTriangles ) { ASSERT( m_SystemVB ); m_Triangles = triangleCount; // get the system vertex buffer info D3DVERTEXBUFFER_DESC svbd; m_SystemVB->GetDesc( &svbd ); // calculate the number of verticies in this buffer int vertexCount = svbd.Size / sizeof(Vertex ); // lock the system vertex buffer Vertex * pVerticies = NULL; if ( m_SystemVB->Lock( 0, svbd.Size, (void **)&pVerticies, 0 ) != D3D_OK ) return false; // initialize the remap array Array< int > remap( vertexCount ); for(int i=0;i<remap.size();i++) remap[i] = -1; // initialize the culled verts array Array< Vertex > culled; //culled.setGrowSize( vertexCount ); // create the index buffer, cull and sort the veritices while initializing the index buffer dword vbi = m_Triangles * 3; dword vbiSize = vbi * sizeof(word); if ( d3dd->CreateIndexBuffer( vbiSize, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_VBI, NULL ) != D3D_OK ) return false; byte * pBuffer = NULL; if ( m_VBI->Lock( 0, vbiSize, (void **)&pBuffer, 0 ) != D3D_OK ) return false; for(dword j=0;j<vbi;j++) { word v = pTriangles[j]; if ( remap[v] < 0 ) { remap[v] = culled.size(); culled.push( pVerticies[v] ); } ((word *)pBuffer)[j] = remap[v]; } m_VBI->Unlock(); // create the optmized vertex buffer m_Verts = culled.size(); dword vbSize = m_Verts * sizeof(Vertex); if ( d3dd->CreateVertexBuffer( vbSize, D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1, D3DPOOL_MANAGED, &m_VB, NULL ) != D3D_OK ) return false; if ( m_VB->Lock( 0, vbSize, (void **)&pBuffer, 0 ) != D3D_OK ) return false; for(int i=0;i<culled.size();i++) ((Vertex *)pBuffer)[i] = culled[i]; m_VB->Unlock(); m_SystemVB->Unlock(); return true; }
HRESULT CreateCube() { // The vertex order in each face is in the following order when you // look at the face perpendicular against it's normal // bottom left -> bottom right -> top right -> top left // So you need to set the cull mode as D3DCULL_CW, since the default mode is D3DCULL_CCW Vertex vertices[] = { // Front face { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f }, // 0 { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f }, // 1 { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f }, // 2 { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f }, // 3 // Back face { 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, // Left face { 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, // Right face { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, // Top face { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, // Bottom face { 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f }, { 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f }, } ; // Index buffer WORD indices[] = { // Front side 0, 1, 2, 0, 2, 3, // Back side 4, 5, 6, 4, 6, 7, // Top side 8, 9, 10, 8, 10, 11, // Bottom side 12, 13, 14, 12, 14, 15, // Left side 16, 17, 18, 16, 18, 19, // Right side 20, 21, 22, 20, 22, 23, } ; // Create vertex buffer if( FAILED( g_pd3dDevice->CreateVertexBuffer( sizeof(vertices) * sizeof(Vertex), D3DUSAGE_WRITEONLY, VERTEX_FVF, D3DPOOL_MANAGED, &g_pVB, NULL ) ) ) { return E_FAIL; } // Copy vertex data VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices, sizeof(vertices) ); g_pVB->Unlock(); // Create index buffer if( FAILED( g_pd3dDevice->CreateIndexBuffer( sizeof(indices) * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_pIB, 0) ) ) { return E_FAIL ; } // Copy index data VOID *pIndices; if( FAILED( g_pIB->Lock( 0, sizeof(indices), (void **)&pIndices, 0) ) ) return E_FAIL; memcpy(pIndices, indices, sizeof(indices) ); g_pIB->Unlock() ; }
bool ReadModelFile( const string &fileName, LPDIRECT3DVERTEXBUFFER9 &vtxBuff, int &vtxSize, LPDIRECT3DINDEXBUFFER9 &idxBuff, int &faceSize ) { using namespace std; ifstream fin(fileName.c_str()); if (!fin.is_open()) return false; string vtx, vtx_eq; int numVertices; fin >> vtx >> vtx_eq >> numVertices; if (numVertices <= 0) return false; vtxSize = numVertices; // 버텍스 버퍼 생성. if (FAILED(g_pDevice->CreateVertexBuffer( numVertices * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &vtxBuff, NULL))) { return false; } // 버텍스 버퍼 초기화. Vertex* vertices; if (FAILED(vtxBuff->Lock( 0, sizeof(Vertex), (void**)&vertices, 0))) return false; float num1, num2, num3; for (int i = 0; i < numVertices; i++) { fin >> num1 >> num2 >> num3; vertices[i] = Vertex(num1, num2, num3); } vtxBuff->Unlock(); string idx, idx_eq; int numIndices; fin >> idx >> idx_eq >> numIndices; if (numIndices <= 0) return false; faceSize = numIndices; if (FAILED(g_pDevice->CreateIndexBuffer(numIndices*3*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &idxBuff, NULL))) { return false; } WORD *indices = NULL; idxBuff->Lock(0, 0, (void**)&indices, 0); int num4, num5, num6; for (int i = 0; i < numIndices*3; i+=3) { fin >> num4 >> num5 >> num6; indices[ i] = num4; indices[ i+1] = num5; indices[ i+2] = num6; } idxBuff->Unlock(); ComputeNormals(vtxBuff, vtxSize, idxBuff, faceSize); return true; }