@@ -20,32 +20,35 @@ import AVFoundation
20
20
struct Vertex {
21
21
var Position : ( Float , Float , Float )
22
22
var Color : ( Float , Float , Float , Float )
23
+ var TexCoord : ( Float , Float )
23
24
}
24
25
25
26
class OpenGLView : UIView {
26
27
var _context : EAGLContext ?
27
28
var _colorRenderBuffer = GLuint ( )
28
29
var _colorSlot = GLuint ( )
29
- var _texCoordSlot = GLuint ( )
30
30
var _currentRotation = Float ( )
31
31
var _depthRenderBuffer = GLuint ( )
32
32
var _eaglLayer : CAEAGLLayer ?
33
33
var _modelViewUniform = GLuint ( )
34
34
var _positionSlot = GLuint ( )
35
35
var _projectionUniform = GLuint ( )
36
+
37
+ var _floorTexture = GLuint ( )
38
+ var _fishTexture = GLuint ( )
39
+ var _texCoordSlot = GLuint ( )
36
40
var _textureUniform = GLuint ( )
37
- var _textureID = GLuint ( )
38
- var spriteImage = UIImage ( named: " mountain.jpg " ) !. cgImage!;
41
+
39
42
40
43
var _vertices = [
41
- Vertex ( Position: ( 1 , - 1 , 0 ) , Color: ( 1 , 0 , 0 , 1 ) ) ,
42
- Vertex ( Position: ( 1 , 1 , 0 ) , Color: ( 1 , 0 , 0 , 1 ) ) ,
43
- Vertex ( Position: ( - 1 , 1 , 0 ) , Color: ( 0 , 1 , 0 , 1 ) ) ,
44
- Vertex ( Position: ( - 1 , - 1 , 0 ) , Color: ( 0 , 1 , 0 , 1 ) ) ,
45
- Vertex ( Position: ( 1 , - 1 , - 1 ) , Color: ( 1 , 0 , 0 , 1 ) ) ,
46
- Vertex ( Position: ( 1 , 1 , - 1 ) , Color: ( 1 , 0 , 0 , 1 ) ) ,
47
- Vertex ( Position: ( - 1 , 1 , - 1 ) , Color: ( 0 , 1 , 0 , 1 ) ) ,
48
- Vertex ( Position: ( - 1 , - 1 , - 1 ) , Color: ( 0 , 1 , 0 , 1 ) )
44
+ Vertex ( Position: ( 1 , - 1 , 0 ) , Color: ( 1 , 0 , 0 , 1 ) , TexCoord : ( 1 , 0 ) ) ,
45
+ Vertex ( Position: ( 1 , 1 , 0 ) , Color: ( 1 , 0 , 0 , 1 ) , TexCoord : ( 1 , 1 ) ) ,
46
+ Vertex ( Position: ( - 1 , 1 , 0 ) , Color: ( 0 , 1 , 0 , 1 ) , TexCoord : ( 0 , 1 ) ) ,
47
+ Vertex ( Position: ( - 1 , - 1 , 0 ) , Color: ( 0 , 1 , 0 , 1 ) , TexCoord : ( 0 , 0 ) ) ,
48
+ Vertex ( Position: ( 1 , - 1 , - 1 ) , Color: ( 1 , 0 , 0 , 1 ) , TexCoord : ( 1 , 0 ) ) ,
49
+ Vertex ( Position: ( 1 , 1 , - 1 ) , Color: ( 1 , 0 , 0 , 1 ) , TexCoord : ( 1 , 1 ) ) ,
50
+ Vertex ( Position: ( - 1 , 1 , - 1 ) , Color: ( 0 , 1 , 0 , 1 ) , TexCoord : ( 0 , 1 ) ) ,
51
+ Vertex ( Position: ( - 1 , - 1 , - 1 ) , Color: ( 0 , 1 , 0 , 1 ) , TexCoord : ( 0 , 0 ) )
49
52
]
50
53
51
54
var _indices : [ GLubyte ] = [
@@ -193,13 +196,19 @@ class OpenGLView: UIView {
193
196
glUseProgram ( program)
194
197
195
198
_positionSlot = GLuint ( glGetAttribLocation ( program, " Position " ) )
196
- _colorSlot = GLuint ( glGetAttribLocation ( program, " SourceColor " ) )
197
- _texCoordSlot = GLuint ( glGetAttribLocation ( program, " TexCoordIn " ) )
198
- _textureUniform = GLuint ( glGetUniformLocation ( program, " Texture " ) )
199
199
glEnableVertexAttribArray ( _positionSlot)
200
+
201
+ _colorSlot = GLuint ( glGetAttribLocation ( program, " SourceColor " ) )
200
202
glEnableVertexAttribArray ( _colorSlot)
203
+
204
+ _texCoordSlot = GLuint ( glGetAttribLocation ( program, " TexCoordIn " ) ) ;
201
205
glEnableVertexAttribArray ( _texCoordSlot) ;
202
206
207
+ _textureUniform = GLuint ( glGetUniformLocation ( program, " Texture " ) ) ;
208
+
209
+ _floorTexture = self . setupTexture ( fileName: " tile_floor.png " ) ;
210
+ _fishTexture = self . setupTexture ( fileName: " item_powerup_fish.png " ) ;
211
+
203
212
_projectionUniform = GLuint ( glGetUniformLocation ( program, " Projection " ) )
204
213
_modelViewUniform = GLuint ( glGetUniformLocation ( program, " Modelview " ) )
205
214
@@ -236,13 +245,13 @@ class OpenGLView: UIView {
236
245
let colorSlotFirstComponent = UnsafePointer < Int > ( bitPattern: sizeof ( Float) * 3 )
237
246
glVertexAttribPointer ( _colorSlot, 4 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , colorSlotFirstComponent)
238
247
239
- glEnableVertexAttribArray ( _texCoordSlot )
240
- let texCoordFirstComponent = UnsafePointer < Int > ( bitPattern : sizeof ( Float ) * 3 )
241
- glVertexAttribPointer ( _texCoordSlot , 2 , GLenum ( GL_FLOAT ) , GLboolean ( GL_FALSE ) , Int32 ( sizeof ( Vertex) ) , texCoordFirstComponent )
248
+ let vertexSlotFirstComponent = UnsafePointer < Int > ( bitPattern : sizeof ( GLfloat ) * 7 )
249
+ glVertexAttribPointer ( _texCoordSlot , 2 , GLenum ( GL_FLOAT ) , GLboolean ( GL_FALSE ) ,
250
+ GLsizei ( sizeof ( Vertex) ) , vertexSlotFirstComponent ) ;
242
251
243
- glActiveTexture ( UInt32 ( GL_TEXTURE0) )
244
- glBindTexture ( GLenum ( GL_TEXTURE_2D) , _textureID )
245
- glUniform1i ( GLint ( _textureUniform) , 0 )
252
+ glActiveTexture ( GLenum ( GL_TEXTURE0) ) ;
253
+ glBindTexture ( GLenum ( GL_TEXTURE_2D) , _floorTexture ) ;
254
+ glUniform1i ( GLint ( _textureUniform) , 0 ) ;
246
255
247
256
248
257
let vertexBufferOffset = UnsafeMutablePointer < Void > ( bitPattern: 0 )
@@ -301,6 +310,59 @@ class OpenGLView: UIView {
301
310
return 0
302
311
}
303
312
313
+ func setupTexture( fileName: String ) -> GLuint {
314
+ let spriteImage : CGImage ? = UIImage ( named: fileName) ? . cgImage
315
+ if ( spriteImage == nil ) {
316
+ print ( " Failed to load image! " )
317
+ exit ( 1 )
318
+ }
319
+ let width : Int = spriteImage!. width
320
+ let height : Int = spriteImage!. height
321
+ let spriteData = UnsafeMutablePointer < GLubyte > ( calloc ( Int ( UInt ( CGFloat ( width) * CGFloat( height) * 4 ) ) , sizeof ( GLubyte) ) )
322
+
323
+ let spriteContext : CGContext = CGContext ( data: spriteData, width: width, height: height, bitsPerComponent: 8 , bytesPerRow: width*4, space: spriteImage!. colorSpace!, bitmapInfo: CGImageAlphaInfo . premultipliedLast. rawValue) !
324
+ spriteContext. draw ( in: CGRect ( x: 0 , y: 0 , width: CGFloat ( width) , height: CGFloat ( height) ) , image: spriteImage!)
325
+ var texName : GLuint = GLuint ( )
326
+ glGenTextures ( 1 , & texName)
327
+ glBindTexture ( GLenum ( GL_TEXTURE_2D) , texName)
328
+
329
+ glTexParameteri ( GLenum ( GL_TEXTURE_2D) , GLenum ( GL_TEXTURE_MIN_FILTER) , GL_NEAREST)
330
+ glTexImage2D ( GLenum ( GL_TEXTURE_2D) , 0 , GL_RGBA, GLsizei ( width) , GLsizei ( height) , 0 , GLenum ( GL_RGBA) , UInt32 ( GL_UNSIGNED_BYTE) , spriteData)
331
+
332
+ free ( spriteData)
333
+ return texName
334
+
335
+ }
336
+
337
+ // func getTextureFromImageWithName(fileName: String) -> GLuint {
338
+ //
339
+ // let spriteImage: CGImage? = UIImage(named: fileName)!.cgImage
340
+ //
341
+ // if (spriteImage == nil) {
342
+ // print("Failed to load image!")
343
+ // exit(1)
344
+ // }
345
+ //
346
+ // let width: Int = spriteImage!.width
347
+ // let height: Int = spriteImage!.height
348
+ // let spriteData = UnsafeMutablePointer<GLubyte>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte)))
349
+ //
350
+ // let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
351
+ // let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage!.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
352
+ //
353
+ // spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage!)
354
+ //// CGContextRelease(spriteContext)
355
+ //
356
+ // var texName: GLuint = GLuint()
357
+ // glGenTextures(1, &texName)
358
+ // glBindTexture(GLenum(GL_TEXTURE_2D), texName)
359
+ //
360
+ // glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
361
+ // glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
362
+ //
363
+ // free(spriteData)
364
+ // return texName
365
+ // }
304
366
305
367
func setupRenderBuffer( ) -> Int {
306
368
glGenRenderbuffers ( 1 , & _colorRenderBuffer)
0 commit comments