From 662d04ffa53f9c400d959ceeb9af9a7bb9ad63cf Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 12 May 2021 20:43:45 -0400 Subject: Add (chickadee graphics light) module. --- Makefile.am | 1 + chickadee/graphics/light.scm | 103 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 chickadee/graphics/light.scm diff --git a/Makefile.am b/Makefile.am index be0f917..4b5d802 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/light.scm \ chickadee/graphics/phong.scm \ chickadee/graphics/pbr.scm \ chickadee/graphics/model.scm \ diff --git a/chickadee/graphics/light.scm b/chickadee/graphics/light.scm new file mode 100644 index 0000000..d217f56 --- /dev/null +++ b/chickadee/graphics/light.scm @@ -0,0 +1,103 @@ +;;; 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 lights. +;; +;;; Code: + +(define-module (chickadee graphics light) + #:use-module (chickadee graphics color) + #:use-module (chickadee graphics shader) + #:use-module (chickadee math) + #:use-module (chickadee math vector) + #:export (%max-lights + make-point-light + make-directional-light + make-spot-light + %disabled-light + light? + point-light? + directional-light? + spot-light? + light-enabled? + light-type + light-position + light-direction + light-color + light-cut-off + set-light-position! + set-light-direction! + set-light-color! + set-light-cut-off!)) + +;; Maximum number of lights supported by our shaders. +(define %max-lights 4) + +(define-shader-type + make-light + light? + (bool enabled light-enabled?) + (int type %light-type) + (float-vec3 position light-position set-light-position!) + (float-vec3 direction light-direction set-light-direction!) + (float-vec4 color light-color set-light-color!) + (float cut-off light-cut-off set-light-cut-off!)) + +(define %disabled-light (make-light #:enabled #f)) + +(define (light-type light) + (let ((t (light-type light))) + (cond + ((= t 0) 'point) + ((= t 1) 'directional) + ((= t 2) 'spot)))) + +(define (point-light? light) + (eq? (light-type light) 'point)) + +(define (directional-light? light) + (eq? (light-type light) 'directional)) + +(define (spot-light? light) + (eq? (light-type light) 'spot)) + +(define* (make-point-light #:key (position (vec3 0.0 0.0 0.0)) + (color black)) + (make-light #:enabled #t + #:type 0 + #:position position + #:color color)) + +(define* (make-directional-light #:key (direction (vec3 0.0 -1.0 0.0)) + (color black)) + (make-light #:enabled #t + #:type 1 + #:direction direction + #:color color)) + +(define* (make-spot-light #:key (position (vec3 0.0 0.0 0.0)) + (direction (vec3 0.0 0.0 -1.0)) + (color black) + (cut-off (/ pi 4.0))) + (make-light #:enabled #t + #:type 2 + #:position position + #:direction direction + #:color color + #:cut-off (cos cut-off))) -- cgit v1.2.3