├── .gitignore ├── README.md ├── SimpleFragment.glsl ├── SimpleVertex.glsl ├── example.png ├── iOSSwiftOpenGL.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── gitmo.xcuserdatad │ └── xcschemes │ ├── iOSSwiftOpenGL.xcscheme │ └── xcschememanagement.plist ├── iOSSwiftOpenGL ├── AppDelegate.swift ├── Base.lproj │ └── Main.storyboard ├── Images.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json ├── Info.plist ├── OpenGLView.swift ├── ViewController.swift └── iOSSwiftOpenGL.xcdatamodeld │ ├── .xccurrentversion │ └── iOSSwiftOpenGL.xcdatamodel │ └── contents └── iOSSwiftOpenGLTests ├── Info.plist └── iOSSwiftOpenGLTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 🚨🚨🚨 2 | Notice: This repository stopped working properly after changes were made early on to the Swift language. After efforts on my part to repair the breakages, I have decided to use Metal instead for my purposes. You can [see example code here](https://github.com/bradley/iOSSwiftMetal). 3 | 🚨🚨🚨 4 | 5 | #OpenGL for iOS using Swift 6 | 7 | ![Example](example.png) 8 | 9 | A basic example showing the use of OpenGL and GLSL Shaders in iOS using Swift. 10 | 11 | *The code in this project is a direct port (as best I could accomplish anyway) of of the Objective-C code for OpenGL in iOS outlined by Ray Wenderlich in his [OpenGL for iOS Tutorial](http://www.raywenderlich.com/3664/opengl-tutorial-for-ios-opengl-es-2-0).* 12 | -------------------------------------------------------------------------------- /SimpleFragment.glsl: -------------------------------------------------------------------------------- 1 | varying lowp vec4 DestinationColor; 2 | 3 | void main(void) { 4 | gl_FragColor = DestinationColor; 5 | } -------------------------------------------------------------------------------- /SimpleVertex.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 Position; 2 | attribute vec4 SourceColor; 3 | 4 | varying vec4 DestinationColor; 5 | 6 | void main(void) { 7 | DestinationColor = SourceColor; 8 | gl_Position = Position; 9 | } -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bradley/iOSSwiftOpenGL/f9e37bd70973d8bb88f3cc08c83f81b1560fe117/example.png -------------------------------------------------------------------------------- /iOSSwiftOpenGL.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | AD8814681960EEBF005F2E41 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8814671960EEBF005F2E41 /* AppDelegate.swift */; }; 11 | AD88146B1960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = AD8814691960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodeld */; }; 12 | AD88146D1960EEBF005F2E41 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD88146C1960EEBF005F2E41 /* ViewController.swift */; }; 13 | AD8814701960EEBF005F2E41 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD88146E1960EEBF005F2E41 /* Main.storyboard */; }; 14 | AD8814721960EEBF005F2E41 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AD8814711960EEBF005F2E41 /* Images.xcassets */; }; 15 | AD88147E1960EEBF005F2E41 /* iOSSwiftOpenGLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD88147D1960EEBF005F2E41 /* iOSSwiftOpenGLTests.swift */; }; 16 | AD88148A1960EF38005F2E41 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD8814891960EF38005F2E41 /* OpenGLES.framework */; }; 17 | AD88148C1960EFBA005F2E41 /* SimpleVertex.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AD88148B1960EFBA005F2E41 /* SimpleVertex.glsl */; }; 18 | AD88148E1960EFCE005F2E41 /* SimpleFragment.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AD88148D1960EFCE005F2E41 /* SimpleFragment.glsl */; }; 19 | AD8814901960F047005F2E41 /* Images.xcassets in Sources */ = {isa = PBXBuildFile; fileRef = AD8814711960EEBF005F2E41 /* Images.xcassets */; }; 20 | AD8814911960F27B005F2E41 /* OpenGLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8814871960EEDA005F2E41 /* OpenGLView.swift */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXContainerItemProxy section */ 24 | AD8814781960EEBF005F2E41 /* PBXContainerItemProxy */ = { 25 | isa = PBXContainerItemProxy; 26 | containerPortal = AD88145A1960EEBF005F2E41 /* Project object */; 27 | proxyType = 1; 28 | remoteGlobalIDString = AD8814611960EEBF005F2E41; 29 | remoteInfo = iOSSwiftOpenGL; 30 | }; 31 | /* End PBXContainerItemProxy section */ 32 | 33 | /* Begin PBXFileReference section */ 34 | AD8814621960EEBF005F2E41 /* iOSSwiftOpenGL.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iOSSwiftOpenGL.app; sourceTree = BUILT_PRODUCTS_DIR; }; 35 | AD8814661960EEBF005F2E41 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 36 | AD8814671960EEBF005F2E41 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 37 | AD88146A1960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = iOSSwiftOpenGL.xcdatamodel; sourceTree = ""; }; 38 | AD88146C1960EEBF005F2E41 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 39 | AD88146F1960EEBF005F2E41 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 40 | AD8814711960EEBF005F2E41 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 41 | AD8814771960EEBF005F2E41 /* iOSSwiftOpenGLTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = iOSSwiftOpenGLTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | AD88147C1960EEBF005F2E41 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 43 | AD88147D1960EEBF005F2E41 /* iOSSwiftOpenGLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSSwiftOpenGLTests.swift; sourceTree = ""; }; 44 | AD8814871960EEDA005F2E41 /* OpenGLView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGLView.swift; sourceTree = ""; }; 45 | AD8814891960EF38005F2E41 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; 46 | AD88148B1960EFBA005F2E41 /* SimpleVertex.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SimpleVertex.glsl; path = ../SimpleVertex.glsl; sourceTree = ""; }; 47 | AD88148D1960EFCE005F2E41 /* SimpleFragment.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SimpleFragment.glsl; path = ../SimpleFragment.glsl; sourceTree = ""; }; 48 | /* End PBXFileReference section */ 49 | 50 | /* Begin PBXFrameworksBuildPhase section */ 51 | AD88145F1960EEBF005F2E41 /* Frameworks */ = { 52 | isa = PBXFrameworksBuildPhase; 53 | buildActionMask = 2147483647; 54 | files = ( 55 | AD88148A1960EF38005F2E41 /* OpenGLES.framework in Frameworks */, 56 | ); 57 | runOnlyForDeploymentPostprocessing = 0; 58 | }; 59 | AD8814741960EEBF005F2E41 /* Frameworks */ = { 60 | isa = PBXFrameworksBuildPhase; 61 | buildActionMask = 2147483647; 62 | files = ( 63 | ); 64 | runOnlyForDeploymentPostprocessing = 0; 65 | }; 66 | /* End PBXFrameworksBuildPhase section */ 67 | 68 | /* Begin PBXGroup section */ 69 | AD8814591960EEBF005F2E41 = { 70 | isa = PBXGroup; 71 | children = ( 72 | AD8814891960EF38005F2E41 /* OpenGLES.framework */, 73 | AD8814641960EEBF005F2E41 /* iOSSwiftOpenGL */, 74 | AD88147A1960EEBF005F2E41 /* iOSSwiftOpenGLTests */, 75 | AD8814631960EEBF005F2E41 /* Products */, 76 | ); 77 | sourceTree = ""; 78 | }; 79 | AD8814631960EEBF005F2E41 /* Products */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | AD8814621960EEBF005F2E41 /* iOSSwiftOpenGL.app */, 83 | AD8814771960EEBF005F2E41 /* iOSSwiftOpenGLTests.xctest */, 84 | ); 85 | name = Products; 86 | sourceTree = ""; 87 | }; 88 | AD8814641960EEBF005F2E41 /* iOSSwiftOpenGL */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | AD8814671960EEBF005F2E41 /* AppDelegate.swift */, 92 | AD88146C1960EEBF005F2E41 /* ViewController.swift */, 93 | AD8814871960EEDA005F2E41 /* OpenGLView.swift */, 94 | AD8814651960EEBF005F2E41 /* Supporting Files */, 95 | ); 96 | path = iOSSwiftOpenGL; 97 | sourceTree = ""; 98 | }; 99 | AD8814651960EEBF005F2E41 /* Supporting Files */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | AD88146E1960EEBF005F2E41 /* Main.storyboard */, 103 | AD88148B1960EFBA005F2E41 /* SimpleVertex.glsl */, 104 | AD88148D1960EFCE005F2E41 /* SimpleFragment.glsl */, 105 | AD8814711960EEBF005F2E41 /* Images.xcassets */, 106 | AD8814691960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodeld */, 107 | AD8814661960EEBF005F2E41 /* Info.plist */, 108 | ); 109 | name = "Supporting Files"; 110 | sourceTree = ""; 111 | }; 112 | AD88147A1960EEBF005F2E41 /* iOSSwiftOpenGLTests */ = { 113 | isa = PBXGroup; 114 | children = ( 115 | AD88147D1960EEBF005F2E41 /* iOSSwiftOpenGLTests.swift */, 116 | AD88147B1960EEBF005F2E41 /* Supporting Files */, 117 | ); 118 | path = iOSSwiftOpenGLTests; 119 | sourceTree = ""; 120 | }; 121 | AD88147B1960EEBF005F2E41 /* Supporting Files */ = { 122 | isa = PBXGroup; 123 | children = ( 124 | AD88147C1960EEBF005F2E41 /* Info.plist */, 125 | ); 126 | name = "Supporting Files"; 127 | sourceTree = ""; 128 | }; 129 | /* End PBXGroup section */ 130 | 131 | /* Begin PBXNativeTarget section */ 132 | AD8814611960EEBF005F2E41 /* iOSSwiftOpenGL */ = { 133 | isa = PBXNativeTarget; 134 | buildConfigurationList = AD8814811960EEBF005F2E41 /* Build configuration list for PBXNativeTarget "iOSSwiftOpenGL" */; 135 | buildPhases = ( 136 | AD88145E1960EEBF005F2E41 /* Sources */, 137 | AD88145F1960EEBF005F2E41 /* Frameworks */, 138 | AD8814601960EEBF005F2E41 /* Resources */, 139 | ); 140 | buildRules = ( 141 | ); 142 | dependencies = ( 143 | ); 144 | name = iOSSwiftOpenGL; 145 | productName = iOSSwiftOpenGL; 146 | productReference = AD8814621960EEBF005F2E41 /* iOSSwiftOpenGL.app */; 147 | productType = "com.apple.product-type.application"; 148 | }; 149 | AD8814761960EEBF005F2E41 /* iOSSwiftOpenGLTests */ = { 150 | isa = PBXNativeTarget; 151 | buildConfigurationList = AD8814841960EEBF005F2E41 /* Build configuration list for PBXNativeTarget "iOSSwiftOpenGLTests" */; 152 | buildPhases = ( 153 | AD8814731960EEBF005F2E41 /* Sources */, 154 | AD8814741960EEBF005F2E41 /* Frameworks */, 155 | AD8814751960EEBF005F2E41 /* Resources */, 156 | ); 157 | buildRules = ( 158 | ); 159 | dependencies = ( 160 | AD8814791960EEBF005F2E41 /* PBXTargetDependency */, 161 | ); 162 | name = iOSSwiftOpenGLTests; 163 | productName = iOSSwiftOpenGLTests; 164 | productReference = AD8814771960EEBF005F2E41 /* iOSSwiftOpenGLTests.xctest */; 165 | productType = "com.apple.product-type.bundle.unit-test"; 166 | }; 167 | /* End PBXNativeTarget section */ 168 | 169 | /* Begin PBXProject section */ 170 | AD88145A1960EEBF005F2E41 /* Project object */ = { 171 | isa = PBXProject; 172 | attributes = { 173 | LastUpgradeCheck = 0600; 174 | ORGANIZATIONNAME = "Bradley Griffith"; 175 | TargetAttributes = { 176 | AD8814611960EEBF005F2E41 = { 177 | CreatedOnToolsVersion = 6.0; 178 | }; 179 | AD8814761960EEBF005F2E41 = { 180 | CreatedOnToolsVersion = 6.0; 181 | TestTargetID = AD8814611960EEBF005F2E41; 182 | }; 183 | }; 184 | }; 185 | buildConfigurationList = AD88145D1960EEBF005F2E41 /* Build configuration list for PBXProject "iOSSwiftOpenGL" */; 186 | compatibilityVersion = "Xcode 3.2"; 187 | developmentRegion = English; 188 | hasScannedForEncodings = 0; 189 | knownRegions = ( 190 | en, 191 | Base, 192 | ); 193 | mainGroup = AD8814591960EEBF005F2E41; 194 | productRefGroup = AD8814631960EEBF005F2E41 /* Products */; 195 | projectDirPath = ""; 196 | projectRoot = ""; 197 | targets = ( 198 | AD8814611960EEBF005F2E41 /* iOSSwiftOpenGL */, 199 | AD8814761960EEBF005F2E41 /* iOSSwiftOpenGLTests */, 200 | ); 201 | }; 202 | /* End PBXProject section */ 203 | 204 | /* Begin PBXResourcesBuildPhase section */ 205 | AD8814601960EEBF005F2E41 /* Resources */ = { 206 | isa = PBXResourcesBuildPhase; 207 | buildActionMask = 2147483647; 208 | files = ( 209 | AD8814701960EEBF005F2E41 /* Main.storyboard in Resources */, 210 | AD88148E1960EFCE005F2E41 /* SimpleFragment.glsl in Resources */, 211 | AD8814721960EEBF005F2E41 /* Images.xcassets in Resources */, 212 | AD88148C1960EFBA005F2E41 /* SimpleVertex.glsl in Resources */, 213 | ); 214 | runOnlyForDeploymentPostprocessing = 0; 215 | }; 216 | AD8814751960EEBF005F2E41 /* Resources */ = { 217 | isa = PBXResourcesBuildPhase; 218 | buildActionMask = 2147483647; 219 | files = ( 220 | ); 221 | runOnlyForDeploymentPostprocessing = 0; 222 | }; 223 | /* End PBXResourcesBuildPhase section */ 224 | 225 | /* Begin PBXSourcesBuildPhase section */ 226 | AD88145E1960EEBF005F2E41 /* Sources */ = { 227 | isa = PBXSourcesBuildPhase; 228 | buildActionMask = 2147483647; 229 | files = ( 230 | AD8814901960F047005F2E41 /* Images.xcassets in Sources */, 231 | AD88146D1960EEBF005F2E41 /* ViewController.swift in Sources */, 232 | AD88146B1960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodeld in Sources */, 233 | AD8814911960F27B005F2E41 /* OpenGLView.swift in Sources */, 234 | AD8814681960EEBF005F2E41 /* AppDelegate.swift in Sources */, 235 | ); 236 | runOnlyForDeploymentPostprocessing = 0; 237 | }; 238 | AD8814731960EEBF005F2E41 /* Sources */ = { 239 | isa = PBXSourcesBuildPhase; 240 | buildActionMask = 2147483647; 241 | files = ( 242 | AD88147E1960EEBF005F2E41 /* iOSSwiftOpenGLTests.swift in Sources */, 243 | ); 244 | runOnlyForDeploymentPostprocessing = 0; 245 | }; 246 | /* End PBXSourcesBuildPhase section */ 247 | 248 | /* Begin PBXTargetDependency section */ 249 | AD8814791960EEBF005F2E41 /* PBXTargetDependency */ = { 250 | isa = PBXTargetDependency; 251 | target = AD8814611960EEBF005F2E41 /* iOSSwiftOpenGL */; 252 | targetProxy = AD8814781960EEBF005F2E41 /* PBXContainerItemProxy */; 253 | }; 254 | /* End PBXTargetDependency section */ 255 | 256 | /* Begin PBXVariantGroup section */ 257 | AD88146E1960EEBF005F2E41 /* Main.storyboard */ = { 258 | isa = PBXVariantGroup; 259 | children = ( 260 | AD88146F1960EEBF005F2E41 /* Base */, 261 | ); 262 | name = Main.storyboard; 263 | sourceTree = ""; 264 | }; 265 | /* End PBXVariantGroup section */ 266 | 267 | /* Begin XCBuildConfiguration section */ 268 | AD88147F1960EEBF005F2E41 /* Debug */ = { 269 | isa = XCBuildConfiguration; 270 | buildSettings = { 271 | ALWAYS_SEARCH_USER_PATHS = NO; 272 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 273 | CLANG_CXX_LIBRARY = "libc++"; 274 | CLANG_ENABLE_MODULES = YES; 275 | CLANG_ENABLE_OBJC_ARC = YES; 276 | CLANG_WARN_BOOL_CONVERSION = YES; 277 | CLANG_WARN_CONSTANT_CONVERSION = YES; 278 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 279 | CLANG_WARN_EMPTY_BODY = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INT_CONVERSION = YES; 282 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 283 | CLANG_WARN_UNREACHABLE_CODE = YES; 284 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 285 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 286 | COPY_PHASE_STRIP = NO; 287 | ENABLE_STRICT_OBJC_MSGSEND = YES; 288 | GCC_C_LANGUAGE_STANDARD = gnu99; 289 | GCC_DYNAMIC_NO_PIC = NO; 290 | GCC_OPTIMIZATION_LEVEL = 0; 291 | GCC_PREPROCESSOR_DEFINITIONS = ( 292 | "DEBUG=1", 293 | "$(inherited)", 294 | ); 295 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 296 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 297 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 298 | GCC_WARN_UNDECLARED_SELECTOR = YES; 299 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 300 | GCC_WARN_UNUSED_FUNCTION = YES; 301 | GCC_WARN_UNUSED_VARIABLE = YES; 302 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 303 | METAL_ENABLE_DEBUG_INFO = YES; 304 | ONLY_ACTIVE_ARCH = YES; 305 | SDKROOT = iphoneos; 306 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 307 | }; 308 | name = Debug; 309 | }; 310 | AD8814801960EEBF005F2E41 /* Release */ = { 311 | isa = XCBuildConfiguration; 312 | buildSettings = { 313 | ALWAYS_SEARCH_USER_PATHS = NO; 314 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 315 | CLANG_CXX_LIBRARY = "libc++"; 316 | CLANG_ENABLE_MODULES = YES; 317 | CLANG_ENABLE_OBJC_ARC = YES; 318 | CLANG_WARN_BOOL_CONVERSION = YES; 319 | CLANG_WARN_CONSTANT_CONVERSION = YES; 320 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 321 | CLANG_WARN_EMPTY_BODY = YES; 322 | CLANG_WARN_ENUM_CONVERSION = YES; 323 | CLANG_WARN_INT_CONVERSION = YES; 324 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 325 | CLANG_WARN_UNREACHABLE_CODE = YES; 326 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 327 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 328 | COPY_PHASE_STRIP = YES; 329 | ENABLE_NS_ASSERTIONS = NO; 330 | ENABLE_STRICT_OBJC_MSGSEND = YES; 331 | GCC_C_LANGUAGE_STANDARD = gnu99; 332 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 333 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 334 | GCC_WARN_UNDECLARED_SELECTOR = YES; 335 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 336 | GCC_WARN_UNUSED_FUNCTION = YES; 337 | GCC_WARN_UNUSED_VARIABLE = YES; 338 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 339 | METAL_ENABLE_DEBUG_INFO = NO; 340 | SDKROOT = iphoneos; 341 | VALIDATE_PRODUCT = YES; 342 | }; 343 | name = Release; 344 | }; 345 | AD8814821960EEBF005F2E41 /* Debug */ = { 346 | isa = XCBuildConfiguration; 347 | buildSettings = { 348 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 349 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; 350 | INFOPLIST_FILE = iOSSwiftOpenGL/Info.plist; 351 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 352 | PRODUCT_NAME = "$(TARGET_NAME)"; 353 | }; 354 | name = Debug; 355 | }; 356 | AD8814831960EEBF005F2E41 /* Release */ = { 357 | isa = XCBuildConfiguration; 358 | buildSettings = { 359 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 360 | ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; 361 | INFOPLIST_FILE = iOSSwiftOpenGL/Info.plist; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | }; 365 | name = Release; 366 | }; 367 | AD8814851960EEBF005F2E41 /* Debug */ = { 368 | isa = XCBuildConfiguration; 369 | buildSettings = { 370 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/iOSSwiftOpenGL.app/iOSSwiftOpenGL"; 371 | FRAMEWORK_SEARCH_PATHS = ( 372 | "$(SDKROOT)/Developer/Library/Frameworks", 373 | "$(inherited)", 374 | ); 375 | GCC_PREPROCESSOR_DEFINITIONS = ( 376 | "DEBUG=1", 377 | "$(inherited)", 378 | ); 379 | INFOPLIST_FILE = iOSSwiftOpenGLTests/Info.plist; 380 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 381 | METAL_ENABLE_DEBUG_INFO = YES; 382 | PRODUCT_NAME = "$(TARGET_NAME)"; 383 | TEST_HOST = "$(BUNDLE_LOADER)"; 384 | }; 385 | name = Debug; 386 | }; 387 | AD8814861960EEBF005F2E41 /* Release */ = { 388 | isa = XCBuildConfiguration; 389 | buildSettings = { 390 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/iOSSwiftOpenGL.app/iOSSwiftOpenGL"; 391 | FRAMEWORK_SEARCH_PATHS = ( 392 | "$(SDKROOT)/Developer/Library/Frameworks", 393 | "$(inherited)", 394 | ); 395 | INFOPLIST_FILE = iOSSwiftOpenGLTests/Info.plist; 396 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 397 | METAL_ENABLE_DEBUG_INFO = NO; 398 | PRODUCT_NAME = "$(TARGET_NAME)"; 399 | TEST_HOST = "$(BUNDLE_LOADER)"; 400 | }; 401 | name = Release; 402 | }; 403 | /* End XCBuildConfiguration section */ 404 | 405 | /* Begin XCConfigurationList section */ 406 | AD88145D1960EEBF005F2E41 /* Build configuration list for PBXProject "iOSSwiftOpenGL" */ = { 407 | isa = XCConfigurationList; 408 | buildConfigurations = ( 409 | AD88147F1960EEBF005F2E41 /* Debug */, 410 | AD8814801960EEBF005F2E41 /* Release */, 411 | ); 412 | defaultConfigurationIsVisible = 0; 413 | defaultConfigurationName = Release; 414 | }; 415 | AD8814811960EEBF005F2E41 /* Build configuration list for PBXNativeTarget "iOSSwiftOpenGL" */ = { 416 | isa = XCConfigurationList; 417 | buildConfigurations = ( 418 | AD8814821960EEBF005F2E41 /* Debug */, 419 | AD8814831960EEBF005F2E41 /* Release */, 420 | ); 421 | defaultConfigurationIsVisible = 0; 422 | defaultConfigurationName = Release; 423 | }; 424 | AD8814841960EEBF005F2E41 /* Build configuration list for PBXNativeTarget "iOSSwiftOpenGLTests" */ = { 425 | isa = XCConfigurationList; 426 | buildConfigurations = ( 427 | AD8814851960EEBF005F2E41 /* Debug */, 428 | AD8814861960EEBF005F2E41 /* Release */, 429 | ); 430 | defaultConfigurationIsVisible = 0; 431 | defaultConfigurationName = Release; 432 | }; 433 | /* End XCConfigurationList section */ 434 | 435 | /* Begin XCVersionGroup section */ 436 | AD8814691960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodeld */ = { 437 | isa = XCVersionGroup; 438 | children = ( 439 | AD88146A1960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodel */, 440 | ); 441 | currentVersion = AD88146A1960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodel */; 442 | path = iOSSwiftOpenGL.xcdatamodeld; 443 | sourceTree = ""; 444 | versionGroupType = wrapper.xcdatamodel; 445 | }; 446 | /* End XCVersionGroup section */ 447 | }; 448 | rootObject = AD88145A1960EEBF005F2E41 /* Project object */; 449 | } 450 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL.xcodeproj/xcuserdata/gitmo.xcuserdatad/xcschemes/iOSSwiftOpenGL.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL.xcodeproj/xcuserdata/gitmo.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | iOSSwiftOpenGL.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | AD8814611960EEBF005F2E41 16 | 17 | primary 18 | 19 | 20 | AD8814761960EEBF005F2E41 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // iOSSwiftOpenGL 4 | // 5 | // Created by Bradley Griffith on 6/29/14. 6 | // Copyright (c) 2014 Bradley Griffith. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import CoreData 11 | 12 | @UIApplicationMain 13 | class AppDelegate: UIResponder, UIApplicationDelegate { 14 | 15 | var window: UIWindow? 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | // Saves changes in the application's managed object context before the application terminates. 43 | self.saveContext() 44 | } 45 | 46 | func saveContext () { 47 | var error: NSError? = nil 48 | let managedObjectContext = self.managedObjectContext 49 | if managedObjectContext.hasChanges && !managedObjectContext.save(&error) { 50 | // Replace this implementation with code to handle the error appropriately. 51 | // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 52 | //println("Unresolved error \(error), \(error.userInfo)") 53 | abort() 54 | } 55 | } 56 | 57 | // #pragma mark - Core Data stack 58 | 59 | // Returns the managed object context for the application. 60 | // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 61 | var managedObjectContext: NSManagedObjectContext { 62 | if _managedObjectContext != nil { 63 | let coordinator = self.persistentStoreCoordinator 64 | _managedObjectContext = NSManagedObjectContext() 65 | _managedObjectContext!.persistentStoreCoordinator = coordinator 66 | } 67 | return _managedObjectContext! 68 | } 69 | var _managedObjectContext: NSManagedObjectContext? = nil 70 | 71 | // Returns the managed object model for the application. 72 | // If the model doesn't already exist, it is created from the application's model. 73 | var managedObjectModel: NSManagedObjectModel { 74 | if _managedObjectModel != nil { 75 | let modelURL = NSBundle.mainBundle().URLForResource("iOSSwiftOpenGL", withExtension: "momd") 76 | _managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL!) 77 | } 78 | return _managedObjectModel! 79 | } 80 | var _managedObjectModel: NSManagedObjectModel? = nil 81 | 82 | // Returns the persistent store coordinator for the application. 83 | // If the coordinator doesn't already exist, it is created and the application's store added to it. 84 | var persistentStoreCoordinator: NSPersistentStoreCoordinator { 85 | if _persistentStoreCoordinator != nil { 86 | let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("iOSSwiftOpenGL.sqlite") 87 | var error: NSError? = nil 88 | _persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 89 | if _persistentStoreCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) == nil { 90 | /* 91 | Replace this implementation with code to handle the error appropriately. 92 | 93 | abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 94 | 95 | Typical reasons for an error here include: 96 | * The persistent store is not accessible; 97 | * The schema for the persistent store is incompatible with current managed object model. 98 | Check the error message to determine what the actual problem was. 99 | 100 | 101 | If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 102 | 103 | If you encounter schema incompatibility errors during development, you can reduce their frequency by: 104 | * Simply deleting the existing store: 105 | NSFileManager.defaultManager().removeItemAtURL(storeURL, error: nil) 106 | 107 | * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 108 | [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true} 109 | 110 | Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 111 | 112 | */ 113 | //println("Unresolved error \(error), \(error.userInfo)") 114 | abort() 115 | } 116 | } 117 | return _persistentStoreCoordinator! 118 | } 119 | var _persistentStoreCoordinator: NSPersistentStoreCoordinator? = nil 120 | 121 | // #pragma mark - Application's Documents directory 122 | 123 | // Returns the URL to the application's Documents directory. 124 | var applicationDocumentsDirectory: NSURL { 125 | let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 126 | return urls[urls.endIndex-1] as! NSURL 127 | } 128 | 129 | } 130 | 131 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /iOSSwiftOpenGL/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /iOSSwiftOpenGL/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | Bradley-Griffith.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/OpenGLView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // OpenGLView.swift 3 | // iOSSwiftOpenGL 4 | // 5 | // Created by Bradley Griffith on 6/29/14. 6 | // Copyright (c) 2014 Bradley Griffith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | import QuartzCore 12 | import OpenGLES 13 | import GLKit 14 | 15 | 16 | 17 | struct Vertex { 18 | var Position: (CFloat, CFloat, CFloat) 19 | var Color: (CFloat, CFloat, CFloat, CFloat) 20 | } 21 | 22 | var Vertices = [ 23 | Vertex(Position: (1, -1, 0) , Color: (1, 0, 0, 1)), 24 | Vertex(Position: (1, 1, 0) , Color: (0, 1, 0, 1)), 25 | Vertex(Position: (-1, 1, 0) , Color: (0, 0, 1, 1)), 26 | Vertex(Position: (-1, -1, 0), Color: (0, 0, 0, 1)) 27 | ] 28 | 29 | var Indices: [GLubyte] = [ 30 | 0, 1, 2, 31 | 2, 3, 0 32 | ] 33 | 34 | 35 | //helper extensions to pass arguments to GL land 36 | extension Array { 37 | func size () -> Int { 38 | return self.count * sizeofValue(self[0]) 39 | } 40 | } 41 | 42 | extension Int32 { 43 | func __conversion() -> GLenum { 44 | return GLuint(self) 45 | } 46 | 47 | func __conversion() -> GLboolean { 48 | return GLboolean(UInt8(self)) 49 | } 50 | } 51 | 52 | extension Int { 53 | func __conversion() -> Int32 { 54 | return Int32(self) 55 | } 56 | 57 | func __conversion() -> GLubyte { 58 | return GLubyte(self) 59 | } 60 | 61 | } 62 | 63 | 64 | 65 | class OpenGLView: UIView { 66 | 67 | var eaglLayer: CAEAGLLayer! 68 | var context: EAGLContext! 69 | var colorRenderBuffer: GLuint = GLuint() 70 | var positionSlot: GLuint = GLuint() 71 | var colorSlot: GLuint = GLuint() 72 | var indexBuffer: GLuint = GLuint() 73 | var vertexBuffer: GLuint = GLuint() 74 | var VAO:GLuint = GLuint() 75 | 76 | 77 | /* Class Methods 78 | ------------------------------------------*/ 79 | 80 | override class func layerClass() -> AnyClass { 81 | // In order for our view to display OpenGL content, we need to set it's 82 | // default layer to be a CAEAGLayer 83 | return CAEAGLLayer.self 84 | } 85 | 86 | 87 | /* Lifecycle 88 | ------------------------------------------*/ 89 | 90 | required init(coder aDecoder: NSCoder) { 91 | super.init(coder: aDecoder) 92 | 93 | self.setupLayer() 94 | self.setupContext() 95 | self.setupRenderBuffer() 96 | self.setupFrameBuffer() 97 | self.compileShaders() 98 | self.setupVBOs() 99 | self.render() 100 | } 101 | 102 | 103 | 104 | /* Instance Methods 105 | ------------------------------------------*/ 106 | 107 | func setupLayer() { 108 | // CALayer's are, by default, non-opaque, which is 'bad for performance with OpenGL', 109 | // so let's set our CAEAGLLayer layer to be opaque. 110 | self.eaglLayer = self.layer as! CAEAGLLayer 111 | self.eaglLayer.opaque = true 112 | } 113 | 114 | func setupContext() { 115 | // Just like with CoreGraphics, in order to do much with OpenGL, we need a context. 116 | // Here we create a new context with the version of the rendering API we want and 117 | // tells OpenGL that when we draw, we want to do so within this context. 118 | var api: EAGLRenderingAPI = EAGLRenderingAPI.OpenGLES2 119 | self.context = EAGLContext(API: api) 120 | 121 | if (self.context == nil) { 122 | println("Failed to initialize OpenGLES 2.0 context!") 123 | exit(1) 124 | } 125 | 126 | if (!EAGLContext.setCurrentContext(self.context)) { 127 | println("Failed to set current OpenGL context!") 128 | exit(1) 129 | } 130 | } 131 | 132 | func setupRenderBuffer() { 133 | glGenRenderbuffers(1, &self.colorRenderBuffer) 134 | glBindRenderbuffer(GLenum(GL_RENDERBUFFER), self.colorRenderBuffer) 135 | self.context.renderbufferStorage(Int(GL_RENDERBUFFER), fromDrawable:self.eaglLayer) 136 | } 137 | 138 | func setupFrameBuffer() { 139 | var frameBuffer: GLuint = GLuint() 140 | glGenFramebuffers(1, &frameBuffer) 141 | glBindFramebuffer(GLenum(GL_FRAMEBUFFER), frameBuffer) 142 | glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), self.colorRenderBuffer) 143 | } 144 | 145 | func compileShader(shaderName: String, shaderType: GLenum) -> GLuint { 146 | 147 | // Get NSString with contents of our shader file. 148 | var shaderPath: String! = NSBundle.mainBundle().pathForResource(shaderName, ofType: "glsl") 149 | var error: NSError? = nil 150 | var shaderString = NSString(contentsOfFile:shaderPath, encoding: NSUTF8StringEncoding, error: &error) 151 | if (shaderString == nil) { 152 | println("Failed to set contents shader of shader file!") 153 | } 154 | 155 | // Tell OpenGL to create an OpenGL object to represent the shader, indicating if it's a vertex or a fragment shader. 156 | var shaderHandle: GLuint = glCreateShader(shaderType) 157 | 158 | if shaderHandle == 0 { 159 | NSLog("Couldn't create shader") 160 | } 161 | // Conver shader string to CString and call glShaderSource to give OpenGL the source for the shader. 162 | var shaderStringUTF8 = shaderString!.UTF8String 163 | var shaderStringLength: GLint = GLint(Int32(shaderString!.length)) 164 | glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength) 165 | 166 | // Tell OpenGL to compile the shader. 167 | glCompileShader(shaderHandle) 168 | 169 | // But compiling can fail! If we have errors in our GLSL code, we can here and output any errors. 170 | var compileSuccess: GLint = GLint() 171 | glGetShaderiv(shaderHandle, GLenum(GL_COMPILE_STATUS), &compileSuccess) 172 | if (compileSuccess == GL_FALSE) { 173 | println("Failed to compile shader!") 174 | // TODO: Actually output the error that we can get from the glGetShaderInfoLog function. 175 | exit(1); 176 | } 177 | 178 | return shaderHandle 179 | } 180 | 181 | func compileShaders() { 182 | 183 | // Compile our vertex and fragment shaders. 184 | var vertexShader: GLuint = self.compileShader("SimpleVertex", shaderType: GLenum(GL_VERTEX_SHADER)) 185 | var fragmentShader: GLuint = self.compileShader("SimpleFragment", shaderType: GLenum(GL_FRAGMENT_SHADER)) 186 | 187 | // Call glCreateProgram, glAttachShader, and glLinkProgram to link the vertex and fragment shaders into a complete program. 188 | var programHandle: GLuint = glCreateProgram() 189 | glAttachShader(programHandle, vertexShader) 190 | glAttachShader(programHandle, fragmentShader) 191 | glLinkProgram(programHandle) 192 | 193 | // Check for any errors. 194 | var linkSuccess: GLint = GLint() 195 | glGetProgramiv(programHandle, GLenum(GL_LINK_STATUS), &linkSuccess) 196 | if (linkSuccess == GL_FALSE) { 197 | println("Failed to create shader program!") 198 | // TODO: Actually output the error that we can get from the glGetProgramInfoLog function. 199 | exit(1); 200 | } 201 | 202 | // Call glUseProgram to tell OpenGL to actually use this program when given vertex info. 203 | glUseProgram(programHandle) 204 | 205 | // Finally, call glGetAttribLocation to get a pointer to the input values for the vertex shader, so we 206 | // can set them in code. Also call glEnableVertexAttribArray to enable use of these arrays (they are disabled by default). 207 | self.positionSlot = GLuint(glGetAttribLocation(programHandle, "Position")) 208 | self.colorSlot = GLuint(glGetAttribLocation(programHandle, "SourceColor")) 209 | glEnableVertexAttribArray(self.positionSlot) 210 | glEnableVertexAttribArray(self.colorSlot) 211 | } 212 | 213 | // Setup Vertex Buffer Objects 214 | func setupVBOs() { 215 | 216 | glGenVertexArraysOES(1, &VAO); 217 | glBindVertexArrayOES(VAO); 218 | 219 | glGenBuffers(1, &vertexBuffer) 220 | glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer) 221 | glBufferData(GLenum(GL_ARRAY_BUFFER), Vertices.size(), Vertices, GLenum(GL_STATIC_DRAW)) 222 | 223 | // let positionSlotFirstComponent : UnsafePointer(&0) 224 | glEnableVertexAttribArray(positionSlot) 225 | glVertexAttribPointer(positionSlot, 3, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil) 226 | 227 | glEnableVertexAttribArray(colorSlot) 228 | // let colorSlotFirstComponent = UnsafePointer(sizeof(Float) * 3) 229 | glVertexAttribPointer(colorSlot, 4, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil) 230 | 231 | glGenBuffers(1, &indexBuffer) 232 | glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), indexBuffer) 233 | glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER), Indices.size(), Indices, GLenum(GL_STATIC_DRAW)) 234 | 235 | glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0) 236 | glBindVertexArrayOES(0) 237 | } 238 | 239 | func render() { 240 | glBindVertexArrayOES(VAO); 241 | glViewport(0, 0, GLint(self.frame.size.width), GLint(self.frame.size.height)); 242 | 243 | glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), nil) 244 | 245 | self.context.presentRenderbuffer(Int(GL_RENDERBUFFER)) 246 | 247 | glBindVertexArrayOES(0) 248 | } 249 | } 250 | 251 | /////////////////////////////////////// 252 | 253 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // iOSSwiftOpenGL 4 | // 5 | // Created by Bradley Griffith on 6/29/14. 6 | // Copyright (c) 2014 Bradley Griffith. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | // Do any additional setup after loading the view, typically from a nib. 16 | } 17 | 18 | override func didReceiveMemoryWarning() { 19 | super.didReceiveMemoryWarning() 20 | // Dispose of any resources that can be recreated. 21 | } 22 | 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/iOSSwiftOpenGL.xcdatamodeld/.xccurrentversion: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | _XCCurrentVersionName 6 | iOSSwiftOpenGL.xcdatamodel 7 | 8 | 9 | -------------------------------------------------------------------------------- /iOSSwiftOpenGL/iOSSwiftOpenGL.xcdatamodeld/iOSSwiftOpenGL.xcdatamodel/contents: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /iOSSwiftOpenGLTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | Bradley-Griffith.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /iOSSwiftOpenGLTests/iOSSwiftOpenGLTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // iOSSwiftOpenGLTests.swift 3 | // iOSSwiftOpenGLTests 4 | // 5 | // Created by Bradley Griffith on 6/29/14. 6 | // Copyright (c) 2014 Bradley Griffith. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class iOSSwiftOpenGLTests: XCTestCase { 12 | 13 | override func setUp() { 14 | super.setUp() 15 | // Put setup code here. This method is called before the invocation of each test method in the class. 16 | } 17 | 18 | override func tearDown() { 19 | // Put teardown code here. This method is called after the invocation of each test method in the class. 20 | super.tearDown() 21 | } 22 | 23 | func testExample() { 24 | // This is an example of a functional test case. 25 | XCTAssert(true, "Pass") 26 | } 27 | 28 | func testPerformanceExample() { 29 | // This is an example of a performance test case. 30 | self.measureBlock() { 31 | // Put the code you want to measure the time of here. 32 | } 33 | } 34 | 35 | } 36 | --------------------------------------------------------------------------------