Skip to content

Commit d672184

Browse files
Guillaume SabranGuillaume Sabran
authored andcommitted
add textures
1 parent 20451a9 commit d672184

File tree

7 files changed

+96
-24
lines changed

7 files changed

+96
-24
lines changed

HelloOpenGL_Swift.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
21D0C3DE1B8CDEA300E51C72 /* CC3Math.m in Sources */ = {isa = PBXBuildFile; fileRef = 21D0C3D91B8CDEA300E51C72 /* CC3Math.m */; };
2323
21D0C3E01B8CDEC700E51C72 /* OpenGLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D0C3DF1B8CDEC700E51C72 /* OpenGLView.swift */; };
2424
6FB8CAF71D2EEBA3005B634B /* mountain.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 6FB8CAF61D2EEBA3005B634B /* mountain.jpg */; };
25+
6FB8CAFA1D2F0FFD005B634B /* item_powerup_fish.png in Resources */ = {isa = PBXBuildFile; fileRef = 6FB8CAF81D2F0FFD005B634B /* item_powerup_fish.png */; };
26+
6FB8CAFB1D2F0FFD005B634B /* tile_floor.png in Resources */ = {isa = PBXBuildFile; fileRef = 6FB8CAF91D2F0FFD005B634B /* tile_floor.png */; };
2527
/* End PBXBuildFile section */
2628

2729
/* Begin PBXContainerItemProxy section */
@@ -70,6 +72,8 @@
7072
21D0C3DA1B8CDEA300E51C72 /* ccTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ccTypes.h; sourceTree = "<group>"; };
7173
21D0C3DF1B8CDEC700E51C72 /* OpenGLView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGLView.swift; sourceTree = "<group>"; };
7274
6FB8CAF61D2EEBA3005B634B /* mountain.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = mountain.jpg; sourceTree = "<group>"; };
75+
6FB8CAF81D2F0FFD005B634B /* item_powerup_fish.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = item_powerup_fish.png; sourceTree = "<group>"; };
76+
6FB8CAF91D2F0FFD005B634B /* tile_floor.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tile_floor.png; sourceTree = "<group>"; };
7377
/* End PBXFileReference section */
7478

7579
/* Begin PBXFrameworksBuildPhase section */
@@ -127,6 +131,8 @@
127131
21D0C3A01B8CDE4000E51C72 /* Main.storyboard */,
128132
21D0C3A31B8CDE4000E51C72 /* Assets.xcassets */,
129133
6FB8CAF61D2EEBA3005B634B /* mountain.jpg */,
134+
6FB8CAF81D2F0FFD005B634B /* item_powerup_fish.png */,
135+
6FB8CAF91D2F0FFD005B634B /* tile_floor.png */,
130136
21D0C3A51B8CDE4000E51C72 /* LaunchScreen.storyboard */,
131137
21D0C3A81B8CDE4000E51C72 /* Info.plist */,
132138
21D0C3CA1B8CDE6E00E51C72 /* Cocos3DMathLib */,
@@ -285,6 +291,8 @@
285291
isa = PBXResourcesBuildPhase;
286292
buildActionMask = 2147483647;
287293
files = (
294+
6FB8CAFB1D2F0FFD005B634B /* tile_floor.png in Resources */,
295+
6FB8CAFA1D2F0FFD005B634B /* item_powerup_fish.png in Resources */,
288296
21D0C3A71B8CDE4000E51C72 /* LaunchScreen.storyboard in Resources */,
289297
21D0C3CE1B8CDE9100E51C72 /* SimpleVertex.glsl in Resources */,
290298
6FB8CAF71D2EEBA3005B634B /* mountain.jpg in Resources */,

HelloOpenGL_Swift/OpenGLView.swift

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,35 @@ import AVFoundation
2020
struct Vertex {
2121
var Position: (Float, Float, Float)
2222
var Color: (Float, Float, Float, Float)
23+
var TexCoord: (Float, Float)
2324
}
2425

2526
class OpenGLView: UIView {
2627
var _context: EAGLContext?
2728
var _colorRenderBuffer = GLuint()
2829
var _colorSlot = GLuint()
29-
var _texCoordSlot = GLuint()
3030
var _currentRotation = Float()
3131
var _depthRenderBuffer = GLuint()
3232
var _eaglLayer: CAEAGLLayer?
3333
var _modelViewUniform = GLuint()
3434
var _positionSlot = GLuint()
3535
var _projectionUniform = GLuint()
36+
37+
var _floorTexture = GLuint()
38+
var _fishTexture = GLuint()
39+
var _texCoordSlot = GLuint()
3640
var _textureUniform = GLuint()
37-
var _textureID = GLuint()
38-
var spriteImage = UIImage(named: "mountain.jpg")!.cgImage!;
41+
3942

4043
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))
4952
]
5053

