mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.8 KiB
105 lines
4.8 KiB
|
13 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
|
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
|
copy of this software and associated documentation files (the "Software"),
|
||
|
|
to deal in the Software without restriction, including without limitation
|
||
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included
|
||
|
|
in all copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
|
DEALINGS IN THE SOFTWARE.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "DistanceFieldGlyphCache.h"
|
||
|
|
|
||
|
13 years ago
|
#ifndef CORRADE_NO_ASSERT
|
||
|
13 years ago
|
#include "ColorFormat.h"
|
||
|
13 years ago
|
#endif
|
||
|
13 years ago
|
#include "Context.h"
|
||
|
13 years ago
|
#include "Extensions.h"
|
||
|
13 years ago
|
#include "ImageReference.h"
|
||
|
13 years ago
|
#include "TextureFormat.h"
|
||
|
13 years ago
|
#include "TextureTools/DistanceField.h"
|
||
|
|
|
||
|
|
namespace Magnum { namespace Text {
|
||
|
|
|
||
|
13 years ago
|
DistanceFieldGlyphCache::DistanceFieldGlyphCache(const Vector2i& originalSize, const Vector2i& size, const UnsignedInt radius):
|
||
|
|
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3)
|
||
|
|
GlyphCache(TextureFormat::R8, originalSize, size, Vector2i(radius)),
|
||
|
|
#else
|
||
|
13 years ago
|
/* Luminance is not renderable in most cases */
|
||
|
13 years ago
|
GlyphCache(Context::current()->isExtensionSupported<Extensions::GL::EXT::texture_rg>() ?
|
||
|
|
TextureFormat::Red : TextureFormat::RGB, originalSize, size, Vector2i(radius)),
|
||
|
|
#endif
|
||
|
13 years ago
|
scale(Vector2(size)/Vector2(originalSize)), radius(radius)
|
||
|
13 years ago
|
{
|
||
|
13 years ago
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
|
MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg);
|
||
|
|
#endif
|
||
|
|
|
||
|
13 years ago
|
#ifdef MAGNUM_TARGET_GLES2
|
||
|
13 years ago
|
/* Luminance is not renderable in most cases */
|
||
|
13 years ago
|
if(!Context::current()->isExtensionSupported<Extensions::GL::EXT::texture_rg>())
|
||
|
13 years ago
|
Warning() << "Text::DistanceFieldGlyphCache:" << Extensions::GL::EXT::texture_rg::string() << "not supported, using inefficient RGB format for glyph cache texture";
|
||
|
13 years ago
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
void DistanceFieldGlyphCache::setImage(const Vector2i& offset, const ImageReference2D& image) {
|
||
|
13 years ago
|
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3)
|
||
|
|
const TextureFormat internalFormat = TextureFormat::R8;
|
||
|
13 years ago
|
CORRADE_ASSERT(image.format() == ColorFormat::Red,
|
||
|
|
"Text::DistanceFieldGlyphCache::setImage(): expected" << ColorFormat::Red << "but got" << image.format(), );
|
||
|
13 years ago
|
#else
|
||
|
|
TextureFormat internalFormat;
|
||
|
|
if(Context::current()->isExtensionSupported<Extensions::GL::EXT::texture_rg>()) {
|
||
|
|
internalFormat = TextureFormat::Red;
|
||
|
13 years ago
|
CORRADE_ASSERT(image.format() == ColorFormat::Red,
|
||
|
|
"Text::DistanceFieldGlyphCache::setImage(): expected" << ColorFormat::Red << "but got" << image.format(), );
|
||
|
13 years ago
|
} else {
|
||
|
|
internalFormat = TextureFormat::Luminance;
|
||
|
13 years ago
|
CORRADE_ASSERT(image.format() == ColorFormat::Luminance,
|
||
|
|
"Text::DistanceFieldGlyphCache::setImage(): expected" << ColorFormat::Luminance << "but got" << image.format(), );
|
||
|
13 years ago
|
}
|
||
|
|
#endif
|
||
|
13 years ago
|
|
||
|
|
Texture2D input;
|
||
|
13 years ago
|
input.setWrapping(Sampler::Wrapping::ClampToEdge)
|
||
|
13 years ago
|
.setMinificationFilter(Sampler::Filter::Linear)
|
||
|
|
.setMagnificationFilter(Sampler::Filter::Linear)
|
||
|
|
.setImage(0, internalFormat, image);
|
||
|
13 years ago
|
|
||
|
|
/* Create distance field from input texture */
|
||
|
13 years ago
|
TextureTools::distanceField(input, texture(), Range2Di::fromSize(offset*scale, image.size()*scale), radius, image.size());
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
void DistanceFieldGlyphCache::setDistanceFieldImage(const Vector2i& offset, const ImageReference2D& image) {
|
||
|
13 years ago
|
#if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3)
|
||
|
13 years ago
|
CORRADE_ASSERT(image.format() == ColorFormat::Red,
|
||
|
|
"Text::DistanceFieldGlyphCache::setDistanceFieldImage(): expected" << ColorFormat::Red << "but got" << image.format(), );
|
||
|
13 years ago
|
#else
|
||
|
|
if(Context::current()->isExtensionSupported<Extensions::GL::EXT::texture_rg>())
|
||
|
13 years ago
|
CORRADE_ASSERT(image.format() == ColorFormat::Red,
|
||
|
|
"Text::DistanceFieldGlyphCache::setDistanceFieldImage(): expected" << ColorFormat::Red << "but got" << image.format(), );
|
||
|
13 years ago
|
|
||
|
|
/* Luminance is not renderable in most cases */
|
||
|
13 years ago
|
else CORRADE_ASSERT(image.format() == ColorFormat::RGB,
|
||
|
|
"Text::DistanceFieldGlyphCache::setDistanceFieldImage(): expected" << ColorFormat::RGB << "but got" << image.format(), );
|
||
|
13 years ago
|
#endif
|
||
|
|
|
||
|
13 years ago
|
texture().setSubImage(0, offset, image);
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
|
}}
|