summaryrefslogtreecommitdiff
path: root/sly/render/shape.scm
blob: de4fd9534df4fe67641ab728c3d0e86dc66eeeba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;; Sly
;;; Copyright (C) 2014 David Thompson <davet@gnu.org>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Primitive 2D/3D shapes.
;;
;;; Code:

(define-module (sly render shape)
  #:use-module (sly math)
  #:use-module (sly render)
  #:use-module (sly render mesh)
  #:use-module (sly render texture)
  #:use-module (sly math vector)
  #:export (make-cube))

;; TODO: Write some combinators for composing triangles into more
;; complex shapes with a procedure to convert them into a mesh.

(define* (make-cube size #:optional #:key (texture #f))
  (let* ((half-size (half size))
         (mesh (build-mesh #(
                             ;; Front
                             0 3 2 0 2 1
                               ;; Back
                               4 6 7 4 5 6
                               ;; Top
                               8 11 10 8 10 9
                               ;; Bottom
                               12 14 15 12 13 14
                               ;; Left
                               16 19 18 16 18 17
                               ;; Right
                               20 22 23 20 21 22)
                           (vector
                            ;; Front
                            (vector3 (- half-size) (- half-size) (- half-size))
                            (vector3 half-size (- half-size) (- half-size))
                            (vector3 half-size half-size (- half-size))
                            (vector3 (- half-size) half-size (- half-size))
                            ;; Back
                            (vector3 (- half-size) (- half-size) half-size)
                            (vector3 half-size (- half-size) half-size)
                            (vector3 half-size half-size half-size)
                            (vector3 (- half-size) half-size half-size)
                            ;; Top
                            (vector3 (- half-size) half-size (- half-size))
                            (vector3 half-size half-size (- half-size))
                            (vector3 half-size half-size half-size)
                            (vector3 (- half-size) half-size half-size)
                            ;; Bottom
                            (vector3 (- half-size) (- half-size) (- half-size))
                            (vector3 half-size (- half-size) (- half-size))
                            (vector3 half-size (- half-size) half-size)
                            (vector3 (- half-size) (- half-size) half-size)
                            ;; Left
                            (vector3 (- half-size) (- half-size) (- half-size))
                            (vector3 (- half-size) half-size (- half-size))
                            (vector3 (- half-size) half-size half-size)
                            (vector3 (- half-size) (- half-size) half-size)
                            ;; Right
                            (vector3 half-size (- half-size) (- half-size))
                            (vector3 half-size half-size (- half-size))
                            (vector3 half-size half-size half-size)
                            (vector3 half-size (- half-size) half-size))
                           (let ((s1 (texture-s1 texture))
                                 (t1 (texture-t1 texture))
                                 (s2 (texture-s2 texture))
                                 (t2 (texture-t2 texture)))
                             (vector
                              ;; Front
                              (vector2 s1 t1)
                              (vector2 s2 t1)
                              (vector2 s2 t2)
                              (vector2 s1 t2)
                              ;; Back
                              (vector2 s1 t1)
                              (vector2 s2 t1)
                              (vector2 s2 t2)
                              (vector2 s1 t2)
                              ;; Top
                              (vector2 s1 t1)
                              (vector2 s2 t1)
                              (vector2 s2 t2)
                              (vector2 s1 t2)
                              ;; Bottom
                              (vector2 s1 t1)
                              (vector2 s2 t1)
                              (vector2 s2 t2)
                              (vector2 s1 t2)
                              ;; Left
                              (vector2 s1 t1)
                              (vector2 s2 t1)
                              (vector2 s2 t2)
                              (vector2 s1 t2)
                              ;; Right
                              (vector2 s1 t1)
                              (vector2 s2 t1)
                              (vector2 s2 t2)
                              (vector2 s1 t2))))))
    (with-texture texture
      (render-mesh mesh))))