~nch/gamelog

2860fd647c20bdd08c60e5362e2ecffd9a4c1961 — nc a month ago 7543221 master
minor reorg
3 files changed, 70 insertions(+), 11 deletions(-)

M debug_draw.nim
R df.nim => game.nim
A test_collisions.nim
M debug_draw.nim => debug_draw.nim +13 -11
@@ 45,17 45,19 @@ proc clear*(c: var DebugCtx) =
    c.mesh.set_len(0)

proc draw*(c: DebugCtx, mvp: var Mat4f) =
    gl_use_program(c.prog)
    let p = gl_map_named_buffer(c.buffer, GL_WRITE_ONLY)
    copy_mem(p, c.mesh[0].unsafeAddr, Vec3f.sizeof * len(c.mesh))
    discard gl_unmap_named_buffer(c.buffer)
    if len(c.mesh) > 0:
        gl_use_program(c.prog)

    gl_bind_vertex_array(c.buffer_vao)
    gl_bind_buffer(GL_ARRAY_BUFFER, c.buffer)
    gl_uniform_matrix4fv(gl_get_uniform_location(c.prog, "MVP"), 1, false, mvp.caddr)
    gl_point_size(5)
    gl_draw_arrays(GL_LINES, 0, (len(c.mesh)).GLsizei)
    gl_draw_arrays(GL_POINTS, 0, (len(c.mesh)).GLsizei)
        let p = gl_map_named_buffer(c.buffer, GL_WRITE_ONLY)
        copy_mem(p, c.mesh[0].unsafeAddr, Vec3f.sizeof * len(c.mesh))
        discard gl_unmap_named_buffer(c.buffer)

        gl_bind_vertex_array(c.buffer_vao)
        gl_bind_buffer(GL_ARRAY_BUFFER, c.buffer)
        gl_uniform_matrix4fv(gl_get_uniform_location(c.prog, "MVP"), 1, false, mvp.caddr)
        gl_point_size(5)
        gl_draw_arrays(GL_LINES, 0, (len(c.mesh)).GLsizei)
        gl_draw_arrays(GL_POINTS, 0, (len(c.mesh)).GLsizei)

proc edge*(c: var DebugCtx, vs: varargs[Vec3f]) =
    for (a, b) in zip(vs, vs[1..^1]):


@@ 63,7 65,7 @@ proc edge*(c: var DebugCtx, vs: varargs[Vec3f]) =
        c.mesh.add(b)

proc sphere*(c: var DebugCtx, pos: Vec3f, radius: float32, np: uint = 8, nm: uint = 16) =
    for j in 0..< np:
    for j in 0 ..< np:
        let pp = PI * (j).float / np.float
        let p = PI * (j + 1).float / np.float
        let

R df.nim => game.nim +4 -0
@@ 287,6 287,8 @@ if is_main_module:

    var c = DefaultOpenglWindowConfig
    c.title = "game"
    c.version = glv45

    var w = new_window(c)
    w.cursor_position_cb = cursor_pos_callback
    w.cursor_mode = cm_disabled


@@ 294,6 296,8 @@ if is_main_module:
    glfw.make_context_current(w)
    var debug = init_debug()

    echo "Version: ", cast[cstring](gl_get_string(GL_VERSION))

    ###

    let prog = make_program((GL_VERTEX_SHADER, """

A test_collisions.nim => test_collisions.nim +53 -0
@@ 0,0 1,53 @@
import unittest
import glm
import game
import options
import glfw
import opengl
import debug_draw

let unit_sphere = (vec3f(0, 0, 0), vec3f(1, 1, 1)).Ellipse

proc display_scene(triangle: array[3, Vec3f]) =
    glfw.initialize()
    load_extensions()

    let width = 800
    let height = 600


    let proj: Mat4[float32] = perspective[float32](Pi / 4, width.float32 / height.float32, 0.1, 100)

    var c = DefaultOpenglWindowConfig
    c.title = "game"
    var w = new_window(c)

    glfw.make_context_current(w)

    var debug = init_debug()

    while true:
        let cam_pos = vec3f(sin(glfw.get_time()) * 5, 2, cos(glfw.get_time()) * 5)
        let view: Mat4[float32] = look_at(cam_pos, vec3f(0, 0, 0), vec3f(0, 1, 0))
        var mvp = proj * view

        gl_clear(GL_COLOR_BUFFER_BIT)
        debug.sphere(unit_sphere.pos, unit_sphere.radius.x)
        debug.edge(triangle[0], triangle[1], triangle[2], triangle[0])
        debug.draw(mvp)

        w.swap_buffers
        poll_events()
        if w.should_close == true or w.is_key_down(keyEscape):
            break

display_scene([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)])

suite "collision":
    test "no collision":
        require not check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()
        require not check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()

    test "collisions while still":
        require check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(0, 0, 0), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()
        require check_collision(unit_sphere, vec3f(0, 0, 0), ([vec3f(2, 2, 2), vec3f(2, 0, 2), vec3f(2, 2, 0)]).Triangle).is_some()