summaryrefslogtreecommitdiff
path: root/chickadee/graphics/skybox.scm
blob: 093f6298028256411a5b67948fe23fd811a82963 (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
;;; Chickadee Game Toolkit
;;; Copyright © 2021 David Thompson <dthompson2@worcester.edu>
;;;
;;; Chickadee 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.
;;;
;;; Chickadee 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:
;;
;; 3D Skybox
;;
;;; Code:

(define-module (chickadee graphics skybox)
  #:use-module (chickadee config)
  #:use-module (chickadee graphics buffer)
  #:use-module (chickadee graphics blend)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics depth)
  #:use-module (chickadee graphics engine)
  #:use-module (chickadee graphics polygon)
  #:use-module (chickadee graphics shader)
  #:use-module (chickadee graphics texture)
  #:use-module (srfi srfi-9)
  #:export (make-skybox
            skybox?
            skybox-cube-map
            skybox-mesh
            default-skybox
            draw-skybox))

(define-record-type <skybox>
  (%make-skybox cube-map vertex-array)
  skybox?
  (cube-map skybox-cube-map)
  (vertex-array skybox-vertex-array))

(define (make-skybox cube-map)
  (let* ((index (u32vector 0 3 2 0 2 1
                           4 7 6 4 6 5
                           8 11 10 8 10 9
                           12 15 14 12 14 13
                           16 19 18 16 18 17
                           20 23 22 20 22 21))
         (verts (f32vector -1.0 -1.0 -1.0 ; bottom
                           1.0 -1.0 -1.0
                           1.0 -1.0 1.0
                           -1.0 -1.0 1.0
                           -1.0 1.0 -1.0 ; top
                           1.0 1.0 -1.0
                           1.0 1.0 1.0
                           -1.0 1.0 1.0
                           -1.0 -1.0 -1.0 ; left
                           -1.0 1.0 -1.0
                           -1.0 1.0 1.0
                           -1.0 -1.0 1.0
                           1.0 -1.0 -1.0 ; right
                           1.0 1.0 -1.0
                           1.0 1.0 1.0
                           1.0 -1.0 1.0
                           -1.0 -1.0 1.0 ; front
                           1.0 -1.0 1.0
                           1.0 1.0 1.0
                           -1.0 1.0 1.0
                           -1.0 -1.0 -1.0 ; back
                           1.0 -1.0 -1.0
                           1.0 1.0 -1.0
                           -1.0 1.0 -1.0))
         (index-buffer (make-buffer index #:target 'index))
         (vertex-buffer (make-buffer verts))
         (indices (make-vertex-attribute #:buffer index-buffer
                                         #:type 'scalar
                                         #:component-type 'unsigned-int))
         (positions (make-vertex-attribute #:buffer vertex-buffer
                                           #:type 'vec3
                                           #:component-type 'float))
         (va (make-vertex-array #:indices indices
                                #:attributes `((0 . ,positions)))))
    (%make-skybox cube-map va)))

;; A default, dark gray skybox.
(define %default-skybox
  (delay
    (let ((face (list (u32vector #xff202020
                                 #xff202020
                                 #xff202020
                                 #xff202020)
                      2 2)))
      (make-skybox (make-cube-map (make-list 6 face))))))

(define (default-skybox)
  (force %default-skybox))

(define %skybox-shader
  (delay (load-shader (scope-datadir "shaders/skybox-vert.glsl")
                      (scope-datadir "shaders/skybox-frag.glsl"))))

(define (skybox-shader)
  (force %skybox-shader))

(define (draw-skybox skybox view)
  (with-graphics-state ((g:texture-0 (skybox-cube-map skybox)))
    (shader-apply (skybox-shader) (skybox-vertex-array skybox)
                  #:view view
                  #:projection (current-projection))))