我正在尝试创建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();
}
所以我在球体上看不到任何纹理。哪里出错?如果有人有任何想法,链接或示例,将不胜感激!
答案 0 :(得分: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);
应该全部......