thi-ng / geom

2D/3D geometry toolkit for Clojure/Clojurescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there an example which uses multiple textures?

Rovanion opened this issue · comments

I'm terribly sorry that I'm posing what's essentially a support question here on the issue tracker. But we've been unable to figure out how to use multiple textures with geom and have equally been able to find an example either among your workshops or in the given WebGL examples.

More specifically: What is it that determines :target of the map returned by buf/load-texture? Because for our application it's always the same value, i.e. all textures get loaded into the same texture unit.

Or it may be that I'm misunderstanding the purpose of :target Reading into the code of load-texture and then make-texture in buffers.cljc I see that :target is always set to glc/texture-2d, i.e. 3553.

Looked up the documentation on glBindTexture which seemed to indicate that I had completely misunderstood the purpose of :target which brings my question back to 0.

On with my adventure through your code: Through a refresh of my WebGL knowledge I found that it's gl.activeTexture() which selects which texture unit the texture is uploaded to, and in your buffers.cljc that's called in bind. Thing is that make-texture which in turn is what's called from load-texture doesn't pass on a second argument unit to bind which means that texture unit 0 is only ever used. It seems to me like some modifications to thi.ng/geom are requiered to use multiple textures.

Just pushed a new demo showing how to use multiple tex units: http://demo.thi.ng/geom/webgl/multitex/

For textures, gl/bind only defaults to tex unit 0 if no extra arg is given, but you can bind to a specific unit via (gl/bind tex id). Furthermore, if your shader uses multiple textures, you can either specify their default slots in the shader spec (see demo source) or set their uniforms dynamically for each use...

Thank you so much! This example also shows how we can bind different textures to the different texture units at will which be very useful to us.

Just a quick secondary question gl/release is the function which unloads a texture from GPU memory, right?

glad it helped! :) And yes, release is the protocol fn to release the memory of any GL related resources (textures, frame buffers, render bufffers). Currently, the only exception is vertex array/attribute buffers. These are not yet wrapped as defrecord and therefore don't yet implement the gl/IRelease protocol. So if you need to delete those, you'll have to use the direct GL/WebGL call (for now).

Thanks a ton for the wonderful help! And equally thanks for the library, wrote a short guide on interactively programming WebGL with Figwheel and Geom and I'm finding it absolutely delightful to have that 0.5s iteration time even when doing 3D graphics.