Why setTransparency would affect all objects?

Started by Eric, December 13, 2010, 07:40:18 PM

Previous topic - Next topic

Eric

Hi, I found a strange problem. That there's an object with texture contains alpha channel and appeares translucent, if I call setTransparency to assign any value to any object in the world, the texture will not be translucent any more.

EgonOlsen

I can't verify this. I'm doing this all the time in Alien Runner without a problem. Do you have a simple test case for this problem?

censivn

#2
I have the same problem    when call setTransparency to "testModel "

"texture1" contains alpha

CODE:


package com.censvin.TestDemo;

import java.lang.reflect.Field;

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


import android.app.Activity;
import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Light;
import com.threed.jpct.Loader;
import com.threed.jpct.Logger;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;

import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.MemoryHelper;

/**
* A simple demo. Slightly more advanced than the HelloWorld example, but still
* pretty basic.
*
* @author EgonOlsen
*
*/
public class Demo extends Activity {

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

   private GLSurfaceView mGLView;
   private MyRenderer renderer = null;
   private FrameBuffer fb = null;
   private World world = null;

   private RGBColor back = new RGBColor(50, 50, 100);


   private Object3D plane1 = null;
   private Object3D plane2 = null;
   private Object3D testModel = null;


   protected void onCreate(Bundle savedInstanceState) {
      Logger.log("onCreate");

      if (master != null) {
         
      }

      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);
   }

   

   protected void onStop() {
      Logger.log("onStop");
      super.onStop();
   }


   class MyRenderer implements GLSurfaceView.Renderer {

      private int fps = 0;
      private int lfps = 0;

      private long time = System.currentTimeMillis();

      private boolean stop = false;

      private SimpleVector sunRot = new SimpleVector(0, 0.05f, 0);

      public MyRenderer() {
         Config.maxPolysVisible = 500;
         Config.farPlane = 1500;
         Config.glTransparencyMul = 0.1f;
         Config.glTransparencyOffset = 0.1f;
         Config.useVBO=true;
         
         Texture.defaultToMipmapping(true);
         Texture.defaultTo4bpp(true);
      }

      public void stop() {
         stop = true;
      }

      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();
            Resources res = getResources();

            TextureManager tm = TextureManager.getInstance();

            Texture texture1 = new Texture(res.openRawResource(R.raw.l_32));
            
            Texture texture2 = new Texture(res.openRawResource(R.raw.l_31));

            tm.addTexture("texture1", texture1);
            tm.addTexture("texture2", texture2);
            
            plane1 =  Primitives.getPlane(1, 10);
            plane2 =  Primitives.getPlane(1, 10);
            testModel = Primitives.getPlane(1, 10);
            
            plane1.setTexture("texture1");
            
            plane2.setTexture("texture1");
            
            testModel.setTexture("texture2");

            plane2.translate(-5, -5, -10);
            
            testModel.translate(5, 5, 5);
            
   

            world.addObject(plane1);
            world.addObject(plane2);
            world.addObject(testModel);

            testModel.setTransparency(5);


            world.setAmbientLight(255, 255, 255);
            world.buildAllObjects();

            
            Camera cam = world.getCamera();
            cam.moveCamera(Camera.CAMERA_MOVEOUT, 30);
            cam.lookAt(plane1.getTransformedCenter());

         }
      }

      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
         
         gl.glEnable(GL10.GL_BLEND);
         
         gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
      }

      public void onDrawFrame(GL10 gl) {

         try {
            if (!stop) {
               

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

               fb.display();

            
            } else {
               if (fb != null) {
                  fb.dispose();
                  fb = null;
               }
            }
         } catch (Exception e) {
            e.printStackTrace();
            Logger.log("Drawing thread terminated!", Logger.MESSAGE);
         }
      }

      
   }

}

EgonOlsen

If that test case (thanks for that btw...such things make life a whole lot easier) describes the problem, then there is none. When using alpha in a texture, two things have to be considered:


  • Alpha won't be used until you say so. That means that when loading the texture, you have to use the appropriate constructor, i.e. that one with the boolean parameter (set to true). Otherwise, jPCT will calculate it's own alpha channel based on black areas in the texture.
  • To make the alpha channel have an effect, you have to enable transparency on the object that uses the alpha channeled texture. Otherwise, it will be rendered opaque ignoring alpha.

Hope that helps...

censivn

plane1.setTransparency(100);
plane2.setTransparency(100);

The problem has been solved !thank you very much!