2

OpenGL-ESを使用してAndroidで白いクワッドを描画しようとしていますが、色が薄くなっています。

写真を参照してください:http://img88.imageshack.us/img88/2787/tempew.png

私も試してみましたint numVertices = 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 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
}
4

1 に答える 1

2
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
}

これはあなたが思っていることをしません。あなたは書く必要がありますi += 4

それ以外の場合は、意図したとおりにnumVertices*4ではなくnumVertices/4+4コンポーネントを初期化します。

于 2011-07-11T16:24:28.397 に答える