gpt4 book ai didi

android - 如何在不创建 GLSurfaceView 的情况下检测 OpenGL 功能 (Android)

转载 作者:行者123 更新时间:2023-11-29 18:22:07 28 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

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

关于android - 如何在不创建 GLSurfaceView 的情况下检测 OpenGL 功能 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4837652/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com