Back

to-radians (clj)

(source)

function

(to-radians deg)
Converts an angle in degrees to an approximate equivalent angle in radians. See: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#toRadians-double-

Examples

clojure
(ns clojure.test-clojure.math
  (:require
    [clojure.test :refer :all]
    [clojure.math :as m]))

(deftest test-radians-degrees-roundtrip
  (doseq [d (range 0.0 360.0 5.0)]
    (is (ulp= (m/round d) (m/round (-> d m/to-radians m/to-degrees)) 1))))
wedesoft/sfsim25
(ns sfsim25.t-model
    (:require [midje.sweet :refer :all]
              [malli.instrument :as mi]
              [malli.dev.pretty :as pretty]
              [clojure.math :refer (to-radians sqrt PI)]
              [sfsim25.conftest :refer (roughly-matrix roughly-vector roughly-quaternion record-image is-image)]
              [fastmath.matrix :refer (eye mulm mat4x4)]
              [fastmath.vector :refer (vec3 normalize)]
              [sfsim25.matrix :refer :all]
              [sfsim25.render :refer :all]
              [sfsim25.model :refer :all :as model]
              [sfsim25.quaternion :refer (->Quaternion)])
    (:import [org.lwjgl.glfw GLFW]))

(fact "Render red cube"
      (offscreen-render 160 120
        (let [program      (make-program :vertex [vertex-cube] :fragment [fragment-cube])
              opengl-scene (load-scene-into-opengl (constantly program) cube)
              transform    (transformation-matrix (mulm (rotation-x 0.5) (rotation-y -0.4)) (vec3 0 0 -5))
              moved-scene  (assoc-in opengl-scene [:root :transform] transform)]
          (clear (vec3 0 0 0) 0.0)
          (use-program program)
          (uniform-matrix4 program "projection" (projection-matrix 160 120 0.1 10.0 (to-radians 60)))
          (uniform-vector3 program "light" (normalize (vec3 1 2 3)))
          (render-scene (constantly program) moved-scene
                        (fn [{:keys [transform diffuse]}]
                            (uniform-matrix4 program "transform" transform)
                            (uniform-vector3 program "diffuse_color" diffuse)))
          (unload-scene-from-opengl opengl-scene)
          (destroy-program program))) => (is-image "test/sfsim25/fixtures/model/cube.png" 0.0))

(fact "Render red and green cube"
      (offscreen-render 160 120
        (let [program      (make-program :vertex [vertex-cube] :fragment [fragment-cube])
              opengl-scene (load-scene-into-opengl (constantly program) cubes)
              transform    (transformation-matrix (mulm (rotation-x 0.5) (rotation-y -0.4)) (vec3 0 0 -7))
              moved-scene  (assoc-in opengl-scene [:root :transform] transform)]
          (clear (vec3 0 0 0) 0.0)
          (use-program program)
          (uniform-matrix4 program "projection" (projection-matrix 160 120 0.1 10.0 (to-radians 60)))
          (uniform-vector3 program "light" (normalize (vec3 1 2 3)))
          (render-scene (constantly program) moved-scene
                        (fn [{:keys [transform diffuse]}]
                            (uniform-matrix4 program "transform" transform)
                            (uniform-vector3 program "diffuse_color" diffuse)))
          (unload-scene-from-opengl opengl-scene)
          (destroy-program program))) => (is-image "test/sfsim25/fixtures/model/cubes.png" 0.01))

(fact "Render textured cube"
      (offscreen-render 160 120
        (let [program      (make-program :vertex [vertex-dice] :fragment [fragment-dice])
              opengl-scene (load-scene-into-opengl (constantly program) dice)
              transform    (transformation-matrix (mulm (rotation-x 0.5) (rotation-y -0.4)) (vec3 0 0 -5))
              moved-scene  (assoc-in opengl-scene [:root :transform] transform)]
          (clear (vec3 0 0 0) 0.0)
          (use-program program)
          (uniform-matrix4 program "projection" (projection-matrix 160 120 0.1 10.0 (to-radians 60)))
          (uniform-vector3 program "light" (normalize (vec3 1 2 3)))
          (uniform-sampler program "colors" 0)
          (render-scene (constantly program) moved-scene
                        (fn [{:keys [transform colors]}]
                            (uniform-matrix4 program "transform" transform)
                            (use-textures {0 colors})))
          (unload-scene-from-opengl opengl-scene)
          (destroy-program program))) => (is-image "test/sfsim25/fixtures/model/dice.png" 0.01))

