rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/csg.sc -rwxr-xr-x 5.3 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env scopes

let stringtest = "Hello"
let booltest = true

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 (CSG)"
    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"

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

struct ShaderGlobals
    aspect : f32
    time : f32

let BUFFER_COUNT = 3

xvar uniform phase : f32
    location = 0

xvar uniform shglobals : ShaderGlobals
    binding = 0

let globals-data = (glCreateBuffer)
setup-ubo globals-data shglobals BUFFER_COUNT

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

let MAX_BRUSHES = 8192:usize
let MAX_OPS = 1:usize

struct Brush
    origin : vec3
    rotation : vec4
    # uberprim parameters
    s : vec4
    r : vec3

struct Op
    # voxel tile index
    tile : ivec3
    # brush index
    brush : u32

struct Scene
    brushes : (array Brush MAX_BRUSHES)
    ops : (array Op MAX_OPS)
    numbrushes : u32
    numops : u32

    method 'new& (self)
        self.numbrushes = 0:u32
        self.numops = 0:u32

    method 'repr& (self)
        .. "<numbrushes="
            repr (load self.numbrushes)
            " numops="
            repr (load self.numops)
            ">"

fn testsize ()
    dump (sizeof Scene)
    print "allocating scene..."
    let scene =
        nullof Scene
    print "scene allocated"
    unconst scene

compile
    typify testsize
    'dump-module
    #'dump-disassembly

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

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

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

fn test-vertex ()
    uv =
        ((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) (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
xvar out out_Color : vec4
fn test-fragment ()
    let uv =
        (uv * 2.0 - 1.0) * (vec2 shglobals.aspect 1)
    let loop (i t) = (unconst 0) (unconst 0.0)
    let rd =
        normalize
            vec3 uv 1
    let p = (rd * t)
    let d = (map p)
    if ((d > 1e-3) & (t < FAR_LIMIT) & (i < MAX_STEPS))
        loop (i + 1) (t + d)
    out_Color =
        vec4
            ? (t >= FAR_LIMIT)
                vec3 0.0
                (sdNormal map p) * 0.5 + 0.5
            1
    return;

#compile-glsl 'vertex
    typify fragment-fn
    'dump-module

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

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

print "running main program"

global test-window-visible = false
'append dockgui.on-define-windows
    fn ()
        if (test-window-visible as immutable)
            ShowDemoWindow test-window-visible

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

global g_phase = 0.0
global g_frame = 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-test
    #glUniform phase (normsin (g_phase as immutable))

    let buffer-index = (g_frame % BUFFER_COUNT)

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

    g_phase = (g_phase + 0.1)
    g_frame = g_frame + 1
    screen.draw;
    glUseProgram 0

let source =
    list-load module-path

'append dockgui.on-define-dock
    fn ()
        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
'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."