require 'gl3'

BMPSIZE=: 512 512
IFPRINT=: 0

A=: 0 : 0
pc a closeok;
menupop "&Help";
menu help "&Help" "" "" "";
menupopz;
xywh 0 24 100 100;cc g isigraph ws_clipchildren ws_clipsiblings rightmove bottommove;
xywh 0 24 100 100;cc g1 isigraph ws_clipchildren ws_clipsiblings rightmove bottommove;
xywh 5 4 34 14;cc print button;cn "Print";
pas 0 0;
rem form end;
)

a_run=: 3 : 0
wd :: ] 'psel a;pclose'
wd A
glsel 'g1'
glaRC 1, BMPSIZE
glsel ,'g'
glaRC''
R=: 20 30 0
wd 'setshow g1 0'
wd 'pshow;'
)

NB. a_print_button
NB. draws g1, then sets flag to print on next paint event
a_print_button=: 3 : 0
glsel 'g1'
a_size BMPSIZE
a_g_paint ''
glsel ,'g'
IFPRINT=: 1
glpaintx''
)

NB. a_printit
NB. prints, then sets off print flag
a_printit=: 3 : 0
if. 0=IFPRINT do. return. end.
IFPRINT=: 0
glsel 'g1'
f=. jpath '~temp\temp.bmp'
glaSaveBMP f
glsel ,'g'
print f
)

a_g_mbldown=: 3 : 0
wd'setfocus g'	NB. mouse click sets focus for chars
)

a_g_char =: verb define
R =: 360 | R + 2 * 'xyz' = {.sysdata
glpaintx''
)

a_size=: 3 : 0
glViewport 0 0,y.
glMatrixMode GL_PROJECTION
glLoadIdentity''
gluPerspective 30, (%/y.),1 10
)

a_g_size=: a_size @ (glqwh bind '')

a_g_paint =: verb define
glClearColor 1 1 1 0
glClear GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT
glEnable GL_DEPTH_TEST
glMatrixMode GL_MODELVIEW
glLoadIdentity''
glTranslate 0 0 _8
glRotate R ,. 3 3 $ 1 0 0 0
drawbox ''
glaSwapBuffers ''
a_printit ''
)

BLUE=:  0 0 1 0
GREEN=: 0 1 0 0
RED=:   1 0 0 0

drawbox=:3 : 0
p=. _1 ^ #: i.8
BLUE         polygon 0 1 3 2{p
GREEN        polygon 0 1 5 4{p
RED          polygon 0 2 6 4{p
(RED+BLUE)   polygon 4 5 7 6{p
(RED+GREEN)  polygon 1 3 7 5{p
(BLUE+GREEN) polygon 2 3 7 6{p
)

polygon=: 4 : 0
glColor x.
glBegin GL_POLYGON
 glVertex y.
glEnd ''
)

a_help_button=: 3 : 0
wd'mb OpenGL *Click or tab to give focus. Press x y z to rotate.'
)

dword =: 2:3!:4] NB. integer to char flipped bytes

NB. return w h of bmp file
bmpwh=: verb define
_2 (3!:4) 1!:11 y.;18 8
)

print=: verb define
wd'pc p closeok;cc g isigraph'
glmap MM_TWIPS
box=.2000 4000 8000, 8000*%%/bmpwh y.	NB. bmp aspect ratio
glbmpxywh box
glbmp y.
glbrushnull''
glrect box
glshow''
glzcreate''
glzstartdoc 'abc'
glzstartpage''
glzprint''
glzendpage''
glzenddoc''
wd'pclose'
)

wd :: ] 'psel a;pclose'
a_run''
