OpenGL ES (Swift) Dev 4

OpenGL ES (Swift) 紋理的簡單應(yīng)用

上一節(jié)中用一張圖片來渲染圖形,這次則對上一節(jié)渲染的圖形進(jìn)行簡單的動畫和添加濾鏡,先上圖:

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,主要就是封裝重用設(shè)置紋理屬性的代碼。

方法2:

//重新加載緩存數(shù)據(jù)
    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)
        );
    }

這個方法就不再做解釋了,這是前面章節(jié)內(nèi)容中的AGLKVertexAttribArrayBuffer里面的方法之一,主要就是重新加載緩存數(shù)據(jù)。

添加濾鏡和動畫

在這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

簡單來說,它的作用就是設(shè)置每秒的刷新次數(shù),而且還會觸發(fā)update()方法,刷新多少次,同時就會調(diào)用多少次update()方法。所以我們設(shè)置動畫和濾鏡的動作就實現(xiàn)在update()方法中:

func update() {
        //添加濾鏡
        updateTextureParameters()
        //添加動畫
        updateAnimatedVertexPositions()
        //重新加載緩存數(shù)據(jù)
        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
        }
    }

這里只是實現(xiàn)簡單的旋轉(zhuǎn),拋磚引玉的,可以多去嘗試其它方法~~~

源碼點這里,喜歡就點個喜歡、加個關(guān)注唄,持續(xù)更新中。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容