Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to specify colors per vertex #99

Open
p-himik opened this issue Nov 21, 2023 · 0 comments
Open

Unable to specify colors per vertex #99

p-himik opened this issue Nov 21, 2023 · 0 comments

Comments

@p-himik
Copy link

p-himik commented Nov 21, 2023

I might be misunderstanding something but AFAICT judging by the source, gl/as-gl-buffer-spec can accept not only :fixed-color (which works) but also :colors.

Given this line, it seems that :colors are supposed to be a collection of collections, so I guess RGB or RGBA vectors.

However, trying to do that fails due to how fill-vertex-buffer is implemented for vectors - it expects for each element of the vector to be something that implements IIntoBuffer, and numbers don't implement that.

I see that the comment above the common-attrib-buffer-specs function mentions that :colors isn't expected to work.
However, I was able to make it work (at least, in CLJS) with this:

(extend-protocol streams/IIntoBuffer
  number
  (into-float-buffer
    [x dest _stride idx]
    [(aset dest idx x)]
    (unchecked-add-int idx 1)))

This is a complete working example:

(ns app.thing
  (:require [thi.ng.dstruct.streams :as streams]
            [thi.ng.geom.gl.core :as gl]
            [thi.ng.geom.gl.shaders :as sh]
            [thi.ng.geom.gl.shaders.basic :as basic]
            [thi.ng.geom.gl.webgl.constants :as glc]
            [thi.ng.geom.matrix :as mat]
            [thi.ng.geom.polygon :as poly]))

(extend-protocol streams/IIntoBuffer
  number
  (into-float-buffer
    [x dest _stride idx]
    [(aset dest idx x)]
    (unchecked-add-int idx 1)))

(defn init []
  (let [gl (gl/gl-context "canvas")
        view-rect (gl/get-viewport-rect gl)
        shader (sh/make-shader-from-spec gl (basic/make-shader-spec-2d true))
        model (-> (poly/polygon2 [[-1 -1] [0 1] [1 -1]])
                  (gl/as-gl-buffer-spec {:normals false
                                         :colors  [[1 0 0 1]
                                                   [0 1 0 1]
                                                   [0 0 1 1]]})
                  (gl/make-buffers-in-spec gl glc/static-draw)
                  (update :uniforms merge {:proj  (gl/ortho view-rect)
                                           :model mat/M44})
                  (assoc :shader shader))]
    (gl/set-viewport gl view-rect)
    (gl/clear-color-and-depth-buffer gl 1 0.98 0.95 1 1)
    (gl/draw-with-shader gl model)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant