Created
June 4, 2015 22:08
-
-
Save sgrove/ae5a04613f6f633d465a to your computer and use it in GitHub Desktop.
Framebuffer draw call working and not working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn draw-fn [gl driver programs] | |
(fn [state] | |
(let [{:keys [p mv | |
color-texture specular-texture | |
sphere | |
model | |
rotation]} (:scene state) | |
now (/ (.getTime (js/Date.)) 50) | |
rotation (- (utils/deg->rad now)) | |
program (get programs :simple) | |
point-lighting-location {:data #js[-10 4 0] | |
:immutable? false} | |
square-mv (-> mv | |
;;(geom/translate [(* rotation 4) -0.5 -5]) | |
(geom/translate [(* (js/Math.sin (/ now 8)) 4) (* (js/Math.cos (/ now 3)) 2) -5]) | |
(geom/* (-> (mat/matrix44) | |
(geom/rotate-around-axis [0 1 0] rotation) | |
(geom/rotate-around-axis [1 0 0] (- (/ js/Math.PI 3))))) | |
object-array) | |
model-mv (-> mv | |
(geom/translate [0 -0.5 -2.5]) | |
(geom/* (-> M44 | |
(geom/rotate-around-axis [1 0 0] (- (/ js/Math.PI 2)))))) | |
sphere-mv (-> mv | |
(geom/translate [-3 -0.5 -2.5]) | |
(geom/* (-> M44 | |
(geom/rotate-around-axis [0 1 0] rotation) | |
(geom/rotate-around-axis [1 0 0] (- (/ js/Math.PI 2)))))) | |
scene-data (-> (get-data p model-mv (:vertices model) (:normals model) color-texture (:texture-coords model) point-lighting-location #js[0.2 0.2 0.2] #js[0.2 0.2 0.2] #js[0.2 0.2 0.2]) | |
(select-keys (get-in programs [:specular :inputs])) | |
(assoc {:tag :element-index} (:indices model))) | |
screen-data (-> (get-data p model-mv (:vertices laptop-screen) (:normals laptop-screen) (get-in state [:framebuffer :color]) | |
(:texture-coords laptop-screen) point-lighting-location #js[0.2 0.2 0.2] #js[0.2 0.2 0.2] #js[0.2 0.2 0.2]) | |
(select-keys (get-in programs [:specular :inputs])) | |
(assoc {:tag :element-index} (:indices model))) | |
sphere-data (-> (get-data p sphere-mv (:vertices sphere) (:normals sphere) color-texture (:texture-coords sphere) point-lighting-location #js[0.2 0.2 0.2] #js[0.2 0.2 0.2] #js[0.2 0.2 0.2]) | |
(select-keys (get-in programs [:specular :inputs])) | |
(assoc {:tag :element-index} (:indices sphere))) | |
screen-texture-data {progs/u-p-matrix p | |
progs/u-mv-matrix square-mv | |
progs/a-position (get-in state [:scene :square-vertices]) | |
progs/a-color (get-in state [:scene :square-colors])}] | |
(.clear gl (bit-or (.-COLOR_BUFFER_BIT gl) (.-DEPTH_BUFFER_BIT gl))) | |
;; First draw to the framebuffer | |
(.viewport gl 0 0 512 512) | |
(.bindFramebuffer gl ggl/FRAMEBUFFER (get-in state [:framebuffer :frame-buffer])) | |
(.clear gl (bit-or (.-COLOR_BUFFER_BIT gl) (.-DEPTH_BUFFER_BIT gl))) | |
;; 1. This draw call ends up in the frame buffer to be used as a texture | |
(gd/bind driver (get programs :simple) screen-texture-data) | |
(gd/draw-arrays driver (get programs :simple) {:draw-mode :triangle-strip | |
:count 4} (:framebuffer state)) | |
;; 2. This doesn't generate any errors, and does not end up in the frame buffer | |
(gd/bind driver (get programs :specular) sphere-data) | |
(gd/draw-elements driver (get programs :specular) {:draw-mode :triangles | |
:first 0 | |
:count (get-in sphere [:indices :count])} (:framebuffer state)) | |
;; Reset the viewport, bind our framebuffer as a texture, and draw the "real" scene | |
(.viewport gl 0 0 (get-in state [:canvas :width]) (get-in state [:canvas :height])) | |
(.bindTexture gl ggl/TEXTURE_2D (get-in state [:framebuffer :color :texture])) | |
(gd/bind driver (get programs :specular) screen-data) | |
(gd/draw-arrays driver (get programs :specular) {:draw-mode :triangle-strip | |
:count 4}) | |
(gd/draw-elements driver (gd/bind driver (get programs :specular) scene-data) {:draw-mode :triangles | |
:first 0 | |
:count (get-in model [:indices :count])}) | |
;; 3. Same call as #2, gets drawn properly | |
(gd/draw-elements driver (gd/bind driver (get programs :specular) sphere-data) {:draw-mode :triangles | |
:first 0 | |
:count (get-in sphere [:indices :count])})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment