我试图在网格上放置纹理,但失败了。我正在尝试渲染这样的东西,可以看到纹理,但我得到了这个,我几乎看不到三角形。我正在运行以下代码:
public class Game implements ApplicationListener{
Mesh mesh;
ShaderProgram shader;
Texture texture;
public static void main(String[] args) {
LwjglApplication app = new LwjglApplication(new Game(), "Mesh Tutorial 1", 800, 600, true);
}
protected static ShaderProgram createMeshShader() {
String vert = Gdx.files.internal("data/VertexShader.txt").readString();
String frag = Gdx.files.internal("data/FragmentShader.txt").readString();
ShaderProgram.pedantic = false;
ShaderProgram shader = new ShaderProgram(vert, frag);
return shader;
}
@Override
public void create() {
if (mesh == null) {
mesh = new Mesh(true, 3, 3,
new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.Color, 4, ShaderProgram.COLOR_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 0.2f, 0.3f, 0.4f, 1f, 0, 1,
0.5f, -0.5f, 0, 0.1f, 0.2f, 0.1f, 1f, 1, 1,
0, 0.5f, 0, 0, 0.4f, 0.5f, 0.5f, 1f, 0 });
mesh.setIndices(new short[] { 0, 1, 2 });
texture = new Texture(Gdx.files.internal("data/badlogic.png"));
}
shader = createMeshShader();
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
texture.bind();
mesh.render(shader, GL20.GL_TRIANGLES);
}
@Override
public void resize(int width, int height) {}
@Override
public void pause() {}
@Override
public void resume() {}
@Override
public void dispose() {}
顶点着色器是:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoords;
uniform mat4 u_projTrans;
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = a_color;
vTexCoord = a_texCoords;
gl_Position = u_projTrans * a_position;
}
片段着色器:
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main(){
vec4 texColor = texture2D(u_texture, v_texCoords);
gl_FragColor = v_color * texColor;
}