]> git.jsancho.org Git - guile-irrlicht.git/blob - irrlicht/scene.scm
Appropiate method bindings
[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   #:use-module (irrlicht util)
28   #:export (add-animated-mesh-scene-node
29             add-camera-scene-node!
30             add-camera-scene-node-fps!
31             add-custom-scene-node!
32             add-octree-scene-node
33             get-mesh
34             get-root-scene-node
35             scene-draw-all
36             set-material-flag!
37             set-material-texture!
38             set-md2-animation!
39             set-position!))
40
41 (define* (add-animated-mesh-scene-node scene-manager mesh
42                                        #:key
43                                        (parent %null-pointer)
44                                        (id -1)
45                                        (position '(0 0 0))
46                                        (rotation '(0 0 0))
47                                        (scale '(1 1 1))
48                                        (also-add-if-mesh-pointer-zero #f))
49   (let ((node (ffi-scene:add-animated-mesh-scene-node
50                scene-manager
51                mesh
52                parent
53                id
54                (make-c-struct ffi-core:vector3df position)
55                (make-c-struct ffi-core:vector3df rotation)
56                (make-c-struct ffi-core:vector3df scale)
57                (bool->integer also-add-if-mesh-pointer-zero))))
58     (if (null-pointer? node) #f node)))
59
60 (define* (add-camera-scene-node! scene-manager
61                                  #:key
62                                  (parent %null-pointer)
63                                  (position '(0 0 0))
64                                  (lookat '(0 0 100))
65                                  (id -1)
66                                  (make-active #t))
67   (let ((camera (ffi-scene:add-camera-scene-node
68                  scene-manager
69                  parent
70                  (make-c-struct ffi-core:vector3df position)
71                  (make-c-struct ffi-core:vector3df lookat)
72                  id
73                  (bool->integer make-active))))
74     (if (null-pointer? camera) #f camera)))
75
76 (define* (add-camera-scene-node-fps! scene-manager
77                                      #:key
78                                      (parent %null-pointer)
79                                      (rotate-speed 100.0)
80                                      (move-speed 0.5)
81                                      (id -1)
82                                      (key-map-array %null-pointer)
83                                      (key-map-size 0)
84                                      (no-vertical-movement #f)
85                                      (jump-speed 0.0)
86                                      (invert-mouse #f)
87                                      (make-active #t))
88   (ffi-scene:add-camera-scene-node-fps
89    scene-manager
90    parent
91    rotate-speed
92    move-speed
93    id
94    key-map-array
95    key-map-size
96    (bool->integer no-vertical-movement)
97    jump-speed
98    (bool->integer invert-mouse)
99    (bool->integer make-active)))
100
101 (define (add-custom-scene-node! parent
102                                 scene-manager
103                                 id
104                                 custom-render)
105   (ffi-scene:add-custom-scene-node
106    parent
107    scene-manager
108    id
109    (procedure->pointer void custom-render '())))
110
111 (define* (add-octree-scene-node scene-manager mesh
112                                 #:key
113                                 (parent %null-pointer)
114                                 (id -1)
115                                 (minimal-polys-per-node 512)
116                                 (also-add-if-mesh-pointer-zero #f))
117   (ffi-scene:add-octree-scene-node
118    scene-manager
119    mesh
120    parent
121    id
122    minimal-polys-per-node
123    (bool->integer also-add-if-mesh-pointer-zero)))
124
125 (define (get-mesh scene-manager filename)
126   (let ((mesh (ffi-scene:get-mesh scene-manager (string->pointer filename))))
127     (if (null-pointer? mesh) #f mesh)))
128
129 (define (get-root-scene-node scene-manager)
130   (ffi-scene:get-root-scene-node scene-manager))
131
132 (define (scene-draw-all scene-manager)
133   (ffi-scene:draw-all scene-manager))
134
135 (define (set-material-flag! node flag newvalue)
136   (let ((material-flag
137          (match flag
138                 ('wireframe ffi-video:EMF_WIREFRAME)
139                 ('pointcloud ffi-video:EMF_POINTCLOUD)
140                 ('gouraud-shading ffi-video:EMF_GOURAUD_SHADING)
141                 ('lighting ffi-video:EMF_LIGHTING)
142                 ('zbuffer ffi-video:EMF_ZBUFFER)
143                 ('zwrite-enable ffi-video:EMF_ZWRITE_ENABLE)
144                 ('back-face-culling ffi-video:EMF_BACK_FACE_CULLING)
145                 ('front-face-culling ffi-video:EMF_FRONT_FACE_CULLING)
146                 ('bilinear-filter ffi-video:EMF_BILINEAR_FILTER)
147                 ('trilinear-filter ffi-video:EMF_TRILINEAR_FILTER)
148                 ('anisotropic-filter ffi-video:EMF_ANISOTROPIC_FILTER)
149                 ('fog-enable ffi-video:EMF_FOG_ENABLE)
150                 ('normalize-normals ffi-video:EMF_NORMALIZE_NORMALS)
151                 ('texture-wrap ffi-video:EMF_TEXTURE_WRAP)
152                 ('anti-aliasing ffi-video:EMF_ANTI_ALIASING)
153                 ('color-mask ffi-video:EMF_COLOR_MASK)
154                 ('color-material ffi-video:EMF_COLOR_MATERIAL)
155                 ('use-mip-maps ffi-video:EMF_USE_MIP_MAPS)
156                 ('blend-operation ffi-video:EMF_BLEND_OPERATION)
157                 ('polygon-offset ffi-video:EMF_POLYGON_OFFSET))))
158     (ffi-scene:set-material-flag
159      node
160      material-flag
161      (bool->integer newvalue))))
162
163 (define (set-material-texture! node texture-layer texture)
164   (ffi-scene:set-material-texture node texture-layer texture))
165
166 (define (set-md2-animation! node anim)
167   (let ((animation-type
168          (match anim
169                 ('stand ffi-scene:EMAT_STAND)
170                 ('run ffi-scene:EMAT_RUN)
171                 ('attack ffi-scene:EMAT_ATTACK)
172                 ('pain-a ffi-scene:EMAT_PAIN_A)
173                 ('pain-b ffi-scene:EMAT_PAIN_B)
174                 ('pain-c ffi-scene:EMAT_PAIN_C)
175                 ('jump ffi-scene:EMAT_JUMP)
176                 ('flip ffi-scene:EMAT_FLIP)
177                 ('salute ffi-scene:EMAT_SALUTE)
178                 ('fallback ffi-scene:EMAT_FALLBACK)
179                 ('wave ffi-scene:EMAT_WAVE)
180                 ('point ffi-scene:EMAT_POINT)
181                 ('crouch-stand ffi-scene:EMAT_CROUCH_STAND)
182                 ('crouch-walk ffi-scene:EMAT_CROUCH_WALK)
183                 ('crouch-attack ffi-scene:EMAT_CROUCH_ATTACK)
184                 ('crouch-pain ffi-scene:EMAT_CROUCH_PAIN)
185                 ('crouch-death ffi-scene:EMAT_CROUCH_DEATH)
186                 ('death-fallback ffi-scene:EMAT_DEATH_FALLBACK)
187                 ('death-fallforward ffi-scene:EMAT_DEATH_FALLFORWARD)
188                 ('death-fallbackslow ffi-scene:EMAT_DEATH_FALLBACKSLOW)
189                 ('boom ffi-scene:EMAT_BOOM)
190                 ('count ffi-scene:EMAT_COUNT))))
191     (ffi-scene:set-md2-animation
192      node
193      animation-type)))
194
195 (define (set-position! node newpos)
196   (ffi-scene:set-position
197    node
198    (make-c-struct ffi-core:vector3df newpos)))