2011-06-17 52 views
0

我正在做OpenGL for Android的第一步,用於開發增強現實應用程序,並且我在照相機圖像前集成OpenGL形狀的時間很糟糕。該程序應該在XZ平面上繪製一個正方形(假設Z軸指向上方)在相機圖像上,但是我得到了兩個正方形,並且它們比如果我不使用相機時小。當GLSurface是TRANSLUCENT時繪製正方形的問題。 Square得到兩次繪製

這是一段代碼。

在主要活動,我這樣設置的意見和傳感器:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Create an Instance with this Activity 
    glSurface = new GLSurfaceView(this); 
    //Set our own Renderer 
    MyRenderer renderer = new Lesson02(); 
    glSurface.setRenderer(renderer); 
    glSurface.getHolder().setFormat(PixelFormat.TRANSLUCENT); 

    //Set the GLSurface as View to this Activity 
    setContentView(glSurface); 

    mCameraView = new CameraView(this); 
    addContentView(mCameraView, new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 

    List<Sensor> listSensors = mSensorManager 
      .getSensorList(Sensor.TYPE_ACCELEROMETER); 
    if (listSensors.size() > 0) { 
     mSensorManager.registerListener(renderer, 
       listSensors.get(0), SensorManager.SENSOR_DELAY_UI); 
    } 

    listSensors = mSensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD); 
    if (listSensors.size() > 0) { 
     mSensorManager.registerListener(renderer, 
       listSensors.get(0), SensorManager.SENSOR_DELAY_UI); 
    } 
} 

我的渲​​染器的onDraw方法做這樣的事情:

public void onDrawFrame(GL10 gl) { 
    // Get rotation matrix from the sensor 
    SensorManager.getRotationMatrix(rotationMatrix, null, 
            mAccelerometerValues, mMagneticValues); 
    // As the documentation says, we are using the device as a compass in 
    // landscape mode 
    SensorManager.remapCoordinateSystem(rotationMatrix, 
             SensorManager.AXIS_Y, 
             SensorManager.AXIS_MINUS_X, 
             remappedRotationMatrix); 

    //Clear Screen And Depth Buffer 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    // Load remapped matrix 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    //Reset The Current Modelview Matrix 
    gl.glLoadIdentity();  
    gl.glLoadMatrixf(remappedRotationMatrix, 0); 

    // Draw square 
    gl.glTranslatef(-1.2f, 18.0f, 0.0f); 
    //gl.glTranslatef(0.0f, -1.2f, -6.0f); //Move down 1.2 Unit And Into The Screen 6.0 
    square.draw(gl);      //Draw the square 
} 

最後,我inicialize內的一切onSurfaceCreated方法是這樣的:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
    gl.glShadeModel(GL10.GL_SMOOTH);   //Enable Smooth Shading 
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);   //Black Background 
    gl.glClearDepthf(1.0f);     //Depth Buffer Setup 
    gl.glEnable(GL10.GL_DEPTH_TEST);   //Enables Depth Testing 
    gl.glDepthFunc(GL10.GL_LEQUAL);    //The Type Of Depth Testing To Do 

    /* 
    * By default, OpenGL enables features that improve quality but reduce 
    * performance. One might want to tweak that especially on software 
    * renderer. 
    */ 
    gl.glDisable(GL10.GL_DITHER); 

    /* 
    * Some one-time OpenGL initialization can be made here probably based 
    * on features of this particular context 
    */ 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
} 

如果我評論行

glSurface.getHolder().setFormat(PixelFormat.TRANSLUCENT); 

在它的活動範圍內它繪製的方塊很好而且光滑,而且只有一次。但是,當我將它變成半透明或甚至透明時,它會繪製兩個較小的方格,當我旋轉移動時,這些方格不能很好地繪製出來。

如果有人知道發生了什麼,這將非常感激。

謝謝!

回答

0

剛剛發現瞭解決方案,看看演示。在我的主要活動中,我需要設置EGL配置選擇器。像這樣應當寫入主活動的onCreate方法(其中我添加了OpenGL視圖和攝像機視圖):

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //Create an Instance with this Activity 
     glSurface = new GLSurfaceView(this); 
     // We want an 8888 pixel format because that's required for a 
     //translucent window. And we want a depth buffer. 
     glSurface.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // <-- NEW LINE ADDED 
     //Set our own Renderer 
     MyRenderer renderer = new MyRenderer(); 
     glSurface.setRenderer(renderer); 
     glSurface.getHolder().setFormat(PixelFormat.TRANSLUCENT); 

     //Set the GLSurface as View to this Activity 
     setContentView(glSurface); 

     mCameraView = new CameraView(this); 
     addContentView(mCameraView, new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 

       // More code ... 
} 

顯然,該行是必要的具有半透明OpenGL窗口。