]> git.jsancho.org Git - guile-irrlicht.git/blob - irrlicht/video.scm
Transformation
[guile-irrlicht.git] / irrlicht / video.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 video)
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 video) #:prefix ffi-video:)
26   #:use-module (irrlicht util)
27   #:use-module (irrlicht util foreign)
28   #:export (begin-scene
29             end-scene
30             get-fps
31             get-texture
32             get-video-driver-name
33             set-material!
34             set-transform!
35             make-s3dvertex
36             vertex-position
37             make-material))
38
39 (define* (begin-scene driver
40                       #:key
41                       (back-buffer #t)
42                       (z-buffer #t)
43                       (color '(255 0 0 0))
44                       (video-data %null-pointer)
45                       (source-rect '()))
46   (ffi-video:begin-scene driver
47                          (bool->integer back-buffer)
48                          (bool->integer z-buffer)
49                          (ffi-video:scolor->pointer color)
50                          video-data
51                          (if (null? source-rect)
52                              %null-pointer
53                              (ffi-core:rect->pointer source-rect))))
54
55 (define (end-scene driver)
56   (ffi-video:end-scene driver))
57
58 (define (get-fps driver)
59   (ffi-video:get-fps driver))
60
61 (define (get-texture driver filename)
62   (ffi-video:get-texture driver (string->pointer filename)))
63
64 (define (get-video-driver-name driver)
65   (pointer->string
66    (ffi-video:get-video-driver-name driver)))
67
68 (define (set-material! driver material)
69   (ffi-video:set-material
70    driver
71    (ffi-video:smaterial->pointer material)))
72
73 (define (set-transform! driver state mat)
74   (let ((transform-state
75          (match state
76                 ('view ffi-video:ETS_VIEW)
77                 ('world ffi-video:ETS_WORLD)
78                 ('projection ffi-video:ETS_PROJECTION)
79                 ('texture0 ffi-video:ETS_TEXTURE_0)
80                 ('texture1 ffi-video:ETS_TEXTURE_1)
81                 ('texture2 ffi-video:ETS_TEXTURE_2)
82                 ('texture3 ffi-video:ETS_TEXTURE_3)
83                 ('texture4 ffi-video:ETS_TEXTURE_4)
84                 ('texture5 ffi-video:ETS_TEXTURE_5)
85                 ('texture6 ffi-video:ETS_TEXTURE_6)
86                 ('texture7 ffi-video:ETS_TEXTURE_7)
87                 ('count ffi-video:ETS_COUNT))))
88     (ffi-video:set-transform
89      driver
90      transform-state
91      mat)))
92
93 ;; s3d vertices
94 (define (make-s3dvertex position normal color t-coords)
95   (ffi-video:pointer->s3dvertex
96    (make-c-struct ffi-video:s3dvertex
97                   (list position normal color t-coords))))
98
99 (define (vertex-position vertex)
100   (let ((data (parse-c-struct (ffi-video:s3dvertex->pointer vertex)
101                               ffi-video:s3dvertex)))
102     (car data)))
103
104 ;; smaterial
105 (define* (make-material #:key (wireframe #f) (lighting #t))
106   (let ((material
107          (list
108           ;; textureLayer[4]
109           (list %null-pointer 0 0 0 0 0 0 %null-pointer %null-pointer)
110           (list %null-pointer 0 0 0 0 0 0 %null-pointer %null-pointer)
111           (list %null-pointer 0 0 0 0 0 0 %null-pointer %null-pointer)
112           (list %null-pointer 0 0 0 0 0 0 %null-pointer %null-pointer)
113           ffi-video:EMT_SOLID         ; materialType
114           (list 255 255 255 255)      ; ambientColor
115           (list 255 255 255 255)      ; diffuseColor
116           (list 0 0 0 0)              ; emissiveColor
117           (list 255 255 255 255)      ; specularColor
118           0                           ; shininess
119           0                           ; materialTypeParam
120           0                           ; materialTypeParam2
121           1                           ; thickness
122           ffi-video:ECFN_LESSEQUAL    ; zBuffer
123           ffi-video:EAAM_SIMPLE       ; antiAliasing
124           (list
125            ffi-video:ECP_ALL          ; colorMask
126            ffi-video:ECM_DIFFUSE      ; colorMaterial
127            ffi-video:EBO_NONE         ; blendOperation
128            0                          ; polygonOffsetFactor
129            ffi-video:EPO_FRONT        ; polygonOffsetDirection
130            (bool->integer wireframe)  ; wireframe
131            (bool->integer #f)         ; pointCloud
132            (bool->integer #t)         ; gouraudShading
133            (bool->integer lighting)   ; lighting
134            (bool->integer #t)         ; zWriteEnable
135            (bool->integer #t)         ; backfaceCulling
136            (bool->integer #f)         ; frontfaceCulling
137            (bool->integer #f)         ; fogEnable
138            (bool->integer #f)         ; normalizeNormals
139            (bool->integer #t)         ; useMipMaps
140            ))))
141     (ffi-video:pointer->smaterial
142      (make-c-struct+ ffi-video:smaterial material))))