Skip to content

Commit 0cc759c

Browse files
committed
Rendering again after refactor
1 parent 7aa529b commit 0cc759c

File tree

8 files changed

+205
-77
lines changed

8 files changed

+205
-77
lines changed

project.clj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
opengl.models
1111
opengl.opengl
1212
opengl.route
13+
opengl.objects
1314
opengl.b3d]
1415
:dependencies [[org.clojure/clojure "1.4.0"]
1516
[org.clojars.toxi/jogl "2.0.0-rc10"]

src/opengl/b3d.clj

+4
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,7 @@
243243

244244
(defn parse-file [^java.io.File f]
245245
(parse-string (slurp f) {:path (.getParent f)}))
246+
247+
(defn parse-file-from-string [^String s]
248+
(try (parse-file (java.io.File. s))
249+
(catch Exception e {:errors (format "File not found %s" s)})))

src/opengl/builder.clj

+32-27
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@
8484
(if (= (count (.trim s)) 0) nil
8585
(read-string s)))
8686

87+
(defn- trim-trailing-comma [^String s]
88+
(if (= \, (last s))
89+
(.substring s 0 (- (count s) 1))
90+
s))
91+
8792
(defn- split-body [node]
8893
(if (nil? node)
8994
[]
9095
(map read-string-if-not-empty
9196
(.split
92-
(.trim ^String (route/trim-trailing-comma (:body node))) ";"))))
97+
(.trim ^String (trim-trailing-comma (:body node))) ";"))))
9398

9499
(defn create-rail-objects
95100
[block rail track-pos]
@@ -724,29 +729,29 @@
724729
:next-block next-block))
725730
blocks (concat (rest blocks) [nil])))
726731

727-
(def r
728-
(route/resolve-symbol-table
729-
(route/parse-route-file "Flushing/test.csv")))
730-
(def s (:symbol-table r))
731-
(def bv (build-block-vector (:nodes r) 25))
732-
(def ablock (nth bv 0))
733-
(def bblock (nth bv 1))
734-
(def cblock (nth bv 2))
735-
(def dblock (nth bv 3))
736-
737-
(def blocks (take 20 bv))
738-
(def ablocks (provide-forward-references blocks))
739-
(def ablocks (second
740-
(reduce (fn [[context blocks] block]
741-
(let [[context block]
742-
(parse-block-information context (last blocks) block s)]
743-
[context (conj blocks block)]))
744-
[(create-starting-context s) []] ablocks)))
745-
(let [[context obj]
746-
(reduce (fn [[context objs] block]
747-
(let [[context new-objs] (create-objects-for-block2 context block)]
748-
[context
749-
(concat objs new-objs)]))
750-
[(create-starting-context s) []]
751-
ablocks)]
752-
(def objs obj))
732+
;; (def r
733+
;; (route/resolve-symbol-table
734+
;; (route/parse-route-file "Flushing/test.csv")))
735+
;; (def s (:symbol-table r))
736+
;; (def bv (build-block-vector (:nodes r) 25))
737+
;; (def ablock (nth bv 0))
738+
;; (def bblock (nth bv 1))
739+
;; (def cblock (nth bv 2))
740+
;; (def dblock (nth bv 3))
741+
742+
;; (def blocks (take 20 bv))
743+
;; (def ablocks (provide-forward-references blocks))
744+
;; (def ablocks (second
745+
;; (reduce (fn [[context blocks] block]
746+
;; (let [[context block]
747+
;; (parse-block-information context (last blocks) block s)]
748+
;; [context (conj blocks block)]))
749+
;; [(create-starting-context s) []] ablocks)))
750+
;; (let [[context obj]
751+
;; (reduce (fn [[context objs] block]
752+
;; (let [[context new-objs] (create-objects-for-block2 context block)]
753+
;; [context
754+
;; (concat objs new-objs)]))
755+
;; [(create-starting-context s) []]
756+
;; ablocks)]
757+
;; (def objs obj))

src/opengl/objects.clj

+100-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,46 @@
44
[opengl.geom :as geom]
55
[opengl.util :as util]
66
[opengl.b3d :as b3d]
7+
[opengl.models :as m]
78
[opengl.route :as route]]
89
(:gen-class))
910

