Skip to content

Unable to specify colors per vertex #99

@p-himik

Description

@p-himik

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)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions