android – OpenGL ES 2.0 IllegalArgumentException

android – OpenGL ES 2.0 IllegalArgumentException,第1张

概述现在我正在尝试为一个小游戏创建图形. 首先,我试图创造我的障碍. 这些只是矩形. 我尝试将所有数据(对于所有矩形都相同)静态化. 颜色,中StartPosition,着色器. 我正在尝试这个,导致早期版本工作,有性能问题. 当我开始工作时,我也想切换到VBO. 这些对象将被单独移动和缩放(而不是代码). 我想尝试我的代码,但我面临一个我不明白的错误. 这是我的Logcat输出 04-08 15:0 现在我正在尝试为一个小游戏创建图形.
首先,我试图创造我的障碍.
这些只是矩形.
我尝试将所有数据(对于所有矩形都相同)静态化.
颜色,中Startposition,着色器.
我正在尝试这个,导致早期版本工作,有性能问题.
当我开始工作时,我也想切换到VBO.
这些对象将被单独移动和缩放(而不是代码).

我想尝试我的代码,但我面临一个我不明白的错误.
这是我的Logcat输出

04-08 15:03:05.573: E/AndroIDRuntime(3051): FATAL EXCEPTION: GLThread 492604-08 15:03:05.573: E/AndroIDRuntime(3051): Process: com.example.jump,PID: 305104-08 15:03:05.573: E/AndroIDRuntime(3051): java.lang.IllegalArgumentException: length - offset < count*4 < needed04-08 15:03:05.573: E/AndroIDRuntime(3051):     at androID.opengl.GLES20.gluniform4fv(Native Method)04-08 15:03:05.573: E/AndroIDRuntime(3051):     at com.example.jump.ObstacleGL.initialize(ObstacleGL.java:82)04-08 15:03:05.573: E/AndroIDRuntime(3051):     at com.example.jump.GameRenderer.onSurfaceCreated(GameRenderer.java:57)04-08 15:03:05.573: E/AndroIDRuntime(3051):     at androID.opengl.GLSurfaceVIEw$GLThread.guardedRun(GLSurfaceVIEw.java:1501)04-08 15:03:05.573: E/AndroIDRuntime(3051):     at androID.opengl.GLSurfaceVIEw$GLThread.run(GLSurfaceVIEw.java:1240)

这让我困惑,长度为3,偏移量为0,计数* 4为4.
我不知道“需要”是什么价值.我认为这是声明失败的地方.

这是我的班级.
我在onSurfacecreated中调用了初始化方法