(fact "Render brick wall"
      (offscreen-render 160 120
        (let [program      (make-program :vertex [vertex-bricks] :fragment [fragment-bricks])
              opengl-scene (load-scene-into-opengl (constantly program) bricks)
              transform    (transformation-matrix (rotation-x 1.8) (vec3 0 0 -3))
              moved-scene  (assoc-in opengl-scene [:root :transform] transform)]
          (clear (vec3 0 0 0) 0.0)
          (use-program program)
          (uniform-matrix4 program "projection" (projection-matrix 160 120 0.1 10.0 (to-radians 60)))
          (uniform-vector3 program "light" (normalize (vec3 0 -3 1)))
          (uniform-sampler program "colors" 0)
          (uniform-sampler program "normals" 1)
          (render-scene (constantly program) moved-scene
                        (fn [{:keys [transform colors normals]}]
                            (uniform-matrix4 program "transform" transform)
                            (use-textures {0 colors 1 normals})))
          (unload-scene-from-opengl opengl-scene)
          (destroy-program program))) => (is-image "test/sfsim25/fixtures/model/bricks.png" 0.01))

(fact "Render uniformly colored cube and textured cube"
      (offscreen-render 160 120
        (let [program-cube      (make-program :vertex [vertex-cube] :fragment [fragment-cube])
              program-dice      (make-program :vertex [vertex-dice] :fragment [fragment-dice])
              program-selection (fn [material] (if (:color-texture-index material) program-dice program-cube))
              opengl-scene      (load-scene-into-opengl program-selection cube-and-dice)
              transform         (transformation-matrix (mulm (rotation-x 0.5) (rotation-y -0.4)) (vec3 0 0 -7))
              moved-scene       (assoc-in opengl-scene [:root :transform] transform)]
          (clear (vec3 0 0 0) 0.0)
          (doseq [program [program-cube program-dice]]
                 (use-program program)
                 (uniform-matrix4 program "projection" (projection-matrix 160 120 0.1 10.0 (to-radians 60)))
                 (uniform-vector3 program "light" (normalize (vec3 1 2 3))))
          (uniform-sampler program-dice "colors" 0)
          (render-scene program-selection moved-scene render-model)
          (unload-scene-from-opengl opengl-scene)
          (destroy-program program-dice)
          (destroy-program program-cube))) => (is-image "test/sfsim25/fixtures/model/cube-and-dice.png" 0.01))
wedesoft/sfsim25
(ns sfsim25.t-render
  (:require [midje.sweet :refer :all]
            [malli.instrument :as mi]
            [malli.dev.pretty :as pretty]
            [sfsim25.conftest :refer (is-image record-image roughly-vector)]
            [fastmath.vector :refer (vec3 vec4 normalize)]
            [fastmath.matrix :refer (eye diagonal) :as m]
            [clojure.math :refer (to-radians)]
            [comb.template :as template]
            [sfsim25.util :refer :all]
            [sfsim25.matrix :refer :all :as matrix]
            [sfsim25.shaders :as s]
            [sfsim25.quaternion :as q]
            [sfsim25.render :refer :all :as render])
  (:import [org.lwjgl.opengl GL11 GL12 GL30 GL42]
           [org.lwjgl.glfw GLFW]
           [org.lwjgl BufferUtils]))

