Beginning to learn OpenGL ES. Drawing quad

落花浮王杯 提交于 2019-12-10 09:42:42

问题


I try to draw a quad with open GL ES 1.0. But I have an exception which say me that i try to draw more vertices than i have.

Code of Renderer :

package com.example.MyGLTest;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class OpenGLRenderer implements GLSurfaceView.Renderer{

    FloatBuffer plaineVerticesBuffer;
    FloatBuffer plaineIndicesBuffer;

    private float[] plaineVertices = {
            -1, -1, 0, //bottom left corner
            -1,  1, 0, //top left corner
            1,  1, 0,  //top right corner
            1, -1, 0   // bottom right rocner
    };
    private float plaine_indices[] = {
            0,1,2, // first triangle (bottom left - top left - top right)
            0,2,3 // second triangle (bottom left - top right - bottom right);
    };

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {


        ByteBuffer bb = ByteBuffer.allocateDirect(plaineVertices.length * 4);
        bb.order(ByteOrder.nativeOrder());
        plaineVerticesBuffer = bb.asFloatBuffer();
        plaineVerticesBuffer.put(plaineVertices);

        ByteBuffer bb1  = ByteBuffer.allocateDirect(plaine_indices.length * 4);
        bb1.order(ByteOrder.nativeOrder());
        plaineIndicesBuffer = bb1.asFloatBuffer();
        plaineIndicesBuffer.put(plaine_indices);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glVertexPointer(4,GL10.GL_FLOAT,0, plaineVerticesBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 6 ,GL10.GL_FLOAT, plaineIndicesBuffer);
    }
}

Error of logcat :

java.lang.ArrayIndexOutOfBoundsException: remaining() < count < needed
        at com.google.android.gles_jni.GLImpl.glDrawElements(Native Method)
        at com.example.MyGLTest.OpenGLRenderer.onDrawFrame(OpenGLRenderer.java:60)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1531)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)

Can anyone explain me why i getting this error?


回答1:


You are tracing a strip of 2 triangles, and you are passing 6 vertices. You should either draw GL_TRIANGLES or fix the indices.



来源:https://stackoverflow.com/questions/20265714/beginning-to-learn-opengl-es-drawing-quad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!