0

我正在尝试创建 3D 场景。有带云的纹理地球。我认为我可以通过透明球体和云的纹理来创建云(如果我错了,请纠正我)。所以我在下面有一些代码:

ShaderProgram shader;
Mesh mesh, cloudMesh;
Texture texture, cloudTexture;
Matrix4 matrix = new Matrix4();

 public void create() {
String vertexShader = "attribute vec4 a_position;    \n"
    + "attribute vec4 a_color;\n" 
    + "attribute vec2 a_texCoords;\n"
    + "uniform mat4 u_worldView;\n" 
    + "varying vec4 v_color;"
    + "varying vec2 v_texCoords;"
    + "void main()                  \n"
    + "{                            \n"
    + "   v_color = vec4(1, 1, 1, 1); \n"
    + "   v_texCoords = a_texCoords; \n"
    + "   gl_Position =  u_worldView * a_position;  \n"
    + "}                            \n";
String fragmentShader = "#ifdef GL_ES\n"
    + "precision mediump float;\n"
    + "#endif\n"
    + "varying vec4 v_color;\n"
    + "varying vec2 v_texCoords;\n"
    + "uniform sampler2D u_texture;\n"
    + "void main()                                  \n"
    + "{                                            \n"      
     + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n"
    + "}";

shader = new ShaderProgram(vertexShader, fragmentShader);
if (shader.isCompiled() == false) {     
  System.exit(0);
}

mesh = ObjLoader.loadObj(Gdx.files.internal("objects/earth.obj").read()); 
cloudMesh = ObjLoader.loadObj(Gdx.files.internal("objects/cloud_sphere.obj").read()); 
texture = new Texture(Gdx.files.internal("images/earthmap.jpg"));
cloudTexture = new Texture(Gdx.files.internal("images/earth_clouds_map.jpg"));
}

public void render() {
angle += Gdx.graphics.getDeltaTime() * 30;
matrix.setToRotation(axis, angle);

Gdx.graphics.getGL20().glViewport(-10, 10, Gdx.graphics.getWidth(),
    Gdx.graphics.getHeight());

Gdx.graphics.getGL20().glClearColor(0.2f, 0.2f, 0.2f, 1);
Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.graphics.getGL20().glEnable(GL20.GL_TEXTURE_2D);
Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND);
Gdx.graphics.getGL20().glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
texture.bind();
shader.begin();
shader.setUniformMatrix("u_worldView", matrix);
shader.setUniformi("u_texture", 0);
mesh.render(shader, GL20.GL_TRIANGLE_STRIP);
shader.end();    
//SHOULD I USE SHADER PROGRAM LIKE BELOW?
cloudTexture .bind();
shader.begin();
shader.setUniformMatrix("u_worldView", matrix);
shader.setUniformi("u_texture", 0);
cloudMesh.render(shader, GL20.GL_TRIANGLE_STRIP);
shader.end();  
}    

所以我在球体上看不到任何纹理。错在哪里?如果有人有任何想法、链接或示例,将不胜感激!

4

1 回答 1

3

在加载纹理之前,您必须定义一个“槽”来存储纹理:

Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE0);
texture = new Texture(Gdx.files.internal("images/earthmap.jpg"));
texture.bind();

Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE1);
cloudTexture = new Texture(Gdx.files.internal("images/earth_clouds_map.jpg"));
cloudTexture.bind();

然后在您的渲染方法中绑定纹理并使用这些“插槽”将其分配给着色器

shader.setUniformi("u_texture", 0);
...
...
shader.setUniformi("u_texture", 1);

这应该是所有...

于 2012-12-28T16:38:46.433 回答