Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android OpenGL ES 2.0 VBO_Java_Android_Opengl Es_Vbo_Glsles - Fatal编程技术网

Java Android OpenGL ES 2.0 VBO

Java Android OpenGL ES 2.0 VBO,java,android,opengl-es,vbo,glsles,Java,Android,Opengl Es,Vbo,Glsles,我花了几天时间试图让VBO使用OpenGLES2.0在Android上工作,但我似乎无法让它工作 以下是我正在使用的代码: /* The Android shader code */ private static final String[] androidVertexShaderCode = new String[] { "attribute vec4 vertexPosition;", "void main() {", " gl_Positi

我花了几天时间试图让VBO使用OpenGLES2.0在Android上工作,但我似乎无法让它工作

以下是我正在使用的代码:

/* The Android shader code */
private static final String[] androidVertexShaderCode = new String[] {
        "attribute vec4 vertexPosition;",
        "void main() {",
        "  gl_Position = vertexPosition;",
        "}" };

private static final String[] androidFragmentShaderCode = new String[] {
        "precision mediump float;",
        "uniform vec4 colour;",
        "void main() {",
        "  gl_FragColor = colour;",
        "}" };

/* The Android shader */
public Shader androidShader;

/* The constructor with the render mode and the 
 * number of vertex values given */
public AndroidRenderer(int renderMode, int vertexValuesCount) {
    super(renderMode, vertexValuesCount);
    //Add this renderer to the list
    allRenderers.add(this);
    usage = GLES20.GL_STATIC_DRAW;
    //Setup the Android shader
    this.androidShader = new AndroidShader();
    this.androidShader.vertexShader = ShaderUtils.createShader(ArrayUtils.toStringList(androidVertexShaderCode), Shader.VERTEX_SHADER);
    this.androidShader.fragmentShader = ShaderUtils.createShader(ArrayUtils.toStringList(androidFragmentShaderCode), Shader.FRAGMENT_SHADER);
    this.androidShader.create();
}

/* The method used to setup the buffers,
 * assumes the vertices have already been set */
public void setupBuffers() {
    //Create the vertices buffer
    this.verticesBuffer = BufferUtils.createFlippedBuffer(this.verticesData);
    int[] vh = new int[1];
    GLES20.glGenBuffers(1, vh, 0);
    //Setup the vertices handle
    this.verticesHandle = vh[0];

    //Bind the vertices buffer and give OpenGL the data
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.verticesHandle);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, Float.BYTES * verticesData.length, this.verticesBuffer, this.usage);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    //Check to see whether the normals have been set
    if (this.normalsData != null) {
        //Create the normals buffer
        this.normalsBuffer = BufferUtils.createFlippedBuffer(this.normalsData);
        int[] nh = new int[1];
        GLES20.glGenBuffers(1, nh, 0);
        //Setup the normals handle
        this.normalsHandle = nh[0];

        //Bind the normals buffer and give OpenGL the data
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.normalsHandle);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, Float.BYTES * verticesData.length, this.normalsBuffer, this.usage);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }

    //Check to see whether the colours have been set
    if (this.colourData!= null) {
        //Create the colours buffer
        this.coloursBuffer = BufferUtils.createFlippedBuffer(this.colourData);
        int[] ch = new int[1];
        GLES20.glGenBuffers(1, ch, 0);
        //Setup the colours handle
        this.coloursHandle = ch[0];

        //Bind the colours buffer and give OpenGL the data
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.coloursHandle);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, Float.BYTES * colourData.length, this.coloursBuffer, this.usage);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }

    //Check to see whether the texture coordinates have been set
    if (this.textureData != null) {
        //Create the texture coordinates buffer
        this.texturesBuffer = BufferUtils.createFlippedBuffer(this.textureData);
        int[] th = new int[1];
        GLES20.glGenBuffers(1, th, 0);
        //Setup the texture coordinates handle
        this.texturesHandle = th[0];

        //Bind the texture coordinates buffer and give OpenGL the data
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.texturesHandle);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, Float.BYTES * textureData.length, this.texturesBuffer, this.usage);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }
}

