diff org/depth-map.org @ 458:42ddfe406c0a

working on depth maps for the lulz.
author Robert McIntyre <rlm@mit.edu>
date Tue, 11 Jun 2013 07:13:42 -0400
parents
children a86555b02916
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/depth-map.org	Tue Jun 11 07:13:42 2013 -0400
     1.3 @@ -0,0 +1,51 @@
     1.4 +
     1.5 +#+begin_src clojure
     1.6 +(ns cortex.depth-map
     1.7 +  "lol depth maps."
     1.8 +  {:author "Robert McIntyre"}
     1.9 +  (:use (cortex world util body sense vision touch))
    1.10 +  (:import (com.jme3.scene Geometry Node Mesh))
    1.11 +  (:import com.jme3.collision.CollisionResults)
    1.12 +  (:import com.jme3.scene.VertexBuffer$Type)
    1.13 +
    1.14 +  (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
    1.15 +
    1.16 +(cortex.import/mega-import-jme3)
    1.17 +
    1.18 +(defn convert-pixel-to-ray
    1.19 +  [camera x y]
    1.20 +  (let [pixel-arrow
    1.21 +        (.getWorldCoordinates
    1.22 +         camera
    1.23 +         (Vector2f. (float x) (float y))
    1.24 +         ;; this is the "z depth" and can be anything, since
    1.25 +         ;; we only care about direction
    1.26 +         (float 1))
    1.27 +        ;; now that we have the direction of a pixel, subtract
    1.28 +        ray (Ray.)]
    1.29 +    (set-ray ray Matrix4f/IDENTITY (.getLocation camera)
    1.30 +             pixel-arrow) ray))
    1.31 +
    1.32 +
    1.33 +(defn generate-depth-map-points
    1.34 +  "Generate a data structure representating the depth of the first
    1.35 +  object to collide with a ray from the camera's current position."
    1.36 +  [world camera]
    1.37 +  (let [width (.getWidth camera)
    1.38 +        height (.getHeight camera)
    1.39 +        pixels (for [x (range width) y (range height)]
    1.40 +                 [x y])
    1.41 +        
    1.42 +        depths
    1.43 +        (for [[x y] pixels]
    1.44 +          (let [ray (convert-pixel-to-ray camera x y)
    1.45 +                results (CollisionResults.)]
    1.46 +            (.collideWith world ray results)
    1.47 +            (.getDistance (.getClosestCollision results))))]
    1.48 +    (zipmap pixels depths)))
    1.49 +
    1.50 +(defn test-world []
    1.51 +  (nodify [(floor*) (brick-wall*)]))
    1.52 +
    1.53 +(defn run-depth-map-test []
    1.54 +#+end_src
    1.55 \ No newline at end of file