7 #include "shaders/model_font.h"
11 k_SRglyph_end
= 0x00, /* control characters */
12 k_SRglyph_ctrl_variant
= 0x01,
13 k_SRglyph_ctrl_size
= 0x02, /* normalized 0-1 */
14 k_SRglyph_ctrl_center
= 0x03, /* useful when text is scaled down */
15 k_SRglyph_ctrl_baseline
= 0x04, /* . */
16 k_SRglyph_ctrl_top
= 0x05, /* . */
17 k_SRglyph_mod_circle
= 0x1e, /* surround and center next charater */
18 k_SRglyph_mod_square
= 0x1f, /* surround and center next character */
19 k_SRglyph_ascii_min
= 0x20, /* standard ascii */
20 k_SRglyph_ascii_max
= 0x7e,
21 k_SRglyph_ps4_square
= 0x7f,/* playstation buttons */
22 k_SRglyph_ps4_triangle
= 0x80,
23 k_SRglyph_ps4_circle
= 0x81,
24 k_SRglyph_ps4_cross
= 0x82,
25 k_SRglyph_xb1_x
= 0x83,/* xbox buttons */
26 k_SRglyph_xb1_y
= 0x84,
27 k_SRglyph_xb1_a
= 0x85,
28 k_SRglyph_xb1_b
= 0x86,
29 k_SRglyph_gen_ls
= 0x87,/* generic gamepad */
30 k_SRglyph_gen_lsh
= 0x88,
31 k_SRglyph_gen_lsv
= 0x89,
32 k_SRglyph_gen_lshv
= 0x8a,
33 k_SRglyph_gen_rs
= 0x8b,
34 k_SRglyph_gen_rsh
= 0x8c,
35 k_SRglyph_gen_rsv
= 0x8d,
36 k_SRglyph_gen_rshv
= 0x8e,
37 k_SRglyph_gen_lt
= 0x8f,
38 k_SRglyph_gen_rt
= 0x90,
39 k_SRglyph_gen_lb
= 0x91,
40 k_SRglyph_gen_rb
= 0x92,
41 k_SRglyph_gen_left
= 0x93,
42 k_SRglyph_gen_up
= 0x94,
43 k_SRglyph_gen_right
= 0x95,
44 k_SRglyph_gen_down
= 0x96,
45 k_SRglyph_gen_options
= 0x97,
46 k_SRglyph_gen_shareview
= 0x98,
47 k_SRglyph_kbm_m0
= 0x99,/* mouse */
48 k_SRglyph_kbm_m1
= 0x9a,
49 k_SRglyph_kbm_m01
= 0x9b,
50 k_SRglyph_kbm_m2
= 0x9c,
51 k_SRglyph_kbm_m2s
= 0x9d,
52 k_SRglyph_kbm_shift
= 0x9e,/* modifiers */
53 k_SRglyph_kbm_ctrl
= 0x9f,
54 k_SRglyph_kbm_alt
= 0xa0,
55 k_SRglyph_kbm_space
= 0xa1,
56 k_SRglyph_kbm_return
= 0xa2
59 typedef struct font3d font3d
;
66 mdl_array_ptr font_variants
,
70 VG_STATIC
void font3d_load( font3d
*font
, const char *mdl_path
, void *alloc
)
72 mdl_open( &font
->mdl
, mdl_path
, alloc
);
73 mdl_load_metadata_block( &font
->mdl
, alloc
);
75 vg_linear_clear( vg_mem
.scratch
);
77 mdl_load_array( &font
->mdl
, &fonts
, "ent_font", vg_mem
.scratch
);
78 font
->info
= *((ent_font
*)mdl_arritm(&fonts
,0));
80 mdl_load_array( &font
->mdl
, &font
->font_variants
, "ent_font_variant", alloc
);
81 mdl_load_array( &font
->mdl
, &font
->glyphs
, "ent_glyph", alloc
);
83 vg_linear_clear( vg_mem
.scratch
);
85 if( !mdl_arrcount( &font
->mdl
.textures
) )
86 vg_fatal_error( "No texture in font file" );
88 mdl_texture
*tex0
= mdl_arritm( &font
->mdl
.textures
, 0 );
89 void *data
= vg_linear_alloc( vg_mem
.scratch
, tex0
->file
.pack_size
);
90 mdl_fread_pack_file( &font
->mdl
, &tex0
->file
, data
);
92 mdl_async_load_glmesh( &font
->mdl
, &font
->mesh
);
93 vg_tex2d_load_qoi_async( data
, tex0
->file
.pack_size
,
94 VG_TEX2D_LINEAR
|VG_TEX2D_CLAMP
,
97 mdl_close( &font
->mdl
);
100 VG_STATIC
void font3d_init(void)
102 shader_model_font_register();
105 VG_STATIC u32
font3d_find_variant( font3d
*font
, const char *name
)
107 for( u32 i
=0; i
<mdl_arrcount( &font
->font_variants
); i
++ ){
108 ent_font_variant
*variant
= mdl_arritm( &font
->font_variants
, i
);
110 if( !strcmp( mdl_pstr( &font
->mdl
, variant
->name
), name
) ){
118 VG_STATIC
void font3d_bind( font3d
*font
, camera
*cam
)
120 shader_model_font_use();
121 shader_model_font_uColour( (v4f
){1.0f
,1.0f
,1.0f
,1.0f
} );
122 shader_model_font_uTexMain( 1 );
123 glActiveTexture( GL_TEXTURE1
);
124 glBindTexture( GL_TEXTURE_2D
, font
->texture
);
126 shader_model_font_uPv( cam
->mtx
.pv
);
127 mesh_bind( &font
->mesh
);
130 VG_STATIC ent_glyph
*font3d_glyph( font3d
*font
, u32 variant_id
, u32 utf32
)
132 if( utf32
< font
->info
.glyph_utf32_base
) return NULL
;
133 if( utf32
>= font
->info
.glyph_utf32_base
+font
->info
.glyph_count
) return NULL
;
135 u32 index
= utf32
- font
->info
.glyph_utf32_base
;
136 index
+= font
->info
.glyph_start
;
137 index
+= font
->info
.glyph_count
* variant_id
;
138 return mdl_arritm( &font
->glyphs
, index
);
142 float font3d_simple_draw( font3d
*font
, u32 variant_id
, const char *text
,
143 camera
*cam
, m4x3f transform
)
146 q_identity( offset
);
150 m4x3_expand( transform
, prev_mtx
);
151 m4x4_mul( cam
->mtx_prev
.pv
, prev_mtx
, prev_mtx
);
153 shader_model_font_uPvmPrev( prev_mtx
);
154 shader_model_font_uMdl( transform
);
156 const u8
*u8str
= (u8
*)text
;
158 for( u32 i
=0;; i
++ ){
164 ent_glyph
*glyph0
= font3d_glyph( font
, variant_id
, c0
),
167 /* multibyte characters */
168 if( c0
>= 1 && c0
< k_SRglyph_ascii_min
){
171 glyph1
= font3d_glyph( font
, variant_id
, c1
);
174 if( c0
== k_SRglyph_ctrl_variant
){
179 else if( c0
== k_SRglyph_ctrl_size
){
180 offset
[3] = (float)c1
* (1.0f
/255.0f
);
184 else if( c0
== k_SRglyph_ctrl_baseline
){
188 else if( c0
== k_SRglyph_ctrl_center
){
190 offset
[1] = (glyph1
->size
[1] - glyph1
->size
[1]*offset
[3]) * 0.5f
;
193 else if( c0
== k_SRglyph_ctrl_top
){
195 offset
[1] = glyph1
->size
[1] - glyph1
->size
[1]*offset
[3];
199 if( !glyph0
) continue;
201 if( glyph1
&& (c0
== k_SRglyph_mod_square
|| c0
== k_SRglyph_mod_circle
)){
203 v2_sub( glyph0
->size
, glyph1
->size
, v0
);
204 v2_muladds( offset
, v0
, -0.5f
, v0
);
208 shader_model_font_uOffset( v0
);
209 mesh_drawn( glyph0
->indice_start
, glyph0
->indice_count
);
213 shader_model_font_uOffset( offset
);
214 mesh_drawn( glyph0
->indice_start
, glyph0
->indice_count
);
217 offset
[0] += glyph0
->size
[0]*offset
[3];
224 float font3d_string_width( font3d
*font
, u32 variant_id
, const char *text
)
227 for( int i
=0;; i
++ ){
231 ent_glyph
*glyph
= font3d_glyph( font
, variant_id
, c
);
232 if( !glyph
) continue;
234 width
+= glyph
->size
[0];