next up previous
Up: Design of an OpenGL Previous: A. Remaining C code

B. Remaining code in J

vertices =. 8 3 $ _1.0 _1.0 _1.0  1.0 _1.0 _1.0 1.0 1.0 _1.0 
                  _1.0 1.0 _1.0   _1.0 _1.0 1.0 1.0 _1.0 1.0
                  1.0 1.0 1.0 _1.0 1.0 1.0

normals =. 8 3 $ _1.0 _1.0 _1.0 1.0 _1.0 _1.0 1.0 1.0 _1.0
                 _1.0 1.0 _1.0  _1.0 _1.0 1.0 1.0 _1.0 1.0
                 1.0 1.0 1.0  _1.0 1.0 1.0

colors =. 8 3 $ 0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0
                0.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0 1.0 0.0 1.0 1.0

polygon monad define script
('a' ; 'b' ; 'c' ; 'd') =. y.

NB. draw a polygon via list of vertices 

gl 'Begin' ; 'GL_POLYGON'
gl 'Color3fv' ; a from colors
gl 'Normal3fv' ; a from normals
gl 'Vertex3fv' ; a from vertices
gl 'Color3fv' ; b from colors
gl 'Normal3fv' ; b from normals
gl 'Vertex3fv' ; b from vertices
gl 'Color3fv' ;  c from colors
gl 'Normal3fv' ; c from normals
gl 'Vertex3fv' ; c from vertices
gl 'Color3fv' ; d from colors
gl 'Normal3fv' ; d from normals
gl 'Vertex3fv' ; d from vertices
gl 'End'
)												
												
colorcube =. monad define script
NB. map vertices to faces 
polygon 0 3 2 1
polygon 2 3 7 6
polygon 0 4 7 3
polygon 1 2 6 5
polygon 4 5 6 7
polygon 0 1 5 4
)

theta =. 0.0 0.0 0.0
axis =. 2


spinCube =. monad define script
NB. Idle callback, spin cube 2 degrees about selected axis
2+ (axis from theta)
if. 360.0 < axis from theta
  do.   360 - axis from theta
end.
display ''
}

mouse =. monad define script
('btn' ; 'state' ; 'x' ; 'y') =. y.
NB. mouse callback, selects an axis about which to rotate 
  if.(btn = GLUT_LEFT_BUTTON) and (state = GLUT_DOWN)
    do.  axis =. 0
  elseif.(btn = GLUT_MIDDLE_BUTTON) and (state = GLUT_DOWN)
    do. axis =. 1
  elseif.(btn = GLUT_RIGHT_BUTTON) and (state = GLUT_DOWN)
    do. axis =. 2
  end.
)

myReshape =. monad define script
('w' ; 'h') =. y.
gl 'Viewport' ; 0 ; 0 ; w ; h
gl 'MatrixMode' ; 'GL_PROJECTION'
gl 'LoadIdentity' 
  if. (w < h) or (w = h)
    do.
      gl 'Ortho' ; _2.0 ; 2.0 ; _2.0 * h % w ; 2.0 * h % w ; _10.0 ; 10.0
    else.
      gl 'Ortho' ; _2.0 * w % h ; 2.0 * w % h ; _2.0 ; 2.0 ; _10.0 ; 10.0
      gl 'MatrixMode' ; 'GL_MODELVIEW'
  end.
)

next up previous
Up: Design of an OpenGL Previous: A. Remaining C code
2002-09-30