NB. OpenGL viewer

cocurrent 'jgldemo'

j=. 'TITLE BASIC1 BASIC2 BASIC3 BASIC4 BASIC5'
j=. j,' COLORED JBOX MODEL TEAPOT TRIANGLE'
j=. j,' TEXTB1'
j=. j,' TANK SOLAR'
j=. j,' BRAID LISSAJOU TREFOIL TORUS'
j=. j,' BUCKY SOCCER'
j=. j,' DINI ENNEPERS KUEN MOEBIUS SELLIPSE UMBTORUS WHITNEY'
j=. j,' HELIX INTSECT1 SEASHELL'
j=. j,' BOYS CROSSCAP KLEIN ROMAN CUPS DRUM'
GLDEMONAMES=: ;:j

GLDEMOVER=: 1.6
GLDEMOSEL=: 'TITLE'
GLDEMOPATH=: jpath '~system\examples\graphics\opengl\demo\'
GLSLIDES=: 0

GLHANDLERS=: <;._2 (0 : 0)
opengl_g_char
opengl_g_mbldown
)

NB. =========================================================
glresethandlers=: 3 : 0
b=. GLHANDLERS e. nl 3
4!:55 b#GLHANDLERS
)

NB. =========================================================
OPENGLDEMO=: 0 : 0
pc opengl closeok;pn "J Graphics";
menupop "&Options";
menu view "&View Definition" "" "" "";
menusep ;
menu clip "&Clip" "" "" "";
menusep ;
menu savebmp "&Save opengl.bmp" "" "" "";
menusep ;
menu print "&Print" "" "" "";
menusep ;
menu slides "&Slide Show" "" "" "";
menusep ;
menu exit "E&xit" "" "" "";
menupopz;
menupop "&Basic";
menu MTITLE "OpenGL Graphics" "" "" "";
menusep ;
menu MBASIC1 "&Simple" "" "" "";
menu MBASIC2 "&Lighting" "" "" "";
menu MBASIC3 "&Two Objects" "" "" "";
menu MBASIC4 "&Viewing" "" "" "";
menu MBASIC5 "&Cylinders" "" "" "";
menusep ;
menu MCOLORED "C&olors" "" "" "";
menu MJBOX "&J Logo" "" "" "";
menu MMODEL "&Model" "" "" "";
menu MTEAPOT "Tea&pot" "" "" "";
menu MTRIANGLE "T&riangles" "" "" "";
menusep ;
menupop "Te&xtures";
menu MTEXTB1 "&Basic" "" "" "";
menupopz;
menusep ;
menu MTANK "Tan&k" "" "" "";
menu MSOLAR "So&lar" "" "" "";
menupopz;
menupop "&Knots";
menu MBRAID "&Braid" "" "" "";
menu MLISSAJOU "&Lissajous" "" "" "";
menu MTREFOIL "&Trefoil" "" "" "";
menu MTORUS "T&orus" "" "" "";
menupopz;
menupop "&Models";
menu MBUCKY "&Bucky Ball" "" "" "";
menu MSOCCER "&Soccer Ball" "" "" "";
menupopz;
menupop "&Surface";
menu MDINI "&Dini" "" "" "";
menu MENNEPERS "&Enneper" "" "" "";
menu MKUEN "&Kuen" "" "" "";
menu MMOEBIUS "&Moebius" "" "" "";
menu MSELLIPSE "&Super Ellipse" "" "" "";
menu MUMBTORUS "&Umbilic Torus" "" "" "";
menu MWHITNEY "&Whitney" "" "" "";
menusep ;
menu MHELIX "&Helix" "" "" "";
menu MINTSECT1 "&Intersection" "" "" "";
menu MSEASHELL "&Seashell" "" "" "";
menusep ;
menu MBOYS "&Boys" "" "" "";
menu MCROSSCAP "&Crosscap" "" "" "";
menu MKLEIN "&Klein" "" "" "";
menu MROMAN "&Roman" "" "" "";
menusep ;
menu MCUPS "Cu&ps" "" "" "";
menu MDRUM "&Drum" "" "" "";
menupopz;
menupop "&Help";
menu MF12 "&F12 Next" "" "" "";
menu MF12S "&Shift F12 Previous" "" "" "";
menusep ;
menu movement "&Movement" "" "" "";
menusep ;
menu about "&About" "" "" "";
menupopz;
xywh 0 0 200 150;cc g isigraph ws_clipsiblings ws_clipchildren rightmove bottommove;
pas 0 0;
rem form end;
)

NB. =========================================================
opengl_about_button=: 3 : 0
j=. 'OpenGL Graphics Demo V',(4.2 ": GLDEMOVER)
wdinfo 'OpenGL Graphics';j
)

