-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathybot_vg.xtm
243 lines (195 loc) · 7.65 KB
/
ybot_vg.xtm
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
(if (and (defined? '*xtmlib-ybot-vg-loaded*) *xtmlib-ybot-vg-loaded*)
(sys:load-escape "ybot_vg library already loaded"))
(define *xtmlib-ybot-vg-loaded* #f)
(define *ybot-lib-dir* "/Users/s2805534/Dropbox/ybot/src/extempore_libs")
(if (not (defined? '*ybot-lib-dir*))
(sys:load-escape "Set the *ybot-lib-dir* variable before loading this library"))
(sys:load (string-append *ybot-lib-dir* "/ybot_maths.xtm"))
(sys:load "libs/external/glfw3.xtm")
(bind-val width float 1024.)
(bind-val height float 768.)
(bind-val window GLFWwindow*
(begin
(glfwSetErrorCallback (convert (get_native_fptr glfw_error_callback)))
(glfw_init_and_create_interaction_window (convert width) (convert height))))
(sys:load "libs/external/nanovg.xtm")
(bind-val vg NVGcontext* (nvg_create_context))
(bind-val pixel_ratio float (glfw_get_pixel_ratio window))
;; background image
(bind-val bg_paint NVGpaint*
(_nvgImagePattern vg
0. 0.
width height
0.
(nvgCreateImage vg "assets/images/image.png" 0)
1.))
(define *xtmlib-ybot-vg-loaded* #t)
;;;;;;;;;;; IGNORE BELOW HERE ;;;;;
;; (sys:load "libs/external/gl.xtm")
;; (sys:load "libs/external/glfw3.xtm")
;; (bind-func create_drawing_window:[GLFWwindow*,i32,i32]*
;; (lambda (width height)
;; (let ((window (glfw_init_and_create_interaction_window width height)))
;; (glEnable GL_LINE_SMOOTH)
;; (glEnable GL_BLEND)
;; (glEnable GL_STENCIL_TEST)
;; (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
;; (glHint GL_LINE_SMOOTH_HINT GL_DONT_CARE)
;; (glLineWidth 1.5)
;; (glClearColor 1.0 0.0 0.0 0.0)
;; (glClear GL_COLOR_BUFFER_BIT)
;; (let ((glfw_key_callback:GLFWkeyfun
;; (lambda (window key scancode action mods)
;; (if (and (= key GLFW_KEY_ESCAPE) (= action GLFW_PRESS))
;; (glfwSetWindowShouldClose window 1))
;; void)))
;; (glfwSetErrorCallback (convert (get_native_fptr glfw_error_callback)))
;; (glfwSetKeyCallback window (convert (get_native_fptr glfw_key_callback))))
;; (glfwSwapBuffers window)
;; window)))
;; (create_drawing_window 400 400)
;; (bind-func glDrawCircle:[void,float,float,float]*
;; (lambda (x y r)
;; (let* ((n:i64 360)
;; (vertices:float* (salloc (* 2 n)))
;; (i:i64 0))
;; (dotimes (i n)
;; (let* ((theta (/ (* 2.0 (acos -1.0) (i64tod i)) (i64tod n)))
;; (u (+ x (dtof (* (ftod r) (cos theta)))))
;; (v (+ y (dtof (* (ftod r) (sin theta))))))
;; (pset! vertices (* 2 i) (correct_x u ar))
;; (pset! vertices (+ (* 2 i) 1) v)))
;; ;(glEnableClientState GL_VERTEX_ARRAY)
;; (glVertexPointer 2 GL_FLOAT 0 (bitcast vertices i8*))
;; (glDrawArrays GL_LINE_LOOP 0 (i64toi32 n))
;; ;(glDisableClientState GL_VERTEX_ARRAY)
;; void
;; )))
;; (bind-func glDrawTriangle:[void,float,float,float,float,float,float]*
;; (let ((vertices:float* (zalloc 6))
;; (pvbo:GLuint* (zalloc 1)))
;; (glGenBuffers 1 pvbo)
;; (glBindBuffer GL_ARRAY_BUFFER (pref vbo 0))
;; (lambda (x1 y1 x2 y2 x3 y3)
;; (pfill! vertices x1 y1 x2 y2 x3 y3)
;; (glBufferData GL_ARRAY_BUFFER 6 vertices GL_DYNAMIC_DRAW))))
;; ;(create_drawing_window 1000 600)
;; (bind-func render:[void]*
;; (let ((dirty:bool #t))
;; (lambda ()
;; (if dirty
;; (begin
;; (glLoadIdentity)
;; ;; Clear the background (with alpha-trails)
;; (glColor4f 0.0 0.0 0.0 0.05)
;; (glBegin GL_QUADS)
;; (glVertex2f -1.0 -1.0)
;; (glVertex2f -1.0 1.0)
;; (glVertex2f 1.0 1.0)
;; (glVertex2f 1.0 -1.0)
;; (glEnd)
;; ;; Draw in last segment
;; (glPointSize 10.0)
;; (glColor3f 1.0 1.0 0.0)
;; (glBegin GL_POINTS)
;; (glVertex2f x y)
;; (glEnd)))
;; void)))
;; (bind-func render_single_trace:[void,double*,float*]*
;; (let ((last_x:float 0.0)
;; (last_y:float 0.0))
;; (lambda ($phase:double* $trace:float*)
;; (let ((x:float (dtof (linterp -1.0 1.0 (pref $phase 0))))
;; (y:float (pref $trace 0)))
;; (glLoadIdentity)
;; ; Clear the background (with alpha-trails)
;; (glColor4f 0.0 0.0 0.0 0.05)
;; (glBegin GL_QUADS)
;; (glVertex2f -1.0 -1.0)
;; (glVertex2f -1.0 1.0)
;; (glVertex2f 1.0 1.0)
;; (glVertex2f 1.0 -1.0)
;; (glEnd)
;; ; Draw in last segment
;; (glPointSize 10.0)
;; (glColor3f 1.0 1.0 0.0)
;; (glBegin GL_POINTS)
;; (glVertex2f x y)
;; (glEnd)
;; ;(if (< last_x x)
;; ; (begin
;; ; (glBegin GL_LINES)
;; ; (glVertex2f last_x last_y)
;; ; (glVertex2f x y)
;; ; (glEnd)))
;; (set! last_x x)
;; (set! last_y y)
;; void))))
;; (define fullscreen #f)
;; ;; (define width 1920.0)
;; ;; (define height 1200.0)
;; (define width 1024.0)
;; (define height 768.0)
;; (bind-val width float width)
;; (bind-val height float height)
;; ;; setup opengl
;; (define *gl-ctx* (gl:make-ctx ":0.0" fullscreen 0.0 0.0 width height))
;; ;; first try to load from prebuilt stdlib
;; (sys:load "libs/xtm.xtm")
;; ;; failing that compile on-the-fly
;; (sys:load "libs/external/shaders.xtm")
;; (sys:load "libs/openvg.xtm")
;; ;; init xtm render pipeline
;; (xtm_render_setup width height)
;; (if (<> (xtm-vg-create-context width height) 1) (xtm_vg_print_error))
;; (bind-func post:XTMRENDERCB
;; (let ((image:VGImage (xtm_load_vgimage "/Users/s2805534/Desktop/Screen Shot 2014-06-14 at 2.11.05 pm.png"))
;; (path (xtm_create_path))
;; (fill (vgCreatePaint))
;; (fill_color:xtm_color* (alloc))
;; (stroke (vgCreatePaint))
;; (stroke_color:xtm_color* (alloc))
;; (clear_color:xtm_color* (alloc)))
;; (afill! fill_color 1.0 0.0 0.0 0.5)
;; (afill! stroke_color 0.0 0.0 1.0 0.5)
;; (afill! clear_color 0.0 0.0 0.0 1.0)
;; (xtm_paint_set_fill fill fill_color)
;; (xtm_paint_set_stroke stroke stroke_color 10.)
;; (lambda (frame shader m v p data)
;; (let ((i:float 0.0))
;; (xtm_vg_reset_matrices)
;; (xtm_vg_clear (convert width) (convert height) clear_color)
;; ;; create the path (using the add_circle closure)
;; (dotimes (i 200.0)
;; (xtm_add_ellipse
;; path
;; (+ (/ width 2.0) (* 200. (cos (* i 0.00025 (convert frame)))))
;; (+ (/ height 2.0) (* 100. (tan (* 0.0001 i (convert frame)))))
;; 30.
;; 30.))
;; ;; draw the image
;; (if (null? image) void (vgDrawImage image))
;; ;; draw the paths
;; (xtm_draw_path_keep path)
;; ;; cleanup
;; (xtm_clear_path path)
;; (xtm_vg_print_error)
;; void))))
;; (bind-func gl_render
;; (lambda ()
;; (xtm_render null null null post null)
;; 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_render)
;; (gl:swap-buffers *gl-ctx*)))
;; (callback (* 0.9 next-frame-time)
;; 'gl-loop
;; next-frame-time
;; delta-t))))
;; (gl-loop (now) 1/20)