日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android OpenGL Cannot create GL program: 0 GL error: 1282

發(fā)布時(shí)間:2025/3/12 Android 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android OpenGL Cannot create GL program: 0 GL error: 1282 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android OpenGL create GL program: 0 & GL error: 1282

快速解決

1. 使用GLSurfaceView的話

請(qǐng)?jiān)诶^承類中合適的地方(一般是構(gòu)造函數(shù)里面)設(shè)置當(dāng)前的clientversion 為 2
具體代碼:

setEGLContextClientVersion(2);

2. 使用自己構(gòu)建的opengl環(huán)境的話

請(qǐng)?jiān)趧?chuàng)建glContext的時(shí)傳入的參數(shù)中配置 int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2 , EGL10.EGL_NONE };
具體代碼如下,注意 attrib_list 中的第二個(gè)元素寫為 2

//注意 :int EGL_CONTEXT_CLIENT_VERSION = 0x3098 int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; EGLContext glContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT,attrib_list);

注意,int EGL_CONTEXT_CLIENT_VERSION = 0x3098。到此,是同樣的問題的話,應(yīng)該得到了解決,可以繼續(xù)搬磚了。

有興趣的繼續(xù)往下看:

出錯(cuò)代碼鑒賞

int shader = GLES20.glCreateShader(type);// add the source code to the shader and compile itGLES20.glShaderSource(shader, shaderCode);checkError();GLES20.glCompileShader(shader);checkError();int program = GLES20.glCreateProgram();checkError();

該段代碼一般是封裝為一個(gè)叫l(wèi)oadShader的函數(shù)中,本身是無誤,但達(dá)不到期望的效果,如果在checkError里面拋出異常,那么就掛掉了。

通過上面的解決方法和代碼,問題的原因就明顯了:
GLES20.xxx() 和open環(huán)境不一致。即 opengl的環(huán)境不是2.0,卻使用了2.0的接口 ,所以將opengl的版本指定(初始化)為 2。

有些朋友可能直接提問 loadShader出錯(cuò),實(shí)際上的代碼是上面的代碼片段。

GLSurfaceView 部分代碼分享

GLSurfaceView 源碼是API level 30.

  • GLSurfaceView中 mEGLContextClientVersion 外部沒有設(shè)置的話,默認(rèn)是0
  • DefaultContextFactory 具體實(shí)現(xiàn)
  • private class DefaultContextFactory implements EGLContextFactory {private int EGL_CONTEXT_CLIENT_VERSION = 0x3098;public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, mEGLContextClientVersion,EGL10.EGL_NONE };return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT,mEGLContextClientVersion != 0 ? attrib_list : null);}public void destroyContext(EGL10 egl, EGLDisplay display,EGLContext context) {if (!egl.eglDestroyContext(display, context)) {Log.e("DefaultContextFactory", "display:" + display + " context: " + context);if (LOG_THREADS) {Log.i("DefaultContextFactory", "tid=" + Thread.currentThread().getId());}EglHelper.throwEglException("eglDestroyContex", egl.eglGetError());}}}

    從createContext 中代碼可看出,mEGLContextClientVersion 為0 的時(shí)候 ,egl.eglCreateContext的最后一個(gè)參數(shù)傳入的是null。同時(shí)mEGLContextClientVersion 又是 attrib_list 數(shù)組第二個(gè)元素,版本openGl2.0就是通過這個(gè)參數(shù)確定的。

  • setEGLContextClientVersion
    精簡(jiǎn)了函數(shù)的注釋,可以從注釋上面得到一些啟發(fā), Pick an OpenGL ES 2.0 context
  • /*** Inform the default EGLContextFactory and default EGLConfigChooser* which EGLContext client version to pick.* <p>Use this method to create an OpenGL ES 2.0-compatible context.* Example:* <pre class="prettyprint">* public MyView(Context context) {* super(context);* setEGLContextClientVersion(2); // Pick an OpenGL ES 2.0 context.* setRenderer(new MyRenderer());* }* </pre>*/public void setEGLContextClientVersion(int version) {checkRenderThreadState();mEGLContextClientVersion = version;}

    綜合起來使用GLSurfaceView的opengl便利的情況下要使用2.0,優(yōu)先調(diào)用setEGLContextClientVersion(2)

    基于TextureView 或SufaceView創(chuàng)建openglcontext

    最終的要調(diào)用的代碼如下:

    int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; EGLContext glContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT,attrib_list);

    version 可以根據(jù)需要進(jìn)行指定。

    補(bǔ)充:
    自定義 GLSurfaceView 的 setEGLContextFactory 是提供自定義創(chuàng)建opengl環(huán)境的接口,只要根據(jù)上面兩個(gè)關(guān)鍵調(diào)用實(shí)現(xiàn)EGLContextFactory 就可以,GLSurfaceView的默認(rèn)實(shí)現(xiàn)就是DefaultContextFactory。

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的Android OpenGL Cannot create GL program: 0 GL error: 1282的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。