5154
var _indices : [GLubyte] = [
@@ -193,13 +196,19 @@ class OpenGLView: UIView {
193196
glUseProgram(program)
194197

195198
_positionSlot = GLuint(glGetAttribLocation(program, "Position"))
196-
_colorSlot = GLuint(glGetAttribLocation(program, "SourceColor"))
197-
_texCoordSlot = GLuint(glGetAttribLocation(program, "TexCoordIn"))
198-
_textureUniform = GLuint(glGetUniformLocation(program, "Texture"))
199199
glEnableVertexAttribArray(_positionSlot)
200+
201+
_colorSlot = GLuint(glGetAttribLocation(program, "SourceColor"))
200202
glEnableVertexAttribArray(_colorSlot)
203+
204+
_texCoordSlot = GLuint(glGetAttribLocation(program, "TexCoordIn"));
201205
glEnableVertexAttribArray(_texCoordSlot);
202206

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+
203212
_projectionUniform = GLuint(glGetUniformLocation(program, "Projection"))
204213
_modelViewUniform = GLuint(glGetUniformLocation(program, "Modelview"))
205214

@@ -236,13 +245,13 @@ class OpenGLView: UIView {
236245
let colorSlotFirstComponent = UnsafePointer<Int>(bitPattern: sizeof(Float) * 3)
237246
glVertexAttribPointer(_colorSlot, 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), colorSlotFirstComponent)
238247

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);
242251

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);
246255

247256

248257
let vertexBufferOffset = UnsafeMutablePointer<Void>(bitPattern: 0)
@@ -301,6 +310,59 @@ class OpenGLView: UIView {
301310
return 0
302311
}
303312

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

305367
func setupRenderBuffer() -> Int {
306368
glGenRenderbuffers(1, &_colorRenderBuffer)

HelloOpenGL_Swift/SimpleFragment.glsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
uniform sampler2D Texture;
2-
varying mediump vec2 CameraTextureCoord;
2+
varying mediump vec2 TexCoordOut;
33
varying lowp vec4 DestinationColor;
44

55
void main(void) {
6-
gl_FragColor = vec4(DestinationColor.x * CameraTextureCoord.x, DestinationColor.y * CameraTextureCoord.y, DestinationColor.z, DestinationColor.w);}
6+
gl_FragColor = DestinationColor * texture2D(Texture, TexCoordOut);
7+
// gl_FragColor = vec4(DestinationColor.x * TexCoordOut.x, DestinationColor.y * TexCoordOut.y, DestinationColor.z, DestinationColor.w);
8+
}

HelloOpenGL_Swift/SimpleVertex.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ attribute vec4 SourceColor;
33
attribute vec2 TexCoordIn;
44

55
varying vec4 DestinationColor;
6-
varying vec2 CameraTextureCoord;
6+
varying vec2 TexCoordOut;
77

88
uniform mat4 Projection;
99
uniform mat4 Modelview;
1010

1111
void main(void) {
1212
DestinationColor = SourceColor;
1313
gl_Position = Projection * Modelview * Position;
14-
CameraTextureCoord = TexCoordIn;
14+
TexCoordOut = TexCoordIn;
1515
}
3.43 KB
Loading

HelloOpenGL_Swift/tile_floor.png

3.24 KB
Loading

0 commit comments

Comments
 (0)