简体   繁体   中英

Pass “uniform sampler2D” to glsl shader in libgdx(using modelBatch)

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM