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ù)更新中。