├── 01-environment
├── 01-environment.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 01-environment
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _1_environment.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 02-MoveDemo
├── 02-MoveDemo.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 02-MoveDemo
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _2_MoveDemo.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 03-Primitives
├── 03-Primitives.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 03-Primitives
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _3_Primitives.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 04-GeoTest
├── 04-GeoTest.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 04-GeoTest
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _4_GeoTest.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 05-Scissor
├── 05-Scissor.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 05-Scissor
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _5_Scissor.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 06-Blending
├── 06-Blending.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 06-Blending
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _6_Blending.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 07-Smoother
├── 07-Smoother.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 07-Smoother
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _7_Smoother.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 08-MoveByMatrix
├── 08-MoveByMatrix.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 08-MoveByMatrix
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _8_MoveByMatrix.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 09-Objects
├── 09-Objects.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 09-Objects
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _9_Objects.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 10-Orthographic
├── 10-Orthographic.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 10-Orthographic
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _0_Orthographic.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 11-Perspective
├── 11-Perspective.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 11-Perspective
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _1_Perspective.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 12-ModelViewProjection
├── 12-ModelViewProjection.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 12-ModelViewProjection
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _2_ModelViewProjection.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 13-SphereWorld-Lv1
├── 13-SphereWorld-Lv1.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 13-SphereWorld-Lv1
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _3_SphereWorld_Lv1.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 14-SphereWorld-Lv2
├── 14-SphereWorld-Lv2.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 14-SphereWorld-Lv2
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _4_SphereWorld_Lv2.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 15-SphereWorld-Lv3
├── 15-SphereWorld-Lv3.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 15-SphereWorld-Lv3
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _5_SphereWorld_Lv3.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 16-SphereWorld-Lv4
├── 16-SphereWorld-Lv4.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 16-SphereWorld-Lv4
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _6_SphereWorld_Lv4.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 17-Pyramid
├── 17-Pyramid.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 17-Pyramid
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _7_Pyramid.entitlements
│ ├── main.cpp
│ └── stone.tga
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 18-Tunnel
├── 18-Tunnel.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 18-Tunnel
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _8_Tunnel.entitlements
│ ├── brick.tga
│ ├── ceiling.tga
│ ├── floor.tga
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 19-Anisotropic
├── 19-Anisotropic.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 19-Anisotropic
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── _9_Anisotropic.entitlements
│ ├── brick.tga
│ ├── ceiling.tga
│ ├── floor.tga
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
├── 20-SphereWorld-Lv5
├── 20-SphereWorld-Lv5.xcodeproj
│ ├── project.pbxproj
│ ├── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ └── luheliu.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── luheliu.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 20-SphereWorld-Lv5
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── Info.plist
│ ├── Marble.tga
│ ├── Marslike.tga
│ ├── MoonLike.tga
│ ├── _0_SphereWorld_Lv5.entitlements
│ └── main.cpp
├── libGLEW.2.1.0.dylib
└── libgltools.dylib
└── README.md
/01-environment/01-environment.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/01-environment/01-environment.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/01-environment/01-environment.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/01-environment/01-environment.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 01-environment.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/01-environment/01-environment/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/01-environment/01-environment/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/01-environment/01-environment/_1_environment.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/01-environment/01-environment/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 01-environment
4 | //
5 | // Created by luhe liu on 2018/3/24.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | GLBatch triangleBatch;
15 | GLShaderManager shaderManager;
16 |
17 | //窗口大小改变时接受新的宽度和高度
18 | void ChangeSize(int width, int height) {
19 | // 设置视口,(x, y, width, height),其中 (x, y) 代表窗口中视口的左下角坐标
20 | glViewport(0, 0, width, height);
21 | }
22 |
23 | //为程序作一次性的设置
24 | void SetupRC() {
25 | //设置背影颜色
26 | glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
27 | //初始化着色管理器
28 | shaderManager.InitializeStockShaders();
29 | //构建三角形顶点数组,vVert包含3个顶点的(x, y, z)笛卡尔坐标
30 | GLfloat vVerts[] = {
31 | -0.5f, 0.0f, 0.0f,
32 | 0.5f, 0.0f, 0.0f,
33 | 0.0f, 0.5f, 0.0f,
34 | };
35 | //开始构建批次,GL_TRIANGLES 表示三角形,后面参数是顶点数
36 | triangleBatch.Begin(GL_TRIANGLES, 3);
37 | /*
38 | * 批次增加存储属性
39 | * CopyVertexData3f(XYZ顶点数据)
40 | * CopyNormalDataf(表面法线)
41 | * CopyColorData4f(RGBA颜色)
42 | * CopyTexCoordData2f(纹理坐标)
43 | */
44 | triangleBatch.CopyVertexData3f(vVerts);
45 | //结束批次属性设置,表明完成数据复制操作,不能再添加新属性
46 | triangleBatch.End();
47 | }
48 |
49 | //开始渲染
50 | void RenderScene(void) {
51 | //清除一个或一组特定的缓冲区
52 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
53 | //设置一组浮点数来表示红色
54 | GLfloat vRed[] = {1.0f, 0.0f, 0.0f, 1.0f};
55 | //传递到存储着色器,即GLT_SHADER_IDENTITY着色器,这个着色器只是使用指定颜色以默认笛卡尔坐标第在屏幕上渲染几何图形
56 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
57 | //根据批次里的顶点数据利用当前设置的着色器进行画图
58 | triangleBatch.Draw();
59 | /*
60 | * 当 glutInitDisplayMode 有传入双缓冲模式时,
61 | * 将在后台缓冲区进行渲染,然后在结束时交换到前台
62 | * 为了防止观察者看到可能随着动画帧和动画帧之间闪烁的渲染过程
63 | */
64 | glutSwapBuffers();
65 | }
66 |
67 | //程序入口
68 | int main(int argc, char* argv[]) {
69 | //设置当前工作目录,针对MAC OS X
70 | gltSetWorkingDirectory(argv[0]);
71 | //初始化GLUT库
72 | glutInit(&argc, argv);
73 | /*初始化渲染模式,其中标志GLUT_DOUBLE、GLUT_RGBA、GLUT_DEPTH、GLUT_STENCIL分别指
74 | 双缓冲窗口、RGBA颜色模式、深度测试、模板缓冲区*/
75 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
76 | //初始化窗口大小
77 | glutInitWindowSize(800, 600);
78 | //创建窗口
79 | glutCreateWindow("Triangle");
80 | //注册回调函数,拦截窗口大小变化消息,ChangeSize 是自定义方法名
81 | glutReshapeFunc(ChangeSize);
82 | //注册回调函数,拦截窗口渲染消息,RenderScene 是自定义方法名
83 | glutDisplayFunc(RenderScene);
84 | //确保驱动程序的初始化中没有出现任何问题。
85 | GLenum err = glewInit();
86 | if(GLEW_OK != err) {
87 | fprintf(stderr, "glew error:%s\n", glewGetErrorString(err));
88 | return 1;
89 | }
90 | //初始化设置
91 | SetupRC();
92 | //进入调用循环
93 | glutMainLoop();
94 | return 0;
95 | }
96 |
--------------------------------------------------------------------------------
/01-environment/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/01-environment/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/01-environment/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/01-environment/libgltools.dylib
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/02-MoveDemo/02-MoveDemo.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 02-MoveDemo.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo/_2_MoveDemo.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/02-MoveDemo/02-MoveDemo/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 02-MoveDemo
4 | //
5 | // Created by luhe liu on 2018/4/9.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | GLBatch squareBatch;
15 | GLShaderManager shaderManager;
16 |
17 | GLfloat blockSize = 0.2f;
18 | GLfloat vVerts[] = {
19 | -blockSize, -blockSize, 0.0f, //左下角
20 | blockSize, -blockSize, 0.0f, //右下角
21 | blockSize, blockSize, 0.0f, //右上角
22 | -blockSize, blockSize, 0.0f //左上角
23 | };
24 |
25 | int specialKey = GLUT_KEY_RIGHT;
26 |
27 | void BounceFunction(int key) {
28 | //每次按键的矩形移动距离
29 | GLfloat stepSize = 0.025f;
30 | //左下角坐标的 (x, y)
31 | GLfloat blockX = vVerts[0];
32 | GLfloat blockY = vVerts[1];
33 |
34 | //判断键盘点击的是哪个按钮,计算下一个点的位置
35 | if(key == GLUT_KEY_UP) blockY += stepSize;
36 | if(key == GLUT_KEY_DOWN) blockY -= stepSize;
37 | if(key == GLUT_KEY_LEFT) blockX -= stepSize;
38 | if(key == GLUT_KEY_RIGHT) blockX += stepSize;
39 |
40 | //边界检测,让矩形没法超出边界
41 | if(blockX < -1.0f) blockX = -1.0f;
42 | if(blockX > (1.0f - blockSize * 2)) blockX = 1.0f - blockSize * 2;
43 | if(blockY < -1.0f) blockY = -1.0f;
44 | if(blockY > (1.0f - blockSize * 2)) blockY = 1.0f - blockSize * 2;
45 |
46 | //设置下个位置的 4 个顶点坐标
47 | //左下角
48 | vVerts[0] = blockX;
49 | vVerts[1] = blockY;
50 | //右下角
51 | vVerts[3] = blockX + blockSize * 2;
52 | vVerts[4] = blockY;
53 | //右上角
54 | vVerts[6] = blockX + blockSize * 2;
55 | vVerts[7] = blockY + blockSize * 2;
56 | //左上角
57 | vVerts[9] = blockX;
58 | vVerts[10] = blockY + blockSize * 2;
59 |
60 | //批次里面的数据更新
61 | squareBatch.CopyVertexData3f(vVerts);
62 | }
63 |
64 | // 监控特殊按键(功能键和方向键)点击
65 | void SpecialKeys(int key, int x, int y) {
66 |
67 | specialKey = key;
68 |
69 | // BounceFunction(key);
70 |
71 | //触发重画事件 -> RenderScene
72 | // glutPostRedisplay();
73 | }
74 |
75 | //窗口渲染
76 | void RenderScene(void) {
77 | //清除一个或一组特定的缓冲区
78 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
79 |
80 | //利用单位着色器设置矩形颜色为红色
81 | GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
82 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
83 | //画图
84 | squareBatch.Draw();
85 |
86 | //将在后台缓冲区进行渲染,然后在结束时交换到前台
87 | glutSwapBuffers();
88 |
89 | //计算下一个矩形位置
90 | BounceFunction(specialKey);
91 |
92 | //触发重画事件 -> RenderScene
93 | glutPostRedisplay();
94 | }
95 |
96 | //为程序作一次性的设置
97 | void SetupRC() {
98 | //设置背景颜色为蓝色
99 | glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
100 |
101 | //着色器初始化
102 | shaderManager.InitializeStockShaders();
103 |
104 | //批次初始化
105 | squareBatch.Begin(GL_TRIANGLE_FAN, 4);
106 | squareBatch.CopyVertexData3f(vVerts);
107 | squareBatch.End();
108 | }
109 |
110 | //窗口大小改变时接受新的宽度和高度,其中0,0代表窗口中视口的左下角坐标,w,h代表像素
111 | void ChangeSize(int width, int height) {
112 | glViewport(0, 0, width, height);
113 | }
114 |
115 | //程序入口
116 | int main(int argc, char* argv[]) {
117 | //设置当前工作目录,针对MAC OS X
118 | gltSetWorkingDirectory(argv[0]);
119 |
120 | //初始化GLUT库
121 | glutInit(&argc, argv);
122 |
123 | /*初始化渲染模式,其中标志GLUT_DOUBLE、GLUT_RGBA、GLUT_DEPTH、GLUT_STENCIL分别指
124 | 双缓冲窗口、RGBA颜色模式、深度测试、模板缓冲区*/
125 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
126 |
127 | //初始化窗口大小
128 | glutInitWindowSize(800, 600);
129 | //创建窗口
130 | glutCreateWindow("MoveDemo");
131 |
132 | //注册窗口大小变化回调函数
133 | glutReshapeFunc(ChangeSize);
134 | //注册窗口渲染回调函数
135 | glutDisplayFunc(RenderScene);
136 | //注册键盘特殊按键点击回调函数
137 | glutSpecialFunc(SpecialKeys);
138 |
139 | //确保驱动程序的初始化中没有出现任何问题。
140 | GLenum err = glewInit();
141 | if(GLEW_OK != err) {
142 | fprintf(stderr, "glew error:%s\n", glewGetErrorString(err));
143 | return 1;
144 | }
145 |
146 | //初始化设置
147 | SetupRC();
148 |
149 | //进入调用循环
150 | glutMainLoop();
151 | return 0;
152 | }
153 |
--------------------------------------------------------------------------------
/02-MoveDemo/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/02-MoveDemo/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/02-MoveDemo/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/02-MoveDemo/libgltools.dylib
--------------------------------------------------------------------------------
/03-Primitives/03-Primitives.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/03-Primitives/03-Primitives.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/03-Primitives/03-Primitives.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/03-Primitives/03-Primitives.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 03-Primitives.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/03-Primitives/03-Primitives/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/03-Primitives/03-Primitives/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/03-Primitives/03-Primitives/_3_Primitives.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/03-Primitives/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/03-Primitives/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/03-Primitives/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/03-Primitives/libgltools.dylib
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/04-GeoTest/04-GeoTest.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 04-GeoTest.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest/_4_GeoTest.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/04-GeoTest/04-GeoTest/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 04-GeoTest
4 | //
5 | // Created by luhe liu on 2018/4/12.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 |
17 | GLFrame viewFrame;
18 | GLFrustum viewFrustum;
19 | GLTriangleBatch torusBatch;
20 | GLMatrixStack modelViewMatrix;
21 | GLMatrixStack projectionMatrix;
22 | GLGeometryTransform transformPipeline;
23 | GLShaderManager shaderManager;
24 |
25 | //是否开启正反面剔除
26 | int iCull = 0;
27 | //是否开始深度测试
28 | int iDepth = 0;
29 |
30 | //点击菜单选项
31 | void ProcessMenu(int value) {
32 | switch(value) {
33 | case 1:
34 | //开关深度测试
35 | iDepth = !iDepth;
36 | break;
37 | case 2:
38 | //开关正反面剔除
39 | iCull = !iCull;
40 | break;
41 | case 3:
42 | //开关多边形面模式
43 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
44 | break;
45 | case 4:
46 | //开关多边形线模式
47 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
48 | break;
49 | case 5:
50 | //开关多边形点模式
51 | glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
52 | break;
53 | }
54 |
55 | //触发渲染
56 | glutPostRedisplay();
57 | }
58 |
59 | //渲染画面
60 | void RenderScene(void) {
61 | //清除一个或一组特定的缓冲区
62 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63 |
64 | //判断是否开启正反面剔除
65 | if(iCull)
66 | glEnable(GL_CULL_FACE);
67 | else
68 | glDisable(GL_CULL_FACE);
69 |
70 | //判断是否开启深度测试
71 | if(iDepth)
72 | glEnable(GL_DEPTH_TEST);
73 | else
74 | glDisable(GL_DEPTH_TEST);
75 |
76 | //保存当前的模型视图矩阵
77 | modelViewMatrix.PushMatrix(viewFrame);
78 |
79 | //这里使用默认光源进行着色,可以看到阴影
80 | GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
81 | shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vRed);
82 |
83 | //画花托
84 | torusBatch.Draw();
85 |
86 | // 还原以前的模型视图矩阵
87 | modelViewMatrix.PopMatrix();
88 |
89 | //将在后台缓冲区进行渲染,然后在结束时交换到前台
90 | glutSwapBuffers();
91 | }
92 |
93 | //为程序作一次性的设置
94 | void SetupRC() {
95 | //设置窗口背景颜色
96 | glClearColor(0.3f, 0.3f, 0.3f, 1.0f );
97 |
98 | //初始化着色器管理器
99 | shaderManager.InitializeStockShaders();
100 |
101 | //设置变换管线以使用两个矩阵堆栈
102 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
103 |
104 | //移动物体的位置,值越大物体越远,值越小物体越近
105 | viewFrame.MoveForward(6.0f);
106 |
107 | //创建一个花托批次,参数依次是:批次,外半径,内半径,片段数,堆叠数(在计算机中圆是正多边形,顶点数越多越像真正的圆)
108 | gltMakeTorus(torusBatch, 1.0f, 0.3f, 52, 26);
109 |
110 | //设置默认点大小
111 | glPointSize(4.0f);
112 | }
113 |
114 | //特殊按键(功能键或者方向键)监听
115 | void SpecialKeys(int key, int x, int y) {
116 | //上、下、左、右按键,3D 旋转
117 | /*
118 | * RotateWorld(float fAngle, float x, float y, float z)
119 | * fAngle: 旋转弧度, x/y/z:以哪个坐标轴旋转
120 | * m3dDegToRad:角度 -> 弧度
121 | */
122 | switch (key) {
123 | case GLUT_KEY_UP:
124 | viewFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
125 | break;
126 | case GLUT_KEY_DOWN:
127 | viewFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);
128 | break;
129 | case GLUT_KEY_LEFT:
130 | viewFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
131 | break;
132 | case GLUT_KEY_RIGHT:
133 | viewFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
134 | break;
135 | default:
136 | break;
137 | }
138 |
139 | //触发渲染
140 | glutPostRedisplay();
141 | }
142 |
143 | //窗口大小改变时接受新的宽度和高度,其中0,0代表窗口中视口的左下角坐标,w,h代表像素
144 | void ChangeSize(int width, int height) {
145 | // 防止下面除法的除数为0导致的闪退
146 | if(height == 0) height = 1;
147 |
148 | //设置视图窗口位置
149 | glViewport(0, 0, width, height);
150 |
151 | // 创建投影矩阵,并将它载入到投影矩阵堆栈中
152 | viewFrustum.SetPerspective(35.0f, float(width) / float(height), 1.0f, 500.0f);
153 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
154 | }
155 |
156 | //程序入口
157 | int main(int argc, char* argv[]) {
158 | //设置当前工作目录,针对MAC OS X
159 | gltSetWorkingDirectory(argv[0]);
160 |
161 | //初始化GLUT库
162 | glutInit(&argc, argv);
163 |
164 | /*初始化渲染模式,其中标志GLUT_DOUBLE、GLUT_RGBA、GLUT_DEPTH、GLUT_STENCIL分别指
165 | 双缓冲窗口、RGBA颜色模式、深度测试、模板缓冲区*/
166 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
167 |
168 | //初始化窗口大小
169 | glutInitWindowSize(800, 600);
170 | //创建窗口
171 | glutCreateWindow("Geometry Test Program");
172 |
173 | //注册回调函数
174 | glutReshapeFunc(ChangeSize);
175 | glutDisplayFunc(RenderScene);
176 | glutSpecialFunc(SpecialKeys);
177 |
178 | //创建菜单,绑定一个响应方法
179 | glutCreateMenu(ProcessMenu);
180 |
181 | //为菜单添加选项
182 | glutAddMenuEntry("Toggle depth test",1);
183 | glutAddMenuEntry("Toggle cull backface",2);
184 | glutAddMenuEntry("Set Fill Mode", 3);
185 | glutAddMenuEntry("Set Line Mode", 4);
186 | glutAddMenuEntry("Set Point Mode", 5);
187 |
188 | //右键弹出菜单
189 | glutAttachMenu(GLUT_RIGHT_BUTTON);
190 |
191 | //确保驱动程序的初始化中没有出现任何问题。
192 | GLenum err = glewInit();
193 | if(GLEW_OK != err) {
194 | fprintf(stderr, "glew error:%s\n", glewGetErrorString(err));
195 | return 1;
196 | }
197 |
198 | //初始化设置
199 | SetupRC();
200 |
201 | //进入调用循环
202 | glutMainLoop();
203 | return 0;
204 | }
205 |
--------------------------------------------------------------------------------
/04-GeoTest/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/04-GeoTest/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/04-GeoTest/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/04-GeoTest/libgltools.dylib
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/05-Scissor/05-Scissor.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 05-Scissor.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor/_5_Scissor.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/05-Scissor/05-Scissor/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 05-Scissor
4 | //
5 | // Created by luhe liu on 2018/4/12.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 |
12 | //渲染画面
13 | void RenderScene(void) {
14 |
15 | //设置颜色缓冲区背景色为蓝色
16 | glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
17 | //清空颜色缓存区,设置到后台缓存区,执行完这步才绘制了背景颜色
18 | glClear(GL_COLOR_BUFFER_BIT);
19 | //开启裁剪
20 | glEnable(GL_SCISSOR_TEST);
21 |
22 | //设置颜色缓冲区背景色为红色
23 | glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
24 | //裁剪出(x: 100, y: 100, w: 600, h: 400)区域
25 | glScissor(100, 100, 600, 400);
26 | //清空颜色缓存区,设置到后台缓冲区,注意这里会设置的是裁剪区域
27 | glClear(GL_COLOR_BUFFER_BIT);
28 |
29 | //设置颜色缓冲区背景色为绿色
30 | glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
31 | //裁剪出(x: 200, y: 200, w: 400, h: 200)区域
32 | glScissor(200, 200, 400, 200);
33 | //清空颜色缓存区,设置到后台缓冲区,注意这里会设置的是裁剪区域
34 | glClear(GL_COLOR_BUFFER_BIT);
35 |
36 | //关闭裁剪
37 | glDisable(GL_SCISSOR_TEST);
38 | //将在后台缓冲区进行渲染,然后在结束时交换到前台
39 | glutSwapBuffers();
40 | }
41 |
42 | //窗口大小改变时接受新的宽度和高度
43 | void ChangeSize(int w, int h) {
44 | //设置视图窗口位置
45 | glViewport(0, 0, w, h);
46 | }
47 |
48 | //程序入口
49 | int main(int argc, char* argv[]) {
50 | //设置当前工作目录,针对MAC OS X
51 | gltSetWorkingDirectory(argv[0]);
52 |
53 | //初始化GLUT库
54 | glutInit(&argc, argv);
55 |
56 | /*初始化渲染模式,其中标志GLUT_DOUBLE、GLUT_RGBA、GLUT_DEPTH、GLUT_STENCIL分别指
57 | 双缓冲窗口、RGBA颜色模式、深度测试、模板缓冲区*/
58 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
59 |
60 | //初始化窗口大小
61 | glutInitWindowSize(800, 600);
62 | //创建窗口
63 | glutCreateWindow("OpenGL Scissor");
64 |
65 | //注册回调函数
66 | glutReshapeFunc(ChangeSize);
67 | glutDisplayFunc(RenderScene);
68 |
69 | //确保驱动程序的初始化中没有出现任何问题。
70 | GLenum err = glewInit();
71 | if(GLEW_OK != err) {
72 | fprintf(stderr, "glew error:%s\n", glewGetErrorString(err));
73 | return 1;
74 | }
75 |
76 | //进入调用循环
77 | glutMainLoop();
78 | return 0;
79 | }
80 |
--------------------------------------------------------------------------------
/05-Scissor/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/05-Scissor/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/05-Scissor/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/05-Scissor/libgltools.dylib
--------------------------------------------------------------------------------
/06-Blending/06-Blending.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/06-Blending/06-Blending.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/06-Blending/06-Blending.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/06-Blending/06-Blending.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 06-Blending.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/06-Blending/06-Blending/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/06-Blending/06-Blending/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/06-Blending/06-Blending/_6_Blending.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/06-Blending/06-Blending/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 06-Blending
4 | //
5 | // Created by luhe liu on 2018/4/12.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 |
13 | GLBatch squareBatch;
14 | GLBatch greenBatch;
15 | GLBatch redBatch;
16 | GLBatch blueBatch;
17 | GLBatch blackBatch;
18 |
19 | GLShaderManager shaderManager;
20 |
21 | GLfloat blockSize = 0.2f;
22 | GLfloat vVerts[] = {
23 | -blockSize, -blockSize, 0.0f,
24 | blockSize, -blockSize, 0.0f,
25 | blockSize, blockSize, 0.0f,
26 | -blockSize, blockSize, 0.0f
27 | };
28 |
29 | //程序一次性初始化
30 | void SetupRC() {
31 | //设置窗口背景为黑色
32 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
33 |
34 | //初始化着色器管理者
35 | shaderManager.InitializeStockShaders();
36 |
37 | //创建移动矩形批次
38 | squareBatch.Begin(GL_TRIANGLE_FAN, 4);
39 | squareBatch.CopyVertexData3f(vVerts);
40 | squareBatch.End();
41 |
42 | //绿色矩形批次
43 | GLfloat vBlock[] = {
44 | 0.25f, 0.25f, 0.0f,
45 | 0.75f, 0.25f, 0.0f,
46 | 0.75f, 0.75f, 0.0f,
47 | 0.25f, 0.75f, 0.0f
48 | };
49 | greenBatch.Begin(GL_TRIANGLE_FAN, 4);
50 | greenBatch.CopyVertexData3f(vBlock);
51 | greenBatch.End();
52 |
53 | //红色矩形批次
54 | GLfloat vBlock2[] = {
55 | -0.75f, 0.25f, 0.0f,
56 | -0.25f, 0.25f, 0.0f,
57 | -0.25f, 0.75f, 0.0f,
58 | -0.75f, 0.75f, 0.0f
59 | };
60 | redBatch.Begin(GL_TRIANGLE_FAN, 4);
61 | redBatch.CopyVertexData3f(vBlock2);
62 | redBatch.End();
63 |
64 | //蓝色矩形批次
65 | GLfloat vBlock3[] = {
66 | -0.75f, -0.75f, 0.0f,
67 | -0.25f, -0.75f, 0.0f,
68 | -0.25f, -0.25f, 0.0f,
69 | -0.75f, -0.25f, 0.0f
70 | };
71 | blueBatch.Begin(GL_TRIANGLE_FAN, 4);
72 | blueBatch.CopyVertexData3f(vBlock3);
73 | blueBatch.End();
74 |
75 | //黑色矩形批次
76 | GLfloat vBlock4[] = {
77 | 0.25f, -0.75f, 0.0f,
78 | 0.75f, -0.75f, 0.0f,
79 | 0.75f, -0.25f, 0.0f,
80 | 0.25f, -0.25f, 0.0f
81 | };
82 | blackBatch.Begin(GL_TRIANGLE_FAN, 4);
83 | blackBatch.CopyVertexData3f(vBlock4);
84 | blackBatch.End();
85 | }
86 |
87 | //特殊按钮监听
88 | void SpecialKeys(int key, int x, int y) {
89 | GLfloat stepSize = 0.025f;
90 |
91 | //左上角的 X 坐标和右下角的 Y 坐标
92 | GLfloat blockX = vVerts[0];
93 | GLfloat blockY = vVerts[7];
94 |
95 | //根据移动方向移动位置
96 | switch (key) {
97 | case GLUT_KEY_UP: blockY += stepSize; break;
98 | case GLUT_KEY_DOWN: blockY -= stepSize; break;
99 | case GLUT_KEY_LEFT: blockX -= stepSize; break;
100 | case GLUT_KEY_RIGHT: blockX += stepSize; break;
101 | default: break;
102 | }
103 |
104 | //移动边界处理
105 | if(blockX < -1.0f) blockX = -1.0f;
106 | if(blockX > (1.0f - blockSize * 2)) blockX = 1.0f - blockSize * 2;;
107 | if(blockY < -1.0f + blockSize * 2) blockY = -1.0f + blockSize * 2;
108 | if(blockY > 1.0f) blockY = 1.0f;
109 |
110 | //矩形四个顶点位置
111 | vVerts[0] = blockX;
112 | vVerts[1] = blockY - blockSize*2;
113 | vVerts[3] = blockX + blockSize*2;
114 | vVerts[4] = blockY - blockSize*2;
115 | vVerts[6] = blockX + blockSize*2;
116 | vVerts[7] = blockY;
117 | vVerts[9] = blockX;
118 | vVerts[10] = blockY;
119 |
120 | //批次顶点数据编号
121 | squareBatch.CopyVertexData3f(vVerts);
122 |
123 | //触发渲染
124 | glutPostRedisplay();
125 | }
126 |
127 | //渲染画面
128 | void RenderScene(void) {
129 | //清理各个缓存区
130 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
131 |
132 | //定义4种颜色
133 | GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
134 | GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f };
135 | GLfloat vBlue[] = { 0.0f, 0.0f, 1.0f, 0.6f };
136 | GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
137 |
138 | //画绿色矩形
139 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vGreen);
140 | greenBatch.Draw();
141 |
142 | //画红色矩形
143 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
144 | redBatch.Draw();
145 |
146 | //画蓝色矩形
147 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vBlue);
148 | blueBatch.Draw();
149 |
150 | //画黑色矩形
151 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vBlack);
152 | blackBatch.Draw();
153 |
154 | //开启颜色混合
155 | glEnable(GL_BLEND);
156 | //配置混合方程式
157 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158 | //画移动矩形,本身半透明蓝色
159 | shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vBlue);
160 | squareBatch.Draw();
161 | //关闭颜色混合
162 | glDisable(GL_BLEND);
163 |
164 | //将在后台缓冲区进行渲染,然后在结束时交换到前台
165 | glutSwapBuffers();
166 | }
167 |
168 | //窗口大小改变时接受新的宽度和高度
169 | void ChangeSize(int w, int h) {
170 |
171 | //设置视图窗口位置
172 | glViewport(0, 0, w, h);
173 | }
174 |
175 | //程序入口
176 | int main(int argc, char* argv[]) {
177 | //设置当前工作目录,针对MAC OS X
178 | gltSetWorkingDirectory(argv[0]);
179 |
180 | //初始化GLUT库
181 | glutInit(&argc, argv);
182 |
183 | /*初始化渲染模式,其中标志GLUT_DOUBLE、GLUT_RGBA、GLUT_DEPTH、GLUT_STENCIL分别指
184 | 双缓冲窗口、RGBA颜色模式、深度测试、模板缓冲区*/
185 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
186 |
187 | //初始化窗口大小
188 | glutInitWindowSize(800, 600);
189 | //创建窗口
190 | glutCreateWindow("Move Block with Arrow Keys to see blending");
191 |
192 | //注册回调函数
193 | glutReshapeFunc(ChangeSize);
194 | glutDisplayFunc(RenderScene);
195 | glutSpecialFunc(SpecialKeys);
196 |
197 | //确保驱动程序的初始化中没有出现任何问题。
198 | GLenum err = glewInit();
199 | if(GLEW_OK != err) {
200 | fprintf(stderr, "glew error:%s\n", glewGetErrorString(err));
201 | return 1;
202 | }
203 |
204 | //初始化设置
205 | SetupRC();
206 |
207 | //进入调用循环
208 | glutMainLoop();
209 | return 0;
210 | }
211 |
--------------------------------------------------------------------------------
/06-Blending/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/06-Blending/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/06-Blending/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/06-Blending/libgltools.dylib
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/07-Smoother/07-Smoother.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 07-Smoother.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother/_7_Smoother.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/07-Smoother/07-Smoother/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 07-Smoother
4 | //
5 | // Created by luhe liu on 2018/4/12.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 |
13 | GLShaderManager shaderManager;
14 | GLFrustum viewFrustum;
15 | GLBatch smallStarBatch;
16 | GLBatch mediumStarBatch;
17 | GLBatch largeStarBatch;
18 | GLBatch mountainRangeBatch;
19 | GLBatch moonBatch;
20 |
21 | //常量宏
22 | #define SMALL_STARS 100
23 | #define MEDIUM_STARS 40
24 | #define LARGE_STARS 15
25 |
26 | #define SCREEN_X 800
27 | #define SCREEN_Y 600
28 |
29 | //点击菜单选项触发的回调方法
30 | void ProcessMenu(int value) {
31 | switch(value) {
32 | case 1:
33 | //开启抗锯齿处理,必须先开启颜色混合模式
34 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
35 | glEnable(GL_BLEND);
36 | //点抗锯齿
37 | glEnable(GL_POINT_SMOOTH);
38 | glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
39 | //线抗锯齿
40 | glEnable(GL_LINE_SMOOTH);
41 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
42 | //多边形抗锯齿
43 | glEnable(GL_POLYGON_SMOOTH);
44 | glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
45 | break;
46 | case 2:
47 | //关闭抗锯齿处理
48 | glDisable(GL_BLEND);
49 | glDisable(GL_LINE_SMOOTH);
50 | glDisable(GL_POINT_SMOOTH);
51 | glDisable(GL_POLYGON_SMOOTH);
52 | break;
53 | default:
54 | break;
55 | }
56 |
57 | //触发渲染
58 | glutPostRedisplay();
59 | }
60 |
61 | //渲染画面
62 | void RenderScene(void) {
63 | //清除缓存区
64 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
65 |
66 | //设置着色器为单位黑色
67 | GLfloat vWhite [] = { 1.0f, 1.0f, 1.0f, 1.0f };
68 | shaderManager.UseStockShader(GLT_SHADER_FLAT, viewFrustum.GetProjectionMatrix(), vWhite);
69 |
70 | //画小点
71 | glPointSize(1.0f);
72 | smallStarBatch.Draw();
73 |
74 | //画大点
75 | glPointSize(4.0f);
76 | mediumStarBatch.Draw();
77 |
78 | //画超大点
79 | glPointSize(8.0f);
80 | largeStarBatch.Draw();
81 |
82 | //画月亮
83 | moonBatch.Draw();
84 |
85 | //画山的轮廓
86 | glLineWidth(3.5);
87 | mountainRangeBatch.Draw();
88 |
89 | //将在后台缓冲区进行渲染,然后在结束时交换到前台
90 | glutSwapBuffers();
91 | }
92 |
93 | //初始化小点批次
94 | void SetupSmallStarBatch() {
95 | M3DVector3f vVerts[SMALL_STARS];
96 | for(int i = 0; i < SMALL_STARS; i++) {
97 | vVerts[i][0] = (GLfloat)(rand() % SCREEN_X);
98 | vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f;
99 | vVerts[i][2] = 0.0f;
100 | }
101 | smallStarBatch.Begin(GL_POINTS, SMALL_STARS);
102 | smallStarBatch.CopyVertexData3f(vVerts);
103 | smallStarBatch.End();
104 | }
105 |
106 | //初始化大点批次
107 | void SetupMeiumStarBatch() {
108 | M3DVector3f vVerts[MEDIUM_STARS];
109 | for(int i = 0; i < MEDIUM_STARS; i++) {
110 | vVerts[i][0] = (GLfloat)(rand() % SCREEN_X);
111 | vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f;
112 | vVerts[i][2] = 0.0f;
113 | }
114 |
115 | mediumStarBatch.Begin(GL_POINTS, MEDIUM_STARS);
116 | mediumStarBatch.CopyVertexData3f(vVerts);
117 | mediumStarBatch.End();
118 | }
119 |
120 | //初始化超大点批次
121 | void SetupLargeStarBatch() {
122 | M3DVector3f vVerts[LARGE_STARS];
123 | for(int i = 0; i < LARGE_STARS; i++) {
124 | vVerts[i][0] = (GLfloat)(rand() % SCREEN_X);
125 | vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f;
126 | vVerts[i][2] = 0.0f;
127 | }
128 |
129 | largeStarBatch.Begin(GL_POINTS, LARGE_STARS);
130 | largeStarBatch.CopyVertexData3f(vVerts);
131 | largeStarBatch.End();
132 | }
133 |
134 | //初始化山轮廓批次
135 | void SetupMountainRangeBatch() {
136 | M3DVector3f vMountains[12] = {
137 | 0.0f, 25.0f, 0.0f,
138 | 50.0f, 100.0f, 0.0f,
139 | 100.0f, 25.0f, 0.0f,
140 | 225.0f, 125.0f, 0.0f,
141 | 300.0f, 50.0f, 0.0f,
142 | 375.0f, 100.0f, 0.0f,
143 | 460.0f, 25.0f, 0.0f,
144 | 525.0f, 100.0f, 0.0f,
145 | 600.0f, 20.0f, 0.0f,
146 | 675.0f, 70.0f, 0.0f,
147 | 750.0f, 25.0f, 0.0f,
148 | 800.0f, 90.0f, 0.0f
149 | };
150 |
151 | mountainRangeBatch.Begin(GL_LINE_STRIP, 12);
152 | mountainRangeBatch.CopyVertexData3f(vMountains);
153 | mountainRangeBatch.End();
154 | }
155 |
156 | //初始化月亮批次
157 | void SetupMoonBatch() {
158 | GLfloat x = 700.0f;
159 | GLfloat y = 500.0f;
160 | GLfloat r = 50.0f;
161 | M3DVector3f vVerts[SMALL_STARS];
162 | int nVerts = 0;
163 | vVerts[nVerts][0] = x;
164 | vVerts[nVerts][1] = y;
165 | vVerts[nVerts][2] = 0.0f;
166 | for(GLfloat angle = 0; angle < M3D_2PI; angle += 0.2f) {
167 | nVerts++;
168 | vVerts[nVerts][0] = x + float(cos(angle)) * r;
169 | vVerts[nVerts][1] = y + float(sin(angle)) * r;
170 | vVerts[nVerts][2] = 0.0f;
171 | }
172 | nVerts++;
173 | vVerts[nVerts][0] = x + r;;
174 | vVerts[nVerts][1] = y;
175 | vVerts[nVerts][2] = 0.0f;
176 |
177 | moonBatch.Begin(GL_TRIANGLE_FAN, 34);
178 | moonBatch.CopyVertexData3f(vVerts);
179 | moonBatch.End();
180 | }
181 |
182 | //程序化一次性初始化
183 | void SetupRC() {
184 | //设置背景色为黑色
185 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
186 |
187 | //初始化着色器
188 | shaderManager.InitializeStockShaders();
189 |
190 | //初始化各个图元批次
191 | SetupSmallStarBatch();
192 | SetupMeiumStarBatch();
193 | SetupLargeStarBatch();
194 | SetupMountainRangeBatch();
195 | SetupMoonBatch();
196 | }
197 |
198 | //窗口大小改变时接受新的宽度和高度
199 | void ChangeSize(int w, int h) {
200 | glViewport(0, 0, w, h);
201 |
202 | //正投影
203 | viewFrustum.SetOrthographic(0.0f, SCREEN_X, 0.0f, SCREEN_Y, -1.0f, 1.0f);
204 | }
205 |
206 | //程序入口
207 | int main(int argc, char* argv[]) {
208 | //设置当前工作目录,针对MAC OS X
209 | gltSetWorkingDirectory(argv[0]);
210 |
211 | //GLUT初始化
212 | glutInit(&argc, argv);
213 |
214 | /*初始化渲染模式,其中标志GLUT_DOUBLE、GLUT_RGBA、GLUT_DEPTH、GLUT_STENCIL分别指
215 | 双缓冲窗口、RGBA颜色模式、深度测试、模板缓冲区*/
216 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
217 |
218 | //创建窗口大小、标题
219 | glutInitWindowSize(800, 600);
220 | glutCreateWindow("Smoothing Out The Jaggies");
221 |
222 | //创建菜单并绑定回调函数,添加选项,确定右键触发
223 | glutCreateMenu(ProcessMenu);
224 | glutAddMenuEntry("Antialiased Rendering",1);
225 | glutAddMenuEntry("Normal Rendering",2);
226 | glutAttachMenu(GLUT_RIGHT_BUTTON);
227 |
228 | //注册回调函数
229 | glutReshapeFunc(ChangeSize);
230 | glutDisplayFunc(RenderScene);
231 |
232 | //判断驱动是否正常
233 | GLenum err = glewInit();
234 | if (GLEW_OK != err) {
235 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
236 | return 1;
237 | }
238 |
239 | //初始化
240 | SetupRC();
241 |
242 | //运行循环
243 | glutMainLoop();
244 | return 0;
245 | }
246 |
--------------------------------------------------------------------------------
/07-Smoother/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/07-Smoother/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/07-Smoother/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/07-Smoother/libgltools.dylib
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/08-MoveByMatrix/08-MoveByMatrix.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 08-MoveByMatrix.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix/_8_MoveByMatrix.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/08-MoveByMatrix/08-MoveByMatrix/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 08-MoveByMatrix
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 |
14 | GLBatch squareBatch;
15 | GLShaderManager shaderManager;
16 |
17 | GLfloat blockSize = 0.1f;
18 | GLfloat xPos = 0.0f;
19 | GLfloat yPos = 0.0f;
20 |
21 | // 矩形4个顶点位置坐标
22 | GLfloat vVerts[] = {
23 | -blockSize, -blockSize, 0.0f,
24 | blockSize, -blockSize, 0.0f,
25 | blockSize, blockSize, 0.0f,
26 | -blockSize, blockSize, 0.0f
27 | };
28 |
29 |
30 | // 初始化环境
31 | void SetupRC() {
32 | // 设置窗口背景为蓝色
33 | glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
34 |
35 | // 着色器初始化
36 | shaderManager.InitializeStockShaders();
37 |
38 | // 移动矩形批次初始化顶点数据
39 | squareBatch.Begin(GL_TRIANGLE_FAN, 4);
40 | squareBatch.CopyVertexData3f(vVerts);
41 | squareBatch.End();
42 | }
43 |
44 | // 特殊按键点击响应回调
45 | void SpecialKeys(int key, int x, int y) {
46 | // 步进值,每次按键移动的距离
47 | GLfloat stepSize = 0.025f;
48 |
49 | // 4个方向位置偏移
50 | if(key == GLUT_KEY_UP) yPos += stepSize;
51 | if(key == GLUT_KEY_DOWN) yPos -= stepSize;
52 | if(key == GLUT_KEY_LEFT) xPos -= stepSize;
53 | if(key == GLUT_KEY_RIGHT) xPos += stepSize;
54 |
55 | // 边界判断和限制
56 | if(xPos < (-1.0f + blockSize)) xPos = -1.0f + blockSize;
57 | if(xPos > (1.0f - blockSize)) xPos = 1.0f - blockSize;
58 | if(yPos < (-1.0f + blockSize)) yPos = -1.0f + blockSize;
59 | if(yPos > (1.0f - blockSize)) yPos = 1.0f - blockSize;
60 |
61 | // 触发渲染
62 | glutPostRedisplay();
63 | }
64 |
65 | // 窗口渲染回调
66 | void RenderScene(void) {
67 | // 清除缓存区
68 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
69 |
70 | // 创建3个4x4矩阵,分别是合成矩阵、平移矩阵、旋转矩阵
71 | M3DMatrix44f mFinalTransform, mTranslationMatrix, mRotationMatrix;
72 |
73 | // 平移(xPos, yPos, 0)的矩阵表示
74 | m3dTranslationMatrix44(mTranslationMatrix, xPos, yPos, 0.0f);
75 |
76 | // 绕z轴旋转的矩阵,每次旋转角度加5度,m3dDegToRad = 角度 -> 弧度
77 | static float zRot = 0.0f;
78 | zRot += 5.0f;
79 | m3dRotationMatrix44(mRotationMatrix, m3dDegToRad(zRot), 0.0f, 0.0f, 1.0f);
80 |
81 | // 矩阵相乘,参数顺序很重要,先平移,后旋转
82 | m3dMatrixMultiply44(mFinalTransform, mTranslationMatrix, mRotationMatrix);
83 |
84 | // 着色器上色,移动矩形为红色
85 | GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
86 | shaderManager.UseStockShader(GLT_SHADER_FLAT, mFinalTransform, vRed);
87 | squareBatch.Draw();
88 |
89 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
90 | glutSwapBuffers();
91 | }
92 |
93 | // 窗口变化回调
94 | void ChangeSize(int width, int height) {
95 | // 设置视口
96 | glViewport(0, 0, width, height);
97 | }
98 |
99 | // 程序入口
100 | int main(int argc, char* argv[]) {
101 | // 设置 Mac OS 工作目录路径
102 | gltSetWorkingDirectory(argv[0]);
103 |
104 | // GLUT初始化
105 | glutInit(&argc, argv);
106 |
107 | // 设置渲染模式
108 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
109 |
110 | // 初始化窗口大小
111 | glutInitWindowSize(600, 600);
112 |
113 | // 创建窗口并命名
114 | glutCreateWindow("Move Block with Arrow Keys");
115 |
116 | // 判断驱动程序是否初始化完毕
117 | GLenum err = glewInit();
118 | if (GLEW_OK != err) {
119 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
120 | return 1;
121 | }
122 |
123 | // 窗口变化回调函数设置
124 | glutReshapeFunc(ChangeSize);
125 |
126 | // 窗口渲染回调函数设置
127 | glutDisplayFunc(RenderScene);
128 |
129 | // 特殊按钮响应回调函数设置
130 | glutSpecialFunc(SpecialKeys);
131 |
132 | // 初始化环境
133 | SetupRC();
134 |
135 | // 主消息循环
136 | glutMainLoop();
137 |
138 | return 0;
139 | }
140 |
--------------------------------------------------------------------------------
/08-MoveByMatrix/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/08-MoveByMatrix/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/08-MoveByMatrix/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/08-MoveByMatrix/libgltools.dylib
--------------------------------------------------------------------------------
/09-Objects/09-Objects.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/09-Objects/09-Objects.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/09-Objects/09-Objects.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/09-Objects/09-Objects.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 09-Objects.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/09-Objects/09-Objects/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/09-Objects/09-Objects/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/09-Objects/09-Objects/_9_Objects.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/09-Objects/09-Objects/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 09-Objects
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | GLShaderManager shaderManager;
19 | GLMatrixStack modelViewMatrix;
20 | GLMatrixStack projectionMatrix;
21 | GLFrame cameraFrame;
22 | GLFrame objectFrame;
23 | GLFrustum viewFrustum;
24 |
25 | GLTriangleBatch sphereBatch;
26 | GLTriangleBatch torusBatch;
27 | GLTriangleBatch cylinderBatch;
28 | GLTriangleBatch coneBatch;
29 | GLTriangleBatch diskBatch;
30 |
31 | GLGeometryTransform transformPipeline;
32 |
33 | GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f };
34 | GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
35 |
36 | int nStep = 0;
37 |
38 | // 程序初始化环境
39 | void SetupRC() {
40 | // 设置背景为灰色背景
41 | glClearColor(0.7f, 0.7f, 0.7f, 1.0f );
42 |
43 | // 初始化着色器
44 | shaderManager.InitializeStockShaders();
45 |
46 | // 开启深度测试
47 | glEnable(GL_DEPTH_TEST);
48 |
49 | // 设置变换管线,管理模型视图堆栈和投影堆栈
50 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
51 |
52 | // 照相机位置往后移动 15
53 | cameraFrame.MoveForward(-15.0f);
54 |
55 | // 初始化球体的三角形批次,后面参数依次是,球半径,片段数,堆叠数
56 | gltMakeSphere(sphereBatch, 3.0, 10, 20);
57 |
58 | // 初始化花托的三角形批次,后面参数依次是,外半径,内半径,片段数,堆叠数
59 | gltMakeTorus(torusBatch, 3.0f, 0.75f, 150, 15);
60 |
61 | // 初始化圆柱的三角形批次,后面参数依次是,底部半径,顶部半径,高度,片段数,堆叠数
62 | gltMakeCylinder(cylinderBatch, 2.0f, 2.0f, 3.0f, 13, 2);
63 |
64 | // 初始化圆锥的三角形批次,后面参数依次是,底部半径,顶部半径,高度,片段数,堆叠数
65 | gltMakeCylinder(coneBatch, 2.0f, 0.0f, 3.0f, 13, 2);
66 |
67 | // 初始化圆盘的三角形批次,后面参数依次是,内半径,外半径,片段数,堆叠数
68 | gltMakeDisk(diskBatch, 1.5f, 3.0f, 13, 3);
69 | }
70 |
71 | // 画三角形批次共用方法
72 | void DrawWireFramedBatch(GLTriangleBatch* pBatch) {
73 |
74 | // 画物体表面画绿色
75 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vGreen);
76 | pBatch->Draw();
77 |
78 | // 设置多边形偏移
79 | glPolygonOffset(-1.0f, -1.0f);
80 |
81 | // 开启线段的抗锯齿
82 | glEnable(GL_LINE_SMOOTH);
83 |
84 | // 开启混合颜色模式
85 | glEnable(GL_BLEND);
86 |
87 | // 设置混合方程式
88 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
89 |
90 | // 设置多边型模式为线段
91 | glEnable(GL_POLYGON_OFFSET_LINE);
92 |
93 | // 设置多边形模式为前后面的线段
94 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
95 |
96 | // 设置线宽 2.5
97 | glLineWidth(2.5f);
98 |
99 | // 画物体线条黑色
100 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack);
101 | pBatch->Draw();
102 |
103 | // 以下为还原渲染环境,养成还原环境的好习惯
104 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
105 | glDisable(GL_POLYGON_OFFSET_LINE);
106 | glLineWidth(1.0f);
107 | glDisable(GL_BLEND);
108 | glDisable(GL_LINE_SMOOTH);
109 | }
110 |
111 | // 窗口渲染调用
112 | void RenderScene(void) {
113 | // 清除缓存区
114 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
115 |
116 | // 模型堆栈压栈,顶部是单位矩阵
117 | modelViewMatrix.PushMatrix();
118 |
119 | // 获取照相机位置矩阵
120 | M3DMatrix44f mCamera;
121 | cameraFrame.GetCameraMatrix(mCamera);
122 |
123 | // 单位矩阵 x 照相机位置矩阵 = 照相机位置矩阵
124 | modelViewMatrix.MultMatrix(mCamera);
125 |
126 | // 获取物体位置矩阵
127 | M3DMatrix44f mObjectFrame;
128 | objectFrame.GetMatrix(mObjectFrame);
129 |
130 | // 照相机位置矩阵 x 物体位置矩阵 = 物体在照相机下的位置矩阵
131 | modelViewMatrix.MultMatrix(mObjectFrame);
132 |
133 | // 设置着色器为黑色
134 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack);
135 |
136 | // 根据不同状态,画不同的对象
137 | switch(nStep) {
138 | case 0: DrawWireFramedBatch(&sphereBatch); break;
139 | case 1: DrawWireFramedBatch(&torusBatch); break;
140 | case 2: DrawWireFramedBatch(&cylinderBatch); break;
141 | case 3: DrawWireFramedBatch(&coneBatch); break;
142 | case 4: DrawWireFramedBatch(&diskBatch); break;
143 | }
144 |
145 | // 出栈,还原栈顶为单位矩阵
146 | modelViewMatrix.PopMatrix();
147 |
148 | // 双缓冲区模式,后台缓冲区切换到前台进行显示
149 | glutSwapBuffers();
150 | }
151 |
152 |
153 | // 特殊按键点击响应
154 | void SpecialKeys(int key, int x, int y) {
155 |
156 | // 按下上、下、左、右方向键,对物体进行旋转,m3dDegToRad = 角度 -> 弧度
157 | switch(key) {
158 | case GLUT_KEY_UP:
159 | // angle=-5, x=1, y=0, z=0 表示绕x轴正方向顺时针旋转(从x轴正方向看去)
160 | objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
161 | break;
162 | case GLUT_KEY_DOWN:
163 | // angle=5, x=1, y=0, z=0 表示绕x轴正方向逆时针旋转(从x轴正方向看去)
164 | objectFrame.RotateWorld(m3dDegToRad(-5.0f), -1.0f, 0.0f, 0.0f);
165 | break;
166 | case GLUT_KEY_LEFT:
167 | // angle=-5, x=0, y=1, z=0 表示绕y轴正方向顺时针旋转(从y轴正方向看去)
168 | objectFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
169 | break;
170 | case GLUT_KEY_RIGHT:
171 | // angle=5, x=0, y=1, z=0 表示绕y轴正方向逆时针旋转(从y轴正方向看去)
172 | objectFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
173 | break;
174 | }
175 |
176 | // 触发渲染
177 | glutPostRedisplay();
178 | }
179 |
180 | // 普通按键点击响应
181 | void KeyPressFunc(unsigned char key, int x, int y) {
182 |
183 | // key = 32 是空格键,5次空格键一次循环
184 | if(key == 32) {
185 | nStep++;
186 | if(nStep > 4) nStep = 0;
187 | }
188 |
189 | // 切换窗口标题
190 | switch(nStep) {
191 | case 0: glutSetWindowTitle("Sphere"); break;
192 | case 1: glutSetWindowTitle("Torus"); break;
193 | case 2: glutSetWindowTitle("Cylinder"); break;
194 | case 3: glutSetWindowTitle("Cone"); break;
195 | case 4: glutSetWindowTitle("Disk"); break;
196 | }
197 |
198 | // 触发渲染
199 | glutPostRedisplay();
200 | }
201 |
202 | // 窗口变化回调
203 | void ChangeSize(int width, int height) {
204 | // 设置视口
205 | glViewport(0, 0, width, height);
206 |
207 | // 防止除数为0导致错误
208 | if(height <= 0) height = 1;
209 |
210 | // 设置投影矩阵,参数分别为:投影视角,宽高比,近点距离,远点距离
211 | viewFrustum.SetPerspective(35.0f, float(width) / float(height), 1.0f, 500.0f);
212 |
213 | // 投影矩阵堆栈顶部载入当前设置的投影矩阵
214 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
215 |
216 | // 模型矩阵堆栈载入单位矩阵
217 | modelViewMatrix.LoadIdentity();
218 | }
219 |
220 | // 程序入口
221 | int main(int argc, char* argv[]) {
222 | // 针对 Mac OS 设置当前工作目录路径
223 | gltSetWorkingDirectory(argv[0]);
224 |
225 | // 初始化 GLUT
226 | glutInit(&argc, argv);
227 |
228 | // 设置渲染模式,双缓冲、颜色、深度测试、模式
229 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
230 |
231 | // 初始化窗口大小
232 | glutInitWindowSize(800, 600);
233 |
234 | // 创建窗口并命名
235 | glutCreateWindow("Sphere");
236 |
237 | // 驱动程序初始化是否成功
238 | GLenum err = glewInit();
239 | if (GLEW_OK != err) {
240 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
241 | return 1;
242 | }
243 |
244 | // 窗口大小变化回调函数设置
245 | glutReshapeFunc(ChangeSize);
246 |
247 | // 普通键盘按钮点击监听回调函数设置
248 | glutKeyboardFunc(KeyPressFunc);
249 |
250 | // 特殊键盘按钮点击监听回调函数设置
251 | glutSpecialFunc(SpecialKeys);
252 |
253 | // 窗口渲染回调函数设置
254 | glutDisplayFunc(RenderScene);
255 |
256 | // 程序初始化环境
257 | SetupRC();
258 |
259 | // 主消息循环
260 | glutMainLoop();
261 |
262 | return 0;
263 | }
264 |
--------------------------------------------------------------------------------
/09-Objects/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/09-Objects/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/09-Objects/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/09-Objects/libgltools.dylib
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/10-Orthographic/10-Orthographic.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 10-Orthographic.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic/_0_Orthographic.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/10-Orthographic/10-Orthographic/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 10-Orthographic
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | GLFrame viewFrame;
19 | GLFrustum viewFrustum;
20 | GLBatch tubeBatch;
21 | GLBatch innerBatch;
22 | GLMatrixStack modelViewMatix;
23 | GLMatrixStack projectionMatrix;
24 | GLGeometryTransform transformPipeline;
25 | GLShaderManager shaderManager;
26 | float fZ = 100.0f;
27 | float bZ = -100.0f;
28 |
29 | // 窗口渲染调用
30 | void RenderScene(void) {
31 | // 清除缓存区
32 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
33 |
34 | // 压栈,物体矩阵
35 | modelViewMatix.PushMatrix(viewFrame);
36 |
37 | // 默认红色光源着色
38 | GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
39 | shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vRed);
40 | tubeBatch.Draw();
41 |
42 | // 默认灰色光源着色
43 | GLfloat vGray[] = { 0.75f, 0.75f, 0.75f, 1.0f };
44 | shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vGray);
45 | innerBatch.Draw();
46 |
47 | // 出栈,还原为单位矩阵
48 | modelViewMatix.PopMatrix();
49 |
50 | // 双缓存模式,后台缓存切换到前台进行显示
51 | glutSwapBuffers();
52 | }
53 |
54 | void SetupTubeBatch() {
55 | tubeBatch.Begin(GL_QUADS, 200);
56 | tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
57 | // 后面
58 | GLfloat vBack[] = {
59 | // Left Pannel
60 | -50.0f, 50.0f, fZ,
61 | -50.0f, -50.0f, fZ,
62 | -35.0f, -50.0f, fZ,
63 | -35.0f, 50.0f, fZ,
64 | // Right Panel
65 | 50.0f, 50.0f, fZ,
66 | 35.0f, 50.0f, fZ,
67 | 35.0f, -50.0f, fZ,
68 | 50.0f,-50.0f, fZ,
69 | // Top Panel
70 | -35.0f, 50.0f, fZ,
71 | -35.0f, 35.0f, fZ,
72 | 35.0f, 35.0f, fZ,
73 | 35.0f, 50.0f, fZ,
74 | // Bottom Panel
75 | -35.0f, -35.0f, fZ,
76 | -35.0f, -50.0f, fZ,
77 | 35.0f, -50.0f, fZ,
78 | 35.0f, -35.0f, fZ
79 | };
80 | for(int i = 0; i < 16; i++) {
81 | int index = i * 3;
82 | tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
83 | tubeBatch.Vertex3f(vBack[index], vBack[index+1], vBack[index+2]);
84 | }
85 |
86 | // 前面
87 | GLfloat vFront[] = {
88 | // Left Pannel
89 | -35.0f, 50.0f, bZ,
90 | -35.0f, -50.0f, bZ,
91 | -50.0f, -50.0f, bZ,
92 | -50.0f, 50.0f, bZ,
93 | // Right Panel
94 | 50.0f, -50.0f, bZ,
95 | 35.0f, -50.0f, bZ,
96 | 35.0f, 50.0f, bZ,
97 | 50.0f, 50.0f, bZ,
98 | // Top Panel
99 | 35.0f, 50.0f, bZ,
100 | 35.0f, 35.0f, bZ,
101 | -35.0f, 35.0f, bZ,
102 | -35.0f, 50.0f, bZ,
103 | // Bottom Panel
104 | 35.0f, -35.0f, bZ,
105 | 35.0f, -50.0f, bZ,
106 | -35.0f, -50.0f, bZ,
107 | -35.0f, -35.0f, bZ
108 | };
109 | for(int i = 0; i < 16; i++) {
110 | int index = i * 3;
111 | tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
112 | tubeBatch.Vertex3f(vFront[index], vFront[index+1], vFront[index+2]);
113 | }
114 |
115 | // 上面
116 | GLfloat vTop[] = {
117 | -50.0f, 50.0f, fZ,
118 | 50.0f, 50.0f, fZ,
119 | 50.0f, 50.0f, bZ,
120 | -50.0f, 50.0f, bZ
121 | };
122 | for(int i = 0; i < 4; i++) {
123 | int index = i * 3;
124 | tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
125 | tubeBatch.Vertex3f(vTop[index], vTop[index+1], vTop[index+2]);
126 | }
127 |
128 | // 下面
129 | GLfloat vBottom[] = {
130 | -50.0f, -50.0f, fZ,
131 | -50.0f, -50.0f, bZ,
132 | 50.0f, -50.0f, bZ,
133 | 50.0f, -50.0f, fZ
134 | };
135 | for(int i = 0; i < 4; i++) {
136 | int index = i * 3;
137 | tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
138 | tubeBatch.Vertex3f(vBottom[index], vBottom[index+1], vBottom[index+2]);
139 | }
140 |
141 | // 左面
142 | GLfloat vLeft[] = {
143 | 50.0f, 50.0f, fZ,
144 | 50.0f, -50.0f, fZ,
145 | 50.0f, -50.0f, bZ,
146 | 50.0f, 50.0f, bZ
147 | };
148 | for(int i = 0; i < 4; i++) {
149 | int index = i * 3;
150 | tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
151 | tubeBatch.Vertex3f(vLeft[index], vLeft[index+1], vLeft[index+2]);
152 | }
153 |
154 | // 右面
155 | GLfloat vRight[] = {
156 | -50.0f, 50.0f, fZ,
157 | -50.0f, 50.0f, bZ,
158 | -50.0f, -50.0f, bZ,
159 | -50.0f, -50.0f, fZ
160 | };
161 | for(int i = 0; i < 4; i++) {
162 | int index = i * 3;
163 | tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
164 | tubeBatch.Vertex3f(vRight[index], vRight[index+1], vRight[index+2]);
165 | }
166 | tubeBatch.End();
167 | }
168 |
169 | void SetupInnerBatch() {
170 | innerBatch.Begin(GL_QUADS, 40);
171 | innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
172 | // 上面
173 | GLfloat innerTop[] = {
174 | -35.0f, 35.0f, fZ,
175 | 35.0f, 35.0f, fZ,
176 | 35.0f, 35.0f, bZ,
177 | -35.0f, 35.0f, bZ
178 | };
179 | for(int i = 0; i < 4; i++) {
180 | int index = i * 3;
181 | innerBatch.Normal3f(0.0f, -1.0f, 0.0f);
182 | innerBatch.Vertex3f(innerTop[index], innerTop[index+1], innerTop[index+2]);
183 | }
184 |
185 | // 下面
186 | GLfloat innerBottom[] = {
187 | -35.0f, -35.0f, fZ,
188 | -35.0f, -35.0f, bZ,
189 | 35.0f, -35.0f, bZ,
190 | 35.0f, -35.0f, fZ
191 | };
192 | for(int i = 0; i < 4; i++) {
193 | int index = i * 3;
194 | innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
195 | innerBatch.Vertex3f(innerBottom[index], innerBottom[index+1], innerBottom[index+2]);
196 | }
197 |
198 | // 左面
199 | GLfloat innerLeft[] = {
200 | 35.0f, 35.0f, fZ,
201 | 35.0f, -35.0f, fZ,
202 | 35.0f, -35.0f, bZ,
203 | 35.0f, 35.0f, bZ
204 | };
205 | for(int i = 0; i < 4; i++) {
206 | int index = i * 3;
207 | innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
208 | innerBatch.Vertex3f(innerLeft[index], innerLeft[index+1], innerLeft[index+2]);
209 | }
210 |
211 | // 右面
212 | GLfloat innerRight[] = {
213 | -35.0f, 35.0f, fZ,
214 | -35.0f, 35.0f, bZ,
215 | -35.0f, -35.0f, bZ,
216 | -35.0f, -35.0f, fZ
217 | };
218 | for(int i = 0; i < 4; i++) {
219 | int index = i * 3;
220 | innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
221 | innerBatch.Vertex3f(innerRight[index], innerRight[index+1], innerRight[index+2]);
222 | }
223 |
224 | innerBatch.End();
225 | }
226 |
227 | // 程序初始化环境
228 | void SetupRC() {
229 | // 设置背景颜色为淡蓝色
230 | glClearColor(0.0f, 0.0f, 0.75f, 1.0f );
231 |
232 | // 开启深度测试
233 | glEnable(GL_DEPTH_TEST);
234 |
235 | // 着色器初始化
236 | shaderManager.InitializeStockShaders();
237 |
238 | // 创建长方体暴露在外面的红色面
239 | SetupTubeBatch();
240 |
241 | // 创建长方体里面的灰色面
242 | SetupInnerBatch();
243 | }
244 |
245 | void SpecialKeys(int key, int x, int y) {
246 | // 按下上、下、左、右方向键,对物体进行旋转,m3dDegToRad = 角度 -> 弧度
247 | switch(key) {
248 | case GLUT_KEY_UP:
249 | // angle=-5, x=1, y=0, z=0 表示绕x轴正方向顺时针旋转(从x轴正方向看去)
250 | viewFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
251 | break;
252 | case GLUT_KEY_DOWN:
253 | // angle=5, x=1, y=0, z=0 表示绕x轴正方向逆时针旋转(从x轴正方向看去)
254 | viewFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);
255 | break;
256 | case GLUT_KEY_LEFT:
257 | // angle=-5, x=0, y=1, z=0 表示绕y轴正方向顺时针旋转(从y轴正方向看去)
258 | viewFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
259 | break;
260 | case GLUT_KEY_RIGHT:
261 | // angle=5, x=0, y=1, z=0 表示绕y轴正方向逆时针旋转(从y轴正方向看去)
262 | viewFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
263 | break;
264 | }
265 |
266 | // 重写渲染窗口
267 | glutPostRedisplay();
268 | }
269 |
270 | // 窗口变化回调
271 | void ChangeSize(int width, int height) {
272 |
273 | // 设置视口
274 | glViewport(0, 0, width, height);
275 |
276 | // 设置正投影,(xMin, xMax, yMin, yMax, zMin, zMax)
277 | viewFrustum.SetOrthographic(-130.0f, 130.0f, -130.0f, 130.0f, -130.0f, 130.0f);
278 |
279 | // 获得到的正投影矩阵载入堆栈中
280 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
281 |
282 | // 变换管线,管理2个堆栈
283 | transformPipeline.SetMatrixStacks(modelViewMatix, projectionMatrix);
284 | }
285 |
286 | // 程序入口
287 | int main(int argc, char* argv[]) {
288 | // 针对 Mac OS 设置工作目录路径
289 | gltSetWorkingDirectory(argv[0]);
290 |
291 | // 初始化 GLUT
292 | glutInit(&argc, argv);
293 |
294 | // 初始化渲染模式
295 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
296 |
297 | // 初始化窗口大小
298 | glutInitWindowSize(800, 600);
299 |
300 | // 创建窗口并命名
301 | glutCreateWindow("Orthographic Projection Example");
302 |
303 | // 检测驱动程序是否初始化成功
304 | GLenum err = glewInit();
305 | if (GLEW_OK != err) {
306 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
307 | return 1;
308 | }
309 |
310 | // 窗口大小改变回调函数设置
311 | glutReshapeFunc(ChangeSize);
312 |
313 | // 特殊按键点击回调函数设置
314 | glutSpecialFunc(SpecialKeys);
315 |
316 | // 窗口渲染回调函数设置
317 | glutDisplayFunc(RenderScene);
318 |
319 | // 程序初始化环境
320 | SetupRC();
321 |
322 | // 主消息循环
323 | glutMainLoop();
324 |
325 | return 0;
326 | }
327 |
--------------------------------------------------------------------------------
/10-Orthographic/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/10-Orthographic/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/10-Orthographic/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/10-Orthographic/libgltools.dylib
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/11-Perspective/11-Perspective.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 11-Perspective.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective/_1_Perspective.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/11-Perspective/11-Perspective/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 11-Perspective
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 |
19 | GLFrame viewFrame;
20 | GLFrustum viewFrustum;
21 | GLBatch tubeBatch;
22 | GLBatch innerBatch;
23 | GLMatrixStack modelViewMatix;
24 | GLMatrixStack projectionMatrix;
25 | GLGeometryTransform transformPipeline;
26 | GLShaderManager shaderManager;
27 | float fZ = 100.0f;
28 | float bZ = -100.0f;
29 |
30 | // 窗口渲染调用
31 | void RenderScene(void) {
32 | // 清除缓存区
33 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
34 |
35 | // 压栈,物体矩阵
36 | modelViewMatix.PushMatrix(viewFrame);
37 |
38 | // 默认红色光源着色
39 | GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
40 | shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vRed);
41 | tubeBatch.Draw();
42 |
43 | // 默认灰色光源着色
44 | GLfloat vGray[] = { 0.75f, 0.75f, 0.75f, 1.0f };
45 | shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vGray);
46 | innerBatch.Draw();
47 |
48 | // 出栈,还原为单位矩阵
49 | modelViewMatix.PopMatrix();
50 |
51 | // 双缓存模式,后台缓存切换到前台进行显示
52 | glutSwapBuffers();
53 | }
54 |
55 | void SetupTubeBatch() {
56 | tubeBatch.Begin(GL_QUADS, 200);
57 | tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
58 | // 后面
59 | GLfloat vBack[] = {
60 | // Left Pannel
61 | -50.0f, 50.0f, fZ,
62 | -50.0f, -50.0f, fZ,
63 | -35.0f, -50.0f, fZ,
64 | -35.0f, 50.0f, fZ,
65 | // Right Panel
66 | 50.0f, 50.0f, fZ,
67 | 35.0f, 50.0f, fZ,
68 | 35.0f, -50.0f, fZ,
69 | 50.0f,-50.0f, fZ,
70 | // Top Panel
71 | -35.0f, 50.0f, fZ,
72 | -35.0f, 35.0f, fZ,
73 | 35.0f, 35.0f, fZ,
74 | 35.0f, 50.0f, fZ,
75 | // Bottom Panel
76 | -35.0f, -35.0f, fZ,
77 | -35.0f, -50.0f, fZ,
78 | 35.0f, -50.0f, fZ,
79 | 35.0f, -35.0f, fZ
80 | };
81 | for(int i = 0; i < 16; i++) {
82 | int index = i * 3;
83 | tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
84 | tubeBatch.Vertex3f(vBack[index], vBack[index+1], vBack[index+2]);
85 | }
86 |
87 | // 前面
88 | GLfloat vFront[] = {
89 | // Left Pannel
90 | -35.0f, 50.0f, bZ,
91 | -35.0f, -50.0f, bZ,
92 | -50.0f, -50.0f, bZ,
93 | -50.0f, 50.0f, bZ,
94 | // Right Panel
95 | 50.0f, -50.0f, bZ,
96 | 35.0f, -50.0f, bZ,
97 | 35.0f, 50.0f, bZ,
98 | 50.0f, 50.0f, bZ,
99 | // Top Panel
100 | 35.0f, 50.0f, bZ,
101 | 35.0f, 35.0f, bZ,
102 | -35.0f, 35.0f, bZ,
103 | -35.0f, 50.0f, bZ,
104 | // Bottom Panel
105 | 35.0f, -35.0f, bZ,
106 | 35.0f, -50.0f, bZ,
107 | -35.0f, -50.0f, bZ,
108 | -35.0f, -35.0f, bZ
109 | };
110 | for(int i = 0; i < 16; i++) {
111 | int index = i * 3;
112 | tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
113 | tubeBatch.Vertex3f(vFront[index], vFront[index+1], vFront[index+2]);
114 | }
115 |
116 | // 上面
117 | GLfloat vTop[] = {
118 | -50.0f, 50.0f, fZ,
119 | 50.0f, 50.0f, fZ,
120 | 50.0f, 50.0f, bZ,
121 | -50.0f, 50.0f, bZ
122 | };
123 | for(int i = 0; i < 4; i++) {
124 | int index = i * 3;
125 | tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
126 | tubeBatch.Vertex3f(vTop[index], vTop[index+1], vTop[index+2]);
127 | }
128 |
129 | // 下面
130 | GLfloat vBottom[] = {
131 | -50.0f, -50.0f, fZ,
132 | -50.0f, -50.0f, bZ,
133 | 50.0f, -50.0f, bZ,
134 | 50.0f, -50.0f, fZ
135 | };
136 | for(int i = 0; i < 4; i++) {
137 | int index = i * 3;
138 | tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
139 | tubeBatch.Vertex3f(vBottom[index], vBottom[index+1], vBottom[index+2]);
140 | }
141 |
142 | // 左面
143 | GLfloat vLeft[] = {
144 | 50.0f, 50.0f, fZ,
145 | 50.0f, -50.0f, fZ,
146 | 50.0f, -50.0f, bZ,
147 | 50.0f, 50.0f, bZ
148 | };
149 | for(int i = 0; i < 4; i++) {
150 | int index = i * 3;
151 | tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
152 | tubeBatch.Vertex3f(vLeft[index], vLeft[index+1], vLeft[index+2]);
153 | }
154 |
155 | // 右面
156 | GLfloat vRight[] = {
157 | -50.0f, 50.0f, fZ,
158 | -50.0f, 50.0f, bZ,
159 | -50.0f, -50.0f, bZ,
160 | -50.0f, -50.0f, fZ
161 | };
162 | for(int i = 0; i < 4; i++) {
163 | int index = i * 3;
164 | tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
165 | tubeBatch.Vertex3f(vRight[index], vRight[index+1], vRight[index+2]);
166 | }
167 | tubeBatch.End();
168 | }
169 |
170 | void SetupInnerBatch() {
171 | innerBatch.Begin(GL_QUADS, 40);
172 | innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
173 | // 上面
174 | GLfloat innerTop[] = {
175 | -35.0f, 35.0f, fZ,
176 | 35.0f, 35.0f, fZ,
177 | 35.0f, 35.0f, bZ,
178 | -35.0f, 35.0f, bZ
179 | };
180 | for(int i = 0; i < 4; i++) {
181 | int index = i * 3;
182 | innerBatch.Normal3f(0.0f, -1.0f, 0.0f);
183 | innerBatch.Vertex3f(innerTop[index], innerTop[index+1], innerTop[index+2]);
184 | }
185 |
186 | // 下面
187 | GLfloat innerBottom[] = {
188 | -35.0f, -35.0f, fZ,
189 | -35.0f, -35.0f, bZ,
190 | 35.0f, -35.0f, bZ,
191 | 35.0f, -35.0f, fZ
192 | };
193 | for(int i = 0; i < 4; i++) {
194 | int index = i * 3;
195 | innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
196 | innerBatch.Vertex3f(innerBottom[index], innerBottom[index+1], innerBottom[index+2]);
197 | }
198 |
199 | // 左面
200 | GLfloat innerLeft[] = {
201 | 35.0f, 35.0f, fZ,
202 | 35.0f, -35.0f, fZ,
203 | 35.0f, -35.0f, bZ,
204 | 35.0f, 35.0f, bZ
205 | };
206 | for(int i = 0; i < 4; i++) {
207 | int index = i * 3;
208 | innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
209 | innerBatch.Vertex3f(innerLeft[index], innerLeft[index+1], innerLeft[index+2]);
210 | }
211 |
212 | // 右面
213 | GLfloat innerRight[] = {
214 | -35.0f, 35.0f, fZ,
215 | -35.0f, 35.0f, bZ,
216 | -35.0f, -35.0f, bZ,
217 | -35.0f, -35.0f, fZ
218 | };
219 | for(int i = 0; i < 4; i++) {
220 | int index = i * 3;
221 | innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
222 | innerBatch.Vertex3f(innerRight[index], innerRight[index+1], innerRight[index+2]);
223 | }
224 |
225 | innerBatch.End();
226 | }
227 |
228 | // 程序初始化环境
229 | void SetupRC() {
230 | // 设置背景颜色为淡蓝色
231 | glClearColor(0.0f, 0.0f, 0.75f, 1.0f);
232 |
233 | // 开启深度测试
234 | glEnable(GL_DEPTH_TEST);
235 |
236 | // 着色器初始化
237 | shaderManager.InitializeStockShaders();
238 |
239 | // 物体向里面移动 450 距离
240 | viewFrame.MoveForward(450.0f);
241 |
242 | // 创建长方体暴露在外面的红色面
243 | SetupTubeBatch();
244 |
245 | // 创建长方体里面的灰色面
246 | SetupInnerBatch();
247 | }
248 |
249 | void SpecialKeys(int key, int x, int y) {
250 | // 按下上、下、左、右方向键,对物体进行旋转,m3dDegToRad = 角度 -> 弧度
251 | switch(key) {
252 | case GLUT_KEY_UP:
253 | // angle=-5, x=1, y=0, z=0 表示绕x轴正方向顺时针旋转(从x轴正方向看去)
254 | viewFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
255 | break;
256 | case GLUT_KEY_DOWN:
257 | // angle=5, x=1, y=0, z=0 表示绕x轴正方向逆时针旋转(从x轴正方向看去)
258 | viewFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);
259 | break;
260 | case GLUT_KEY_LEFT:
261 | // angle=-5, x=0, y=1, z=0 表示绕y轴正方向顺时针旋转(从y轴正方向看去)
262 | viewFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
263 | break;
264 | case GLUT_KEY_RIGHT:
265 | // angle=5, x=0, y=1, z=0 表示绕y轴正方向逆时针旋转(从y轴正方向看去)
266 | viewFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
267 | break;
268 | }
269 |
270 | // 重写渲染窗口
271 | glutPostRedisplay();
272 | }
273 |
274 | // 窗口变换回调
275 | void ChangeSize(int width, int height) {
276 | // 防止除数为0
277 | if(height == 0)
278 | height = 1;
279 |
280 | // 设置视口
281 | glViewport(0, 0, width, height);
282 |
283 | // 设置透视投影矩阵,参数分别为:透视角,宽高比,近距,远距
284 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 1000.0f);
285 |
286 | // 保持透视投影矩阵
287 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
288 |
289 | // 利用变换管线,管理2个堆栈
290 | transformPipeline.SetMatrixStacks(modelViewMatix, projectionMatrix);
291 | }
292 |
293 | // 程序入口
294 | int main(int argc, char* argv[]) {
295 | // 针对 Mac OS 设置工作目录路径
296 | gltSetWorkingDirectory(argv[0]);
297 |
298 | // 初始化 GLUT
299 | glutInit(&argc, argv);
300 |
301 | // 初始化渲染模式
302 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
303 |
304 | // 初始化窗口大小
305 | glutInitWindowSize(800, 600);
306 |
307 | // 创建窗口并命名
308 | glutCreateWindow("Perspective Projection Example");
309 |
310 | // 检测驱动程序是否初始化成功
311 | GLenum err = glewInit();
312 | if (GLEW_OK != err) {
313 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
314 | return 1;
315 | }
316 |
317 | // 窗口大小改变回调函数设置
318 | glutReshapeFunc(ChangeSize);
319 |
320 | // 特殊按键点击回调函数设置
321 | glutSpecialFunc(SpecialKeys);
322 |
323 | // 窗口渲染回调函数设置
324 | glutDisplayFunc(RenderScene);
325 |
326 | // 程序初始化环境
327 | SetupRC();
328 |
329 | // 主消息循环
330 | glutMainLoop();
331 |
332 | return 0;
333 | }
334 |
--------------------------------------------------------------------------------
/11-Perspective/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/11-Perspective/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/11-Perspective/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/11-Perspective/libgltools.dylib
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/12-ModelViewProjection/12-ModelViewProjection.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 12-ModelViewProjection.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection/_2_ModelViewProjection.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/12-ModelViewProjection/12-ModelViewProjection/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 12-ModelViewProjection
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 |
19 | // 视景体
20 | GLFrustum viewFrustum;
21 |
22 | // 着色器
23 | GLShaderManager shaderManager;
24 |
25 | // 花托批次
26 | GLTriangleBatch torusBatch;
27 |
28 | // 窗口变化回调
29 | void ChangeSize(int width, int height) {
30 | // 防止除数为0
31 | if(height == 0)
32 | height = 1;
33 |
34 | // 设置视口
35 | glViewport(0, 0, width, height);
36 |
37 | // 设置投影方式是透视投影
38 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 1000.0f);
39 | }
40 |
41 | // 窗口渲染回调
42 | void RenderScene(void) {
43 | // 定义一个测试运行时间
44 | static CStopWatch rotTimer;
45 | // 获取到上一个时间点到当前的时间间隔(单位是每秒刻度数,即 1/60s)
46 | float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
47 |
48 | // 清空缓冲区
49 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
50 |
51 | // 定义矩阵
52 | M3DMatrix44f mTranslate, mRotate, mModelview, mModelViewProjection;
53 |
54 | // 向z轴平移的矩阵变换
55 | m3dTranslationMatrix44(mTranslate, 0.0f, 0.0f, -2.5f);
56 |
57 | // 绕y轴旋转的矩阵变换
58 | m3dRotationMatrix44(mRotate, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
59 |
60 | // 矩阵变换的合并矩阵
61 | m3dMatrixMultiply44(mModelview, mTranslate, mRotate);
62 |
63 | // 投影矩阵 + 视图变换矩阵 = 最终物体位置坐标
64 | m3dMatrixMultiply44(mModelViewProjection, viewFrustum.GetProjectionMatrix(), mModelview);
65 |
66 | // 绘制花托
67 | GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
68 | shaderManager.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vBlack);
69 | torusBatch.Draw();
70 |
71 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
72 | glutSwapBuffers();
73 |
74 | // 自动触发下次渲染回调,达到动画的效果
75 | glutPostRedisplay();
76 | }
77 |
78 | // 程序初始化
79 | void SetupRC() {
80 | // 设置背景颜色为黑色
81 | glClearColor(0.8f, 0.8f, 0.8f, 1.0f );
82 |
83 | // 开启深度测试
84 | glEnable(GL_DEPTH_TEST);
85 |
86 | // 初始化着色器
87 | shaderManager.InitializeStockShaders();
88 |
89 | // 得到花托批次数据
90 | gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
91 |
92 | // 设置多边形模式为前后面线段模式
93 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
94 | }
95 |
96 | // 程序入口
97 | int main(int argc, char* argv[]) {
98 | // 设置 Mac OS 工作目录路径
99 | gltSetWorkingDirectory(argv[0]);
100 |
101 | // GLUT初始化
102 | glutInit(&argc, argv);
103 |
104 | // 设置渲染模式
105 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
106 |
107 | // 初始化窗口大小
108 | glutInitWindowSize(600, 600);
109 |
110 | // 创建窗口并命名
111 | glutCreateWindow("ModelViewProjection");
112 |
113 | // 判断驱动程序是否初始化完毕
114 | GLenum err = glewInit();
115 | if (GLEW_OK != err) {
116 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
117 | return 1;
118 | }
119 |
120 | // 窗口变化回调函数设置
121 | glutReshapeFunc(ChangeSize);
122 |
123 | // 窗口渲染回调函数设置
124 | glutDisplayFunc(RenderScene);
125 |
126 | // 初始化环境
127 | SetupRC();
128 |
129 | // 主消息循环
130 | glutMainLoop();
131 |
132 | return 0;
133 | }
134 |
--------------------------------------------------------------------------------
/12-ModelViewProjection/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/12-ModelViewProjection/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/12-ModelViewProjection/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/12-ModelViewProjection/libgltools.dylib
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/13-SphereWorld-Lv1/13-SphereWorld-Lv1.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 13-SphereWorld-Lv1.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1/_3_SphereWorld_Lv1.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/13-SphereWorld-Lv1/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 13-SphereWorld-Lv1
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 |
20 | // 着色器
21 | GLShaderManager shaderManager;
22 | // 视图矩阵堆栈
23 | GLMatrixStack modelViewMatrix;
24 | // 投影矩阵堆栈
25 | GLMatrixStack projectionMatrix;
26 | // 视景体
27 | GLFrustum viewFrustum;
28 | // 变换管线
29 | GLGeometryTransform transformPipeline;
30 | // 花托批次
31 | GLTriangleBatch torusBatch;
32 | // 地板批次
33 | GLBatch floorBatch;
34 | // 地板颜色,绿色
35 | GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
36 | // 花托线颜色,红色
37 | GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
38 |
39 | // 程序初始化
40 | void SetupRC() {
41 |
42 | // 设置窗口背景颜色为黑色
43 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
44 |
45 | // 初始化着色器
46 | shaderManager.InitializeStockShaders();
47 |
48 | // 开启深度测试
49 | glEnable(GL_DEPTH_TEST);
50 |
51 | // 设置多边形模式为前后面线段模式
52 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
53 |
54 | // 得到花托批次数据
55 | gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
56 |
57 | // 得到方格地板批次数据
58 | floorBatch.Begin(GL_LINES, 324);
59 | for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) {
60 | floorBatch.Vertex3f(x, -0.55f, 20.0f);
61 | floorBatch.Vertex3f(x, -0.55f, -20.0f);
62 | floorBatch.Vertex3f(20.0f, -0.55f, x);
63 | floorBatch.Vertex3f(-20.0f, -0.55f, x);
64 | }
65 | floorBatch.End();
66 | }
67 |
68 | // 窗口渲染回调
69 | void RenderScene(void) {
70 |
71 | // 获取2次渲染之间的时间间隔
72 | static CStopWatch rotTimer;
73 | float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
74 |
75 | // 清空缓冲区
76 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77 |
78 | // 压栈,保持原始矩阵
79 | modelViewMatrix.PushMatrix();
80 |
81 | // 绘制地板
82 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor);
83 | floorBatch.Draw();
84 |
85 | // 视图矩阵进行平移、旋转后进行绘制花托
86 | modelViewMatrix.Translate(0.0f, 0.0f, -2.5f);
87 | modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
88 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor);
89 | torusBatch.Draw();
90 |
91 | // 出栈,恢复原始矩阵
92 | modelViewMatrix.PopMatrix();
93 |
94 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
95 | glutSwapBuffers();
96 |
97 | // 自动触发渲染,达到动画效果
98 | glutPostRedisplay();
99 | }
100 |
101 | // 窗口变换回调
102 | void ChangeSize(int width, int height) {
103 |
104 | // 防止除数为0
105 | if(height == 0)
106 | height = 1;
107 |
108 | // 设置视口
109 | glViewport(0, 0, width, height);
110 |
111 | // 设置透视投影
112 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 100.0f);
113 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
114 |
115 | // 设置变换管线的矩阵堆栈
116 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
117 | }
118 |
119 | // 程序入口
120 | int main(int argc, char* argv[]) {
121 | // 设置 Mac OS 工作目录路径
122 | gltSetWorkingDirectory(argv[0]);
123 |
124 | // GLUT初始化
125 | glutInit(&argc, argv);
126 |
127 | // 设置渲染模式
128 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
129 |
130 | // 初始化窗口大小
131 | glutInitWindowSize(600, 600);
132 |
133 | // 创建窗口并命名
134 | glutCreateWindow("OpenGL SphereWorld");
135 |
136 | // 判断驱动程序是否初始化完毕
137 | GLenum err = glewInit();
138 | if (GLEW_OK != err) {
139 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
140 | return 1;
141 | }
142 |
143 | // 窗口变化回调函数设置
144 | glutReshapeFunc(ChangeSize);
145 |
146 | // 窗口渲染回调函数设置
147 | glutDisplayFunc(RenderScene);
148 |
149 | // 初始化环境
150 | SetupRC();
151 |
152 | // 主消息循环
153 | glutMainLoop();
154 |
155 | return 0;
156 | }
157 |
158 |
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/13-SphereWorld-Lv1/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/13-SphereWorld-Lv1/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/13-SphereWorld-Lv1/libgltools.dylib
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/14-SphereWorld-Lv2/14-SphereWorld-Lv2.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 14-SphereWorld-Lv2.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2/_4_SphereWorld_Lv2.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/14-SphereWorld-Lv2/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 14-SphereWorld-Lv2
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include
19 | #include
20 |
21 | #ifdef __APPLE__
22 | #include
23 | #else
24 | #define FREEGLUT_STATIC
25 | #include
26 | #endif
27 |
28 | // 着色器
29 | GLShaderManager shaderManager;
30 | // 视图矩阵堆栈
31 | GLMatrixStack modelViewMatrix;
32 | // 投影矩阵堆栈
33 | GLMatrixStack projectionMatrix;
34 | // 视景体
35 | GLFrustum viewFrustum;
36 | // 照相机角色【Lv2 增加的代码】
37 | GLFrame cameraFrame;
38 | // 变换管线
39 | GLGeometryTransform transformPipeline;
40 | // 花托批次
41 | GLTriangleBatch torusBatch;
42 | // 地板批次
43 | GLBatch floorBatch;
44 | // 旋转小球批次【Lv2 增加的代码】
45 | GLTriangleBatch sphereBatch;
46 | // 地板颜色,绿色
47 | GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
48 | // 花托线颜色,红色
49 | GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
50 | // 旋转小球颜色,蓝色【Lv2 增加的代码】
51 | GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
52 |
53 | // 程序初始化
54 | void SetupRC() {
55 |
56 | // 设置窗口背景颜色为黑色
57 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
58 |
59 | // 初始化着色器
60 | shaderManager.InitializeStockShaders();
61 |
62 | // 开启深度测试
63 | glEnable(GL_DEPTH_TEST);
64 |
65 | // 设置多边形模式为前后面线段模式
66 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
67 |
68 | // 得到花托批次数据
69 | gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
70 |
71 | // 得到旋转小球批次数据【Lv2 增加的代码】
72 | gltMakeSphere(sphereBatch, 0.1f, 26, 13);
73 |
74 | // 得到方格地板批次数据
75 | floorBatch.Begin(GL_LINES, 324);
76 | for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) {
77 | floorBatch.Vertex3f(x, -0.55f, 20.0f);
78 | floorBatch.Vertex3f(x, -0.55f, -20.0f);
79 | floorBatch.Vertex3f(20.0f, -0.55f, x);
80 | floorBatch.Vertex3f(-20.0f, -0.55f, x);
81 | }
82 | floorBatch.End();
83 | }
84 |
85 | // 窗口渲染回调
86 | void RenderScene(void) {
87 |
88 | // 获取2次渲染之间的时间间隔
89 | static CStopWatch rotTimer;
90 | float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
91 |
92 | // 清空缓冲区
93 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94 |
95 | // 原来是没法移动所以压入原始矩阵,但现在角色可以移动通过角色帧获取照相机矩阵,并压入栈中【Lv2 修改的代码块】
96 | M3DMatrix44f mCamera;
97 | cameraFrame.GetCameraMatrix(mCamera);
98 | modelViewMatrix.PushMatrix(mCamera);
99 |
100 | // 绘制地板
101 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor);
102 | floorBatch.Draw();
103 |
104 | // 视图矩阵进行平移
105 | modelViewMatrix.Translate(0.0f, 0.0f, -2.5f);
106 |
107 | // 保持旋转前的视图矩阵,因为如果不在旋转前保存,会导致旋转小球也会带上花托的旋转【Lv2 增加的代码】
108 | modelViewMatrix.PushMatrix();
109 |
110 | // 继续进行旋转并进行绘制花托
111 | modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
112 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor);
113 | torusBatch.Draw();
114 |
115 | // 恢复到旋转前的视图矩阵【Lv2 增加的代码】
116 | modelViewMatrix.PopMatrix();
117 |
118 | // 绘制旋转小球【Lv2 增加的代码】
119 | modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f);
120 | modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);
121 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor);
122 | sphereBatch.Draw();
123 |
124 | // 出栈,恢复成原始矩阵
125 | modelViewMatrix.PopMatrix();
126 |
127 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
128 | glutSwapBuffers();
129 |
130 | // 自动触发渲染,达到动画效果
131 | glutPostRedisplay();
132 | }
133 |
134 | // 特殊按键点击回调【Lv2 增加的代码】
135 | void SpecialKeys(int key, int x, int y) {
136 | float linear = 0.1f;
137 | float angular = float(m3dDegToRad(5.0f));
138 |
139 | // 控制角色向前后移动
140 | if(key == GLUT_KEY_UP)
141 | cameraFrame.MoveForward(linear);
142 |
143 | if(key == GLUT_KEY_DOWN)
144 | cameraFrame.MoveForward(-linear);
145 |
146 | // 控制角色向左右旋转
147 | if(key == GLUT_KEY_LEFT)
148 | cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
149 |
150 | if(key == GLUT_KEY_RIGHT)
151 | cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
152 | }
153 |
154 | // 窗口变换回调
155 | void ChangeSize(int width, int height) {
156 |
157 | // 防止除数为0
158 | if(height == 0)
159 | height = 1;
160 |
161 | // 设置视口
162 | glViewport(0, 0, width, height);
163 |
164 | // 设置透视投影
165 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 100.0f);
166 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
167 |
168 | // 设置变换管线的矩阵堆栈
169 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
170 | }
171 |
172 | // 程序入口
173 | int main(int argc, char* argv[]) {
174 | // 设置 Mac OS 工作目录路径
175 | gltSetWorkingDirectory(argv[0]);
176 |
177 | // GLUT初始化
178 | glutInit(&argc, argv);
179 |
180 | // 设置渲染模式
181 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
182 |
183 | // 初始化窗口大小
184 | glutInitWindowSize(600, 600);
185 |
186 | // 创建窗口并命名
187 | glutCreateWindow("OpenGL SphereWorld");
188 |
189 | // 判断驱动程序是否初始化完毕
190 | GLenum err = glewInit();
191 | if (GLEW_OK != err) {
192 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
193 | return 1;
194 | }
195 |
196 | // 窗口变化回调函数设置
197 | glutReshapeFunc(ChangeSize);
198 |
199 | // 窗口渲染回调函数设置
200 | glutDisplayFunc(RenderScene);
201 |
202 | // 特殊按键回调函数设置【Lv2 增加的代码】
203 | glutSpecialFunc(SpecialKeys);
204 |
205 | // 初始化环境
206 | SetupRC();
207 |
208 | // 主消息循环
209 | glutMainLoop();
210 |
211 | return 0;
212 | }
213 |
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/14-SphereWorld-Lv2/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/14-SphereWorld-Lv2/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/14-SphereWorld-Lv2/libgltools.dylib
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/15-SphereWorld-Lv3/15-SphereWorld-Lv3.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 15-SphereWorld-Lv3.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3/_5_SphereWorld_Lv3.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/15-SphereWorld-Lv3/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 15-SphereWorld-Lv3
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include
19 | #include
20 |
21 | #ifdef __APPLE__
22 | #include
23 | #else
24 | #define FREEGLUT_STATIC
25 | #include
26 | #endif
27 |
28 | // 着色器
29 | GLShaderManager shaderManager;
30 | // 视图矩阵堆栈
31 | GLMatrixStack modelViewMatrix;
32 | // 投影矩阵堆栈
33 | GLMatrixStack projectionMatrix;
34 | // 视景体
35 | GLFrustum viewFrustum;
36 | // 照相机角色【Lv2 增加的代码】
37 | GLFrame cameraFrame;
38 | // 变换管线
39 | GLGeometryTransform transformPipeline;
40 | // 花托批次
41 | GLTriangleBatch torusBatch;
42 | // 地板批次
43 | GLBatch floorBatch;
44 | // 旋转小球批次【Lv2 增加的代码】
45 | GLTriangleBatch sphereBatch;
46 | // 地板颜色,绿色
47 | GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
48 | // 花托线颜色,红色
49 | GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
50 | // 旋转小球颜色,蓝色【Lv2 增加的代码】
51 | GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
52 |
53 | // 随机小球群
54 | #define NUM_SPHERES 50
55 | GLFrame spheres[NUM_SPHERES];
56 |
57 | // 程序初始化
58 | void SetupRC() {
59 |
60 | // 设置窗口背景颜色为黑色
61 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
62 |
63 | // 初始化着色器
64 | shaderManager.InitializeStockShaders();
65 |
66 | // 开启深度测试
67 | glEnable(GL_DEPTH_TEST);
68 |
69 | // 设置多边形模式为前后面线段模式
70 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
71 |
72 | // 得到花托批次数据
73 | gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
74 |
75 | // 得到旋转小球批次数据【Lv2 增加的代码】
76 | gltMakeSphere(sphereBatch, 0.1f, 26, 13);
77 |
78 | // 得到方格地板批次数据
79 | floorBatch.Begin(GL_LINES, 324);
80 | for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) {
81 | floorBatch.Vertex3f(x, -0.55f, 20.0f);
82 | floorBatch.Vertex3f(x, -0.55f, -20.0f);
83 | floorBatch.Vertex3f(20.0f, -0.55f, x);
84 | floorBatch.Vertex3f(-20.0f, -0.55f, x);
85 | }
86 | floorBatch.End();
87 |
88 | // 随机小球群位置数据生成【Lv3 增加的代码】
89 | for(int i = 0; i < NUM_SPHERES; i++) {
90 | GLfloat x = ((GLfloat)((rand() % 400) - 200) * 0.1f);
91 | GLfloat z = ((GLfloat)((rand() % 400) - 200) * 0.1f);
92 | spheres[i].SetOrigin(x, 0.0f, z);
93 | }
94 | }
95 |
96 | // 窗口渲染回调
97 | void RenderScene(void) {
98 |
99 | // 获取2次渲染之间的时间间隔
100 | static CStopWatch rotTimer;
101 | float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
102 |
103 | // 清空缓冲区
104 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 |
106 | // 原来是没法移动所以压入原始矩阵,但现在角色可以移动通过角色帧获取照相机矩阵,并压入栈中【Lv2 修改的代码块】
107 | M3DMatrix44f mCamera;
108 | cameraFrame.GetCameraMatrix(mCamera);
109 | modelViewMatrix.PushMatrix(mCamera);
110 |
111 | // 绘制地板
112 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor);
113 | floorBatch.Draw();
114 |
115 | // 绘制随机小球群,这里的做法是瞬间切换当前角色位置并开始绘制小球,然后切换回原来角色位置【Lv3 增加的代码】
116 | for(int i = 0; i < NUM_SPHERES; i++) {
117 | // 保存当前矩阵状态,为了能切换回原来的角色位置
118 | modelViewMatrix.PushMatrix();
119 | // 调整当前角色位置为随机生成小球的位置
120 | modelViewMatrix.MultMatrix(spheres[i]);
121 | // 绘制蓝色小球
122 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor);
123 | sphereBatch.Draw();
124 | // 还原矩阵状态,切换为原来角色位置
125 | modelViewMatrix.PopMatrix();
126 | }
127 |
128 | // 视图矩阵进行平移
129 | modelViewMatrix.Translate(0.0f, 0.0f, -2.5f);
130 |
131 | // 保持旋转前的视图矩阵,因为如果不在旋转前保存,会导致旋转小球也会带上花托的旋转【Lv2 增加的代码】
132 | modelViewMatrix.PushMatrix();
133 |
134 | // 继续进行旋转并进行绘制花托
135 | modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
136 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor);
137 | torusBatch.Draw();
138 |
139 | // 恢复到旋转前的视图矩阵【Lv2 增加的代码】
140 | modelViewMatrix.PopMatrix();
141 |
142 | // 绘制旋转小球【Lv2 增加的代码】
143 | modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f);
144 | modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);
145 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor);
146 | sphereBatch.Draw();
147 |
148 | // 出栈,恢复成原始矩阵
149 | modelViewMatrix.PopMatrix();
150 |
151 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
152 | glutSwapBuffers();
153 |
154 | // 自动触发渲染,达到动画效果
155 | glutPostRedisplay();
156 | }
157 |
158 | // 特殊按键点击回调【Lv2 增加的代码】
159 | void SpecialKeys(int key, int x, int y) {
160 | float linear = 0.1f;
161 | float angular = float(m3dDegToRad(5.0f));
162 |
163 | // 控制角色向前后移动
164 | if(key == GLUT_KEY_UP)
165 | cameraFrame.MoveForward(linear);
166 |
167 | if(key == GLUT_KEY_DOWN)
168 | cameraFrame.MoveForward(-linear);
169 |
170 | // 控制角色向左右旋转
171 | if(key == GLUT_KEY_LEFT)
172 | cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
173 |
174 | if(key == GLUT_KEY_RIGHT)
175 | cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
176 | }
177 |
178 | // 窗口变换回调
179 | void ChangeSize(int width, int height) {
180 |
181 | // 防止除数为0
182 | if(height == 0)
183 | height = 1;
184 |
185 | // 设置视口
186 | glViewport(0, 0, width, height);
187 |
188 | // 设置透视投影
189 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 100.0f);
190 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
191 |
192 | // 设置变换管线的矩阵堆栈
193 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
194 | }
195 |
196 | // 程序入口
197 | int main(int argc, char* argv[]) {
198 | // 设置 Mac OS 工作目录路径
199 | gltSetWorkingDirectory(argv[0]);
200 |
201 | // GLUT初始化
202 | glutInit(&argc, argv);
203 |
204 | // 设置渲染模式
205 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
206 |
207 | // 初始化窗口大小
208 | glutInitWindowSize(600, 600);
209 |
210 | // 创建窗口并命名
211 | glutCreateWindow("OpenGL SphereWorld");
212 |
213 | // 判断驱动程序是否初始化完毕
214 | GLenum err = glewInit();
215 | if (GLEW_OK != err) {
216 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
217 | return 1;
218 | }
219 |
220 | // 窗口变化回调函数设置
221 | glutReshapeFunc(ChangeSize);
222 |
223 | // 窗口渲染回调函数设置
224 | glutDisplayFunc(RenderScene);
225 |
226 | // 特殊按键回调函数设置【Lv2 增加的代码】
227 | glutSpecialFunc(SpecialKeys);
228 |
229 | // 初始化环境
230 | SetupRC();
231 |
232 | // 主消息循环
233 | glutMainLoop();
234 |
235 | return 0;
236 | }
237 |
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/15-SphereWorld-Lv3/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/15-SphereWorld-Lv3/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/15-SphereWorld-Lv3/libgltools.dylib
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/16-SphereWorld-Lv4/16-SphereWorld-Lv4.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 16-SphereWorld-Lv4.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4/_6_SphereWorld_Lv4.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/16-SphereWorld-Lv4/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 16-SphereWorld-Lv4
4 | //
5 | // Created by luhe liu on 2018/4/18.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include
19 | #include
20 |
21 | #ifdef __APPLE__
22 | #include
23 | #else
24 | #define FREEGLUT_STATIC
25 | #include
26 | #endif
27 |
28 | // 着色器
29 | GLShaderManager shaderManager;
30 | // 视图矩阵堆栈
31 | GLMatrixStack modelViewMatrix;
32 | // 投影矩阵堆栈
33 | GLMatrixStack projectionMatrix;
34 | // 视景体
35 | GLFrustum viewFrustum;
36 | // 照相机角色【Lv2 增加的代码】
37 | GLFrame cameraFrame;
38 | // 变换管线
39 | GLGeometryTransform transformPipeline;
40 | // 花托批次
41 | GLTriangleBatch torusBatch;
42 | // 地板批次
43 | GLBatch floorBatch;
44 | // 旋转小球批次【Lv2 增加的代码】
45 | GLTriangleBatch sphereBatch;
46 | // 地板颜色,绿色
47 | GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
48 | // 花托线颜色,红色
49 | GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
50 | // 旋转小球颜色,蓝色【Lv2 增加的代码】
51 | GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
52 |
53 | // 随机小球群
54 | #define NUM_SPHERES 50
55 | GLFrame spheres[NUM_SPHERES];
56 |
57 | // 程序初始化
58 | void SetupRC() {
59 |
60 | // 设置窗口背景颜色为黑色
61 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
62 |
63 | // 初始化着色器
64 | shaderManager.InitializeStockShaders();
65 |
66 | // 开启深度测试
67 | glEnable(GL_DEPTH_TEST);
68 |
69 | // 设置多边形模式为前后面线段模式【Lv4 删除的代码】
70 | // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
71 |
72 | // 得到花托批次数据
73 | gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
74 |
75 | // 得到旋转小球批次数据【Lv2 增加的代码】
76 | gltMakeSphere(sphereBatch, 0.1f, 26, 13);
77 |
78 | // 得到方格地板批次数据
79 | floorBatch.Begin(GL_LINES, 324);
80 | for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) {
81 | floorBatch.Vertex3f(x, -0.55f, 20.0f);
82 | floorBatch.Vertex3f(x, -0.55f, -20.0f);
83 | floorBatch.Vertex3f(20.0f, -0.55f, x);
84 | floorBatch.Vertex3f(-20.0f, -0.55f, x);
85 | }
86 | floorBatch.End();
87 |
88 | // 随机小球群位置数据生成【Lv3 增加的代码】
89 | for(int i = 0; i < NUM_SPHERES; i++) {
90 | GLfloat x = ((GLfloat)((rand() % 400) - 200) * 0.1f);
91 | GLfloat z = ((GLfloat)((rand() % 400) - 200) * 0.1f);
92 | spheres[i].SetOrigin(x, 0.0f, z);
93 | }
94 | }
95 |
96 | // 窗口渲染回调
97 | void RenderScene(void) {
98 |
99 | // 获取2次渲染之间的时间间隔
100 | static CStopWatch rotTimer;
101 | float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
102 |
103 | // 清空缓冲区
104 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 |
106 | // 原来是没法移动所以压入原始矩阵,但现在角色可以移动通过角色帧获取照相机矩阵,并压入栈中【Lv2 修改的代码块】
107 | M3DMatrix44f mCamera;
108 | cameraFrame.GetCameraMatrix(mCamera);
109 | modelViewMatrix.PushMatrix(mCamera);
110 |
111 | // 得到点光源位置 【Lv4 增加的代码】
112 | M3DVector4f vLightPos = { 0.0f, 10.0f, 5.0f, 1.0f };
113 | M3DVector4f vLightEyePos;
114 | m3dTransformVector4(vLightEyePos, vLightPos, mCamera);
115 |
116 | // 绘制地板
117 | shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor);
118 | floorBatch.Draw();
119 |
120 | // 绘制随机小球群,这里的做法是瞬间切换当前角色位置并开始绘制小球,然后切换回原来角色位置【Lv3 增加的代码】
121 | for(int i = 0; i < NUM_SPHERES; i++) {
122 | // 保存当前矩阵状态,为了能切换回原来的角色位置
123 | modelViewMatrix.PushMatrix();
124 | // 调整当前角色位置为随机生成小球的位置
125 | modelViewMatrix.MultMatrix(spheres[i]);
126 | // 绘制蓝色小球
127 | // 设置着色器为点光源着色器【Lv4 调整的代码】
128 | shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(),
129 | transformPipeline.GetProjectionMatrix(), vLightEyePos, vSphereColor);
130 | // shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor);
131 | sphereBatch.Draw();
132 | // 还原矩阵状态,切换为原来角色位置
133 | modelViewMatrix.PopMatrix();
134 | }
135 |
136 | // 视图矩阵进行平移
137 | modelViewMatrix.Translate(0.0f, 0.0f, -2.5f);
138 |
139 | // 保持旋转前的视图矩阵,因为如果不在旋转前保存,会导致旋转小球也会带上花托的旋转【Lv2 增加的代码】
140 | modelViewMatrix.PushMatrix();
141 |
142 | // 继续进行旋转并进行绘制花托
143 | modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
144 | // 设置着色器为点光源着色器【Lv4 调整的代码】
145 | shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(),
146 | transformPipeline.GetProjectionMatrix(), vLightEyePos, vTorusColor);
147 | // shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor);
148 | torusBatch.Draw();
149 |
150 | // 恢复到旋转前的视图矩阵【Lv2 增加的代码】
151 | modelViewMatrix.PopMatrix();
152 |
153 | // 绘制旋转小球【Lv2 增加的代码】
154 | modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f);
155 | modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);
156 | // 设置着色器为点光源着色器【Lv4 调整的代码】
157 | shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(),
158 | transformPipeline.GetProjectionMatrix(), vLightEyePos, vSphereColor);
159 | // shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor);
160 | sphereBatch.Draw();
161 |
162 | // 出栈,恢复成原始矩阵
163 | modelViewMatrix.PopMatrix();
164 |
165 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
166 | glutSwapBuffers();
167 |
168 | // 自动触发渲染,达到动画效果
169 | glutPostRedisplay();
170 | }
171 |
172 | // 特殊按键点击回调【Lv2 增加的代码】
173 | void SpecialKeys(int key, int x, int y) {
174 | float linear = 0.1f;
175 | float angular = float(m3dDegToRad(5.0f));
176 |
177 | // 控制角色向前后移动
178 | if(key == GLUT_KEY_UP)
179 | cameraFrame.MoveForward(linear);
180 |
181 | if(key == GLUT_KEY_DOWN)
182 | cameraFrame.MoveForward(-linear);
183 |
184 | // 控制角色向左右旋转
185 | if(key == GLUT_KEY_LEFT)
186 | cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);
187 |
188 | if(key == GLUT_KEY_RIGHT)
189 | cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
190 | }
191 |
192 | // 窗口变换回调
193 | void ChangeSize(int width, int height) {
194 |
195 | // 防止除数为0
196 | if(height == 0)
197 | height = 1;
198 |
199 | // 设置视口
200 | glViewport(0, 0, width, height);
201 |
202 | // 设置透视投影
203 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 100.0f);
204 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
205 |
206 | // 设置变换管线的矩阵堆栈
207 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
208 | }
209 |
210 | // 程序入口
211 | int main(int argc, char* argv[]) {
212 | // 设置 Mac OS 工作目录路径
213 | gltSetWorkingDirectory(argv[0]);
214 |
215 | // GLUT初始化
216 | glutInit(&argc, argv);
217 |
218 | // 设置渲染模式
219 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
220 |
221 | // 初始化窗口大小
222 | glutInitWindowSize(600, 600);
223 |
224 | // 创建窗口并命名
225 | glutCreateWindow("OpenGL SphereWorld");
226 |
227 | // 判断驱动程序是否初始化完毕
228 | GLenum err = glewInit();
229 | if (GLEW_OK != err) {
230 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
231 | return 1;
232 | }
233 |
234 | // 窗口变化回调函数设置
235 | glutReshapeFunc(ChangeSize);
236 |
237 | // 窗口渲染回调函数设置
238 | glutDisplayFunc(RenderScene);
239 |
240 | // 特殊按键回调函数设置【Lv2 增加的代码】
241 | glutSpecialFunc(SpecialKeys);
242 |
243 | // 初始化环境
244 | SetupRC();
245 |
246 | // 主消息循环
247 | glutMainLoop();
248 |
249 | return 0;
250 | }
251 |
252 |
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/16-SphereWorld-Lv4/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/16-SphereWorld-Lv4/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/16-SphereWorld-Lv4/libgltools.dylib
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/17-Pyramid/17-Pyramid.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 17-Pyramid.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid/_7_Pyramid.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 17-Pyramid
4 | //
5 | // Created by luhe liu on 2018/5/20.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include // OpenGL toolkit
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 |
16 | #include
17 | #ifdef __APPLE__
18 | #include
19 | #else
20 | #define FREEGLUT_STATIC
21 | #include
22 | #endif
23 |
24 | // 着色器
25 | GLShaderManager shaderManager;
26 | // 模型视图矩阵堆栈
27 | GLMatrixStack modelViewMatrix;
28 | // 投影矩阵堆栈
29 | GLMatrixStack projectionMatrix;
30 | // 照相机角色帧
31 | GLFrame cameraFrame;
32 | // 物体角色帧
33 | GLFrame objectFrame;
34 | // 视景体
35 | GLFrustum viewFrustum;
36 | // 金字塔批次
37 | GLBatch pyramidBatch;
38 | // 纹理唯一标识
39 | GLuint textureID;
40 | // 变换管线
41 | GLGeometryTransform transformPipeline;
42 | // 光源位置
43 | GLfloat vLightPos [] = { 1.0f, 1.0f, 0.0f };
44 | // 白色
45 | GLfloat vWhite [] = { 1.0f, 1.0f, 1.0f, 1.0f };
46 |
47 | // 添加一个金字体三角形,传入3个顶点坐标以及对应的纹理坐标
48 | void MakePyramidTriangle(M3DVector3f vertex1, M3DVector2f texcoord1,
49 | M3DVector3f vertex2, M3DVector2f texcoord2,
50 | M3DVector3f vertex3, M3DVector2f texcoord3) {
51 |
52 | M3DVector3f n;
53 | m3dFindNormal(n, vertex1, vertex2, vertex3);
54 |
55 | pyramidBatch.Normal3fv(n);
56 | // 注意这里使用 MultiTexCoord2f,不要使用 MultiTexCoord2fv
57 | pyramidBatch.MultiTexCoord2f(0, texcoord1[0], texcoord1[1]);
58 | pyramidBatch.Vertex3fv(vertex1);
59 |
60 | pyramidBatch.Normal3fv(n);
61 | pyramidBatch.MultiTexCoord2f(0, texcoord2[0], texcoord2[1]);
62 | pyramidBatch.Vertex3fv(vertex2);
63 |
64 | pyramidBatch.Normal3fv(n);
65 | pyramidBatch.MultiTexCoord2f(0, texcoord3[0], texcoord3[1]);
66 | pyramidBatch.Vertex3fv(vertex3);
67 | }
68 |
69 | // 获取金字塔批次数据
70 | void MakePyramid() {
71 |
72 | // 顶点坐标和纹理坐标
73 | M3DVector3f vApex = { 0.0f, 1.0f, 0.0f };
74 | M3DVector3f vFrontLeft = { -1.0f, -1.0f, 1.0f };
75 | M3DVector3f vFrontRight = { 1.0f, -1.0f, 1.0f };
76 | M3DVector3f vBackLeft = { -1.0f, -1.0f, -1.0f };
77 | M3DVector3f vBackRight = { 1.0f, -1.0f, -1.0f };
78 | M3DVector2f vTex00 = { 0.0f, 0.0f };
79 | M3DVector2f vTex01 = { 0.0f, 1.0f };
80 | M3DVector2f vTex10 = { 1.0f, 0.0f };
81 | M3DVector2f vTex11 = { 1.0f, 1.0f };
82 | M3DVector2f vApexTex = { 0.5f, 1.0f };
83 |
84 | // 金字塔
85 | pyramidBatch.Begin(GL_TRIANGLES, 18, 1);
86 | MakePyramidTriangle(vBackLeft, vTex00, vBackRight, vTex10, vFrontRight, vTex11);
87 | MakePyramidTriangle(vBackLeft, vTex00, vFrontRight, vTex11, vFrontLeft, vTex01);
88 | MakePyramidTriangle(vApex, vApexTex, vFrontLeft, vTex00, vFrontRight, vTex10);
89 | MakePyramidTriangle(vApex, vApexTex, vBackLeft, vTex10, vFrontLeft, vTex00);
90 | MakePyramidTriangle(vApex, vApexTex, vFrontRight, vTex10, vBackRight, vTex00);
91 | MakePyramidTriangle(vApex, vApexTex, vBackRight, vTex00, vBackLeft, vTex10);
92 | pyramidBatch.End();
93 | }
94 |
95 | // 从文件中读取纹理数据
96 | bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode) {
97 |
98 | GLbyte *pBits;
99 | GLint nWidth, nHeight, nComponents;
100 | GLenum eFormat;
101 |
102 | // 根据文件名从磁盘中读取纹理数据
103 | pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);
104 | if(pBits == NULL)
105 | return false;
106 |
107 | // 设置纹理数据的填充方式
108 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
109 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
110 |
111 | // 设置纹理缩小和放大时使用的过滤器
112 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
113 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
114 |
115 | // 精密打包纹理数据(主要是字节对齐)
116 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117 |
118 | // 加载纹理数据到二维纹理层
119 | glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBits);
120 |
121 | // 释放二进制缓存数据
122 | free(pBits);
123 |
124 | // 判断下缩小过滤器是否 Mip 贴图过滤器,是的话,启用 Mip 贴图
125 | if(minFilter == GL_LINEAR_MIPMAP_LINEAR ||
126 | minFilter == GL_LINEAR_MIPMAP_NEAREST ||
127 | minFilter == GL_NEAREST_MIPMAP_LINEAR ||
128 | minFilter == GL_NEAREST_MIPMAP_NEAREST)
129 | glGenerateMipmap(GL_TEXTURE_2D);
130 |
131 | return true;
132 | }
133 |
134 | // 程序初始化
135 | void SetupRC() {
136 | // 设置背景颜色为灰色
137 | glClearColor(0.7f, 0.7f, 0.7f, 1.0f );
138 |
139 | // 初始化着色器
140 | shaderManager.InitializeStockShaders();
141 |
142 | // 开启深度测试
143 | glEnable(GL_DEPTH_TEST);
144 |
145 | // 照相机移动位置,保证物体完全显示
146 | cameraFrame.MoveForward(-7.0f);
147 |
148 | // 获取纹理标识
149 | glGenTextures(1, &textureID);
150 |
151 | // 绑定当前二维纹理的纹理为 textureID 标识代表的纹理
152 | glBindTexture(GL_TEXTURE_2D, textureID);
153 |
154 | // 从 TGB 文件中加载纹理数据(自定义)
155 | // 参数1:文件名
156 | // 参数2:纹理缩小时的过滤器
157 | // 参数3:纹理放大时的过滤器
158 | // 参数4:纹理填充模式
159 | LoadTGATexture("stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
160 |
161 | // 获取金字塔批次数据(自定义)
162 | MakePyramid();
163 | }
164 |
165 | // 资源销毁
166 | void ShutdownRC(void) {
167 | // 销毁纹理资源
168 | glDeleteTextures(1, &textureID);
169 | }
170 |
171 |
172 | // 窗口渲染回调方法
173 | void RenderScene(void) {
174 |
175 | // 清空缓冲区
176 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
177 |
178 | // 压栈,保留原始矩阵
179 | modelViewMatrix.PushMatrix();
180 |
181 | // 获取照相机矩阵,并合并到模型视图矩阵
182 | M3DMatrix44f mCamera;
183 | cameraFrame.GetCameraMatrix(mCamera);
184 | modelViewMatrix.MultMatrix(mCamera);
185 |
186 | // 获取物体几何变换矩阵,并合并到模型视图矩阵
187 | M3DMatrix44f mObjectFrame;
188 | objectFrame.GetMatrix(mObjectFrame);
189 | modelViewMatrix.MultMatrix(mObjectFrame);
190 |
191 | // 绑定当前二维纹理的纹理为 textureID 标识代表的纹理
192 | glBindTexture(GL_TEXTURE_2D, textureID);
193 |
194 | // 点光源着色器绘制金字塔
195 | shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
196 | transformPipeline.GetModelViewMatrix(),
197 | transformPipeline.GetProjectionMatrix(),
198 | vLightPos, vWhite, 0);
199 | pyramidBatch.Draw();
200 |
201 | // 还原矩阵
202 | modelViewMatrix.PopMatrix();
203 |
204 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
205 | glutSwapBuffers();
206 | }
207 |
208 | // 特殊按键点击回调
209 | void SpecialKeys(int key, int x, int y) {
210 | // 以下是物体的上下左右旋转
211 | if(key == GLUT_KEY_UP)
212 | objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
213 |
214 | if(key == GLUT_KEY_DOWN)
215 | objectFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);
216 |
217 | if(key == GLUT_KEY_LEFT)
218 | objectFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
219 |
220 | if(key == GLUT_KEY_RIGHT)
221 | objectFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
222 |
223 | // 触发渲染
224 | glutPostRedisplay();
225 | }
226 |
227 |
228 | // 窗口变换回调
229 | void ChangeSize(int width, int height) {
230 |
231 | // 防止除数为0
232 | if(height == 0)
233 | height = 1;
234 |
235 | // 设置视口
236 | glViewport(0, 0, width, height);
237 |
238 | // 设置透视投影
239 | viewFrustum.SetPerspective(35.0f, float(width)/float(height), 1.0f, 100.0f);
240 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
241 |
242 | // 设置变换管线的矩阵堆栈
243 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
244 | }
245 |
246 | // 程序入口
247 | int main(int argc, char* argv[]) {
248 | // 设置 Mac OS 工作目录路径
249 | gltSetWorkingDirectory(argv[0]);
250 |
251 | // GLUT初始化
252 | glutInit(&argc, argv);
253 |
254 | // 设置渲染模式
255 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
256 |
257 | // 初始化窗口大小
258 | glutInitWindowSize(600, 600);
259 |
260 | // 创建窗口并命名
261 | glutCreateWindow("Pyramid");
262 |
263 | // 判断驱动程序是否初始化完毕
264 | GLenum err = glewInit();
265 | if (GLEW_OK != err) {
266 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
267 | return 1;
268 | }
269 |
270 | // 窗口变化回调函数设置
271 | glutReshapeFunc(ChangeSize);
272 |
273 | // 窗口渲染回调函数设置
274 | glutDisplayFunc(RenderScene);
275 |
276 | // 特殊按键回调函数设置【Lv2 增加的代码】
277 | glutSpecialFunc(SpecialKeys);
278 |
279 | // 初始化环境
280 | SetupRC();
281 |
282 | // 主消息循环
283 | glutMainLoop();
284 |
285 | // 资源销毁
286 | ShutdownRC();
287 |
288 | return 0;
289 | }
290 |
291 |
--------------------------------------------------------------------------------
/17-Pyramid/17-Pyramid/stone.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/17-Pyramid/17-Pyramid/stone.tga
--------------------------------------------------------------------------------
/17-Pyramid/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/17-Pyramid/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/17-Pyramid/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/17-Pyramid/libgltools.dylib
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/18-Tunnel/18-Tunnel.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 18-Tunnel.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/_8_Tunnel.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/brick.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/18-Tunnel/18-Tunnel/brick.tga
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/ceiling.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/18-Tunnel/18-Tunnel/ceiling.tga
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/floor.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/18-Tunnel/18-Tunnel/floor.tga
--------------------------------------------------------------------------------
/18-Tunnel/18-Tunnel/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 18-Tunnel
4 | //
5 | // Created by luhe liu on 2018/6/3.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 |
17 | #ifdef __APPLE__
18 | #include
19 | #else
20 | #define FREEGLUT_STATIC
21 | #include
22 | #endif
23 |
24 | // 着色器管理器
25 | GLShaderManager shaderManager;
26 | // 视景体
27 | GLFrustum viewFrustum;
28 | // 变换管线,以及它管理的2个矩阵堆栈
29 | GLGeometryTransform transformPipeline;
30 | GLMatrixStack modelViewMatrix;
31 | GLMatrixStack projectionMatrix;
32 |
33 | // 4个数据批次
34 | GLBatch floorBatch;
35 | GLBatch ceilingBatch;
36 | GLBatch leftWallBatch;
37 | GLBatch rightWallBatch;
38 |
39 | // 用于控制观察者向前和向后
40 | GLfloat viewZ = -65.0f;
41 |
42 | // 宏定义,地板、天花板、墙分别对应的纹理标识数组索引
43 | #define TEXTURE_BRICK 0
44 | #define TEXTURE_FLOOR 1
45 | #define TEXTURE_CEILING 2
46 | #define TEXTURE_COUNT 3
47 | // 纹理标识数组
48 | GLuint textures[TEXTURE_COUNT];
49 | // 纹理文件名数组
50 | const char *szTextureFiles[TEXTURE_COUNT] = { "brick.tga", "floor.tga", "ceiling.tga" };
51 |
52 | // 点击右键菜单选项回调
53 | void ProcessMenu(int value) {
54 | // 循环遍历所有纹理数据
55 | for(GLint iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++) {
56 | // 绑定当前纹理为该索引对应的纹理数据
57 | glBindTexture(GL_TEXTURE_2D, textures[iLoop]);
58 |
59 | // 设置该纹理数据的缩小过滤器
60 | switch(value) {
61 | case 0: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); break;
62 | case 1: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); break;
63 | case 2: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); break;
64 | case 3: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); break;
65 | case 4: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); break;
66 | case 5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); break;
67 | }
68 | }
69 | // 触发渲染
70 | glutPostRedisplay();
71 | }
72 |
73 | // 加载所有的纹理数据
74 | void LoadAllTextureData() {
75 | GLbyte *pBytes;
76 | GLint iWidth, iHeight, iComponents;
77 | GLenum eFormat;
78 | GLint iLoop;
79 | // 申请要加载的纹理数据数量
80 | glGenTextures(TEXTURE_COUNT, textures);
81 | // 循环遍历所有纹理文件
82 | for(iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++) {
83 | // 绑定当前纹理为该索引对应的纹理数据
84 | glBindTexture(GL_TEXTURE_2D, textures[iLoop]);
85 |
86 | // 从 TGA 文件中读取纹理数据
87 | pBytes = gltReadTGABits(szTextureFiles[iLoop], &iWidth, &iHeight, &iComponents, &eFormat);
88 |
89 | // 设置纹理数据缩小、放大过滤器
90 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
91 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
92 |
93 | // 设置纹理数据(s, t)环绕模式
94 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
95 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
96 |
97 | // 加载纹理数据到2维纹理缓冲区
98 | glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
99 |
100 | // 2维纹理缓冲区开启 Mip 贴图
101 | glGenerateMipmap(GL_TEXTURE_2D);
102 |
103 | // 释放纹理数据
104 | free(pBytes);
105 | }
106 | }
107 |
108 | // 初始化地板数据
109 | void SetupFloorBatch() {
110 | GLfloat z;
111 | floorBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
112 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
113 | floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
114 | floorBatch.Vertex3f(-10.0f, -10.0f, z);
115 |
116 | floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
117 | floorBatch.Vertex3f(10.0f, -10.0f, z);
118 |
119 | floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
120 | floorBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f);
121 |
122 | floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
123 | floorBatch.Vertex3f(10.0f, -10.0f, z - 10.0f);
124 | }
125 | floorBatch.End();
126 | }
127 |
128 | // 初始化天花板数据
129 | void SetupCeilingBatch() {
130 | GLfloat z;
131 | ceilingBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
132 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
133 | ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
134 | ceilingBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f);
135 |
136 | ceilingBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
137 | ceilingBatch.Vertex3f(10.0f, 10.0f, z - 10.0f);
138 |
139 | ceilingBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
140 | ceilingBatch.Vertex3f(-10.0f, 10.0f, z);
141 |
142 | ceilingBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
143 | ceilingBatch.Vertex3f(10.0f, 10.0f, z);
144 | }
145 | ceilingBatch.End();
146 | }
147 |
148 | // 初始化左右墙数据
149 | void SetupWallBatch() {
150 | // 左墙
151 | GLfloat z;
152 | leftWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
153 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
154 | leftWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
155 | leftWallBatch.Vertex3f(-10.0f, -10.0f, z);
156 |
157 | leftWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
158 | leftWallBatch.Vertex3f(-10.0f, 10.0f, z);
159 |
160 | leftWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
161 | leftWallBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f);
162 |
163 | leftWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
164 | leftWallBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f);
165 | }
166 | leftWallBatch.End();
167 |
168 | // 右墙
169 | rightWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
170 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
171 | rightWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
172 | rightWallBatch.Vertex3f(10.0f, -10.0f, z);
173 |
174 | rightWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
175 | rightWallBatch.Vertex3f(10.0f, 10.0f, z);
176 |
177 | rightWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
178 | rightWallBatch.Vertex3f(10.0f, -10.0f, z - 10.0f);
179 |
180 | rightWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
181 | rightWallBatch.Vertex3f(10.0f, 10.0f, z - 10.0f);
182 | }
183 | rightWallBatch.End();
184 | }
185 |
186 | // 程序初始化渲染环境
187 | void SetupRC() {
188 | // 设置窗口背景为黑色
189 | glClearColor(0.0f, 0.0f, 0.0f,1.0f);
190 |
191 | // 初始化着色器
192 | shaderManager.InitializeStockShaders();
193 |
194 | // 加载所有纹理数据
195 | LoadAllTextureData();
196 |
197 | // 初始化地板
198 | SetupFloorBatch();
199 | // 初始化天花板
200 | SetupCeilingBatch();
201 | // 初始化墙
202 | SetupWallBatch();
203 | }
204 |
205 | // 程序释放资源
206 | void ShutdownRC(void) {
207 | // 删除所有的纹理数据
208 | glDeleteTextures(TEXTURE_COUNT, textures);
209 | }
210 |
211 | // 特殊按键点击回调
212 | void SpecialKeys(int key, int x, int y) {
213 | // 上按键前进
214 | if(key == GLUT_KEY_UP)
215 | viewZ += 0.5f;
216 | // 下按键后退
217 | if(key == GLUT_KEY_DOWN)
218 | viewZ -= 0.5f;
219 |
220 | // 触发渲染
221 | glutPostRedisplay();
222 | }
223 |
224 | // 窗口渲染回调方法
225 | void RenderScene(void) {
226 | // 清除缓冲区内容
227 | glClear(GL_COLOR_BUFFER_BIT);
228 |
229 | // 压入单位矩阵
230 | modelViewMatrix.PushMatrix();
231 | // 模型视图向Z轴平移
232 | modelViewMatrix.Translate(0.0f, 0.0f, viewZ);
233 |
234 | // 采用纹理替换着色器进行绘制
235 | shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), 0);
236 |
237 | // 绑定当前纹理为地板纹理,进行地板绘制
238 | glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_FLOOR]);
239 | floorBatch.Draw();
240 |
241 | // 绑定当前纹理为天花板,进行天花板绘制
242 | glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_CEILING]);
243 | ceilingBatch.Draw();
244 |
245 | // 绑定当前纹理为左右墙纹理,进行左右墙绘制
246 | glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_BRICK]);
247 | leftWallBatch.Draw();
248 | rightWallBatch.Draw();
249 |
250 | // 出栈,恢复原来环境
251 | modelViewMatrix.PopMatrix();
252 |
253 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
254 | glutSwapBuffers();
255 | }
256 |
257 | // 窗口大小变化回调方法
258 | void ChangeSize(int width, int height) {
259 | // 防止除数为0
260 | if(height == 0)
261 | height = 1;
262 |
263 | // 设置视口
264 | glViewport(0, 0, width, height);
265 | // 计算窗口宽高比
266 | GLfloat fAspect = (GLfloat)width / (GLfloat)height;
267 | // 设置投影矩阵
268 | viewFrustum.SetPerspective(80.0f, fAspect, 1.0, 120.0);
269 | // 投影矩阵堆栈保持投影矩阵
270 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
271 | // 设置变换管线的投影矩阵堆栈和模型视图矩阵堆栈
272 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
273 |
274 | }
275 |
276 | // 程序入口
277 | int main(int argc, char *argv[]) {
278 | // 设置 Mac OS 工作目录路径
279 | gltSetWorkingDirectory(argv[0]);
280 |
281 | // GLUT 初始化
282 | glutInit(&argc, argv);
283 |
284 | // 设置 GLUT 渲染模式
285 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
286 |
287 | // 创建窗口
288 | glutInitWindowSize(800, 600);
289 | glutCreateWindow("Tunnel");
290 |
291 | // 绑定事件回调
292 | glutReshapeFunc(ChangeSize);
293 | glutSpecialFunc(SpecialKeys);
294 | glutDisplayFunc(RenderScene);
295 |
296 | // 创建右键菜单
297 | glutCreateMenu(ProcessMenu);
298 | // 添加右键菜单选项
299 | glutAddMenuEntry("GL_NEAREST",0);
300 | glutAddMenuEntry("GL_LINEAR",1);
301 | glutAddMenuEntry("GL_NEAREST_MIPMAP_NEAREST",2);
302 | glutAddMenuEntry("GL_NEAREST_MIPMAP_LINEAR", 3);
303 | glutAddMenuEntry("GL_LINEAR_MIPMAP_NEAREST", 4);
304 | glutAddMenuEntry("GL_LINEAR_MIPMAP_LINEAR", 5);
305 | // 设置右键菜单弹出
306 | glutAttachMenu(GLUT_RIGHT_BUTTON);
307 |
308 | // GLEW 驱动程序初始化
309 | GLenum err = glewInit();
310 | if (GLEW_OK != err) {
311 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
312 | return 1;
313 | }
314 |
315 | // 程序初始化渲染环境
316 | SetupRC();
317 |
318 | // 运行循环
319 | glutMainLoop();
320 |
321 | // 程序释放资源
322 | ShutdownRC();
323 |
324 | return 0;
325 | }
326 |
--------------------------------------------------------------------------------
/18-Tunnel/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/18-Tunnel/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/18-Tunnel/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/18-Tunnel/libgltools.dylib
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/19-Anisotropic/19-Anisotropic.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 19-Anisotropic.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/_9_Anisotropic.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/brick.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/19-Anisotropic/19-Anisotropic/brick.tga
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/ceiling.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/19-Anisotropic/19-Anisotropic/ceiling.tga
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/floor.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/19-Anisotropic/19-Anisotropic/floor.tga
--------------------------------------------------------------------------------
/19-Anisotropic/19-Anisotropic/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // 19-Anisotropic
4 | //
5 | // Created by luhe liu on 2018/6/3.
6 | // Copyright © 2018年 luhe liu. All rights reserved.
7 | //
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 |
17 | #ifdef __APPLE__
18 | #include
19 | #else
20 | #define FREEGLUT_STATIC
21 | #include
22 | #endif
23 |
24 | // 着色器管理器
25 | GLShaderManager shaderManager;
26 | // 视景体
27 | GLFrustum viewFrustum;
28 | // 变换管线,以及它管理的2个矩阵堆栈
29 | GLGeometryTransform transformPipeline;
30 | GLMatrixStack modelViewMatrix;
31 | GLMatrixStack projectionMatrix;
32 |
33 | // 4个数据批次
34 | GLBatch floorBatch;
35 | GLBatch ceilingBatch;
36 | GLBatch leftWallBatch;
37 | GLBatch rightWallBatch;
38 |
39 | // 用于控制观察者向前和向后
40 | GLfloat viewZ = -65.0f;
41 |
42 | // 宏定义,地板、天花板、墙分别对应的纹理标识数组索引
43 | #define TEXTURE_BRICK 0
44 | #define TEXTURE_FLOOR 1
45 | #define TEXTURE_CEILING 2
46 | #define TEXTURE_COUNT 3
47 | // 纹理标识数组
48 | GLuint textures[TEXTURE_COUNT];
49 | // 纹理文件名数组
50 | const char *szTextureFiles[TEXTURE_COUNT] = { "brick.tga", "floor.tga", "ceiling.tga" };
51 |
52 | // 点击右键菜单选项回调
53 | void ProcessMenu(int value) {
54 | // 循环遍历所有纹理数据
55 | for(GLint iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++) {
56 | // 绑定当前纹理为该索引对应的纹理数据
57 | glBindTexture(GL_TEXTURE_2D, textures[iLoop]);
58 |
59 | // 设置该纹理数据的缩小过滤器
60 | switch(value) {
61 | case 0: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); break;
62 | case 1: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); break;
63 | case 2: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); break;
64 | case 3: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); break;
65 | case 4: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); break;
66 | case 5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); break;
67 | case 6:
68 | // 设置各向异性
69 | GLfloat fLargest;
70 | glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest);
71 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest);
72 | break;
73 | case 7:
74 | // 设置各向同性
75 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
76 | break;
77 | }
78 | }
79 | // 触发渲染
80 | glutPostRedisplay();
81 | }
82 |
83 | // 加载所有的纹理数据
84 | void LoadAllTextureData() {
85 | GLbyte *pBytes;
86 | GLint iWidth, iHeight, iComponents;
87 | GLenum eFormat;
88 | GLint iLoop;
89 | // 申请要加载的纹理数据数量
90 | glGenTextures(TEXTURE_COUNT, textures);
91 | // 循环遍历所有纹理文件
92 | for(iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++) {
93 | // 绑定当前纹理为该索引对应的纹理数据
94 | glBindTexture(GL_TEXTURE_2D, textures[iLoop]);
95 |
96 | // 从 TGA 文件中读取纹理数据
97 | pBytes = gltReadTGABits(szTextureFiles[iLoop], &iWidth, &iHeight, &iComponents, &eFormat);
98 |
99 | // 设置纹理数据缩小、放大过滤器
100 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
101 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
102 |
103 | // 设置纹理数据(s, t)环绕模式
104 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
105 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
106 |
107 | // 加载纹理数据到2维纹理缓冲区
108 | glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
109 |
110 | // 2维纹理缓冲区开启 Mip 贴图
111 | glGenerateMipmap(GL_TEXTURE_2D);
112 |
113 | // 释放纹理数据
114 | free(pBytes);
115 | }
116 | }
117 |
118 | // 初始化地板数据
119 | void SetupFloorBatch() {
120 | GLfloat z;
121 | floorBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
122 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
123 | floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
124 | floorBatch.Vertex3f(-10.0f, -10.0f, z);
125 |
126 | floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
127 | floorBatch.Vertex3f(10.0f, -10.0f, z);
128 |
129 | floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
130 | floorBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f);
131 |
132 | floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
133 | floorBatch.Vertex3f(10.0f, -10.0f, z - 10.0f);
134 | }
135 | floorBatch.End();
136 | }
137 |
138 | // 初始化天花板数据
139 | void SetupCeilingBatch() {
140 | GLfloat z;
141 | ceilingBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
142 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
143 | ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
144 | ceilingBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f);
145 |
146 | ceilingBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
147 | ceilingBatch.Vertex3f(10.0f, 10.0f, z - 10.0f);
148 |
149 | ceilingBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
150 | ceilingBatch.Vertex3f(-10.0f, 10.0f, z);
151 |
152 | ceilingBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
153 | ceilingBatch.Vertex3f(10.0f, 10.0f, z);
154 | }
155 | ceilingBatch.End();
156 | }
157 |
158 | // 初始化左右墙数据
159 | void SetupWallBatch() {
160 | // 左墙
161 | GLfloat z;
162 | leftWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
163 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
164 | leftWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
165 | leftWallBatch.Vertex3f(-10.0f, -10.0f, z);
166 |
167 | leftWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
168 | leftWallBatch.Vertex3f(-10.0f, 10.0f, z);
169 |
170 | leftWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
171 | leftWallBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f);
172 |
173 | leftWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
174 | leftWallBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f);
175 | }
176 | leftWallBatch.End();
177 |
178 | // 右墙
179 | rightWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);
180 | for(z = 60.0f; z >= 0.0f; z -=10.0f) {
181 | rightWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
182 | rightWallBatch.Vertex3f(10.0f, -10.0f, z);
183 |
184 | rightWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
185 | rightWallBatch.Vertex3f(10.0f, 10.0f, z);
186 |
187 | rightWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
188 | rightWallBatch.Vertex3f(10.0f, -10.0f, z - 10.0f);
189 |
190 | rightWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
191 | rightWallBatch.Vertex3f(10.0f, 10.0f, z - 10.0f);
192 | }
193 | rightWallBatch.End();
194 | }
195 |
196 | // 程序初始化渲染环境
197 | void SetupRC() {
198 | // 设置窗口背景为黑色
199 | glClearColor(0.0f, 0.0f, 0.0f,1.0f);
200 |
201 | // 初始化着色器
202 | shaderManager.InitializeStockShaders();
203 |
204 | // 加载所有纹理数据
205 | LoadAllTextureData();
206 |
207 | // 初始化地板
208 | SetupFloorBatch();
209 | // 初始化天花板
210 | SetupCeilingBatch();
211 | // 初始化墙
212 | SetupWallBatch();
213 | }
214 |
215 | // 程序释放资源
216 | void ShutdownRC(void) {
217 | // 删除所有的纹理数据
218 | glDeleteTextures(TEXTURE_COUNT, textures);
219 | }
220 |
221 | // 特殊按键点击回调
222 | void SpecialKeys(int key, int x, int y) {
223 | // 上按键前进
224 | if(key == GLUT_KEY_UP)
225 | viewZ += 0.5f;
226 | // 下按键后退
227 | if(key == GLUT_KEY_DOWN)
228 | viewZ -= 0.5f;
229 |
230 | // 触发渲染
231 | glutPostRedisplay();
232 | }
233 |
234 | // 窗口渲染回调方法
235 | void RenderScene(void) {
236 | // 清除缓冲区内容
237 | glClear(GL_COLOR_BUFFER_BIT);
238 |
239 | // 压入单位矩阵
240 | modelViewMatrix.PushMatrix();
241 | // 模型视图向Z轴平移
242 | modelViewMatrix.Translate(0.0f, 0.0f, viewZ);
243 |
244 | // 采用纹理替换着色器进行绘制
245 | shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), 0);
246 |
247 | // 绑定当前纹理为地板纹理,进行地板绘制
248 | glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_FLOOR]);
249 | floorBatch.Draw();
250 |
251 | // 绑定当前纹理为天花板,进行天花板绘制
252 | glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_CEILING]);
253 | ceilingBatch.Draw();
254 |
255 | // 绑定当前纹理为左右墙纹理,进行左右墙绘制
256 | glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_BRICK]);
257 | leftWallBatch.Draw();
258 | rightWallBatch.Draw();
259 |
260 | // 出栈,恢复原来环境
261 | modelViewMatrix.PopMatrix();
262 |
263 | // 因为是双缓冲区模式,后台缓冲区替换到前台缓存区进行显示
264 | glutSwapBuffers();
265 | }
266 |
267 | // 窗口大小变化回调方法
268 | void ChangeSize(int width, int height) {
269 | // 防止除数为0
270 | if(height == 0)
271 | height = 1;
272 |
273 | // 设置视口
274 | glViewport(0, 0, width, height);
275 | // 计算窗口宽高比
276 | GLfloat fAspect = (GLfloat)width / (GLfloat)height;
277 | // 设置投影矩阵
278 | viewFrustum.SetPerspective(80.0f, fAspect, 1.0, 120.0);
279 | // 投影矩阵堆栈保持投影矩阵
280 | projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
281 | // 设置变换管线的投影矩阵堆栈和模型视图矩阵堆栈
282 | transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
283 |
284 | }
285 |
286 | // 程序入口
287 | int main(int argc, char *argv[]) {
288 | // 设置 Mac OS 工作目录路径
289 | gltSetWorkingDirectory(argv[0]);
290 |
291 | // GLUT 初始化
292 | glutInit(&argc, argv);
293 |
294 | // 设置 GLUT 渲染模式
295 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
296 |
297 | // 创建窗口
298 | glutInitWindowSize(800, 600);
299 | glutCreateWindow("Tunnel");
300 |
301 | // 绑定事件回调
302 | glutReshapeFunc(ChangeSize);
303 | glutSpecialFunc(SpecialKeys);
304 | glutDisplayFunc(RenderScene);
305 |
306 | // 创建右键菜单
307 | glutCreateMenu(ProcessMenu);
308 | // 添加右键菜单选项
309 | glutAddMenuEntry("GL_NEAREST",0);
310 | glutAddMenuEntry("GL_LINEAR",1);
311 | glutAddMenuEntry("GL_NEAREST_MIPMAP_NEAREST",2);
312 | glutAddMenuEntry("GL_NEAREST_MIPMAP_LINEAR", 3);
313 | glutAddMenuEntry("GL_LINEAR_MIPMAP_NEAREST", 4);
314 | glutAddMenuEntry("GL_LINEAR_MIPMAP_LINEAR", 5);
315 | glutAddMenuEntry("Anisotropic Filter", 6);
316 | glutAddMenuEntry("Anisotropic Off", 7);
317 | // 设置右键菜单弹出
318 | glutAttachMenu(GLUT_RIGHT_BUTTON);
319 |
320 | // GLEW 驱动程序初始化
321 | GLenum err = glewInit();
322 | if (GLEW_OK != err) {
323 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
324 | return 1;
325 | }
326 |
327 | // 程序初始化渲染环境
328 | SetupRC();
329 |
330 | // 运行循环
331 | glutMainLoop();
332 |
333 | // 程序释放资源
334 | ShutdownRC();
335 |
336 | return 0;
337 | }
338 |
--------------------------------------------------------------------------------
/19-Anisotropic/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/19-Anisotropic/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/19-Anisotropic/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/19-Anisotropic/libgltools.dylib
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/20-SphereWorld-Lv5/20-SphereWorld-Lv5.xcodeproj/project.xcworkspace/xcuserdata/luheliu.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5.xcodeproj/xcuserdata/luheliu.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 20-SphereWorld-Lv5.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
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 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018年 luhe liu. All rights reserved.
27 | NSMainNibFile
28 | MainMenu
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5/Marble.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/20-SphereWorld-Lv5/20-SphereWorld-Lv5/Marble.tga
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5/Marslike.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/20-SphereWorld-Lv5/20-SphereWorld-Lv5/Marslike.tga
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5/MoonLike.tga:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/20-SphereWorld-Lv5/20-SphereWorld-Lv5/MoonLike.tga
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/20-SphereWorld-Lv5/_0_SphereWorld_Lv5.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/libGLEW.2.1.0.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/20-SphereWorld-Lv5/libGLEW.2.1.0.dylib
--------------------------------------------------------------------------------
/20-SphereWorld-Lv5/libgltools.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liutingluhe/OpenGLDemo/31369d4409e5c05415710ae6110993c38a86c927/20-SphereWorld-Lv5/libgltools.dylib
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # OpenGLDemo
2 | OpenGL learn demo
3 |
4 | * 01-环境配置
5 | * 02-移动
6 | * 03-图元
7 | * 04-花托(剔除、深度测试、多边形模式)
8 | * 05-裁剪
9 | * 06-颜色混合
10 | * 07-抗锯齿
11 | * 08-移动(矩阵变换)
12 | * 09-更多对象(球体、花托、圆柱圆锥、圆盘)
13 | * 10-正投影矩阵
14 | * 11-透视投影矩阵
15 | * 12-模型视图投影矩阵
16 | * 13-球体世界 Lv1(背景、旋转的花托)
17 | * 14-球体世界 Lv2(加入角色移动和蓝色小球旋转)
18 | * 15-球体世界 Lv3(加入随机小球分布场景)
19 | * 16-球体世界 Lv4(加入点光源着色)
20 | * 17-金字塔(加入纹理)
21 | * 18-通道(纹理过滤器、Mip 贴图)
22 | * 19-通道(增加 **各向异性** 和 **各向同性** 右键选项)
23 | * 20-球体世界 Lv5(使用纹理、增加地板镜像效果)
24 |
--------------------------------------------------------------------------------