在3D对象Android Opengl上使用Alpha的PNG

时间:2012-10-13 11:42:39

标签: android opengl-es

我创建了一个带纹理的shpere,并在Android 2.3上使用opengl es正确显示。我想添加一个带有alpha的2-D 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);
}

0 个答案:

没有答案