Android OpenGL ES渲染细分网格

时间:2014-04-25 19:58:39

标签: android opengl-es

我试图渲染一个细分网格,上面有一个位移纹理和一个颜色纹理。为此,我遍历每个像素,为它创建一个顶点,并根据我的黑白图像移动该顶点。问题是,当我渲染它时,我得到的东西看起来有点像电视雪。 enter image description here

以下是相关代码:

public Plane(Bitmap image, Bitmap depth)
{
    this.image = image; //color image
    this.depth = depth; //BW depth image
    this.w = image.getWidth();
    this.h = image.getHeight();

    vertexCoords = vertexArray(); //places vertices in 3d
    drawOrder = orderArray(); //sets the draw order
    colorCoords = colorArray(); //sets color per vertex

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

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 4);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

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

public void draw(GL10 gl) {
    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW);
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE);
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK);

    // Enabled the vertices buffer for writing and to be used during
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    // Enable the color array buffer to be used during rendering.
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY); // NEW LINE ADDED.
    // Point out the where the color buffer is.
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); // NEW LINE ADDED.

    gl.glDrawElements(GL10.GL_TRIANGLES, drawOrder.length,
            GL10.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    // Disable face culling.
    gl.glDisable(GL10.GL_CULL_FACE);

    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

我可以做些什么来实际查看模型,而不是这个雪的东西?如果我打开和关闭屏幕,模式会发生变化,有时会随机变化。似乎原始位图中存在的颜色也存在于雪中(雪色随着不同的图片而变化),所以我知道我做了正确的事情,我只是不知道什么是'这里错了。

编辑:这是vertexArray()

的代码
public float[] vertexArray()
{
    int totalPoints = w*h;
    float[] arr = new float[totalPoints*3];

    int i = 0;
    for(int y = 0; y<h; y++)
    {
        for(int x = 0; x<w; x++)
        {
            arr[i] = x * 0.01f;
            arr[i+1] = y * 0.01f;
            arr[i+2] = 1.0f;//getDepth(x,y);
            i+=3;
        }
    }

    return arr;
}

0 个答案:

没有答案