【问题标题】:How can I use Multiple GLSurfaceView components in the same Layout?如何在同一个布局中使用多个 GLSurfaceView 组件?
【发布时间】:2011-06-26 20:09:52
【问题描述】:

我正在为 Android 编写信息可视化 API,但在尝试将自定义 GLSurfaceView 的两个单元放入布局时遇到了问题。此时的自定义GLSurfaceView只是GLSurfaceView的扩展,以消除自定义方法可能导致的故障。

当我在布局中添加了两个组件并启动它运行的应用程序时。但是什么也没画,好像进入了一个无限循环。因为渲染器中的调试消息会打印到 LogCat 中。但是,如果我只使用自定义 GLSurfaceView 组件之一,它就可以正常工作。

我读到在多个活动中使用GLSurfaceView 存在问题,我想它也适用于同时使用其中两个组件时。我已尝试发布here 的解决方法,但似乎也无法正常工作。

如果有任何帮助,我将不胜感激。我选择使用 openGL 以获得更好的性能,但如果我不能一次使用多个组件,我想我将不得不使用 Canvas。

清单如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="@string/hello" android:id="@+id/TextView01"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <com.syntronic.vtadlib.VisualizationView android:id="@+id/glview"
        android:layout_width="fill_parent" android:layout_height="300px" />


    <TextView android:text="@string/hello" android:id="@+id/TextView02"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <LinearLayout 
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <com.syntronic.vtadlib.VisualizationView android:id="@+id/glview2"
            android:layout_width="fill_parent" android:layout_height="fill_parent" />

    </LinearLayout>

</LinearLayout>

从Activity看代码是这样的:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mSurfaceView = (VisualizationView) findViewById(R.id.glview);
    mSurfaceView2 = (VisualizationView) findViewById(R.id.glview2);

    //Enables debug flags for Errors
    //mSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);   
    //mSurfaceView2.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);  

    mSurfaceView.setRenderer(new CoordinateSystemRenderer());
    mSurfaceView2.setRenderer(new CoordinateSystemRenderer());

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    mSurfaceView.onPause();
    mSurfaceView2.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mSurfaceView.onResume();
    mSurfaceView2.onResume();
}

我是否遗漏了一些明显的东西?或者有人可以解释为什么它不起作用?

【问题讨论】:

  • 首先让我跳出来的是你嵌套的LinearLayout设置为fill_parent。我会检查以确保您的两个 GLSurfaceViews 都以非零高度布局。可能是它们都在绘图,但其中一个的高度为 0 像素,因此是不可见的。
  • 而不是两个GLSurfaceViews,最好自己创建/管理一个GL线程和上下文,将其应用于两个SurfaceViews(不是GLSurfaceViews)。见fadden's discussion and link to Grafika

标签: android android-layout opengl-es glsurfaceview


【解决方案1】:

