]> git.jsancho.org Git - guile-irrlicht.git/blob - irrlicht/scene.scm
Split public functions
[guile-irrlicht.git] / irrlicht / scene.scm
1 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
2 ;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; This file is part of guile-irrlicht.
5 ;;;
6 ;;; Guile-irrlicht is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation; either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; Guile-irrlicht is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with guile-irrlicht.  If not, see
18 ;;; <http://www.gnu.org/licenses/>.
19
20
21 (define-module (irrlicht scene)
22   #:use-module (ice-9 match)
23   #:use-module (system foreign)
24   #:use-module ((irrlicht bindings core) #:prefix ffi-core:)
25   #:use-module ((irrlicht bindings scene) #:prefix ffi-scene:)
26   #:use-module ((irrlicht bindings video) #:prefix ffi-video:)
27   #:export (add-animated-mesh-scene-node
28             add-camera-scene-node
29             add-camera-scene-node-fps!
30             add-octree-scene-node-am
31             get-mesh
32             scene-draw-all
33             set-material-flag-am!
34             set-material-texture-am!
35             set-md2-animation!
36             set-position!))
37
38 (define* (add-animated-mesh-scene-node scene-manager mesh
39                                        #:key
40                                        (parent %null-pointer)
41                                        (id -1)
42                                        (position '(0 0 0))
43                                        (rotation '(0 0 0))
44                                        (scale '(1 1 1))
45                                        (also-add-if-mesh-pointer-zero #f))
46   (let ((node (ffi-scene:add-animated-mesh-scene-node
47                scene-manager
48                mesh
49                parent
50                id
51                (make-c-struct ffi-core:vector3df position)
52                (make-c-struct ffi-core:vector3df rotation)
53                (make-c-struct ffi-core:vector3df scale)
54                (if also-add-if-mesh-pointer-zero 1 0))))
55     (if (null-pointer? node) #f node)))
56
57 (define* (add-camera-scene-node scene-manager
58                                 #:key
59                                 (parent %null-pointer)
60                                 (position '(0 0 0))
61                                 (lookat '(0 0 100))
62                                 (id -1)
63                                 (make-active #t))
64   (let ((camera (ffi-scene:add-camera-scene-node
65                  scene-manager
66                  parent
67                  (make-c-struct ffi-core:vector3df position)
68                  (make-c-struct ffi-core:vector3df lookat)
69                  id
70                  (if make-active 1 0))))
71     (if (null-pointer? camera) #f camera)))
72
73 (define* (add-camera-scene-node-fps! scene-manager
74                                      #:key
75                                      (parent %null-pointer)
76                                      (rotate-speed 100.0)
77                                      (move-speed 0.5)
78                                      (id -1)
79                                      (key-map-array %null-pointer)
80                                      (key-map-size 0)
81                                      (no-vertical-movement #f)
82                                      (jump-speed 0.0)
83                                      (invert-mouse #f)
84                                      (make-active #t))
85   (ffi-scene:add-camera-scene-node-fps
86    scene-manager
87    parent
88    rotate-speed
89    move-speed
90    id
91    key-map-array
92    key-map-size
93    (if no-vertical-movement 1 0)
94    jump-speed
95    (if invert-mouse 1 0)
96    (if make-active 1 0)))
97
98 (define* (add-octree-scene-node-am scene-manager mesh
99                                    #:key
100                                    (parent %null-pointer)
101                                    (id -1)
102                                    (minimal-polys-per-node 512)
103                                    (also-add-if-mesh-pointer-zero #f))
104   (ffi-scene:add-octree-scene-node-am
105    scene-manager
106    mesh
107    parent
108    id
109    minimal-polys-per-node
110    (if also-add-if-mesh-pointer-zero 1 0)))
111
112 (define (get-mesh scene-manager filename)
113   (let ((mesh (ffi-scene:get-mesh scene-manager (string->pointer filename))))
114     (if (null-pointer? mesh) #f mesh)))
115
116 (define (scene-draw-all scene-manager)
117   (ffi-scene:draw-all scene-manager))
118
119 (define (set-material-flag-am! node flag newvalue)
120   (let ((material-flag
121          (match flag
122                 ('wireframe ffi-video:EMF_WIREFRAME)
123                 ('pointcloud ffi-video:EMF_POINTCLOUD)
124                 ('gouraud-shading ffi-video:EMF_GOURAUD_SHADING)
125                 ('lighting ffi-video:EMF_LIGHTING)
126                 ('zbuffer ffi-video:EMF_ZBUFFER)
127                 ('zwrite-enable ffi-video:EMF_ZWRITE_ENABLE)
128                 ('back-face-culling ffi-video:EMF_BACK_FACE_CULLING)
129                 ('front-face-culling ffi-video:EMF_FRONT_FACE_CULLING)
130                 ('bilinear-filter ffi-video:EMF_BILINEAR_FILTER)
131                 ('trilinear-filter ffi-video:EMF_TRILINEAR_FILTER)
132                 ('anisotropic-filter ffi-video:EMF_ANISOTROPIC_FILTER)
133                 ('fog-enable ffi-video:EMF_FOG_ENABLE)
134                 ('normalize-normals ffi-video:EMF_NORMALIZE_NORMALS)
135                 ('texture-wrap ffi-video:EMF_TEXTURE_WRAP)
136                 ('anti-aliasing ffi-video:EMF_ANTI_ALIASING)
137                 ('color-mask ffi-video:EMF_COLOR_MASK)
138                 ('color-material ffi-video:EMF_COLOR_MATERIAL)
139                 ('use-mip-maps ffi-video:EMF_USE_MIP_MAPS)
140                 ('blend-operation ffi-video:EMF_BLEND_OPERATION)
141                 ('polygon-offset ffi-video:EMF_POLYGON_OFFSET))))
142     (ffi-scene:set-material-flag-am
143      node
144      material-flag
145      (if newvalue 1 0))))
146
147 (define (set-material-texture-am! node texture-layer texture)
148   (ffi-scene:set-material-texture-am node texture-layer texture))
149
150 (define (set-md2-animation! node anim)
151   (let ((animation-type
152          (match anim
153                 ('stand ffi-scene:EMAT_STAND)
154                 ('run ffi-scene:EMAT_RUN)
155                 ('attack ffi-scene:EMAT_ATTACK)
156                 ('pain-a ffi-scene:EMAT_PAIN_A)
157                 ('pain-b ffi-scene:EMAT_PAIN_B)
158                 ('pain-c ffi-scene:EMAT_PAIN_C)
159                 ('jump ffi-scene:EMAT_JUMP)
160                 ('flip ffi-scene:EMAT_FLIP)
161                 ('salute ffi-scene:EMAT_SALUTE)
162                 ('fallback ffi-scene:EMAT_FALLBACK)
163                 ('wave ffi-scene:EMAT_WAVE)
164                 ('point ffi-scene:EMAT_POINT)
165                 ('crouch-stand ffi-scene:EMAT_CROUCH_STAND)
166                 ('crouch-walk ffi-scene:EMAT_CROUCH_WALK)
167                 ('crouch-attack ffi-scene:EMAT_CROUCH_ATTACK)
168                 ('crouch-pain ffi-scene:EMAT_CROUCH_PAIN)
169                 ('crouch-death ffi-scene:EMAT_CROUCH_DEATH)
170                 ('death-fallback ffi-scene:EMAT_DEATH_FALLBACK)
171                 ('death-fallforward ffi-scene:EMAT_DEATH_FALLFORWARD)
172                 ('death-fallbackslow ffi-scene:EMAT_DEATH_FALLBACKSLOW)
173                 ('boom ffi-scene:EMAT_BOOM)
174                 ('count ffi-scene:EMAT_COUNT))))
175     (ffi-scene:set-md2-animation
176      node
177      animation-type)))
178
179 (define (set-position! node newpos)
180   (ffi-scene:set-position
181    node
182    (make-c-struct ffi-core:vector3df newpos)))