@@ -23,7 +23,7 @@ struct Vertex {
23
23
var TexCoord : ( Float , Float )
24
24
}
25
25
26
- let TEX_COORD_MAX : Float = 1.0
26
+ let TEX_COORD_MAX : Float = 4
27
27
28
28
class OpenGLView : UIView {
29
29
var _context : EAGLContext ?
@@ -36,11 +36,17 @@ class OpenGLView: UIView {
36
36
var _positionSlot = GLuint ( )
37
37
var _projectionUniform = GLuint ( )
38
38
39
+ // texture
39
40
var _floorTexture = GLuint ( )
40
41
var _fishTexture = GLuint ( )
41
42
var _texCoordSlot = GLuint ( )
42
43
var _textureUniform = GLuint ( )
43
44
45
+ // buffers
46
+ var _vertexBuffer = GLuint ( )
47
+ var _indexBuffer = GLuint ( )
48
+ var _vertexBuffer2 = GLuint ( )
49
+ var _indexBuffer2 = GLuint ( )
44
50
45
51
var _vertices = [
46
52
// Front
@@ -73,7 +79,7 @@ class OpenGLView: UIView {
73
79
Vertex ( Position: ( 1 , - 1 , 0 ) , Color: ( 0 , 1 , 0 , 1 ) , TexCoord: ( TEX_COORD_MAX, TEX_COORD_MAX) ) ,
74
80
Vertex ( Position: ( - 1 , - 1 , 0 ) , Color: ( 0 , 0 , 1 , 1 ) , TexCoord: ( 0 , TEX_COORD_MAX) ) ,
75
81
Vertex ( Position: ( - 1 , - 1 , - 2 ) , Color: ( 0 , 0 , 0 , 1 ) , TexCoord: ( 0 , 0 ) ) ,
76
- ]
82
+ ]
77
83
78
84
var _indices : [ GLubyte ] = [
79
85
// Front
@@ -96,6 +102,18 @@ class OpenGLView: UIView {
96
102
22 , 23 , 20
97
103
]
98
104
105
+ var _vertices2 = [
106
+ Vertex ( Position: ( 0.5 , - 0.5 , 0.01 ) , Color: ( 1 , 1 , 1 , 1 ) , TexCoord: ( 1 , 1 ) ) ,
107
+ Vertex ( Position: ( 0.5 , 0.5 , 0.01 ) , Color: ( 1 , 1 , 1 , 1 ) , TexCoord: ( 1 , 0 ) ) ,
108
+ Vertex ( Position: ( - 0.5 , 0.5 , 0.01 ) , Color: ( 1 , 1 , 1 , 1 ) , TexCoord: ( 0 , 0 ) ) ,
109
+ Vertex ( Position: ( - 0.5 , - 0.5 , 0.01 ) , Color: ( 1 , 1 , 1 , 1 ) , TexCoord: ( 0 , 1 ) ) ,
110
+ ] ;
111
+
112
+ var _indices2 : [ GLubyte ] = [
113
+ 1 , 0 , 2 , 3
114
+ ] ;
115
+
116
+
99
117
override init ( frame: CGRect ) {
100
118
super. init ( frame: frame)
101
119
@@ -221,7 +239,7 @@ class OpenGLView: UIView {
221
239
222
240
_positionSlot = GLuint ( glGetAttribLocation ( program, " Position " ) )
223
241
glEnableVertexAttribArray ( _positionSlot)
224
-
242
+
225
243
_colorSlot = GLuint ( glGetAttribLocation ( program, " SourceColor " ) )
226
244
glEnableVertexAttribArray ( _colorSlot)
227
245
@@ -261,6 +279,11 @@ class OpenGLView: UIView {
261
279
glUniformMatrix4fv ( GLint ( _modelViewUniform) , 1 , 0 , modelView!. glMatrix)
262
280
glViewport ( 0 , 0 , GLsizei ( self . frame. size. width) , GLsizei ( self . frame. size. height) ) ;
263
281
282
+
283
+ // draw on first item
284
+ glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , _vertexBuffer) ;
285
+ glBindBuffer ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , _indexBuffer) ;
286
+
264
287
let positionSlotFirstComponent = UnsafePointer < Int > ( bitPattern: 0 )
265
288
glEnableVertexAttribArray ( _positionSlot)
266
289
glVertexAttribPointer ( _positionSlot, 3 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , positionSlotFirstComponent)
@@ -282,6 +305,23 @@ class OpenGLView: UIView {
282
305
glDrawElements ( GLenum ( GL_TRIANGLES) , GLsizei ( ( _indices. count * sizeof( GLubyte) ) / sizeof( GLubyte) ) ,
283
306
GLenum ( GL_UNSIGNED_BYTE) , vertexBufferOffset)
284
307
308
+ // draw on second item
309
+ glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , _vertexBuffer2) ;
310
+ glBindBuffer ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , _indexBuffer2) ;
311
+
312
+ glActiveTexture ( GLenum ( GL_TEXTURE0) ) ;
313
+ glBindTexture ( GLenum ( GL_TEXTURE_2D) , _fishTexture) ;
314
+ glUniform1i ( GLint ( _textureUniform) , 0 ) ;
315
+
316
+ glUniformMatrix4fv ( GLint ( _modelViewUniform) , 1 , 0 , modelView? . glMatrix) ;
317
+
318
+ glVertexAttribPointer ( _positionSlot, 3 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , positionSlotFirstComponent) ;
319
+ glVertexAttribPointer ( _colorSlot, 4 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , colorSlotFirstComponent) ;
320
+ glVertexAttribPointer ( _texCoordSlot, 2 , GLenum ( GL_FLOAT) , GLboolean ( GL_FALSE) , GLsizei ( sizeof ( Vertex) ) , vertexSlotFirstComponent) ;
321
+
322
+ glDrawElements ( GLenum ( GL_TRIANGLE_STRIP) , GLsizei ( ( _indices2. count * sizeof( GLubyte) ) / sizeof( GLubyte) ) , GLenum ( GL_UNSIGNED_BYTE) , positionSlotFirstComponent) ;
323
+
324
+
285
325
_context!. presentRenderbuffer ( Int ( GL_RENDERBUFFER) )
286
326
return 0
287
327
}
@@ -355,38 +395,38 @@ class OpenGLView: UIView {
355
395
356
396
free ( spriteData)
357
397
return texName
358
-
398
+
359
399
}
360
400
361
- // func getTextureFromImageWithName(fileName: String) -> GLuint {
362
- //
363
- // let spriteImage: CGImage? = UIImage(named: fileName)!.cgImage
364
- //
365
- // if (spriteImage == nil) {
366
- // print("Failed to load image!")
367
- // exit(1)
368
- // }
369
- //
370
- // let width: Int = spriteImage!.width
371
- // let height: Int = spriteImage!.height
372
- // let spriteData = UnsafeMutablePointer<GLubyte>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte)))
373
- //
374
- // let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
375
- // let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage!.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
376
- //
377
- // spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage!)
378
- //// CGContextRelease(spriteContext)
379
- //
380
- // var texName: GLuint = GLuint()
381
- // glGenTextures(1, &texName)
382
- // glBindTexture(GLenum(GL_TEXTURE_2D), texName)
383
- //
384
- // glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
385
- // glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
386
- //
387
- // free(spriteData)
388
- // return texName
389
- // }
401
+ // func getTextureFromImageWithName(fileName: String) -> GLuint {
402
+ //
403
+ // let spriteImage: CGImage? = UIImage(named: fileName)!.cgImage
404
+ //
405
+ // if (spriteImage == nil) {
406
+ // print("Failed to load image!")
407
+ // exit(1)
408
+ // }
409
+ //
410
+ // let width: Int = spriteImage!.width
411
+ // let height: Int = spriteImage!.height
412
+ // let spriteData = UnsafeMutablePointer<GLubyte>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte)))
413
+ //
414
+ // let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
415
+ // let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage!.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
416
+ //
417
+ // spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage!)
418
+ //// CGContextRelease(spriteContext)
419
+ //
420
+ // var texName: GLuint = GLuint()
421
+ // glGenTextures(1, &texName)
422
+ // glBindTexture(GLenum(GL_TEXTURE_2D), texName)
423
+ //
424
+ // glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
425
+ // glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
426
+ //
427
+ // free(spriteData)
428
+ // return texName
429
+ // }
390
430
391
431
func setupRenderBuffer( ) -> Int {
392
432
glGenRenderbuffers ( 1 , & _colorRenderBuffer)
@@ -408,15 +448,23 @@ class OpenGLView: UIView {
408
448
}
409
449
410
450
func setupVBOs( ) -> Int {
411
- var vertexBuffer = GLuint ( )
412
- glGenBuffers ( 1 , & vertexBuffer )
413
- glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , vertexBuffer )
451
+ // first object
452
+ glGenBuffers ( 1 , & _vertexBuffer )
453
+ glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , _vertexBuffer )
414
454
glBufferData ( GLenum ( GL_ARRAY_BUFFER) , ( _vertices. count * sizeof( Vertex) ) , _vertices, GLenum ( GL_STATIC_DRAW) )
415
455
416
- var indexBuffer = GLuint ( )
417
- glGenBuffers ( 1 , & indexBuffer)
418
- glBindBuffer ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , indexBuffer)
456
+ glGenBuffers ( 1 , & _indexBuffer)
457
+ glBindBuffer ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , _indexBuffer)
419
458
glBufferData ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , ( _indices. count * sizeof( GLubyte) ) , _indices, GLenum ( GL_STATIC_DRAW) )
459
+
460
+ // second object
461
+ glGenBuffers ( 1 , & _vertexBuffer2)
462
+ glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , _vertexBuffer2)
463
+ glBufferData ( GLenum ( GL_ARRAY_BUFFER) , ( _vertices2. count * sizeof( Vertex) ) , _vertices2, GLenum ( GL_STATIC_DRAW) )
464
+
465
+ glGenBuffers ( 1 , & _indexBuffer2)
466
+ glBindBuffer ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , _indexBuffer2)
467
+ glBufferData ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , ( _indices2. count * sizeof( GLubyte) ) , _indices2, GLenum ( GL_STATIC_DRAW) )
420
468
return 0
421
469
}
422
470
0 commit comments