fontatlas.h 5.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

#ifndef FONTATLAS_H__
#define FONTATLAS_H__

28
#include "cgt/matrix.h"
29
30
31
32
#include "cgt/shadermanager.h"
#include "cgt/texture.h"
#include "cgt/vector.h"

33
34
#include "modules/modulesapi.h"

35
36
#include <string>

37
// forward declarations
38
39
40
41
42
43
44
45
46
struct FT_LibraryRec_;
typedef struct FT_LibraryRec_  *FT_Library;
struct FT_FaceRec_;
typedef struct FT_FaceRec_*  FT_Face;

namespace campvis {
namespace fontrendering {

    /**
47
48
49
50
51
52
53
54
55
56
57
58
     * A font atlas providing to render text with OpenGL.
     * The FontAtlas manages a set of glyphs for each printable character of a font with given size
     * at construction. You can then use renderText() to render any text with that font into the
     * current OpenGL framebuffer.
     * 
     * ATTENTION:   Throws an exception when it cannot initialiize the font atlas (e.g. due to 
     *              missing font file).
     * 
     * Code inspired by http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_02
     * 
     * \note    Current implementation only supports the ASCII-256 character set.
     * \throws  std::exception on initialization error
59
     */
60
    class CAMPVIS_MODULES_API FontAtlas {
61
    public:
62
63
64
65
66
67
        /**
         * Creates a new FontAtlas of the given font and size.
         * \note    Needs a valid OpenGL context.
         * \param   fontFileName    Filename of the font to use for rendering.
         * \param   height          Font height in pixels. This is in viewport coordinates, i.e. \b after applying the optional transformation matrix in renderText()!
         */
68
69
        FontAtlas(const std::string& fontFileName, int height);

70
71
72
        /**
         * Destructor of the FontAtlas, needs a valid OpenGL context.
         */
73
74
75
        ~FontAtlas();


76
77
        /**
         * Renders the given text with the given parameters into the current framebuffer.
78
79
80
81
         * \param   text                    The text to render (currently only ASCII-256 support).
         * \param   position                Position where to start rendering the text (in normalized [-1, 1] coordinates).
         * \param   color                   Color of the text to render.
         * \param   transformationMatrix    Additional transformation matrix that is applied to each rendered vertex.
82
83
         */
        void renderText(const std::string& text, const cgt::vec2& position, const cgt::vec4& color, const cgt::mat4& transformationMatrix = cgt::mat4::identity);
84

Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
85
86
87
88
89
90
91
92
93
94
95
96

        /**
         * Renders the given text with the given parameters into the current framebuffer.
         * \param   text                    The text to render (currently only ASCII-256 support).
         * \param   position                Position where to start rendering the text (in normalized [-1, 1] coordinates).
         * \param   color                   Color of the text to render.
         * \param   pickingInformation      Color to render into the picking info texture (second FBO color attachment).
         * \param   transformationMatrix    Additional transformation matrix that is applied to each rendered vertex.
         */
        void renderTextWithPickingInformation(const std::string& text, const cgt::vec2& position, const cgt::vec4& color, const cgt::vec4& pickingInfo, const cgt::mat4& transformationMatrix = cgt::mat4::identity);


97
    private:
98
99
100
101
        // disable copying and assignment
        explicit FontAtlas(const FontAtlas& rhs);
        FontAtlas& operator=(FontAtlas rhs);

102
103
104
105
106
107
        /// Struct storing glyph parameters of each printable ASCII character
        struct {
            cgt::vec2 advance;          ///< Advance of this glyph
            cgt::vec2 bitmapSize;       ///< Bitmap size of this glyph
            cgt::vec2 bitmapOffset;     ///< Bitmap offset of this glyph
            cgt::vec2 offset;           ///< Offset of this glyph in texture coordinates
108
        } _glyphs[256];
109

110
111
112
113
        FT_Library _ftLibrary;          ///< Pointer to the freetype2 library handle
        FT_Face _ftFace;                ///< Pointer to the font face
        cgt::Texture* _texture;         ///< OpenGL texture storing the texture atlas
        cgt::Shader* _shader;           ///< Shader used for font rendering
114
        int _height;
115
116
117
118
119
120
121
122
123

        static const int MAX_TEXTURE_WIDTH;
        static const std::string loggerCat_;
    };

}
}

#endif // FONTATLAS_H__