package com.example.jump;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.floatBuffer;import java.nio.ShortBuffer;import androID.opengl.GLES20;import androID.opengl.Matrix;public class ObstacleGL {    static final int COORDS_PER_VERTEX = 3;    static final int vertexStrIDe = COORDS_PER_VERTEX * 4;    private static final String vertexshadercode =            "uniform mat4 uMVPMatrix;" +            "attribute vec4 vposition;" +            "voID main() {" +            "  gl_position = uMVPMatrix * vposition;" +            "}";    private static final String fragmentshadercode =            "precision mediump float;" +            "uniform vec4 vcolor;" +            "voID main() {" +            "  gl_Fragcolor = vcolor;" +            "}";    private static float [] coords={            //x,y,z   z=0            1,1,//ol            1,//ul            1.2f,//ur            1.2f,0  //or    };    private static final short[] drawOrder = { 0,2,3 };    private static int fragmentShader;    private static int vertexShader;    private static int mProgram;    private static float[] color={0.33f,0.33f};    private static int colorHandle;    private static int positionHandle;      private static floatBuffer vertexBuffer;    private static ShortBuffer drawListBuffer;    //Gamelogic    public final int ID;    private float x,height,wIDth;    private float xOffset,yOffset;    //Opengl    private int matrixHandle;    private int vertexCount;    private float[] translationMatrix=new float[16];    public static voID initialize(){        fragmentShader=GameRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentshadercode);        vertexShader=GameRenderer.loadShader(GLES20.GL_VERTEX_SHADER,vertexshadercode);        ByteBuffer bb;        ByteBuffer dlb;        bb= ByteBuffer.allocateDirect(coords.length*4);        bb.order(ByteOrder.nativeOrder());        vertexBuffer=bb.asfloatBuffer();        vertexBuffer.put(coords);        vertexBuffer.position(0);        dlb=ByteBuffer.allocateDirect(12); //drawOrder.length*2        dlb.order(ByteOrder.nativeOrder());        drawListBuffer=dlb.asShortBuffer();        drawListBuffer.put(drawOrder);        drawListBuffer.position(0);        mProgram=GLES20.glCreateProgram();        GLES20.glAttachShader(mProgram,fragmentShader);        GLES20.glAttachShader(mProgram,vertexShader);        colorHandle=GLES20.glGetUniformlocation(mProgram,"vcolor");        positionHandle=GLES20.glGetAttribLocation(mProgram,"vposition");        GLES20.gllinkProgram(mProgram);        GLES20.glVertexAttribPointer(positionHandle,COORDS_PER_VERTEX,GLES20.GL_float,false,vertexStrIDe,vertexBuffer);        GLES20.gluniform4fv(colorHandle,color,0);    }    public ObstacleGL(int ID,float x,float y,float height,float wIDth)    {        this.ID=ID;        //not used at the moment        this.x=x;        this.xOffset=0;        this.y=0;        this.yOffset=0;        this.height=height;        this.wIDth=wIDth;        //        vertexCount=coords.length/COORDS_PER_VERTEX;        Matrix.setIDentityM(translationMatrix,0);        //getting the handle to set new translations        matrixHandle=GLES20.glGetUniformlocation(mProgram,"uMVPMatrix");    }    public voID draw()    {        GLES20.gluseProgram(mProgram);        //here I give the new position        GLES20.gluniformMatrix4fv(matrixHandle,translationMatrix,0);        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN,vertexCount);        GLES20.gldisabLevertexAttribarray(positionHandle);    }    public voID moveX(float x)    //moves the Obstacle on the x axIEs    {        this.xOffset+=x;        Matrix.translateM(translationMatrix,x,0);    }    public voID moveY(float y)    //moves the Obstacle on the y axIEs    {        this.yOffset+=y;        Matrix.translateM(translationMatrix,0);    }}

我也可以告诉你我的渲染器类

package com.example.jump;import java.util.linkedList;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import androID.opengl.GLES20;import androID.opengl.GLSurfaceVIEw;public class GameRenderer implements GLSurfaceVIEw.Renderer,GameVisualisation {    private linkedList<ObstacleGL> obstacleList=new linkedList<ObstacleGL>();    private linkedList<Obstacle> obstacleQueue= new linkedList<Obstacle>();    //Renderer Methoden    @OverrIDe    public voID onDrawFrame(GL10 gl) {        GLES20.glClear(GLES20.GL_color_BUFFER_BIT);        createNewObstacles();        for (int i = 0; i < obstacleList.size(); i++) {            obstacleList.get(i).draw();        }    }    public static int loadShader(int type,String shadercode)    {        int shader=GLES20.glCreateShader(type);        GLES20.glShaderSource(shader,shadercode);        GLES20.glCompileShader(shader);        return shader;    }    private voID createNewObstacles() {        while(!obstacleQueue.isEmpty()){            Obstacle obs=obstacleQueue.poll();            obstacleList.add(new ObstacleGL(obs.ID,(float)obs.xposition,(float)obs.yposition,(float)obs.height,(float)obs.wIDth));        }    }    @OverrIDe    public voID onSurfaceChanged(GL10 gl,int wIDth,int height) {        GLES20.glVIEwport(0,wIDth,height);    }    @OverrIDe    public voID onSurfaceCreated(GL10 gl,EGLConfig config) {        GLES20.glClearcolor(0.5f,0.5f,1.0f);        ObstacleGL.initialize();    }//more gamerelated stuff is happening here}
解决方法 所以我自己做了.问题似乎是他不接受尺寸为3的float阵列和vec4制服.解决方案是将Alpha通道添加到我的颜色数组中.

我换了

private static float[] color={0.33f,0.33f};

private static float[] color={0.33f,0.33f,1};
总结

以上是内存溢出为你收集整理的android – OpenGL ES 2.0 IllegalArgumentException全部内容,希望文章能够帮你解决android – OpenGL ES 2.0 IllegalArgumentException所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存