void Init() { PROFILE_SCOPED() frac = 1.0 / double(edgeLen-1); // also want vtx indices for tris not touching edge of patch indices.reset(new unsigned short[IDX_VBO_COUNT_ALL_IDX()]); unsigned short *idx = indices.get(); for (int x=0; x<edgeLen-1; x++) { for (int y=0; y<edgeLen-1; y++) { idx[0] = x + edgeLen*y; idx[1] = x+1 + edgeLen*y; idx[2] = x + edgeLen*(y+1); idx+=3; idx[0] = x+1 + edgeLen*y; idx[1] = x+1 + edgeLen*(y+1); idx[2] = x + edgeLen*(y+1); idx+=3; } } // these will hold the optimised indices std::vector<unsigned short> pl_short; // populate the N indices lists from the arrays built during InitTerrainIndices() // iterate over each index list and optimize it unsigned int tri_count = GetIndices(pl_short); VertexCacheOptimizerUShort vco; VertexCacheOptimizerUShort::Result res = vco.Optimize(&pl_short[0], tri_count); assert(0 == res); //create buffer & copy indexBuffer.Reset(Pi::renderer->CreateIndexBuffer(pl_short.size(), Graphics::BUFFER_USAGE_STATIC)); Uint16* idxPtr = indexBuffer->Map(Graphics::BUFFER_MAP_WRITE); for (Uint32 j = 0; j < pl_short.size(); j++) { idxPtr[j] = pl_short[j]; } indexBuffer->Unmap(); if (indices) { indices.reset(); } }
void DrawOutdent(const float size[2], Graphics::RenderState *state) { PROFILE_SCOPED() // locals RefCountedPtr<Graphics::VertexBuffer> vb; RefCountedPtr<Graphics::IndexBuffer> ib[3]; // see if we have this size of indent in the cache already const vector2f vsize(size[0], size[1]); MapOutdentBuffers::iterator bufIt = s_outdentBuffers.find(vsize); if (bufIt != s_outdentBuffers.end()) { // found it vb = bufIt->second.vb; ib[0] = bufIt->second.ib[0]; ib[1] = bufIt->second.ib[1]; ib[2] = bufIt->second.ib[2]; } else { // generate it const vector3f vertices[] = { /* 0 */ vector3f(0, 0, 0), /* 1 */ vector3f(0, size[1], 0), /* 2 */ vector3f(size[0], size[1], 0), /* 3 */ vector3f(size[0], 0, 0), /* 4 */ vector3f(BORDER_WIDTH, BORDER_WIDTH, 0), /* 5 */ vector3f(BORDER_WIDTH, size[1] - BORDER_WIDTH, 0), /* 6 */ vector3f(size[0] - BORDER_WIDTH, size[1] - BORDER_WIDTH, 0), /* 7 */ vector3f(size[0] - BORDER_WIDTH, BORDER_WIDTH, 0) }; const Uint32 indices[] = { 0, 1, 5, 0, 5, 4, 0, 4, 7, 0, 7, 3, 3, 7, 6, 3, 6, 2, 1, 2, 6, 1, 6, 5, 4, 5, 6, 4, 6, 7 }; // create buffer Graphics::VertexBufferDesc vbd; vbd.attrib[0].semantic = Graphics::ATTRIB_POSITION; vbd.attrib[0].format = Graphics::ATTRIB_FORMAT_FLOAT3; vbd.numVertices = 8; vbd.usage = Graphics::BUFFER_USAGE_STATIC; // Upload data vb.Reset(Screen::GetRenderer()->CreateVertexBuffer(vbd)); TPos* vtxPtr = vb->Map<TPos>(Graphics::BUFFER_MAP_WRITE); assert(vb->GetDesc().stride == sizeof(TPos)); for (Uint32 i = 0; i < 8; i++) { vtxPtr[i].pos = vertices[i]; } vb->Unmap(); // indices Uint32 IndexStart = 0; Uint32 IndexEnd = 12; Uint32 NumIndices = 12; ib[0].Reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); IndexStart += NumIndices; NumIndices = 12; IndexEnd += NumIndices; ib[1].Reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); IndexStart += NumIndices; NumIndices = 6; IndexEnd += NumIndices; ib[2].Reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); TOutdentBuffers tib; tib.vb = vb; tib.ib[0] = ib[0]; tib.ib[1] = ib[1]; tib.ib[2] = ib[2]; s_outdentBuffers[vsize] = tib; } // Draw it! Screen::flatColorMaterial->diffuse = Color(153,153,153,255); Screen::GetRenderer()->DrawBufferIndexed(vb.Get(), ib[0].Get(), state, Screen::flatColorMaterial); Screen::flatColorMaterial->diffuse = Colors::bgShadow; Screen::GetRenderer()->DrawBufferIndexed(vb.Get(), ib[1].Get(), state, Screen::flatColorMaterial); Screen::flatColorMaterial->diffuse = Colors::bg; Screen::GetRenderer()->DrawBufferIndexed(vb.Get(), ib[2].Get(), state, Screen::flatColorMaterial); }