11+
12+
(defn- apply-translate-to-mesh
13+
[mesh [tx ty tz]
14+
normal-transform1
15+
normal-transform2]
16+
(m/create-mesh
17+
(map (fn [face]
18+
(let [verts
19+
(map (fn [vert]
20+
(let [[x y z] (geom/rotate-with-transform
21+
(geom/rotate-with-transform
22+
(:coordinate vert) normal-transform1)
23+
normal-transform2)
24+
[nx ny nz] (:normal vert)
25+
[nx' ny' nz'] (geom/rotate-with-transform
26+
(geom/rotate-with-transform
27+
[nx ny nz] normal-transform1)
28+
normal-transform2)]
29+
(let [v (m/create-vertex
30+
[(+ tx x)
31+
(+ ty y)
32+
(+ tz z)]
33+
(:texture-coordinate vert)
34+
[nx' ny' nz'])]
35+
v)))
36+
(:verts face))]
37+
(m/update-face face verts)))
38+
(:faces mesh))))
39+
40+
(defn- create-transformed-object
41+
[prototype pos base-transform aux-transform track-position]
42+
(map (fn [mesh]
43+
(apply-translate-to-mesh mesh pos aux-transform base-transform))
44+
prototype))
45+
46+
1047
(defn- create-track-transforms-in-block-with-direction
1148
[block direction]
1249
(let [[dx dy] direction
@@ -52,12 +89,12 @@
5289
radius (:radius curves)
5390
cant (:cant curves)
5491
block-length (- (:end-ref block) (:start-ref block))]
55-
(if (or (nil? radius) (= radius 0.0))
92+
(if (or (nil? radius) (= (float radius) 0.0))
5693
(create-track-transforms-in-block-with-direction
5794
block direction)
58-
(let [b (/ block-length (Math/abs radius))
59-
c (Math/sqrt (* 2.0 radius radius (- 1.0 (Math/cos b))))
60-
a (* -1.0 0.5 (Math/signum radius) b)]
95+
(let [b (/ block-length (Math/abs (float radius)))
96+
c (Math/sqrt (* 2.0 (float radius) (float radius) (- 1.0 (Math/cos b))))
97+
a (* -1.0 0.5 (Math/signum (float radius)) b)]
6198
(create-track-transforms-in-block-with-direction
6299
block
63100
(geom/rotate-vector-2d
@@ -77,15 +114,23 @@
77114

78115
direction (:direction block)
79116
position (:position block)
117+
118+
zzz (println "Incoming" direction position)
119+
80120
curve (:curve block)
81121
height (or (:height block) 0.0)
122+
123+
;;TODO remove me
124+
height 0.0
125+
82126
block-length (- (:end-ref block) (:start-ref block))
83127

84128
end-x (or end-x start-x)
85129
end-y (or end-y start-y)
86130

87131
[dx dy] direction
88132
[pxo pyo pzo] position
133+
89134
[px py pz] (geom/vector-add position [(* dy start-x) start-y (* -1.0 dx start-x)])
90135

91136
px2 (+ (* dx block-length) pxo)
@@ -120,16 +165,25 @@
120165
(* 2.0 next-curve next-curve (- 1.0 (Math/cos b2))))
121166

122167
a2 (* 0.5 (Math/signum next-curve) b2)
168+
169+
zzz (println "a2" a2 direction2)
170+
123171
[dx2 dy2] (geom/rotate-vector-2d direction2
124172
(Math/cos (- a2)) (Math/sin (- a2)))
125173

174+
zzz (println "dx2" dx2 dy2)
175+
126176
offset2 [(* dy2 end-x) end-y (* -1.0 dx2 end-x)]
127177
position2 (geom/vector-add offset2 [px2 py2 pz2])
128178

179+
zzz (println "op" offset2 position2)
180+
129181
[pdx pdy pdz] (geom/vector-normalize (geom/vector-sub position2 [px py pz]))
130182

131183
[norm-x norm-z] (geom/vector-normalize [pdx pdz])
132184

185+
zzz (println "pdx pdy pdz" pdx pdy pdz)
186+
133187
rail-trans-z [pdx pdy pdz]
134188
rail-trans-x [norm-z 0.0 (* -1.0 norm-x)]
135189
rail-trans-y (geom/vector-cross rail-trans-z rail-trans-x)
@@ -167,7 +221,8 @@
167221
rail
168222
(if (or (= railidx 0) (nil? next-block))
169223
(create-transforms-for-player-rail block)
170-
(create-transforms-for-rail block next-block rail)
224+
(do
225+
(create-transforms-for-rail block next-block rail))
171226
))])
172227
(:rails block))))))
173228

@@ -183,3 +238,43 @@
183238
(update-position-in-block (last blocks) block block-length))
184239
(next-block context block))))
185240
[] (:blocks context)))))
241+
242+
(defn- get-rail-aligned-objects-in-rail [context block rail]
243+
(let [[dx dy] (:start rail)
244+
[x y z] (:position block)
245+
x (+ dx x)
246+
y (+ dy y)
247+
pos [x y z]
248+
rail-transform (:rail-transform rail)
249+
track-pos (:start-ref block)]
250+
(create-transformed-object
251+
(:prototype rail) pos
252+
rail-transform geom/identity-transform
253+
track-pos)))
254+
255+
(defn- get-rail-aligned-objects-in-block [context block]
256+
(apply
257+
concat
258+
(map
259+
#(get-rail-aligned-objects-in-rail context block (second %))
260+
(:rails block))))
261+
262+
(defn get-drawable-objects-in-block [context block]
263+
(get-rail-aligned-objects-in-block context block))
264+
265+
(defn get-drawable-objects-in-context [context]
266+
(reduce (fn [objs block]
267+
(concat
268+
objs
269+
(get-drawable-objects-in-block context block)))
270+
[]
271+
(:blocks context)))
272+
273+
(def context
274+
(create-geometries-for-blocks-in-context
275+
(route/parse-route-file "Flushing/test.csv")))
276+
277+
(def context (assoc context :blocks (take 10 (:blocks context))))
278+
(def objs (get-drawable-objects-in-context context))
279+
280+
(def dummy 1)

