Exemplo n.º 1
0
void RenderManager::DrawTextPrivate()
{
	if(enqueuedTextData.size() == 0)
		return;

	gRenderAPI->BeginPerfEvent("RenderText");

	for(int fnt=0; fnt<FONT_MaxSupported; fnt++)
	{
		// Calculate number of quads to render
		int numQuads = 0;
		for(vector<TextRenderData>::const_iterator it=enqueuedTextData.begin(); it != enqueuedTextData.end(); it++)
		{
			if(it->useFont == fnt)
				numQuads += it->text.length();
		}

		if(numQuads == 0)
			continue;

		// Create vertex buffer
		VertexBuffer* textVB = gRenderAPI->CreateVertexBuffer(VFMT_P3C3U2, numQuads*4, NULL, true);
		VertexContainer_P3C3U2* pData = (VertexContainer_P3C3U2*)textVB->Lock();

		float fontHeightNDC, fontWidthNDC;
		for(vector<TextRenderData>::const_iterator it=enqueuedTextData.begin(); it != enqueuedTextData.end(); it++)
		{
			const TextRenderData& data = *it;

			if(data.useFont == fnt)
			{
				// Convert font height from pixel units to NDC units
				fontHeightNDC = 2*data.size/fullScreenBufferSizeY;

				// The current position of the render cursor
				float renderCursorX = data.posX;

				// Font used
				BitmapFont* font = engineFonts[data.useFont];

				// Font color
				ColorVector color = data.color;

				// Text to render
				const string& renderText = data.text;

				for(unsigned int i=0; i<renderText.length(); i++)
				{
					BitmapFont::CharDescriptor charData = font->GetData(renderText.at(i), data.bBold);

					fontWidthNDC = charData.aspectRatio * fontHeightNDC; 

					pData->px = renderCursorX;
					pData->py = data.posY;
					pData->pz = 0.f;
					pData->cx = color.r;
					pData->cy = color.g;
					pData->cz = color.b;
					pData->u = charData.u1;
					pData->v = charData.v1;

					pData++;

					pData->px = renderCursorX + fontWidthNDC;
					pData->py = data.posY;
					pData->pz = 0.f;
					pData->cx = color.r;
					pData->cy = color.g;
					pData->cz = color.b;
					pData->u = charData.u2;
					pData->v = charData.v1;

					pData++;

					pData->px = renderCursorX + fontWidthNDC;
					pData->py = data.posY - fontHeightNDC;
					pData->pz = 0.f;
					pData->cx = color.r;
					pData->cy = color.g;
					pData->cz = color.b;
					pData->u = charData.u2;
					pData->v = charData.v2;

					pData++;

					pData->px = renderCursorX;
					pData->py = data.posY - fontHeightNDC;
					pData->pz = 0.f;
					pData->cx = color.r;
					pData->cy = color.g;
					pData->cz = color.b;
					pData->u = charData.u1;
					pData->v = charData.v2;

					pData++;

					renderCursorX += fontWidthNDC;
				}
			}
		}

		textVB->Unlock();

		// Create index buffer
		IndexBuffer* textIB = gRenderAPI->CreateIndexBuffer(IFMT_16Bit, numQuads*6, NULL, true);
		short* iData = (short*)textIB->Lock();

		int offset = 0;
		for(unsigned int i=0; i<enqueuedTextData.size(); i++)
		{
			if(enqueuedTextData.at(i).useFont == fnt)
			{
				for(unsigned int j=0; j<enqueuedTextData.at(i).text.length(); j++)
				{
					*iData = offset + 4*j + 0; iData++;
					*iData = offset + 4*j + 1; iData++;
					*iData = offset + 4*j + 3; iData++;

					*iData = offset + 4*j + 2; iData++;
					*iData = offset + 4*j + 3; iData++;
					*iData = offset + 4*j + 1; iData++;
				}

				offset += 4*enqueuedTextData.at(i).text.length();
			}
		}

		textIB->Unlock();

		// Vertex shader
		VertexShader* vShader = gRenderManager.GetPassthroughShader(VFMT_P3C3U2, INTERPOLANT_MeshUV | INTERPOLANT_MeshColor);
		vShader->Bind();

		// Pixel shader
		simpleFontPixelShader->SetValues(engineFonts[fnt]->GetTexture(), SamplerState::TrilinearWrap);
		simpleFontPixelShader->Bind();

		gRenderAPI->SetVertexBuffer(0, textVB);
		gRenderAPI->SetIndexBuffer(textIB);
		gRenderAPI->DrawIndexed(TOPOLOGY_TriangleList, numQuads*2);

		// Clean up the buffers
		gRenderAPI->DestroyVertexBuffer(textVB);
		gRenderAPI->DestroyIndexBuffer(textIB);
	}

	gRenderAPI->EndPerfEvent();
}