NB. =========================================================
opengl_default=: 3 : 0
if. systype -: 'button' do.
  name=. }.syschild
  if. name -: GLDEMOSEL do.
    wd 'set M',GLDEMOSEL,' 1'
    return.
  end.
  if. (<name) e. GLDEMONAMES do.
    opengl_showname name
  end.
end.
''
)

NB. =========================================================
opengl_f12_fkey=: 3 : 0
ndx=. (#GLDEMONAMES) | >: GLDEMONAMES i. <GLDEMOSEL
opengl_showname ndx pick GLDEMONAMES
)

NB. =========================================================
opengl_f12shift_fkey=: 3 : 0
ndx=. (#GLDEMONAMES) | <: GLDEMONAMES i. <GLDEMOSEL
opengl_showname ndx pick GLDEMONAMES
)

NB. =========================================================
opengl_MF12_button=: opengl_f12_fkey
opengl_MF12S_button=: opengl_f12shift_fkey

NB. =========================================================
opengl_showname=: 3 : 0
if. 0=wdisparent <'opengl' do. return. end.
wd 'psel opengl'
wd 'set M',GLDEMOSEL,' 0'
GLDEMOSEL=: y.
wd 'set M',GLDEMOSEL,' 1'
opengl_run''
)

NB. =========================================================
opengl_slides_button=: 3 : 0
GLSLIDES=: -. GLSLIDES
wd 'set slides ',":GLSLIDES
if. GLSLIDES do.
  wd 'timer 3000'
  sys_timer_z_=: gldemo_slides_jgldemo_
else.
  wd 'timer 0'
  sys_timer_z_=: ]
end.
empty''
)

NB. =========================================================
opengl_nextslide=: 3 : 0
ndx=. GLDEMONAMES i. <GLDEMOSEL
ndx=. (#GLDEMONAMES) | >:ndx
ndx=. ndx + (ndx{GLDEMONAMES) e. <'SOLAR'
opengl_showname ndx pick GLDEMONAMES
)

NB. =========================================================
gldemo_slides=: 3 : 0
try.
  opengl_nextslide''
catch.
  wd 'timer 0'
  sys_timer_z_=: ]
end.
)

NB. =========================================================
opengl_exit_button=: 3 : 0
wd 'psel opengl;pclose;'
try. wd 'psel gledit;pclose' catch. end.
)

NB. =========================================================
opengl_movement_button=: 3 : 0
htmlhelp_j_ 'user\movement_keys.htm'
)

NB. =========================================================
opengl_run=: 3 : 0
opengl_run1 GLDEMODAT=: fread GLDEMOPATH,tolower GLDEMOSEL,'.ijs'
)

NB. =========================================================
opengl_run1=: 3 : 0
glreset''
glresethandlers''
0!:100 y.
opengl_init''
demo''
wd 'setfocus g'
opengl_g_size''
glpaintx''
)

NB. =========================================================
opengl_view_button=: 3 : 0
gledit_run GLDEMODAT
)

NB. =========================================================
opengl_clipper=: 4 : 0
lists=. LISTS
quads=. QUADS
nurbs=. NURBS
LISTS=: QUADS=: NURBS=: ''
gldraw=: demo
if. 0=4!:0 <'CLIPSIZE' do.
  s=. CLIPSIZE
else.
  s=. 1024 768
end.
y. glwritebmp s
LISTS=: lists
QUADS=: quads
NURBS=: nurbs
if. x. do. printbmp y. end.
)

NB. =========================================================
opengl_clip_button=: 0 & opengl_clipper bind ''
opengl_print_button=: 1 & opengl_clipper bind (jpath '~temp\gltemp.bmp')
opengl_savebmp_button=: 0 & opengl_clipper bind (jpath '~temp\opengl.bmp')

NB. =========================================================
opengl_init=: 3 : 0
wd 'psel opengl'
glaRC''
if. #LISTS do.
  try. glDeleteLists (<./LISTS),#LISTS catch. end.
end.
LISTS=: ''
if. #NURBS do.
  try. gluDeleteNurbsRenderer each NURBS catch. end.
end.
NURBS=: ''
glsnap''
opengl_g_paint=: stdpaint
opengl_g_size=: stdsizeaspect
glEnable GL_DEPTH_TEST
glClearColor color4 CLEARCOLOR
glcolor color4 COLOR
)

NB. =========================================================
opengldemo=: 3 : 0
try. wd'psel opengl;pclose;' catch. end.
wd OPENGLDEMO
if. #GLDEMOSEL do.
  wd 'set M',GLDEMOSEL,' 1'
else.
  wd 'set MTITLE 1'
end.
opengl_run''
wd 'pcenter;pshow'
)
