Skip to content

Commit e5c87f6

Browse files
Guillaume SabranGuillaume Sabran
authored andcommitted
draw second image on top of cube
1 parent 9ee1785 commit e5c87f6

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

HelloOpenGL_Swift/OpenGLView.swift

Lines changed: 87 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct Vertex {
2323
var TexCoord: (Float, Float)
2424
}
2525

26-
let TEX_COORD_MAX : Float = 1.0
26+
let TEX_COORD_MAX : Float = 4
2727

2828
class OpenGLView: UIView {
2929
var _context: EAGLContext?
@@ -36,11 +36,17 @@ class OpenGLView: UIView {
3636
var _positionSlot = GLuint()
3737
var _projectionUniform = GLuint()
3838

39+
// texture
3940
var _floorTexture = GLuint()
4041
var _fishTexture = GLuint()
4142
var _texCoordSlot = GLuint()
4243
var _textureUniform = GLuint()
4344

45+
// buffers
46+
var _vertexBuffer = GLuint()
47+
var _indexBuffer = GLuint()
48+
var _vertexBuffer2 = GLuint()
49+
var _indexBuffer2 = GLuint()
4450

4551
var _vertices = [
4652
// Front
@@ -73,7 +79,7 @@ class OpenGLView: UIView {
7379
Vertex(Position: (1, -1, 0), Color: (0, 1, 0, 1), TexCoord: (TEX_COORD_MAX, TEX_COORD_MAX)),
7480
Vertex(Position: (-1, -1, 0), Color: (0, 0, 1, 1), TexCoord: (0, TEX_COORD_MAX)),
7581
Vertex(Position: (-1, -1, -2), Color: (0, 0, 0, 1), TexCoord: (0, 0)),
76-
]
82+
]
7783

7884
var _indices : [GLubyte] = [
7985
// Front
@@ -96,6 +102,18 @@ class OpenGLView: UIView {
96102
22, 23, 20
97103
]
98104

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+
99117
override init(frame: CGRect) {
100118
super.init(frame: frame)
101119

@@ -221,7 +239,7 @@ class OpenGLView: UIView {
221239

222240
_positionSlot = GLuint(glGetAttribLocation(program, "Position"))
223241
glEnableVertexAttribArray(_positionSlot)
224-
242+
225243
_colorSlot = GLuint(glGetAttribLocation(program, "SourceColor"))
226244
glEnableVertexAttribArray(_colorSlot)
227245

@@ -261,6 +279,11 @@ class OpenGLView: UIView {
261279
glUniformMatrix4fv(GLint(_modelViewUniform), 1, 0, modelView!.glMatrix)
262280
glViewport(0, 0, GLsizei(self.frame.size.width), GLsizei(self.frame.size.height));
263281

282+
283+
// draw on first item
284+
glBindBuffer(GLenum(GL_ARRAY_BUFFER), _vertexBuffer);
285+
glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), _indexBuffer);
286+
264287
let positionSlotFirstComponent = UnsafePointer<Int>(bitPattern: 0)
265288
glEnableVertexAttribArray(_positionSlot)
266289
glVertexAttribPointer(_positionSlot, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), positionSlotFirstComponent)
@@ -282,6 +305,23 @@ class OpenGLView: UIView {
282305
glDrawElements(GLenum(GL_TRIANGLES), GLsizei((_indices.count * sizeof(GLubyte))/sizeof(GLubyte)),
283306
GLenum(GL_UNSIGNED_BYTE), vertexBufferOffset)
284307

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+
285325
_context!.presentRenderbuffer(Int(GL_RENDERBUFFER))
286326
return 0
287327
}
@@ -355,38 +395,38 @@ class OpenGLView: UIView {
355395

356396
free(spriteData)
357397
return texName
358-
398+
359399
}
360400

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+
// }
390430

391431
func setupRenderBuffer() -> Int {
392432
glGenRenderbuffers(1, &_colorRenderBuffer)
@@ -408,15 +448,23 @@ class OpenGLView: UIView {
408448
}
409449

410450
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)
414454
glBufferData(GLenum(GL_ARRAY_BUFFER), (_vertices.count * sizeof(Vertex)), _vertices, GLenum(GL_STATIC_DRAW))
415455

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)
419458
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))
420468
return 0
421469
}
422470

0 commit comments

Comments
 (0)