-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
skybox.lisp
56 lines (44 loc) · 1.69 KB
/
skybox.lisp
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
(in-package #:org.shirakumo.fraf.trial)
(define-shader-entity skybox (fullscreen-entity)
((texture :initarg :texture :accessor texture)
(color :initarg :color :initform (vec3 1) :accessor color)
(orientation :initarg :orientation :initform (quat) :accessor orientation)))
(define-transfer skybox texture color)
(defmethod stage :after ((skybox skybox) (area staging-area))
(stage (texture skybox) area))
(defmethod bind-textures ((skybox skybox))
(gl:active-texture :texture0)
(activate (texture skybox)))
(defmethod render ((skybox skybox) (shader shader-program))
(let ((mat (mat4)))
(declare (dynamic-extent mat))
(qmat (orientation skybox) mat)
(nm* mat (view-matrix))
(setf (uniform shader "view_matrix") mat))
(setf (uniform shader "projection_matrix") (projection-matrix))
(setf (uniform shader "texture_image") 0)
(setf (uniform shader "multiplier") (color skybox))
(with-depth-mask NIL
(render-array (// 'trial 'fullscreen-square))))
(define-class-shader (skybox :vertex-shader)
"uniform mat4 projection_matrix;
uniform mat4 view_matrix;
smooth out vec3 eye;
void main@after() {
mat4 inverseProjection = inverse(projection_matrix);
mat3 inverseModelview = transpose(mat3(view_matrix));
vec3 unprojected = (inverseProjection * vec4(position,1)).xyz;
eye = inverseModelview * unprojected;
eye.y *= -1;
gl_Position.z = +1;
}")
(define-class-shader (skybox :fragment-shader)
"uniform samplerCube texture_image;
uniform vec3 multiplier;
smooth in vec3 eye;
out vec4 color;
void main() {
color = vec4(texture(texture_image, eye).rgb * multiplier, 1);
}")
(define-shader-pass skybox-pass (single-shader-pass skybox)
((color :port-type output)))