Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Raise window

* Render wireframe under transform
* Using backside culling stub
  • Loading branch information...
commit 3ec7778a58e75f2c80fdedf6c48ed7d34c74283a 1 parent 6f4bf7d
@timmc authored
View
58 src/timmcHW4/flat.clj
@@ -1,8 +1,10 @@
(ns timmcHW4.flat
"2D triangle renderer program with barycentric interpolation."
- (:import [java.awt Dimension]
+ (:require [timmcHW4.geom :as g]
+ [timmcHW4.tri :as t])
+ (:import [java.awt Color Dimension Graphics2D]
[java.awt.geom AffineTransform Point2D Point2D$Double]
- [javax.swing SwingUtilities UIManager]))
+ [javax.swing JFrame JComponent SwingUtilities UIManager]))
;;;; Geometry
@@ -25,29 +27,67 @@
(def view-w 512)
(def view-h 512)
-(def xform-to-view
+(def ^AffineTransform xform-to-view
(doto (AffineTransform.)
(.translate (/ view-w 2) (/ view-h 2))
(.scale 1 -1)))
-(def xform-to-world
+(def ^AffineTransform xform-to-world
(.createInverse xform-to-view))
(defn to-world
"Take an [x y] from view to world coords."
- [[x y]]
- (de-pt (.transform xform-to-world (pt x y))))
+ [[x y & _]]
+ (de-pt (.transform xform-to-world (pt x y) nil)))
(defn to-view
"Take an [x y] from world to view coords."
- [[x y]]
- (de-pt (.transform xform-to-view (pt x y))))
+ [[x y & _]]
+ (de-pt (.transform xform-to-view (pt x y) nil)))
+
+;;;; State
+
+(def *tris* (ref nil))
+
+;;;; Display
+
+(defn render
+ "Render the scene."
+ [^Graphics2D g2]
+ (doto g2
+ (.setPaint Color/BLACK)
+ (.fillRect 0 0 (dec view-w) (dec view-h))
+ (.setPaint Color/WHITE))
+ (let [displayable (filter (comp g/is-CCW2? t/vertices)
+ (map (partial t/xform2 to-view) @*tris*))]
+ (println "After backside culling:" (count displayable))
+ (doseq [t displayable]
+ (doseq [e (t/edges t)]
+ (let [[x1 y1 x2 y2] (flatten e)]
+ (.draw g2 (java.awt.geom.Line2D$Double. x1 y1 x2 y2)))))))
;;;; Interaction
+(defn ^JComponent new-canvas
+ "Make a canvas with the given size."
+ [w h]
+ (doto (proxy [JComponent] []
+ (paint [^Graphics2D g] (render g)))
+ (.setDoubleBuffered true)
+ (.setMinimumSize (Dimension. w h))
+ (.setPreferredSize (Dimension. w h))))
+
(defn launch
[tris]
+ (dosync (ref-set *tris* tris))
(UIManager/setLookAndFeel (UIManager/getSystemLookAndFeelClassName))
- (println (count tris)))
+ (let [canvas (new-canvas view-w view-h)]
+ (doto (JFrame. "TimMc HW4 - CS4300")
+ (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
+ (.add canvas)
+ (.pack)
+ (.setResizable false)
+ (.setVisible true)))
+ (println "Recieved" (count tris) "triangles"))
(defn start
"Start an instance on the event loop."
View
23 src/timmcHW4/geom.clj
@@ -3,12 +3,15 @@
;;;; Utility
+;; TODO: move to separate utility class?
(defn wrap-pairs
"Given a collection [a b c ... y z],
return seq [[a b] [b c] ... [y z] [z a]]"
[coll]
(take (count coll) (partition 2 1 (cycle coll))))
+;;;; Vector basics
+
(defn mag
"Compute magnitude of a vector."
[v]
@@ -19,11 +22,6 @@
[v1 v2]
(apply + (map * v1 v2)))
-(defn normal2
- "Compute the left normal of a 2D vector."
- [[x y]]
- [(- y) x])
-
(defn unit
"Normalize to unit vector. Zero vector is returned unchanged."
[v]
@@ -43,11 +41,18 @@
[m v]
(vec (take m (concat v (repeat 0)))))
+;;;; Plane geometry
+
+(defn normal2
+ "Compute the left normal of a 2D vector."
+ [[x y]]
+ [(- y) x])
+
(defn dist-to-line2
"Get the signed distance from the point (p) to the line (from s through e)
in the [x y] plane. Points to the left get a positive value."
[p s e]
- (dot (vect s p)
+ (dot (project 2 (vect s p))
(unit (normal2 (vect s e)))))
(defn in-poly2?
@@ -60,3 +65,9 @@
(if (some neg? dists)
nil
(reduce min dists))))
+
+(defn is-CCW2?
+ "Check if the [x y] vertices go in CCW order."
+ [verts]
+ (let [edges (wrap-pairs (map (partial project 2) verts))]
+ ())) ;; todo: culling
View
16 src/timmcHW4/tri.clj
@@ -10,18 +10,34 @@
;;; CCW order and :c to a corresponding collection of 3 [r g b] float colls
;;; for vertex colors.
+;;;; Constructors
+
(defn make
"Make a triangle from three vertex colls and three [r g b] colls."
[v0 v1 v2 c0 c1 c2]
{:v [v0 v1 v2]
:c [c0 c1 c2]})
+;;;; Accessors
+
(defn vertices
"Get the vertices of a triangle."
[tri]
(:v tri))
+(defn edges
+ "Get the edge pairs of a triangle."
+ [tri]
+ (g/wrap-pairs (vertices tri)))
+
(defn colors
"Get the colors of a triangle's vertices."
[tri]
(:c tri))
+
+;;;; Geometry
+
+(defn xform2
+ "Transform a triangle projected into [x y] using a vertex function."
+ [vf tri]
+ (update-in tri [:v] #(map vf %)))
View
10 test/timmcHW4/test/geom.clj
@@ -14,14 +14,14 @@
[[1 1] [3 1] [1 3]])
(deftest distance
- (is (= (dist-to-line2 [3 102] [0 100] [5 100]) 2))
- (is (= (dist-to-line2 [3 102] [5 100] [0 100]) -2)))
+ (is (== (dist-to-line2 [3 102] [0 100] [5 100]) 2))
+ (is (== (dist-to-line2 [3 102] [5 100] [0 100]) -2)))
(deftest picking
(is (not (in-poly2? [2 3] right-tri)))
(is (not (in-poly2? [0 0] right-tri)))
(is (not (in-poly2? [2 0] right-tri)))
- (is (= (in-poly2? [1.5 1.5] right-tri) 0.5))
- (is (= (in-poly2? [1.25 1.5] right-tri) 0.25))
- (is (= (in-poly2? [1.5 1.25] right-tri) 0.25)))
+ (is (== (in-poly2? [1.5 1.5] right-tri) 0.5))
+ (is (== (in-poly2? [1.25 1.5] right-tri) 0.25))
+ (is (== (in-poly2? [1.5 1.25] right-tri) 0.25)))
View
3  test/timmcHW4/test/tri.clj
@@ -5,7 +5,8 @@
(deftest accessors
(let [basic-tri (make [0 1] [2 3] [4 5] [0 0 1] [0 1 0] [1 0 0])]
(is (= (vertices basic-tri) [[0 1] [2 3] [4 5]]))
- (is (= (colors basic-tri) [[0 0 1] [0 1 0] [1 0 0]]))))
+ (is (= (colors basic-tri) [[0 0 1] [0 1 0] [1 0 0]]))
+ (is (= (edges basic-tri) [[[0 1] [2 3]] [[2 3] [4 5]] [[4 5] [0 1]]]))))
Please sign in to comment.
Something went wrong with that request. Please try again.