gpt4 book ai didi

java - 网格上的纹理无法正确呈现 Libgdx

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:30:24 27 4
gpt4 key购买 nike

我试图在网格上放置纹理,但失败了。我正在尝试渲染 something like this, where texture can be seen但我得到 this ,在那里我几乎看不到三角形。我正在运行以下代码:

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;
}

最佳答案

如果我错了请纠正我,但是你是不是漏掉了设置制服的部分?

参见以下示例:OpenGlShader

关于java - 网格上的纹理无法正确呈现 Libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com