z-sorting problem

Started by sunghoney, January 23, 2013, 06:52:38 AM

Previous topic - Next topic

sunghoney

obj files created on a Blender.






checked in android ...








The problem occurs when we rotate.




public class TestActivity extends Activity {

   // Used to handle pause and resume...
   private static TestActivity master = null;

   private GLSurfaceView mGLView;
   private MyRenderer renderer = null;
   private FrameBuffer fb = null;
   private World world = null;
   private RGBColor back = new RGBColor(255, 255, 255);

   private float touchTurn = 0;
   private float touchTurnUp = 0;

   private float xpos = -1;
   private float ypos = -1;

   private Object3D object = null;
   private int fps = 0;

   private Light sun = null;

   protected void onCreate(Bundle savedInstanceState) {
      Logger.log("onCreate");
      if( master != null ) {
         copy(master);
      }

      super.onCreate(savedInstanceState);
      mGLView = new GLSurfaceView(getApplication());

      mGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
         public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            // Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
            // back to Pixelflinger on some device (read: Samsung I7500)
            int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
            EGLConfig[] configs = new EGLConfig[1];
            int[] result = new int[1];
            egl.eglChooseConfig(display, attributes, configs, 1, result);
            return configs[0];
         }
      });

      renderer = new MyRenderer();
      mGLView.setRenderer(renderer);
      setContentView(mGLView);
   }

   @Override
   protected void onPause() {
      super.onPause();
      mGLView.onPause();
   }

   @Override
   protected void onResume() {
      super.onResume();
      mGLView.onResume();
   }

   @Override
   protected void onStop() {
      super.onStop();
   }

   private void copy(Object src) {
      try {
         Logger.log("Copying data from master Activity!");
         Field[] fs = src.getClass().getDeclaredFields();
         for( Field f : fs ) {
            f.setAccessible(true);
            f.set(this, f.get(src));
         }
      } catch ( Exception e ) {
         throw new RuntimeException(e);
      }
   }

   public boolean onTouchEvent(MotionEvent me) {

      if( me.getAction() == MotionEvent.ACTION_DOWN ) {
         xpos = me.getX();
         ypos = me.getY();
         return true;
      }

      if( me.getAction() == MotionEvent.ACTION_UP ) {
         xpos = -1;
         ypos = -1;
         touchTurn = 0;
         touchTurnUp = 0;
         return true;
      }

      if( me.getAction() == MotionEvent.ACTION_MOVE ) {
         float xd = me.getX() - xpos;
         float yd = me.getY() - ypos;

         xpos = me.getX();
         ypos = me.getY();

         touchTurn = xd / -100f;
         touchTurnUp = yd / -100f;
         return true;
      }

      try {
         Thread.sleep(15);
      } catch ( Exception e ) {
         // No need for this...
      }

      return super.onTouchEvent(me);
   }

   protected boolean isFullscreenOpaque() {
      return true;
   }

   class MyRenderer implements GLSurfaceView.Renderer {

      private long time = System.currentTimeMillis();

      public MyRenderer() {
      }

      public void onSurfaceChanged(GL10 gl, int w, int h) {
         if( fb != null ) {
            fb.dispose();
         }
         fb = new FrameBuffer(gl, w, h);

         if( master == null ) {
            world = new World();
            world.setAmbientLight(100, 100, 100);

            sun = new Light(world);
            sun.setIntensity(255, 255, 255);
           
            try {
               TextureManager.getInstance().addTexture("test.png", new Texture(getAssets().open("maps/test.png"), true));
            } catch ( IOException e ) {
            }

            object = loadModel("untitled", 3);
            //object.rotateX((float) (-180 * Math.PI / 180));
            object.setCenter(new SimpleVector());
            object.setTransparency(15); //Necessary for transparent textures
            object.build();
            world.addObject(object);

            SimpleVector sv = new SimpleVector();
            sv.set(object.getTransformedCenter());

            Camera cam = world.getCamera();
            cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
            cam.lookAt(sv);

            sv.set(object.getTransformedCenter());
            sv.z -= 10;
            sun.setPosition(sv);
            MemoryHelper.compact();

            if( master == null ) {
               Logger.log("Saving master Activity!");
               master = TestActivity.this;
            }
         }
      }

      private Object3D loadModel(String filename, float scale) {
         AssetManager assetManager = getResources().getAssets();
         Object3D[] model = null;
         try {
            model = Loader.loadOBJ(assetManager.open(filename + ".obj"), assetManager.open(filename + ".mtl"), scale);
         } catch ( IOException e ) {
         }
         return Object3D.mergeAll(model);
      }

      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      }

      public void onDrawFrame(GL10 gl) {
         if( touchTurn != 0 ) {
            object.rotateY(touchTurn);
            //world.getCamera().rotateY(touchTurn);
            touchTurn = 0;
         }

         if( touchTurnUp != 0 ) {
            object.rotateX(touchTurnUp);
            //world.getCamera().rotateX(touchTurnUp);
            touchTurnUp = 0;
         }

         fb.clear(back);
         world.renderScene(fb);
         world.draw(fb);
         fb.display();

         if( System.currentTimeMillis() - time >= 1000 ) {
            Log.i("ROOEX", "fps : " + fps);
            fps = 0;
            time = System.currentTimeMillis();
         }
         fps++;
      }
   }
}




Can anyone advise on this issue?

EgonOlsen

Transparent object will be sorted per object. In this case, all objects are merged into one and so they can't be sorted correctly in all cases. You have two options: Don't merge them into one or don't use transparency on them.