OpenGL ES (Swift) Dev 4

作者: YxxxHao | 来源:发表于2016-07-14 14:04 被阅读328次

    OpenGL ES (Swift) 纹理的简单应用

    上一节中用一张图片来渲染图形,这次则对上一节渲染的图形进行简单的动画和添加滤镜,先上图:

    5F8C3489-54F3-435D-A133-F1F921B3413E.png

    核心方法 aglkSetParameter

    方法1:

    extension GLKEffectPropertyTexture {
        func aglkSetParameter(parameterID: GLenum, value: GLint) {
            glBindTexture(self.target.rawValue, self.name);
            
            glTexParameteri(
                self.target.rawValue,
                parameterID,
                value);
        }
    }
    

    该方法是GLKEffectPropertyTexture的extension,主要就是封装重用设置纹理属性的代码。

    方法2:

    //重新加载缓存数据
        func reinitWithAttribStride(stride: GLsizei, numberOfVertices:GLsizei, dataPtr: UnsafePointer<Void>) {
            self.stride = stride;
            bufferSizeBytes = Int(stride) * Int(numberOfVertices);
            
            glBindBuffer(GLenum(GL_ARRAY_BUFFER),
                         self.bufferId);
            glBufferData(
                GLenum(GL_ARRAY_BUFFER),
                bufferSizeBytes,
                dataPtr,
                GLenum(GL_DYNAMIC_DRAW)
            );
        }
    

    这个方法就不再做解释了,这是前面章节内容中的AGLKVertexAttribArrayBuffer里面的方法之一,主要就是重新加载缓存数据。

    添加滤镜和动画

    在这GLKViewController有一个重要的属性必须要先介绍下:

    /*
        For setting the desired frames per second at which the update and drawing will take place.
        The default is 30.
      */
     public var preferredFramesPerSecond: Int
    

    简单来说,它的作用就是设置每秒的刷新次数,而且还会触发update()方法,刷新多少次,同时就会调用多少次update()方法。所以我们设置动画和滤镜的动作就实现在update()方法中:

    func update() {
            //添加滤镜
            updateTextureParameters()
            //添加动画
            updateAnimatedVertexPositions()
            //重新加载缓存数据
            self.vertextBuffer.reinitWithAttribStride(GLsizei(sizeofValue(vertices[0])), numberOfVertices: GLsizei(vertices.count), dataPtr: vertices)
        }
    

    添加滤镜方法:

    func updateTextureParameters() {
            self.baseEffect.texture2d0.aglkSetParameter(GLenum(GL_TEXTURE_MAG_FILTER), value: self.shouldUseLinearFilter ? GL_LINEAR : GL_NEAREST)
        }
    

    添加动画方法:

    func updateAnimatedVertexPositions() {
            if shouldAnimate {
                for index in 0...3 {
                    vertices[index].positionCoords = GLKVector3Make(vertices[index].positionCoords.x + movementVectors[index][0], vertices[index].positionCoords.y, vertices[index].positionCoords.z)
                    
                    if vertices[index].positionCoords.x >= 1.0 ||
                        vertices[index].positionCoords.x <= -1.0
                    {
                        movementVectors[index] = GLKVector3Make(-movementVectors[index][0], movementVectors[index][1], movementVectors[index][2]);
                    }
                    
                    if vertices[index].positionCoords.y >= 1.0 ||
                        vertices[index].positionCoords.y <= -1.0
                    {
                        movementVectors[index] = GLKVector3Make(movementVectors[index][0], -movementVectors[index][1], movementVectors[index][2]);
                    }
                    
                    if vertices[index].positionCoords.z >= 1.0 ||
                        vertices[index].positionCoords.z <= -1.0
                    {
                        movementVectors[index] = GLKVector3Make(movementVectors[index][0], movementVectors[index][1], -movementVectors[index][2]);
                    }
                }
            } else {
                vertices = DefaultVertices
            }
        }
    

    这里只是实现简单的旋转,抛砖引玉的,可以多去尝试其它方法~~~

    源码点这里,喜欢就点个喜欢、加个关注呗,持续更新中。

    相关文章

      网友评论

        本文标题:OpenGL ES (Swift) Dev 4

        本文链接:https://www.haomeiwen.com/subject/xnmejttx.html