我创建了一个带纹理的 shpere,并在 Android 2.3 上使用 opengl es 正确显示。我想添加一个带有 alpha 的二维 PNG,这样我的 PNG 的透明部分就显示了球体。我可以用我的 PNG 和另一个在 Sqaure 上纹理的 PNG 来实现透明度,但是当我用 Sphere 应用相同的东西时,它会扭曲 Sphere 上的纹理。我怎样才能做到这一点?
这是我使用的代码。
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) height = 1; // To prevent divide by zero
float aspect = (float)width / height;
// Set the viewport (display area) to cover the entire window
gl.glViewport(0, 0, width, height);
// Setup perspective projection, with aspect ratio matches viewport
gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix
gl.glLoadIdentity(); // Reset projection matrix
// Use perspective projection
GLU.gluPerspective(gl, 45, aspect, 0.1f, 100.f);
gl.glMatrixMode(GL10.GL_MODELVIEW); // Select model-view matrix
gl.glLoadIdentity(); // Reset
texs[0] = loadTexture(gl, context, "e.png");
texs[1] = loadTexture(gl, context, "ipo7x.png");
gl.glEnable(GL10.GL_TEXTURE_2D);
}
// Call back to draw the current frame.
public void onDrawFrame(GL10 gl) {
// Clear color and depth buffers using clear-value set earlier
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glLoadIdentity(); // Reset model-view matrix ( NEW )
gl.glTranslatef(0.0f, 0.0f, -6.0f);
gl.glClientActiveTexture(GL10.GL_TEXTURE0); //new
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 8, sphere.getTextureBuffer());
gl.glActiveTexture(GL11.GL_TEXTURE0); //2
gl.glBindTexture(GL11.GL_TEXTURE_2D, texs[0]);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
gl.glClientActiveTexture(GL10.GL_TEXTURE1);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 8, square.getTexBuffer());
gl.glActiveTexture(GL10.GL_TEXTURE1); //2
gl.glBindTexture(GL10.GL_TEXTURE_2D, texs[1]);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_COMBINE);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC0_RGB, GL11.GL_PRIMARY_COLOR);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND0_RGB, GL11.GL_SRC_COLOR);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_PREVIOUS);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_RGB, GL11.GL_TEXTURE);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_RGB, GL11.GL_SRC_COLOR);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_INTERPOLATE);
sphere.draw(gl);
square.draw(gl);
gl.glDisable(GL10.GL_DEPTH_TEST);
}