DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit 1c28e97556dd66a2937dde059f8642d9fbc2c004
parent 586b9f9ea29a7e3136d9ca467338f3c46b8fe799
Author: falkTX <falktx@gmail.com>
Date:   Sat, 26 Apr 2014 02:49:22 +0100

More text tests

Diffstat:
Mdgl/Makefile | 10++++++++--
Adgl/src/freetype-gl/font-manager.c | 270+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/font-manager.h | 205+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/markup.h | 191+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/mat4.c | 263+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/mat4.h | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/shader.c | 124+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/shader.h | 119+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/text-buffer.c | 448+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/text-buffer.h | 285+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdgl/src/freetype-gl/texture-font.c | 4+++-
Adgl/src/freetype-gl/vertex-attribute.c | 168+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/vertex-attribute.h | 320+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/vertex-buffer.c | 662+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adgl/src/freetype-gl/vertex-buffer.h | 350+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdgl/src/freetype/FreeTypeAmalgam.h | 2+-
Aexamples/shaders/cube.frag | 36++++++++++++++++++++++++++++++++++++
Aexamples/shaders/cube.vert | 45+++++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/distance-field-2.frag | 43+++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/distance-field-2.vert | 47+++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/distance-field-3.frag | 43+++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/distance-field.frag | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/distance-field.vert | 40++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/text.frag | 152+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/text.vert | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/v3f-c4f.frag | 36++++++++++++++++++++++++++++++++++++
Aexamples/shaders/v3f-c4f.vert | 43+++++++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/v3f-t2f-c4f.frag | 38++++++++++++++++++++++++++++++++++++++
Aexamples/shaders/v3f-t2f-c4f.vert | 45+++++++++++++++++++++++++++++++++++++++++++++
Mexamples/text.cpp | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
30 files changed, 4365 insertions(+), 6 deletions(-)

