7 #include "shaders/model_font.h"
9 typedef struct font3d font3d
;
16 mdl_array_ptr font_variants
,
20 VG_STATIC
void font3d_load( font3d
*font
, const char *mdl_path
, void *alloc
)
22 mdl_open( &font
->mdl
, mdl_path
, alloc
);
23 mdl_load_metadata_block( &font
->mdl
, alloc
);
25 vg_linear_clear( vg_mem
.scratch
);
27 mdl_load_array( &font
->mdl
, &fonts
, "ent_font", vg_mem
.scratch
);
28 font
->info
= *((ent_font
*)mdl_arritm(&fonts
,0));
30 mdl_load_array( &font
->mdl
, &font
->font_variants
, "ent_font_variant", alloc
);
31 mdl_load_array( &font
->mdl
, &font
->glyphs
, "ent_glyph", alloc
);
33 vg_linear_clear( vg_mem
.scratch
);
34 mdl_load_mesh_block( &font
->mdl
, vg_mem
.scratch
);
35 mdl_load_pack_block( &font
->mdl
, vg_mem
.scratch
);
36 mdl_close( &font
->mdl
);
38 vg_acquire_thread_sync();
41 mesh_upload( &font
->mesh
,
42 font
->mdl
.verts
.data
, font
->mdl
.verts
.count
,
43 font
->mdl
.indices
.data
, font
->mdl
.indices
.count
);
45 /* upload first texture */
46 font
->texture
= vg_tex2d_new();
47 mdl_texture
*tex0
= mdl_arritm( &font
->mdl
.textures
, 0 );
50 vg_tex2d_qoi( mdl_arritm( &font
->mdl
.pack
, tex0
->file
.pack_offset
),
52 mdl_pstr( &font
->mdl
, tex0
->file
.pstr_path
));
56 vg_release_thread_sync();
59 VG_STATIC
void font3d_init(void)
61 shader_model_font_register();
64 VG_STATIC u32
font3d_find_variant( font3d
*font
, const char *name
)
66 for( u32 i
=0; i
<mdl_arrcount( &font
->font_variants
); i
++ ){
67 ent_font_variant
*variant
= mdl_arritm( &font
->font_variants
, i
);
69 if( !strcmp( mdl_pstr( &font
->mdl
, variant
->name
), name
) ){
77 VG_STATIC
void font3d_bind( font3d
*font
, camera
*cam
)
79 shader_model_font_use();
80 shader_model_font_uColour( (v4f
){1.0f
,1.0f
,1.0f
,1.0f
} );
81 shader_model_font_uTexMain( 1 );
82 glActiveTexture( GL_TEXTURE1
);
83 glBindTexture( GL_TEXTURE_2D
, font
->texture
);
85 shader_model_font_uPv( cam
->mtx
.pv
);
86 mesh_bind( &font
->mesh
);
89 VG_STATIC ent_glyph
*font3d_glyph( font3d
*font
, u32 variant_id
, u32 utf32
)
91 if( utf32
< font
->info
.glyph_utf32_base
) return NULL
;
92 if( utf32
>= font
->info
.glyph_utf32_base
+font
->info
.glyph_count
) return NULL
;
94 u32 index
= utf32
- font
->info
.glyph_utf32_base
;
95 index
+= font
->info
.glyph_start
;
96 index
+= font
->info
.glyph_count
* variant_id
;
97 return mdl_arritm( &font
->glyphs
, index
);
101 void font3d_simple_draw( font3d
*font
, u32 variant_id
, const char *text
,
102 camera
*cam
, m4x3f transform
)
109 m4x3_expand( transform
, prev_mtx
);
110 m4x4_mul( cam
->mtx_prev
.pv
, prev_mtx
, prev_mtx
);
112 shader_model_font_uPvmPrev( prev_mtx
);
113 shader_model_font_uMdl( transform
);
115 for( int i
=0;; i
++ ){
119 ent_glyph
*glyph
= font3d_glyph( font
, variant_id
, c
);
120 if( !glyph
) continue;
122 if( glyph
->indice_count
){
123 shader_model_font_uOffset( offset
);
124 mesh_drawn( glyph
->indice_start
, glyph
->indice_count
);
126 offset
[0] += glyph
->size
[0];
131 float font3d_string_width( font3d
*font
, u32 variant_id
, const char *text
)
134 for( int i
=0;; i
++ ){
138 ent_glyph
*glyph
= font3d_glyph( font
, variant_id
, c
);
139 if( !glyph
) continue;
141 width
+= glyph
->size
[0];