├── .gitignore
├── FullViewPlayer
├── 1973059-ca7d71f37cdb4f70.gif
├── FullViewPlayer.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
└── FullViewPlayer
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ └── AppIcon.appiconset
│ │ └── Contents.json
│ ├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── demo.mp4
│ ├── fragmentShader.glsl
│ ├── main.m
│ └── vertexShader.glsl
├── LICENSE
├── OpenGLESGLKit04光照
├── OpenGLESGLKit04.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
├── OpenGLESGLKit04
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ │ ├── AppIcon.appiconset
│ │ │ └── Contents.json
│ │ ├── Contents.json
│ │ └── timg.imageset
│ │ │ ├── Contents.json
│ │ │ └── timg.jpeg
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ └── main.m
├── OpenGLESGLKit04Tests
│ ├── Info.plist
│ └── OpenGLESGLKit04Tests.m
└── OpenGLESGLKit04UITests
│ ├── Info.plist
│ └── OpenGLESGLKit04UITests.m
├── OpenGLESWithoutGLKIt03立方体
├── OpenGLESWithoutGLKIt03立方体.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
└── OpenGLESWithoutGLKIt03立方体
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ ├── AppIcon.appiconset
│ │ └── Contents.json
│ ├── Contents.json
│ └── timg.imageset
│ │ ├── Contents.json
│ │ └── timg.jpeg
│ ├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── cube.gif
│ ├── fragmentShader.glsl
│ ├── ksMatrix.c
│ ├── ksMatrix.h
│ ├── ksVector.c
│ ├── ksVector.h
│ ├── lineFragmentShader.glsl
│ ├── lineVertexShader.glsl
│ ├── main.m
│ └── vertexShader.glsl
├── OpenGLESWithoutGLKit01三角形
├── OpenGLESWithoutGLKit01三角形.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
├── OpenGLESWithoutGLKit01三角形
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── fragmentShader.glsl
│ ├── main.m
│ └── vertexShader.glsl
├── OpenGLESWithoutGLKit01三角形Tests
│ ├── Info.plist
│ └── OpenGLESWithoutGLKit01___Tests.m
└── OpenGLESWithoutGLKit01三角形UITests
│ ├── Info.plist
│ └── OpenGLESWithoutGLKit01___UITests.m
├── OpenGLESWithoutGLKit02纹理
├── OpenGLESWithoutGLKit02纹理.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
└── OpenGLESWithoutGLKit02纹理
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ ├── AppIcon.appiconset
│ │ └── Contents.json
│ ├── Contents.json
│ └── myImage.imageset
│ │ ├── @2x.png
│ │ └── Contents.json
│ ├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── fragmentShader.glsl
│ ├── main.m
│ ├── texture.png
│ └── vertexShader.glsl
├── README.md
├── learnOpenGLESGLKit01三角形
├── learnOpenGLESGLKit01三角形.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
├── learnOpenGLESGLKit01三角形
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── main.m
│ └── triangle.png
├── learnOpenGLESGLKit01三角形Tests
│ ├── Info.plist
│ └── learnOpenGLESGLKit01___Tests.m
└── learnOpenGLESGLKit01三角形UITests
│ ├── Info.plist
│ └── learnOpenGLESGLKit01___UITests.m
├── learnOpenGLESGLKit03立方体
├── learnOpenGLESGLKit03.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
├── learnOpenGLESGLKit03
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ │ ├── AppIcon.appiconset
│ │ │ └── Contents.json
│ │ ├── Contents.json
│ │ └── timg.imageset
│ │ │ ├── Contents.json
│ │ │ └── timg.jpeg
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ └── main.m
├── learnOpenGLESGLKit03Tests
│ ├── Info.plist
│ └── learnOpenGLESGLKit03Tests.m
└── learnOpenGLESGLKit03UITests
│ ├── Info.plist
│ └── learnOpenGLESGLKit03UITests.m
├── learnOpenGLESGLKit05球体
├── learnOpenGLESGLKit05球体.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
└── learnOpenGLESGLKit05球体
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ ├── AppIcon.appiconset
│ │ └── Contents.json
│ ├── Contents.json
│ └── timg.imageset
│ │ ├── Contents.json
│ │ └── timg.jpeg
│ ├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ ├── ball.gif
│ └── main.m
├── learnOpenGLESGLkit02纹理
├── learnOpenGLESGLkit02纹理.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ └── contents.xcworkspacedata
├── learnOpenGLESGLkit02纹理
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── Assets.xcassets
│ │ ├── AppIcon.appiconset
│ │ │ └── Contents.json
│ │ ├── Contents.json
│ │ └── myImage.imageset
│ │ │ ├── Contents.json
│ │ │ └── myImage@2x.png
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Info.plist
│ ├── ViewController.h
│ ├── ViewController.m
│ └── main.m
├── learnOpenGLESGLkit02纹理Tests
│ ├── Info.plist
│ └── learnOpenGLESGLkit02__Tests.m
└── learnOpenGLESGLkit02纹理UITests
│ ├── Info.plist
│ └── learnOpenGLESGLkit02__UITests.m
└── openGLESGLKit06天空盒子
├── openGLESGLKit06.xcodeproj
├── project.pbxproj
└── project.xcworkspace
│ └── contents.xcworkspacedata
├── openGLESGLKit06
├── AppDelegate.h
├── AppDelegate.m
├── Assets.xcassets
│ └── AppIcon.appiconset
│ │ └── Contents.json
├── Base.lproj
│ ├── LaunchScreen.storyboard
│ └── Main.storyboard
├── Info.plist
├── ViewController.h
├── ViewController.m
├── image.png
└── main.m
├── openGLESGLKit06Tests
├── Info.plist
└── openGLESGLKit06Tests.m
└── openGLESGLKit06UITests
├── Info.plist
└── openGLESGLKit06UITests.m
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData/
8 |
9 | ## Various settings
10 | .DS_Store
11 | *.pbxuser
12 | !default.pbxuser
13 | *.mode1v3
14 | !default.mode1v3
15 | *.mode2v3
16 | !default.mode2v3
17 | *.perspectivev3
18 | !default.perspectivev3
19 | xcuserdata/
20 |
21 | ## Other
22 | *.moved-aside
23 | *.xccheckout
24 | *.xcscmblueprint
25 |
26 | ## Obj-C/Swift specific
27 | *.hmap
28 | *.ipa
29 | *.dSYM.zip
30 | *.dSYM
31 |
32 | # CocoaPods
33 | #
34 | # We recommend against adding the Pods directory to your .gitignore. However
35 | # you should judge for yourself, the pros and cons are mentioned at:
36 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
37 | #
38 | # Pods/
39 |
40 | # Carthage
41 | #
42 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
43 | # Carthage/Checkouts
44 |
45 | Carthage/Build
46 |
47 | # fastlane
48 | #
49 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
50 | # screenshots whenever they are needed.
51 | # For more information about the recommended setup visit:
52 | # https://docs.fastlane.tools/best-practices/source-control/#source-control
53 |
54 | fastlane/report.xml
55 | fastlane/Preview.html
56 | fastlane/screenshots
57 | fastlane/test_output
58 |
59 | # Code Injection
60 | #
61 | # After new code Injection tools there's a generated folder /iOSInjectionProject
62 | # https://github.com/johnno1962/injectionforxcode
63 |
64 | iOSInjectionProject/
65 |
--------------------------------------------------------------------------------
/FullViewPlayer/1973059-ca7d71f37cdb4f70.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/FullViewPlayer/1973059-ca7d71f37cdb4f70.gif
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // FullViewPlayer
4 | //
5 | // Created by 刘晓亮 on 2017/10/12.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // FullViewPlayer
4 | //
5 | // Created by 刘晓亮 on 2017/10/12.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | }
88 | ],
89 | "info" : {
90 | "version" : 1,
91 | "author" : "xcode"
92 | }
93 | }
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/Base.lproj/LaunchScreen.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 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/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 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationLandscapeLeft
34 |
35 | UISupportedInterfaceOrientations~ipad
36 |
37 | UIInterfaceOrientationPortrait
38 | UIInterfaceOrientationPortraitUpsideDown
39 | UIInterfaceOrientationLandscapeLeft
40 | UIInterfaceOrientationLandscapeRight
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // FullViewPlayer
4 | //
5 | // Created by 刘晓亮 on 2017/10/12.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/demo.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/FullViewPlayer/FullViewPlayer/demo.mp4
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/fragmentShader.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | uniform sampler2D myTexture;
4 | uniform sampler2D samplerUV;
5 |
6 | varying vec2 myTextureCoordsOut;
7 |
8 | void main()
9 | {
10 | // 用一个矩阵来简化后面YUV转GRB的计算公式
11 | mat3 conversionColor = mat3(1.164, 1.164, 1.164,
12 | 0.0, -0.213, 2.112,
13 | 1.793, -0.533, 0.0);
14 |
15 | mediump vec3 yuv;
16 | lowp vec3 rgb;
17 |
18 | yuv.x = texture2D(myTexture, myTextureCoordsOut).r - (16.0/255.0);
19 | yuv.yz = texture2D(samplerUV, myTextureCoordsOut).rg - vec2(0.5, 0.5);
20 |
21 | rgb = conversionColor * yuv;
22 |
23 |
24 | gl_FragColor = vec4(rgb, 1.0);
25 | // gl_FragColor = vec4(texture2D(myTexture, myTextureCoordsOut).rgb, 1.0);
26 | }
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // FullViewPlayer
4 | //
5 | // Created by 刘晓亮 on 2017/10/12.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/FullViewPlayer/FullViewPlayer/vertexShader.glsl:
--------------------------------------------------------------------------------
1 | attribute vec4 myPosition;
2 |
3 | attribute vec2 myTextureCoordsIn;
4 |
5 | varying vec2 myTextureCoordsOut;
6 |
7 | uniform mat4 modelView;
8 | uniform mat4 projection;
9 |
10 | void main()
11 | {
12 | gl_Position = projection * modelView * myPosition;
13 | myTextureCoordsOut = myTextureCoordsIn;
14 | }
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 1677
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // OpenGLESGLKit04
4 | //
5 | // Created by 刘晓亮 on 2017/8/19.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // OpenGLESGLKit04
4 | //
5 | // Created by 刘晓亮 on 2017/8/19.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | }
43 | ],
44 | "info" : {
45 | "version" : 1,
46 | "author" : "xcode"
47 | }
48 | }
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/Assets.xcassets/timg.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "timg.jpeg",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "universal",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/Assets.xcassets/timg.imageset/timg.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/OpenGLESGLKit04光照/OpenGLESGLKit04/Assets.xcassets/timg.imageset/timg.jpeg
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/Base.lproj/LaunchScreen.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 |
28 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/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 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleDisplayName
8 | 光照
9 | CFBundleExecutable
10 | $(EXECUTABLE_NAME)
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleVersion
22 | 1
23 | LSRequiresIPhoneOS
24 |
25 | UILaunchStoryboardName
26 | LaunchScreen
27 | UIMainStoryboardFile
28 | Main
29 | UIRequiredDeviceCapabilities
30 |
31 | armv7
32 |
33 | UISupportedInterfaceOrientations
34 |
35 | UIInterfaceOrientationPortrait
36 | UIInterfaceOrientationLandscapeLeft
37 | UIInterfaceOrientationLandscapeRight
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // OpenGLESGLKit04
4 | //
5 | // Created by 刘晓亮 on 2017/8/19.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @interface ViewController : GLKViewController
13 |
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // OpenGLESGLKit04
4 | //
5 | // Created by 刘晓亮 on 2017/8/19.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04Tests/OpenGLESGLKit04Tests.m:
--------------------------------------------------------------------------------
1 | //
2 | // OpenGLESGLKit04Tests.m
3 | // OpenGLESGLKit04Tests
4 | //
5 | // Created by 刘晓亮 on 2017/8/19.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface OpenGLESGLKit04Tests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation OpenGLESGLKit04Tests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 | // Put setup code here. This method is called before the invocation of each test method in the class.
20 | }
21 |
22 | - (void)tearDown {
23 | // Put teardown code here. This method is called after the invocation of each test method in the class.
24 | [super tearDown];
25 | }
26 |
27 | - (void)testExample {
28 | // This is an example of a functional test case.
29 | // Use XCTAssert and related functions to verify your tests produce the correct results.
30 | }
31 |
32 | - (void)testPerformanceExample {
33 | // This is an example of a performance test case.
34 | [self measureBlock:^{
35 | // Put the code you want to measure the time of here.
36 | }];
37 | }
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/OpenGLESGLKit04光照/OpenGLESGLKit04UITests/OpenGLESGLKit04UITests.m:
--------------------------------------------------------------------------------
1 | //
2 | // OpenGLESGLKit04UITests.m
3 | // OpenGLESGLKit04UITests
4 | //
5 | // Created by 刘晓亮 on 2017/8/19.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface OpenGLESGLKit04UITests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation OpenGLESGLKit04UITests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 |
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 |
22 | // In UI tests it is usually best to stop immediately when a failure occurs.
23 | self.continueAfterFailure = NO;
24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
25 | [[[XCUIApplication alloc] init] launch];
26 |
27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
28 | }
29 |
30 | - (void)tearDown {
31 | // Put teardown code here. This method is called after the invocation of each test method in the class.
32 | [super tearDown];
33 | }
34 |
35 | - (void)testExample {
36 | // Use recording to get started writing UI tests.
37 | // Use XCTAssert and related functions to verify your tests produce the correct results.
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // OpenGLESWithoutGLKIt03立方体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // OpenGLESWithoutGLKIt03立方体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Assets.xcassets/timg.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "timg.jpeg",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "universal",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Assets.xcassets/timg.imageset/timg.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Assets.xcassets/timg.imageset/timg.jpeg
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Base.lproj/LaunchScreen.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 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/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 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // OpenGLESWithoutGLKIt03立方体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/cube.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/cube.gif
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/fragmentShader.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | // 纹理坐标
4 | varying vec2 textureCoordsOut;
5 |
6 | // 要传的纹理
7 | uniform sampler2D myTexture;
8 |
9 | void main()
10 | {
11 | vec4 colors = texture2D(myTexture, textureCoordsOut);
12 | gl_FragColor = vec4(colors.rgb, 1.0);
13 | }
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/ksMatrix.h:
--------------------------------------------------------------------------------
1 | //
2 | // ksMatrix.h
3 | //
4 | // Created by kesalin@gmail.com on 12-11-26.
5 | // Copyright (c) 2012. http://blog.csdn.net/kesalin/. All rights reserved.
6 | //
7 |
8 | #ifndef __KS_MATRIX_H__
9 | #define __KS_MATRIX_H__
10 |
11 | #include
12 | #include "ksVector.h"
13 |
14 | #ifndef M_PI
15 | #define M_PI 3.1415926535897932384626433832795f
16 | #endif
17 |
18 | #define DEG2RAD( a ) (((a) * M_PI) / 180.0f)
19 | #define RAD2DEG( a ) (((a) * 180.f) / M_PI)
20 |
21 | // angle indexes
22 | #define PITCH 0 // up / down
23 | #define YAW 1 // left / right
24 | #define ROLL 2 // fall over
25 |
26 | typedef struct ksMatrix3
27 | {
28 | float m[3][3];
29 | } ksMatrix3;
30 |
31 | typedef struct ksMatrix4
32 | {
33 | float m[4][4];
34 | } ksMatrix4;
35 |
36 | #ifdef __cplusplus
37 | extern "C" {
38 | #endif
39 |
40 | unsigned int ksNextPot(unsigned int n);
41 |
42 | void ksMatrixCopy(ksMatrix4 * target, const ksMatrix4 * src);
43 |
44 | int ksMatrixInvert(ksMatrix4 * out, const ksMatrix4 * in);
45 |
46 | void ksMatrixTranspose(ksMatrix4 * result, const ksMatrix4 * src);
47 |
48 | void ksMatrix4ToMatrix3(ksMatrix3 * target, const ksMatrix4 * src);
49 |
50 | void ksMatrixDotVector(ksVec4 * out, const ksMatrix4 * m, const ksVec4 * v);
51 |
52 | //
53 | /// multiply matrix specified by result with a scaling matrix and return new matrix in result
54 | /// result Specifies the input matrix. Scaled matrix is returned in result.
55 | /// sx, sy, sz Scale factors along the x, y and z axes respectively
56 | //
57 | void ksMatrixScale(ksMatrix4 * result, float sx, float sy, float sz);
58 |
59 | //
60 | /// multiply matrix specified by result with a translation matrix and return new matrix in result
61 | /// result Specifies the input matrix. Translated matrix is returned in result.
62 | /// tx, ty, tz Scale factors along the x, y and z axes respectively
63 | //
64 | void ksMatrixTranslate(ksMatrix4 * result, float tx, float ty, float tz);
65 |
66 | //
67 | /// multiply matrix specified by result with a rotation matrix and return new matrix in result
68 | /// result Specifies the input matrix. Rotated matrix is returned in result.
69 | /// angle Specifies the angle of rotation, in degrees.
70 | /// x, y, z Specify the x, y and z coordinates of a vector, respectively
71 | //
72 | void ksMatrixRotate(ksMatrix4 * result, float angle, float x, float y, float z);
73 |
74 | //
75 | /// perform the following operation - result matrix = srcA matrix * srcB matrix
76 | /// result Returns multiplied matrix
77 | /// srcA, srcB Input matrices to be multiplied
78 | //
79 | void ksMatrixMultiply(ksMatrix4 * result, const ksMatrix4 *srcA, const ksMatrix4 *srcB);
80 |
81 | //
82 | //// return an identity matrix
83 | //// result returns identity matrix
84 | //
85 | void ksMatrixLoadIdentity(ksMatrix4 * result);
86 |
87 | //
88 | /// multiply matrix specified by result with a perspective matrix and return new matrix in result
89 | /// result Specifies the input matrix. new matrix is returned in result.
90 | /// fovy Field of view y angle in degrees
91 | /// aspect Aspect ratio of screen
92 | /// nearZ Near plane distance
93 | /// farZ Far plane distance
94 | //
95 | void ksPerspective(ksMatrix4 * result, float fovy, float aspect, float nearZ, float farZ);
96 |
97 | //
98 | /// multiply matrix specified by result with a perspective matrix and return new matrix in result
99 | /// result Specifies the input matrix. new matrix is returned in result.
100 | /// left, right Coordinates for the left and right vertical clipping planes
101 | /// bottom, top Coordinates for the bottom and top horizontal clipping planes
102 | /// nearZ, farZ Distances to the near and far depth clipping planes. These values are negative if plane is behind the viewer
103 | //
104 | void ksOrtho(ksMatrix4 * result, float left, float right, float bottom, float top, float nearZ, float farZ);
105 |
106 | //
107 | // multiply matrix specified by result with a perspective matrix and return new matrix in result
108 | /// result Specifies the input matrix. new matrix is returned in result.
109 | /// left, right Coordinates for the left and right vertical clipping planes
110 | /// bottom, top Coordinates for the bottom and top horizontal clipping planes
111 | /// nearZ, farZ Distances to the near and far depth clipping planes. Both distances must be positive.
112 | //
113 | void ksFrustum(ksMatrix4 * result, float left, float right, float bottom, float top, float nearZ, float farZ);
114 |
115 | void ksLookAt(ksMatrix4 * result, const ksVec3 * eye, const ksVec3 * target, const ksVec3 * up);
116 |
117 | #ifdef __cplusplus
118 | }
119 | #endif
120 |
121 | #endif // __KS_MATRIX_H__
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/ksVector.c:
--------------------------------------------------------------------------------
1 | //
2 | // ksVector.c
3 | //
4 | // Created by kesalin@gmail.com on 12-11-26.
5 | // Copyright (c) 2012. http://blog.csdn.net/kesalin/. All rights reserved.
6 | //
7 |
8 | #include "ksVector.h"
9 | #include
10 |
11 | void ksVectorCopy(ksVec3 * out, const ksVec3 * in)
12 | {
13 | out->x = in->x;
14 | out->y = in->y;
15 | out->z = in->z;
16 | }
17 |
18 | void ksVectorAdd(ksVec3 * out, const ksVec3 * a, const ksVec3 * b)
19 | {
20 | out->x = a->x + b->x;
21 | out->y = a->y + b->y;
22 | out->z = a->z + b->z;
23 | }
24 |
25 | void ksVectorSubtract(ksVec3 * out, const ksVec3 * a, const ksVec3 * b)
26 | {
27 | out->x = a->x - b->x;
28 | out->y = a->y - b->y;
29 | out->z = a->z - b->z;
30 | }
31 |
32 | void ksCrossProduct(ksVec3 * out, const ksVec3 * a, const ksVec3 * b)
33 | {
34 | out->x = a->y * b->z - a->z * b->y;
35 | out->y = a->z * b->x - a->x * b->z;
36 | out->z = a->x * b->y - b->y * a->x;
37 | }
38 |
39 | float ksDotProduct(const ksVec3 * a, const ksVec3 * b)
40 | {
41 | return (a->x * b->x + a->y * b->y + a->z * b->z);
42 | }
43 |
44 | void ksVectorLerp(ksVec3 * out, const ksVec3 * a, const ksVec3 * b, float t)
45 | {
46 | out->x = (a->x * (1 - t) + b->x * t);
47 | out->y = (a->y * (1 - t) + b->y * t);
48 | out->z = (a->z * (1 - t) + b->z * t);
49 | }
50 |
51 | void ksVectorScale(ksVec3 * v, float scale)
52 | {
53 | v->x *= scale;
54 | v->y *= scale;
55 | v->z *= scale;
56 | }
57 |
58 | void ksVectorInverse(ksVec3 * v)
59 | {
60 | v->x = -v->x;
61 | v->y = -v->y;
62 | v->z = -v->z;
63 | }
64 |
65 | void ksVectorNormalize(ksVec3 * v)
66 | {
67 | float length = ksVectorLength(v);
68 | if (length != 0)
69 | {
70 | length = 1.0 / length;
71 | v->x *= length;
72 | v->y *= length;
73 | v->z *= length;
74 | }
75 | }
76 |
77 | int ksVectorCompare(const ksVec3 * a, const ksVec3 * b)
78 | {
79 | if (a == b)
80 | return 1;
81 |
82 | if (a->x != b->x || a->y != b->y || a->z != b->z)
83 | return 0;
84 | return 1;
85 | }
86 |
87 | float ksVectorLength(const ksVec3 * in)
88 | {
89 | return (float)sqrt(in->x * in->x + in->y * in->y + in->z * in->z);
90 | }
91 |
92 | float ksVectorLengthSquared(const ksVec3 * in)
93 | {
94 | return (in->x * in->x + in->y * in->y + in->z * in->z);
95 | }
96 |
97 | float ksVectorDistance(const ksVec3 * a, const ksVec3 * b)
98 | {
99 | ksVec3 v;
100 | ksVectorSubtract(&v, a, b);
101 | return ksVectorLength(&v);
102 | }
103 |
104 | float ksVectorDistanceSquared(const ksVec3 * a, const ksVec3 * b)
105 | {
106 | ksVec3 v;
107 | ksVectorSubtract(&v, a, b);
108 | return (v.x * v.x + v.y * v.y + v.z * v.z);
109 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/ksVector.h:
--------------------------------------------------------------------------------
1 | //
2 | // ksVector.h
3 | //
4 | // Created by kesalin@gmail.com on 12-11-26.
5 | // Copyright (c) 2012. http://blog.csdn.net/kesalin/. All rights reserved.
6 | //
7 |
8 | #ifndef __KS_VECTOR_H__
9 | #define __KS_VECTOR_H__
10 |
11 | typedef struct
12 | {
13 | float x;
14 | float y;
15 | float z;
16 | } ksVec3;
17 |
18 | typedef struct
19 | {
20 | float x;
21 | float y;
22 | float z;
23 | float w;
24 | } ksVec4;
25 |
26 | typedef struct
27 | {
28 | float r;
29 | float g;
30 | float b;
31 | float a;
32 | } ksColor;
33 |
34 | typedef unsigned char byte;
35 |
36 | #ifdef __cplusplus
37 | extern "C" {
38 | #endif
39 |
40 | void ksVectorCopy(ksVec3 * out, const ksVec3 * in);
41 | void ksVectorAdd(ksVec3 * out, const ksVec3 * a, const ksVec3 * b);
42 | void ksVectorSubtract(ksVec3 * out, const ksVec3 * a, const ksVec3 * b);
43 | void ksVectorLerp(ksVec3 * out, const ksVec3 * a, const ksVec3 * b, float t);
44 | void ksCrossProduct(ksVec3 * out, const ksVec3 * a, const ksVec3 * b);
45 | float ksDotProduct(const ksVec3 * a, const ksVec3 * b);
46 |
47 | float ksVectorLengthSquared(const ksVec3 * in);
48 | float ksVectorDistanceSquared(const ksVec3 * a, const ksVec3 * b);
49 |
50 | void ksVectorScale(ksVec3 * v, float scale);
51 | void ksVectorNormalize(ksVec3 * v);
52 | void ksVectorInverse(ksVec3 * v);
53 |
54 | int ksVectorCompare(const ksVec3 * a, const ksVec3 * b);
55 | float ksVectorLength(const ksVec3 * in);
56 | float ksVectorDistance(const ksVec3 * a, const ksVec3 * b);
57 |
58 | #ifdef __cplusplus
59 | }
60 | #endif
61 |
62 | #endif //__KS_VECTOR_H__
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/lineFragmentShader.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | void main()
4 | {
5 | gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
6 | }
7 |
8 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/lineVertexShader.glsl:
--------------------------------------------------------------------------------
1 | attribute vec4 myPosition;
2 |
3 | uniform mat4 modelView;
4 | uniform mat4 projection;
5 |
6 | void main()
7 | {
8 | gl_Position = projection * modelView * myPosition;
9 | }
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // OpenGLESWithoutGLKIt03立方体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKIt03立方体/OpenGLESWithoutGLKIt03立方体/vertexShader.glsl:
--------------------------------------------------------------------------------
1 | attribute vec4 myPosition;
2 |
3 | uniform mat4 modelView;
4 | uniform mat4 projection;
5 |
6 | attribute vec2 textureCoordsIn;
7 |
8 | varying vec2 textureCoordsOut;
9 |
10 |
11 |
12 | void main()
13 | {
14 | gl_Position = projection * modelView * myPosition;
15 | textureCoordsOut = textureCoordsIn;
16 | }
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // OpenGLESWithoutGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // OpenGLESWithoutGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | }
88 | ],
89 | "info" : {
90 | "version" : 1,
91 | "author" : "xcode"
92 | }
93 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/Base.lproj/LaunchScreen.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 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/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 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // OpenGLESWithoutGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // OpenGLESWithoutGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import
11 | #import
12 |
13 |
14 | @interface ViewController ()
15 |
16 | @end
17 |
18 | @implementation ViewController{
19 |
20 | CAEAGLLayer * _myLayer; // 用于显示的layer
21 | EAGLContext * _myContext; // 用于管理状态的context
22 |
23 | GLuint _myFrameBuffer; // 帧缓存
24 | GLuint _myRenderBuffer; // 渲染缓存
25 |
26 | GLuint _myPrograme; // 用于链接着色器的程序
27 | GLuint _myPositionSlot; // 用于向着色器传递顶点数据的槽
28 | }
29 |
30 | - (void)viewDidLoad {
31 | [super viewDidLoad];
32 | // Do any additional setup after loading the view, typically from a nib.
33 |
34 | /*** 创建显示的layer ***/
35 | _myLayer = [[CAEAGLLayer alloc] init];
36 | _myLayer.frame = self.view.frame;
37 | _myLayer.opaque = YES; // 设置为不透明
38 | // 设置kEAGLDrawablePropertyRetainedBacking为NO表示不维持上一次绘制的内容
39 | // kEAGLColorFormatRGBA8表示设置色彩空间为RGBA8
40 | _myLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:@(NO), kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
41 | // 将创建的layer添加到视图
42 | [self.view.layer addSublayer:_myLayer];
43 |
44 | /*** 创建和设置context ***/
45 |
46 | // 创建的context为OpenGL ES 2.0,因为2.0开始支持可编辑管线且大多数苹果设备都支持
47 | _myContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
48 |
49 | if (nil == _myContext) {
50 | NSLog(@"context 创建失败");
51 | }
52 | // 将创建的context设置为当前context
53 | if (![EAGLContext setCurrentContext:_myContext]) {
54 | NSLog(@"context 设置失败");
55 | }
56 |
57 | /*** 申请和绑定渲染缓存和帧缓存 ***/
58 | // 为renderbuffer申请一个id
59 | glGenRenderbuffers(1, &_myRenderBuffer);
60 | // 设置当前的renderbuffer为刚申请的
61 | glBindRenderbuffer(GL_RENDERBUFFER, _myRenderBuffer);
62 | // 为renderbuffer分配存储空间
63 | [_myContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_myLayer];
64 | // 为framebuffer申请一个id
65 | glGenFramebuffers(1, &_myFrameBuffer);
66 | // 设置当前的framebuffer为刚申请的
67 | glBindFramebuffer(GL_FRAMEBUFFER, _myFrameBuffer);
68 | // 将renderbuffer关联到framebuffer上
69 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _myRenderBuffer);
70 |
71 | /*** 编译链接着色器 ***/
72 | [self setupPrograme];
73 |
74 | /*** 设置顶点信息 ***/
75 | // 创建三角形的顶点
76 | GLfloat vertexes[] = {
77 |
78 | -0.5f, -0.5f, 0.0f,
79 | 0.5f, -0.5f, 0.0f,
80 | 0.0f, 0.5f, 0.0f
81 | };
82 | // 设置顶点数据的指针信息
83 | glVertexAttribPointer(_myPositionSlot, // 传入对应数据槽的位置
84 | 3, // 一组有多少数据,即3个一组
85 | GL_FLOAT, // 数据类型
86 | GL_FALSE, // 是否是正交视图
87 | sizeof(GLfloat) * 3, // 数据跨度
88 | vertexes // 顶点数据
89 | );
90 | // 启用顶点数据
91 | glEnableVertexAttribArray(_myPositionSlot);
92 |
93 | /*** 渲染显示三角形 ***/
94 | // 设置显示区域
95 | glViewport(0, 0, self.view.frame.size.width, self.view.frame.size.height);
96 | // 设置清屏颜色
97 | glClearColor(1.0, 1.0, 1.0, 1.0);
98 | glClear(GL_COLOR_BUFFER_BIT);
99 | // 绘制三角开
100 | glDrawArrays(GL_TRIANGLES, 0, 3);
101 | // 显示三角形
102 | [_myContext presentRenderbuffer:GL_RENDERBUFFER];
103 | }
104 |
105 | /**
106 | 编译和链接程序
107 | */
108 | - (void)setupPrograme{
109 |
110 | GLuint vertexShader = [self loadShader:GL_VERTEX_SHADER withFileName:@"vertexShader.glsl"];
111 | GLuint fragmentShader = [self loadShader:GL_FRAGMENT_SHADER withFileName:@"fragmentShader.glsl"];
112 | // 创建一个程序
113 | _myPrograme = glCreateProgram();
114 |
115 | if (!_myPrograme) {
116 | NSLog(@"创建programe失败");
117 | }
118 | // 添加着色器到程序中并链接
119 | glAttachShader(_myPrograme, vertexShader);
120 | glAttachShader(_myPrograme, fragmentShader);
121 | glLinkProgram(_myPrograme);
122 |
123 | GLint success = 0;
124 | // 获取程序信息
125 | glGetProgramiv(_myPrograme, GL_LINK_STATUS, &success);
126 | if (success == GL_FALSE) { // 程序链接失败
127 |
128 | GLint infoLen = 0;
129 | // 获取错误信息长度
130 | glGetProgramiv(_myPrograme, GL_INFO_LOG_LENGTH, &infoLen);
131 | if (infoLen > 0) {
132 | // 申请内存存放错误信息
133 | GLchar * info = malloc(sizeof(GLchar) * infoLen);
134 | glGetProgramInfoLog(_myPrograme, sizeof(GLchar) * infoLen, &infoLen, info);
135 |
136 | NSLog(@"%s", info);
137 | free(info);
138 | }
139 | // 删除链接失败的程序
140 | glDeleteProgram(_myPrograme);
141 | _myPrograme = 0;
142 | return;
143 | }
144 | // 启用程序
145 | glUseProgram(_myPrograme);
146 | // 获取顶点着色器myPosition的内存地址
147 | _myPositionSlot = glGetAttribLocation(_myPrograme, "myPosition");
148 | }
149 |
150 |
151 | /**
152 | 创建和编译着色器
153 |
154 | @param type 着色器类型
155 | @param fileName 着色器文件
156 | @return 返回创建好的着色器,创建失败则返回0
157 | */
158 | - (GLuint)loadShader:(GLenum)type withFileName:(NSString *)fileName{
159 | // 获取文件路径
160 | NSString * path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
161 |
162 | NSError * error;
163 | NSString * shaderString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
164 | // 转成C字符串
165 | const GLchar * shaderStringUTF8 = shaderString.UTF8String;
166 |
167 | if (error) {
168 | NSLog(@"文件读取错误:%@",error);
169 | }
170 | // 创建着色器程序
171 | GLuint shader = glCreateShader(type);
172 | // 给着色器程序传递着色器字符串
173 | glShaderSource(shader, 1, &shaderStringUTF8, NULL);
174 | // 编译
175 | glCompileShader(shader);
176 | // 查看编译情况
177 | GLint compiled = 0;
178 | // 获取着色器信息
179 | glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
180 | if (compiled == GL_FALSE) {
181 |
182 | GLint infoLen = 0;
183 | // 获取错误信息的长度
184 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
185 |
186 | if (infoLen > 0) {
187 |
188 | GLchar * info = malloc(sizeof(GLchar) * infoLen);
189 |
190 | glGetShaderInfoLog(shader, // 对应的着色器
191 | sizeof(GLchar) * infoLen, // buffer的大小
192 | &infoLen, // 传入错误长度
193 | info); // 存放错误信息的内存
194 |
195 | NSLog(@"着色器错误:%s", info);
196 | free(info); // 释放内存
197 | }
198 | // 移除创建失败的着色器
199 | glDeleteShader(shader);
200 | return 0;
201 | }
202 | return shader;
203 | }
204 |
205 |
206 | - (void)didReceiveMemoryWarning {
207 | [super didReceiveMemoryWarning];
208 | // Dispose of any resources that can be recreated.
209 | }
210 |
211 |
212 | @end
213 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/fragmentShader.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float; // 声明精度,在顶点着色器里可以省略精度
2 |
3 | void main()
4 | {
5 | gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // 为了简单这里将颜色设成固定值
6 | }
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // OpenGLESWithoutGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形/vertexShader.glsl:
--------------------------------------------------------------------------------
1 | attribute vec4 myPosition;
2 |
3 | void main()
4 | {
5 | gl_Position = myPosition; // 设置顶点位置信息
6 | }
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形Tests/OpenGLESWithoutGLKit01___Tests.m:
--------------------------------------------------------------------------------
1 | //___FILEHEADER___
2 |
3 | #import
4 |
5 | @interface ___FILEBASENAMEASIDENTIFIER___ : XCTestCase
6 |
7 | @end
8 |
9 | @implementation ___FILEBASENAMEASIDENTIFIER___
10 |
11 | - (void)setUp {
12 | [super setUp];
13 | // Put setup code here. This method is called before the invocation of each test method in the class.
14 | }
15 |
16 | - (void)tearDown {
17 | // Put teardown code here. This method is called after the invocation of each test method in the class.
18 | [super tearDown];
19 | }
20 |
21 | - (void)testExample {
22 | // This is an example of a functional test case.
23 | // Use XCTAssert and related functions to verify your tests produce the correct results.
24 | }
25 |
26 | - (void)testPerformanceExample {
27 | // This is an example of a performance test case.
28 | [self measureBlock:^{
29 | // Put the code you want to measure the time of here.
30 | }];
31 | }
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit01三角形/OpenGLESWithoutGLKit01三角形UITests/OpenGLESWithoutGLKit01___UITests.m:
--------------------------------------------------------------------------------
1 | //___FILEHEADER___
2 |
3 | #import
4 |
5 | @interface ___FILEBASENAMEASIDENTIFIER___ : XCTestCase
6 |
7 | @end
8 |
9 | @implementation ___FILEBASENAMEASIDENTIFIER___
10 |
11 | - (void)setUp {
12 | [super setUp];
13 |
14 | // Put setup code here. This method is called before the invocation of each test method in the class.
15 |
16 | // In UI tests it is usually best to stop immediately when a failure occurs.
17 | self.continueAfterFailure = NO;
18 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
19 | [[[XCUIApplication alloc] init] launch];
20 |
21 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
22 | }
23 |
24 | - (void)tearDown {
25 | // Put teardown code here. This method is called after the invocation of each test method in the class.
26 | [super tearDown];
27 | }
28 |
29 | - (void)testExample {
30 | // Use recording to get started writing UI tests.
31 | // Use XCTAssert and related functions to verify your tests produce the correct results.
32 | }
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // OpenGLESWithoutGLKit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // OpenGLESWithoutGLKit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Assets.xcassets/myImage.imageset/@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Assets.xcassets/myImage.imageset/@2x.png
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Assets.xcassets/myImage.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "scale" : "1x"
6 | },
7 | {
8 | "idiom" : "universal",
9 | "filename" : "@2x.png",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Base.lproj/LaunchScreen.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 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/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 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // OpenGLESWithoutGLKit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // OpenGLESWithoutGLKit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import
11 | #import
12 |
13 | @interface ViewController ()
14 |
15 | @end
16 |
17 | @implementation ViewController{
18 |
19 | EAGLContext * _myContext; // 管理状态的context
20 | CAEAGLLayer * _myLayer; // 用于显示的layer
21 |
22 | GLuint _myFrameBuffer; // 帧缓存
23 | GLuint _myRenderBuffer; // 渲染缓存
24 | GLuint _myTextTure; // 纹理对象
25 |
26 | GLuint _myPrograme; // 用于链接着色器的程序
27 |
28 | GLuint _myPosintSlot; // 顶点数据对应的槽
29 | GLuint _myTextTureCoordSlot; // 纹理坐标对应的槽
30 | }
31 |
32 | - (void)viewDidLoad {
33 | [super viewDidLoad];
34 | // Do any additional setup after loading the view, typically from a nib.
35 | [self setupGLLayer]; //
36 | [self setupGLContext];
37 | [self setupRenderAndFrameBuffer];
38 | [self setupShaders];
39 | [self setupTexture];
40 | [self render4Index];
41 | }
42 |
43 | /**
44 | 设置纹理
45 | */
46 | - (void)setupTexture {
47 |
48 | // 能实现图片翻转
49 | CGImageRef cgImageRef = [UIImage imageNamed:@"myImage"].CGImage;
50 | GLuint width = (GLuint)CGImageGetWidth(cgImageRef);
51 | GLuint height = (GLuint)CGImageGetHeight(cgImageRef);
52 | CGRect rect = CGRectMake(0, 0, width, height);
53 |
54 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
55 | void *data = malloc(width * height * 4);
56 | CGContextRef context = CGBitmapContextCreate(data, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
57 | CGContextTranslateCTM(context, 0, height);
58 | CGContextScaleCTM(context, 1.0f, -1.0f);
59 | CGColorSpaceRelease(colorSpace);
60 | CGContextClearRect(context, rect);
61 | CGContextDrawImage(context, rect, cgImageRef);
62 |
63 |
64 | glEnable(GL_TEXTURE_2D); // 开启2D纹理
65 | // 申请纹理id
66 | glGenTextures(1, &_myTextTure);
67 | // 绑定纹理id
68 | glBindTexture(GL_TEXTURE_2D, _myTextTure);
69 |
70 | // 设置图像拉伸变形时的处理方法
71 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
72 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
73 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
75 | // 将图片数据传递给GL_TEXTURE_2D,因为上面已绑定纹理对象所以会把数据传递给_myTextTure
76 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
77 |
78 | CGContextRelease(context);
79 | free(data);
80 | }
81 |
82 | /**
83 | 使用顶点索引的渲染方法
84 | */
85 | - (void)render4Index {
86 |
87 | GLfloat vertexes[] = {
88 | // 第一个三角形 // 纹理
89 | -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // 左上
90 | -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // 左下
91 | 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // 右下
92 | 0.5f, 0.5f, 0.0f, 1.0f, 1.0f // 右上
93 | };
94 |
95 |
96 | GLbyte indexes[] = {
97 |
98 | 0, 1, 2,
99 | 0, 2, 3
100 | };
101 |
102 | // 设置VBO(顶点缓存)
103 | GLuint bufferVBO;
104 | glGenBuffers(1, &bufferVBO);
105 | glBindBuffer(GL_ARRAY_BUFFER, bufferVBO);
106 | glBufferData(GL_ARRAY_BUFFER, // 目标
107 | sizeof(vertexes), // 顶点数组数据大小
108 | vertexes, // 顶点数组数据
109 | GL_STATIC_DRAW); // 传入VBO数据的使用方式,这里一般设在表态
110 | // 设置索引缓存
111 | GLuint bufferIndex;
112 | glGenBuffers(1, &bufferIndex);
113 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferIndex);
114 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);
115 |
116 |
117 | // 设置图形顶点指针数据(因为使用了VBO所以最后一个参数不用传)
118 | glVertexAttribPointer(_myPosintSlot, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, NULL);
119 | glEnableVertexAttribArray(_myPosintSlot);
120 |
121 | // 设置纹理顶点数据(因为使用了VBO所以最后一个参数不用传)
122 | glVertexAttribPointer(_myTextTureCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (GLfloat *)NULL + 3);
123 | glEnableVertexAttribArray(_myTextTureCoordSlot);
124 |
125 | glClearColor(1.0, 1.0, 1.0, 1.0);
126 | glClear(GL_COLOR_BUFFER_BIT);
127 | glViewport(0, 0, self.view.frame.size.width, self.view.frame.size.height);
128 |
129 | // glDrawArrays(GL_TRIANGLES, 0, 6);
130 | // 用索引绘制顶点
131 | glDrawElements(GL_TRIANGLES, sizeof(indexes) / sizeof(indexes[0]), GL_UNSIGNED_BYTE, 0);
132 | [_myContext presentRenderbuffer:GL_RENDERBUFFER];
133 | }
134 |
135 |
136 | /**
137 | 设置context
138 | */
139 | - (void)setupGLContext {
140 |
141 | _myContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
142 | if (nil == _myContext) {
143 | NSLog(@"创建context失败");
144 | }
145 | if (![EAGLContext setCurrentContext:_myContext]) {
146 | NSLog(@"设置当前context失败");
147 | }
148 | }
149 |
150 |
151 | /**
152 | 设置帧缓存和渲染缓存
153 | */
154 | - (void)setupRenderAndFrameBuffer {
155 |
156 | glGenRenderbuffers(1, &_myRenderBuffer);
157 | glBindRenderbuffer(GL_RENDERBUFFER, _myRenderBuffer);
158 | [_myContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_myLayer];
159 |
160 | glGenFramebuffers(1, &_myFrameBuffer);
161 | glBindFramebuffer(GL_FRAMEBUFFER, _myFrameBuffer);
162 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _myRenderBuffer);
163 | }
164 |
165 |
166 |
167 | /**
168 | 设置显示的layer
169 | */
170 | - (void)setupGLLayer{
171 |
172 | _myLayer = [[CAEAGLLayer alloc] init];
173 | _myLayer.frame = self.view.frame;
174 | _myLayer.opaque = YES; // 可不写,因为默认是YES
175 | _myLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:@(NO), kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
176 |
177 | [self.view.layer addSublayer:_myLayer];
178 | }
179 |
180 |
181 |
182 | /**
183 | 编译和链接着色器程序
184 | */
185 | - (void)setupShaders{
186 |
187 | GLuint vertexShader = [self loadShader:GL_VERTEX_SHADER withFileName:@"vertexShader.glsl"];
188 | GLuint fragmentShader = [self loadShader:GL_FRAGMENT_SHADER withFileName:@"fragmentShader.glsl"];
189 |
190 | _myPrograme = glCreateProgram();
191 |
192 | glAttachShader(_myPrograme, vertexShader);
193 | glAttachShader(_myPrograme, fragmentShader);
194 |
195 | glLinkProgram(_myPrograme);
196 |
197 | GLint linked = GL_FALSE;
198 | glGetProgramiv(_myPrograme, GL_LINK_STATUS, &linked);
199 | if (linked == GL_FALSE) {
200 |
201 | GLint infoLen;
202 | glGetProgramiv(_myPrograme, GL_INFO_LOG_LENGTH, &infoLen);
203 |
204 | if (infoLen > 0) {
205 |
206 | GLchar * info = malloc(sizeof(GLchar) * infoLen);
207 |
208 | glGetProgramInfoLog(_myPrograme, sizeof(GLchar) * infoLen, &infoLen, info);
209 | NSLog(@"%s", info);
210 | free(info);
211 | }
212 | glDeleteProgram(_myPrograme);
213 | _myPrograme = 0;
214 | return;
215 | }
216 | glUseProgram(_myPrograme);
217 | // 获取顶点的槽
218 | _myPosintSlot = glGetAttribLocation(_myPrograme, "myPosition");
219 | // 获取纹理坐标的槽
220 | _myTextTureCoordSlot = glGetAttribLocation(_myPrograme, "textureCoord");
221 |
222 | }
223 |
224 |
225 | /**
226 | 加载和编译着色器
227 |
228 | @param type 着色器类型
229 | @param fileName 着色器代码
230 | @return 返回编译好的着色器
231 | */
232 | - (GLuint)loadShader:(GLenum)type withFileName:(NSString *)fileName{
233 |
234 | NSString * path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
235 | NSError * error;
236 | NSString * shaderString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
237 |
238 | if (error) {
239 | NSLog(@"着色器文件读取失败");
240 | return 0;
241 | }
242 |
243 | const GLchar * shaderStringUTF8 = shaderString.UTF8String;
244 |
245 | GLuint shader = glCreateShader(type);
246 |
247 | glShaderSource(shader, 1, &shaderStringUTF8, NULL);
248 | glCompileShader(shader);
249 |
250 | GLint compiled = GL_FALSE;
251 | glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
252 |
253 | if (compiled == GL_FALSE) {
254 |
255 | GLint infoLen = 0;
256 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
257 | if (infoLen > 0) {
258 |
259 | GLchar * info = malloc(sizeof(GLchar) * infoLen);
260 | glGetShaderInfoLog(shader, sizeof(GLchar) * infoLen, &infoLen, info);
261 |
262 | NSLog(@"%s", info);
263 | free(info);
264 | }
265 | glDeleteShader(shader);
266 | return 0;
267 | }
268 | return shader;
269 | }
270 |
271 |
272 | - (void)didReceiveMemoryWarning {
273 | [super didReceiveMemoryWarning];
274 | // Dispose of any resources that can be recreated.
275 | }
276 |
277 |
278 | @end
279 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/fragmentShader.glsl:
--------------------------------------------------------------------------------
1 | precision mediump float; // 声明精度
2 | // 声明色彩变量
3 | varying vec2 outTextTureCoord;
4 | // 传递图片数据
5 | uniform sampler2D myTexture;
6 |
7 | void main()
8 | {
9 | vec4 color = texture2D(myTexture, outTextTureCoord);
10 | // 通过纹理坐标数据来获取对应坐标色值并传递
11 | gl_FragColor = vec4(color.rgb, 1.0);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // OpenGLESWithoutGLKit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/texture.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/texture.png
--------------------------------------------------------------------------------
/OpenGLESWithoutGLKit02纹理/OpenGLESWithoutGLKit02纹理/vertexShader.glsl:
--------------------------------------------------------------------------------
1 | attribute vec4 myPosition; // 顶点位置
2 | // 输入的色彩
3 | attribute vec2 textureCoord;
4 | // 输出的色彩
5 | varying vec2 outTextTureCoord;
6 |
7 | void main()
8 | {
9 | gl_Position = myPosition; // 传递顶点位置数据
10 | outTextTureCoord = textureCoord; // 传递色彩数据
11 | }
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # learnOpenGL-ES
2 | 一个学习OpenGL ES的练习代码,可供大家参考学习
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 |
28 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // learnOpenGLESGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // learnOpenGLESGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/Base.lproj/LaunchScreen.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 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/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 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // learnOpenGLESGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @interface ViewController : GLKViewController
13 |
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // learnOpenGLESGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import
11 | #import
12 |
13 | typedef struct{
14 | GLKVector3 position;
15 | } Vertex;
16 |
17 |
18 | Vertex vertexs[] = {
19 | {-0.5f, -0.5f, 0.0f},
20 | {0.5f, -0.5f, 0.0f},
21 | {-0.5f, 0.5f, 0.0f},
22 | {-0.5f, 0.5f, 0.0f},
23 | {0.5f, -0.5f, 0.0f},
24 | {0.5f, 0.5f, 0.0f},
25 | };
26 |
27 | GLfloat vertexes[] = {
28 |
29 | -0.5f, -0.5f, 0.0f,
30 | 0.5f, -0.5f, 0.0f,
31 | 0.0f, 0.5f, 0.0f
32 | };
33 |
34 | @interface ViewController ()
35 |
36 | @property (nonatomic,strong)GLKBaseEffect * effect;
37 | @property (nonatomic,strong)EAGLContext * context;
38 |
39 | @end
40 |
41 | @implementation ViewController{
42 |
43 | GLuint _vertexBuffber;
44 | }
45 |
46 | - (void)viewDidLoad {
47 | [super viewDidLoad];
48 | // Do any additional setup after loading the view, typically from a nib.
49 |
50 | // 获取控制器的view
51 | GLKView * glView = (GLKView *)self.view;
52 | // 设置当前的context
53 | EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
54 | if (nil == context) {
55 | NSLog(@"context create failed");
56 | }
57 | self.context = context;
58 | glView.context = context;
59 |
60 | if (![EAGLContext setCurrentContext:context]) {
61 | NSLog(@"set currentContext failed");
62 | }
63 | // 创建GLKBaseEffect
64 | self.effect = [[GLKBaseEffect alloc] init];
65 | // 设置使用的颜色
66 | self.effect.useConstantColor = GL_TRUE;
67 | self.effect.constantColor = GLKVector4Make(1.0f, 0.0f, 0.0f, 1.0f);
68 | // 设置清屏颜色
69 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
70 |
71 | // glGenBuffers(1, &_vertexBuffber);
72 | // glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffber);
73 | // glBufferData(GL_ARRAY_BUFFER, sizeof(vertexs), vertexs, GL_STATIC_DRAW);
74 |
75 | // 开启顶点
76 | glEnableVertexAttribArray(GLKVertexAttribPosition);
77 | // 设置数据指针
78 | glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, vertexes);
79 | // glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL);
80 |
81 | }
82 |
83 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
84 |
85 | glClear(GL_COLOR_BUFFER_BIT);
86 |
87 | [self.effect prepareToDraw];
88 |
89 | glDrawArrays(GL_TRIANGLES, 0, 3);
90 | }
91 |
92 | - (void)didReceiveMemoryWarning {
93 | [super didReceiveMemoryWarning];
94 | // Dispose of any resources that can be recreated.
95 | }
96 |
97 |
98 | @end
99 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // learnOpenGLESGLKit01三角形
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/triangle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形/triangle.png
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形Tests/learnOpenGLESGLKit01___Tests.m:
--------------------------------------------------------------------------------
1 | //___FILEHEADER___
2 |
3 | #import
4 |
5 | @interface ___FILEBASENAMEASIDENTIFIER___ : XCTestCase
6 |
7 | @end
8 |
9 | @implementation ___FILEBASENAMEASIDENTIFIER___
10 |
11 | - (void)setUp {
12 | [super setUp];
13 | // Put setup code here. This method is called before the invocation of each test method in the class.
14 | }
15 |
16 | - (void)tearDown {
17 | // Put teardown code here. This method is called after the invocation of each test method in the class.
18 | [super tearDown];
19 | }
20 |
21 | - (void)testExample {
22 | // This is an example of a functional test case.
23 | // Use XCTAssert and related functions to verify your tests produce the correct results.
24 | }
25 |
26 | - (void)testPerformanceExample {
27 | // This is an example of a performance test case.
28 | [self measureBlock:^{
29 | // Put the code you want to measure the time of here.
30 | }];
31 | }
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit01三角形/learnOpenGLESGLKit01三角形UITests/learnOpenGLESGLKit01___UITests.m:
--------------------------------------------------------------------------------
1 | //___FILEHEADER___
2 |
3 | #import
4 |
5 | @interface ___FILEBASENAMEASIDENTIFIER___ : XCTestCase
6 |
7 | @end
8 |
9 | @implementation ___FILEBASENAMEASIDENTIFIER___
10 |
11 | - (void)setUp {
12 | [super setUp];
13 |
14 | // Put setup code here. This method is called before the invocation of each test method in the class.
15 |
16 | // In UI tests it is usually best to stop immediately when a failure occurs.
17 | self.continueAfterFailure = NO;
18 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
19 | [[[XCUIApplication alloc] init] launch];
20 |
21 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
22 | }
23 |
24 | - (void)tearDown {
25 | // Put teardown code here. This method is called after the invocation of each test method in the class.
26 | [super tearDown];
27 | }
28 |
29 | - (void)testExample {
30 | // Use recording to get started writing UI tests.
31 | // Use XCTAssert and related functions to verify your tests produce the correct results.
32 | }
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // learnOpenGLESGLKit03
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // learnOpenGLESGLKit03
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | }
43 | ],
44 | "info" : {
45 | "version" : 1,
46 | "author" : "xcode"
47 | }
48 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Assets.xcassets/timg.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "timg.jpeg",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "universal",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Assets.xcassets/timg.imageset/timg.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Assets.xcassets/timg.imageset/timg.jpeg
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Base.lproj/LaunchScreen.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 |
28 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/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 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleDisplayName
8 | 正方体
9 | CFBundleExecutable
10 | $(EXECUTABLE_NAME)
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleVersion
22 | 1
23 | LSRequiresIPhoneOS
24 |
25 | UILaunchStoryboardName
26 | LaunchScreen
27 | UIMainStoryboardFile
28 | Main
29 | UIRequiredDeviceCapabilities
30 |
31 | armv7
32 |
33 | UISupportedInterfaceOrientations
34 |
35 | UIInterfaceOrientationPortrait
36 | UIInterfaceOrientationLandscapeLeft
37 | UIInterfaceOrientationLandscapeRight
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // learnOpenGLESGLKit03
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @interface ViewController : GLKViewController
13 |
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // learnOpenGLESGLKit03
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import
11 | #import
12 |
13 | #define kLimitDegree 35.0f
14 |
15 | typedef struct {
16 |
17 | GLKVector3 position;
18 | GLKVector2 texturePosition;
19 | } Vertex;
20 |
21 |
22 | @interface ViewController ()
23 |
24 | @property(nonatomic, strong)GLKBaseEffect * effect;
25 |
26 | @property(nonatomic, assign)CGFloat degreeY;
27 | @property(nonatomic, assign)CGFloat degreeX;
28 |
29 | @property(nonatomic, assign)CGFloat scale;
30 |
31 | @end
32 |
33 | @implementation ViewController
34 |
35 |
36 | - (void)viewDidLoad {
37 | [super viewDidLoad];
38 |
39 | self.scale = self.view.frame.size.width / self.view.frame.size.height;
40 | [self setupViewAndContext];
41 |
42 | self.effect = [[GLKBaseEffect alloc] init];
43 | // 想画只有线绘成的立方体就下面注释的代码打开,并将相同的代码注释掉和将纹理的代码也注释掉就可以了,下面注释的代码也做这样的操作
44 | // self.effect.useConstantColor = GL_TRUE;
45 | // self.effect.constantColor = GLKVector4Make(1.0, 0.0, 0.0, 1.0);
46 |
47 | [self setupVertex];
48 | [self setupTexture2D];
49 |
50 | glClearColor(1.0, 1.0, 1.0, 1.0);
51 |
52 | // 添加深度测试,别忘了在glClear里添加GL_DEPTH_BUFFER_BIT,不然会造成无法显示
53 | glEnable(GL_DEPTH_TEST);
54 |
55 | glViewport(0, 0, self.view.frame.size.width, self.view.frame.size.height);
56 |
57 | [self setupBaseTransform];
58 |
59 | }
60 |
61 |
62 | /**
63 | 设置GLKView和context
64 | */
65 | - (void)setupViewAndContext {
66 |
67 | GLKView * glView = (GLKView *)self.view;
68 | glView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
69 | glView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
70 |
71 | EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
72 | if (!context) {
73 | NSLog(@"context 创建失败");
74 | }
75 | if (![EAGLContext setCurrentContext:context]) {
76 | NSLog(@"设置当前context失败");
77 | }
78 |
79 | glView.context = context;
80 | }
81 |
82 |
83 | /**
84 | 设置顶点数据
85 | */
86 | - (void)setupVertex {
87 |
88 | // 设置顶点数据
89 | GLfloat vertexes[] = {
90 | // 前面
91 | -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // 前左上 0
92 | -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // 前左下 1
93 | 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // 前右下 2
94 | 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // 前右上 3
95 | // 后面
96 | -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // 后左上 4
97 | -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // 后左下 5
98 | 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // 后右下 6
99 | 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // 后右上 7
100 | // 左面
101 | -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // 后左上 8
102 | -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // 后左下 9
103 | -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // 前左上 10
104 | -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // 前左下 11
105 | // 右面
106 | 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // 前右上 12
107 | 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // 前右下 13
108 | 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // 后右下 14
109 | 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // 后右上 15
110 | // 上面
111 | -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, // 前左上 16
112 | 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // 前右上 17
113 | -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // 后左上 18
114 | 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // 后右上 19
115 | // 下面
116 | -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, // 前左下 20
117 | 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, // 前右下 21
118 | -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // 后左下 22
119 | 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // 后右下 23
120 | };
121 |
122 | // 索引
123 | const GLbyte indexes[] = {
124 | // 前面
125 | 0, 1, 2,
126 | 0, 2, 3,
127 | // 后面
128 | 4, 5, 6,
129 | 4, 6, 7,
130 | // 左面
131 | 8, 9, 11,
132 | 8, 11, 10,
133 | // 右面
134 | 12, 13, 14,
135 | 12, 14, 15,
136 | // 上面
137 | 18, 16, 17,
138 | 18, 17, 19,
139 | // 下面
140 | 20, 22, 23,
141 | 20, 23, 21
142 | };
143 |
144 |
145 | // const GLfloat vertexes[] = {
146 | //
147 | // -0.5f, 0.5f, 0.5f, // 前左上 0
148 | // -0.5f, -0.5f, 0.5f, // 前左下 1
149 | // 0.5f, -0.5f, 0.5f, // 前右下 2
150 | // 0.5f, 0.5f, 0.5f, // 前右上 3
151 | // // 后面
152 | // -0.5f, 0.5f, -0.5f, // 后左上 4
153 | // -0.5f, -0.5f, -0.5f, // 后左下 5
154 | // 0.5f, -0.5f, -0.5f, // 后右下 6
155 | // 0.5f, 0.5f, -0.5f // 后右上 7
156 | // };
157 | //
158 | // const GLbyte indexes[] = {
159 | // 0, 1,
160 | // 1, 2,
161 | // 2, 3,
162 | // 3, 0,
163 | //
164 | // 4, 5,
165 | // 5, 6,
166 | // 6, 7,
167 | // 7, 4,
168 | //
169 | // 0, 4,
170 | // 1, 5,
171 | // 2, 6,
172 | // 3, 7
173 | // };
174 |
175 |
176 | // 创建VBO并传递顶点数据
177 | GLuint buffer;
178 | glGenBuffers(1, &buffer);
179 | glBindBuffer(GL_ARRAY_BUFFER, buffer);
180 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
181 |
182 | // 创建VBO并传递顶点索引(顶点索引不用设置指针参数)
183 | GLuint indexBuffer;
184 | glGenBuffers(1, &indexBuffer);
185 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
186 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);
187 |
188 | // 设置顶点指针数据参数
189 | glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, NULL);
190 | // glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
191 |
192 | // 启用顶点数据
193 | glEnableVertexAttribArray(GLKVertexAttribPosition);
194 |
195 | // 设置纹理指针坐标信息
196 | glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (GLfloat *)NULL + 3);
197 | glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
198 | }
199 |
200 |
201 |
202 | /**
203 | 设置纹理
204 | */
205 | - (void)setupTexture2D{
206 |
207 | UIImage * image = [UIImage imageNamed:@"timg"];
208 |
209 | NSDictionary * option = [NSDictionary dictionaryWithObjectsAndKeys:@(YES),GLKTextureLoaderOriginBottomLeft, nil];
210 | NSError * error;
211 | GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithCGImage:image.CGImage options:option error:&error];
212 |
213 | if (error) {
214 | NSLog(@"%@",error);
215 | }
216 |
217 | self.effect.texture2d0.enabled = GL_TRUE;
218 | self.effect.texture2d0.name = textureInfo.name;
219 | }
220 |
221 |
222 | /**
223 | 设置初始的视图变换
224 | */
225 | - (void)setupBaseTransform{
226 |
227 | // 设置基础变换
228 | GLKMatrix4 mat = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -3.0f);
229 |
230 | mat = GLKMatrix4RotateY(mat, GLKMathDegreesToRadians(0));
231 |
232 | GLKMatrix4 temMat = GLKMatrix4RotateX(mat, GLKMathDegreesToRadians(0));
233 |
234 | self.effect.transform.modelviewMatrix = temMat;
235 |
236 | // 设置视角变换(添加该方法后可解决图形因屏幕而被拉伸的问题)
237 | float aspect = self.view.frame.size.width / self.view.frame.size.height;
238 |
239 | GLKMatrix4 matPersPective = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60.0), aspect, 0.1f, 100.0f);
240 |
241 | self.effect.transform.projectionMatrix = matPersPective;
242 | }
243 |
244 |
245 |
246 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
247 |
248 | UITouch * touch = touches.anyObject;
249 | CGPoint currentPoint = [touch locationInView:self.view];
250 | CGPoint previousPoint = [touch previousLocationInView:self.view];
251 |
252 | self.degreeY += currentPoint.y - previousPoint.y;
253 | self.degreeX += currentPoint.x - previousPoint.x;
254 | if (self.degreeY > kLimitDegree) {
255 | self.degreeY = kLimitDegree;
256 | }
257 | if (self.degreeY < -kLimitDegree) {
258 | self.degreeY = -kLimitDegree;
259 | }
260 | }
261 |
262 |
263 | /**
264 | 系统会调用些方法
265 | */
266 | - (void)update{
267 |
268 | // 设置物体变换 (让物体远离是为了能看全,因为摄像机默认在0,0,0点,即在物体内部)
269 | GLKMatrix4 mat = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -3.0f);
270 |
271 | mat = GLKMatrix4RotateX(mat, GLKMathDegreesToRadians(self.degreeY));
272 |
273 | GLKMatrix4 temMat = GLKMatrix4RotateY(mat, GLKMathDegreesToRadians(self.degreeX));
274 |
275 | self.effect.transform.modelviewMatrix = temMat;
276 | }
277 |
278 |
279 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
280 |
281 | // 清屏和清除深度缓存
282 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
283 |
284 | [self.effect prepareToDraw];
285 |
286 | glDrawElements(GL_TRIANGLES, // 绘制顶点的模式
287 | 36, // 索引的个数
288 | GL_UNSIGNED_BYTE, // 索引的数据类型
289 | 0); // 索引从第几个开始
290 |
291 | // glDrawElements(GL_LINES, // 绘制顶点的模式
292 | // 24, // 索引的个数
293 | // GL_UNSIGNED_BYTE, // 索引的数据类型
294 | // 0); // 索引从第几个开始
295 | }
296 |
297 |
298 | - (void)didReceiveMemoryWarning {
299 | [super didReceiveMemoryWarning];
300 | // Dispose of any resources that can be recreated.
301 | }
302 |
303 |
304 | @end
305 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // learnOpenGLESGLKit03
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03Tests/learnOpenGLESGLKit03Tests.m:
--------------------------------------------------------------------------------
1 | //
2 | // learnOpenGLESGLKit03Tests.m
3 | // learnOpenGLESGLKit03Tests
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface learnOpenGLESGLKit03Tests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation learnOpenGLESGLKit03Tests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 | // Put setup code here. This method is called before the invocation of each test method in the class.
20 | }
21 |
22 | - (void)tearDown {
23 | // Put teardown code here. This method is called after the invocation of each test method in the class.
24 | [super tearDown];
25 | }
26 |
27 | - (void)testExample {
28 | // This is an example of a functional test case.
29 | // Use XCTAssert and related functions to verify your tests produce the correct results.
30 | }
31 |
32 | - (void)testPerformanceExample {
33 | // This is an example of a performance test case.
34 | [self measureBlock:^{
35 | // Put the code you want to measure the time of here.
36 | }];
37 | }
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit03立方体/learnOpenGLESGLKit03UITests/learnOpenGLESGLKit03UITests.m:
--------------------------------------------------------------------------------
1 | //
2 | // learnOpenGLESGLKit03UITests.m
3 | // learnOpenGLESGLKit03UITests
4 | //
5 | // Created by 刘晓亮 on 2017/8/17.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface learnOpenGLESGLKit03UITests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation learnOpenGLESGLKit03UITests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 |
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 |
22 | // In UI tests it is usually best to stop immediately when a failure occurs.
23 | self.continueAfterFailure = NO;
24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
25 | [[[XCUIApplication alloc] init] launch];
26 |
27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
28 | }
29 |
30 | - (void)tearDown {
31 | // Put teardown code here. This method is called after the invocation of each test method in the class.
32 | [super tearDown];
33 | }
34 |
35 | - (void)testExample {
36 | // Use recording to get started writing UI tests.
37 | // Use XCTAssert and related functions to verify your tests produce the correct results.
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // learnOpenGLESGLKit05球体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // learnOpenGLESGLKit05球体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Assets.xcassets/timg.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "filename" : "timg.jpeg",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "universal",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Assets.xcassets/timg.imageset/timg.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Assets.xcassets/timg.imageset/timg.jpeg
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Base.lproj/LaunchScreen.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 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/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 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // learnOpenGLESGLKit05球体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @interface ViewController : GLKViewController
13 |
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // learnOpenGLESGLKit05球体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import
11 | #import
12 |
13 | #define kDevidCount 80
14 |
15 | #define kLimitDegreeUpDown 80.0
16 |
17 |
18 | typedef struct{
19 | GLfloat position[3];
20 | GLfloat texturePosition[2];
21 | } Vertex;
22 |
23 |
24 | @interface ViewController ()
25 |
26 | @property (nonatomic,strong)GLKBaseEffect * effect;
27 |
28 | @property(nonatomic,assign)GLint degreeX;
29 | @property(nonatomic,assign)GLint degreeY;
30 |
31 | @end
32 |
33 | @implementation ViewController{
34 |
35 | Vertex * _cirleVertex;
36 | GLuint * _vertextIndex;
37 |
38 | GLKMatrix4 _modelMatrix;
39 |
40 | GLuint _bufferVBO;
41 | GLuint _bufferIndexVBO;
42 | }
43 |
44 | - (void)viewDidLoad {
45 | [super viewDidLoad];
46 | // Do any additional setup after loading the view, typically from a nib.
47 |
48 | GLKView * glView = (GLKView *)self.view;
49 |
50 | EAGLContext * contex = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
51 |
52 | if (!contex) {
53 | NSLog(@"context创建失败");
54 | }
55 | if (![EAGLContext setCurrentContext:contex]) {
56 | NSLog(@"设置当前context失败");
57 | }
58 |
59 | glView.context = contex;
60 | glView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
61 |
62 | self.effect = [[GLKBaseEffect alloc] init];
63 | self.effect.useConstantColor = GL_TRUE;
64 | self.effect.constantColor = GLKVector4Make(0.8, 0.8, 0.8, 1.0);
65 |
66 | glEnable(GL_DEPTH_TEST);
67 | [self setupLighting];
68 | [self setupTexture];
69 | [self setupBufferVBO];
70 |
71 | glClearColor(0.3, 0.3, 0.3, 1.0);
72 |
73 | // 设置视角和物体的矩阵变换
74 | GLfloat aspect = self.view.frame.size.width / self.view.frame.size.height;
75 |
76 | self.effect.transform.modelviewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0, 0.0, -3);
77 |
78 | self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), aspect, 0.1f, 10.0f);
79 | }
80 |
81 |
82 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
83 |
84 | UITouch * touch = touches.anyObject;
85 | CGPoint currentPoint = [touch locationInView:self.view];
86 | CGPoint previousPoint = [touch previousLocationInView:self.view];
87 |
88 | self.degreeX += currentPoint.x - previousPoint.x;
89 | self.degreeY += currentPoint.y - previousPoint.y;
90 |
91 | // 限制上下转动的角度
92 | if (self.degreeY > kLimitDegreeUpDown) {
93 | self.degreeY = kLimitDegreeUpDown;
94 | }
95 |
96 | if (self.degreeY < -kLimitDegreeUpDown) {
97 | self.degreeY = -kLimitDegreeUpDown;
98 | }
99 |
100 | }
101 |
102 |
103 |
104 | /**
105 | 设置纹理
106 | */
107 | - (void)setupTexture{
108 |
109 | // 加载纹理图片
110 | NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@(1), GLKTextureLoaderOriginBottomLeft, nil];//GLKTextureLoaderOriginBottomLeft 纹理坐标系是相反的
111 | NSError * error;
112 |
113 | CGImageRef image = [UIImage imageNamed:@"timg"].CGImage;
114 | GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithCGImage:image options:options error:&error];
115 |
116 | // 设置纹理可用
117 | self.effect.texture2d0.enabled = GL_TRUE;
118 | // 传递纹理信息
119 | self.effect.texture2d0.name = textureInfo.name;
120 | self.effect.texture2d0.target = textureInfo.target;
121 | }
122 |
123 |
124 | /**
125 | 设置顶点缓存VBO
126 | */
127 | - (void)setupBufferVBO {
128 |
129 | // 获取球的顶点和索引
130 | _cirleVertex = [self getBallDevidNum:kDevidCount];
131 | _vertextIndex = [self getBallVertexIndex:kDevidCount];
132 |
133 | // 设置VBO
134 | glGenBuffers(1, &_bufferVBO);
135 | glBindBuffer(GL_ARRAY_BUFFER, _bufferVBO);
136 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * (kDevidCount + 1) * (kDevidCount / 2 + 1), _cirleVertex, GL_STATIC_DRAW);
137 |
138 | glGenBuffers(1, &_bufferIndexVBO);
139 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _bufferIndexVBO);
140 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * kDevidCount * (kDevidCount + 1), _vertextIndex, GL_STATIC_DRAW);
141 |
142 |
143 | glEnableVertexAttribArray(GLKVertexAttribPosition);
144 | glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)NULL);
145 | // 设置法线
146 | glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)NULL);
147 | glEnableVertexAttribArray(GLKVertexAttribNormal);
148 |
149 |
150 | // 设置纹理坐标
151 | glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
152 | glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLfloat *)NULL + 3);
153 | // 释放顶点数据
154 | free(_cirleVertex);
155 | free(_vertextIndex);
156 | }
157 |
158 | /**
159 | 设置光照
160 | */
161 | - (void)setupLighting{
162 |
163 | self.effect.light0.enabled = GL_TRUE;
164 | self.effect.light0.position = GLKVector4Make(1.0, 0.8, 0.8, 0.0);
165 |
166 | self.effect.light0.ambientColor = GLKVector4Make(0.5, 0.5, 0.5, 1.0);
167 | self.effect.light0.diffuseColor = GLKVector4Make(1.0, 1.0, 1.0, 1.0);
168 | }
169 |
170 |
171 | /**
172 | 绘制一个圆环的顶点数组
173 |
174 | @param num 要多少个顶点
175 | @return 返回顶点数组
176 | */
177 | - (Vertex *)getCirleDevidNum:(GLint) num{
178 |
179 | float delta = 2 * M_PI / num;
180 | float myScale = 0.5; // 半径
181 | float tempY;
182 | float tempX;
183 |
184 | Vertex * cirleVertex = malloc(sizeof(Vertex) * num);
185 | memset(cirleVertex, 0x00, sizeof(Vertex) * num);
186 |
187 | for (int i = 0; i < num; i++) {
188 |
189 | tempY = myScale * sin(delta * i);
190 | tempX = myScale * cos(delta * i);
191 |
192 | cirleVertex[i] = (Vertex){tempX, tempY, 0.0f};
193 | }
194 | return cirleVertex;
195 | }
196 |
197 |
198 |
199 | /**
200 | 绘制一个球的顶点
201 |
202 | @param num 传入要生成的顶点的一层的个数(最后生成的顶点个数为 num * num)
203 | @return 返回生成后的顶点
204 | */
205 | - (Vertex *)getBallDevidNum:(GLint) num{
206 |
207 | if (num % 2 == 1) {
208 | return 0;
209 | }
210 |
211 | GLfloat delta = 2 * M_PI / num; // 分割的份数
212 | GLfloat ballRaduis = 0.8; // 球的半径
213 | GLfloat pointZ;
214 | GLfloat pointX;
215 | GLfloat pointY;
216 | GLfloat textureY;
217 | GLfloat textureX;
218 | GLfloat textureYdelta = 1.0 / (num / 2);
219 | GLfloat textureXdelta = 1.0 / num;
220 | GLint layerNum = num / 2.0 + 1; // 层数
221 | GLint perLayerNum = num + 1; // 要让点再加到起点所以num + 1
222 |
223 | Vertex * cirleVertex = malloc(sizeof(Vertex) * perLayerNum * layerNum);
224 | memset(cirleVertex, 0x00, sizeof(Vertex) * perLayerNum * layerNum);
225 |
226 | // 层数
227 | for (int i = 0; i < layerNum; i++) {
228 | // 每层的高度(即pointY),为负数让其从下向上创建
229 | pointY = -ballRaduis * cos(delta * i);
230 |
231 | // 每层的半径
232 | GLfloat layerRaduis = ballRaduis * sin(delta * i);
233 | // 每层圆的点,
234 | for (int j = 0; j < perLayerNum; j++) {
235 | // 计算
236 | pointX = layerRaduis * cos(delta * j);
237 | pointZ = layerRaduis * sin(delta * j);
238 | textureX = textureXdelta * j;
239 | textureY = textureYdelta * i;
240 |
241 | cirleVertex[i * perLayerNum + j] = (Vertex){pointX, pointY, pointZ, textureX, textureY};
242 | }
243 | }
244 |
245 | return cirleVertex;
246 | }
247 |
248 |
249 |
250 | - (GLuint *)getBallVertexIndex:(GLint)num{
251 |
252 | // 每层要多原点两次
253 | GLint sizeNum = sizeof(GLuint) * (num + 1) * (num + 1);
254 |
255 | GLuint * ballVertexIndex = malloc(sizeNum);
256 | memset(ballVertexIndex, 0x00, sizeNum);
257 | GLint layerNum = num / 2 + 1;
258 | GLint perLayerNum = num + 1; // 要让点再加到起点所以num + 1
259 |
260 | for (int i = 0; i < layerNum; i++) {
261 |
262 |
263 | if (i + 1 < layerNum) {
264 |
265 | for (int j = 0; j < perLayerNum; j++) {
266 |
267 | // i * perLayerNum * 2每层的下标是原来的2倍
268 | ballVertexIndex[(i * perLayerNum * 2) + (j * 2)] = i * perLayerNum + j;
269 | // 后一层数据
270 | ballVertexIndex[(i * perLayerNum * 2) + (j * 2 + 1)] = (i + 1) * perLayerNum + j;
271 | }
272 | } else {
273 |
274 | for (int j = 0; j < perLayerNum; j++) {
275 | // 后最一层数据单独处理
276 | ballVertexIndex[i * perLayerNum * 2 + j] = i * perLayerNum + j;
277 | }
278 | }
279 | }
280 |
281 | return ballVertexIndex;
282 | }
283 |
284 |
285 |
286 | - (void)update{
287 |
288 | _modelMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0, 0.0, -3);
289 |
290 | _modelMatrix = GLKMatrix4RotateX(_modelMatrix, GLKMathDegreesToRadians(self.degreeY % 360));
291 |
292 | _modelMatrix = GLKMatrix4RotateY(_modelMatrix, GLKMathDegreesToRadians(self.degreeX % 360));
293 |
294 | self.effect.transform.modelviewMatrix = _modelMatrix;
295 | }
296 |
297 |
298 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
299 |
300 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
301 |
302 | [self.effect prepareToDraw];
303 |
304 | // 绘制一个圆环
305 | // glDrawArrays(GL_LINE_LOOP, 0, kDevidCount * (kDevidCount / 2));
306 | // 绘制一个圆
307 | // glDrawArrays(GL_TRIANGLE_FAN, 0, kDevidCount * (kDevidCount / 2));
308 |
309 | // 绘制一个球(用层表示)
310 | // glDrawArrays(GL_LINE_LOOP, 0, (kDevidCount + 1) * (kDevidCount / 2 + 1));
311 |
312 | // 绘制一个球
313 | glDrawElements(GL_TRIANGLE_STRIP, kDevidCount * (kDevidCount + 1), GL_UNSIGNED_INT, 0);
314 | }
315 |
316 | - (void)didReceiveMemoryWarning {
317 | [super didReceiveMemoryWarning];
318 | // Dispose of any resources that can be recreated.
319 | }
320 |
321 |
322 | @end
323 |
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/ball.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/ball.gif
--------------------------------------------------------------------------------
/learnOpenGLESGLKit05球体/learnOpenGLESGLKit05球体/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // learnOpenGLESGLKit05球体
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // learnOpenGLESGLkit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // learnOpenGLESGLkit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // 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.
26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
27 | }
28 |
29 |
30 | - (void)applicationDidEnterBackground:(UIApplication *)application {
31 | // 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.
32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 | }
34 |
35 |
36 | - (void)applicationWillEnterForeground:(UIApplication *)application {
37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
38 | }
39 |
40 |
41 | - (void)applicationDidBecomeActive:(UIApplication *)application {
42 | // 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.
43 | }
44 |
45 |
46 | - (void)applicationWillTerminate:(UIApplication *)application {
47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 | }
49 |
50 |
51 | @end
52 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Assets.xcassets/myImage.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "scale" : "1x"
6 | },
7 | {
8 | "idiom" : "universal",
9 | "filename" : "myImage@2x.png",
10 | "scale" : "2x"
11 | },
12 | {
13 | "idiom" : "universal",
14 | "scale" : "3x"
15 | }
16 | ],
17 | "info" : {
18 | "version" : 1,
19 | "author" : "xcode"
20 | }
21 | }
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Assets.xcassets/myImage.imageset/myImage@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Assets.xcassets/myImage.imageset/myImage@2x.png
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Base.lproj/LaunchScreen.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 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/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 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // learnOpenGLESGLkit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @interface ViewController : GLKViewController
13 |
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // learnOpenGLESGLkit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import
11 | #import
12 |
13 | typedef struct{
14 | GLKVector3 position;
15 | } Vertex;
16 |
17 | typedef struct{
18 | GLKVector2 position;
19 | } TextureCoord;
20 |
21 | Vertex vertex[] = {
22 |
23 | {-0.5f, 0.5f, 0.0f}, // 左上
24 | {-0.5f, -0.5f, 0.0f}, // 左下
25 | {0.5f, -0.5f, 0.0f}, // 右下
26 |
27 | {-0.5f, 0.5f, 0.0f}, // 左上
28 | {0.5f, -0.5f, 0.0f}, // 右下
29 | {0.5f, 0.5f, 0.0f}, // 右上
30 | };
31 |
32 | TextureCoord textrueCoord[] = {
33 |
34 | {0.0f, 1.0f}, // 左上
35 | {0.0f, 0.0f}, // 左下
36 | {1.0f, 0.0f}, // 右下
37 |
38 | {0.0f, 1.0f}, // 左上
39 | {1.0f, 0.0f}, // 右下
40 | {1.0f, 1.0f}, // 右上
41 | };
42 |
43 | @interface ViewController ()
44 |
45 | @property (nonatomic,strong)GLKBaseEffect * effect;
46 |
47 | @end
48 |
49 | @implementation ViewController
50 |
51 | - (void)viewDidLoad {
52 | [super viewDidLoad];
53 | // Do any additional setup after loading the view, typically from a nib.
54 |
55 | GLKView * glView = (GLKView *)self.view;
56 |
57 | EAGLContext * contex = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
58 |
59 | if (!contex) {
60 | NSLog(@"context创建失败");
61 | }
62 | if (![EAGLContext setCurrentContext:contex]) {
63 | NSLog(@"设置当前context失败");
64 | }
65 |
66 | glView.context = contex;
67 | glView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
68 |
69 |
70 | self.effect = [[GLKBaseEffect alloc] init];
71 |
72 | // 加载纹理图片
73 | NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@(1), GLKTextureLoaderOriginBottomLeft, nil];//GLKTextureLoaderOriginBottomLeft 纹理坐标系是相反的
74 | NSError * error;
75 |
76 | CGImageRef image = [UIImage imageNamed:@"myImage"].CGImage;
77 | GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithCGImage:image options:options error:&error];
78 |
79 | // 设置纹理可用
80 | self.effect.texture2d0.enabled = GL_TRUE;
81 | // 传递纹理信息
82 | self.effect.texture2d0.name = textureInfo.name;
83 |
84 | // 设置顶点
85 | glEnableVertexAttribArray(GLKVertexAttribPosition);
86 | glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, vertex);
87 | // 设置纹理坐标
88 | glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
89 | glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, textrueCoord);
90 |
91 |
92 | glClearColor(1.0, 1.0, 1.0, 1.0);
93 | }
94 |
95 |
96 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
97 |
98 | glClear(GL_COLOR_BUFFER_BIT);
99 |
100 | [self.effect prepareToDraw];
101 |
102 | glDrawArrays(GL_TRIANGLES, 0, 6);
103 | }
104 |
105 | - (void)didReceiveMemoryWarning {
106 | [super didReceiveMemoryWarning];
107 | // Dispose of any resources that can be recreated.
108 | }
109 |
110 |
111 | @end
112 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // learnOpenGLESGLkit02纹理
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理Tests/learnOpenGLESGLkit02__Tests.m:
--------------------------------------------------------------------------------
1 | //
2 | // learnOpenGLESGLkit02__Tests.m
3 | // learnOpenGLESGLkit02纹理Tests
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface learnOpenGLESGLkit02__Tests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation learnOpenGLESGLkit02__Tests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 | // Put setup code here. This method is called before the invocation of each test method in the class.
20 | }
21 |
22 | - (void)tearDown {
23 | // Put teardown code here. This method is called after the invocation of each test method in the class.
24 | [super tearDown];
25 | }
26 |
27 | - (void)testExample {
28 | // This is an example of a functional test case.
29 | // Use XCTAssert and related functions to verify your tests produce the correct results.
30 | }
31 |
32 | - (void)testPerformanceExample {
33 | // This is an example of a performance test case.
34 | [self measureBlock:^{
35 | // Put the code you want to measure the time of here.
36 | }];
37 | }
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/learnOpenGLESGLkit02纹理/learnOpenGLESGLkit02纹理UITests/learnOpenGLESGLkit02__UITests.m:
--------------------------------------------------------------------------------
1 | //
2 | // learnOpenGLESGLkit02__UITests.m
3 | // learnOpenGLESGLkit02纹理UITests
4 | //
5 | // Created by 刘晓亮 on 2017/9/30.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface learnOpenGLESGLkit02__UITests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation learnOpenGLESGLkit02__UITests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 |
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 |
22 | // In UI tests it is usually best to stop immediately when a failure occurs.
23 | self.continueAfterFailure = NO;
24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
25 | [[[XCUIApplication alloc] init] launch];
26 |
27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
28 | }
29 |
30 | - (void)tearDown {
31 | // Put teardown code here. This method is called after the invocation of each test method in the class.
32 | [super tearDown];
33 | }
34 |
35 | - (void)testExample {
36 | // Use recording to get started writing UI tests.
37 | // Use XCTAssert and related functions to verify your tests produce the correct results.
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // openGLESGLKit06
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // openGLESGLKit06
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 |
12 |
13 | @interface AppDelegate ()
14 |
15 | @end
16 |
17 | @implementation AppDelegate
18 |
19 |
20 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
21 | // Override point for customization after application launch.
22 | return YES;
23 | }
24 |
25 |
26 | - (void)applicationWillResignActive:(UIApplication *)application {
27 | // 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.
28 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
29 | }
30 |
31 |
32 | - (void)applicationDidEnterBackground:(UIApplication *)application {
33 | // 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.
34 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
35 | }
36 |
37 |
38 | - (void)applicationWillEnterForeground:(UIApplication *)application {
39 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
40 | }
41 |
42 |
43 | - (void)applicationDidBecomeActive:(UIApplication *)application {
44 | // 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.
45 | }
46 |
47 |
48 | - (void)applicationWillTerminate:(UIApplication *)application {
49 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
50 | }
51 |
52 |
53 | @end
54 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | }
43 | ],
44 | "info" : {
45 | "version" : 1,
46 | "author" : "xcode"
47 | }
48 | }
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/Base.lproj/LaunchScreen.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 |
28 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/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 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleDisplayName
8 | 天空盒子
9 | CFBundleExecutable
10 | $(EXECUTABLE_NAME)
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleVersion
22 | 1
23 | LSRequiresIPhoneOS
24 |
25 | UILaunchStoryboardName
26 | LaunchScreen
27 | UIMainStoryboardFile
28 | Main
29 | UIRequiredDeviceCapabilities
30 |
31 | armv7
32 |
33 | UISupportedInterfaceOrientations
34 |
35 | UIInterfaceOrientationPortrait
36 | UIInterfaceOrientationLandscapeLeft
37 | UIInterfaceOrientationLandscapeRight
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // openGLESGLKit06
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import
11 |
12 | @interface ViewController : GLKViewController
13 |
14 |
15 | @end
16 |
17 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // openGLESGLKit06
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 |
10 | #import "ViewController.h"
11 | #import
12 | #import
13 | @interface ViewController ()
14 |
15 | @property(nonatomic, strong)EAGLContext * context;
16 | @property(nonatomic, strong)GLKSkyboxEffect * skyboxEffect;
17 | @property(nonatomic,assign)GLint degreeX;
18 | @property(nonatomic,assign)GLint degreeY;
19 |
20 | @end
21 |
22 | @implementation ViewController{
23 |
24 | GLKMatrix4 _modelMatrix;
25 | }
26 |
27 | - (void)viewDidLoad {
28 | [super viewDidLoad];
29 |
30 |
31 | GLKView * glView = (GLKView *)self.view;
32 | EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
33 |
34 | if (!context) {
35 | NSLog(@"创建context失败");
36 | }
37 | if (![EAGLContext setCurrentContext:context]) {
38 | NSLog(@"设置当前context失败");
39 | }
40 |
41 | glView.context = context;
42 | glView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
43 | glView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
44 |
45 |
46 | self.skyboxEffect = [[GLKSkyboxEffect alloc] init];
47 |
48 | NSString * path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
49 | NSError * error;
50 | GLKTextureInfo * textureInfo = [GLKTextureLoader cubeMapWithContentsOfFile:path options:nil error:&error];
51 |
52 | self.skyboxEffect.textureCubeMap.name = textureInfo.name;
53 | self.skyboxEffect.xSize = 0.3;
54 | self.skyboxEffect.ySize = 0.3;
55 | self.skyboxEffect.zSize = 0.3;
56 |
57 |
58 | glClearColor(1.0, 0.0, 0.0, 1.0);
59 |
60 | GLfloat aspect = self.view.frame.size.width / self.view.frame.size.height;
61 |
62 | self.skyboxEffect.transform.projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), aspect, 0.1f, 400.0f);
63 | }
64 |
65 | -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
66 |
67 | UITouch * touch = touches.anyObject;
68 | CGPoint currentPoint = [touch locationInView:self.view];
69 | CGPoint previousPoint = [touch previousLocationInView:self.view];
70 |
71 | self.degreeX += previousPoint.x - currentPoint.x;
72 | self.degreeY += previousPoint.y - currentPoint.y;
73 | }
74 |
75 |
76 | - (void)update{
77 |
78 | _modelMatrix = GLKMatrix4RotateX(GLKMatrix4Identity, GLKMathDegreesToRadians(self.degreeY % 360));
79 |
80 | _modelMatrix = GLKMatrix4RotateY(_modelMatrix, GLKMathDegreesToRadians(self.degreeX % 360));
81 |
82 | self.skyboxEffect.transform.modelviewMatrix = _modelMatrix;
83 | }
84 |
85 |
86 |
87 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
88 |
89 | glClear(GL_COLOR_BUFFER_BIT);
90 | [self.skyboxEffect prepareToDraw];
91 | [self.skyboxEffect draw];
92 |
93 | }
94 |
95 |
96 | - (void)didReceiveMemoryWarning {
97 | [super didReceiveMemoryWarning];
98 | // Dispose of any resources that can be recreated.
99 | }
100 |
101 | @end
102 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1677/learnOpenGL-ES/8fa863b0cf6d453781abc57b28b4cbb28ea5aa2e/openGLESGLKit06天空盒子/openGLESGLKit06/image.png
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // openGLESGLKit06
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06Tests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06Tests/openGLESGLKit06Tests.m:
--------------------------------------------------------------------------------
1 | //
2 | // openGLESGLKit06Tests.m
3 | // openGLESGLKit06Tests
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface openGLESGLKit06Tests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation openGLESGLKit06Tests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 | // Put setup code here. This method is called before the invocation of each test method in the class.
20 | }
21 |
22 | - (void)tearDown {
23 | // Put teardown code here. This method is called after the invocation of each test method in the class.
24 | [super tearDown];
25 | }
26 |
27 | - (void)testExample {
28 | // This is an example of a functional test case.
29 | // Use XCTAssert and related functions to verify your tests produce the correct results.
30 | }
31 |
32 | - (void)testPerformanceExample {
33 | // This is an example of a performance test case.
34 | [self measureBlock:^{
35 | // Put the code you want to measure the time of here.
36 | }];
37 | }
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06UITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/openGLESGLKit06天空盒子/openGLESGLKit06UITests/openGLESGLKit06UITests.m:
--------------------------------------------------------------------------------
1 | //
2 | // openGLESGLKit06UITests.m
3 | // openGLESGLKit06UITests
4 | //
5 | // Created by 刘晓亮 on 2017/8/23.
6 | // Copyright © 2017年 刘晓亮. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface openGLESGLKit06UITests : XCTestCase
12 |
13 | @end
14 |
15 | @implementation openGLESGLKit06UITests
16 |
17 | - (void)setUp {
18 | [super setUp];
19 |
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 |
22 | // In UI tests it is usually best to stop immediately when a failure occurs.
23 | self.continueAfterFailure = NO;
24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
25 | [[[XCUIApplication alloc] init] launch];
26 |
27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
28 | }
29 |
30 | - (void)tearDown {
31 | // Put teardown code here. This method is called after the invocation of each test method in the class.
32 | [super tearDown];
33 | }
34 |
35 | - (void)testExample {
36 | // Use recording to get started writing UI tests.
37 | // Use XCTAssert and related functions to verify your tests produce the correct results.
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------