java - OpenGL ES - 主线程上的工作太多

标签 java android multithreading opengl-es

我正在尝试渲染每个面(骰子)上具有不同纹理的 3D 立方体,当我仅使用相同图像之一渲染立方体时,它可以完美地工作,但立方体甚至不显示,并且以下内容当我尝试执行前者时,会在 Android 监视器中抛出:

跳过了 36 帧!应用程序可能在其主线程上做了太多工作。

经过一些研究,我知道我可以在自己的线程中运行这个“繁重的处理”,但我不知道如何去做。我对 OpenGL 渲染还比较陌生,所以我也不知道我的代码是否 super 优化。

我想我应该在另一个线程中运行 Dice3D.java 吗?这是唯一一个不扩展或实现任何东西的类,通过一些研究我发现该类需要实现可运行的?

任何帮助完成这项工作都会很棒,提前致谢!

这是我的类(class):

OpenGLRenderer.java

public class OpenGLRenderer implements GLSurfaceView.Renderer {

    private Context context;
    private Dice3D dice3D;

    public OpenGLRenderer(Context context, FrameLayout openGLLayout) {
        this.context = context;
        dice3D = new Dice3D(context);

        GLSurfaceView glView = new GLSurfaceView(context);
        glView.setRenderer(this);

        //put to welcome layout
        openGLLayout.addView(glView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,   FrameLayout.LayoutParams.MATCH_PARENT));
    }

    private float mCubeRotation;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  // Set color's clear-value to black
        gl.glClearDepthf(1.0f);            // Set depth's clear-value to farthest
        gl.glEnable(GL10.GL_DEPTH_TEST);   // Enables depth-buffer for hidden surface removal
        gl.glDepthFunc(GL10.GL_LEQUAL);    // The type of depth testing to do
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // nice perspective view
        gl.glShadeModel(GL10.GL_SMOOTH);   // Enable smooth shading of color
        gl.glDisable(GL10.GL_DITHER);      // Disable dithering for better performance

        // Setup Texture, each time the surface is created (NEW)
        dice3D.loadTexture(gl);             // Load images into textures (NEW)
        gl.glEnable(GL10.GL_TEXTURE_2D);  // Enable texture (NEW)
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -6.0f);
        gl.glRotatef(mCubeRotation, 1.0f, 1.0f, 1.0f);
        dice3D.draw(gl);
        mCubeRotation -= 0.15f;
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
        gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
}

Dice3D.java

public class Dice3D {

    private FloatBuffer vertexBuffer; // Buffer for vertex-array
    private FloatBuffer texBuffer;    // Buffer for texture-coords-array (NEW)

    private int numFaces = 6;
    private int[] imageFileIDs = {  // Image file IDs
            R.drawable.one,
            R.drawable.two,
            R.drawable.three,
            R.drawable.four,
            R.drawable.five,
            R.drawable.six
    };
    private int[] textureIDs = new int[numFaces];
    private Bitmap[] bitmap = new Bitmap[numFaces];

