~jl2/gl-fractals

64b317bfd8523d42065450e543ca4feace2768b5 — Jeremiah LaRocco 1 year, 10 months ago 3e3ad2b
Updates from newgl.
M bs-js.lisp => bs-js.lisp +13 -16
@@ 12,14 12,14 @@
   (animate :initarg :animate :initform nil))
  (:documentation "A Bs-Jsbrot set."))

(defmethod newgl:set-uniforms ((object bs-js))
  ;; (format t "set-uniforms julia-set~%")
(defmethod newgl:assign-uniforms ((object bs-js) &optional view-xform)
  (declare (ignorable view-xform))
  (with-slots (newgl:shader-program center) object
    (newgl:set-uniform newgl:shader-program "cReal" (realpart center))
    (newgl:set-uniform newgl:shader-program "cImag" (imagpart center))
    (call-next-method)))

(defmethod newgl:update ((object bs-js))
(defmethod newgl:update ((object bs-js) elapsed-time)
  (with-slots (center zoom-window animate) object
    (when animate
      (with-slots (radius) zoom-window


@@ 29,7 29,7 @@
                                              (* 0.0048 (imagpart radius)))))
          (setf center (complex (+ (realpart center) real-offset)
                                (+ (imagpart center) imag-offset)))))
      (newgl:set-uniforms object))))
      (newgl:assign-uniforms object))))

(defmethod newgl:handle-key ((object bs-js) window key scancode action mod-keys)
  (declare (ignorable window key scancode action mod-keys))


@@ 40,30 40,29 @@
        (cond
          ((and (eq key :down) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (realpart center) (- (imagpart center) imag-offset)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :up) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (realpart center) (+ (imagpart center) imag-offset)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :left) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (- (realpart center) real-offset) (imagpart center)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :a) (eq action :press))
           (setf animate (not animate)))

          ((and (eq key :right) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (+ (realpart center) real-offset) (imagpart center)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :j) (eq action :press))
           (with-slots (newgl:vertices) object
             (format t
                     "(newgl:display (gl-fractals:make-bs-js~%    :center ~a~%    :window (make-instance 'gl-fractals:complex-window~%        :center ~a~%        :radius ~a)))~%"
                     center
                     (slot-value zoom-window 'center)
                     radius)))
           (format t
                   "(newgl:display (gl-fractals:make-bs-js~%    :center ~a~%    :window (make-instance 'gl-fractals:complex-window~%        :center ~a~%        :radius ~a)))~%"
                   center
                   (slot-value zoom-window 'center)
                   radius))
          (t
           (call-next-method)))))))



@@ 73,11 72,9 @@
  (ctypecase window
    (complex-window (make-instance 'bs-js
                                   :center center
                                   :vertices (to-vertices window)
                                   :zoom-window window))
    (vector (make-instance 'bs-js
                           :center center
                           :vertices window
                           :zoom-window (window-from-vertices window)))))

