Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Android OpenGL:图形四基色褪色问题_Android_Opengl Es_Geometry_Fading - Fatal编程技术网

Android OpenGL:图形四基色褪色问题

Android OpenGL:图形四基色褪色问题,android,opengl-es,geometry,fading,Android,Opengl Es,Geometry,Fading,我正在用OpenGL ES在Android上画一个白色的四边形,颜色正在褪色 见图: 我用intnumvertices=4尝试过它也是,但我得到的不是红色而是黑色。怎么回事 int numVertices = 6; private FloatBuffer vertexBuffer, colorBuffer; private ShortBuffer indexBuffer; private short[] indices = { 0, 1, 2,

我正在用OpenGL ES在Android上画一个白色的四边形,颜色正在褪色

见图:

我用
intnumvertices=4尝试过它也是,但我得到的不是红色而是黑色。怎么回事

int numVertices = 6;

private FloatBuffer vertexBuffer, colorBuffer;
private ShortBuffer indexBuffer;
private short[] indices = { 0, 1, 2, 
                            2, 3, 0 };

public Floor (float coords[][]) {

    float vertices[] = new float[numVertices*3];
    float colors[] = new float[numVertices*4];

    //Set colors
    for(int i=0; i < numVertices; i++){
        colors[i] = 1.0f;   //red
        colors[i+1] = 1.0f; //grn
        colors[i+2] = 1.0f; //blu
        colors[i+3] = 1.0f; //alpha
    }

    vertices[0] = coords[0][0];
    vertices[1] = coords[0][1];
    vertices[2] = coords[0][2];

    vertices[3] = coords[1][0];
    vertices[4] = coords[1][1];
    vertices[5] = coords[1][2]; 

    vertices[6] = coords[2][0];
    vertices[7] = coords[2][1];
    vertices[8] = coords[2][2];

    vertices[9]  = coords[3][0];
    vertices[10] = coords[3][1];
    vertices[11] = coords[3][2];

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    cbb.order(ByteOrder.nativeOrder());
    colorBuffer = cbb.asFloatBuffer();
    colorBuffer.put(colors);
    colorBuffer.position(0);

    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);


}

/**
 * This function draws our square on screen.
 * @param gl
 */
public void draw(GL10 gl) {

    gl.glDisable(GL10.GL_COLOR_MATERIAL);
    gl.glDisable(GL10.GL_BLEND);
    gl.glDisable(GL10.GL_LIGHTING);

    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);

    gl.glPointSize(2.0f);

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

}
int数值=6;
私有浮动缓冲区vertexBuffer、colorBuffer;
私人短缓冲区索引缓冲区;
私有短[]索引={0,1,2,
2, 3, 0 };
公共楼层(浮动坐标[]){
浮动顶点[]=新浮动[numvitices*3];
浮动颜色[]=新浮动[数值*4];
//定色
对于(int i=0;i
}

谢谢你的帮助

找到了答案。对于其他遇到此问题的人,需要进行修复:

int numVertices = 4;

//Set colors
for(int i=0; i < numVertices; i++){
    colors[i*4] = 1.0f; //red
    colors[i*4+1] = 1.0f;   //grn
    colors[i*4+2] = 1.0f;   //blu
    colors[i*4+3] = 1.0f;   //alpha
}
int数值=4;
//定色
对于(int i=0;i
用于(int i=0;i
这和你想象的不一样。您需要编写
i+=4

否则,您将初始化numVertices/4+4组件,而不是按预期初始化numVertices*4

for(int i=0; i < numVertices; i++){
    colors[i] = 1.0f;   //red
    colors[i+1] = 1.0f; //grn
    colors[i+2] = 1.0f; //blu
    colors[i+3] = 1.0f; //alpha
}