2011-01-29 40 views
1

我正在嘗試訪問手機的OpenGL功能,然後決定是否使用OpenGL或Canvas作爲圖形puposes。然而,我可以閱讀文檔的所有函數都要求您已經擁有一個有效的OpenGL上下文(即,創建一個GLSurfaceView併爲其分配一個渲染,然後檢查onSurfaceCreated中的OpenGL參數)。如何在不創建GLSurfaceView的情況下檢測OpenGL功能(Android)

那麼,有沒有辦法在創建任何OpenGL視圖之前檢查手機的擴展名,渲染器名稱和最大紋理尺寸功能?

回答

2

搜索後,我得出結論,我需要一個有效的GL上下文之前,我可以查詢它的能力。這又需要一個Surface,等等。基本上,您需要先創建OpenGL表面,然後才能檢查它支持的內容。

這就是我最終做的事情:我創建了一個新的活動(GraphicChooser,我需要處理我的類名......),而不是我的正常活動。此活動將創建一個GLSurfaceView,用於檢查onSurfaceCreated中設備的功能。根據找到的內容,它會啓動主要活動並附帶一些關於要使用的圖形選項的標誌,然後退出。每個活動模式設置爲singleTask,因此退出一個不會影響另一個,每個活動模式只能有一個實例。例如,在點擊主頁按鈕並重新啓動GraphicChooser活動後,它將激發一個主要活動的新意向,該活動仍處於活動狀態,但不會創建新活動。

這是非常粗糙的,當然有更好的方法來做到這一點,但我找不到它。主要缺點是每次開始活動時,都會產生額外的開銷。在谷歌的朋友

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); 
    } 
} 
+0

嗨亞行,我面臨這個問題在Android上運行我的GLSurfaceview。這裏我使用了opgneles2.0技術。請在這個計算器鏈接看到我的問題,並幫助我解決這個問題.. http://stackoverflow.com/questions/17187032/why-my-opengl-output-differs-for-various-devices – harikrishnan 2013-06-19 12:53:51

0

對於擴展:

EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); 
String extensions = EGL14.eglQueryString(display, EGL14.EGL_EXTENSIONS); 
相關問題