(defun show-bs-js (&key

M burning-ship.lisp => burning-ship.lisp +10 -16
@@ 11,37 11,31 @@
   (center :initarg :center :initform #C(-1.0 0.0) :type complex))
  (:documentation "A Burning-Shipbrot set."))

(defmethod newgl:set-uniforms ((object burning-ship))
(defmethod newgl:assign-uniforms ((object burning-ship) &optional view-xform)
  (declare (ignorable view-xform))
  (with-slots (newgl:shader-program center) object
    (newgl:set-uniform newgl:shader-program "cReal" (realpart center))
    (newgl:set-uniform newgl:shader-program "cImag" (imagpart center))
    (call-next-method)))

(defmethod newgl:handle-key ((object burning-ship) window key scancode action mod-keys)
  (declare (ignorable window key scancode action mod-keys))
  (with-slots ( center zoom-window ) object
    (with-slots (radius) zoom-window
      (let ((real-offset (* 0.00125 (realpart radius)))
            (imag-offset (* 0.00125 (imagpart radius))))
        (cond
          ((and (eq key :j) (eq action :press))
           (with-slots (newgl:vertices) object
             (format t
                     "(newgl:display (gl-fractals:make-burning-ship~%    :center ~a~%    :window (make-instance 'gl-fractals:complex-window~%        :center ~a~%        :radius ~a)))~%"
                     center
                     (slot-value zoom-window 'center)
                     radius)))
          (t (call-next-method)))))))
      (cond
        ((and (eq key :j) (eq action :press))
         (format t
                 "(newgl:display (gl-fractals:make-burning-ship~%    :center ~a~%    :window (make-instance 'gl-fractals:complex-window~%        :center ~a~%        :radius ~a)))~%"
                 center
                 (slot-value zoom-window 'center)
                 radius))
        (t (call-next-method))))))

(defun make-burning-ship (&key (center #C(-0.7682546058236294 0.3247886940487651)) (window (make-instance 'complex-window)))
  (ctypecase window
    (complex-window (make-instance 'burning-ship
                                   :center center
                                   :vertices (to-vertices window)
                                   :zoom-window window))
    (vector (make-instance 'burning-ship
                           :center center
                           :vertices window
                           :zoom-window (window-from-vertices window)))))

(defun show-burning-ship (&key

M complex-fractal.lisp => complex-fractal.lisp +23 -32
@@ 4,42 4,40 @@

(in-package #:gl-fractals)

(defclass complex-fractal (newgl:vertex-object)
  ((newgl:vertices :initform nil)
   (newgl:indices :initform (make-array
                       6
                       :element-type 'fixnum
                       :initial-contents '(0 1 2 1 3 2)))
   (max-iterations :initform 320 :initarg :max-iterations :type fixnum)
   (aspect-ratio :initform 1.0 :initarg :aspect-ratio :type float)

(defclass complex-fractal (newgl:geometry)
  ((max-iterations :initform 320 :initarg :max-iterations :type fixnum)
   (aspect-ratio :initform 1.0 :type real)
   (zoom-window :initarg :zoom-window))
  (:documentation "Base class for fractals iterated on the complex plane."))

(defmethod newgl:set-uniforms ((object complex-fractal))
(defmethod newgl:vertex-buffers ((object complex-fractal))
  (with-slots (zoom-window) object
    (values (to-vertices zoom-window)
            (make-array
             6
             :element-type 'fixnum
             :initial-contents '(0 1 2 1 3 2)))))

(defmethod newgl:render ((object complex-fractal) view-xform)
  (call-next-method)
  (with-slots (newgl:shader-program max-iterations aspect-ratio) object
    (newgl:set-uniform newgl:shader-program
                       "maxIterations"
                       max-iterations)
    (newgl:set-uniform newgl:shader-program
                       "aspectRatio"
                       aspect-ratio)))
    (newgl:set-uniform newgl:shader-program "maxIterations" max-iterations)
    (newgl:set-uniform newgl:shader-program "aspectRatio" aspect-ratio)))

(defun zoom-complex-fractal-window (scale cpos fractal)
  (with-slots (newgl:vertices zoom-window) fractal
  (with-slots (zoom-window) fractal
    (with-slots (center radius) zoom-window
      (let* ((new-radius (* radius scale))
             (new-center (cursor-position-to-complex cpos zoom-window)))
        (setf center new-center
              radius new-radius)))
    (setf newgl:vertices (to-vertices zoom-window))))
              radius new-radius)))))

(defun pan-complex-fractal-window (offset-percent fractal)
  (with-slots (newgl:vertices zoom-window) fractal
  (with-slots (zoom-window) fractal
    (with-slots (radius center) zoom-window
      (incf center (complex (* (realpart radius) (realpart offset-percent))
                            (* (imagpart radius) (imagpart offset-percent)))))
    (setf newgl:vertices (to-vertices zoom-window))))
                            (* (imagpart radius) (imagpart offset-percent)))))))

(defmethod newgl:handle-key ((object complex-fractal) window key scancode action mod-keys)
  (declare (ignorable window key scancode action mod-keys))


@@ 52,14 50,8 @@
         (window-center (mapcar (rcurry #'/ 2.0) (glfw:get-window-size)))
         (need-reload
          (cond ((and (eq key :f5) (eq action :press))
                 (with-slots (newgl:vertices zoom-window) object
                   (setf zoom-window (make-instance 'complex-window))
                   (setf newgl:vertices (to-vertices zoom-window))))

                ((and (eq key :w) (eq action :press))
                 (with-slots (newgl:vertices) object
                   (format t "(newgl:display (gl-fractals:make-complex-fractal :window ~a))~%" newgl:vertices))
                 t)
                 (with-slots (zoom-window) object
                   (setf zoom-window (make-instance 'complex-window))))

                ((and (eq key :page-down)  (or (eq action :press) (eq action :repeat)))
                 (zoom-complex-fractal-window zoom-in-percent window-center object))


@@ 108,16 100,15 @@
    (setf aspect-ratio (if (< height width )
                           (/ width height 1.0)
                           (/ height width -1.0)))
    (newgl:set-uniforms object)))
    (newgl:assign-uniforms object)))

(defmethod newgl:handle-drag ((object complex-fractal) window (click complex-fractal-click) cursor-pos)
  (declare (ignorable window))
  (with-slots (newgl:vertices zoom-window) object
  (with-slots (zoom-window) object
    (with-slots (center radius) zoom-window
      (incf center (- (cursor-position-to-complex (slot-value newgl:*previous-mouse-drag* 'newgl:cursor-pos)
                                                  zoom-window)
                      (cursor-position-to-complex cursor-pos zoom-window))))
    (setf newgl:vertices (to-vertices zoom-window))
    (newgl:reload-object object)
    (with-slots (newgl:cursor-pos newgl:mod-keys newgl:action newgl:button newgl:time) click
      (setf newgl:*previous-mouse-drag* (make-instance 'complex-fractal-click

M julia-set.lisp => julia-set.lisp +13 -16
@@ 12,14 12,14 @@
   (animate :initarg :animate :initform nil))
  (:documentation "A Mandelbrot set."))

(defmethod newgl:set-uniforms ((object julia-set))
  ;; (format t "set-uniforms julia-set~%")
(defmethod newgl:assign-uniforms ((object julia-set) &optional view-xform)
  (declare (ignorable view-xform))
  (with-slots (newgl:shader-program center) object
    (newgl:set-uniform newgl:shader-program "cReal" (realpart center))
    (newgl:set-uniform newgl:shader-program "cImag" (imagpart center))
    (call-next-method)))

(defmethod newgl:update ((object julia-set))
(defmethod newgl:update ((object julia-set) elapsed-time)
  (with-slots (center zoom-window animate) object
    (when animate
      (with-slots (radius) zoom-window


@@ 29,7 29,7 @@
                                              (* 0.0048 (imagpart radius)))))
          (setf center (complex (+ (realpart center) real-offset)
                                (+ (imagpart center) imag-offset)))))
      (newgl:set-uniforms object))))
      (newgl:assign-uniforms object))))





@@ 43,30 43,29 @@
        (cond
          ((and (eq key :down) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (realpart center) (- (imagpart center) imag-offset)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :up) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (realpart center) (+ (imagpart center) imag-offset)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :left) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (- (realpart center) real-offset) (imagpart center)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :right) (find :shift mod-keys) (or (eq action :press) (eq action :repeat)))
           (setf center (complex (+ (realpart center) real-offset) (imagpart center)))
           (newgl:set-uniforms object))
           (newgl:assign-uniforms object))

          ((and (eq key :a) (eq action :press))
           (setf animate (not animate)))

          ((and (eq key :j) (eq action :press))
           (with-slots (newgl:vertices) object
             (format t
                     "(newgl:display (gl-fractals:make-julia-set~%    :center ~a~%    :window (make-instance 'gl-fractals:complex-window~%        :center ~a~%        :radius ~a)))~%"
                     center
                     (slot-value zoom-window 'center)
                     radius)))
           (format t
                   "(newgl:display (gl-fractals:make-julia-set~%    :center ~a~%    :window (make-instance 'gl-fractals:complex-window~%        :center ~a~%        :radius ~a)))~%"
                   center
                   (slot-value zoom-window 'center)
                   radius))
          (t
           (call-next-method)))))))



@@ 74,11 73,9 @@
  (ctypecase window
    (complex-window (make-instance 'julia-set
                                   :center center
                                   :vertices (to-vertices window)
                                   :zoom-window window))
    (vector (make-instance 'julia-set
                           :center center
                           :vertices window
                           :zoom-window (window-from-vertices window)))))

(defun show-julia-set (&key

M mandelbrot.lisp => mandelbrot.lisp +1 -3
@@ 15,12 15,10 @@
                          (max-iterations 3200))
  (ctypecase window
    (complex-window (make-instance 'mandelbrot
                                   :vertices (to-vertices window)
                                   :zoom-window window
                                   :max-iterations max-iterations))
    (vector (make-instance 'mandelbrot
                           :vertices window
                           :zoom-window (window-from-vertices window)))))

(defun show-mandelbrot (&key (mandel (make-mandelbrot)))
  (newgl:display mandel))
  (newgl:display mandel :debug t))

M shaders/julia-set-fragment.glsl => shaders/julia-set-fragment.glsl +1 -3
@@ 12,7 12,7 @@ out vec4 Color;
void main (void)
{
     int iter;
     float tempzx, tempzy, Creal, Cimag;
     float tempzx, tempzy;
     float r2 = 0.0;
     vec2 pos = complexCoordinate;
     float zx;


@@ 26,8 26,6 @@ void main (void)
          zy = -1 * pos.y * aspectRatio;
     }

     Creal = zx;
     Cimag = zy;
     for (iter = 0; iter < maxIterations; iter++)
     {
// z = z^2 + c