Mercurial > cortex
comparison org/cortex.org @ 25:775d97247dd0
cleaning up world.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 24 Oct 2011 05:25:01 -0700 |
parents | cab2da252494 |
children | 6372c108c5c6 |
comparison
equal
deleted
inserted
replaced
24:e965675ec4d0 | 25:775d97247dd0 |
---|---|
76 (use 'clojure.contrib.def) | 76 (use 'clojure.contrib.def) |
77 (rlm.rlm-commands/help) | 77 (rlm.rlm-commands/help) |
78 (cortex.import/mega-import-jme3) | 78 (cortex.import/mega-import-jme3) |
79 (use '[pokemon [lpsolve :only [constant-map]]]) | 79 (use '[pokemon [lpsolve :only [constant-map]]]) |
80 (use 'cortex.world) | 80 (use 'cortex.world) |
81 (use 'cortex.util) | |
81 #+end_src | 82 #+end_src |
82 | 83 |
83 #+srcname: brick-wall-body | 84 #+srcname: brick-wall-body |
84 #+begin_src clojure :results silent | 85 #+begin_src clojure :results silent |
85 (in-ns 'hello.brick-wall) | 86 (in-ns 'hello.brick-wall) |
742 ))) | 743 ))) |
743 #+end_src | 744 #+end_src |
744 | 745 |
745 | 746 |
746 | 747 |
747 * The Body | |
748 ** Eyes | |
749 | |
750 Ultimately I want to make creatures with eyes. Each eye can be | |
751 independely moved and should see its own version of the world | |
752 depending on where it is. | |
753 #+srcname: eyes | |
754 #+begin_src clojure | |
755 (ns body.eye) | |
756 (use 'cortex.world) | |
757 (use 'cortex.import) | |
758 (use 'clojure.contrib.def) | |
759 (cortex.import/mega-import-jme3) | |
760 (rlm.rlm-commands/help) | |
761 (import java.nio.ByteBuffer) | |
762 (import java.awt.image.BufferedImage) | |
763 (import java.awt.Color) | |
764 (import java.awt.Dimension) | |
765 (import java.awt.Graphics) | |
766 (import java.awt.Graphics2D) | |
767 (import java.awt.event.WindowAdapter) | |
768 (import java.awt.event.WindowEvent) | |
769 (import java.awt.image.BufferedImage) | |
770 (import java.nio.ByteBuffer) | |
771 (import javax.swing.JFrame) | |
772 (import javax.swing.JPanel) | |
773 (import javax.swing.SwingUtilities) | |
774 (import javax.swing.ImageIcon) | |
775 (import javax.swing.JOptionPane) | |
776 (import java.awt.image.ImageObserver) | |
777 | |
778 | |
779 | |
780 (defn scene-processor | |
781 "deals with converting FrameBuffers to BufferedImages so | |
782 that the continuation function can be defined only in terms | |
783 of what it does with BufferedImages" | |
784 [continuation] | |
785 (let [byte-buffer (atom nil) | |
786 renderer (atom nil) | |
787 image (atom nil)] | |
788 (proxy [SceneProcessor] [] | |
789 (initialize | |
790 [renderManager viewPort] | |
791 (let [cam (.getCamera viewPort) | |
792 width (.getWidth cam) | |
793 height (.getHeight cam)] | |
794 (reset! renderer (.getRenderer renderManager)) | |
795 (reset! byte-buffer | |
796 (BufferUtils/createByteBuffer | |
797 (* width height 4))) | |
798 (reset! image (BufferedImage. width height | |
799 BufferedImage/TYPE_4BYTE_ABGR)))) | |
800 (isInitialized [] (not (nil? @byte-buffer))) | |
801 (reshape [_ _ _]) | |
802 (preFrame [_]) | |
803 (postQueue [_]) | |
804 (postFrame | |
805 [#^FrameBuffer fb] | |
806 (.clear @byte-buffer) | |
807 (.readFrameBuffer @renderer fb @byte-buffer) | |
808 (Screenshots/convertScreenShot @byte-buffer @image) | |
809 (continuation @image)) | |
810 (cleanup [])))) | |
811 | |
812 (defn add-eye | |
813 "Add an eye to the world, and call continuation on | |
814 every frame produced" | |
815 [world camera continuation] | |
816 (let [width (.getWidth camera) | |
817 height (.getHeight camera) | |
818 render-manager (.getRenderManager world) | |
819 viewport (.createMainView render-manager "eye-view" camera)] | |
820 (doto viewport | |
821 (.setBackgroundColor ColorRGBA/Black) | |
822 (.setClearFlags true true true) | |
823 (.addProcessor (scene-processor continuation)) | |
824 (.attachScene (.getRootNode world))))) | |
825 | |
826 (defn make-display-frame [display width height] | |
827 (SwingUtilities/invokeLater | |
828 (fn [] | |
829 (.setPreferredSize display (Dimension. width height)) | |
830 (doto (JFrame. "Eye Camera!") | |
831 (-> (.getContentPane) (.add display)) | |
832 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE) | |
833 (.pack) | |
834 (.setLocationRelativeTo nil) | |
835 (.setResizable false) | |
836 (.setVisible true))))) | |
837 | |
838 (defn image-monitor [#^BufferedImage image] | |
839 (proxy [JPanel] [] | |
840 (paintComponent | |
841 [g] | |
842 (proxy-super paintComponent g) | |
843 (locking image | |
844 (.drawImage g image 0 0 | |
845 (proxy [ImageObserver] | |
846 [] | |
847 (imageUpdate | |
848 [] | |
849 (proxy-super imageUpdate)))))))) | |
850 | |
851 (defn movie-image [] | |
852 (let [setup | |
853 (runonce | |
854 (fn [#^BufferedImage image] | |
855 (let [width (.getWidth image) | |
856 height (.getHeight image) | |
857 display (image-monitor image) | |
858 frame (make-display-frame display width height)] | |
859 display)))] | |
860 (fn [#^BufferedImage image] | |
861 (.repaint (setup image))))) | |
862 | |
863 | |
864 (defn observer | |
865 "place thy eye!" | |
866 [world camera] | |
867 (let [eye camera | |
868 width (.getWidth eye) | |
869 height (.getHeight eye)] | |
870 (no-exceptions | |
871 (add-eye | |
872 world | |
873 eye | |
874 (movie-image))))) | |
875 #+end_src | |
876 | |
877 #+srcname: test-vision | |
878 #+begin_src clojure | |
879 | |
880 (ns test.vision) | |
881 (use 'cortex.world) | |
882 (use 'cortex.import) | |
883 (use 'clojure.contrib.def) | |
884 (use 'body.eye) | |
885 (cortex.import/mega-import-jme3) | |
886 (rlm.rlm-commands/help) | |
887 (import java.nio.ByteBuffer) | |
888 (import java.awt.image.BufferedImage) | |
889 (import java.awt.Color) | |
890 (import java.awt.Dimension) | |
891 (import java.awt.Graphics) | |
892 (import java.awt.Graphics2D) | |
893 (import java.awt.event.WindowAdapter) | |
894 (import java.awt.event.WindowEvent) | |
895 (import java.awt.image.BufferedImage) | |
896 (import java.nio.ByteBuffer) | |
897 (import javax.swing.JFrame) | |
898 (import javax.swing.JPanel) | |
899 (import javax.swing.SwingUtilities) | |
900 (import javax.swing.ImageIcon) | |
901 (import javax.swing.JOptionPane) | |
902 (import java.awt.image.ImageObserver) | |
903 | |
904 | |
905 (def width 200) | |
906 (def height 200) | |
907 | |
908 (defn camera [] | |
909 (doto (Camera. width height) | |
910 (.setFrustumPerspective 45 1 1 1000) | |
911 (.setLocation (Vector3f. -3 0 -5)) | |
912 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))) | |
913 | |
914 (defn camera2 [] | |
915 (doto (Camera. width height) | |
916 (.setFrustumPerspective 45 1 1 1000) | |
917 (.setLocation (Vector3f. 3 0 -5)) | |
918 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))) | |
919 | |
920 (defn setup-fn [world] | |
921 (let [eye (camera) | |
922 width (.getWidth eye) | |
923 height (.getHeight eye)] | |
924 (no-exceptions | |
925 (add-eye | |
926 world | |
927 eye | |
928 (runonce visual)) | |
929 (add-eye | |
930 world | |
931 (camera2) | |
932 (runonce visual))))) | |
933 | |
934 (defn spider-eye [position] | |
935 (doto (Camera. 200 200 ) | |
936 (.setFrustumPerspective 45 1 1 1000) | |
937 (.setLocation position) | |
938 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))) | |
939 | |
940 (defn setup-fn* [world] | |
941 (let [eye (camera) | |
942 width (.getWidth eye) | |
943 height (.getHeight eye)] | |
944 ;;(.setClearFlags (.getViewPort world) true true true) | |
945 (observer world (.getCamera world)) | |
946 (observer world (spider-eye (Vector3f. 3 0 -5))) | |
947 ;;(observer world (spider-eye (Vector3f. 0 0 -5))) | |
948 ;; (observer world (spider-eye (Vector3f. -3 0 -5))) | |
949 ;; (observer world (spider-eye (Vector3f. 0 3 -5))) | |
950 ;; (observer world (spider-eye (Vector3f. 0 -3 -5))) | |
951 ;; (observer world (spider-eye (Vector3f. 3 3 -5))) | |
952 ;; (observer world (spider-eye (Vector3f. -3 3 -5))) | |
953 ;; (observer world (spider-eye (Vector3f. 3 -3 -5))) | |
954 ;; (observer world (spider-eye (Vector3f. -3 -3 -5))) | |
955 | |
956 ) | |
957 world) | |
958 | |
959 (defn test-world [] | |
960 (let [thing (box 1 1 1 :physical? false)] | |
961 (world | |
962 (doto (Node.) | |
963 (.attachChild thing)) | |
964 {} | |
965 setup-fn | |
966 (fn [world tpf] | |
967 (.rotate thing (* tpf 0.2) 0 0) | |
968 )))) | |
969 | |
970 | |
971 #+end_src | |
972 | |
973 | |
974 #+results: eyes | |
975 : #'body.eye/test-world | |
976 | |
977 Note the use of continuation passing style for connecting the eye to a | |
978 function to process the output. The example code will create two | |
979 videos of the same rotating cube from different angles, sutiable for | |
980 stereoscopic vision. | |
981 | |
982 | |
983 | |
984 | |
985 | |
986 | |
987 * COMMENT code generation | 748 * COMMENT code generation |
988 #+begin_src clojure :tangle ../src/cortex/import.clj | |
989 <<import>> | |
990 #+end_src | |
991 | 749 |
992 #+begin_src clojure :tangle ../src/hello/brick_wall.clj | 750 #+begin_src clojure :tangle ../src/hello/brick_wall.clj |
993 <<brick-wall-header>> | 751 <<brick-wall-header>> |
994 <<brick-wall-body>> | 752 <<brick-wall-body>> |
995 #+end_src | 753 #+end_src |
996 | 754 |
997 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj | 755 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj |
998 <<hello-simple-app>> | 756 <<hello-simple-app>> |
999 #+end_src | 757 #+end_src |
1000 | |
1001 #+begin_src clojure :tangle ../src/cortex/world.clj | |
1002 <<world-inputs>> | |
1003 <<world>> | |
1004 <<world-shapes>> | |
1005 <<world-view>> | |
1006 #+end_src | |
1007 | 758 |
1008 #+begin_src clojure :tangle ../src/cortex/other_games.clj | 759 #+begin_src clojure :tangle ../src/cortex/other_games.clj |
1009 <<other-games>> | 760 <<other-games>> |
1010 #+end_src | 761 #+end_src |
1011 | 762 |
1027 | 778 |
1028 #+begin_src clojure :tangle ../src/hello/material.clj | 779 #+begin_src clojure :tangle ../src/hello/material.clj |
1029 <<material>> | 780 <<material>> |
1030 #+end_src | 781 #+end_src |
1031 | 782 |
1032 #+begin_src clojure :tangle ../src/body/eye.clj | |
1033 <<eyes>> | |
1034 #+end_src | |
1035 | |
1036 #+begin_src clojure :tangle ../src/test/vision.clj | |
1037 <<test-vision>> | |
1038 #+end_src | |
1039 | 783 |
1040 | 784 |
1041 | 785 |