nametag rendering
[carveJwlIkooP6JGAAIwe30JlM.git] / font.h
1 #ifndef FONT_H
2 #define FONT_H
3
4 #include "model.h"
5 #include "entity.h"
6 #include "camera.h"
7 #include "shaders/model_font.h"
8 #include "shaders/scene_font.h"
9 #include "world_render.h"
10
11 enum efont_SRglyph{
12 k_SRglyph_end = 0x00, /* control characters */
13 k_SRglyph_ctrl_variant = 0x01,
14 k_SRglyph_ctrl_size = 0x02, /* normalized 0-1 */
15 k_SRglyph_ctrl_center = 0x03, /* useful when text is scaled down */
16 k_SRglyph_ctrl_baseline = 0x04, /* . */
17 k_SRglyph_ctrl_top = 0x05, /* . */
18 k_SRglyph_mod_circle = 0x1e, /* surround and center next charater */
19 k_SRglyph_mod_square = 0x1f, /* surround and center next character */
20 k_SRglyph_ascii_min = 0x20, /* standard ascii */
21 k_SRglyph_ascii_max = 0x7e,
22 k_SRglyph_ps4_square = 0x7f,/* playstation buttons */
23 k_SRglyph_ps4_triangle = 0x80,
24 k_SRglyph_ps4_circle = 0x81,
25 k_SRglyph_ps4_cross = 0x82,
26 k_SRglyph_xb1_x = 0x83,/* xbox buttons */
27 k_SRglyph_xb1_y = 0x84,
28 k_SRglyph_xb1_a = 0x85,
29 k_SRglyph_xb1_b = 0x86,
30 k_SRglyph_gen_ls = 0x87,/* generic gamepad */
31 k_SRglyph_gen_lsh = 0x88,
32 k_SRglyph_gen_lsv = 0x89,
33 k_SRglyph_gen_lshv = 0x8a,
34 k_SRglyph_gen_rs = 0x8b,
35 k_SRglyph_gen_rsh = 0x8c,
36 k_SRglyph_gen_rsv = 0x8d,
37 k_SRglyph_gen_rshv = 0x8e,
38 k_SRglyph_gen_lt = 0x8f,
39 k_SRglyph_gen_rt = 0x90,
40 k_SRglyph_gen_lb = 0x91,
41 k_SRglyph_gen_rb = 0x92,
42 k_SRglyph_gen_left = 0x93,
43 k_SRglyph_gen_up = 0x94,
44 k_SRglyph_gen_right = 0x95,
45 k_SRglyph_gen_down = 0x96,
46 k_SRglyph_gen_options = 0x97,
47 k_SRglyph_gen_shareview = 0x98,
48 k_SRglyph_kbm_m0 = 0x99,/* mouse */
49 k_SRglyph_kbm_m1 = 0x9a,
50 k_SRglyph_kbm_m01 = 0x9b,
51 k_SRglyph_kbm_m2 = 0x9c,
52 k_SRglyph_kbm_m2s = 0x9d,
53 k_SRglyph_kbm_shift = 0x9e,/* modifiers */
54 k_SRglyph_kbm_ctrl = 0x9f,
55 k_SRglyph_kbm_alt = 0xa0,
56 k_SRglyph_kbm_space = 0xa1,
57 k_SRglyph_kbm_return = 0xa2,
58 k_SRglyph_kbm_escape = 0xa3,
59 k_SRglyph_kbm_mousemove = 0xa4
60 };
61
62 typedef struct font3d font3d;
63 struct font3d{
64 mdl_context mdl;
65 GLuint texture;
66 glmesh mesh;
67
68 ent_font info;
69 mdl_array_ptr font_variants,
70 glyphs;
71 };
72
73 static void font3d_load( font3d *font, const char *mdl_path, void *alloc ){
74 mdl_open( &font->mdl, mdl_path, alloc );
75 mdl_load_metadata_block( &font->mdl, alloc );
76
77 vg_linear_clear( vg_mem.scratch );
78 mdl_array_ptr fonts;
79 mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
80 font->info = *((ent_font *)mdl_arritm(&fonts,0));
81
82 mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
83 mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
84
85 vg_linear_clear( vg_mem.scratch );
86
87 if( !mdl_arrcount( &font->mdl.textures ) )
88 vg_fatal_error( "No texture in font file" );
89
90 mdl_texture *tex0 = mdl_arritm( &font->mdl.textures, 0 );
91 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
92 mdl_fread_pack_file( &font->mdl, &tex0->file, data );
93
94 mdl_async_load_glmesh( &font->mdl, &font->mesh );
95 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
96 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
97 &font->texture );
98
99 mdl_close( &font->mdl );
100 }
101
102 static void font3d_init(void){
103 shader_model_font_register();
104 shader_scene_font_register();
105 }
106
107 static u32 font3d_find_variant( font3d *font, const char *name ){
108 for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
109 ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
110
111 if( !strcmp( mdl_pstr( &font->mdl, variant->name ), name ) ){
112 return i;
113 }
114 }
115
116 return 0;
117 }
118
119 struct _font3d_render{
120 v4f offset;
121 font3d *font;
122 u32 variant_id;
123
124 enum font_shader {
125 k_font_shader_default,
126 k_font_shader_world
127 }
128 shader;
129 }
130 static gui_font3d;
131
132 /*
133 * world can be null if not using world shader
134 */
135 static void font3d_bind( font3d *font, enum font_shader shader,
136 int depth_compare, world_instance *world,
137 camera *cam ){
138 gui_font3d.shader = shader;
139 gui_font3d.font = font;
140 glActiveTexture( GL_TEXTURE1 );
141 glBindTexture( GL_TEXTURE_2D, font->texture );
142
143 if( shader == k_font_shader_default ){
144 shader_model_font_use();
145 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
146 shader_model_font_uTexMain( 1 );
147
148 shader_model_font_uDepthCompare( depth_compare );
149 if( depth_compare ){
150 /* TODO: Compress with code in player_render.c */
151 shader_model_font_uTexSceneDepth( 2 );
152 render_fb_bind_texture( gpipeline.fb_main, 2, 2 );
153 v3f inverse;
154 render_fb_inverse_ratio( gpipeline.fb_main, inverse );
155 inverse[2] = skaterift.cam.farz-skaterift.cam.nearz;
156
157 shader_model_font_uInverseRatioDepth( inverse );
158 render_fb_inverse_ratio( NULL, inverse );
159 inverse[2] = cam->farz-cam->nearz;
160 shader_model_font_uInverseRatioMain( inverse );
161 }
162
163 shader_model_font_uPv( cam->mtx.pv );
164 }
165 else if( shader == k_font_shader_world ){
166 assert( world );
167 shader_scene_font_use();
168 shader_scene_font_uTexGarbage(0);
169 shader_scene_font_uTexMain(1);
170
171 shader_scene_font_uPv( skaterift.cam.mtx.pv );
172 shader_scene_font_uTime( vg.time );
173
174 /* TODO: Code dupe... */
175 world_link_lighting_ub( world, _shader_scene_font.id );
176 world_bind_position_texture( world, _shader_scene_font.id,
177 _uniform_scene_font_g_world_depth, 2 );
178 world_bind_light_array( world, _shader_scene_font.id,
179 _uniform_scene_font_uLightsArray, 3 );
180 world_bind_light_index( world, _shader_scene_font.id,
181 _uniform_scene_font_uLightsIndex, 4 );
182
183 bind_terrain_noise();
184 shader_scene_font_uCamera( skaterift.cam.transform[3] );
185 }
186 mesh_bind( &font->mesh );
187 }
188
189 static ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 ){
190 if( utf32 < font->info.glyph_utf32_base ) return NULL;
191 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
192
193 u32 index = utf32 - font->info.glyph_utf32_base;
194 index += font->info.glyph_start;
195 index += font->info.glyph_count * variant_id;
196 return mdl_arritm( &font->glyphs, index );
197 }
198
199 static void font3d_set_transform( const char *text,
200 camera *cam, m4x3f transform ){
201 v4_copy( (v4f){0.0f,0.0f,0.0f,1.0f}, gui_font3d.offset );
202
203 m4x4f prev_mtx;
204 m4x3_expand( transform, prev_mtx );
205 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
206
207 if( gui_font3d.shader == k_font_shader_default ){
208 shader_model_font_uPvmPrev( prev_mtx );
209 shader_model_font_uMdl( transform );
210 }
211 else if( gui_font3d.shader == k_font_shader_world ){
212 shader_scene_font_uPvmPrev( prev_mtx );
213 shader_scene_font_uMdl( transform );
214 }
215 }
216
217 static void font3d_setoffset( v4f offset ){
218 if( gui_font3d.shader == k_font_shader_default )
219 shader_model_font_uOffset( offset );
220 else if( gui_font3d.shader == k_font_shader_world )
221 shader_scene_font_uOffset( offset );
222 }
223
224 static void font3d_setcolour( v4f colour ){
225 if( gui_font3d.shader == k_font_shader_default )
226 shader_model_font_uColour( colour );
227 #if 0
228 else if( gui_font3d.shader == k_font_shader_world )
229 shader_scene_font_uColour( colour );
230 #endif
231 }
232
233 static void font3d_draw( const char *text ){
234 u8 *u8pch = (u8*)text;
235
236 u32 max_chars = 512;
237 while( u8pch && max_chars ){
238 max_chars --;
239
240 u32 c0 = *u8pch, c1;
241 u8pch ++;
242
243 if( !c0 ) break;
244
245 ent_glyph *glyph0 = font3d_glyph( gui_font3d.font,
246 gui_font3d.variant_id, c0 ),
247 *glyph1 = NULL;
248
249 /* multibyte characters */
250 if( c0 >= 1 && c0 < k_SRglyph_ascii_min ){
251 c1 = *u8pch;
252 if( !c1 ) break;
253 glyph1 = font3d_glyph( gui_font3d.font, gui_font3d.variant_id, c1 );
254 }
255
256 if( c0 == k_SRglyph_ctrl_variant ){
257 gui_font3d.variant_id = c1;
258 u8pch ++;
259 continue;
260 }
261 else if( c0 == k_SRglyph_ctrl_size ){
262 gui_font3d.offset[3] = (float)c1 * (1.0f/255.0f);
263 u8pch ++;
264 continue;
265 }
266 else if( c0 == k_SRglyph_ctrl_baseline ){
267 gui_font3d.offset[1] = 0.0f;
268 continue;
269 }
270 else if( c0 == k_SRglyph_ctrl_center ){
271 if( glyph1 ){
272 float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
273 gui_font3d.offset[1] = diff * 0.5f;
274 }
275 continue;
276 }
277 else if( c0 == k_SRglyph_ctrl_top ){
278 if( glyph1 ){
279 float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
280 gui_font3d.offset[1] = diff;
281 }
282 continue;
283 }
284
285 if( !glyph0 ) continue;
286
287 if( glyph1 && (c0 == k_SRglyph_mod_square || c0 == k_SRglyph_mod_circle)){
288 v4f v0;
289 v2_sub( glyph0->size, glyph1->size, v0 );
290 v2_muladds( gui_font3d.offset, v0, -0.5f, v0 );
291 v0[2] = gui_font3d.offset[2];
292 v0[3] = gui_font3d.offset[3];
293
294 font3d_setoffset( v0 );
295 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
296 continue;
297 }
298 else{
299 font3d_setoffset( gui_font3d.offset );
300 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
301 }
302
303 gui_font3d.offset[0] += glyph0->size[0]*gui_font3d.offset[3];
304 }
305 }
306
307 static f32 font3d_simple_draw( u32 variant_id, const char *text,
308 camera *cam, m4x3f transform ){
309 if( !text ) return 0.0f;
310
311 gui_font3d.variant_id = variant_id;
312 font3d_set_transform( text, cam, transform );
313 font3d_draw( text );
314 return gui_font3d.offset[0];
315 }
316
317 static f32 font3d_string_width( u32 variant_id, const char *text ){
318 if( !text ) return 0.0f;
319 float width = 0.0f;
320
321 const u8 *buf = (const u8 *)text;
322 for( int i=0;; i++ ){
323 u32 c = buf[i];
324 if(!c) break;
325
326 ent_glyph *glyph = font3d_glyph( gui_font3d.font, variant_id, c );
327 if( !glyph ) continue;
328
329 width += glyph->size[0];
330 }
331
332 return width;
333 }
334
335 #endif /* FONT_H */