From be5cff47e5c7a233cda0fee9199310bf3bdde8ca Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 16 Aug 2021 17:11:02 -0400 Subject: graphics: Add skybox module. --- Makefile.am | 1 + chickadee/graphics/skybox.scm | 101 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 chickadee/graphics/skybox.scm diff --git a/Makefile.am b/Makefile.am index f7f4453..338a009 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,6 +80,7 @@ SOURCES = \ chickadee/graphics/font.scm \ chickadee/graphics/tile-map.scm \ chickadee/graphics/particles.scm \ + chickadee/graphics/skybox.scm \ chickadee/graphics/light.scm \ chickadee/graphics/mesh.scm \ chickadee/graphics/phong.scm \ diff --git a/chickadee/graphics/skybox.scm b/chickadee/graphics/skybox.scm new file mode 100644 index 0000000..aecba59 --- /dev/null +++ b/chickadee/graphics/skybox.scm @@ -0,0 +1,101 @@ +;;; Chickadee Game Toolkit +;;; Copyright © 2021 David Thompson +;;; +;;; 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 +;;; . + +;;; 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 + draw-skybox)) + +(define-record-type + (%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-buffer-view #:buffer index-buffer + #:type 'scalar + #:component-type 'unsigned-int)) + (positions (make-buffer-view #:buffer vertex-buffer + #:type 'vec3 + #:component-type 'float)) + (va (make-vertex-array #:indices indices + #:attributes `((0 . ,positions))))) + (%make-skybox cube-map va))) + +(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)))) -- cgit v1.2.3