(defn
compile
"Initializes the provided entity, compiling the shaders and creating all the\n necessary state for rendering."
[game entity]
(let
[{:keys [vertex fragment attributes uniforms indices]}
entity
vertex-source
(ig/iglu->glsl (merge {:version glsl-version} vertex))
fragment-source
(ig/iglu->glsl (merge {:version glsl-version} fragment))
previous-program
(gl
game
#?(:clj getInteger :cljs getParameter)
(gl game CURRENT_PROGRAM))
previous-vao
(gl
game
#?(:clj getInteger :cljs getParameter)
(gl game VERTEX_ARRAY_BINDING))
program
(u/create-program game vertex-source fragment-source)
_
(gl game useProgram program)
vao
(gl game #?(:clj genVertexArrays :cljs createVertexArray))
_
(gl game bindVertexArray vao)
attr-names
(u/get-attribute-names vertex)
entity
(cond->
entity
attr-names
(assoc
:attribute-buffers
(reduce
(fn [m attr-name] (assoc m attr-name (u/create-buffer game)))
{}
attr-names))
indices
(assoc :index-buffer (u/create-buffer game)))
entity
(set-buffers game entity program)
uniform-locations
(reduce
(fn
[m uniform]
(assoc
m
uniform
(gl game getUniformLocation program (name uniform))))
{}
(->
#{}
(into (-> vertex :uniforms keys))
(into (-> fragment :uniforms keys))))
entity
(merge
entity
{:vertex vertex,
:fragment fragment,
:vertex-source vertex-source,
:fragment-source fragment-source,
:program program,
:vao vao,
:uniform-locations uniform-locations,
:textures {}})
entity
(reduce (partial call-uniform game) entity uniforms)]
(some->>
entity
:render-to-texture
(render->texture game (:textures entity)))
(gl game useProgram previous-program)
(gl game bindVertexArray previous-vao)
(dissoc entity :uniforms :attributes :render-to-texture)))