我不明白为什么在构建时将此简单着色器设置为导入的网格(.obj网格)时出现错误,我得到的错误是: “在着色器中没有名称'diffuseTexture'的制服”
问题是,我现在不想使用纹理,出于学习目的,我想简单地从一个红色网格开始,然后从那里一步一步地进行..
以下是一些代码:
ObjLoader loader = new ObjLoader();
model = loader.loadObj(Gdx.files.internal("model.obj"),true);
String vertexShader = "" +
"attribute vec4 Position;" +
"uniform mat4 u_projView;" +
"void main() {" +
"gl_Position = u_projView * Position;" +
"}";
String fragmentShader = "void main() {" +
"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);" +
"}";
ShaderProgram shader = new ShaderProgram(vertexShader,fragmentShader);
model.render(shader);
我得到的错误:
Exception in thread "LWJGL Application"
com.badlogic.gdx.utils.GdxRuntimeException:
java.lang.IllegalArgumentException: no uniform with name
'diffuseTexture' in shader at
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.IllegalArgumentException: no uniform with name
'diffuseTexture' in shader at
com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:283)
at
com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformi(ShaderProgram.java:297)
at
com.badlogic.gdx.graphics.g3d.materials.TextureAttribute.bind(TextureAttribute.java:80)
at
com.badlogic.gdx.graphics.g3d.materials.Material.bind(Material.java:70)
at
com.badlogic.gdx.graphics.g3d.model.still.StillModel.render(StillModel.java:57)
答案 0 :(得分:1)
只要查看libGDX的源代码,我就可以假设您的 model.obj 在材质部分中为其指定了纹理。因此GDX使用纹理属性创建材质并尝试设置它。
您可能尝试做的是:
将“sampler2D diffuseTexture”字符串添加到着色器,但请务必以某种方式使用它,否则此属性将从着色器中删除。你可以这样做,例如像这样:
vec4 texval = texture2d(diffuseTexture, vec2(0,0));
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) + texval*0;