diff --git a/dgl/Makefile b/dgl/Makefile @@ -27,9 +27,15 @@ OBJS = \ # freetype-gl OBJS += \ + src/freetype-gl/font-manager.c.o \ + src/freetype-gl/mat4.c.o \ + src/freetype-gl/shader.c.o \ + src/freetype-gl/text-buffer.c.o \ src/freetype-gl/texture-atlas.c.o \ src/freetype-gl/texture-font.c.o \ - src/freetype-gl/vector.c.o + src/freetype-gl/vector.c.o \ + src/freetype-gl/vertex-attribute.c.o \ + src/freetype-gl/vertex-buffer.c.o ifeq ($(MACOS),true) OBJS += src/pugl/pugl_osx_extended.m.o @@ -71,7 +77,7 @@ all: $(TARGET) # -------------------------------------------------------------- clean: - $(RM) src/*.o src/pugl/*.o ../libdgl.* + $(RM) src/*.o src/pugl/*.o src/freetype-gl/*.o ../libdgl.* debug: $(MAKE) DEBUG=true diff --git a/dgl/src/freetype-gl/font-manager.c b/dgl/src/freetype-gl/font-manager.c @@ -0,0 +1,270 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#if 0 +# if !defined(_WIN32) && !defined(_WIN64) +# include <fontconfig/fontconfig.h> +# endif +#endif +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> +#include "font-manager.h" + + +// ------------------------------------------------------------ file_exists --- +int +file_exists( const char * filename ) +{ + FILE * file = fopen( filename, "r" ); + if ( file ) + { + fclose(file); + return 1; + } + return 0; +} + + +// ------------------------------------------------------- font_manager_new --- +font_manager_t * +font_manager_new( size_t width, size_t height, size_t depth ) +{ + font_manager_t *self; + texture_atlas_t *atlas = texture_atlas_new( width, height, depth ); + self = (font_manager_t *) malloc( sizeof(font_manager_t) ); + if( !self ) + { + fprintf( stderr, + "line %d: No more memory for allocating data\n", __LINE__ ); + exit( EXIT_FAILURE ); + } + self->atlas = atlas; + self->fonts = vector_new( sizeof(texture_font_t *) ); + self->cache = wcsdup( L" " ); + return self; +} + + +// ---------------------------------------------------- font_manager_delete --- +void +font_manager_delete( font_manager_t * self ) +{ + size_t i; + texture_font_t *font; + assert( self ); + + for( i=0; i<vector_size( self->fonts ); ++i) + { + font = *(texture_font_t **) vector_get( self->fonts, i ); + texture_font_delete( font ); + } + vector_delete( self->fonts ); + texture_atlas_delete( self->atlas ); + if( self->cache ) + { + free( self->cache ); + } + free( self ); +} + + + +// ----------------------------------------------- font_manager_delete_font --- +void +font_manager_delete_font( font_manager_t * self, + texture_font_t * font) +{ + size_t i; + texture_font_t *other; + + assert( self ); + assert( font ); + + for( i=0; i<self->fonts->size;++i ) + { + other = (texture_font_t *) vector_get( self->fonts, i ); + if ( (strcmp(font->filename, other->filename) == 0) + && ( font->size == other->size) ) + { + vector_erase( self->fonts, i); + break; + } + } + texture_font_delete( font ); +} + + + +// ----------------------------------------- font_manager_get_from_filename --- +texture_font_t * +font_manager_get_from_filename( font_manager_t *self, + const char * filename, + const float size ) +{ + size_t i; + texture_font_t *font; + + assert( self ); + for( i=0; i<vector_size(self->fonts); ++i ) + { + font = * (texture_font_t **) vector_get( self->fonts, i ); + if( (strcmp(font->filename, filename) == 0) && ( font->size == size) ) + { + return font; + } + } + font = texture_font_new_from_file( self->atlas, size, filename ); + if( font ) + { + vector_push_back( self->fonts, &font ); + texture_font_load_glyphs( font, self->cache ); + return font; + } + fprintf( stderr, "Unable to load \"%s\" (size=%.1f)\n", filename, size ); + return 0; +} + + +// ----------------------------------------- font_manager_get_from_description --- +texture_font_t * +font_manager_get_from_description( font_manager_t *self, + const char * family, + const float size, + const int bold, + const int italic ) +{ + texture_font_t *font; + char *filename = 0; + + assert( self ); + + if( file_exists( family ) ) + { + filename = strdup( family ); + } + else + { +#if defined(_WIN32) || defined(_WIN64) + fprintf( stderr, "\"font_manager_get_from_description\" not implemented yet.\n" ); + return 0; +#endif + filename = font_manager_match_description( self, family, size, bold, italic ); + if( !filename ) + { + fprintf( stderr, "No \"%s (size=%.1f, bold=%d, italic=%d)\" font available.\n", + family, size, bold, italic ); + return 0; + } + } + font = font_manager_get_from_filename( self, filename, size ); + + free( filename ); + return font; +} + +// ------------------------------------------- font_manager_get_from_markup --- +texture_font_t * +font_manager_get_from_markup( font_manager_t *self, + const markup_t *markup ) +{ + assert( self ); + assert( markup ); + + return font_manager_get_from_description( self, markup->family, markup->size, + markup->bold, markup->italic ); +} + +// ----------------------------------------- font_manager_match_description --- +char * +font_manager_match_description( font_manager_t * self, + const char * family, + const float size, + const int bold, + const int italic ) +{ +// Use of fontconfig is disabled by default. +#if 1 + return 0; +#else +# if defined _WIN32 || defined _WIN64 + fprintf( stderr, "\"font_manager_match_description\" not implemented for windows.\n" ); + return 0; +# endif + char *filename = 0; + int weight = FC_WEIGHT_REGULAR; + int slant = FC_SLANT_ROMAN; + if ( bold ) + { + weight = FC_WEIGHT_BOLD; + } + if( italic ) + { + slant = FC_SLANT_ITALIC; + } + FcInit(); + FcPattern *pattern = FcPatternCreate(); + FcPatternAddDouble( pattern, FC_SIZE, size ); + FcPatternAddInteger( pattern, FC_WEIGHT, weight ); + FcPatternAddInteger( pattern, FC_SLANT, slant ); + FcPatternAddString( pattern, FC_FAMILY, (FcChar8*) family ); + FcConfigSubstitute( 0, pattern, FcMatchPattern ); + FcDefaultSubstitute( pattern ); + FcResult result; + FcPattern *match = FcFontMatch( 0, pattern, &result ); + FcPatternDestroy( pattern ); + + if ( !match ) + { + fprintf( stderr, "fontconfig error: could not match family '%s'", family ); + return 0; + } + else + { + FcValue value; + FcResult result = FcPatternGet( match, FC_FILE, 0, &value ); + if ( result ) + { + fprintf( stderr, "fontconfig error: could not match family '%s'", family ); + } + else + { + filename = strdup( (char *)(value.u.s) ); + } + } + FcPatternDestroy( match ); + return filename; +#endif +} diff --git a/dgl/src/freetype-gl/font-manager.h b/dgl/src/freetype-gl/font-manager.h @@ -0,0 +1,205 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#ifndef __FONT_MANAGER_H__ +#define __FONT_MANAGER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "vector.h" +#include "markup.h" +#include "texture-font.h" +#include "texture-atlas.h" + +/** + * @file font-manager.h + * @author Nicolas Rougier (Nicolas.Rougier@inria.fr) + * + * @defgroup font-manager Font manager + * + * Structure in charge of caching fonts. + * + * <b>Example Usage</b>: + * @code + * #include "font-manager.h" + * + * int main( int arrgc, char *argv[] ) + * { + * font_manager_t * manager = manager_new( 512, 512, 1 ); + * texture_font_t * font = font_manager_get( manager, "Mono", 12, 0, 0 ); + * + * return 0; + * } + * @endcode + * + * @{ + */ + + +/** + * Structure in charge of caching fonts. + */ +typedef struct font_manager_t { + /** + * Texture atlas to hold font glyphs. + */ + texture_atlas_t * atlas; + + /** + * Cached textures. + */ + vector_t * fonts; + + /** + * Default glyphs to be loaded when loading a new font. + */ + wchar_t * cache; + +} font_manager_t; + + + +/** + * Creates a new empty font manager. + * + * @param width width of the underlying atlas + * @param height height of the underlying atlas + * @param depth bit depth of the underlying atlas + * + * @return a new font manager. + * + */ + font_manager_t * + font_manager_new( size_t width, + size_t height, + size_t depth ); + + +/** + * Deletes a font manager. + * + * @param self a font manager. + */ + void + font_manager_delete( font_manager_t *self ); + + +/** + * Deletes a font from the font manager. + * + * Note that font glyphs are not removed from the atlas. + * + * @param self a font manager. + * @param font font to be deleted + * + */ + void + font_manager_delete_font( font_manager_t * self, + texture_font_t * font ); + + +/** + * Request for a font based on a filename. + * + * @param self a font manager. + * @param filename font filename + * @param size font size + * + * @return Requested font + */ + texture_font_t * + font_manager_get_from_filename( font_manager_t * self, + const char * filename, + const float size ); + + +/** + * Request for a font based on a description + * + * @param self a font manager + * @param family font family + * @param size font size + * @param bold whether font is bold + * @param italic whether font is italic + * + * @return Requested font + */ + texture_font_t * + font_manager_get_from_description( font_manager_t * self, + const char * family, + const float size, + const int bold, + const int italic ); + + +/** + * Request for a font based on a markup + * + * @param self a font manager + * @param markup Markup describing a font + * + * @return Requested font + */ + texture_font_t * + font_manager_get_from_markup( font_manager_t *self, + const markup_t *markup ); + + +/** + * Search for a font filename that match description. + * + * @param self a font manager + * @param family font family + * @param size font size + * @param bold whether font is bold + * @param italic whether font is italic + * + * @return Requested font filename + */ + char * + font_manager_match_description( font_manager_t * self, + const char * family, + const float size, + const int bold, + const int italic ); + +/** @} */ + +#ifdef __cplusplus +} +#endif // ifdef __cplusplus + +#endif /* __FONT_MANAGER_H__ */ + diff --git a/dgl/src/freetype-gl/markup.h b/dgl/src/freetype-gl/markup.h @@ -0,0 +1,191 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +#ifndef __MARKUP_H__ +#define __MARKUP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "texture-font.h" +#include "vec234.h" + +/** + * @file markup.h + * @author Nicolas Rougier (Nicolas.Rougier@inria.fr) + * + * @defgroup markup Markup + * + * Simple structure that describes text properties. + * + * <b>Example Usage</b>: + * @code + * #include "markup.h" + * + * ... + * + * vec4 black = {{0.0, 0.0, 0.0, 1.0}}; + * vec4 white = {{1.0, 1.0, 1.0, 1.0}}; + * vec4 none = {{1.0, 1.0, 1.0, 0.0}}; + * + * markup_t normal = { + * .family = "Droid Serif", + * .size = 24.0, + * .bold = 0, + * .italic = 0, + * .rise = 0.0, + * .spacing = 1.0, + * .gamma = 1.0, + * .foreground_color = black, .background_color = none, + * .underline = 0, .underline_color = black, + * .overline = 0, .overline_color = black, + * .strikethrough = 0, .strikethrough_color = black, + * .font = 0, + * }; + * + * ... + * + * @endcode + * + * @{ + */ + + +/** + * Simple structure that describes text properties. + */ +typedef struct markup_t +{ + /** + * A font family name such as "normal", "sans", "serif" or "monospace". + */ + char * family; + + /** + * Font size. + */ + float size; + + /** + * Whether text is bold. + */ + int bold; + + /** + * Whether text is italic. + */ + int italic; + + /** + * Vertical displacement from the baseline. + */ + float rise; + + /** + * Spacing between letters. + */ + float spacing; + + /** + * Gamma correction. + */ + float gamma; + + /** + * Text color. + */ + vec4 foreground_color; + + /** + * Background color. + */ + vec4 background_color; + + /** + * Whether outline is active. + */ + int outline; + + /** + * Outline color. + */ + vec4 outline_color; + + /** + * Whether underline is active. + */ + int underline; + + /** + * Underline color. + */ + vec4 underline_color; + + /** + * Whether overline is active. + */ + int overline; + + /** + * Overline color. + */ + vec4 overline_color; + + /** + * Whether strikethrough is active. + */ + int strikethrough; + + /** + * Strikethrough color. + */ + vec4 strikethrough_color; + + /** + * Pointer on the corresponding font (family/size/bold/italic) + */ + texture_font_t * font; + +} markup_t; + + +extern markup_t default_markup; + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* __MARKUP_H__ */ diff --git a/dgl/src/freetype-gl/mat4.c b/dgl/src/freetype-gl/mat4.c @@ -0,0 +1,263 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include "mat4.h" + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +mat4 * +mat4_new( void ) +{ + mat4 *self = (mat4 *) malloc( sizeof(mat4) ); + return self; + +} + +void +mat4_set_zero( mat4 *self ) +{ + assert( self ); + + memset( self, 0, sizeof( mat4 )); +} + +void +mat4_set_identity( mat4 *self ) +{ + assert( self ); + + memset( self, 0, sizeof( mat4 )); + self->m00 = 1.0; + self->m11 = 1.0; + self->m22 = 1.0; + self->m33 = 1.0; +} + +void +mat4_multiply( mat4 *self, mat4 *other ) +{ + mat4 m; + size_t i; + + assert( self ); + assert( other ); + + for( i=0; i<4; ++i ) + { + m.data[i*4+0] = + (self->data[i*4+0] * other->data[0*4+0]) + + (self->data[i*4+1] * other->data[1*4+0]) + + (self->data[i*4+2] * other->data[2*4+0]) + + (self->data[i*4+3] * other->data[3*4+0]) ; + + m.data[i*4+1] = + (self->data[i*4+0] * other->data[0*4+1]) + + (self->data[i*4+1] * other->data[1*4+1]) + + (self->data[i*4+2] * other->data[2*4+1]) + + (self->data[i*4+3] * other->data[3*4+1]) ; + + m.data[i*4+2] = + (self->data[i*4+0] * other->data[0*4+2]) + + (self->data[i*4+1] * other->data[1*4+2]) + + (self->data[i*4+2] * other->data[2*4+2]) + + (self->data[i*4+3] * other->data[3*4+2]) ; + + m.data[i*4+3] = + (self->data[i*4+0] * other->data[0*4+3]) + + (self->data[i*4+1] * other->data[1*4+3]) + + (self->data[i*4+2] * other->data[2*4+3]) + + (self->data[i*4+3] * other->data[3*4+3]) ; + } + memcpy( self, &m, sizeof( mat4 ) ); + +} + + +void +mat4_set_orthographic( mat4 *self, + float left, float right, + float bottom, float top, + float znear, float zfar ) +{ + assert( self ); + assert( right != left ); + assert( bottom != top ); + assert( znear != zfar ); + + mat4_set_zero( self ); + + self->m00 = +2.0/(right-left); + self->m30 = -(right+left)/(right-left); + self->m11 = +2.0/(top-bottom); + self->m31 = -(top+bottom)/(top-bottom); + self->m22 = -2.0/(zfar-znear); + self->m32 = -(zfar+znear)/(zfar-znear); + self->m33 = 1.0; +} + +void +mat4_set_perspective( mat4 *self, + float fovy, float aspect, + float znear, float zfar) +{ + float h, w; + + assert( self ); + assert( znear != zfar ); + + h = tan(fovy / 360.0 * M_PI) * znear; + w = h * aspect; + + mat4_set_frustum( self, -w, w, -h, h, znear, zfar ); +} + +void +mat4_set_frustum( mat4 *self, + float left, float right, + float bottom, float top, + float znear, float zfar ) +{ + + assert( self ); + assert( right != left ); + assert( bottom != top ); + assert( znear != zfar ); + + mat4_set_zero( self ); + + self->m00 = (2.0*znear)/(right-left); + self->m20 = (right+left)/(right-left); + + self->m11 = (2.0*znear)/(top-bottom); + self->m21 = (top+bottom)/(top-bottom); + + self->m22 = -(zfar+znear)/(zfar-znear); + self->m32 = -(2.0*zfar*znear)/(zfar-znear); + + self->m23 = -1.0; +} + +void +mat4_set_rotation( mat4 *self, + float angle, + float x, float y, float z) +{ + float c, s, norm; + + assert( self ); + + c = cos( M_PI*angle/180.0 ); + s = sin( M_PI*angle/180.0 ); + norm = sqrt(x*x+y*y+z*z); + + x /= norm; y /= norm; z /= norm; + + mat4_set_identity( self ); + + self->m00 = x*x*(1-c)+c; + self->m10 = y*x*(1-c)-z*s; + self->m20 = z*x*(1-c)+y*s; + + self->m01 = x*y*(1-c)+z*s; + self->m11 = y*y*(1-c)+c; + self->m21 = z*y*(1-c)-x*s; + + self->m02 = x*z*(1-c)-y*s; + self->m12 = y*z*(1-c)+x*s; + self->m22 = z*z*(1-c)+c; +} + +void +mat4_set_translation( mat4 *self, + float x, float y, float z) +{ + assert( self ); + + mat4_set_identity( self ); + self-> m30 = x; + self-> m31 = y; + self-> m32 = z; +} + +void +mat4_set_scaling( mat4 *self, + float x, float y, float z) +{ + assert( self ); + + mat4_set_identity( self ); + self-> m00 = x; + self-> m11 = y; + self-> m22 = z; +} + +void +mat4_rotate( mat4 *self, + float angle, + float x, float y, float z) +{ + mat4 m; + + assert( self ); + + mat4_set_rotation( &m, angle, x, y, z); + mat4_multiply( self, &m ); +} + +void +mat4_translate( mat4 *self, + float x, float y, float z) +{ + mat4 m; + assert( self ); + + mat4_set_translation( &m, x, y, z); + mat4_multiply( self, &m ); +} + +void +mat4_scale( mat4 *self, + float x, float y, float z) +{ + mat4 m; + assert( self ); + + mat4_set_scaling( &m, x, y, z); + mat4_multiply( self, &m ); +} diff --git a/dgl/src/freetype-gl/mat4.h b/dgl/src/freetype-gl/mat4.h @@ -0,0 +1,117 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#ifndef __MAT4_H__ +#define __MAT4_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * + */ +typedef union +{ + float data[16]; /**< All compoments at once */ + struct { + float m00, m01, m02, m03; + float m10, m11, m12, m13; + float m20, m21, m22, m23; + float m30, m31, m32, m33; + }; +} mat4; + + +mat4 * +mat4_new( void ); + +void +mat4_set_identity( mat4 *self ); + +void +mat4_set_zero( mat4 *self ); + +void +mat4_multiply( mat4 *self, mat4 *other ); + +void +mat4_set_orthographic( mat4 *self, + float left, float right, + float bottom, float top, + float znear, float zfar ); + +void +mat4_set_perspective( mat4 *self, + float fovy, float aspect, + float zNear, float zFar); + +void +mat4_set_frustum( mat4 *self, + float left, float right, + float bottom, float top, + float znear, float zfar ); + +void +mat4_set_rotation( mat4 *self, + float angle, + float x, float y, float z); + +void +mat4_set_translation( mat4 *self, + float x, float y, float z); + +void +mat4_set_scaling( mat4 *self, + float x, float y, float z); + +void +mat4_rotate( mat4 *self, + float angle, + float x, float y, float z); + +void +mat4_translate( mat4 *self, + float x, float y, float z); + +void +mat4_scale( mat4 *self, + float x, float y, float z); + + +#ifdef __cplusplus +} +#endif + +#endif /* __MAT4_H__ */ diff --git a/dgl/src/freetype-gl/shader.c b/dgl/src/freetype-gl/shader.c @@ -0,0 +1,124 @@ +// ---------------------------------------------------------------------------- +// OpenGL Anti-Grain Geometry (GL-AGG) - Version 0.1 +// A high quality OpenGL rendering engine for C +// Copyright (C) 2012 Nicolas P. Rougier. All rights reserved. +// Contact: Nicolas.Rougier@gmail.com +// http://code.google.com/p/gl-agg/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are +// those of the authors and should not be interpreted as representing official +// policies, either expressed or implied, of Nicolas P. Rougier. +// ---------------------------------------------------------------------------- +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "opengl.h" +#include "shader.h" + + +// ------------------------------------------------------------ shader_read --- +char * +shader_read( const char *filename ) +{ + FILE * file; + char * buffer; + size_t size; + + file = fopen( filename, "rb" ); + if( !file ) + { + fprintf( stderr, "Unable to open file \"%s\".\n", filename ); + return 0; + } + fseek( file, 0, SEEK_END ); + size = ftell( file ); + fseek(file, 0, SEEK_SET ); + buffer = (char *) malloc( (size+1) * sizeof( char *) ); + fread( buffer, sizeof(char), size, file ); + buffer[size] = 0; + fclose( file ); + return buffer; +} + + + +// --------------------------------------------------------- shader_compile --- +GLuint +shader_compile( const char* source, + const GLenum type ) +{ + GLint compile_status; + GLuint handle = glCreateShader( type ); + glShaderSource( handle, 1, &source, 0 ); + glCompileShader( handle ); + + glGetShaderiv( handle, GL_COMPILE_STATUS, &compile_status ); + if( compile_status == GL_FALSE ) + { + GLchar messages[256]; + glGetShaderInfoLog( handle, sizeof(messages), 0, &messages[0] ); + fprintf( stderr, "%s\n", messages ); + exit( EXIT_FAILURE ); + } + return handle; +} + + +// ------------------------------------------------------------ shader_load --- +GLuint +shader_load( const char * vert_filename, + const char * frag_filename ) +{ + GLuint handle = glCreateProgram( ); + GLint link_status; + + if( vert_filename && strlen( vert_filename ) ) + { + char *vert_source = shader_read( vert_filename ); + GLuint vert_shader = shader_compile( vert_source, GL_VERTEX_SHADER); + glAttachShader( handle, vert_shader); + glDeleteShader( vert_shader ); + free( vert_source ); + } + if( frag_filename && strlen( frag_filename ) ) + { + char *frag_source = shader_read( frag_filename ); + GLuint frag_shader = shader_compile( frag_source, GL_FRAGMENT_SHADER); + glAttachShader( handle, frag_shader); + glDeleteShader( frag_shader ); + free( frag_source ); + } + + glLinkProgram( handle ); + + glGetProgramiv( handle, GL_LINK_STATUS, &link_status ); + if (link_status == GL_FALSE) + { + GLchar messages[256]; + glGetProgramInfoLog( handle, sizeof(messages), 0, &messages[0] ); + fprintf( stderr, "%s\n", messages ); + exit(1); + } + return handle; +} diff --git a/dgl/src/freetype-gl/shader.h b/dgl/src/freetype-gl/shader.h @@ -0,0 +1,119 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012,2013 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ---------------------------------------------------------------------------- + */ +#ifndef __SHADER_H__ +#define __SHADER_H__ + +#include "opengl.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file shader.h + * @author Nicolas Rougier (Nicolas.Rougier@inria.fr) + * + * @defgroup shader Shader + * + * Simple functions for loading/building a shader from sources. + * + * <b>Example Usage</b>: + * @code + * #include "shader.h" + * + * int main( int arrgc, char *argv[] ) + * { + * GLuint shader = shader_load("shader.vert", "shader.frag"); + * + * return 0; + * } + * @endcode + * + * @{ + */ + +/** + * Read a fragment or vertex shader from a file + * + * @param filename file to read shader from + * @return a newly-allocated text buffer containing code. This buffer + * must be freed after usage. + * + */ + char * + shader_read( const char *filename ); + + +/** + * Compile a shader from a text buffer. + * + * @param source code of the shader + * @param type type of the shader + * + * @return a handle on the compiled program + * + */ + GLuint + shader_compile( const char* source, + const GLenum type ); + + +/** + * Load a vertex and fragment shader sources and build program + * + * @param vert_filename vertex shader filename + * @param frag_filename fragment shader filename + * + * @return a handle on the built program + * + */ + GLuint + shader_load( const char * vert_filename, + const char * frag_filename ); + + +/** + * + */ + GLuint + shader_get( GLuint self, + const char * name ); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* __SHADER_H__ */ diff --git a/dgl/src/freetype-gl/text-buffer.c b/dgl/src/freetype-gl/text-buffer.c @@ -0,0 +1,448 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#include <wchar.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <assert.h> +#include "opengl.h" +#include "text-buffer.h" + + +#define SET_GLYPH_VERTEX(value,x0,y0,z0,s0,t0,r,g,b,a,sh,gm) { \ + glyph_vertex_t *gv=&value; \ + gv->x=x0; gv->y=y0; gv->z=z0; \ + gv->u=s0; gv->v=t0; \ + gv->r=r; gv->g=g; gv->b=b; gv->a=a; \ + gv->shift=sh; gv->gamma=gm;} + + +// ---------------------------------------------------------------------------- +text_buffer_t * +text_buffer_new( size_t depth ) +{ + + text_buffer_t *self = (text_buffer_t *) malloc (sizeof(text_buffer_t)); + self->buffer = vertex_buffer_new( + "vertex:3f,tex_coord:2f,color:4f,ashift:1f,agamma:1f" ); + self->manager = font_manager_new( 512, 512, depth ); + self->shader = shader_load("shaders/text.vert", + "shaders/text.frag"); + self->shader_texture = glGetUniformLocation(self->shader, "texture"); + self->shader_pixel = glGetUniformLocation(self->shader, "pixel"); + self->line_start = 0; + self->line_ascender = 0; + self->base_color.r = 0.0; + self->base_color.g = 0.0; + self->base_color.b = 0.0; + self->base_color.a = 1.0; + self->line_descender = 0; + return self; +} + +// ---------------------------------------------------------------------------- +void +text_buffer_delete( text_buffer_t * self ) +{ + vertex_buffer_delete( self->buffer ); + glDeleteProgram( self->shader ); + free( self ); +} + +// ---------------------------------------------------------------------------- +void +text_buffer_clear( text_buffer_t * self ) +{ + assert( self ); + + vertex_buffer_clear( self->buffer ); + self->line_start = 0; + self->line_ascender = 0; + self->line_descender = 0; +} + + +// ---------------------------------------------------------------------------- +void +text_buffer_render( text_buffer_t * self ) +{ + glEnable( GL_BLEND ); + + glActiveTexture( GL_TEXTURE0 ); + glBindTexture( GL_TEXTURE_2D, self->manager->atlas->id ); + + if( self->manager->atlas->depth == 1 ) + { + //glDisable( GL_COLOR_MATERIAL ); + glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + glBlendColor( 1, 1, 1, 1 ); + } + else + { + //glEnable( GL_COLOR_MATERIAL ); + //glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); + //glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ); + //glBlendColor( 1.0, 1.0, 1.0, 1.0 ); + //glBlendFunc( GL_CONSTANT_COLOR_EXT, GL_ONE_MINUS_SRC_COLOR ); + //glBlendColor( self->base_color.r, + //self->base_color.g, + //self->base_color.b, + //self->base_color.a ); + glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + glBlendColor( 1, 1, 1, 1 ); + } + + glUseProgram( self->shader ); + glUniform1i( self->shader_texture, 0 ); + glUniform3f( self->shader_pixel, + 1.0/self->manager->atlas->width, + 1.0/self->manager->atlas->height, + self->manager->atlas->depth ); + vertex_buffer_render( self->buffer, GL_TRIANGLES ); + glUseProgram( 0 ); +} + +// ---------------------------------------------------------------------------- +void +text_buffer_printf( text_buffer_t * self, vec2 *pen, ... ) +{ + markup_t *markup; + wchar_t *text; + va_list args; + + if( vertex_buffer_size( self->buffer ) == 0 ) + { + self->origin = *pen; + } + + va_start ( args, pen ); + do { + markup = va_arg( args, markup_t * ); + if( markup == NULL ) + { + return; + } + text = va_arg( args, wchar_t * ); + text_buffer_add_text( self, pen, markup, text, wcslen(text) ); + } while( markup != 0 ); + va_end ( args ); +} + +// ---------------------------------------------------------------------------- +void +text_buffer_move_last_line( text_buffer_t * self, float dy ) +{ + size_t i, j; + for( i=self->line_start; i < vector_size( self->buffer->items ); ++i ) + { + ivec4 *item = (ivec4 *) vector_get( self->buffer->items, i); + for( j=item->vstart; j<item->vstart+item->vcount; ++j) + { + glyph_vertex_t * vertex = + (glyph_vertex_t *) vector_get( self->buffer->vertices, j ); + vertex->y -= dy; + } + } +} + + +// ---------------------------------------------------------------------------- +void +text_buffer_add_text( text_buffer_t * self, + vec2 * pen, markup_t * markup, + wchar_t * text, size_t length ) +{ + font_manager_t * manager = self->manager; + size_t i; + + if( markup == NULL ) + { + return; + } + + if( !markup->font ) + { + markup->font = font_manager_get_from_markup( manager, markup ); + if( ! markup->font ) + { + fprintf( stderr, "Houston, we've got a problem !\n" ); + exit( EXIT_FAILURE ); + } + } + + if( length == 0 ) + { + length = wcslen(text); + } + if( vertex_buffer_size( self->buffer ) == 0 ) + { + self->origin = *pen; + } + + text_buffer_add_wchar( self, pen, markup, text[0], 0 ); + for( i=1; i<length; ++i ) + { + text_buffer_add_wchar( self, pen, markup, text[i], text[i-1] ); + } +} + +// ---------------------------------------------------------------------------- +void +text_buffer_add_wchar( text_buffer_t * self, + vec2 * pen, markup_t * markup, + wchar_t current, wchar_t previous ) +{ + size_t vcount = 0; + size_t icount = 0; + vertex_buffer_t * buffer = self->buffer; + texture_font_t * font = markup->font; + float gamma = markup->gamma; + + // Maximum number of vertices is 20 (= 5x2 triangles) per glyph: + // - 2 triangles for background + // - 2 triangles for overline + // - 2 triangles for underline + // - 2 triangles for strikethrough + // - 2 triangles for glyph + glyph_vertex_t vertices[4*5]; + GLuint indices[6*5]; + texture_glyph_t *glyph; + texture_glyph_t *black; + float kerning = 0; + + if( current == L'\n' ) + { + pen->x = self->origin.x; + pen->y += self->line_descender; + self->line_descender = 0; + self->line_ascender = 0; + self->line_start = vector_size( self->buffer->items ); + return; + } + + if( markup->font->ascender > self->line_ascender ) + { + float y = pen->y; + pen->y -= (markup->font->ascender - self->line_ascender); + text_buffer_move_last_line( self, (int)(y-pen->y) ); + self->line_ascender = markup->font->ascender; + } + if( markup->font->descender < self->line_descender ) + { + self->line_descender = markup->font->descender; + } + + glyph = texture_font_get_glyph( font, current ); + black = texture_font_get_glyph( font, -1 ); + + if( glyph == NULL ) + { + return; + } + + if( previous && markup->font->kerning ) + { + kerning = texture_glyph_get_kerning( glyph, previous ); + } + pen->x += kerning; + + // Background + if( markup->background_color.alpha > 0 ) + { + float r = markup->background_color.r; + float g = markup->background_color.g; + float b = markup->background_color.b; + float a = markup->background_color.a; + float x0 = ( pen->x -kerning ); + float y0 = (int)( pen->y + font->descender ); + float x1 = ( x0 + glyph->advance_x ); + float y1 = (int)( y0 + font->height + font->linegap ); + float s0 = black->s0; + float t0 = black->t0; + float s1 = black->s1; + float t1 = black->t1; + + SET_GLYPH_VERTEX(vertices[vcount+0], + (int)x0,y0,0, s0,t0, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+1], + (int)x0,y1,0, s0,t1, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+2], + (int)x1,y1,0, s1,t1, r,g,b,a, x1-((int)x1), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+3], + (int)x1,y0,0, s1,t0, r,g,b,a, x1-((int)x1), gamma ); + indices[icount + 0] = vcount+0; + indices[icount + 1] = vcount+1; + indices[icount + 2] = vcount+2; + indices[icount + 3] = vcount+0; + indices[icount + 4] = vcount+2; + indices[icount + 5] = vcount+3; + vcount += 4; + icount += 6; + } + + // Underline + if( markup->underline ) + { + float r = markup->underline_color.r; + float g = markup->underline_color.g; + float b = markup->underline_color.b; + float a = markup->underline_color.a; + float x0 = ( pen->x - kerning ); + float y0 = (int)( pen->y + font->underline_position ); + float x1 = ( x0 + glyph->advance_x ); + float y1 = (int)( y0 + font->underline_thickness ); + float s0 = black->s0; + float t0 = black->t0; + float s1 = black->s1; + float t1 = black->t1; + + SET_GLYPH_VERTEX(vertices[vcount+0], + (int)x0,y0,0, s0,t0, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+1], + (int)x0,y1,0, s0,t1, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+2], + (int)x1,y1,0, s1,t1, r,g,b,a, x1-((int)x1), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+3], + (int)x1,y0,0, s1,t0, r,g,b,a, x1-((int)x1), gamma ); + indices[icount + 0] = vcount+0; + indices[icount + 1] = vcount+1; + indices[icount + 2] = vcount+2; + indices[icount + 3] = vcount+0; + indices[icount + 4] = vcount+2; + indices[icount + 5] = vcount+3; + vcount += 4; + icount += 6; + } + + // Overline + if( markup->overline ) + { + float r = markup->overline_color.r; + float g = markup->overline_color.g; + float b = markup->overline_color.b; + float a = markup->overline_color.a; + float x0 = ( pen->x -kerning ); + float y0 = (int)( pen->y + (int)font->ascender ); + float x1 = ( x0 + glyph->advance_x ); + float y1 = (int)( y0 + (int)font->underline_thickness ); + float s0 = black->s0; + float t0 = black->t0; + float s1 = black->s1; + float t1 = black->t1; + SET_GLYPH_VERTEX(vertices[vcount+0], + (int)x0,y0,0, s0,t0, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+1], + (int)x0,y1,0, s0,t1, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+2], + (int)x1,y1,0, s1,t1, r,g,b,a, x1-((int)x1), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+3], + (int)x1,y0,0, s1,t0, r,g,b,a, x1-((int)x1), gamma ); + indices[icount + 0] = vcount+0; + indices[icount + 1] = vcount+1; + indices[icount + 2] = vcount+2; + indices[icount + 3] = vcount+0; + indices[icount + 4] = vcount+2; + indices[icount + 5] = vcount+3; + vcount += 4; + icount += 6; + } + + /* Strikethrough */ + if( markup->strikethrough ) + { + float r = markup->strikethrough_color.r; + float g = markup->strikethrough_color.g; + float b = markup->strikethrough_color.b; + float a = markup->strikethrough_color.a; + float x0 = ( pen->x -kerning ); + float y0 = (int)( pen->y + (int)font->ascender*.33); + float x1 = ( x0 + glyph->advance_x ); + float y1 = (int)( y0 + (int)font->underline_thickness ); + float s0 = black->s0; + float t0 = black->t0; + float s1 = black->s1; + float t1 = black->t1; + SET_GLYPH_VERTEX(vertices[vcount+0], + (int)x0,y0,0, s0,t0, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+1], + (int)x0,y1,0, s0,t1, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+2], + (int)x1,y1,0, s1,t1, r,g,b,a, x1-((int)x1), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+3], + (int)x1,y0,0, s1,t0, r,g,b,a, x1-((int)x1), gamma ); + indices[icount + 0] = vcount+0; + indices[icount + 1] = vcount+1; + indices[icount + 2] = vcount+2; + indices[icount + 3] = vcount+0; + indices[icount + 4] = vcount+2; + indices[icount + 5] = vcount+3; + vcount += 4; + icount += 6; + } + { + // Actual glyph + float r = markup->foreground_color.red; + float g = markup->foreground_color.green; + float b = markup->foreground_color.blue; + float a = markup->foreground_color.alpha; + float x0 = ( pen->x + glyph->offset_x ); + float y0 = (int)( pen->y + glyph->offset_y ); + float x1 = ( x0 + glyph->width ); + float y1 = (int)( y0 - glyph->height ); + float s0 = glyph->s0; + float t0 = glyph->t0; + float s1 = glyph->s1; + float t1 = glyph->t1; + + SET_GLYPH_VERTEX(vertices[vcount+0], + (int)x0,y0,0, s0,t0, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+1], + (int)x0,y1,0, s0,t1, r,g,b,a, x0-((int)x0), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+2], + (int)x1,y1,0, s1,t1, r,g,b,a, x1-((int)x1), gamma ); + SET_GLYPH_VERTEX(vertices[vcount+3], + (int)x1,y0,0, s1,t0, r,g,b,a, x1-((int)x1), gamma ); + indices[icount + 0] = vcount+0; + indices[icount + 1] = vcount+1; + indices[icount + 2] = vcount+2; + indices[icount + 3] = vcount+0; + indices[icount + 4] = vcount+2; + indices[icount + 5] = vcount+3; + vcount += 4; + icount += 6; + + vertex_buffer_push_back( buffer, vertices, vcount, indices, icount ); + pen->x += glyph->advance_x * (1.0 + markup->spacing); + } +} diff --git a/dgl/src/freetype-gl/text-buffer.h b/dgl/src/freetype-gl/text-buffer.h @@ -0,0 +1,285 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#ifndef __TEXT_BUFFER_H__ +#define __TEXT_BUFFER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "vertex-buffer.h" +#include "font-manager.h" +#include "markup.h" +#include "shader.h" + + +/** + * Use LCD filtering + */ +#define LCD_FILTERING_ON 3 + +/** + * Do not use LCD filtering + */ +#define LCD_FILTERING_OFF 1 + +/** + * @file text-buffer.h + * @author Nicolas Rougier (Nicolas.Rougier@inria.fr) + * + * @defgroup text-buffer Text buffer + * + * + * <b>Example Usage</b>: + * @code + * #include "shader.h" + * + * int main( int arrgc, char *argv[] ) + * { + * + * return 0; + * } + * @endcode + * + * @{ + */ + +/** + * Text buffer structure + */ +typedef struct text_buffer_t { + /** + * Vertex buffer + */ + vertex_buffer_t *buffer; + + /** + * Font manager + */ + font_manager_t *manager; + + /** + * Base color for text + */ + vec4 base_color; + + + /** + * Pen origin + */ + vec2 origin; + + /** + * Index (in the vertex buffer) of the line start + */ + size_t line_start; + + /** + * Current line ascender + */ + float line_ascender; + + /** + * Current line decender + */ + float line_descender; + + /** + * Shader handler + */ + GLuint shader; + + /** + * Shader "texture" location + */ + GLuint shader_texture; + + /** + * Shader "pixel" location + */ + GLuint shader_pixel; + +} text_buffer_t; + + + +/** + * Glyph vertex structure + */ +typedef struct glyph_vertex_t { + /** + * Vertex x coordinates + */ + float x; + + /** + * Vertex y coordinates + */ + float y; + + /** + * Vertex z coordinates + */ + float z; + + /** + * Texture first coordinate + */ + float u; + + /** + * Texture second coordinate + */ + float v; + + /** + * Color red component + */ + float r; + + /** + * Color green component + */ + float g; + + /** + * Color blue component + */ + float b; + + /** + * Color alpha component + */ + float a; + + /** + * Shift along x + */ + float shift; + + /** + * Color gamma correction + */ + float gamma; + +} glyph_vertex_t; + + + +/** + * Creates a new empty text buffer. + * + * @param depth Underlying atlas bit depth (1 or 3) + * + * @return a new empty text buffer. + * + */ + text_buffer_t * + text_buffer_new( size_t depth ); + + /** + * Deletes texture buffer and its associated shader and vertex buffer. + * + * @param self texture buffer to delete + * + */ +void +text_buffer_delete( text_buffer_t * self ); + +/** + * Render a text buffer. + * + * @param self a text buffer + * + */ + void + text_buffer_render( text_buffer_t * self ); + + + /** + * Print some text to the text buffer + * + * @param self a text buffer + * @param pen position of text start + * @param ... a series of markup_t *, wchar_t * ended by NULL + * + */ + void + text_buffer_printf( text_buffer_t * self, vec2 * pen, ... ); + + + /** + * Add some text to the text buffer + * + * @param self a text buffer + * @param pen position of text start + * @param markup Markup to be used to add text + * @param text Text to be added + * @param length Length of text to be added + */ + void + text_buffer_add_text( text_buffer_t * self, + vec2 * pen, markup_t * markup, + wchar_t * text, size_t length ); + + /** + * Add a char to the text buffer + * + * @param self a text buffer + * @param pen position of text start + * @param markup markup to be used to add text + * @param current charactr to be added + * @param previous previous character (if any) + */ + void + text_buffer_add_wchar( text_buffer_t * self, + vec2 * pen, markup_t * markup, + wchar_t current, wchar_t previous ); + +/** + * Clear text buffer + * + * @param self a text buffer + */ + void + text_buffer_clear( text_buffer_t * self ); + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* #define __TEXT_BUFFER_H__ */ diff --git a/dgl/src/freetype-gl/texture-font.c b/dgl/src/freetype-gl/texture-font.c @@ -31,6 +31,7 @@ * policies, either expressed or implied, of Nicolas P. Rougier. * ============================================================================ */ +#include "../freetype/FreeTypeAmalgam.h" #include "../freetype/FreeTypeAmalgam.c" #include <stdint.h> #include <stdlib.h> @@ -478,7 +479,7 @@ texture_font_load_glyphs( texture_font_t * self, flags |= FT_LOAD_FORCE_AUTOHINT; } - +#if 0 if( depth == 3 ) { FT_Library_SetLcdFilter( library, FT_LCD_FILTER_LIGHT ); @@ -488,6 +489,7 @@ texture_font_load_glyphs( texture_font_t * self, FT_Library_SetLcdFilterWeights( library, self->lcd_weights ); } } +#endif error = FT_Load_Glyph( face, glyph_index, flags ); if( error ) { diff --git a/dgl/src/freetype-gl/vertex-attribute.c b/dgl/src/freetype-gl/vertex-attribute.c @@ -0,0 +1,168 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#include <assert.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include "vec234.h" +#include "platform.h" +#include "vertex-attribute.h" + + + +// ---------------------------------------------------------------------------- +vertex_attribute_t * +vertex_attribute_new( GLchar * name, + GLint size, + GLenum type, + GLboolean normalized, + GLsizei stride, + GLvoid *pointer ) +{ + vertex_attribute_t *attribute = + (vertex_attribute_t *) malloc (sizeof(vertex_attribute_t)); + + assert( size > 0 ); + + attribute->name = (GLchar *) strdup( name ); + attribute->index = -1; + attribute->size = size; + attribute->type = type; + attribute->normalized = normalized; + attribute->stride = stride; + attribute->pointer = pointer; + return attribute; +} + + + +// ---------------------------------------------------------------------------- +void +vertex_attribute_delete( vertex_attribute_t * self ) +{ + assert( self ); + + free( self->name ); + free( self ); +} + + + +// ---------------------------------------------------------------------------- +vertex_attribute_t * +vertex_attribute_parse( char *format ) +{ + GLenum type = 0; + int size; + int normalized = 0; + char ctype; + char *name; + vertex_attribute_t *attr; + char *p = strchr(format, ':'); + if( p != NULL) + { + name = strndup(format, p-format); + if( *(++p) == '\0' ) + { + fprintf( stderr, "No size specified for '%s' attribute\n", name ); + free( name ); + return 0; + } + size = *p - '0'; + + if( *(++p) == '\0' ) + { + fprintf( stderr, "No format specified for '%s' attribute\n", name ); + free( name ); + return 0; + } + ctype = *p; + + if( *(++p) != '\0' ) + { + if( *p == 'n' ) + { + normalized = 1; + } + } + + } + else + { + fprintf(stderr, "Vertex attribute format not understood ('%s')\n", format ); + return 0; + } + + switch( ctype ) + { + case 'b': type = GL_BYTE; break; + case 'B': type = GL_UNSIGNED_BYTE; break; + case 's': type = GL_SHORT; break; + case 'S': type = GL_UNSIGNED_SHORT; break; + case 'i': type = GL_INT; break; + case 'I': type = GL_UNSIGNED_INT; break; + case 'f': type = GL_FLOAT; break; + default: type = 0; break; + } + + + attr = vertex_attribute_new( name, size, type, normalized, 0, 0 ); + free( name ); + return attr; +} + + + +// ---------------------------------------------------------------------------- +void +vertex_attribute_enable( vertex_attribute_t *attr ) +{ + if( attr->index == -1 ) + { + GLint program; + glGetIntegerv( GL_CURRENT_PROGRAM, &program ); + if( program == 0) + { + return; + } + attr->index = glGetAttribLocation( program, attr->name ); + if( attr->index == -1 ) + { + return; + } + } + glEnableVertexAttribArray( attr->index ); + glVertexAttribPointer( attr->index, attr->size, attr->type, + attr->normalized, attr->stride, attr->pointer ); +} diff --git a/dgl/src/freetype-gl/vertex-attribute.h b/dgl/src/freetype-gl/vertex-attribute.h @@ -0,0 +1,320 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#ifndef __VERTEX_ATTRIBUTE_H__ +#define __VERTEX_ATTRIBUTE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "opengl.h" +#include "vector.h" + + +/** + * @file vertex-attribute.h + * @author Nicolas Rougier (Nicolas.Rougier@inria.fr) + * + * @defgroup vertex-attribut Vertex attribute + * + * Besides the required vertex position, vertices can have several other + * numeric attributes. Each is specified in the format string with a letter, + * the number of components and the data type. + * + * Each of the attributes is described in the table below with the set of valid + * format strings written as a regular expression (for example, "v[234][if]" + * means "v2f", "v3i", "v4f", etc. are all valid formats). + * + * Some attributes have a "recommended" format string, which is the most + * efficient form for the video driver as it requires less conversion. + * + * <table> + * <tr> + * <th>Attribute</th> + * <th>Formats</th> + * <th>Recommended</th> + * </tr> + * <tr> + * <td>Vertex position</td> + * <td>"v[234][sifd]"</td> + * <td>"v[234]f"</td> + * </tr> + * <tr> + * <td> Color </td> + * <td> "c[34][bBsSiIfd]" </td> + * <td> "c[34]B" </td> + * </tr> + * <tr> + * <td> Edge flag </td> + * <td> "e1[bB]" </td> + * <td> </td> + * </tr> + * <tr> + * <td> Fog coordinate </td> + * <td> "f[1234][bBsSiIfd]"</td> + * <td> </td> + * </tr> + * <tr> + * <td> Normal </td> + * <td> "n3[bsifd]" </td> + * <td> "n3f" </td> + * </tr> + * <tr> + * <td> Secondary color </td> + * <td> "s[34][bBsSiIfd]" </td> + * <td> "s[34]B" </td> + * </tr> + * <tr> + * <td> Texture coordinate </td> + * <td> "t[234][sifd]" </td> + * <td> "t[234]f" </td> + * </tr> + * <tr> + * <td> Generic attribute </td> + * <td> "[0-15]g(n)?[1234][bBsSiIfd]" </td> + * <td> </td> + * </tr> + * </table> + * + * The possible data types that can be specified in the format string are described below. + * + * <table> + * <tr> + * <th> Format </th> + * <th> Type </th> + * <th> GL Type </th> + * </tr> + * <tr> + * <td> "b" </td> + * <td> Signed byte </td> + * <td> GL_BYTE </td> + * </tr> + * <tr> + * <td> "B" </td> + * <td> Unsigned byte </td> + * <td> GL_UNSIGNED_BYTE </td> + * </tr> + * <tr> + * <td> "s" </td> + * <td> Signed short </td> + * <td> GL_SHORT </td> + * </tr> + * <tr> + * <td> "S" </td> + * <td> Unsigned short </td> + * <td> GL_UNSIGNED_SHORT </td> + * </tr> + * <tr> + * <td> "i" </td> + * <td> Signed int </td> + * <td> GL_INT </td> + * </tr> + * <tr> + * <td> "I" </td> + * <td> Unsigned int </td> + * <td> GL_UNSIGNED_INT </td> + * </tr> + * <tr> + * <td> "f" </td> + * <td> Float </td> + * <td> GL_FLOAT </td> + * </tr> + * <tr> + * <td> "d" </td> + * <td> Double </td> + * <td> GL_DOUBLE T </td> + * </tr> + * </table> + * + * The following attributes are normalised to the range [0, 1]. The value is + * used as-is if the data type is floating-point. If the data type is byte, + * short or int, the value is divided by the maximum value representable by + * that type. For example, unsigned bytes are divided by 255 to get the + * normalised value. + * + * - Color + * - Secondary color + * - Generic attributes with the "n" format given. + * + * Up to 16 generic attributes can be specified per vertex, and can be used by + * shader programs for any purpose (they are ignored in the fixed-function + * pipeline). For the other attributes, consult the OpenGL programming guide + * for details on their effects. + * + * When using the draw and related functions, attribute data is specified + * alongside the vertex position data. The following example reproduces the two + * points from the previous page, except that the first point is blue and the + * second green: + * + * It is an error to provide more than one set of data for any attribute, or to + * mismatch the size of the initial data with the number of vertices specified + * in the first argument. + * + * @{ + */ + + +/** + * Maximum number of attributes per vertex + * + * @private + */ +#define MAX_VERTEX_ATTRIBUTE 16 + + +/** + * Generic vertex attribute. + */ +typedef struct vertex_attribute_t +{ + /** + * atribute name + */ + GLchar * name; + + /** + * index of the generic vertex attribute to be modified. + */ + GLuint index; + + /** + * Number of components per generic vertex attribute. + * + * Must be 1, 2, 3, or 4. The initial value is 4. + */ + GLint size; + + /** + * data type of each component in the array. + * + * Symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, + * GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, or GL_DOUBLE are + * accepted. The initial value is GL_FLOAT. + */ + GLenum type; + + /** + * whether fixed-point data values should be normalized (GL_TRUE) or + * converted directly as fixed-point values (GL_FALSE) when they are + * accessed. + */ + GLboolean normalized; + + /** + * byte offset between consecutive generic vertex attributes. + * + * If stride is 0, the generic vertex attributes are understood to be + * tightly packed in the array. The initial value is 0. + */ + GLsizei stride; + + /** + * pointer to the first component of the first attribute element in the + * array. + */ + GLvoid * pointer; + + /** + * pointer to the function that enable this attribute. + */ + void ( * enable )(void *); + +} vertex_attribute_t; + + + +/** + * Create an attribute from the given parameters. + * + * @param size number of component + * @param type data type + * @param normalized Whether fixed-point data values should be normalized + (GL_TRUE) or converted directly as fixed-point values + (GL_FALSE) when they are accessed. + * @param stride byte offset between consecutive attributes. + * @param pointer pointer to the first component of the first attribute + * element in the array. + * @return a new initialized vertex attribute. + * + * @private + */ +vertex_attribute_t * +vertex_attribute_new( GLchar * name, + GLint size, + GLenum type, + GLboolean normalized, + GLsizei stride, + GLvoid *pointer ); + + + +/** + * Delete a vertex attribute. + * + * @param self a vertex attribute + * + */ +void +vertex_attribute_delete( vertex_attribute_t * self ); + + +/** + * Create an attribute from the given description. + * + * @param format Format string specifies the format of a vertex attribute. + * @return an initialized vertex attribute + * + * @private + */ + vertex_attribute_t * + vertex_attribute_parse( char *format ); + +/** + * Enable a vertex attribute. + * + * @param attr a vertex attribute + * + * @private + */ + void + vertex_attribute_enable( vertex_attribute_t *attr ); + + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* __VERTEX_ATTRIBUTE_H__ */ diff --git a/dgl/src/freetype-gl/vertex-buffer.c b/dgl/src/freetype-gl/vertex-buffer.c @@ -0,0 +1,662 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#include <assert.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include "vec234.h" +#include "platform.h" +#include "vertex-buffer.h" + +/** + * Buffer status + */ +#define CLEAN (0) +#define DIRTY (1) +#define FROZEN (2) + + +// ---------------------------------------------------------------------------- +vertex_buffer_t * +vertex_buffer_new( const char *format ) +{ + size_t i, index = 0, stride = 0; + const char *start = 0, *end = 0; + GLchar *pointer = 0; + + vertex_buffer_t *self = (vertex_buffer_t *) malloc (sizeof(vertex_buffer_t)); + if( !self ) + { + return NULL; + } + + self->format = strdup( format ); + + for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i ) + { + self->attributes[i] = 0; + } + + start = format; + do + { + char *desc = 0; + vertex_attribute_t *attribute; + GLuint attribute_size = 0; + end = (char *) (strchr(start+1, ',')); + + if (end == NULL) + { + desc = strdup( start ); + } + else + { + desc = strndup( start, end-start ); + } + attribute = vertex_attribute_parse( desc ); + start = end+1; + free(desc); + attribute->pointer = pointer; + + switch( attribute->type ) + { + case GL_BOOL: attribute_size = sizeof(GLboolean); break; + case GL_BYTE: attribute_size = sizeof(GLbyte); break; + case GL_UNSIGNED_BYTE: attribute_size = sizeof(GLubyte); break; + case GL_SHORT: attribute_size = sizeof(GLshort); break; + case GL_UNSIGNED_SHORT: attribute_size = sizeof(GLushort); break; + case GL_INT: attribute_size = sizeof(GLint); break; + case GL_UNSIGNED_INT: attribute_size = sizeof(GLuint); break; + case GL_FLOAT: attribute_size = sizeof(GLfloat); break; + default: attribute_size = 0; + } + stride += attribute->size*attribute_size; + pointer += attribute->size*attribute_size; + self->attributes[index] = attribute; + index++; + } while ( end && (index < MAX_VERTEX_ATTRIBUTE) ); + + for( i=0; i<index; ++i ) + { + self->attributes[i]->stride = stride; + } + +#ifdef FREETYPE_GL_USE_VAO + self->VAO_id = 0; +#endif + + self->vertices = vector_new( stride ); + self->vertices_id = 0; + self->GPU_vsize = 0; + + self->indices = vector_new( sizeof(GLuint) ); + self->indices_id = 0; + self->GPU_isize = 0; + + self->items = vector_new( sizeof(ivec4) ); + self->state = DIRTY; + self->mode = GL_TRIANGLES; + return self; +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_delete( vertex_buffer_t *self ) +{ + size_t i; + + assert( self ); + + for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i ) + { + if( self->attributes[i] ) + { + vertex_attribute_delete( self->attributes[i] ); + } + } + +#ifdef FREETYPE_GL_USE_VAO + if( self->VAO_id ) + { + glDeleteVertexArrays( 1, &self->VAO_id ); + } + self->VAO_id = 0; +#endif + + vector_delete( self->vertices ); + self->vertices = 0; + if( self->vertices_id ) + { + glDeleteBuffers( 1, &self->vertices_id ); + } + self->vertices_id = 0; + + vector_delete( self->indices ); + self->indices = 0; + if( self->indices_id ) + { + glDeleteBuffers( 1, &self->indices_id ); + } + self->indices_id = 0; + + vector_delete( self->items ); + + if( self->format ) + { + free( self->format ); + } + self->format = 0; + self->state = 0; + free( self ); +} + + +// ---------------------------------------------------------------------------- +const char * +vertex_buffer_format( const vertex_buffer_t *self ) +{ + assert( self ); + + return self->format; +} + + +// ---------------------------------------------------------------------------- +size_t +vertex_buffer_size( const vertex_buffer_t *self ) +{ + assert( self ); + + return vector_size( self->items ); +} + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_print( vertex_buffer_t * self ) +{ + int i = 0; + static char *gltypes[9] = { + "GL_BOOL", + "GL_BYTE", + "GL_UNSIGNED_BYTE", + "GL_SHORT", + "GL_UNSIGNED_SHORT", + "GL_INT", + "GL_UNSIGNED_INT", + "GL_FLOAT", + "GL_VOID" + }; + + assert(self); + + fprintf( stderr, "%ld vertices, %ld indices\n", + vector_size( self->vertices ), vector_size( self->indices ) ); + while( self->attributes[i] ) + { + int j = 8; + switch( self->attributes[i]->type ) + { + case GL_BOOL: j=0; break; + case GL_BYTE: j=1; break; + case GL_UNSIGNED_BYTE: j=2; break; + case GL_SHORT: j=3; break; + case GL_UNSIGNED_SHORT: j=4; break; + case GL_INT: j=5; break; + case GL_UNSIGNED_INT: j=6; break; + case GL_FLOAT: j=7; break; + default: j=8; break; + } + fprintf(stderr, "%s : %dx%s (+%p)\n", + self->attributes[i]->name, + self->attributes[i]->size, + gltypes[j], + self->attributes[i]->pointer); + + i += 1; + } +} + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_upload ( vertex_buffer_t *self ) +{ + size_t vsize, isize; + + if( self->state == FROZEN ) + { + return; + } + + if( !self->vertices_id ) + { + glGenBuffers( 1, &self->vertices_id ); + } + if( !self->indices_id ) + { + glGenBuffers( 1, &self->indices_id ); + } + + vsize = self->vertices->size*self->vertices->item_size; + isize = self->indices->size*self->indices->item_size; + + + // Always upload vertices first such that indices do not point to non + // existing data (if we get interrupted in between for example). + + // Upload vertices + glBindBuffer( GL_ARRAY_BUFFER, self->vertices_id ); + if( vsize != self->GPU_vsize ) + { + glBufferData( GL_ARRAY_BUFFER, + vsize, self->vertices->items, GL_DYNAMIC_DRAW ); + self->GPU_vsize = vsize; + } + else + { + glBufferSubData( GL_ARRAY_BUFFER, + 0, vsize, self->vertices->items ); + } + glBindBuffer( GL_ARRAY_BUFFER, 0 ); + + // Upload indices + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id ); + if( isize != self->GPU_isize ) + { + glBufferData( GL_ELEMENT_ARRAY_BUFFER, + isize, self->indices->items, GL_DYNAMIC_DRAW ); + self->GPU_isize = isize; + } + else + { + glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, + 0, isize, self->indices->items ); + } + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_clear( vertex_buffer_t *self ) +{ + assert( self ); + + self->state = FROZEN; + vector_clear( self->indices ); + vector_clear( self->vertices ); + vector_clear( self->items ); + self->state = DIRTY; +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_render_setup ( vertex_buffer_t *self, GLenum mode ) +{ + size_t i; + +#ifdef FREETYPE_GL_USE_VAO + // Unbind so no existing VAO-state is overwritten, + // (e.g. the GL_ELEMENT_ARRAY_BUFFER-binding). + glBindVertexArray( 0 ); +#endif + + if( self->state != CLEAN ) + { + vertex_buffer_upload( self ); + self->state = CLEAN; + } + +#ifdef FREETYPE_GL_USE_VAO + if( self->VAO_id == 0 ) + { + // Generate and set up VAO + + glGenVertexArrays( 1, &self->VAO_id ); + glBindVertexArray( self->VAO_id ); + + glBindBuffer( GL_ARRAY_BUFFER, self->vertices_id ); + + for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i ) + { + vertex_attribute_t *attribute = self->attributes[i]; + if( attribute == 0 ) + { + continue; + } + else + { + vertex_attribute_enable( attribute ); + } + } + + glBindBuffer( GL_ARRAY_BUFFER, 0 ); + + if( self->indices->size ) + { + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id ); + } + } + + // Bind VAO for drawing + glBindVertexArray( self->VAO_id ); +#else + + glBindBuffer( GL_ARRAY_BUFFER, self->vertices_id ); + + for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i ) + { + vertex_attribute_t *attribute = self->attributes[i]; + if ( attribute == 0 ) + { + continue; + } + else + { + vertex_attribute_enable( attribute ); + } + } + + if( self->indices->size ) + { + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id ); + } +#endif + + self->mode = mode; +} + +// ---------------------------------------------------------------------------- +void +vertex_buffer_render_finish ( vertex_buffer_t *self ) +{ +#ifdef FREETYPE_GL_USE_VAO + glBindVertexArray( 0 ); +#else + glBindBuffer( GL_ARRAY_BUFFER, 0 ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); +#endif +} + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_render_item ( vertex_buffer_t *self, + size_t index ) +{ + ivec4 * item = (ivec4 *) vector_get( self->items, index ); + assert( self ); + assert( index < vector_size( self->items ) ); + + + if( self->indices->size ) + { + size_t start = item->istart; + size_t count = item->icount; + glDrawElements( self->mode, count, GL_UNSIGNED_INT, (void *)(start*sizeof(GLuint)) ); + } + else if( self->vertices->size ) + { + size_t start = item->vstart; + size_t count = item->vcount; + glDrawArrays( self->mode, start*self->vertices->item_size, count); + } +} + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_render ( vertex_buffer_t *self, GLenum mode ) +{ + size_t vcount = self->vertices->size; + size_t icount = self->indices->size; + + vertex_buffer_render_setup( self, mode ); + if( icount ) + { + glDrawElements( mode, icount, GL_UNSIGNED_INT, 0 ); + } + else + { + glDrawArrays( mode, 0, vcount ); + } + vertex_buffer_render_finish( self ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_push_back_indices ( vertex_buffer_t * self, + const GLuint * indices, + const size_t icount ) +{ + assert( self ); + + self->state |= DIRTY; + vector_push_back_data( self->indices, indices, icount ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_push_back_vertices ( vertex_buffer_t * self, + const void * vertices, + const size_t vcount ) +{ + assert( self ); + + self->state |= DIRTY; + vector_push_back_data( self->vertices, vertices, vcount ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_insert_indices ( vertex_buffer_t *self, + const size_t index, + const GLuint *indices, + const size_t count ) +{ + assert( self ); + assert( self->indices ); + assert( index < self->indices->size+1 ); + + self->state |= DIRTY; + vector_insert_data( self->indices, index, indices, count ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_insert_vertices( vertex_buffer_t *self, + const size_t index, + const void *vertices, + const size_t vcount ) +{ + size_t i; + assert( self ); + assert( self->vertices ); + assert( index < self->vertices->size+1 ); + + self->state |= DIRTY; + + for( i=0; i<self->indices->size; ++i ) + { + if( *(GLuint *)(vector_get( self->indices, i )) > index ) + { + *(GLuint *)(vector_get( self->indices, i )) += index; + } + } + + vector_insert_data( self->vertices, index, vertices, vcount ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_erase_indices( vertex_buffer_t *self, + const size_t first, + const size_t last ) +{ + assert( self ); + assert( self->indices ); + assert( first < self->indices->size ); + assert( (last) <= self->indices->size ); + + self->state |= DIRTY; + vector_erase_range( self->indices, first, last ); +} + + + +// ---------------------------------------------------------------------------- +void +vertex_buffer_erase_vertices( vertex_buffer_t *self, + const size_t first, + const size_t last ) +{ + size_t i; + assert( self ); + assert( self->vertices ); + assert( first < self->vertices->size ); + assert( (first+last) <= self->vertices->size ); + assert( last > first ); + + self->state |= DIRTY; + for( i=0; i<self->indices->size; ++i ) + { + if( *(GLuint *)(vector_get( self->indices, i )) > first ) + { + *(GLuint *)(vector_get( self->indices, i )) -= (last-first); + } + } + vector_erase_range( self->vertices, first, last ); +} + + + +// ---------------------------------------------------------------------------- +size_t +vertex_buffer_push_back( vertex_buffer_t * self, + const void * vertices, const size_t vcount, + const GLuint * indices, const size_t icount ) +{ + return vertex_buffer_insert( self, vector_size( self->items ), + vertices, vcount, indices, icount ); +} + +// ---------------------------------------------------------------------------- +size_t +vertex_buffer_insert( vertex_buffer_t * self, const size_t index, + const void * vertices, const size_t vcount, + const GLuint * indices, const size_t icount ) +{ + size_t vstart, istart, i; + ivec4 item; + assert( self ); + assert( vertices ); + assert( indices ); + + self->state = FROZEN; + + // Push back vertices + vstart = vector_size( self->vertices ); + vertex_buffer_push_back_vertices( self, vertices, vcount ); + + // Push back indices + istart = vector_size( self->indices ); + vertex_buffer_push_back_indices( self, indices, icount ); + + // Update indices within the vertex buffer + for( i=0; i<icount; ++i ) + { + *(GLuint *)(vector_get( self->indices, istart+i )) += vstart; + } + + // Insert item + item.x = vstart; + item.y = vcount; + item.z = istart; + item.w = icount; + vector_insert( self->items, index, &item ); + + self->state = DIRTY; + return index; +} + +// ---------------------------------------------------------------------------- +void +vertex_buffer_erase( vertex_buffer_t * self, + const size_t index ) +{ + ivec4 * item; + size_t vstart, vcount, istart, icount, i; + + assert( self ); + assert( index < vector_size( self->items ) ); + + item = (ivec4 *) vector_get( self->items, index ); + vstart = item->vstart; + vcount = item->vcount; + istart = item->istart; + icount = item->icount; + + // Update items + for( i=0; i<vector_size(self->items); ++i ) + { + ivec4 * item = (ivec4 *) vector_get( self->items, i ); + if( item->vstart > vstart) + { + item->vstart -= vcount; + item->istart -= icount; + } + } + + self->state = FROZEN; + vertex_buffer_erase_indices( self, istart, istart+icount ); + vertex_buffer_erase_vertices( self, vstart, vstart+vcount ); + vector_erase( self->items, index ); + self->state = DIRTY; +} diff --git a/dgl/src/freetype-gl/vertex-buffer.h b/dgl/src/freetype-gl/vertex-buffer.h @@ -0,0 +1,350 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +#ifndef __VERTEX_BUFFER_H__ +#define __VERTEX_BUFFER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "opengl.h" +#include "vector.h" +#include "vertex-attribute.h" + +/** + * @file vertex-buffer.h + * @author Nicolas Rougier (Nicolas.Rougier@inria.fr) + * @date April, 2012 + * + * @defgroup vertex-buffer Vertex buffer + * + * @{ + */ + + +/** + * Generic vertex buffer. + */ +typedef struct vertex_buffer_t +{ + /** Format of the vertex buffer. */ + char * format; + + /** Vector of vertices. */ + vector_t * vertices; + +#ifdef FREETYPE_GL_USE_VAO + /** GL identity of the Vertex Array Object */ + GLuint VAO_id; +#endif + + /** GL identity of the vertices buffer. */ + GLuint vertices_id; + + /** Vector of indices. */ + vector_t * indices; + + /** GL identity of the indices buffer. */ + GLuint indices_id; + + /** Current size of the vertices buffer in GPU */ + size_t GPU_vsize; + + /** Current size of the indices buffer in GPU*/ + size_t GPU_isize; + + /** GL primitives to render. */ + GLenum mode; + + /** Whether the vertex buffer needs to be uploaded to GPU memory. */ + char state; + + /** Individual items */ + vector_t * items; + + /** Array of attributes. */ + vertex_attribute_t *attributes[MAX_VERTEX_ATTRIBUTE]; +} vertex_buffer_t; + + +/** + * Creates an empty vertex buffer. + * + * @param format a string describing vertex format. + * @return an empty vertex buffer. + */ + vertex_buffer_t * + vertex_buffer_new( const char *format ); + + +/** + * Deletes vertex buffer and releases GPU memory. + * + * @param self a vertex buffer + */ + void + vertex_buffer_delete( vertex_buffer_t * self ); + + +/** + * Returns the number of items in the vertex buffer + * + * @param self a vertex buffer + * @return number of items + */ + size_t + vertex_buffer_size( const vertex_buffer_t *self ); + + +/** + * Returns vertex format + * + * @param self a vertex buffer + * @return vertex format + */ + const char * + vertex_buffer_format( const vertex_buffer_t *self ); + + +/** + * Print information about a vertex buffer + * + * @param self a vertex buffer + */ + void + vertex_buffer_print( vertex_buffer_t * self ); + + +/** + * Prepare vertex buffer for render. + * + * @param self a vertex buffer + * @param mode render mode + */ + void + vertex_buffer_render_setup ( vertex_buffer_t *self, + GLenum mode ); + + +/** + * Finish rendering by setting back modified states + * + * @param self a vertex buffer + */ + void + vertex_buffer_render_finish ( vertex_buffer_t *self ); + + +/** + * Render vertex buffer. + * + * @param self a vertex buffer + * @param mode render mode + */ + void + vertex_buffer_render ( vertex_buffer_t *self, + GLenum mode ); + + +/** + * Render a specified item from the vertex buffer. + * + * @param self a vertex buffer + * @param index index of the item to be rendered + */ + void + vertex_buffer_render_item ( vertex_buffer_t *self, + size_t index ); + + +/** + * Upload buffer to GPU memory. + * + * @param self a vertex buffer + */ + void + vertex_buffer_upload( vertex_buffer_t *self ); + + +/** + * Clear all items. + * + * @param self a vertex buffer + */ + void + vertex_buffer_clear( vertex_buffer_t *self ); + + +/** + * Appends indices at the end of the buffer. + * + * @param self a vertex buffer + * @param indices indices to be appended + * @param icount number of indices to be appended + * + * @private + */ + void + vertex_buffer_push_back_indices ( vertex_buffer_t *self, + const GLuint * indices, + const size_t icount ); + + +/** + * Appends vertices at the end of the buffer. + * + * @note Internal use + * + * @param self a vertex buffer + * @param vertices vertices to be appended + * @param vcount number of vertices to be appended + * + * @private + */ + void + vertex_buffer_push_back_vertices ( vertex_buffer_t *self, + const void * vertices, + const size_t vcount ); + + +/** + * Insert indices in the buffer. + * + * @param self a vertex buffer + * @param index location before which to insert indices + * @param indices indices to be appended + * @param icount number of indices to be appended + * + * @private + */ + void + vertex_buffer_insert_indices ( vertex_buffer_t *self, + const size_t index, + const GLuint *indices, + const size_t icount ); + + +/** + * Insert vertices in the buffer. + * + * @param self a vertex buffer + * @param index location before which to insert vertices + * @param vertices vertices to be appended + * @param vcount number of vertices to be appended + * + * @private + */ + void + vertex_buffer_insert_vertices ( vertex_buffer_t *self, + const size_t index, + const void *vertices, + const size_t vcount ); + +/** + * Erase indices in the buffer. + * + * @param self a vertex buffer + * @param first the index of the first index to be erased + * @param last the index of the last index to be erased + * + * @private + */ + void + vertex_buffer_erase_indices ( vertex_buffer_t *self, + const size_t first, + const size_t last ); + +/** + * Erase vertices in the buffer. + * + * @param self a vertex buffer + * @param first the index of the first vertex to be erased + * @param last the index of the last vertex to be erased + * + * @private + */ + void + vertex_buffer_erase_vertices ( vertex_buffer_t *self, + const size_t first, + const size_t last ); + + +/** + * Append a new item to the collection. + * + * @param self a vertex buffer + * @param vcount number of vertices + * @param vertices raw vertices data + * @param icount number of indices + * @param indices raw indices data + */ + size_t + vertex_buffer_push_back( vertex_buffer_t * self, + const void * vertices, const size_t vcount, + const GLuint * indices, const size_t icount ); + + +/** + * Insert a new item into the vertex buffer. + * + * @param self a vertex buffer + * @param index location before which to insert item + * @param vertices raw vertices data + * @param vcount number of vertices + * @param indices raw indices data + * @param icount number of indices + */ + size_t + vertex_buffer_insert( vertex_buffer_t * self, + const size_t index, + const void * vertices, const size_t vcount, + const GLuint * indices, const size_t icount ); + +/** + * Erase an item from the vertex buffer. + * + * @param self a vertex buffer + * @param index index of the item to be deleted + */ + void + vertex_buffer_erase( vertex_buffer_t * self, + const size_t index ); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* __VERTEX_BUFFER_H__ */ diff --git a/dgl/src/freetype/FreeTypeAmalgam.h b/dgl/src/freetype/FreeTypeAmalgam.h @@ -1033,7 +1033,7 @@ FT_BEGIN_HEADER /* This is done to allow FreeType clients to run unmodified, forcing */ /* them to display normal gray-level anti-aliased glyphs. */ /* */ - #define FT_CONFIG_OPTION_SUBPIXEL_RENDERING +/* #define FT_CONFIG_OPTION_SUBPIXEL_RENDERING */ /*************************************************************************/ /* */ diff --git a/examples/shaders/cube.frag b/examples/shaders/cube.frag @@ -0,0 +1,36 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +void main() +{ + gl_FragColor = gl_Color; +} diff --git a/examples/shaders/cube.vert b/examples/shaders/cube.vert @@ -0,0 +1,45 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +uniform vec4 Color; +attribute vec3 vertex; +attribute vec4 color; +void main() +{ + gl_FrontColor = color*Color; + gl_Position = gl_ModelViewProjectionMatrix * vec4(vertex,1.0); + //gl_Position = projection*(view*(model*vec4(vertex,1.0))); +} diff --git a/examples/shaders/distance-field-2.frag b/examples/shaders/distance-field-2.frag @@ -0,0 +1,43 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform sampler2D texture; +void main(void) +{ + vec4 color = texture2D(texture, gl_TexCoord[0].st); + float dist = color.r; + float width = fwidth(dist); + float alpha = smoothstep(0.5-width, 0.5+width, dist); + gl_FragColor = vec4(gl_Color.rgb, alpha*gl_Color.a); +} + + diff --git a/examples/shaders/distance-field-2.vert b/examples/shaders/distance-field-2.vert @@ -0,0 +1,47 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform sampler2D texture; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; +uniform vec4 Color; + +attribute vec3 vertex; +attribute vec2 tex_coord; +attribute vec4 color; +void main() +{ + gl_TexCoord[0].xy = tex_coord.xy; + gl_FrontColor = color*Color; + gl_Position = projection*(view*(model*vec4(vertex,1.0))); +} diff --git a/examples/shaders/distance-field-3.frag b/examples/shaders/distance-field-3.frag @@ -0,0 +1,43 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform sampler2D texture; +void main(void) +{ + vec3 color = vec3(0.0,0.0,0.0); + float dist = texture2D(texture, gl_TexCoord[0].st).r; + float width = fwidth(dist); + float alpha = smoothstep(0.5-width, 0.5+width, dist); + gl_FragColor = vec4(color, alpha); +} + + diff --git a/examples/shaders/distance-field.frag b/examples/shaders/distance-field.frag @@ -0,0 +1,70 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform sampler2D texture; + vec3 glyph_color = vec3(1.0,1.0,1.0); +const float glyph_center = 0.50; + vec3 outline_color = vec3(0.0,0.0,0.0); +const float outline_center = 0.55; + vec3 glow_color = vec3(1.0,1.0,1.0); +const float glow_center = 1.25; +void main(void) +{ + vec4 color = texture2D(texture, gl_TexCoord[0].st); + float dist = color.r; + float width = fwidth(dist); + float alpha = smoothstep(glyph_center-width, glyph_center+width, dist); + + // Smooth + // gl_FragColor = vec4(glyph_color, alpha); + + // Outline + // float mu = smoothstep(outline_center-width, outline_center+width, dist); + // vec3 rgb = mix(outline_color, glyph_color, mu); + // gl_FragColor = vec4(rgb, max(alpha,mu)); + + // Glow + //vec3 rgb = mix(glow_color, glyph_color, alpha); + //float mu = smoothstep(glyph_center, glow_center, sqrt(dist)); + //gl_FragColor = vec4(rgb, max(alpha,mu)); + + // Glow + outline + vec3 rgb = mix(glow_color, glyph_color, alpha); + float mu = smoothstep(glyph_center, glow_center, sqrt(dist)); + color = vec4(rgb, max(alpha,mu)); + float beta = smoothstep(outline_center-width, outline_center+width, dist); + rgb = mix(outline_color, color.rgb, beta); + gl_FragColor = vec4(rgb, max(color.a,beta)); + +} + + diff --git a/examples/shaders/distance-field.vert b/examples/shaders/distance-field.vert @@ -0,0 +1,40 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +void main(void) +{ + gl_FrontColor = gl_Color; + gl_TexCoord[0].xy = gl_MultiTexCoord0.xy; + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; +} + + diff --git a/examples/shaders/text.frag b/examples/shaders/text.frag @@ -0,0 +1,152 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ + + +vec3 +energy_distribution( vec4 previous, vec4 current, vec4 next ) +{ + float primary = 1.0/3.0; + float secondary = 1.0/3.0; + float tertiary = 0.0; + + // Energy distribution as explained on: + // http://www.grc.com/freeandclear.htm + // + // .. v.. + // RGB RGB RGB + // previous.g + previous.b + current.r + current.g + current.b + // + // . .v. . + // RGB RGB RGB + // previous.b + current.r + current.g + current.b + next.r + // + // ..v .. + // RGB RGB RGB + // current.r + current.g + current.b + next.r + next.g + + float r = + tertiary * previous.g + + secondary * previous.b + + primary * current.r + + secondary * current.g + + tertiary * current.b; + + float g = + tertiary * previous.b + + secondary * current.r + + primary * current.g + + secondary * current.b + + tertiary * next.r; + + float b = + tertiary * current.r + + secondary * current.g + + primary * current.b + + secondary * next.r + + tertiary * next.g; + + return vec3(r,g,b); +} + + +uniform sampler2D texture; +uniform vec3 pixel; +varying float vgamma; +varying float vshift; +void main() +{ + vec2 uv = gl_TexCoord[0].xy; + float shift = vshift; + + // LCD Off + if( pixel.z == 1.0) + { + float a = texture2D(texture, uv).r; + gl_FragColor = gl_Color * pow( a, 1.0/vgamma ); + return; + } + + // LCD On + vec4 current = texture2D(texture, uv); + vec4 previous= texture2D(texture, uv+vec2(-1.,0.)*pixel.xy); + vec4 next = texture2D(texture, uv+vec2(+1.,0.)*pixel.xy); + + current = pow(current, vec4(1.0/vgamma)); + previous= pow(previous, vec4(1.0/vgamma)); + + float r = current.r; + float g = current.g; + float b = current.b; + + if( shift <= 0.333 ) + { + float z = shift/0.333; + r = mix(current.r, previous.b, z); + g = mix(current.g, current.r, z); + b = mix(current.b, current.g, z); + } + else if( shift <= 0.666 ) + { + float z = (shift-0.33)/0.333; + r = mix(previous.b, previous.g, z); + g = mix(current.r, previous.b, z); + b = mix(current.g, current.r, z); + } + else if( shift < 1.0 ) + { + float z = (shift-0.66)/0.334; + r = mix(previous.g, previous.r, z); + g = mix(previous.b, previous.g, z); + b = mix(current.r, previous.b, z); + } + + float t = max(max(r,g),b); + vec4 color = vec4(gl_Color.rgb, (r+g+b)/3.0); + color = t*color + (1.0-t)*vec4(r,g,b, min(min(r,g),b)); + gl_FragColor = vec4( color.rgb, gl_Color.a*color.a); + + +// gl_FragColor = vec4(pow(vec3(r,g,b),vec3(1.0/vgamma)),a); + + /* + vec3 color = energy_distribution(previous, vec4(r,g,b,1), next); + color = pow( color, vec3(1.0/vgamma)); + + vec3 color = vec3(r,g,b); //pow( vec3(r,g,b), vec3(1.0/vgamma)); + gl_FragColor.rgb = color; //*gl_Color.rgb; + gl_FragColor.a = (color.r+color.g+color.b)/3.0 * gl_Color.a; + */ + +// gl_FragColor = vec4(pow(vec3(r,g,b),vec3(1.0/vgamma)),a); + //gl_FragColor = vec4(r,g,b,a); +} diff --git a/examples/shaders/text.vert b/examples/shaders/text.vert @@ -0,0 +1,55 @@ +/* ============================================================================ + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ---------------------------------------------------------------------------- + * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ============================================================================ + */ +uniform sampler2D texture; +uniform vec3 pixel; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + + +attribute vec3 vertex; +attribute vec4 color; +attribute vec2 tex_coord; +attribute float ashift; +attribute float agamma; +varying float vshift; +varying float vgamma; +void main() +{ + vshift = ashift; + vgamma = agamma; + gl_FrontColor = color; + gl_TexCoord[0].xy = tex_coord.xy; + gl_Position = projection*(view*(model*vec4(vertex,1.0))); +} diff --git a/examples/shaders/v3f-c4f.frag b/examples/shaders/v3f-c4f.frag @@ -0,0 +1,36 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +void main() +{ + gl_FragColor = gl_Color; +} diff --git a/examples/shaders/v3f-c4f.vert b/examples/shaders/v3f-c4f.vert @@ -0,0 +1,43 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +attribute vec3 vertex; +attribute vec4 color; +void main() +{ + gl_FrontColor = color; + gl_Position = projection*(view*(model*vec4(vertex,1.0))); +} diff --git a/examples/shaders/v3f-t2f-c4f.frag b/examples/shaders/v3f-t2f-c4f.frag @@ -0,0 +1,38 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform sampler2D texture; +void main() +{ + float a = texture2D(texture, gl_TexCoord[0].xy).r; + gl_FragColor = vec4(gl_Color.rgb, gl_Color.a*a); +} diff --git a/examples/shaders/v3f-t2f-c4f.vert b/examples/shaders/v3f-t2f-c4f.vert @@ -0,0 +1,45 @@ +/* ========================================================================= + * Freetype GL - A C OpenGL Freetype engine + * Platform: Any + * WWW: http://code.google.com/p/freetype-gl/ + * ------------------------------------------------------------------------- + * Copyright 2011 Nicolas P. Rougier. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Nicolas P. Rougier. + * ========================================================================= */ +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +attribute vec3 vertex; +attribute vec2 tex_coord; +attribute vec4 color; +void main() +{ + gl_TexCoord[0].xy = tex_coord.xy; + gl_FrontColor = color; + gl_Position = projection*(view*(model*vec4(vertex,1.0))); +} diff --git a/examples/text.cpp b/examples/text.cpp @@ -17,11 +17,31 @@ // ------------------------------------------------------ // DGL Stuff +#define GL_GLEXT_PROTOTYPES + #include "App.hpp" #include "Window.hpp" #include "Widget.hpp" +#include "src/freetype-gl/text-buffer.h" +#include "src/freetype-gl/mat4.h" + +#include <GL/glext.h> + #include <cstdio> +#include <cwchar> + +// ------------------------------------------------------ +extern "C" { + +int z_verbose = 0; + +void z_error (char* message) +{ + d_stderr2(message); +} + +} // ------------------------------------------------------ // use namespace @@ -35,21 +55,137 @@ class TextWidget : public Widget { public: TextWidget(Window& parent) - : Widget(parent) + : Widget(parent), +#if 1 + atlas(nullptr), + fontmgr(nullptr), + font(nullptr), +#endif + textbuf(nullptr) { + vec2 pen = {{20, 200}}; + + vec4 black = {{0.0, 0.0, 0.0, 1.0}}; + vec4 white = {{1.0, 1.0, 1.0, 1.0}}; + vec4 none = {{1.0, 1.0, 1.0, 0.0}}; + + markup_t markup = { + "normal", + 24.0f, 0, 0, + 0.0, 0.0, 2.0f, + white, none, + 0, white, + 0, white, + 0, white, + 0, white, + 0 + }; + + wchar_t* text = L"A Quick Brown Fox Jumps Over The Lazy Dog 0123456789"; + +#if 1 + atlas = texture_atlas_new(600, 300, 2); + DISTRHO_SAFE_ASSERT_RETURN(atlas != nullptr,); + + //fontmgr = font_manager_new(600, 200, 2); + //DISTRHO_SAFE_ASSERT_RETURN(fontmgr != nullptr,); + + //font = font_manager_get_from_filename(fontmgr, "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 12.0f); + //DISTRHO_SAFE_ASSERT_RETURN(font != nullptr,); + + font = texture_font_new_from_file(atlas, 12.0f, "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"); + DISTRHO_SAFE_ASSERT_RETURN(font != nullptr,); +#endif + + textbuf = text_buffer_new(LCD_FILTERING_OFF); + DISTRHO_SAFE_ASSERT_RETURN(textbuf != nullptr,); + + textbuf->base_color = black; + + //markup.family = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"; + markup.font = font; + + text_buffer_printf(textbuf, &pen, + &markup, text, nullptr); + + text_buffer_add_text(textbuf, &pen, &markup, text, std::wcslen(text)); + + mat4_set_identity(&projection); + mat4_set_identity(&model); + mat4_set_identity(&view); + } + + ~TextWidget() + { + if (textbuf != nullptr) + { + text_buffer_delete(textbuf); + textbuf = nullptr; + } + +#if 1 + if (font != nullptr) + { + texture_font_delete(font); + font = nullptr; + } + + if (fontmgr != nullptr) + { + font_manager_delete(fontmgr); + fontmgr = nullptr; + } + + if (atlas != nullptr) + { + texture_atlas_delete(atlas); + atlas = nullptr; + } +#endif } private: void onDisplay() override { + DISTRHO_SAFE_ASSERT_RETURN(textbuf != nullptr,); + + glClearColor(0.4f, 0.4f, 0.45f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + + glUseProgram(textbuf->shader); + { + glUniformMatrix4fv(glGetUniformLocation(textbuf->shader, "model"), 1, 0, model.data); + glUniformMatrix4fv(glGetUniformLocation(textbuf->shader, "view"), 1, 0, view.data); + glUniformMatrix4fv(glGetUniformLocation(textbuf->shader, "projection"), 1, 0, projection.data); + + text_buffer_render(textbuf); + } } void onReshape(int width, int height) override { // make widget same size as window setSize(width, height); - Widget::onReshape(width, height); + //Widget::onReshape(width, height); + + //mat4_set_identity(&projection); + //mat4_set_identity(&model); + //mat4_set_identity(&view); + + glViewport(0, 0, width, height); + mat4_set_orthographic(&projection, 0, width, 0, height, width, height); + //mat4_set_orthographic(&projection, 0, width, 0, height, -1, 1); } + + texture_atlas_t* atlas; + font_manager_t* fontmgr; + texture_font_t* font; + + text_buffer_t* textbuf; + + mat4 model, view, projection; }; // ------------------------------------------------------