require 'gl3'

A=: noun define
pc a closeok;
menupop "&Help";
menu help "&Help" "" "" "";
menupopz;
xywh 0 0 100 100;cc g isigraph ws_clipchildren ws_clipsiblings rightmove bottommove;
pas 0 0;
rem form end;
)

a_run=: verb define
wd A
glaRC ''
R1 =: R2 =: 20 30 0
S =: 1
glSelectBuffer 100
wd 'pshow;'
)

a_g_char =: verb define
k=.0{sysdata
if. S=1 do.
 R1 =: 360 | R1 + 2 * 'xyz'=k end.
if. S=2 do.
 R2 =: 360 | R2 + 2 * 'xyz'=k end.
glpaintx''
)

a_g_size=:verb define
wh=.glqwh''
glViewport 0 0,wh
glMatrixMode GL_PROJECTION
glLoadIdentity''
gluPerspective 60, (%/wh),1 10
)

a_g_paint =: verb define
a_g_size''
draw''
glaSwapBuffers ''
)

a_g_mbldown =: verb define
'x y' =. 2{.".sysdata
'w h' =. glqwh''

glRenderMode GL_SELECT
glInitNames''
glPushName _1
glMatrixMode GL_PROJECTION

glPushMatrix''
 glLoadIdentity''
 gluPickMatrix x , (h-y) , 5 5 , 0 0 , w , h
 gluPerspective 60, (w%h),1 10
 draw''
glPopMatrix''
glFlush''
selbuf =: glRenderMode GL_RENDER
S =: 3 { selbuf,0 0 0 0
)

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

drawbox=:verb define
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 4{.x.,1
glBegin GL_POLYGON
 glVertex y.
glEnd ''
)

draw=: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 2 0 _8
glRotate R1 ,. 3 3 $ 1 0 0 0
glLoadName 1
drawbox ''
glMatrixMode GL_MODELVIEW
glLoadIdentity''
glTranslate _2 0 _8
glRotate R2 ,. 3 3 $ 1 0 0 0
glLoadName 2
drawbox ''
)

a_help_button=: verb define
wd'mb OpenGL *Rotate selected cube with x y z keys. Select cube with mouse click.'
wd 'setfocus g'
)

a_run''
