Android OpenGL Transparency наслагване

Създавам приложение за Android с OpenGL. Създадох 2 квадрата, всеки със собствени текстури (PNG), и ги насложих. От подсказки, които получих от предишен въпрос, използвах gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

Проблемът ми е, че ефектът на прозрачност засяга втория квадрат, поради което мога да видя фона през текстурата на втория квадрат. Има ли начин за това?

Ето Renderer и в долната част Square.java class:

package hello.project;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class Square {

 private FloatBuffer vertexBuffer; // buffer holding the vertices
 static int sex=R.drawable.girl;

 private FloatBuffer textureBuffer; // buffer holding the texture coordinates
 private float texture[] = {
   // Mapping coordinates for the vertices
   0.0f, 1.0f,  // top left  (V2)
   0.0f, 0.0f,  // bottom left (V1)
   1.0f, 1.0f,  // top right (V4)
   1.0f, 0.0f  // bottom right (V3)
 };

 private float vertices[] = {
   -1.0f, -2.0f,  0.0f,  // V1 - bottom left
   -1.0f,  2.0f,  0.0f,  // V2 - top left
    0.8f, -2.0f,  0.0f,  // V3 - bottom right
    0.8f,  2.0f,  0.0f   // V4 - top right
 };

 public Square() {
  ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
  byteBuffer.order(ByteOrder.nativeOrder());
  vertexBuffer = byteBuffer.asFloatBuffer();
  vertexBuffer.put(vertices);
  vertexBuffer.position(0);

  byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
  byteBuffer.order(ByteOrder.nativeOrder());
  textureBuffer = byteBuffer.asFloatBuffer();
  textureBuffer.put(texture);
  textureBuffer.position(0);
 }

 /** The draw method for the square with the GL context */
 public void draw(GL10 gl) {
  // bind the previously generated texture
  gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

  // Point to our buffers
  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);



  // Set the face rotation
  gl.glFrontFace(GL10.GL_CW);

  // Point to our vertex buffer
  gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

  // Draw the vertices as triangle strip
  gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

  //Disable the client state before leaving
  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
 }




 /** The texture pointer */
 private int[] textures = new int[1];

 public void loadGLTexture(GL10 gl, Context context,int sex ) {
  // loading texture
  Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
    sex);

  // generate one texture pointer
  gl.glGenTextures(1, textures, 0);
  // ...and bind it to our array
  gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

  // create nearest filtered texture
  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

  // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
  GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
  // Clean up
  bitmap.recycle();
 }

 public static int getSex() {
  return sex;
 }

 public static void setSex(int sex) {
  Square.sex = sex;
 }
}
---------------------------------------------------------------------------
package hello.project;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class HelloOpenGLES10Renderer implements Renderer {

 private Square   square;  // the square
 private Square2  square2;  // the square
 private Context     context;


 /** Constructor to set the handed over context */
 public HelloOpenGLES10Renderer(Context context) {
  this.square  = new Square();
  this.square2 = new Square2();
  this.context=context;
 }

 public void onDrawFrame(GL10 gl) {
  // clear Screen and Depth Buffer
  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

  // Reset the Modelview Matrix
  gl.glLoadIdentity();

  // Drawing
  gl.glTranslatef(0.0f, 0.0f, -5.0f);  // move 5 units INTO the screen
  square.draw(gl);
  square2.draw(gl); 


 }


 public void onSurfaceChanged(GL10 gl, int width, int height) {
  if(height == 0) {       //Prevent A Divide By Zero By
   height = 1;       //Making Height Equal One
  }

  gl.glViewport(0, 0, width, height);  //Reset The Current Viewport
  gl.glMatrixMode(GL10.GL_PROJECTION);  //Select The Projection Matrix
  gl.glLoadIdentity();      //Reset The Projection Matrix

  //Calculate The Aspect Ratio Of The Window
  GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);

  gl.glMatrixMode(GL10.GL_MODELVIEW);  //Select The Modelview Matrix
  gl.glLoadIdentity();      //Reset The Modelview Matrix
 }


 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  // Load the texture for the square

  square.loadGLTexture(gl, this.context,Square.getSex());
  square2.loadGLTexture(gl, this.context,Square2.getSex());

  gl.glEnable(GL10.GL_TEXTURE_2D); 
  gl.glDisable(GL10.GL_DEPTH_TEST);
  gl.glEnable(GL10.GL_BLEND);

  gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

  gl.glShadeModel(GL10.GL_SMOOTH);    //Enable Smooth Shading
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  //Black Background
  gl.glClearDepthf(1.0f);      //Depth Buffer Setup
  gl.glDepthFunc(GL10.GL_NEVER);    //The Type Of Depth Testing To Do

  //Really Nice Perspective Calculations
  gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
 }
}

person Dorin Rusu    schedule 28.06.2012    source източник
comment

Мога само да препоръчам да стартирате hadoop във виртуална машина на linux или собствения linux. Въпреки че успешно стартирах hadoop 0.20.0 на windows xp+cygwin и windows7+cygwin, веднъж се опитах да настроя по-нова версия на hadoop на windows7, но се провалих ужасно поради грешки в hadoop. IIRC hadoop с корекцията за сигурност дори няма да работи на windows7 поради проблеми с разрешенията за файлове и т.н. Така че моят съвет: стартирайте hadoop на linux, ако можете, ще избегнете сериозни проблеми.

  -  person Chris911    schedule 28.06.2012
comment
Моля, свържете изображение на проблема, това ще ни помогне да разберем какво се опитвате да направите.   -  person Tim    schedule 28.06.2012
comment
Аз ли съм единственият, който се опита да разбере как той използва променливата си „пол“ само защото това е смешно?   -  person Léon Pelletier    schedule 02.07.2015
comment
@Leon Колкото и странно да изглежда, не беше умишлено или се опитваше да бъде смешно. Сексът всъщност е начинът, по който се казва джендър на румънски.   -  person Dorin Rusu    schedule 02.07.2015
comment
О, и на френски. Просто тръгнах във всички посоки, без да мисля за пола. Благодаря! Вече разбирам още по-добре фрагмента.   -  person Léon Pelletier    schedule 03.07.2015


Отговори (1)


Нямате ли проблем само със състоянието на графичния конвейер? Запомнете кой квадрат, който кажете да бъде начертан първи, се изчертава с помощта на функцията за смесване, която е активна в момента (и тя ще остане активна, докато не промените състоянието на OpenGL). Може би искате да добавите още промени в състоянието към функцията за смесване или да промените реда на рисуване, за да получите ефекта, който искате? Може също така да опитате да активирате/деактивирате теста за дълбочина между извикванията на чертане, за да направите квадрат непрозрачен.

Надяваме се това да помогне малко, може да се наложи да предоставите малко повече подробности на въпроса си.

person James Bedford    schedule 28.06.2012
comment
Редактирах публикацията с част от кода! Благодаря за отговора! - person Dorin Rusu; 28.06.2012