rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/main.sc -rwxr-xr-x 3.6 KiB View raw Log this file
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env scopes

import .tukan.glmain
import .tukan.screen
#import .tukan.imgui

let glmain = tukan.glmain
let screen = tukan.screen

import .tukan.dockgui
let dockgui = tukan.dockgui

using import glm
using import glsl
using import .tukan.gl
using import .tukan.imgui

glmain.init
    title = "Tukan"
    width = 960
    height = 540
    resizable = true

print "setting up texture..."
let fbsize =
    ivec2 1920 1080
let fbtex = (glCreateTexture GL_TEXTURE_2D)
setup-texture fbtex fbsize.x fbsize.y
    format = GL_RGBA8
var color =
    arrayof u8 255 0 255 255
glClearTexImage fbtex 0 GL_RGBA GL_UNSIGNED_BYTE (bitcast color (pointer void))

print "setting up framebuffer..."
let fb = (glCreateFramebuffer)
setup-framebuffer fb
    color = fbtex

print "building shaders"

let U_PHASE = 0
let U_ASPECT = 1

fn normsin (x)
    (sin x) * 0.5 + 0.5

let pg = (glCreateProgram)
do
    fn varyings (mode)
        xvar (do mode) uv : vec2 (location = 0)
        xvar (do mode) pos : vec2 (location = 1)
        locals;
        #scopeof
            uv = uv; pos = pos

    let vertex-fn =
        do
            using (varyings 'out)
            fn main ()
                uv =
                    ((screen.set-vertex-position) * 0.5) + 0.5
                return;

    let fragment-fn =
        do
            using (varyings 'in)
            xvar out out_Color : vec4
            xvar uniform phase : f32
                location = U_PHASE
            xvar uniform aspect : f32
                location = U_ASPECT

            fn make-phase ()
                phase as immutable

            fn main ()
                let uv =
                    (uv as immutable) * 2.0 - 1.0
                let uv =
                    vec2
                        uv.x * aspect
                        uv.y
                out_Color =
                    vec4
                        vec2
                            clamp
                                (length uv) - 0.5
                                \ 0.0 1.0
                        #0
                        make-phase;
                        1
                return;

    print "attaching shaders..."
    let link =
        attach-shaders pg
            vertex = vertex-fn
            fragment = fragment-fn
            #debug = true
    link;

print "running main program"

global test-window-visible = true
'append dockgui.on-define-windows
    fn ()
        ShowDemoWindow test-window-visible

global phase = 0.0
fn render-view (size)
    glBindFramebuffer GL_FRAMEBUFFER fb
    glViewport 0 0 (i32 size.x) (i32 size.y)
    glClearColor 0 0 1 1
    glClear
        |
            GL_COLOR_BUFFER_BIT
            GL_DEPTH_BUFFER_BIT
            GL_STENCIL_BUFFER_BIT

    glUseProgram pg
    glUniform1f U_PHASE (normsin (phase as immutable))
    glUniform1f U_ASPECT (size.x / size.y)
    phase = (phase + 0.1)
    screen.draw;
    glUseProgram 0

'append dockgui.on-define-dock
    fn ()
        if (BeginDock "3D")
            let size =
                GetContentRegionAvail;
            render-view size
            Image
                inttoptr fbtex ImTextureID
                size = size
                uv0 =
                    vec2 0.0
                        size.y / (f32 fbsize.y)
                uv1 =
                    vec2
                        size.x / (f32 fbsize.x)
                        0.0
            EndDock;

#'append glmain.on-draw
'append dockgui.on-frame
    fn (s size)
        #glBlitNamedFramebuffer fb 0
            \ 0 0 size.x size.y
            \ 0 0 size.x size.y
            GL_COLOR_BUFFER_BIT
            GL_NEAREST

glmain.loop;

glmain.shutdown;

print "done."