Reputation: 113
I have trouble with passing sampler2d uniform from my code to shader. I have this line in my shader:
uniform sampler2D u_texture;
In code, I am using g3db models created in blender(with textures):
AssetManager assets = new AssetManager();
assets.load(data+"/earth.g3db", Model.class);
assets.finishLoading();
Model earthModel = assets.get(data+"/earth.g3db", Model.class);
earthPlanet = new ModelInstance(earthModel,0,-1,0);
I render this using modelBatch:
modelBatch.begin(cam);
modelBatch.render(earthPlanet, shader);
modelBatch.end();
I have shader class in my code, and a render method within:
public void render(Renderable renderable) {
program.setUniformMatrix(u_worldTrans, renderable.worldTransform);
//how to pass texture??
//program.setUniformf(sampler2D, ????);
renderable.mesh.render(program,
renderable.primitiveType,
renderable.meshPartOffset,
renderable.meshPartSize);
}
I would be happy for any response. Thanks!
Upvotes: 2
Views: 3320
Reputation: 43329
Your confusion stems from the fact that you think you pass textures to sampler uniforms. This is not the case.
When you give a value to a sampler uniform, the value you are supposed to supply is the texture unit. Say you had a texture and bound it to GL_TEXTURE0
, then you would give the sampler uniform the value: 0
. Generally you treat samplers as integer uniforms, by the way, so the proper code would probably be: program.setUniformi (sampler2D, 0);
This tells GLSL that the sampler will use texture unit 0, the actual texture used will depend on whatever is bound to that texture unit.
Upvotes: 6