    // Constructor - Set up the buffers
    public Dice3D(Context context) {
        // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(12 * 4 * numFaces);
        vbb.order(ByteOrder.nativeOrder()); // Use native byte order
        vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float

        for (int face = 0; face < numFaces; face++) {
            bitmap[face] = BitmapFactory.decodeStream(context.getResources().openRawResource(imageFileIDs[face]));

            float[] vertices = { // Vertices for a face
                    -1.0f, -1.0f, 0.0f,  // 0. left-bottom-front
                    1.0f, -1.0f, 0.0f,  // 1. right-bottom-front
                    -1.0f,  1.0f, 0.0f,  // 2. left-top-front
                    1.0f,  1.0f, 0.0f   // 3. right-top-front
            };
            vertexBuffer.put(vertices);  // Populate
        }
        vertexBuffer.position(0);    // Rewind

        float[] texCoords = { // Texture coords for the above face (NEW)
                0.0f, 1.0f,  // A. left-bottom (NEW)
                1.0f, 1.0f,  // B. right-bottom (NEW)
                0.0f, 0.0f,  // C. left-top (NEW)
                1.0f, 0.0f   // D. right-top (NEW)
        };

        // Setup texture-coords-array buffer, in float. An float has 4 bytes (NEW)
        ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4 * numFaces);
        tbb.order(ByteOrder.nativeOrder());
        texBuffer = tbb.asFloatBuffer();
        for (int face = 0; face < numFaces; face++) {
            texBuffer.put(texCoords);
        }
        texBuffer.position(0);
    }

    // Draw the shape
    public void draw(GL10 gl) {
        gl.glFrontFace(GL10.GL_CCW);    // Front face in counter-clockwise orientation
        gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
        gl.glCullFace(GL10.GL_BACK);    // Cull the back face (don't display)

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  // Enable texture-coords-array (NEW)
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer); // Define texture-coords buffer (NEW)

        // front
        gl.glPushMatrix();
        gl.glTranslatef(0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glPopMatrix();

        // left
        gl.glPushMatrix();
        gl.glRotatef(270.0f, 0.0f, 1.0f, 0.0f);
        gl.glTranslatef(0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glPopMatrix();

        // back
        gl.glPushMatrix();
        gl.glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
        gl.glTranslatef(0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glPopMatrix();

        // right
        gl.glPushMatrix();
        gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
        gl.glTranslatef(0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glPopMatrix();

        // top
        gl.glPushMatrix();
        gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
        gl.glTranslatef(0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glPopMatrix();

        // bottom
        gl.glPushMatrix();
        gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
        gl.glTranslatef(0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glPopMatrix();

        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  // Disable texture-coords-array
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisable(GL10.GL_CULL_FACE);
    }

    // Load an image into GL texture
    public void loadTexture(GL10 gl) {
        gl.glGenTextures(6, textureIDs, 0); // Generate texture-ID array for 6 IDs

        // Generate OpenGL texture images
        for (int face = 0; face < numFaces; face++) {
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[face]);
            // Build Texture from loaded bitmap for the currently-bind texture ID
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[face], 0);
            bitmap[face].recycle();
        }
    }

}

最后是我的 Activity Welcome.java

public class Welcome extends AppCompatActivity {

    LinearLayout l1,l2;
    public Button btnsub;
    Animation uptodown,downtoup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        FrameLayout openGLLayout = (FrameLayout) findViewById(R.id.frameLayout1);

        //creates an openGL surface and renders it to the framelayout in the activity layout
        OpenGLRenderer gl3DView = new OpenGLRenderer(this, openGLLayout);


        btnsub = (Button)findViewById(R.id.buttonsub);
        btnsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent test = new Intent(Welcome.this,DiceGame.class);
                startActivity(test);
            }
        });
        l1 = (LinearLayout) findViewById(R.id.l1);
        l2 = (LinearLayout) findViewById(R.id.l2);
        uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
        downtoup = AnimationUtils.loadAnimation(this,R.anim.downtoup);
        l1.setAnimation(uptodown);
        l2.setAnimation(downtoup);

    }
}

最佳答案

在性能方面:

1) 学习OpenGL ES 2.0并使用着色器;它是一个更干净的 API,应用程序和/或驱动程序堆栈中所需的困惑要少得多。所有新 API 都是基于着色器的,因此这是值得学习的好东西。我不会考虑在任何新项目中使用 OpenGL ES 1.x,因为从技术角度来看这是一个死胡同。

(2) 一旦使用 OpenGL ES 2.0 或更高版本,请使用缓冲区对象来存储顶点数据,并在应用程序启动时上传数据,而不是每帧上传数据。在这种情况下,这并不重要(立方体非常简单),但这是一个值得养成的好习惯。

关于java - OpenGL ES - 主线程上的工作太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562118/

相关文章:

android - 将撰写版本更新到 1.3.0-alpha01 的问题

python - 在Python中更快地抓取数据

java - Tomcat 8.0.15 Oracle 11 数据库和 jdbc javax.naming.NamingException

java - 单个 ListView 中的不同对象

java - 将值从编辑文本转换为字节

C/海湾合作委员会 : global variables shared between threads need to be volatile?

ios - NSTimer 在无效和重新初始化后仅被触发一次

java - 如果另一个映射中的键不存在,如何将元素添加到映射中

java - 如何停止java桌面应用程序中移动鼠标的无限循环

java - Android:如何从类的方法访问值?