Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
basicbuilder.cc
Go to the documentation of this file.
1 
18 #include "ion/text/basicbuilder.h"
19 
20 #include "ion/base/datacontainer.h"
21 #include "ion/base/logging.h"
23 #include "ion/gfx/attribute.h"
24 #include "ion/gfx/attributearray.h"
25 #include "ion/gfx/bufferobject.h"
27 #include "ion/gfx/texture.h"
28 #include "ion/gfx/uniform.h"
30 #include "ion/math/vector.h"
31 #include "ion/text/font.h"
32 #include "ion/text/fontimage.h"
33 #include "ion/text/layout.h"
34 
35 namespace ion {
36 namespace text {
37 
38 namespace {
39 
41 
46 
47 
48 static const char* kVertexShaderSource =
49  "uniform mat4 uProjectionMatrix;\n"
50  "uniform mat4 uModelviewMatrix;\n"
51  "attribute vec3 aVertex;\n"
52  "attribute vec2 aTexCoords;\n"
53  "varying vec2 texture_coords;\n"
54  "\n"
55  "void main(void) {\n"
56  " texture_coords = aTexCoords;\n"
57  " gl_Position = uProjectionMatrix * uModelviewMatrix * vec4(aVertex, 1);\n"
58  "}\n";
59 
60 static const char* kFragmentShaderSource =
61  "#ifdef GL_ES\n"
62  "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
63  "precision highp float;\n"
64  "#else\n"
65  "precision mediump float;\n"
66  "#endif\n"
67  "#endif\n"
68  "\n"
69  "varying vec2 texture_coords;\n"
70  "uniform sampler2D uSdfSampler;\n"
71  "uniform float uSdfPadding;\n"
72  "uniform vec4 uTextColor;\n"
73  "\n"
74  "void main(void) {\n"
75  " float dist = texture2D(uSdfSampler, texture_coords).r;\n"
76  " float s = uSdfPadding == 0. ? 0.2 : 0.2 / uSdfPadding;\n"
77  " float d = 1.0 - smoothstep(-s, s, dist - 0.5);\n"
78  " if (dist > 0.5 + s)\n"
79  " discard;\n"
80  " gl_FragColor = d * uTextColor;\n"
81  "}\n";
82 
83 } // anonymous namespace
84 
86 
91 
92 
94  const gfxutils::ShaderManagerPtr& shader_manager,
95  const base::AllocatorPtr& allocator)
96  : Builder(font_image, shader_manager, allocator) {}
97 
99 
100 bool BasicBuilder::SetSdfPadding(float padding) {
101  return GetNode().Get() && GetNode()->SetUniformByName("uSdfPadding", padding);
102 }
103 
104 bool BasicBuilder::SetTextColor(const math::VectorBase4f& color) {
105  return GetNode().Get() && GetNode()->SetUniformByName("uTextColor", color);
106 }
107 
110  reg->IncludeGlobalRegistry();
112  "uSdfPadding", gfx::kFloatUniform, "SDF padding amount"));
114  "uSdfSampler", gfx::kTextureUniform, "SDF font texture sampler"));
116  "uTextColor", gfx::kFloatVector4Uniform, "Text foreground color"));
117  return reg;
118 }
119 
120 void BasicBuilder::GetShaderStrings(std::string* id_string,
121  std::string* vertex_source,
122  std::string* fragment_source) {
123  *id_string = "Basic Text Shader";
124  *vertex_source = kVertexShaderSource;
125  *fragment_source = kFragmentShaderSource;
126 }
127 
129  gfx::Node* node) {
130  const Font* font = GetFont().Get();
131  const float sdf_padding =
132  font ? static_cast<float>(font->GetSdfPadding()) : 0.f;
134  if (node->GetUniforms().size() < 2U)
135  node->ClearUniforms();
136  if (node->GetUniforms().empty()) {
137  node->AddUniform(registry->Create<gfx::Uniform>(
138  "uSdfPadding", sdf_padding));
139  node->AddUniform(registry->Create<gfx::Uniform>(
140  "uSdfSampler", GetFontImageTexture()));
141  node->AddUniform(registry->Create<gfx::Uniform>(
142  "uTextColor", math::Point4f(1.f, 1.f, 1.f, 1.f)));
143  } else {
146  DCHECK_GE(node->GetUniforms().size(), 3U);
147  node->SetUniformValue<float>(0U, sdf_padding);
149  }
150 }
151 
153  const gfx::BufferObjectPtr& buffer_object) {
154  Vertex v;
156  .Bind(v.position, "aVertex")
157  .Bind(v.texture_coords, "aTexCoords")
159  buffer_object);
160 }
161 
163  size_t* vertex_size,
164  size_t* num_vertices) {
166  const size_t num_glyphs = layout.GetGlyphCount();
167  *num_vertices = 4 * num_glyphs;
168  *vertex_size = sizeof(Vertex);
169  base::AllocVector<char> vertex_data(
171  vertex_data.resize(sizeof(Vertex) * (*num_vertices));
172  Vertex* vertices = reinterpret_cast<Vertex*>(&vertex_data[0]);
173  math::Point3f positions[4];
174  math::Point2f texture_coords[4];
175  for (size_t i = 0; i < num_glyphs; ++i) {
176  StoreGlyphVertices(layout, i, positions, texture_coords);
177  for (int j = 0; j < 4; ++j)
178  vertices[4 * i + j] = Vertex(positions[j], texture_coords[j]);
179  }
180  return vertex_data;
181 }
182 
183 
184 } // namespace text
185 } // namespace ion
kShortTerm is used for objects that are very transient in nature, such as scratch memory used to comp...
Definition: allocator.h:36
~BasicBuilder() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
Definition: basicbuilder.cc:98
void StoreGlyphVertices(const Layout &layout, size_t glyph_index, math::Point3f positions[4], math::Point2f texture_coords[4])
Fills in the position and texture_coords for the 4 vertices of the indexed Layout glyph quad...
Definition: builder.cc:246
size_t GetSdfPadding() const
Returns the padding value used when generating SDF glyphs from the font.
Definition: font.h:84
const FontPtr GetFont() const
Returns the Font from the FontImage. This may be a NULL pointer.
Definition: builder.h:57
This struct is stored for each registered ShaderInput.
bool UpdateFontImageTextureUniform(size_t index, gfx::Node *node)
Modifies the indexed Texture uniform in the node if necessary to contain the current FontImage image...
Definition: builder.cc:232
size_t AddUniform(const Uniform &uniform)
Adds a uniform to this and returns an index that can be used to refer to the uniform.
Definition: uniformholder.h:46
static const ShaderInputRegistryPtr & GetGlobalRegistry()
Returns the ShaderInputRegistry instance representing all supported global uniforms and attributes...
void UpdateUniforms(const gfx::ShaderInputRegistryPtr &registry, gfx::Node *node) override
Adds or updates uniforms for the shaders in the node.
bool SetSdfPadding(float padding)
These convenience functions can be used to modify uniform values in the built Node returned by GetNod...
std::string text
bool SetTextColor(const math::VectorBase4f &color)
base::AllocVector< char > BuildVertexData(const Layout &layout, size_t *vertex_size, size_t *num_vertices) override
Returns a vector of data that represents vertex data, the size of a vertex, the number of vertices...
Point2f texture_coords
Definition: shapeutils.cc:74
const base::AllocatorPtr & GetAllocator()
Returns the Allocator passed to the constructor.
Definition: builder.h:97
A Uniform instance represents a uniform shader argument.
Definition: uniform.h:76
const base::AllocVector< Uniform > & GetUniforms() const
Gets the vector of uniforms.
Definition: uniformholder.h:81
void GetShaderStrings(std::string *id_string, std::string *vertex_source, std::string *fragment_source) override
Returns the strings needed for shader definition.
Scalar types.
Definition: uniform.h:39
const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Convenience function that returns the Allocator to use to allocate an object with a specific lifetime...
Definition: allocatable.h:78
void BindAttributes(const gfx::AttributeArrayPtr &attr_array, const gfx::BufferObjectPtr &buffer_object) override
Binds attributes for the Builder's shader program.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
A ShaderInputRegistry is used to manage a collection of shader inputs to a specific ShaderProgram (bo...
const gfx::NodePtr & GetNode() const
Returns the Node set up by the last successful call to Build().
Definition: builder.h:76
void ClearUniforms()
Clears the vector of uniforms in this.
Definition: uniformholder.h:78
#define DCHECK_GE(val1, val2)
Definition: logging.h:336
size_t GetGlyphCount() const
Returns the number of glyphs added to the layout.
Definition: layout.cc:31
const gfx::ShaderInputRegistryPtr GetShaderInputRegistry() override
Required Builder functions.
BufferToAttributeBinder is a simple interface to insert a set of Attributes containing BufferObjectEl...
Copyright 2016 Google Inc.
A Layout instance specifies how glyphs are arranged to form text.
Definition: layout.h:127
BufferToAttributeBinder & Bind(const FieldType &field, const std::string &attribute_name)
BasicBuilder(const FontImagePtr &font_image, const gfxutils::ShaderManagerPtr &shader_manager, const base::AllocatorPtr &allocator)
BasicBuilder functions.
Definition: basicbuilder.cc:93
bool SetUniformValue(size_t index, const T &value)
Sets the value of the uniform at an index if the index is valid.
Definition: uniformholder.h:86
const gfx::TexturePtr GetFontImageTexture()
Returns a Texture that contains the FontImage image.
Definition: builder.cc:223
Builder is an abstract base class for building graphics objects used to render text.
Definition: builder.h:45
A Node instance represents a node in a scene graph.
Definition: node.h:45
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
Font is a base class for implementation-specific representations of fonts.
Definition: font.h:43
void Apply(const gfx::ShaderInputRegistryPtr &reg, const gfx::AttributeArrayPtr &aa, const gfx::BufferObjectPtr &bo)
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...
Definition: allocvector.h:50