0

拡張現実アプリを開発するために、OpenGL for Androidで最初のステップを実行していますが、カメラ画像の前にOpenGLシェイプを統合するのに苦労しています。プログラムは、カメラ画像上にXZ平面(Z軸が上を向いていると仮定)に正方形を描画することになっていますが、2つの正方形が表示され、カメラを使用しない場合よりも小さくなります。

ここに少しのコードがあります。

メインアクティビティでは、ビューとセンサーを次のように設定します。

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
}

最後に、次のように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);

アクティビティ内で、正方形を1回だけ描​​画します。しかし、半透明または透明にすると、モバイルを回転させたときにうまく描画されない2つの小さな正方形が描画されます。

何が起こっているのか誰かが知っているなら、それは非常にありがたいです。

ありがとう!!

4

1 に答える 1

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ウィンドウを持つために必要です。

于 2011-06-20T07:55:54.467 に答える