Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
initial commit of game-of-life example (with visuals)
- Loading branch information
Showing
5 changed files
with
243 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#version 400 | ||
|
||
in vec2 tex_coord; | ||
out vec4 frag_colour; | ||
|
||
uniform sampler2D tex; | ||
|
||
void main() | ||
{ | ||
frag_colour = 1.0 - 128.0 * texture(tex, tex_coord).rrra; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#version 400 | ||
|
||
layout(location = 0) in vec2 vp; | ||
layout(location = 1) in vec2 tc; | ||
out vec2 tex_coord; | ||
|
||
void main () { | ||
// Colour = vc; | ||
gl_Position = vec4(vp, 0.0, 1.0); | ||
tex_coord = tc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
(sys:load "libs/core/game-of-life.xtm") | ||
(sys:load "examples/external/shader-tutorials/shader-setup.xtm") | ||
|
||
(bind-val fullscreen_quad_verts float* 16) | ||
|
||
(bind-val world World*) | ||
(bind-val tex Texture*) | ||
(bind-val vbo VBO*) | ||
(bind-val vao VAO*) | ||
|
||
(bind-func world_texture_setup | ||
(lambda () | ||
(glActiveTexture GL_TEXTURE0) | ||
(glBindTexture GL_TEXTURE_2D (Texture_id tex)) | ||
(glTexImage2D GL_TEXTURE_2D | ||
0 | ||
GL_RGB | ||
(+ 2 (convert (world_width world))) | ||
(+ 2 (convert (world_height world))) | ||
0 | ||
GL_RED | ||
GL_BYTE | ||
(world_data world)) | ||
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE) | ||
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE) | ||
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST) | ||
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST))) | ||
|
||
(define *gol-shader* | ||
(create_shader (file->string "examples/external/game-of-life.vert") | ||
(file->string "examples/external/game-of-life.frag"))) | ||
|
||
;; init | ||
(call-as-xtlang | ||
(pfill! fullscreen_quad_verts | ||
;; pos texcoord | ||
-1.0 1.0 0.0 0.0 | ||
1.0 1.0 1.0 0.0 | ||
-1.0 -1.0 0.0 1.0 | ||
1.0 -1.0 1.0 1.0) | ||
(set! world (world_create 256 128)) | ||
(world_init world .05) | ||
(set! vbo (create_vbo fullscreen_quad_verts 16)) | ||
(bind_vbo vbo) | ||
(set! vao (create_vao)) | ||
(bind_attribute vao vbo 0 2 16 0) ;; position | ||
(bind_attribute vao vbo 1 2 16 8) ;; tex_coord | ||
(set! tex (create_texture)) | ||
(world_texture_setup) | ||
void) | ||
|
||
(call-as-xtlang (world_init world .04)) | ||
|
||
(bind-func gl_draw | ||
(lambda (program) | ||
;; run the simulation | ||
(world_step world) | ||
;; update texture | ||
(glTexSubImage2D GL_TEXTURE_2D 0 0 0 | ||
(+ 2 (convert (world_width world))) | ||
(+ 2 (convert (world_height world))) | ||
GL_RED | ||
GL_BYTE | ||
(world_data world)) | ||
;; draw the world | ||
(glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)) | ||
(glUniform1i (glGetUniformLocation program "tex") 0) | ||
(glUseProgram program) | ||
(draw_vertex_array vao GL_TRIANGLE_STRIP 0 4) | ||
void)) | ||
|
||
(define gl-loop | ||
(lambda (time delta-t) | ||
(let ((late-by (- (now) time)) | ||
(next-frame-time (+ time (* *second* delta-t)))) | ||
(if (> late-by 0) | ||
(print "Late by " (* 1.0 (/ late-by *second*)) "seconds\n") | ||
(begin | ||
(gl_draw *gol-shader*) | ||
(gl:swap-buffers *gl-window*))) | ||
(callback (* 0.9 next-frame-time) | ||
'gl-loop | ||
next-frame-time | ||
delta-t)))) | ||
|
||
(gl-loop (now) 1/20) | ||
|
||
;; actually run the simulation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.