/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> /// <param name="totalWidth"></param> /// <param name="totalHeight"></param> unsafe private void PositionPass(string text, GlyphServer server, out float totalWidth, out float totalHeight) { int textureWidth = server.TextureWidth; int textureHeight = server.TextureHeight; VertexBuffer buffer = this.positionBuffer; var positionArray = (QuadStruct *)buffer.MapBuffer(MapBufferAccess.ReadWrite); const float height = 2.0f; // let's say height is 2.0f. totalWidth = 0; totalHeight = height; int index = 0; foreach (var c in text) { GlyphInfo glyphInfo; float wByH = 0; if (server.GetGlyphInfo(c, out glyphInfo)) { float w = (glyphInfo.quad.rightBottom.x - glyphInfo.quad.leftBottom.x) * textureWidth; float h = (glyphInfo.quad.rightBottom.y - glyphInfo.quad.rightTop.y) * textureHeight; wByH = height * w / h; } else { // put an empty glyph(square) here. wByH = height * 1.0f / 1.0f; } var leftTop = new vec2(totalWidth, 1f); var leftBottom = new vec2(totalWidth, -1f); var rightBottom = new vec2(totalWidth + wByH, -1f); var rightTop = new vec2(totalWidth + wByH, 1f); positionArray[index++] = new QuadStruct(leftTop, leftBottom, rightBottom, rightTop); totalWidth += wByH; } // move to center. const float scale = 1f; for (int i = 0; i < text.Length; i++) { QuadStruct quad = positionArray[i]; var newPos = new QuadStruct( // y is already in [-1, 1], so just shrink x to [-1, 1] new vec2(quad.leftTop.x / totalWidth * 2.0f - 1f, quad.leftTop.y) * scale, new vec2(quad.leftBottom.x / totalWidth * 2.0f - 1f, quad.leftBottom.y) * scale, new vec2(quad.rightBottom.x / totalWidth * 2.0f - 1f, quad.rightBottom.y) * scale, new vec2(quad.rightTop.x / totalWidth * 2.0f - 1f, quad.rightTop.y) * scale ); positionArray[i] = newPos; } buffer.UnmapBuffer(); }
/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> /// <param name="totalWidth"></param> /// <param name="totalHeight"></param> unsafe private void PositionPass(string text, GlyphServer server, out float totalWidth, out float totalHeight) { int textureWidth = server.TextureWidth; int textureHeight = server.TextureHeight; VertexBuffer buffer = this.positionBuffer; var positionArray = (QuadPositionStruct *)buffer.MapBuffer(MapBufferAccess.ReadWrite); const float height = 2; // 2 is the height value in clip space. totalWidth = 0; totalHeight = height; int index = 0; foreach (var c in text) { if (index >= this.textModel.Capacity) { break; } GlyphInfo glyphInfo; float wByH = 0; if (server.GetGlyphInfo(c, out glyphInfo)) { float w = (glyphInfo.quad.rightBottom.x - glyphInfo.quad.leftBottom.x) * textureWidth; float h = (glyphInfo.quad.rightBottom.y - glyphInfo.quad.rightTop.y) * textureHeight; wByH = height * w / h; } else { // put an empty glyph(square) here. wByH = height * 1.0f / 1.0f; } var leftTop = new vec2(totalWidth, height); var leftBottom = new vec2(totalWidth, 0); var rightBottom = new vec2(totalWidth + wByH, 0); var rightTop = new vec2(totalWidth + wByH, height); positionArray[index++] = new QuadPositionStruct(leftTop, leftBottom, rightBottom, rightTop); totalWidth += wByH; } buffer.UnmapBuffer(); }
/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> unsafe private void UVPass(string text, GlyphServer server) { VertexBuffer buffer = this.uvBuffer; var uvArray = (QuadStruct *)buffer.MapBuffer(MapBufferAccess.WriteOnly); int index = 0; foreach (var c in text) { GlyphInfo glyphInfo; if (server.GetGlyphInfo(c, out glyphInfo)) { uvArray[index] = glyphInfo.quad; } index++; } buffer.UnmapBuffer(); }
/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> unsafe private void TextureIndexPass(string text, GlyphServer server) { VertexBuffer buffer = this.textureIndexBuffer; var textureIndexArray = (float *)buffer.MapBuffer(MapBufferAccess.WriteOnly); int index = 0; foreach (var c in text) { GlyphInfo glyphInfo; if (server.GetGlyphInfo(c, out glyphInfo)) { textureIndexArray[index] = glyphInfo.textureIndex; } index++; } buffer.UnmapBuffer(); }
/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> unsafe private void UVPass(string text, GlyphServer server) { VertexBuffer buffer = this.stringBuffer; var textureIndexArray = (QuadSTRStruct *)buffer.MapBuffer(MapBufferAccess.WriteOnly); int index = 0; foreach (var c in text) { if (index >= this.labelModel.Capacity) { break; } GlyphInfo glyphInfo; if (server.GetGlyphInfo(c, out glyphInfo)) { textureIndexArray[index] = glyphInfo.quad; } index++; } buffer.UnmapBuffer(); }