/* The method used to draw the object */
public void render() {
    this.androidShader.use();
    short[] indices = new short[this.verticesData.length];
    for (short a = 0; a < indices.length; a++)
        indices[a] = a;
    ShortBuffer indicesBuffer = BufferUtils.createFlippedBuffer(indices);
    //Enable the arrays as needed
    int vertexPositionAttribute = GLES20.glGetAttribLocation(this.androidShader.program, "vertexPosition");
    int normalAttribute = 0;
    int colourAttribute = 0;
    int texturesAttribute = 0;
    GLES20.glEnableVertexAttribArray(vertexPositionAttribute);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.verticesHandle);
    GLES20.glVertexAttribPointer(vertexPositionAttribute, this.vertexValuesCount, GLES20.GL_FLOAT, false, 0, 0);
    if (this.normalsData != null) {
        GLES20.glEnableVertexAttribArray(normalAttribute);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.normalsHandle);
        GLES20.glVertexAttribPointer(normalAttribute, 2, GLES20.GL_FLOAT, false, 0, 0);
    }
    if (this.colourData != null) {
        colourAttribute = GLES20.glGetAttribLocation(this.androidShader.program, "colour");
        Log.d("HELLO", "" + colourAttribute);
        GLES20.glEnableVertexAttribArray(colourAttribute);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.coloursHandle);
        GLES20.glVertexAttribPointer(colourAttribute, this.colourValuesCount, GLES20.GL_FLOAT, false, 0, 0);
    }
    if (this.textureData != null) {
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.texturesHandle);
        GLES20.glEnableVertexAttribArray(texturesAttribute);
        GLES20.glVertexAttribPointer(texturesAttribute, this.textureValuesCount, GLES20.GL_FLOAT, false, 0, 0);
    }
    //Draw the arrays
    GLES20.glDrawElements(this.renderMode, indices.length, GLES20.GL_UNSIGNED_SHORT, indicesBuffer);
    //Disable the arrays as needed
    if (this.normalsData != null)
        GLES20.glDisableVertexAttribArray(normalAttribute);
    if (this.textureData != null)
        GLES20.glDisableVertexAttribArray(texturesAttribute);
    if (this.colourData != null)
        GLES20.glDisableVertexAttribArray(colourAttribute);
    GLES20.glDisableVertexAttribArray(vertexPositionAttribute);
    this.androidShader.stopUsing();
}
/*Android着色器代码*/
私有静态最终字符串[]androidVertexShaderCode=新字符串[]{
“属性向量4;”,
“void main(){”,
“gl_位置=垂直方向;”,
"}" };
私有静态最终字符串[]androidFragmentShaderCode=新字符串[]{
“精密中泵浮动;”,
“统一的vec4颜色;”,
“void main(){”,
“gl_FragColor=颜色;”,
"}" };
/*Android着色器*/
公共着色器和着色器;
/*具有呈现模式和
*给定的顶点值数*/
public AndroidRenderer(内部渲染模式、内部顶点值){
超级(renderMode、vertexValuesCount);
//将此渲染器添加到列表中
添加(此);
用法=GLES20.GL\u静态\u绘图;
//设置Android着色器
this.androidShader=新的androidShader();
this.androidShader.vertexShader=ShaderUtils.createShader(ArrayUtils.toStringList(androidVertexShaderCode),Shader.VERTEX_Shader);
this.androidShader.fragmentShader=ShaderUtils.createShader(ArrayUtils.toStringList(androidFragmentShaderCode),Shader.FRAGMENT_Shader);
this.androidShader.create();
}
/*用于设置缓冲区的方法,
*假设已设置顶点*/
公共无效设置缓冲区(){
//创建顶点缓冲区
this.verticesBuffer=BufferUtils.createFlippedBuffer(this.verticesData);
int[]vh=新int[1];
GLES20.glGenBuffers(1,vh,0);
//设置顶点控制柄
this.verticesHandle=vh[0];
//绑定顶点缓冲区并向OpenGL提供数据
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,this.verticesHandle);
GLES20.glBufferData(GLES20.GL_数组_BUFFER,Float.BYTES*verticesData.length,this.verticesBuffer,this.usage);
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,0);
//检查以查看是否已设置法线
如果(this.normalsData!=null){
//创建法线缓冲区
this.normalsBuffer=BufferUtils.createFlippedBuffer(this.normalsData);
int[]nh=新的int[1];
GLES20.glGenBuffers(1,nh,0);
//设置法线控制柄
this.normalsHandle=nh[0];
//绑定法线缓冲区并向OpenGL提供数据
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,this.normalsHandle);
GLES20.glBufferData(GLES20.GL_数组_BUFFER,Float.BYTES*verticesData.length,this.normalsBuffer,this.usage);
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,0);
}
//检查颜色是否已设置好
if(this.colordata!=null){
//创建颜色缓冲区
this.colorsbuffer=BufferUtils.createFlippedBuffer(this.colorData);
int[]ch=新的int[1];
GLES20.glGenBuffers(1,ch,0);
//设置颜色手柄
this.colorshandle=ch[0];
//绑定颜色缓冲区并向OpenGL提供数据
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,this.colorshandle);
GLES20.glBufferData(GLES20.GL_数组_BUFFER,Float.BYTES*colordata.length,this.colorsbuffer,this.usage);
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,0);
}
//检查纹理坐标是否已设置
如果(this.textureData!=null){
//创建纹理坐标缓冲区
this.texturesBuffer=BufferUtils.createFlippedBuffer(this.textureData);
int[]th=新的int[1];
GLES20.glGenBuffers(1,th,0);
//设置纹理坐标控制柄
this.texturesHandle=th[0];
//绑定纹理坐标缓冲区并向OpenGL提供数据
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,this.texturesHandle);
GLES20.glBufferData(GLES20.GL_数组_BUFFER,Float.BYTES*textureData.length,this.texturesBuffer,this.usage);
GLES20.glBindBuffer(GLES20.GL_数组_BUFFER,0);
}
}
/*用于绘制对象的方法*/
公共无效呈现(){
this.androidShader.use();
short[]index=新的short[this.verticesData.length];
对于(短a=0;aGLES20.glVertexAttribPointer(colourAttribute, this.colourValuesCount, GLES20.GL_FLOAT, false, **0**, 0);
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
glDrawElements(.. , ..) 
glDrawArrays(.., .., )