User:Brian Schott/code/tankOpengl
This is a partial revision of Alex Kornilovski's tank simulation that worked for J504 but not for J601.
download a script: File:Tank0.ijs
I have revised the previously revised tank.ijs script. It seems
to work for now but has a bad perspective feel because of the scaling
involved, I think. Some of the sensitive parameters are the following.
GS_PERSPECTIVE
GS_UNITSTEP
GS_UNITSCALE
GS_UNITROT
My adjustments required redefining three _jzopenglutil_ verbs: gsinit_jzopenglutil_, gsfly_jzopenglutil_, and gskeys_jzopenglutil_. These revised verbs are near the bottom of the script. The main changes to the first two verbs involves activating the global GS_VIEWLINE.
In gskeys_jzopenglutil_ I have attempted to salvage the gldemo keys somewhat, but tried to incorporate some of the control Alex wanted. The final mixture is not pretty, but is livable for now especially because the cursor keys work somewhat intuitively, and that to me is key for a tank/helicopter. To leave a bread crumb trail as to how the keys and other adjustments were made, I have left whole NB.'d lines in the script. I mostly swapped the Up/Down cursor keys with the i/o keys and reversed the shifted/unshifted functionality of the Rt/Lt keys and the k/K keys.
I left in the script slightly adjusted versions of Alex's opengl_g_char and handle_tank_keys, but neither are actually used at present in favor of the revision to gskeys_jzopenglutil_ .
Please revise and improve this code.
NB. Standalone version of the opengl demo. require 'opengl' cocurrent 'gldemo' coinsert 'jzopenglutil' OPENGL=: 0 : 0 pc opengl closeok; xywh 0 0 200 150;cc g isigraph rightmove bottommove; pas 0 0; rem form end; ) opengl_run=: 3 : 0 wd OPENGL ogl=: ''conew'jzopengl' demo '' paint'' wd'pshow;' ) opengl_close=: 3 : 0 destroy__ogl'' wd 'pclose' ) opengl_cancel=: opengl_close NB. ========================================================= NB. Helicopter demo by Alex Kornilovski NB. NB. Note that this redefines the movement keys. NB. use the cursor keys to move around NB. use SHIFT cursor keys to move around FAST NB. W,S - move Up-Down NB. changed below NB. Q,A - Look Up-Down NB. changed below NB. above was Alex's intention, below is what works here NB. K,k - Look Up-Down NB. o,i - move Up-Down NB. O,I - move Up-Down FAST NB. ========================================================= demo=: verb define GS_AMBIENT=: 0.75 GS_DIFFUSE=: 0.75 GS_SPECULAR=: 1 Init '' ) paint =: monad define gsinit GS_LIGHT,GS_BLEND Piller '' glPushMatrix'' Ground '' glPopMatrix'' DrawView '' NB. getnewXYZ 1 NB. this was a key flaw in my first post gsfini'' ) NB. ========================================================= NB. This works but is not used now opengl_g_char=: 3 : 0 keyb=. (a.{~128+33 34 37 39 38 40),'qawsQAWS' keyb=. (a.{~16 17 20 22 21 23 ),'qawsQAWS' NB. May be Mac specific keyi=. 33 34 37 39 38 40,a.i.'QAWSQAWS' n=. keyi#~keyb=0{sysdata 0 handle_tank_key n paint'' ) NB. ========================================================= NB. This works but is not used now handle_tank_key=: 4 : 0 " 0 s=. x NB. ignored k=. y select. k case. 37;39 do. NB. Turn left-right THETA=: THETA+0.1*gschgsign 1-2*k=39 GS_VIEWLINE=: GS_VIEWXYZ +GS_SPEED* ((cos GAMMA)*sin THETA),(sin GAMMA),-(cos GAMMA)*cos THETA case. 38;40 do. NB. move forward-backward getnewXYZ gschgsign 1-2*k=38 case. <@>49+i.10 do. NB. speed GS_SPEED=: GS_SPEEDS {~ k-49 case. 81;65 do. NB. Look UP-Down GAMMA=: GAMMA+0.05*gschgsign 1-2*k=81 if. GAMMA>0.48p1 do. GAMMA=: 0.48p1 end. if. GAMMA<_0.48p1 do. GAMMA=: _0.48p1 end. GS_VIEWLINE=: GS_VIEWXYZ +GS_SPEED* ((cos GAMMA)*sin THETA),(sin GAMMA),-(cos GAMMA)*cos THETA case. 87;83 do. NB. Move UP-Down GS_VIEWXYZ=: ((1{GS_VIEWXYZ)+gschgsign 1-2*k=87) (1)}GS_VIEWXYZ GS_VIEWLINE=: ((1{GS_VIEWLINE)+gschgsign 1-2*k=87) (1)}GS_VIEWLINE end. ) NB. ========================================================= Init=: verb define GS_VIEWLINE=: 0 0 0 GAMMA=: THETA=: 0 GS_VIEWXYZ=: 0 0 5 GS_PERSPECTIVE=: 35 1 1 2000 NB. 2000 needed for some reason GS_UNITROT=:3 GS_UNITSCALE=:0.2 NB. controls zoom speed GS_UNITSTEP=:0.25 NB. controls jkl speed GS_SPEED=: 4 PILLER=: 2042 Gempos=: _500+?15 2$1000 Stonepos=: _500+?15 2$1000 Pillerpos=: _500+?15 2$1000 TAR=: 99 ) NB. ========================================================= getnewXYZ=: verb define GS_VIEWXYZ=: GS_VIEWXYZ+ GS_SPEED*y*(sin THETA),0,-cos THETA GS_VIEWLINE=: GS_VIEWXYZ +GS_SPEED* ((cos GAMMA)*sin THETA),(sin GAMMA),-(cos GAMMA)*cos THETA ) NB. ========================================================= Ground=: 3 : 0 nStep=. 30 gscolor 0,255,0 glNormal 0,1,0 glBegin GL_LINES r=. _500+20*i. 1+ >.1000%20 h=. r , each <0 _500 s=. h ,each r, each <0 500 s=. ((2*#h),3)$,>s glVertex s h=. (<500 0) ,each r s=. h,each (<_500 0), each r s=. ((2*#h),3)$,>s glVertex s glEnd '' ) NB. ========================================================= DrawView=: verb define for_j. i.#Gempos do. glPushMatrix '' glTranslate ((<j,0){Gempos) ,22,(<j,1){Gempos glScale _5+30 30 30 gsdrawdodecahedron'' glPopMatrix '' end. for_j. i.#Stonepos do. glPushMatrix '' glTranslate ((<j,0){Stonepos) ,20,(<j,1){Stonepos glScale _5+30 30 30 gsdrawicosahedron '' glPopMatrix '' end. for_j. i.#Pillerpos do. glPushMatrix '' glTranslate ((<j,0){Pillerpos) ,20,(<j,1){Pillerpos glCallList PILLER glPopMatrix '' end. ) NB. ========================================================= Piller=: verb define QUADS=: {.>gluNewQuadric'' vNormal=. 1 fStep=. o.1%4 fHeight=. 20 fRadius=. 5 gsnewlist PILLER glPushMatrix '' gscolor 100,0,228 glBegin GL_QUAD_STRIP vNormal=. gsunitnormal fRadius,fHeight,0 glNormal vNormal glVertex fRadius,fHeight,0 glVertex fRadius,-fHeight,0 for_j. fStep*i.1+>.o.3%fStep do. x=. fRadius*sin j z=. fRadius*cos j vNormal=. gsunitnormal x,fHeight,z glNormal vNormal glVertex x,fHeight,z glVertex x,(-fHeight),z end. glEnd '' glTranslate 0,(fHeight+fRadius%2),0 gscolor 0,0,255 gsdrawsphere (fRadius*2.5),20 20 glPopMatrix '' gsendlist'' gluDeleteQuadric QUADS ) NB. ========================================================= Target=: verb define NB. Draw point of look NB. This verb has never functioned as far as I can tell gscolor 255,0,0 glLineWidth 3 glBegin GL_LINES new=. GS_VIEWXYZ +GS_SPEED* ((cos GAMMA)*sin THETA+0.02),(sin GAMMA),-(cos GAMMA)*cos THETA+0.02 new1=. GS_VIEWXYZ +GS_SPEED* ((cos GAMMA)*sin THETA-0.02),(sin GAMMA),-(cos GAMMA)*cos THETA-0.02 glVertex 2 3$GS_VIEWLINE,new glVertex 2 3$GS_VIEWLINE,new1 new=. GS_VIEWXYZ +GS_SPEED* ((cos GAMMA+0.02)*sin THETA),(sin GAMMA+0.02),-(cos GAMMA+0.02)*cos THETA new1=. GS_VIEWXYZ +GS_SPEED* ((cos GAMMA-0.02)*sin THETA),(sin GAMMA-0.02),-(cos GAMMA-0.02)*cos THETA glVertex 2 3$GS_VIEWLINE,new glVertex 2 3$GS_VIEWLINE,new1 glEnd '' glLineWidth 1 ) gsdrawsphere =: 3 : 0 obj=. QUADS gluQuadricDrawStyle obj,GLU_FILL gluQuadricNormals obj,GLU_SMOOTH gluSphere <&> obj,y ) NB. ========================================================= opengl_g_paint=: gspaint opengl_g_char=: gschar opengl_default=: gsdefault NB. ========================================================= gsfly_jzopenglutil_=: 4 : 0 rot=. (rfd x) gsrotxyz y GS_VIEWXYZ=: rot gsmp GS_VIEWXYZ GS_VIEWUP=: rot gsmp GS_VIEWUP GS_VIEWLINE=: rot gsmp GS_VIEWLINE ) gskey_jzopenglutil_=: 4 : 0 select. y case. 33;34 do. ((x { 1 5) * GS_SPEED*GS_UNITROT gschgsign y=34) gsfly GS_VIEWXYZ NB. case. 37;39 do. NB. ((x { 1 5) * GS_SPEED*GS_UNITROT gschgsign y=37) gsfly GS_VIEWUP case. 37;39 do. ((x { 1 5) * GS_SPEED*GS_UNITROT gschgsign y=39) gsfly GS_VIEWUP NB. case. 38;40 do. NB. ((x { 1 5) * GS_SPEED*GS_UNITROT gschgsign y=40) gsfly GS_VIEWXYZ gscross GS_V IEWUP case. 73;79 do. ((x { 1 5) * GS_SPEED*GS_UNITROT gschgsign y=73) gsfly GS_VIEWXYZ gscross GS_VIEWUP case. 88;89;90 do. b=. y=88 89 90 (b i. 1) gsrotate GS_SPEED*GS_UNITROT * b gschgsign x NB. case. 73;79 do. gszoom (>:GS_SPEED*GS_UNITSCALE) ^ {. (x { 1 5) gschgsign y=73 case. 38;40 do. gszoom (>:GS_SPEED*GS_UNITSCALE) ^ {. (x { 1 5) gschgsign y=38 case. 74;75;76 do. gstranslate GS_SPEED*GS_UNITSTEP * (y=74 75 76) gschgsign x case. <@>49+i.10 do. GS_SPEED=: GS_SPEEDS {~ y-49 end. ) gsinit_jzopenglutil_ =: 3 : 0 res=. rc__ogl'' if. res do. glEnable GL_DEPTH_TEST glViewport 0 0,wh__ogl glMatrixMode GL_PROJECTION glLoadIdentity'' gluPerspective (%/wh__ogl) 1 } GS_PERSPECTIVE if. GS_LIGHT e. y do. id=. GL_LIGHT0 glaLight id, GL_AMBIENT, gscolor4 GS_AMBIENT glaLight id, GL_DIFFUSE, gscolor4 GS_DIFFUSE glaLight id, GL_SPECULAR, gscolor4 GS_SPECULAR glEnable GL_LIGHTING glEnable id if. GS_COLORMATERIAL do. glEnable GL_COLOR_MATERIAL glColorMaterial GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE glaMaterial GL_FRONT_AND_BACK,GL_SPECULAR, alpha4 GS_MATSPECULAR glaMaterial GL_FRONT_AND_BACK,GL_SHININESS, alpha4 GS_SHININESS end. if. #GS_POSITION do. glaLight id,GL_POSITION, alpha4 GS_POSITION end. end. if. GS_BLEND e. y do. glEnable GL_BLEND glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA end. else. current__ogl '' end. gsclearcolor GS_CLEARCOLOR glClear GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT gscolor GS_COLOR glMatrixMode GL_MODELVIEW glLoadIdentity'' gluLookAt GS_VIEWXYZ,GS_VIEWLINE,GS_VIEWUP glTranslate GS_TRNXYZ glRotate GS_ROTNDX { GS_ROTXYZ ,. GS_ID3 res ) opengl_run''