在Android onCameraFrme中更改opengl对象顶点

时间:2018-06-15 15:52:28

标签: android opencv opengl-es opengl-es-2.0

我正在使用Opengl和Opencv制作应用程序。 Opencv识别屏幕上方块的角落。 我应用了一个算法,将点的屏幕坐标转换为笛卡尔坐标为opengl,然后创建顶点。 但是从广场中捕获的四个点中取出的每个点都被修改为每个帧, 所以浮点顶点数组也会随着每一帧而变化,如何将顶点更改为每一帧?

这是MyGLSurfaceView

public class MyGLSurfaceView implements GLSurfaceView.Renderer{

    Context context;
    Model3D model3D;

    float[] vertices;

    public MyGLSurfaceView(Context context, float[] vertices){
       this.context = context;
       this.vertices = vertices;
       //ColocaModel(gl,vertices);
       model3D = new Model3D(vertices);

    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        model3D.model3DTexture(gl,context);
        gl.glEnable(GL10.GL_TEXTURE_2D);  // Enable texture (NEW)
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if (height == 0) height = 1;   // To prevent divide by zero
        float aspect = (float)width / height;

        // Set the viewport (display area) to cover the entire window
        gl.glViewport(0, 0, width, height);

        // Setup perspective projection, with aspect ratio matches viewport
        gl.glMatrixMode(GL10.GL_PROJECTION); // Select projection matrix
        gl.glLoadIdentity();                 // Reset projection matrix
        // Use perspective projection
        GLU.gluPerspective(gl, 45, aspect, 0.1f, 100.f);


        gl.glMatrixMode(GL10.GL_MODELVIEW);  // Select model-view matrix
        gl.glLoadIdentity();
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -6.0f);
        //gl.glTranslatex(200, 500, -6);
        //gl.glRotatef(angleCube, 0.1f, 1.0f, 0.2f);
        model3D = new Model3D(vertices);
        model3D.draw(gl,vertices);
    }
}

我的对象

public class Model3D {


    float[] texCoords = { // Texture coords for the above face (NEW)
            0.0f, 1.0f,  // A. left-bottom (NEW)
            1.0f, 1.0f,  // B. right-bottom (NEW)
            0.0f, 0.0f,  // C. left-top (NEW)
            1.0f, 0.0f   // D. right-top (NEW)
    };

    int[] textureIDs = new int[1]; //array para 1 textura

    private FloatBuffer vertexBuffer;
    private FloatBuffer texBuffer;

    public Model3D(float[] vertices){
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
        tbb.order(ByteOrder.nativeOrder());
        texBuffer = tbb.asFloatBuffer();
        texBuffer.put(texCoords);
        texBuffer.position(0);
    }
    public void model3DTexture(GL10 gl, Context context) {
        gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);   // Bind to texture ID
        // Set up texture filters
        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);

        // Construct an input stream to texture image "res\drawable\nehe.png"
        @SuppressLint("ResourceType") InputStream istream = context.getResources().openRawResource(R.drawable.image2);
        Bitmap bitmap;
        try {
            // Read and decode input as bitmap
            bitmap = BitmapFactory.decodeStream(istream);
        } finally {
            try {
                istream.close();
            } catch(IOException e) { }
        }

        // Build Texture from loaded bitmap for the currently-bind texture ID
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
    }

    public void draw(GL10 gl,float[] vertices){
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  // Enable texture-coords-array (NEW)
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer); // Define texture-coords buffer (NEW)

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

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,vertices.length/3);

        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}

我的电话

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyGLSurfaceView view = new MyGLSurfaceView(this,vertices);

        glView = new GLSurfaceView(this);
        glView = (GLSurfaceView) findViewById(R.id.glView);
        glView.setZOrderOnTop(true);
        glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        glView.setRenderer(view);

        glView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);


At this point is where I would like to make the vertex change


    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

...
...


These are my converted vertices 

    public float[] vertices = {
            xCP4, yCP4, 0.0f,
            xCP3, yCP3, 0.0f,
            xCP1, yCP1, 0.0f,
            xCP2, yCP2, 0.0f
    };

0 个答案:

没有答案