如何在不创建 GLSurfaceView (Android) 的情况下检测 OpenGL 功能 [英] How to detect OpenGL capabilities without creating a GLSurfaceView (Android)

查看:17
本文介绍了如何在不创建 GLSurfaceView (Android) 的情况下检测 OpenGL 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在决定是使用 OpenGL 还是 Canvas 来实现图形目的之前,我正在尝试访问手机的 OpenGL 功能.但是,我可以阅读文档的所有函数都要求您已经有一个有效的 OpenGL 上下文(即,创建一个 GLSurfaceView 并为其分配一个渲染.然后检查 onSurfaceCreated 中的 OpenGL 参数).

I am trying to access the OpenGL capability of the phone before deciding whether to use OpenGL or Canvas for graphics puposes. However, all the functions that I can read documentation on requires you to already have a valid OpenGL context (namely, create a GLSurfaceView and assign it a rendered. Then check the OpenGL parameters in the onSurfaceCreated).

那么,有没有办法在必须创建任何 OpenGL 视图之前检查手机的扩展、渲染器名称和最大纹理大小能力?

So, is there a way to check the extensions, renderer name and max texture size capability of the phone BEFORE having to create any OpenGL views?

推荐答案

经过搜索,我得出的结论是,我需要一个有效的 GL 上下文才能查询它的功能.这反过来又需要一个 Surface,依此类推.基本上,您需要先创建 OpenGL 表面,然后才能检查它支持的内容.

After searching, I came to the conclusion that I need a valid GL context before I can query it for its capability. That, in turn requires a Surface, and so on. Basicly, you need to create the OpenGL surface before you can check what it supports.

所以这就是我最终要做的事情:我创建了一个新活动(GraphicChooser,我需要处理我的类名......),它启动而不是我的正常活动.此活动创建一个 GLSurfaceView,它在 onSurfaceCreated 中检查设备的功能.根据找到的内容,它会使用一些关于要使用的图形选项的标志来启动主要活动,然后退出.每个活动模式都设置为 singleTask,因此退出一个不会影响另一个,并且每个活动模式只能有一个实例.例如,在点击主页按钮并重新启动 GraphicChooser 活动后,它将向主活动触发一个新的意图,该意图仍然处于活动状态,但不会创建新的.

So here is what I ended up doing: I created a new activity (GraphicChooser, I need to work on my classes names...) that is launched instead of my normal activity. This activity creates a GLSurfaceView, which check the capability of the device in onSurfaceCreated. Depending on what is found, it starts the main activity with some flags about the graphic options to use, then exits. Each activity mode is set to singleTask so quitting one doesn't affect the other and there can only be a single instance of each. For example, after hitting the home button and you restart the GraphicChooser activity, it will fire a new intent to main activity, which is still active but will not create a new one.

它非常粗糙,当然有更好的方法,但我找不到.主要缺点是每次启动活动时,都会产生额外创建活动的开销.

It is very crude and there certainly is a better way to do it, but I couldn't find it. The main downside is that each time you start the activity, there is the overhead of creating an extra one.

package com.greencod.pinball.android;

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

import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;

public class GraphicChooser extends Activity {

   private GLSurfaceView mGLView;

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

       Log.d("Graphic Chooser", "onCreate: create view and renderer.");
       // get the screen size
       Display display = getWindowManager().getDefaultDisplay();
       int width = display.getWidth();
       int height = display.getHeight();

       mGLView = new GLSurfaceView(this);
       mGLView.setRenderer(new GraphicChooserRenderer(this, width, height));
       setContentView(mGLView);
   }

   @Override
   protected void onResume() {
      super.onResume();

      Log.d("Graphic Chooser", "onResume: purely for testing purpose.");
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();

      Log.d("Graphic Chooser", "onDestroy: Bye bye.");
   }

   static final int GAME_ACTIVITY_REQUEST_CODE = 10;
   public void launchGraphics(int type)
   {
      // launch game activity and kill this activity
      Intent i = new Intent(this, PinballActivity.class);
      i.putExtra("Graphics", type);
      // the activity requested should go in a new task, so even if we are passing
      // a request code, we will not get it when the new activity stops, but right now
      // as a 'cancel' request. That is ok, just quit this activity then.
      startActivityForResult(i, GAME_ACTIVITY_REQUEST_CODE);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if( requestCode == GAME_ACTIVITY_REQUEST_CODE )
         finish();
   }
}

class GraphicChooserRenderer implements GLSurfaceView.Renderer{

   GraphicChooser _activity;
   final int _width, _height;
   public GraphicChooserRenderer( GraphicChooser activity, int width, int height )
   {
      _activity = activity;
      _width = width;
      _height = height;
   }

   final int GRAPHICS_CANVAS = 0;
   final int GRAPHICS_OPENGL_DRAW_TEXTURE = 1;

   public void determineGraphicSupport(GL10 gl)
   {
      int _graphicEngine = GRAPHICS_CANVAS;

      String extensions = gl.glGetString(GL10.GL_EXTENSIONS); 
//      String version = GLES10.glGetString(GL10.GL_VERSION);
      String renderer = gl.glGetString(GL10.GL_RENDERER);
      boolean isSoftwareRenderer = renderer.contains("PixelFlinger");
      boolean supportsDrawTexture = extensions.contains("draw_texture");
      int[] arGlMaxTextureSize = new int[1];
      gl.glGetIntegerv( GL10.GL_MAX_TEXTURE_SIZE, arGlMaxTextureSize, 0 );

      if( !isSoftwareRenderer && supportsDrawTexture && _width >= 480 && _height >= 800 
            && arGlMaxTextureSize[0] >= 2048 )
         _graphicEngine = GRAPHICS_OPENGL_DRAW_TEXTURE;
      else
         _graphicEngine = GRAPHICS_CANVAS;

      Log.i("pinball", "Graphics using " + (_graphicEngine==GRAPHICS_CANVAS?"Canvas":"OpenGL EOS draw texture")
            + ". OpenGL renderer: " + renderer
            + ". Software renderer: " + isSoftwareRenderer
            + ". Support draw texture: " + supportsDrawTexture 
            + ". Texture max size: " + arGlMaxTextureSize[0]
            + ". Resolution: " + _width + "x" + _height );

      _activity.launchGraphics(_graphicEngine);
   }

   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      determineGraphicSupport(gl);
   }

   public void onSurfaceChanged(GL10 gl, int w, int h) {
      gl.glViewport(0, 0, w, h);
   }

   public void onDrawFrame(GL10 gl) {
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
   }  
}

这篇关于如何在不创建 GLSurfaceView (Android) 的情况下检测 OpenGL 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