编辑-- 我在 TenFour04s 回答后更新了我的代码,但仍然只显示黑色。
我已经更新了我的 libgdx,它要求我使用 gl20,这导致我做了一些更改 除了尝试对网格进行纹理处理之外,大部分工作都很好。目前,表面网格显示为黑色,并且根本不显示地面网格。通过一些更改,我可以让它将表面和地面网格显示为黑色。
我尝试过绑定(bind)以及 surTexture.bind 和 grdTexture.bind 的顺序,并使用小于 16 的数字进行渲染,并且我已经使用表面纹理作为除表面和地面之外的所有内容的纹理.
有人能看出我可能哪里出了问题吗?
// creating a mesh with maxVertices set to vertices,size*3
groundMesh = new Mesh(true, vertices.size*3, vertices.size*3,
new VertexAttribute(Usage.Position,2,ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
groundMesh.setVertices(temp);
short[] indices = new short[vertices.size*2];
for(int i=0;i<vertices.size*2;i++){
indices[i] = (short)i;
}
groundMesh.setIndices(indices);
surfaceMesh = new Mesh(true, vertices.size*3, vertices.size*3,
new VertexAttribute(Usage.Position,3,ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
...
grdTexture = new Texture(Gdx.files.internal("data/img/leveltest/ground.png"));
// Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE16);
//says that setWrap and SetFilter bind the texture so I thought I might have to set
//activetexture here but does nothing.
grdTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
grdTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
surTexture = new Texture(Gdx.files.internal("data/img/leveltest/surface.png"));
// Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE17);
surTexture.setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
//TODO change these filters for better quality
surTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
drawWorld 在 render() 内部被调用
public void drawWorld(SpriteBatch batch,OrthographicCamera camera) {
batch.begin();
batch.setProjectionMatrix(camera.combined);
layers.drawLayers(batch);
if ((spatials != null) && (spatials.size > 0)){
for (int i = 0; i < spatials.size; i++){
spatials.get(i).render(batch);
}
}
batch.end();
drawGround(camera);
}
private void drawGround(OrthographicCamera camera){
shader.begin();
shader.setUniformMatrix("u_projTrans", camera.combined);
grdTexture.bind(0);
shader.setUniformi("u_texture", 0);
//changed GL_TRIANGLES to GL_TRIANGLE_STRIP to render meshes correctly after changing to GL20
groundMesh.render(shader, GL20.GL_TRIANGLE_STRIP);
surTexture.bind(0);
shader.setUniformi("u_texture", 0);
surfaceMesh.render(shader, GL20.GL_TRIANGLE_STRIP);
shader.end();
}
片段.glsl
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main()
{
gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
}
顶点.glsl
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_color.a = v_color.a * (256.0/255.0);
v_texCoords = a_texCoord;
gl_Position = u_projTrans * a_position;
}
最佳答案
在顶点着色器中,您使用的是 a_texCoord
,但在网格构造函数中,您已有效地将属性命名为 a_texCoord16
和 a_texCoord17
使用 ShaderProgram.TEXCOORD_ATTRIBUTE+"16"
和 ShaderProgram.TEXCOORD_ATTRIBUTE+"17"
。
由于您不是多重纹理,所以我只需将它们替换为“a_texCoord”
。
看起来您可能将属性名称后缀与纹理单元混为一谈,尽管这两个概念不一定相关。您可能想要向 texCoords 添加数字后缀的原因是,如果您的网格体的每个顶点都有多个 UV,因为它是多重纹理的。但实际上您可以使用您喜欢的任何命名方案。您可能想要绑定(bind)到 0 以外的单位的原因是,如果您在单个网格上进行多重纹理,则需要一次绑定(bind)多个纹理。因此,如果您实际上是多重纹理,那么当您尝试将 UV 与片段着色器中的纹理进行匹配时,使用与纹理单元编号匹配的属性后缀可能有助于避免混淆。
关于java - 网格上的纹理不渲染,只显示黑色 libgdx gl20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25745049/