EasyAR Video in Swift

+3 votes
asked Jun 27, 2018 by emma93 (160 points)
Hi,

I downloaded the Swift project for EasyAR and added the VideoRenderer from the Objective C example.
Now I converted everything into Swift and have some problems:
The video plays, but I can only see a black plane. The sound of the video is there...

Does anybody have an example with video-rendering in Swift, or had the same problem?

Thanks!

2 Answers

+1 vote
answered Jun 28, 2018 by albert52 (31,850 points)
Check to see if the setRenderTexture method was successfully invoked

https://www.easyar.com/doc/EasyAR%20SDK/API%20Reference/2.0/VideoPlayer.html
commented Jun 29, 2018 by emma93 (160 points)
Thanks for your answer!
My openVideoFile function looks like this:

public func openFile(path: String?, texid: Int) {
        path_ = path ?? ""
        player?.setRenderTexture(OpaquePointer(UnsafeRawPointer(bitPattern: Int(texid))!))
        player?.setVideoType(VideoType.Normal)
        weak var weak_self: ARVideo? = self
        player?.open(path_, StorageType.Assets, videoStatus)
    }
commented Jul 3, 2018 by developerinfinx (110 points)
Can you please share VideoRenderer file for Swift.
commented Jul 3, 2018 by emma93 (160 points)
import OpenGLES
import EasyARSwift

internal class VideoRenderer {
    private var program_box: UInt32
    private var pos_coord_box: Int32
    private var pos_tex_box: Int32
    private var pos_trans_box: Int32
    private var pos_proj_box: Int32
    private var vbo_coord_box: UInt32 = 0
    private var vbo_faces_box: UInt32 = 0
    private var vbo_tex_box: UInt32 = 0
    private var texture_id: UInt32 = 0
    
    init() {
        let box_vert = "uniform mat4 trans;\n"
            + "uniform mat4 proj;\n"
            + "attribute vec4 coord;\n"
            + "attribute vec2 texcoord;\n"
            + "varying vec2 vtexcoord;\n"
            + "\n"
            + "void main(void)\n"
            + "{\n"
            + "    vtexcoord = texcoord;\n"
            + "    gl_Position = proj*trans*coord;\n"
            + "}\n"
            + "\n"
        
        let box_frag = "#ifdef GL_ES\n"
            + "precision highp float;\n"
            + "#endif\n"
            + "varying vec2 vtexcoord;\n"
            + "uniform sampler2D texture;\n"
            + "\n"
            + "void main(void)\n"
            + "{\n"
            + "    gl_FragColor = texture2D(texture, vtexcoord);\n"
            + "}\n"
            + "\n"
        
        program_box = glCreateProgram()
        let vertShader = glCreateShader(GLenum(GL_VERTEX_SHADER))
        box_vert.utf8CString.withUnsafeBufferPointer { p in
            var s = p.baseAddress
            glShaderSource(vertShader, 1, &s, nil)
        }
        glCompileShader(vertShader)
        let fragShader = glCreateShader(GLenum(GL_FRAGMENT_SHADER))
        box_frag.utf8CString.withUnsafeBufferPointer { p in
            var s = p.baseAddress
            glShaderSource(fragShader, 1, &s, nil)
        }
        glCompileShader(fragShader)
        glAttachShader(program_box, vertShader)
        glAttachShader(program_box, fragShader)
        glLinkProgram(program_box)
        glUseProgram(program_box)
        pos_coord_box = glGetAttribLocation(program_box, "coord")
        pos_tex_box = glGetAttribLocation(program_box, "texcoord")
        pos_trans_box = glGetUniformLocation(program_box, "trans")
        pos_proj_box = glGetUniformLocation(program_box, "proj")
        
        glGenBuffers(1, &vbo_coord_box)
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vbo_coord_box)
        let cube_vertices: [GLfloat] = [
            1.0 / 2, 1.0 / 2.0, 0.0,
            1.0 / 2, -1.0 / 2, 0.0,
            -1.0 / 2, -1.0 / 2, 0.0,
            -1.0 / 2, 1.0 / 2, 0.0
        ]
        cube_vertices.withUnsafeBytes { p in
            glBufferData(GLenum(GL_ARRAY_BUFFER), p.count, p.baseAddress, GLenum(GL_DYNAMIC_DRAW))
        }
        
