在Android上的OpenGL Sphere上绑定视频纹理

时间:2015-06-24 13:10:51

标签: java android video opengl-es textures

我正在尝试在Android上创建一个 360视频领域(就像纸板一样)。我通过在OpenGL ES1.0中渲染球体而不是将纹理附加到它来拍摄照片。之后我可以使用传感器值来旋转球体。

但是,我无法弄清楚如何将图片更改为视频。我尝试使用texSubImage2D()逐帧渲染,但它是超级慢。我的视频可能是 4k密度,因为我需要一个好的质量,即使它只显示了一小部分。

我已经阅读了一些关于如何做到这一点的理论知识(即帧缓冲,外部纹理,同步等)但我找不到任何示例对于这些事情,所以一些代码将非常感激 ......

以下是我如何渲染球体,绘制它并为其附加纹理(即我的Sphere类)......

$('li').click(function(){
    alert($(this).data('value'));
})

}

这是我的渲染器......

import rapid.decoder.BitmapDecoder;
public class Sphere {


/** Buffer holding the vertices. */
private final List<FloatBuffer> mVertexBuffer = new ArrayList<FloatBuffer>();

/** The vertices for the sphere. */
private final List<float[]> mVertices = new ArrayList<float[]>();

/** Buffer holding the texture coordinates. */
private final List<FloatBuffer> mTextureBuffer = new ArrayList<FloatBuffer>();

/** Mapping texture coordinates for the vertices. */
private final List<float[]> mTexture = new ArrayList<float[]>();

/** The texture pointer. */
private final int[] mTextures = new int[1];

/** Total number of strips for the given depth. */
private final int mTotalNumStrips;

public Sphere(final int depth, final float radius) {

    // Calculate basic values for the sphere.
    this.mTotalNumStrips = Maths.power(2, depth - 1) * 5; //last 5 is related to properties of a icosahedron
    final int numVerticesPerStrip = Maths.power(2, depth) * 3;
    final double altitudeStepAngle = Maths.rad120 / Maths.power(2, depth);
    final double azimuthStepAngle = Maths.rad360 / this.mTotalNumStrips;
    double x, y, z, h, altitude, azimuth;

    Log.e("mTotalNumStrips", ""+mTotalNumStrips);
    Log.e("numVerticesPerStrip", ""+numVerticesPerStrip);

    for (int stripNum = 0; stripNum < this.mTotalNumStrips; stripNum++) {
        // Setup arrays to hold the points for this strip.
        final float[] vertices = new float[numVerticesPerStrip * 3]; // x,y,z
        final float[] texturePoints = new float[numVerticesPerStrip * 2]; // 2d texture
        int vertexPos = 0;
        int texturePos = 0;

        // Calculate position of the first vertex in this strip.
        altitude = Maths.rad90;
        azimuth = stripNum * azimuthStepAngle;

        // Draw the rest of this strip.
        for (int vertexNum = 0; vertexNum < numVerticesPerStrip; vertexNum += 2) {
            // First point - Vertex.
            y = radius * Math.sin(altitude);
            h = radius * Math.cos(altitude);
            z = h * Math.sin(azimuth);
            x = h * Math.cos(azimuth);
            vertices[vertexPos++] = (float) x;
            vertices[vertexPos++] = (float) y;
            vertices[vertexPos++] = (float) z;

            // First point - Texture.
            texturePoints[texturePos++] = (float) (1 + azimuth / Maths.rad360);
            texturePoints[texturePos++] = (float) (1 - (altitude + Maths.rad90) / Maths.rad180);

            // Second point - Vertex.
            altitude -= altitudeStepAngle;
            azimuth -= azimuthStepAngle / 2.0;
            y = radius * Math.sin(altitude);
            h = radius * Math.cos(altitude);
            z = h * Math.sin(azimuth);
            x = h * Math.cos(azimuth);
            vertices[vertexPos++] = (float) x;
            vertices[vertexPos++] = (float) y;
            vertices[vertexPos++] = (float) z;

            // Second point - Texture.
            texturePoints[texturePos++] = (float) (1 + azimuth / Maths.rad360);
            texturePoints[texturePos++] = (float) (1 - (altitude + Maths.rad90) / Maths.rad180);

            azimuth += azimuthStepAngle;
        }

        this.mVertices.add(vertices);
        this.mTexture.add(texturePoints);

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(numVerticesPerStrip * 3 * Float.SIZE);
        byteBuffer.order(ByteOrder.nativeOrder());
        FloatBuffer fb = byteBuffer.asFloatBuffer();
        fb.put(this.mVertices.get(stripNum));
        fb.position(0);
        this.mVertexBuffer.add(fb);

        // Setup texture.
        byteBuffer = ByteBuffer.allocateDirect(numVerticesPerStrip * 2 * Float.SIZE);
        byteBuffer.order(ByteOrder.nativeOrder());
        fb = byteBuffer.asFloatBuffer();
        fb.put(this.mTexture.get(stripNum));
        fb.position(0);
        this.mTextureBuffer.add(fb);
    }


}


public void loadGLTexture(final GL10 gl, final Context context, final int texture) {
    Bitmap bitmap = BitmapDecoder.from(context.getResources(), texture)
            .scale(4048, 2024)
            .decode();

    // Generate one texture pointer, and bind it to the texture array.
    gl.glGenTextures(1, this.mTextures, 0);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, this.mTextures[0]);

    // Create nearest filtered texture.
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    // Use Android GLUtils to specify a two-dimensional texture image from our bitmap.
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    // Tide up.
    bitmap.recycle();
}

/**
 * The draw method for the square with the GL context.
 *
 * @param gl Graphics handle.
 */
public void draw(final GL10 gl) {
    // bind the previously generated texture.
    gl.glBindTexture(GL10.GL_TEXTURE_2D, this.mTextures[0]);

    // Point to our buffers.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // Set the face rotation, clockwise in this case.
    gl.glFrontFace(GL10.GL_CW);

    // Point to our vertex buffer.
    for (int i = 0; i < this.mTotalNumStrips; i++) {
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.mVertexBuffer.get(i));
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, this.mTextureBuffer.get(i));

        // Draw the vertices as triangle strip.
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, this.mVertices.get(i).length / 3);
    }

    // Disable the client state before leaving.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

1 个答案:

答案 0 :(得分:0)

我有一些这类视频纹理问题。我第一次使用ffmpeg进行视频解码,但性能却很差(就像你一样 - 逐帧提取)。为了提高性能,我使用了android默认的mediaplayer。你可以使用表面纹理来创建一个opengl表面(球体,圆柱体,立方体等......),然后在媒体播放器中设置表面

Surface surface = new Surface(mSurface);//mSurface is your surface texture
mMediaPlayer.setSurface(surface);
mMediaPlayer.setScreenOnWhilePlaying(true);

这只是一种技巧。我为一些商业封闭项目做了这个,所以我不能分享代码。我希望我很快就能在github上发布免费代码。