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''
R =: 20 30 0
selectinit''
wd 'pshow;ptop'
)

t1 =:  2  2 _5 ; 3 2 _5 ; 2.5 3 _5
t2 =:  2  7 _5 ; 3 7 _5 ; 2.5 8 _5
t3 =:  2  2  0 ; 3 2  0 ; 2.5 3  0
t4 =:  2  2 _2 ; 3 2 _2 ; 2.5 3 _2

mm =: verb define
glMatrixMode y.
glLoadIdentity''
)

selectinit =: verb define
glDepthFunc GL_LEQUAL
glEnable GL_DEPTH_TEST
glShadeModel GL_FLAT
glSelectBuffer 100
)

a_g_size=: verb define
glViewport 0 0 , glqwh''
)

a_g_paint=:verb define
glClearColor 0 0 0 0
glClear GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT
drawscene''
glaSwapBuffers''
)

drawscene =: verb define
mm GL_PROJECTION
gluPerspective 40 1 0.01 100
mm GL_MODELVIEW
gluLookAt 7.5 7.5 12.5 2.5 2.45 _5 0 1 0
glColor 0 1 0 0
drawtriangle  t1
glColor 1 0 0 0
drawtriangle  t2
glColor 1 1 0 0
drawtriangle  t3
drawtriangle  t4
drawviewvolume 0 5 0 5 0 10
)

drawtriangle =: verb define
glBegin GL_TRIANGLES
 glVertex >y.
glEnd''
)

drawviewvolume =: verb define
'x1 x2 y1 y2 z1 z2'=.y.
glColor 1 1 1 0
glBegin GL_LINE_LOOP
 glVertex x1,y1,-z1
 glVertex x2,y1,-z1
 glVertex x2,y2,-z1
 glVertex x1,y2,-z1
glEnd''

glBegin GL_LINE_LOOP
 glVertex x1,y1,-z2
 glVertex x2,y1,-z2
 glVertex x2,y2,-z2
 glVertex x1,y2,-z2
glEnd''

glBegin GL_LINES
 glVertex x1,y1,-z1
 glVertex x1,y1,-z2
 glVertex x1,y2,-z1
 glVertex x1,y2,-z2
 glVertex x2,y1,-z1
 glVertex x2,y1,-z2
 glVertex x2,y2,-z1
 glVertex x2,y2,-z2
glEnd''
)

selectobjects =: verb define
glRenderMode GL_SELECT
glInitNames ''
glPushName 0

glPushMatrix ''
 mm GL_PROJECTION
 glOrtho 0 5 0 5 0 5
 mm GL_MODELVIEW
 glLoadName 1
 drawtriangle  t1
 glLoadName 2
 drawtriangle  t2
 glLoadName 3
 drawtriangle  t3
 glPushName 4
 drawtriangle  t4
glPopMatrix''
glFlush''
glRenderMode GL_RENDER
)

feedback =: verb define
glFeedbackBuffer 1000, GL_3D_COLOR
glRenderMode GL_FEEDBACK
mm GL_PROJECTION
glOrtho 0 5 0 5 0 5
mm GL_MODELVIEW
drawtriangle  t1
glPassThrough 3.3
drawtriangle  t2
drawtriangle  t3
drawtriangle  t4
glFlush''
glRenderMode GL_RENDER
)

a_help_button=: verb define
wd'mb OpenGL *Run selectobjects'' in JX window.'
wd 'setfocus g'
)

a_run''
