rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_main.sc -rwxr-xr-x 4.6 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env scopes

using import struct

#let stringtest = "Hello"
#let booltest = true

using import glm
using import glsl

using import .tukan.GLMain
using import .tukan.Screen

using import .tukan.DockGUI
using import .tukan.sdf
using import .tukan.rotation

using import .tukan.gl
using import .tukan.imgui

vvv bind glmain
GLMain
    title = "Tukan"
    width = 960
    height = 540
    resizable = true

let screen = (Screen)
DockGUI glmain

print "setting up texture..."
let fbsize =
    ivec2 1920 1080
global fbtex = (GL.CreateTexture GL.TEXTURE_2D)
'setup fbtex
    size = fbsize
    format = GL.RGBA8
local color =
    arrayof u8 255 0 255 255
GL.ClearTexImage fbtex 0 GL.RGBA GL.UNSIGNED_BYTE &color

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

print "building shaders"

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

struct ShaderGlobals
    aspect : f32
    time : f32

let BUFFER_COUNT = 3
#let UBO_ALIGN =
    usize (gl-get-int GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT)
#let SSBO_ALIGN =
    usize (gl-get-int GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT)

uniform phase : f32
    location = 0

uniform shglobals : ShaderGlobals
    binding = 0

let globals-data = (GL.CreateBuffer)
setup-ubo globals-data shglobals BUFFER_COUNT

#-------------------------------------------------------------------------------



#-------------------------------------------------------------------------------

inout uv : vec2 (location = 0)
inout pos : vec2 (location = 1)

fn test-vertex ()
    uv.out =
        ((Screen.set-vertex-position) * 0.5) + 0.5
    return;

fn make-phase ()
    phase as immutable

fn map (p)
    let p =
        versor-rotate
            versor (vec3 0 1 0) (deref shglobals.time)
            + p
                vec3 0 0 -2
    #let p =
        * (rotation (versor (vec3 0 1 0) (shglobals.time as immutable)))
            + p
                vec3 0 0 -2
    sdBox p
        vec3 0.5
    #sdUberprim p
        vec4 1.0 1.0 0.25 0.25
        vec3 1.0 0.25 0.0

let FAR_LIMIT = 100.0
let MAX_STEPS = 50
inline raymarch (mapf ro rd)
    loop (i t = 0 0.0)
        let p = (ro + rd * t)
        let d = (mapf p)
        if ((d > 1e-3) & (t < FAR_LIMIT) & (i < MAX_STEPS))
            repeat (i + 1) (t + d)
        else
            break p (not (t >= FAR_LIMIT))

out out_Color : vec4
fn test-fragment ()
    let uv =
        (uv.in * 2.0 - 1.0) * (vec2 shglobals.aspect 1)
    let p hit =
        raymarch map (vec3 0)
            normalize
                vec3 uv 1
    out_Color =
        vec4
            ? hit
                (sdNormal map p) * 0.5 + 0.5
                vec3 0.0
            1
    return;

print "attaching shaders..."
let pg-test = (GL.CreateProgram)
call
    attach-shaders pg-test
        vertex = test-vertex
        fragment = test-fragment
        #debug = true
locals;

#-------------------------------------------------------------------------------

print "running main program"

global test-window-visible = false
@@ 'on DockGUI.on-define-windows
inline ()
    if (test-window-visible as immutable)
        ShowDemoWindow test-window-visible

@@ 'on DockGUI.on-define-main-menu
inline ()
    if (BeginMenu "View")
        MenuItem "Show Demo Window"
            selected = test-window-visible
        EndMenu;

global g_phase = 0.0
global g_frame = 0
inline render-view (size)
    GL.BindFramebuffer GL.FRAMEBUFFER fb
    GL.Viewport 0 0 (i32 size.x) (i32 size.y)
    GL.ClearColor 0 0 1 1
    GL.Clear
        |
            GL.COLOR_BUFFER_BIT
            GL.DEPTH_BUFFER_BIT
            GL.STENCIL_BUFFER_BIT

    GL.UseProgram pg-test
    #glUniform phase (normsin (g_phase as immutable))

    let buffer-index = (g_frame % BUFFER_COUNT)

    local data =
        ShaderGlobals
            aspect = (size.x / size.y)
            time = (g_frame / 60)
    bind-ubo globals-data shglobals &data buffer-index

    g_phase = (g_phase + 0.1)
    g_frame = g_frame + 1
    'draw screen
    GL.UseProgram 0

let source =
    list-load module-path

@@ 'on DockGUI.on-define-dock
inline ()
    SetNextDock ImGuiDockSlot_Right
    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
@@ 'on 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

'run glmain

drop glmain

print "done."