        glGenBuffers(1, &vbo_tex_box)
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vbo_tex_box)
        let cube_vertex_colors: [GLint] = [
            0, 0,
            0, 1,
            1, 1,
            1, 0
        ]
        
        cube_vertex_colors.withUnsafeBytes { p in
            glBufferData(GLenum(GL_ARRAY_BUFFER), p.count, p.baseAddress, GLenum(GL_STATIC_DRAW))
        }

        glGenBuffers(1, &vbo_faces_box)
        glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), vbo_faces_box)
        let cube_faces: [GLushort] = [3, 2, 1, 0]
        cube_faces.withUnsafeBytes { p in
            glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER), p.count, p.baseAddress, GLenum(GL_STATIC_DRAW))
        }
        
        glGenTextures(1, &texture_id)
        glBindTexture(GLenum(GL_ELEMENT_ARRAY_BUFFER), texture_id)
        glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR)
        glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_LINEAR)
        glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GL_CLAMP_TO_EDGE)
        glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GL_CLAMP_TO_EDGE)
    }
    
    public func render(_ projectionMatrix: Matrix44F, _ cameraview: Matrix44F, _ size: Vec2F) {
        let (size0, size1) = size.data
        
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vbo_coord_box)
        
        let cube_vertices: [GLfloat] = [
            size0 / 2, size1 / 2, 0.0,
            size0 / 2, -size1 / 2, 0.0,
            -size0 / 2, -size1 / 2, 0.0,
            -size0 / 2, size1 / 2, 0.0
        ]
        cube_vertices.withUnsafeBytes { p in
            glBufferData(GLenum(GL_ARRAY_BUFFER), p.count, p.baseAddress, GLenum(GL_DYNAMIC_DRAW))
        }
        
        glEnable(GLenum(GL_BLEND))
        glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA))
        glEnable(GLenum(GL_DEPTH_TEST))
        glUseProgram(program_box)
        
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vbo_coord_box)
        glEnableVertexAttribArray(GLuint(pos_coord_box))
        glVertexAttribPointer(GLuint(pos_coord_box), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, nil)
        
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vbo_tex_box)
        glEnableVertexAttribArray(GLuint(UInt32(pos_tex_box)))
        glVertexAttribPointer(GLuint(pos_tex_box), 2, GLenum(GL_UNSIGNED_BYTE), GLboolean(GL_FALSE), 0, nil)
        
        var cameraview_data = cameraview.data
        var projectionMatrix_data = projectionMatrix.data
        withUnsafePointer(to: &cameraview_data) { p in
            glUniformMatrix4fv(pos_trans_box, 1, GLboolean(GL_FALSE), UnsafePointer<GLfloat>(OpaquePointer(p)))
        }
        withUnsafePointer(to: &projectionMatrix_data) { p in
            glUniformMatrix4fv(pos_proj_box, 1, GLboolean(GL_FALSE), UnsafePointer<GLfloat>(OpaquePointer(p)))
        }
        
        glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), vbo_faces_box)
        glActiveTexture(GLenum(GL_TEXTURE0))
        glBindTexture(GLenum(GL_TEXTURE_2D), texture_id)
        glDrawElements(GLenum(GL_TRIANGLE_FAN), 4, GLenum(GL_UNSIGNED_SHORT), nil)
        glBindTexture(GLenum(GL_TEXTURE_2D), 0)
    }
    
    public func texid() -> GLuint {
        return texture_id
    }
    

}
commented Jul 4, 2018 by developerinfinx (110 points)
Have you converted ARVideo and HelloAR file too ? if then please share them too. Thanks.
commented Feb 1, 2019 by maxvarodom (110 points)
Can you please share `HelloAR` file for Swift. please TT
+3 votes
answered Jul 29, 2018 by sbeck27 (700 points)
Hi Emma, could you please share your code for updated HelloAR and ARVideo swift files?

Thanks, Sven
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...