@@ -14,6 +14,8 @@ import UIKit
14
14
import QuartzCore
15
15
import OpenGLES
16
16
import GLKit
17
+ import CoreMedia
18
+ import AVFoundation
17
19
18
20
struct Vertex {
19
21
var Position : ( Float , Float , Float )
@@ -24,12 +26,16 @@ class OpenGLView: UIView {
24
26
var _context : EAGLContext ?
25
27
var _colorRenderBuffer = GLuint ( )
26
28
var _colorSlot = GLuint ( )
29
+ var _texCoordSlot = GLuint ( )
27
30
var _currentRotation = Float ( )
28
31
var _depthRenderBuffer = GLuint ( )
29
32
var _eaglLayer : CAEAGLLayer ?
30
33
var _modelViewUniform = GLuint ( )
31
34
var _positionSlot = GLuint ( )
32
35
var _projectionUniform = GLuint ( )
36
+ var _textureUniform = GLuint ( )
37
+ var _textureID = GLuint ( )
38
+ var spriteImage = UIImage ( named: " mountain.jpg " ) !. cgImage!;
33
39
34
40
var _vertices = [
35
41
Vertex ( Position: ( 1 , - 1 , 0 ) , Color: ( 1 , 0 , 0 , 1 ) ) ,
@@ -97,6 +103,9 @@ class OpenGLView: UIView {
97
103
if ( self . setupDisplayLink ( ) != 0 ) {
98
104
NSLog ( " OpenGLView init(): setupDisplayLink() failed " )
99
105
}
106
+ // if (self.setupTexture() != 0) {
107
+ // NSLog("OpenGLView init(): setupTexture() failed")
108
+ // }
100
109
}
101
110
102
111
required init ? ( coder aDecoder: NSCoder ) {
@@ -108,7 +117,7 @@ class OpenGLView: UIView {
108
117
}
109
118
110
119
func compileShader( shaderName: String , shaderType: GLenum , shader: UnsafeMutablePointer < GLuint > ) -> Int {
111
- let shaderPath = Bundle . main ( ) . pathForResource ( shaderName, ofType: " glsl " )
120
+ let shaderPath = Bundle . main. pathForResource ( shaderName, ofType: " glsl " )
112
121
var error : NSError ?
113
122
let shaderString : NSString ?
114
123
do {
@@ -188,8 +197,11 @@ class OpenGLView: UIView {
188
197
189
198
_positionSlot = GLuint ( glGetAttribLocation ( program, " Position " ) )
190
199
_colorSlot = GLuint ( glGetAttribLocation ( program, " SourceColor " ) )
200
+ _texCoordSlot = GLuint ( glGetAttribLocation ( program, " TexCoordIn " ) )
201
+ _textureUniform = GLuint ( glGetUniformLocation ( program, " Texture " ) )
191
202
glEnableVertexAttribArray ( _positionSlot)
192
203
glEnableVertexAttribArray ( _colorSlot)
204
+ glEnableVertexAttribArray ( _texCoordSlot) ;
193
205
194
206
_projectionUniform = GLuint ( glGetUniformLocation ( program, " Projection " ) )
195
207
_modelViewUniform = GLuint ( glGetUniformLocation ( program, " Modelview " ) )
@@ -219,14 +231,23 @@ class OpenGLView: UIView {
219
231
glUniformMatrix4fv ( GLint ( _modelViewUniform) , 1 , 0 , modelView!. glMatrix)
220
232
glViewport ( 0 , 0 , GLsizei ( self . frame. size. width) , GLsizei ( self . frame. size. height) ) ;
221
233
222
- let positionSlotFirstComponent = UnsafePointer < Int > ( bitPattern: 0 )
234
+ let positionSlotFirstComponent = UnsafePointer < Int > ( bitPattern: 0 )
223
235
glEnableVertexAttribArray ( _positionSlot)
224
236
glVertexAttribPointer ( _positionSlot, 3 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , positionSlotFirstComponent)
225
237
226
238
glEnableVertexAttribArray ( _colorSlot)
227
- let colorSlotFirstComponent = UnsafePointer < Int > ( bitPattern: sizeof ( Float) * 3 )
239
+ let colorSlotFirstComponent = UnsafePointer < Int > ( bitPattern: sizeof ( Float) * 3 )
228
240
glVertexAttribPointer ( _colorSlot, 4 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , colorSlotFirstComponent)
229
241
242
+ glEnableVertexAttribArray ( _texCoordSlot)
243
+ let texCoordFirstComponent = UnsafePointer < Int > ( bitPattern: sizeof ( Float) * 3 )
244
+ glVertexAttribPointer ( _texCoordSlot, 2 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , Int32 ( sizeof ( Vertex) ) , texCoordFirstComponent)
245
+
246
+ glActiveTexture ( UInt32 ( GL_TEXTURE0) )
247
+ glBindTexture ( GLenum ( GL_TEXTURE_2D) , _textureID)
248
+ glUniform1i ( GLint ( _textureUniform) , 0 )
249
+
250
+
230
251
let vertexBufferOffset = UnsafeMutablePointer < Void > ( bitPattern: 0 )
231
252
glDrawElements ( GLenum ( GL_TRIANGLES) , GLsizei ( ( _indices. count * sizeof( GLubyte) ) / sizeof( GLubyte) ) ,
232
253
GLenum ( GL_UNSIGNED_BYTE) , vertexBufferOffset)
@@ -259,7 +280,7 @@ class OpenGLView: UIView {
259
280
260
281
func setupDisplayLink( ) -> Int {
261
282
let displayLink : CADisplayLink = CADisplayLink ( target: self , selector: #selector( OpenGLView . render ( displayLink: ) ) )
262
- displayLink. add ( to: RunLoop . current ( ) , forMode: RunLoopMode . defaultRunLoopMode. rawValue)
283
+ displayLink. add ( to: RunLoop . current, forMode: RunLoopMode ( rawValue : RunLoopMode . defaultRunLoopMode. rawValue) )
263
284
return 0
264
285
}
265
286
@@ -283,6 +304,60 @@ class OpenGLView: UIView {
283
304
return 0
284
305
}
285
306
307
+ // func setupTexture() -> Int {
308
+ // let width: Int = spriteImage.width
309
+ // let height: Int = spriteImage.height
310
+ // let spriteData = UnsafeMutablePointer<Void>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte.self)))
311
+ //
312
+ // let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
313
+ // let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
314
+ //
315
+ // spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage)
316
+ //// CGContextRelease(spriteContext)
317
+ //
318
+ // var texName: GLuint = GLuint()
319
+ // glGenTextures(1, &texName)
320
+ // glBindTexture(GLenum(GL_TEXTURE_2D), texName)
321
+ //
322
+ // glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
323
+ // glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
324
+ //
325
+ // free(spriteData)
326
+ // _textureID = texName
327
+ // _textureID = getTextureFromImageWithName(fileName: "mountain.jpg")
328
+ // return 0
329
+ // }
330
+
331
+ // func getTextureFromImageWithName(fileName: String) -> GLuint {
332
+ //
333
+ // let spriteImage: CGImage? = UIImage(named: fileName)!.cgImage
334
+ //
335
+ // if (spriteImage == nil) {
336
+ // print("Failed to load image!")
337
+ // exit(1)
338
+ // }
339
+ //
340
+ // let width: Int = spriteImage!.width
341
+ // let height: Int = spriteImage!.height
342
+ // let spriteData = UnsafeMutablePointer<GLubyte>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte)))
343
+ //
344
+ // let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
345
+ // let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage!.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
346
+ //
347
+ // spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage!)
348
+ //// CGContextRelease(spriteContext)
349
+ //
350
+ // var texName: GLuint = GLuint()
351
+ // glGenTextures(1, &texName)
352
+ // glBindTexture(GLenum(GL_TEXTURE_2D), texName)
353
+ //
354
+ // glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
355
+ // glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
356
+ //
357
+ // free(spriteData)
358
+ // return texName
359
+ // }
360
+
286
361
func setupRenderBuffer( ) -> Int {
287
362
glGenRenderbuffers ( 1 , & _colorRenderBuffer)
288
363
glBindRenderbuffer ( GLenum ( GL_RENDERBUFFER) , _colorRenderBuffer)
0 commit comments