src/opengl/opengl.clj

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns opengl.opengl
22
[:require
33
[opengl.core :as core]
4+
[opengl.objects :as objects]
45
[opengl.route :as route]
56
[opengl.models :as m]
67
[opengl.geom :as geom]
@@ -222,7 +223,7 @@
222223
(def gl-context
223224
(ref {:camera {:eye [20.0 20.0 -50.0]
224225
:center [0.0 0.0 0.0]}
225-
:meshes (filter identity builder/objs)
226+
:meshes (filter identity objects/objs)
226227
}))
227228

228229
(defn glu-look-at [^GLUgl2 glu
@@ -273,9 +274,11 @@
273274

274275
(.glScalef gl (float -1.0) 1.0 1.0)
275276

276-
(doseq [meshes-from-b3d objs]
277-
(doseq [mesh meshes-from-b3d]
278-
(gl-render-mesh gl mesh)))
277+
;; (doseq [meshes-from-b3d objs]
278+
;; (doseq [mesh meshes-from-b3d]
279+
;; (gl-render-mesh gl mesh)))
280+
(doseq [mesh objs]
281+
(gl-render-mesh gl mesh))
279282

280283
(gl-draw-axis gl)
281284
(display-fps gl)))
@@ -349,16 +352,4 @@
349352
'())
350353

351354
;(set-looking-at canvas 20.0 20.0 -50.0)
352-
;(set-looking-at canvas 1.0 10.0 -50.0)
353-
;(set-center canvas 0.0 0.0 10.0)
354-
;(set-center canvas 0.0 0.0 120.0)
355-
;(set-looking-at canvas 1.0 5.0 60.0)
356-
(set-center canvas 0.0 0.0 400.0)
357-
(set-looking-at canvas 1.0 5.0 10.0)
358-
;(set-center canvas 0.0 0.0 400.0)
359-
;(set-looking-at canvas 1.0 5.0 -10.0)
360-
;(set-looking-at canvas 1.0 5.0 50.0)
361-
(set-looking-at canvas 1.0 3.0 60.0)
362-
;(set-looking-at canvas 1.0 3.0 10.0)
363-
(set-center canvas 40.0 0.0 500.0)
364-
(set-looking-at canvas 23.0 3.0 400.0)
355+
(set-looking-at canvas 1.0 15.0 -50.0)

0 commit comments

Comments
 (0)