2005-05-22 22:54:14 -04:00
|
|
|
! 3d surface plotter.
|
|
|
|
!
|
|
|
|
! To run this code, bootstrap Factor like so:
|
|
|
|
!
|
|
|
|
! ./f boot.image.le32
|
|
|
|
! -libraries:sdl:name=libSDL.so
|
|
|
|
! -libraries:sdl-gfx:name=libSDL_gfx.so
|
|
|
|
!
|
|
|
|
! (But all on one line)
|
|
|
|
!
|
|
|
|
! Then, start Factor as usual (./f factor.image) and enter this
|
|
|
|
! at the listener:
|
|
|
|
!
|
|
|
|
! "contrib/gl/load.factor" run-file
|
|
|
|
! "examples/plot3d.factor" run-file
|
|
|
|
|
|
|
|
IN: plot3d
|
2005-08-13 23:39:46 -04:00
|
|
|
USING: alien compiler errors gl kernel lists math namespaces
|
|
|
|
prettyprint sdl sequences vectors ;
|
2005-05-22 22:54:14 -04:00
|
|
|
|
|
|
|
: display-list 1 ;
|
|
|
|
|
2005-08-13 23:39:46 -04:00
|
|
|
: matrix-get ( i j matrix -- ) swapd nth nth ;
|
|
|
|
|
2005-05-22 22:54:14 -04:00
|
|
|
: plot-vertex ( matrix i j -- )
|
2005-08-13 23:39:46 -04:00
|
|
|
rot matrix-get 3unseq glVertex3f ;
|
2005-05-22 22:54:14 -04:00
|
|
|
|
|
|
|
: plot-face ( matrix i j -- face )
|
|
|
|
GL_QUADS glBegin
|
|
|
|
[ rot matrix-get ] 3keep
|
|
|
|
[ 1 + rot matrix-get v- ] 3keep
|
|
|
|
[ rot matrix-get ] 3keep
|
2005-08-13 23:39:46 -04:00
|
|
|
[ >r 1 + r> rot matrix-get v- cross normalize 3unseq glNormal3f ] 3keep
|
2005-05-22 22:54:14 -04:00
|
|
|
[ plot-vertex ] 3keep
|
|
|
|
[ 1 + plot-vertex ] 3keep
|
|
|
|
[ >r 1 + r> 1 + plot-vertex ] 3keep
|
|
|
|
>r 1 + r> plot-vertex
|
|
|
|
glEnd ;
|
|
|
|
|
2005-08-11 19:08:22 -04:00
|
|
|
: 2repeat ( i j quot -- | quot: i j -- i j )
|
|
|
|
rot [
|
|
|
|
rot [ [ rot dup slip -rot ] repeat ] keep -rot
|
|
|
|
] repeat 2drop ; inline
|
|
|
|
|
2005-05-22 22:54:14 -04:00
|
|
|
: plot-faces ( points -- )
|
2005-08-13 23:39:46 -04:00
|
|
|
dup length 1 - over first length 1 - [
|
2005-05-22 22:54:14 -04:00
|
|
|
3dup plot-face
|
|
|
|
] 2repeat drop ;
|
|
|
|
|
|
|
|
SYMBOL: theta
|
|
|
|
|
|
|
|
: plot-axes ( -- )
|
|
|
|
GL_LIGHTING glDisable
|
|
|
|
1.0 1.0 1.0 glColor3f
|
|
|
|
GL_LINES glBegin
|
|
|
|
0 0 0 glVertex3f
|
|
|
|
1 0 0 glVertex3f
|
|
|
|
0 0 0 glVertex3f
|
|
|
|
-1 0 0 glVertex3f
|
|
|
|
0 0 0 glVertex3f
|
|
|
|
0 1 0 glVertex3f
|
|
|
|
0 0 0 glVertex3f
|
|
|
|
0 -1 0 glVertex3f
|
|
|
|
0 0 0 glVertex3f
|
|
|
|
0 0 1 glVertex3f
|
|
|
|
0 0 0 glVertex3f
|
|
|
|
0 0 -1 glVertex3f
|
|
|
|
glEnd
|
|
|
|
GL_LIGHTING glEnable ;
|
|
|
|
|
|
|
|
: i/j>x/y ( i j -- x y )
|
|
|
|
swap 15 - 30 / swap 15 - 30 / ;
|
|
|
|
|
|
|
|
: max-z ( seq -- z )
|
2005-08-13 23:39:46 -04:00
|
|
|
0.1 [ third max ] reduce ;
|
2005-05-22 22:54:14 -04:00
|
|
|
|
|
|
|
: min-z ( seq -- z )
|
2005-08-13 23:39:46 -04:00
|
|
|
-0.1 [ third min ] reduce ;
|
2005-05-22 22:54:14 -04:00
|
|
|
|
|
|
|
: normalize-points ( seq -- )
|
2005-08-13 23:39:46 -04:00
|
|
|
dup min-z over [ over >r 3unseq r> - 3vector ] nmap drop
|
|
|
|
dup max-z swap [ over >r 3unseq r> / 3vector ] nmap drop ;
|
2005-05-22 22:54:14 -04:00
|
|
|
|
|
|
|
: valuate-points ( quot -- matrix )
|
2005-08-13 23:39:46 -04:00
|
|
|
30 [
|
|
|
|
( quot i )
|
|
|
|
30 [
|
|
|
|
( quot i j )
|
|
|
|
[ 3dup i/j>x/y rot call ] 2keep i/j>x/y rot 3vector nip
|
|
|
|
] map 2nip
|
|
|
|
] map-with ; inline
|
2005-05-22 22:54:14 -04:00
|
|
|
|
|
|
|
: make-plot
|
2005-08-13 23:39:46 -04:00
|
|
|
[ rect> real ] valuate-points
|
|
|
|
dup [ normalize-points ] each
|
2005-05-22 22:54:14 -04:00
|
|
|
display-list GL_COMPILE glNewList
|
|
|
|
plot-faces
|
|
|
|
plot-axes
|
|
|
|
glEndList ;
|
|
|
|
|
|
|
|
: flags
|
|
|
|
SDL_OPENGL SDL_RESIZABLE bitor SDL_HWSURFACE bitor SDL_DOUBLEBUF bitor ;
|
|
|
|
|
|
|
|
: fov 60.0 ; inline
|
|
|
|
: near 0.1 ; inline
|
|
|
|
: far 100.0 ; inline
|
|
|
|
|
|
|
|
: >float-array ( seq -- float-array )
|
|
|
|
dup length <float-array> over length [
|
|
|
|
[ tuck >r >r swap nth r> r> swap set-float-nth ] 3keep
|
|
|
|
] repeat nip ;
|
|
|
|
|
|
|
|
: init-gl
|
|
|
|
GL_PROJECTION glMatrixMode
|
|
|
|
GL_DEPTH_TEST glEnable
|
|
|
|
GL_LIGHTING glEnable
|
|
|
|
GL_LIGHT0 glEnable
|
|
|
|
GL_LIGHT1 glEnable
|
|
|
|
glLoadIdentity
|
|
|
|
fov width get height get /f near far gluPerspective
|
|
|
|
GL_LIGHT0 GL_POSITION [ 1.0 1.0 -2.0 1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT0 GL_DIFFUSE [ 1.0 0.5 0.0 1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT0 GL_SPECULAR [ 1.0 0.5 1.0 1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT0 GL_AMBIENT [ 1.0 1.0 0.5 1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT1 GL_POSITION [ 1.0 3.0 2.0 -1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT1 GL_DIFFUSE [ 1.0 0.5 0.3 1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT1 GL_SPECULAR [ 1.0 1.0 0.5 1.0 ] >float-array glLightfv
|
|
|
|
GL_LIGHT1 GL_AMBIENT [ 0.0 0.0 1.0 1.0 ] >float-array glLightfv
|
|
|
|
GL_MODELVIEW glMatrixMode
|
|
|
|
glLoadIdentity
|
|
|
|
GL_SMOOTH glShadeModel
|
|
|
|
|
|
|
|
0.0 0.0 0.0 0.0 glClearColor
|
|
|
|
1.0 0.0 0.0 glColor3f ;
|
|
|
|
|
|
|
|
: render ( -- )
|
|
|
|
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
|
|
|
|
GL_MODELVIEW glMatrixMode
|
|
|
|
glLoadIdentity
|
|
|
|
0.0 -0.5 -1.5 glTranslatef
|
|
|
|
-45 1 0 0 glRotatef
|
|
|
|
theta get 0 0 1 glRotatef
|
|
|
|
display-list glCallList
|
|
|
|
SDL_GL_SwapBuffers ;
|
|
|
|
|
|
|
|
: event-loop ( event -- )
|
|
|
|
theta [ 1 + ] change
|
|
|
|
render
|
|
|
|
dup SDL_PollEvent [
|
|
|
|
dup event-type SDL_QUIT = [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
event-loop
|
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
event-loop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: plot3d ( -- )
|
|
|
|
1024 768 16 flags [
|
|
|
|
init-gl
|
|
|
|
0 theta set
|
|
|
|
make-plot
|
|
|
|
<event> event-loop SDL_Quit
|
|
|
|
] with-screen ;
|
2005-07-22 23:39:28 -04:00
|
|
|
|
|
|
|
plot3d
|