[更新:从 Android 5.0 (Lollipop) 开始,此答案不再正确。有关讨论和链接,请参见 fadden's answerIt was also incorrect as of Android 2.0, and apparently was only an issue for OVERLAPPING surfaces even before then.]

您不能将 2 个 SurfaceViews(SV) 放入一个 Activity。 了解为什么您应该了解 SV 的工作原理。

当您创建它并将其放置在活动上时,它实际上不会被放置在活动中(或活动顶部),而是会在当前活动的后面创建,并在该活动中创建“透明”视图。

在 Android 4.0 (API 14) 中有一个名为 TextureView 的新视图 没有办法在旧平台上创建类似的视图。

【讨论】:

  • 你确定吗?我只认为限制是,您不能将它们放在彼此之上或重叠。据说你不能在表面视图上显示视频视图,因为视频视图扩展了表面视图......
  • 是的,我确定。您一次只能使用一个 SV 实例。
  • 如果你有资源可以提供吗?我对完整的 SV 主题非常感兴趣。谢谢!
  • 这个答案不再正确。见fadden's answer
【解决方案2】:

这是另一种方法。在此处从 Android 文档下载示例:http://developer.android.com/shareables/training/OpenGLES.zip 在这个 zip 文件中,您将看到 2 个项目。打开项目:HelloOpenGLES20 并将“MyGLRenderer”类替换为下面列出的我的类并运行项目。

package com.example.android.opengl;

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

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;

public class MyGLRenderer implements GLSurfaceView.Renderer {

    private static final String TAG = "MyGLRenderer";
    private Triangle[] mTriangle = new Triangle[2];
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];
    private final float[] mRotationMatrix = new float[16];

    private float mAngle;

    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {

        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        mTriangle[0] = new Triangle();
        mTriangle[1] = new Triangle();

    }

    @Override
    public void onDrawFrame(GL10 unused) {

        final float[] scratch = new float[16];

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

        Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

        for(int i = 0; i < 2; i++) {

            if(i % 2 == 0) {

                Matrix.setRotateM(mRotationMatrix, 0, mAngle / 2f, 0, 0, 1.0f);

            }
            else {

                Matrix.setRotateM(mRotationMatrix, 0, mAngle / 4f, 0, 0, 1.0f);

            }

            Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

            mTriangle[i].draw(scratch);

        }//End for(int i = 0; i < 2; i++)

    }//End public void onDrawFrame(GL10 unused)

    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height) {

        GLES20.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

    }

    public static int loadShader(int type, String shaderCode){

        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;

    }

    public static void checkGlError(String glOperation) {

        int error;

        while((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {

            Log.e(TAG, glOperation + ": glError " + error);
            throw new RuntimeException(glOperation + ": glError " + error);

        }

    }

    public float getAngle() {
        return mAngle;
    }


    public void setAngle(float angle) {
        mAngle = angle;
    }

}

据我了解,OpenGLES 旨在仅使用一个视图,但可能具有多个渲染目标。虽然我必须管理,但我不确定您尝试做的事情是否错误。我自己对 OpenGLES 有点陌生。我在 bitbucket 中有一个 OpenGL 开源库。你或许可以从中得到一些想法:https://bitbucket.org/warwick/hacergestov2,它是一个手势库。

【讨论】:

  • 请解释一下——这是如何做到的“另一种方法”? OP 讨论的“它”是将 GL 渲染到布局中的两个不同视图。您提出的替代解决方案是什么?
【解决方案3】:

您可以在一个 Activity 中激活多个 GLSurfaceViews 并使其可见。每个视图都有自己的 GL 上下文。

【讨论】:

  • OP 尝试使用两个 GLSurfaceViews,但无法正常工作。这个“答案”有什么帮助?
【解决方案4】:

CoordinateSystemRenderer 的实现是什么?

我今天遇到了同样的要求并尝试了,它确实有效,这意味着你可以将 2 个GLSurfaceView 放入同一个活动中。

有一点需要注意,

  1. GLRender 中,当onSurfaceChanged 被调用时,您必须调整视口的大小
  2. 使用 2 GLSurfaceView,渲染线程将为 2,因此会出现同步问题。这取决于您对onDrawFrame 的实现。

SDKGLSurfaceViewActivity中有一个使用Android API demo的快速测试

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.graphics;

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

import android.opengl.GLSurfaceView;

/**
 * Render a pair of tumbling cubes.
 */

public class CubeRenderer implements GLSurfaceView.Renderer
{
    boolean isReverse = false;

    public CubeRenderer(boolean useTranslucentBackground, boolean isReverse)
    {
        mTranslucentBackground = useTranslucentBackground;
        mCube = new Cube();
        this.isReverse = isReverse;
    }

    public CubeRenderer(boolean useTranslucentBackground)
    {
        this(useTranslucentBackground, false);
    }

    public void onDrawFrame(GL10 gl)
    {
        /*
         * Usually, the first thing one might want to do is to clear the screen. The most efficient way of doing this is
         * to use glClear().
         */

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        /*
         * Now we're ready to draw some 3D objects
         */

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -3.0f);
        gl.glRotatef(mAngle, 0, 1, 0);
        gl.glRotatef(mAngle * 0.25f, 1, 0, 0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        mCube.draw(gl);

        gl.glRotatef(mAngle * 2.0f, 0, 1, 1);
        gl.glTranslatef(0.5f, 0.5f, 0.5f);

        mCube.draw(gl);

        if (isReverse)
        {
            mAngle -= 1.2f;
        }
        else
        {
            mAngle += 1.2f;
        }
    }

    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        System.out.println("Joey's Log width : " + width + " height : " + height);
        gl.glViewport(0, 0, width, height);

        /*
         * Set our projection matrix. This doesn't have to be done each time we draw, but usually a new projection needs
         * to be set when the viewport is resized.
         */

        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        /*
         * 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);

        if (mTranslucentBackground)
        {
            gl.glClearColor(0, 0, 0, 0);
        }
        else
        {
            gl.glClearColor(1, 1, 1, 1);
        }
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
    }

    private boolean mTranslucentBackground;

    private Cube mCube;

    private float mAngle;
}


------------------------------------------------------------------------------------------
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.graphics;

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

import android.opengl.GLSurfaceView;

/**
 * Render a pair of tumbling cubes.
 */

public class CubeRenderer implements GLSurfaceView.Renderer {
    boolean isReverse = false;
    public CubeRenderer(boolean useTranslucentBackground,boolean isReverse) {
        mTranslucentBackground = useTranslucentBackground;
        mCube = new Cube();
        this.isReverse = isReverse;
    }

    public CubeRenderer(boolean useTranslucentBackground)
    {
        this(useTranslucentBackground, false);
    }

    public void onDrawFrame(GL10 gl) {
        /*
         * Usually, the first thing one might want to do is to clear
         * the screen. The most efficient way of doing this is to use
         * glClear().
         */

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        /*
         * Now we're ready to draw some 3D objects
         */

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -3.0f);
        gl.glRotatef(mAngle,        0, 1, 0);
        gl.glRotatef(mAngle*0.25f,  1, 0, 0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        mCube.draw(gl);

        gl.glRotatef(mAngle*2.0f, 0, 1, 1);
        gl.glTranslatef(0.5f, 0.5f, 0.5f);

        mCube.draw(gl);

        if (isReverse)
        {
            mAngle -= 1.2f;
        }
        else
        {
            mAngle += 1.2f;
        }
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        System.out.println("Joey's Log width : " + width + " height : " + height);
         gl.glViewport(0, 0, width, height);

         /*
          * Set our projection matrix. This doesn't have to be done
          * each time we draw, but usually a new projection needs to
          * be set when the viewport is resized.
          */

         float ratio = (float) width / height;
         gl.glMatrixMode(GL10.GL_PROJECTION);
         gl.glLoadIdentity();
         gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        /*
         * 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);

         if (mTranslucentBackground) {
             gl.glClearColor(0,0,0,0);
         } else {
             gl.glClearColor(1,1,1,1);
         }
         gl.glEnable(GL10.GL_CULL_FACE);
         gl.glShadeModel(GL10.GL_SMOOTH);
         gl.glEnable(GL10.GL_DEPTH_TEST);
    }
    private boolean mTranslucentBackground;
    private Cube mCube;
    private float mAngle;
}

【讨论】:

  • 这段代码有什么关系?它没有演示两个 GLSurfaceView。它只显示一个视图,绘制两个立方体。
【解决方案5】:

您可能希望使用全屏 GLSurfaceView 调查在屏幕的“正确”区域中覆盖/隐藏模型。您可能想要组合某种布局框架以使其更简单,或者可能在全屏 GLSurfaceView 上使用多个视口。尚未在 OpenGL ES 中尝试过这些方法,但通常这些方法中的任何一种都将用于在桌面系统上的单个应用程序中渲染相同甚至许多不同模型的多个视图,而不是使用多个 GLContexts(如果这是在这里幕后进行)。

【讨论】:

    【解决方案6】:

    GLSurfaceView 背后有很多事情要做,包括管理 GLContext,我很确定你无法让它工作,即使你成功了,你以后可能会遇到更多意想不到的问题。所以我真的认为这不是正确的应用程序架构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多