简体   繁体   English

将“uniform sampler2D”传递给libgdx中的glsl着色器(使用modelBatch)

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

I have trouble with passing sampler2d uniform from my code to shader. 我将sampler2d制服从我的代码传递到着色器时遇到了麻烦。 I have this line in my shader: 我的着色器中有这一行:

uniform sampler2D u_texture;

In code, I am using g3db models created in blender(with textures): 在代码中,我使用在blender中创建的g3db模型(带纹理):

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渲染:

 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 . 假设你有一个纹理并将其绑定到GL_TEXTURE0 ,那么你会给采样器统一值: 0 Generally you treat samplers as integer uniforms, by the way, so the proper code would probably be: program.setUniformi (sampler2D, 0); 顺便说一下,你通常将采样器视为整数制服,所以正确的代码可能是: 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. 这告诉GLSL采样器将使用纹理单元0,所使用的实际纹理将取决于绑定到该纹理单元的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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