Rendering first pass
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 //#define VG_STEAM
4 #include "vg/vg.h"
5
6 #define CELL_SHEET_X 512
7 #define CELL_SHEET_Y 512
8
9 SHADER_DEFINE( colour_shader,
10
11 // VERTEX
12 "layout (location=0) in vec2 a_co;"
13 "uniform mat4 uPv;"
14 "uniform mat4 uMdl;"
15 ""
16 "void main()"
17 "{"
18 " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
19 " gl_Position = vert_pos;"
20 "}",
21
22 // FRAGMENT
23 "out vec4 FragColor;"
24 "uniform vec4 uColour;"
25 ""
26 "void main()"
27 "{"
28 " FragColor = uColour;"
29 "}"
30 ,
31 UNIFORMS({ "uPv", "uMdl", "uColour" })
32 )
33
34 SHADER_DEFINE( tilemap_shader,
35
36 // VERTEX
37 "layout (location=0) in vec2 a_co;" // XY
38 "layout (location=1) in vec2 a_offset;" // XY offset
39 "layout (location=2) in vec4 a_data;" // atlas-uv, amt, other
40 "uniform mat4 uPv;"
41 "uniform vec2 uOrigin;"
42 ""
43 "out vec2 aCoords;"
44 ""
45 "void main()"
46 "{"
47 "vec2 world_coord = a_co+a_offset+uOrigin;"
48 "gl_Position = uPv * vec4( world_coord.x, 0.0, world_coord.y, 1.0 );"
49 "aCoords = (a_co*0.98+0.01 + a_data.xy) * 0.0625;"
50 "}",
51
52 // FRAGMENT
53 "uniform sampler2D uTexTiles;"
54 "uniform sampler2D uTexRipples;"
55 "uniform float uTime;"
56
57 "in vec2 aCoords;"
58 "out vec4 FragColor;"
59
60 "void main()"
61 "{"
62 "vec4 glyph = texture( uTexTiles, aCoords );"
63 "vec4 ripple = texture( uTexRipples, vec2( glyph.x - uTime, glyph.y ));"
64 "float wstatus = -(fract(uTime)*2.0-1.0);" // -1 := None in, 0.0 := Hold middle, 1.0 := All out
65 "float dev = step( 0.0, glyph.x+wstatus ) * step( glyph.x+wstatus, 1.0 );" //+ abs(glyph.y-0.5) );"
66 "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.1, step( 0.75, glyph.a )*dev ) * glyph.z;"
67 "FragColor = vec4( composite, 1.0 );"
68 "}"
69 ,
70 UNIFORMS({ "uPv", "uTexTiles", "uTexRipples", "uTime", "uOrigin" })
71 )
72
73 mat4 m_projection;
74 mat4 m_view;
75 mat4 m_mdl;
76
77 int main( int argc, char *argv[] )
78 {
79 vg_init( argc, argv, "FishLadder" );
80 }
81
82 #define CELL_FLAG_INPUT 0x1
83 #define CELL_FLAG_OUTPUT 0x2
84 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
85 #define CELL_FLAG_WALL 0x4
86 #define CELL_FLAG_HOVER 0x8
87 #define CELL_FLAG_ITER 0x10
88 #define CELL_FLAG_CANAL 0x20
89 #define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
90 #define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
91 #define CELL_FLAG_VISITED 0x80
92 #define CELL_FLAG_UPLVL 0x100
93 #define CELL_FLAG_MERGE 0x200
94
95 static struct
96 {
97 u32 x,y;
98
99 struct cell
100 {
101 u32 flags;
102 u32 model_id;
103
104 char *conditions;
105
106 int level;
107 int state;
108 }
109 * cells;
110
111 struct fish
112 {
113 int alive;
114 int co[2];
115 int dir[2];
116 char data;
117 }
118 fishes[ 20 ];
119 int num_fishes;
120
121 vec3 origin;
122 struct cell *selected;
123 int select_valid;
124 int playing;
125 u32 frame;
126
127 u32 *io;
128
129 struct vstack
130 {
131 struct vframe
132 {
133 int x, y;
134 int i;
135 }
136 frames[ 64 ];
137
138 int level;
139 u32 flags;
140 }
141 stack;
142
143 GLuint tile_texture;
144 GLuint flow_texture;
145
146 GLuint tiles_vao;
147 GLuint tiles_vbo;
148 }
149 map;
150
151 static void map_free(void)
152 {
153 for( int i = 0; i < arrlen( map.io ); i ++ )
154 {
155 arrfree( map.cells[ map.io[i] ].conditions );
156 }
157
158 arrfree( map.cells );
159 arrfree( map.io );
160 map.x = 0;
161 map.y = 0;
162 map.cells = NULL;
163 map.io = NULL;
164 }
165
166 static struct cell *map_tile( int pos[2] )
167 {
168 return map.cells + pos[1]*map.x + pos[0];
169 }
170
171 static struct cell *map_tile_at( int pos[2] )
172 {
173 if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
174 return map.cells + pos[1]*map.x + pos[0];
175 return NULL;
176 }
177
178 static struct cell *map_tile_at_cond( int pos[2], u32 flags )
179 {
180 struct cell *cell = map_tile_at( pos );
181 if( cell && (cell->flags & flags) )
182 return cell;
183
184 return NULL;
185 }
186
187 static void map_tile_coords_from_index( int i, int coords[2] )
188 {
189 coords[0] = i % map.x;
190 coords[1] = (i - coords[0])/map.x;
191 }
192
193 static void map_stack_refresh(void)
194 {
195 for( int i = 0; i < map.x*map.y; i ++ )
196 map.cells[i].flags &= ~CELL_FLAG_VISITED;
197 }
198
199 static void map_stack_init( int coords[2] )
200 {
201 map.stack.level = 0;
202 map.stack.frames[0].i = 0;
203 map.stack.frames[0].x = coords[0];
204 map.stack.frames[0].y = coords[1];
205 }
206
207 static struct cell *map_stack_next(void)
208 {
209 struct cell *tile = NULL;
210
211 while( !tile )
212 {
213 struct vframe *frame = &map.stack.frames[ map.stack.level ];
214
215 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
216
217 if( frame->i < 3 )
218 {
219 int *dir = output_dirs[ frame->i ];
220 tile = map_tile_at( (int[2]){frame->x+dir[0], frame->y+dir[1]} );
221 frame->i ++;
222
223 if( tile && !(tile->flags & CELL_FLAG_VISITED) )
224 {
225 map.stack.level ++;
226 frame[1].i = 0;
227 frame[1].x = frame[0].x+dir[0];
228 frame[1].y = frame[0].y+dir[1];
229 }
230 else
231 tile = NULL;
232 }
233 else
234 {
235 map.stack.level --;
236 tile = NULL;
237
238 if( map.stack.level < 0 )
239 return NULL;
240 }
241 }
242
243 return tile;
244 }
245
246 static void map_update_visual(void)
247 {
248 u8 celldata[ 4096 ];
249
250 for( int i = 0; i < map.x*map.y; i ++ )
251 {
252 celldata[i*4+0] = i & 0x7;
253 celldata[i*4+1] = (i & ~0x7) >> 3;
254 }
255
256 // Configurations
257 // DCBA | X Y
258 // 0001 3 0
259 // 0010
260
261 for( int y = 0; y < map.y; y ++ )
262 {
263 for( int x = 0; x < map.x; x ++ )
264 {
265 struct cell *cur = map.cells + y*map.x + x;
266 u8 *cellbytes = celldata + (y*map.x+x)*4;
267
268 if( cur->flags & CELL_FLAG_WALKABLE )
269 {
270 struct cell *a, *b, *c, *d;
271
272 a = map_tile_at_cond( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE );
273 b = map_tile_at_cond( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE );
274 c = map_tile_at_cond( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE );
275 d = map_tile_at_cond( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE );
276
277 u32 config = (a?0x1:0x0) | (b?0x2:0x0) | (c?0x4:0x0) | (d?0x8:0x0);
278
279 cellbytes[ 0 ] = config;
280 cellbytes[ 1 ] = 0;
281 }
282 else
283 {
284 // TODO: Random background tiles
285 cellbytes[ 0 ] = 1;
286 cellbytes[ 1 ] = 0;
287 }
288 }
289 }
290
291 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
292 glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float) + 1024*2*sizeof(float), map.x*map.y*4, celldata );
293 }
294
295 static int map_load( const char *str )
296 {
297 map_free();
298
299 char *c = str;
300
301 // Scan for width
302 for(;; map.x ++)
303 {
304 if( str[map.x] == ';' )
305 break;
306 else if( !str[map.x] )
307 {
308 vg_error( "Unexpected EOF when parsing level!\n" );
309 return 0;
310 }
311 }
312
313 struct cell *row = arraddnptr( map.cells, map.x );
314 int cx = 0;
315 int reg_start = 0, reg_end = 0;
316
317 for(;;)
318 {
319 if( !*c )
320 break;
321
322 if( *c == ';' )
323 {
324 c ++;
325
326 // Parse attribs
327 if( *c != '\n' )
328 {
329 while( *c )
330 {
331 if( reg_start < reg_end )
332 {
333 if( *c >= 'a' && *c <= 'z' )
334 {
335 arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
336 }
337 else
338 {
339 if( *c == ',' || *c == '\n' )
340 {
341 reg_start ++;
342
343 if( *c == '\n' )
344 break;
345 }
346 else
347 {
348 vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
349 return 0;
350 }
351 }
352 }
353 else
354 {
355 vg_error( "Over-assigned values (row: %u)\n", map.y );
356 return 0;
357 }
358
359 c ++;
360 }
361 }
362
363 if( reg_start != reg_end )
364 {
365 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
366 return 0;
367 }
368
369 if( cx != map.x )
370 {
371 vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
372 return 0;
373 }
374
375 row = arraddnptr( map.cells, map.x );
376 cx = 0;
377 map.y ++;
378 reg_end = reg_start = arrlen( map.io );
379 }
380 else
381 {
382 if( cx == map.x )
383 {
384 vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
385 return 0;
386 }
387
388 row[ cx ].conditions = NULL;
389
390 // Parse the various cell types
391 if( *c == '+' || *c == '-' )
392 {
393 arrpush( map.io, cx + map.y*map.x );
394 row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
395 reg_end ++;
396 }
397 else if( *c == '#' )
398 {
399 row[ cx ++ ].flags = CELL_FLAG_WALL;
400 }
401 else
402 {
403 row[ cx ++ ].flags = 0x00;
404 }
405 }
406
407 c ++;
408 }
409
410 // Origin top left corner
411 map.origin[0] = -((float)map.x) * 0.5f;
412 map.origin[2] = -((float)map.y) * 0.5f;
413
414 float *offset_array = (float *)malloc( map.x*map.y*2*sizeof(float) );
415
416 for( int y = 0; y < map.y; y ++ )
417 {
418 for( int x = 0; x < map.x; x ++ )
419 {
420 float *coord = offset_array + (y*map.x+x)*2;
421 coord[0] = x;
422 coord[1] = y;
423 }
424 }
425
426 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
427 glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float), map.x*map.y*2*sizeof(float), offset_array );
428
429 free( offset_array );
430 vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
431 return 1;
432 }
433
434 static int map_tile_availible( int co[2] )
435 {
436 // Extract 5x5 grid surrounding tile
437 u32 blob = 0x1000;
438 for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ )
439 for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ )
440 {
441 struct cell *cell = map_tile( (int[2]){ x, y } );
442
443 if( cell && (cell->flags & CELL_FLAG_WALKABLE) )
444 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
445 }
446
447 // Run filter over center 3x3 grid to check for invalid configurations
448 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
449 for( int i = 0; i < vg_list_size(kernel); i ++ )
450 {
451 if( blob & (0x1 << (6+kernel[i])) )
452 {
453 // (reference window: 0x1CE7) Illegal moves
454 // 0100011100010 ;
455 // 0000001100011 ;
456 // 0000011000110 ;
457 // 0110001100000 ;
458 // 1100011000000 ;
459 // 0100001100010 ;
460 // 0100011000010 ;
461
462 u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
463 u32 window = blob >> kernel[i];
464
465 for( int j = 0; j < vg_list_size(invalid); j ++ )
466 if((window & invalid[j]) == invalid[j])
467 return 0;
468 }
469 }
470
471 return 1;
472 }
473
474 void vg_update(void)
475 {
476 // Update camera
477 float ratio = (float)vg_window_y / (float)vg_window_x;
478 float const size = 7.5f;
479 glm_ortho( -size, size, -size*ratio, size*ratio, 0.01f, 150.f, m_projection );
480
481 glm_mat4_identity( m_view );
482 glm_translate_z( m_view, -10.f );
483 glm_rotate_x( m_view, 1.5708f, m_view );
484
485 glm_mat4_mul( m_projection, m_view, vg_pv );
486
487 // Compute map update
488 for( int y = 0; y < map.y; y ++ )
489 {
490 for( int x = 0; x < map.x; x ++ )
491 {
492 struct cell *tile, *upper, *lower, *l, *r;
493 tile = map_tile_at( (int [2]){ x, y } );
494 tile->flags &= ~(CELL_FLAG_SPLIT|CELL_FLAG_MERGE|CELL_FLAG_UPLVL);
495
496 if( tile->flags & CELL_FLAG_WALKABLE )
497 {
498 r = map_tile_at_cond( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE );
499 l = map_tile_at_cond( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE );
500
501 if( r && l )
502 {
503 upper = map_tile_at_cond( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE );
504 lower = map_tile_at_cond( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE );
505
506 if( upper )
507 {
508 tile->flags |= CELL_FLAG_MERGE | CELL_FLAG_UPLVL;
509 }
510
511 if( lower )
512 {
513 tile->flags |= CELL_FLAG_SPLIT;
514 l->flags |= CELL_FLAG_UPLVL;
515 r->flags |= CELL_FLAG_UPLVL;
516 }
517 }
518 }
519 }
520 }
521
522 // Compute classification
523 /*
524 map_stack_refresh();
525
526 for( int i = 0; i < arrlen( map.io ); i ++ )
527 {
528 struct *cell cell = &map.cells[ map.io ];
529 int coords[2];
530
531 if( cell->flags & CELL_FLAG_INPUT )
532 {
533 map_tile_coords_from_index( map.io, coords );
534 map_stack_init( coords );
535
536 do
537 {
538 if( cell->flags & CELL_FLAG_CONNECTOR )
539 {
540
541 }
542 }
543 while( (cell = map_stack_next()) );
544 }
545 }*/
546
547 // Get mouse ray
548 vec3 ray_origin;
549 vec3 ray_dir;
550
551 mat4 pv_inverse;
552 vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
553 glm_mat4_inv( vg_pv, pv_inverse );
554 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
555 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
556 glm_vec3_sub( ray_dir, ray_origin, ray_dir );
557
558 // Get floor tile intersection
559 float ray_t = -ray_origin[1] / ray_dir[1];
560
561 vec3 tile_pos;
562 glm_vec3_copy( ray_origin, tile_pos );
563 glm_vec3_muladds( ray_dir, ray_t, tile_pos );
564 glm_vec3_sub( tile_pos, map.origin, tile_pos );
565
566 int tile_x = floorf( tile_pos[0] );
567 int tile_y = floorf( tile_pos[2] );
568
569 map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
570
571 if( map.playing )
572 {
573 static int fish_counter = 0;
574 fish_counter ++;
575
576 if( fish_counter > 20 )
577 {
578 fish_counter = 0;
579
580 // Advance characters
581 for( int i = 0; i < map.num_fishes; i ++ )
582 {
583 struct fish *fish = map.fishes + i;
584
585 if( !fish->alive )
586 continue;
587
588 struct cell *tile, *next;
589 tile = map_tile_at( fish->co );
590
591 if( tile->flags & CELL_FLAG_OUTPUT )
592 {
593 vg_info( "Fish got zucced (%d)\n", i );
594 fish->alive = 0;
595 continue;
596 }
597
598 int die = 0;
599 if( tile->flags & CELL_FLAG_SPLIT )
600 {
601 die = 1;
602 int new_dir[][2] = { {0,-1},{1,0},{-1,0} };
603 int *test_dir;
604
605 for( int j = 0; j < 3; j ++ )
606 {
607 test_dir = new_dir[ tile->state ];
608 tile->state = (tile->state+1)%3;
609
610 next = map_tile_at( (int[2]){ fish->co[0]+test_dir[0], fish->co[1]+test_dir[1] } );
611 if( next && (next->flags & (CELL_FLAG_WALKABLE)) )
612 {
613 fish->dir[0] = test_dir[0];
614 fish->dir[1] = test_dir[1];
615 die = 0;
616 break;
617 }
618 }
619 }
620
621 next = map_tile_at( (int[2]){ fish->co[0]+fish->dir[0], fish->co[1]+fish->dir[1] } );
622 if( !next || (next && !(next->flags & CELL_FLAG_WALKABLE)) )
623 {
624 // Try UP
625 die = 1;
626 }
627
628 if( die )
629 {
630 vg_info( "Fish died! (%d)\n", i );
631 fish->alive = 0;
632 continue;
633 }
634
635
636 fish->co[0] += fish->dir[0];
637 fish->co[1] += fish->dir[1];
638 }
639
640 // Try spawn fish
641 for( int i = 0; i < arrlen( map.io ); i ++ )
642 {
643 struct cell *input = &map.cells[ map.io[i] ];
644
645 if( input->flags & CELL_FLAG_INPUT )
646 {
647 if( input->state < arrlen( input->conditions ) )
648 {
649 struct fish *fish = &map.fishes[ map.num_fishes ];
650 map_tile_coords_from_index( map.io[i], fish->co );
651
652 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
653 int can_spawn = 0;
654
655 for( int i = 0; i < vg_list_size( output_dirs ); i ++ )
656 {
657 int *dir = output_dirs[i];
658 struct cell *next = map_tile_at( (int[2]){ fish->co[0]+dir[0], fish->co[1]+dir[1] } );
659 if( next && next->flags & CELL_FLAG_CANAL )
660 {
661 fish->dir[0] = dir[0];
662 fish->dir[1] = dir[1];
663 can_spawn = 1;
664 }
665 }
666
667 if( can_spawn )
668 {
669 fish->alive = 1;
670 input->state ++;
671 map.num_fishes ++;
672 }
673 }
674 }
675 }
676
677 vg_info( "There are now %u active fish\n", map.num_fishes );
678 }
679
680 if( vg_get_button_down( "go" ) )
681 {
682 map.playing = 0;
683 map.num_fishes = 0;
684
685 vg_info( "Ending!\n" );
686 }
687 }
688 else
689 {
690 if( vg_get_button_down( "go" ) )
691 {
692 map.playing = 1;
693
694 // Reset everything
695 for( int i = 0; i < map.x*map.y; i ++ )
696 map.cells[ i ].state = 0;
697
698 vg_info( "Starting!\n" );
699 }
700
701 if( map.selected )
702 {
703 map.select_valid = map_tile_availible( (int[2]){ tile_x, tile_y } );
704
705 if( map.select_valid )
706 {
707 if( vg_get_button_down("primary") )
708 {
709 if( map.selected->flags & CELL_FLAG_CANAL )
710 {
711 map.selected->flags &= ~(CELL_FLAG_CANAL);
712 }
713 else
714 {
715 map.selected->flags |= CELL_FLAG_CANAL;
716 }
717 }
718 }
719 }
720 }
721 }
722
723 GLuint tile_vao;
724 GLuint tile_vbo;
725
726 void vg_render(void)
727 {
728 glViewport( 0,0, vg_window_x, vg_window_y );
729
730 //glEnable( GL_DEPTH_TEST );
731 glClearColor( 0.94f, 0.94f, 0.94f, 1.0f );
732 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
733
734 /*
735 glBindVertexArray( tile_vao );
736
737 SHADER_USE( colour_shader );
738 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
739
740 for( int y = 0; y < map.y; y ++ )
741 {
742 for( int x = 0; x < map.x; x ++ )
743 {
744 glm_mat4_identity( m_mdl );
745 glm_translate( m_mdl,
746 (vec3){
747 map.origin[0] + (float)x,
748 0.f,
749 map.origin[2] + (float)y
750 }
751 );
752 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
753
754 struct cell *cell = &map.cells[ y*map.x+x ];
755
756 vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f };
757
758 if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour );
759 else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour );
760 else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour );
761 else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour );
762
763 if( cell->flags & CELL_FLAG_SPLIT )
764 glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour );
765 else if( cell->flags & CELL_FLAG_MERGE )
766 glm_vec3_copy( (vec3){ 0.f, 0.6f, 0.8f }, colour );
767
768 if( map.selected == cell )
769 {
770 if( !map.select_valid )
771 glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour );
772
773 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
774 glm_vec3_scale( colour, flash, colour );
775 }
776
777 glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour );
778 glDrawArrays( GL_TRIANGLES, 0, 6 );
779 }
780 }
781
782 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 1.f, 0.f, 1.f, 1.f );
783
784 for( int i = 0; i < map.num_fishes; i ++ )
785 {
786 struct fish *fish = map.fishes + i;
787
788 if( fish->alive )
789 {
790 glm_mat4_identity( m_mdl );
791 glm_translate( m_mdl,
792 (vec3){
793 map.origin[0] + (float)fish->co[0] + 0.5f,
794 0.1f,
795 map.origin[2] + (float)fish->co[1] + 0.5f
796 }
797 );
798 glm_scale_uni( m_mdl, 0.2f );
799 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
800 glDrawArrays( GL_TRIANGLES, 0, 6 );
801 }
802 }
803 */
804
805 glBindVertexArray( map.tiles_vao );
806
807 map_update_visual();
808
809 SHADER_USE( tilemap_shader );
810 glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
811
812 glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexTiles" ), 0 );
813 glActiveTexture( GL_TEXTURE0 );
814 glBindTexture( GL_TEXTURE_2D, map.tile_texture );
815
816 glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexRipples" ), 1 );
817 glActiveTexture( GL_TEXTURE1 );
818 glBindTexture( GL_TEXTURE_2D, map.flow_texture );
819
820 glUniform2f( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), map.origin[0], map.origin[2] );
821 glUniform1f( SHADER_UNIFORM( tilemap_shader, "uTime" ), vg_time * 0.5f );
822
823 glDrawArraysInstanced( GL_TRIANGLES, 0, 6, map.x*map.y );
824 }
825
826 void vg_register(void)
827 {
828 SHADER_INIT( colour_shader );
829 SHADER_INIT( tilemap_shader );
830 }
831
832 void vg_start(void)
833 {
834 glGenVertexArrays( 1, &tile_vao );
835 glGenBuffers( 1, &tile_vbo );
836
837 float quad_mesh[] =
838 {
839 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
840 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
841
842 // Padding
843 0.0f, 0.0f, 0.0f, 0.0f
844 };
845
846 glBindVertexArray( tile_vao );
847 glBindBuffer( GL_ARRAY_BUFFER, tile_vbo );
848 glBufferData
849 (
850 GL_ARRAY_BUFFER,
851 sizeof( quad_mesh ),
852 quad_mesh,
853 GL_STATIC_DRAW
854 );
855
856 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
857 glEnableVertexAttribArray( 0 );
858
859 VG_CHECK_GL();
860
861 // Create map buffers
862 glGenVertexArrays( 1, &map.tiles_vao );
863 glGenBuffers( 1, &map.tiles_vbo );
864
865 glBindVertexArray( map.tiles_vao );
866 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
867 glBufferData( GL_ARRAY_BUFFER,
868 sizeof( quad_mesh ) +
869 sizeof( float )*2 * 1024 +
870 sizeof( u8 )*4 * 1024,
871 NULL,
872 GL_DYNAMIC_DRAW
873 );
874
875 glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof( quad_mesh ), quad_mesh );
876
877 // Base quad
878 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
879 glEnableVertexAttribArray( 0 );
880
881 // Offset, data arrays (instancing)
882 glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)(sizeof(quad_mesh)) );
883 glEnableVertexAttribArray( 1 );
884 glVertexAttribDivisor( 1, 1 );
885
886 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_FALSE, 4, (void*)(sizeof(quad_mesh)+sizeof(float)*2*1024) );
887 glEnableVertexAttribArray( 2 );
888 glVertexAttribDivisor( 2, 1 );
889
890 map.tile_texture = vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
891 vg_tex2d_nearest();
892 vg_tex2d_repeat();
893
894 map.flow_texture = vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
895 vg_tex2d_nearest();
896 vg_tex2d_repeat();
897
898 map_load
899 (
900 "#####-#####;aa\n"
901 "# #;\n"
902 "# #;\n"
903 "# -;bb\n"
904 "# #;\n"
905 "# #;\n"
906 "#####+#####;abab\n"
907 );
908 }
909
910 void vg_free(void)
911 {
912 map_free();
913
914 glDeleteVertexArrays( 1, &tile_vao );
915 glDeleteVertexArrays( 1, &map.tiles_vao );
916
917 glDeleteBuffers( 1, &tile_vbo );
918 glDeleteBuffers( 1, &map.tiles_vbo );
919
920 glDeleteTextures( 1, &map.tile_texture );
921 glDeleteTextures( 1, &map.flow_texture );
922 }
923
924 void vg_ui(void)
925 {
926
927 }