Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – glCreateShader和glCreateProgram在android上失败大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Android上创建一个着色器程序有一个非常困难的问题.当我调用glCreateShader或者glCreateProgram时,总是返回0.

关于故障排除,我已经涵盖了我的所有基础:

>我检查过,以确保我有一个ogl上下文(我做的,我通过清除帧缓冲区的各种颜色,这是有效的).
>我试过glGetError但是没有返回(GL_NO_ERROR)

我不是opengl或android专家,所以我不知道可能导致这个的其他任何事情.

我已经在一个nexus 7平板电脑上运行我的应用程序,我使用的是OpenGL ES 2.0,并且定位到最新版本的Android(17版).

最后,我有我的代码显示

这是设置应用程序的样板代码

public class Platform implements ILinkable<Activity> {
    class GameLoop extends GLSurfaceView implements GLSurfaceView.Renderer {
        class Graphics2D implements IGraphics2D {
            int width  = 0;
            int height = 0;

            public void setWidth (int width ) { this.width = width; }
            public void setHeight(int height) { this.height = height; }

            public int getWidth () { return width;  }
            public int getHeight() { return height; }
        }

        class Time implements ITime {
            float frametime = 0;
            float @R_899_10586@ltime = 0;
            long  temptime = 0;
            Boolean running = true;

            public void beginTimeCount() {
                temptime = System.nanoTime();
            }

            public void endTimeCount() {
                frametime  = (System.nanoTime() - temptimE) / 1000000;
                @R_899_10586@ltime += frametime;
            }

            public float getFrameTime() { return frametime; }
            public float get@R_899_10586@lTime() { return @R_899_10586@ltime; }
        }

        Graphics2D graphics2d = new Graphics2D();
        Time       time       = new Time();
        Boolean    running    = true;

        public GameLoop(Context context) {
            super(context);

            setEGLContextClientVersion(2);
            setRenderer(this);
            //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }

        public void onSurfaceCreated(GL10 unused,EGLConfig config) {
            GLES20.glClearColor(0.5f,0.0f,0.5f,1.0f);
        }

        public void onDrawFrame(GL10 unused) {
            if (running) {
                time.beginTimeCount();

                for (Iupdateable u : Platform.this.root.updatE)
                    u.onupdate(timE);

                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                for (IDrawable2D d : Platform.this.root.draw2d) {
                    d.onDraw2D(graphics2d);
                }

                for (IDrawable3D d : Platform.this.root.draw3d)
                    d.onDraw3D();

                time.endTimeCount();
            }
        }

        public void onSurfaceChanged(GL10 unused,int width,int height) {
            GLES20.glViewport(0,width,height);
            graphics2d.setWidth(width);
            graphics2d.setHeight(height);
            for (IDrawable2D d : Platform.this.root.draw2d)
                d.onSize2D(graphics2d);
        }

        public void onPause() {
            super.onPause();
            running = false;
        }

        public void onResume() {
            super.onResume();
            running = true;
        }
    }

    private GameLoop gameloop;
    public Node root;

    public Platform() {
        this.root = new Node();
    }

    public void link(Activity activity) {
        this.gameloop = new GameLoop(activity);
        activity.requestWindowFeature(Window.FEATURE_NO_titlE);
        activity.setContentView(this.gameloop);
    }

    public void unlink(Activity activity) {
        this.gameloop = null;
        activity.setContentView(null);
    }
}

这是主要的活动:

public class MainActivity extends Activity {

    private Game game;
    private Platform platform;

    public MainActivity() {
        platform = new Platform();
        game = new Game();
    }

    public void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        platform.link(this);
        game.link(platform.root);
        game.onStart();
    }

    public void onDestroy() {
        super.onDestroy();
        game.onStop();
        game.unlink(platform.root);
        platform.unlink(this);
    }
}

这是创建着色器和程序的代码

public static int loadShader(int shaderType,String sourcE) throws FmtException {
    int[] gotVar = new int[]{ 0 };
    int shader = GLES20.glCreateShader(shaderTypE);

    if (shader == 0)
        throw new FmtException(FmtException.GLES,"Could not create shader: %s",getError());

    GLES20.glShadersource(shader,sourcE);
    GLES20.glCompileShader(shader);

    GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,gotVar,0);
    if (gotVar[0] == 0) {
        GLES20.glGetShaderiv(shader,GLES20.GL_INFO_LOG_LENGTH,0);
        if (gotVar[0] != 0) {
            GLES20.gldeleteShader(shader);
            throw new FmtException(FmtException.GLES,"Could not compile shader %d:\n%s\n",shaderType,GLES20.glGetShaderInfoLog(shader));
        }
    }

    return shader;
}

public static int createProgram(String pVertexsource,String pFragmentsourcE) throws FmtException {
    int[] gotVar = new int[]{ GLES20.GL_falSE };
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,pVertexsourcE);
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER,pFragmentsourcE);

    int program = GLES20.glCreateProgram();
    if (program == 0)
        throw new FmtException(FmtException.GLES,"Could not create program: %s",getError());


    GLES20.glAttachShader(program,vertexShader);
    GLES20.glAttachShader(program,pixelShader);
    GLES20.glLinkProgram(program);

    GLES20.glGetProgramiv(program,GLES20.GL_LINK_STATUS,0);
    if (gotVar[0] != GLES20.GL_TRUE) {
        GLES20.glGetProgramiv(program,0);
        if (gotVar[0] != 0) {
            GLES20.gldeleteProgram(program);
            throw new FmtException(FmtException.GLES,"Could not link program:\n%s\n",GLES20.glGetProgramInfoLog(program));
        }
    }

    return program;
}

任何帮助或建议将不胜感激.

解决方法

对于那些正在寻找答案的人来说,它隐藏在评论中.我在评论中背诵用户Brainberg.

大佬总结

以上是大佬教程为你收集整理的java – glCreateShader和glCreateProgram在android上失败全部内容,希望文章能够帮你解决java – glCreateShader和glCreateProgram在android上失败所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:android失败