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"
56 " texture_coords = aTexCoords;\n"
57 " gl_Position = uProjectionMatrix * uModelviewMatrix * vec4(aVertex, 1);\n"
60 static const char* kFragmentShaderSource =
62 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
63 "precision highp float;\n"
65 "precision mediump float;\n"
69 "varying vec2 texture_coords;\n"
70 "uniform sampler2D uSdfSampler;\n"
71 "uniform float uSdfPadding;\n"
72 "uniform vec4 uTextColor;\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"
80 " gl_FragColor = d * uTextColor;\n"
96 :
Builder(font_image, shader_manager, allocator) {}
110 reg->IncludeGlobalRegistry();
121 std::string* vertex_source,
122 std::string* fragment_source) {
123 *id_string =
"Basic Text Shader";
124 *vertex_source = kVertexShaderSource;
125 *fragment_source = kFragmentShaderSource;
131 const float sdf_padding =
138 "uSdfPadding", sdf_padding));
142 "uTextColor", math::Point4f(1.f, 1.f, 1.f, 1.f)));
156 .Bind(v.position,
"aVertex")
157 .
Bind(v.texture_coords,
"aTexCoords")
164 size_t* num_vertices) {
167 *num_vertices = 4 * num_glyphs;
168 *vertex_size =
sizeof(Vertex);
171 vertex_data.resize(
sizeof(Vertex) * (*num_vertices));
172 Vertex* vertices =
reinterpret_cast<Vertex*
>(&vertex_data[0]);
173 math::Point3f positions[4];
175 for (
size_t i = 0; i < num_glyphs; ++i) {
177 for (
int j = 0; j < 4; ++j)
178 vertices[4 * i + j] = Vertex(positions[j], texture_coords[j]);
kShortTerm is used for objects that are very transient in nature, such as scratch memory used to comp...
~BasicBuilder() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
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...
size_t GetSdfPadding() const
Returns the padding value used when generating SDF glyphs from the font.
const FontPtr GetFont() const
Returns the Font from the FontImage. This may be a NULL pointer.
bool UpdateFontImageTextureUniform(size_t index, gfx::Node *node)
Modifies the indexed Texture uniform in the node if necessary to contain the current FontImage image...
void UpdateUniforms(const gfx::ShaderInputRegistryPtr ®istry, 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...
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...
const base::AllocatorPtr & GetAllocator()
Returns the Allocator passed to the constructor.
void GetShaderStrings(std::string *id_string, std::string *vertex_source, std::string *fragment_source) override
Returns the strings needed for shader definition.
const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Convenience function that returns the Allocator to use to allocate an object with a specific lifetime...
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.
const gfx::NodePtr & GetNode() const
Returns the Node set up by the last successful call to Build().
#define DCHECK_GE(val1, val2)
size_t GetGlyphCount() const
Returns the number of glyphs added to the layout.
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.
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.
const gfx::TexturePtr GetFontImageTexture()
Returns a Texture that contains the FontImage image.
Builder is an abstract base class for building graphics objects used to render text.
A Node instance represents a node in a scene graph.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Font is a base class for implementation-specific representations of fonts.
void Apply(const gfx::ShaderInputRegistryPtr ®, 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...