(fact "Shadow mapping integration test"
      (with-invisible-window
        (let [projection     (projection-matrix 320 240 2.0 5.0 (to-radians 90))
              transform      (eye 4)
              light-vector   (normalize (vec3 1 1 2))
              shadow-mat     (shadow-matrices projection transform light-vector 1.0)
              indices        [0 1 3 2 6 7 5 4 8 9 11 10]
              vertices       [-2.0 -2.0 -4.0, 2.0 -2.0 -4.0, -2.0 2.0 -4.0, 2.0 2.0 -4.0,
                              -1.0 -1.0 -3.0, 1.0 -1.0 -3.0, -1.0 1.0 -3.0, 1.0 1.0 -3.0
                              -1.0 -1.0 -2.9, 1.0 -1.0 -2.9, -1.0 1.0 -2.9, 1.0 1.0 -2.9]
              program-shadow (make-program :vertex [vertex-shadow s/shrink-shadow-index]
                                           :fragment [fragment-shadow])
              program-main   (make-program :vertex [vertex-scene]
                                           :fragment [fragment-scene s/shadow-lookup])
              vao            (make-vertex-array-object program-main indices vertices ["point" 3])
              shadow-map     (texture-render-depth
                               128 128
                               (clear)
                               (use-program program-shadow)
                               (uniform-matrix4 program-shadow "transform" (:shadow-ndc-matrix shadow-mat))
                               (render-quads vao))]
          (let [depth (make-empty-depth-texture-2d :linear :clamp 320 240)
                tex   (make-empty-texture-2d :linear :clamp GL11/GL_RGBA8 320 240)]
            (framebuffer-render 320 240 :cullback depth [tex]
                                (clear (vec3 0 0 0))
                                (use-program program-main)
                                (uniform-sampler program-main "shadow_map" 0)
                                (uniform-int program-main "shadow_size" 128)
                                (uniform-matrix4 program-main "projection" projection)
                                (uniform-matrix4 program-main "shadow_map_matrix" (:shadow-map-matrix shadow-mat))
                                (use-textures {0 shadow-map})
                                (render-quads vao))
            (let [img (texture->image tex)]
              (destroy-texture shadow-map)
              (destroy-vertex-array-object vao)
              (destroy-program program-main)
              (destroy-program program-shadow)
              img))))
      => (is-image "test/sfsim25/fixtures/render/shadow.png" 0.04))

(fact "Shadow cascade integration test"
      (with-invisible-window
        (let [projection     (projection-matrix 320 240 2.0 5.0 (to-radians 90))
              transform      (eye 4)
              num-steps      1
              light-vector   (normalize (vec3 1 1 2))
              shadow-data    #:sfsim25.opacity{:num-steps num-steps :mix 0.5 :depth 1.0}
              render-vars    #:sfsim25.render{:projection projection :extrinsics transform :light-direction light-vector
                                              :z-near 2.0 :z-far 5.0}
              shadow-mats    (shadow-matrix-cascade shadow-data render-vars)
              indices        [0 1 3 2 6 7 5 4 8 9 11 10]
              vertices       [-2.0 -2.0 -4.0, 2.0 -2.0 -4.0, -2.0 2.0 -4.0, 2.0 2.0 -4.0,
                              -1.0 -1.0 -3.0, 1.0 -1.0 -3.0, -1.0 1.0 -3.0, 1.0 1.0 -3.0
                              -1.0 -1.0 -2.9, 1.0 -1.0 -2.9, -1.0 1.0 -2.9, 1.0 1.0 -2.9]
              program-shadow (make-program :vertex [vertex-shadow s/shrink-shadow-index]
                                           :fragment [fragment-shadow])
              program-main   (make-program :vertex [vertex-scene]
                                           :fragment [fragment-scene-cascade (s/shadow-cascade-lookup num-steps "shadow_lookup")
                                                      s/shadow-lookup])
              vao            (make-vertex-array-object program-main indices vertices ["point" 3])
              shadow-maps    (shadow-cascade 128 shadow-mats program-shadow
                                             (fn [shadow-ndc-matrix]
                                                 (uniform-matrix4 program-shadow "transform" shadow-ndc-matrix)
                                                 (render-quads vao)))]
          (let [depth (make-empty-depth-texture-2d :linear :clamp 320 240)
                tex   (make-empty-texture-2d :linear :clamp GL11/GL_RGBA8 320 240)]
            (framebuffer-render 320 240 :cullback depth [tex]
                                (clear (vec3 0 0 0))
                                (use-program program-main)
                                (uniform-sampler program-main "shadow_map0" 0)
                                (uniform-float program-main "split0" 0.0)
                                (uniform-float program-main "split1" 50.0)
                                (uniform-int program-main "shadow_size" 128)
                                (uniform-matrix4 program-main "projection" projection)
                                (uniform-matrix4 program-main "transform" (eye 4))
                                (uniform-matrix4 program-main "shadow_map_matrix0" (:shadow-map-matrix (shadow-mats 0)))
                                (use-textures (zipmap (range) shadow-maps))
                                (render-quads vao))
            (let [img (texture->image tex)]
              (doseq [shadow-map shadow-maps]
                     (destroy-texture shadow-map))
              (destroy-vertex-array-object vao)
              (destroy-program program-main)
              (destroy-program program-shadow)
              img))))
      => (is-image "test/sfsim25/fixtures/render/shadow.png" 0.04))