├── .gitignore
├── .gitmodules
├── README.md
├── app.qrc
├── assets.pri
├── assets.qrc
├── deploy_uwp.bat
├── doc
├── Basic-Lighting.md
├── Camera.md
├── Colors.md
├── Coordinate-Systems.md
├── FreeTalks.md
├── Hello-Triangle.md
├── Hello-Window.md
├── Light-casters.md
├── Lighting-maps.md
├── Materials.md
├── Multiple-lights.md
├── Shaders.md
├── Texture.md
├── Transformations.md
└── img
│ ├── camera_keyboard.0.png
│ ├── camera_zoom.0.png
│ ├── coordinate_systems_with_depth.0.png
│ ├── coordinate_systems_with_multiple_objects.0.png
│ ├── hellotriangle.0.png
│ ├── hellotriangle.1.png
│ ├── hellotriangle.2.png
│ ├── hellotriangle2.0.png
│ ├── hellowindow.0.png
│ ├── hellowindow.1.png
│ ├── hellowindow2.0.png
│ ├── materials.0.png
│ ├── sc-qt3d-desktop.gif
│ ├── shaders-interpolated.0.png
│ ├── shaders-uniform.0.png
│ ├── ss-qml.png
│ ├── ss-qt3d-android.png
│ ├── ss-qt3d-desktop.png
│ ├── textures.0.png
│ ├── transformations-exercise2.0.png
│ └── transformations.0.png
├── learnopengl-qt3d.pro
├── main.cpp
├── qml
├── Components
│ ├── AssetCheck.qml
│ ├── AutoShaderProgram.qml
│ ├── AutoTechnique.qml
│ ├── Examples.qml
│ ├── FpsLine.qml
│ ├── FrameSwap.qml
│ ├── NonUniformScaledCuboidMesh0.qml
│ ├── OurCamera.qml
│ ├── OurCameraController.qml
│ ├── RenderInputSettings0.qml
│ ├── RenderSettings0.qml
│ ├── RenderSettings1.qml
│ ├── RenderSettings2.qml
│ ├── Resources.qml
│ ├── Scene0.qml
│ ├── Scene1.qml
│ ├── Scene2.qml
│ ├── Scene3.qml
│ ├── TextureCube0.qml
│ ├── TextureCubeMesh0.qml
│ ├── TextureRectanglePlane0.qml
│ ├── Time.qml
│ ├── TrianglePlane0.qml
│ ├── geo.js
│ ├── qmldir
│ └── utils.js
├── app-textfps.qml
├── app.qml
├── basic_lighting-exercise1.qml
├── basic_lighting-exercise2.qml
├── basic_lighting-exercise3.qml
├── basic_lighting_diffuse.qml
├── basic_lighting_specular.qml
├── camera-exercise1.qml
├── camera-exercise2.qml
├── camera_circle.qml
├── camera_keyboard.qml
├── camera_keyboard_dt.qml
├── camera_quaternion.qml
├── camera_zoom.qml
├── colors_scene.qml
├── coordinate_systems-exercise1.qml
├── coordinate_systems-exercise2.qml
├── coordinate_systems-exercise3.qml
├── coordinate_systems.qml
├── coordinate_systems_multiple_objects.qml
├── coordinate_systems_with_depth.qml
├── deploy.sh
├── depth_testing.qml
├── hello-triangle-exercise1.qml
├── hello-triangle-exercise2.qml
├── hello-triangle-exercise3.qml
├── hellotriangle.qml
├── hellotriangle2.qml
├── hellowindow.qml
├── hellowindow2.qml
├── learnopengl-qml.qmlproject
├── light_casters_directional.qml
├── light_casters_point.qml
├── light_casters_spotlight_hard.qml
├── light_casters_spotlight_soft.qml
├── lighting_maps-exercise1.qml
├── lighting_maps-exercise2.qml
├── lighting_maps-exercise3.qml
├── lighting_maps-exercise4.qml
├── lighting_maps_diffuse.qml
├── lighting_maps_specular.qml
├── main.qml
├── materials.qml
├── model.qml
├── multiple_lights-exercise1.qml
├── multiple_lights-exercise2.qml
├── multiple_lights.qml
├── shaders-exercise1.qml
├── shaders-exercise2.qml
├── shaders-exercise3.qml
├── shaders-interpolated.qml
├── shaders-uniform.qml
├── stencil_testing.qml
├── tests
│ ├── run_tests.sh
│ ├── tst_app.qml
│ ├── tst_extras.qml
│ └── utils.js
├── textures-exercise1.qml
├── textures-exercise2.qml
├── textures-exercise3.qml
├── textures-exercise4.qml
├── textures.qml
├── textures2.qml
├── textures_combined.qml
├── transformations-exercise1.qml
├── transformations-exercise2.qml
└── transformations.qml
└── shared
├── assets
└── texture
│ ├── awesomeface.png
│ ├── blending_transparent_window.png
│ ├── container.jpg
│ ├── container2.png
│ ├── container2_specular.png
│ ├── grass.png
│ ├── lighting_maps_specular_color.png
│ └── matrix.jpg
└── shaders
├── es20
├── basic_lighting-exercise2.frag
├── basic_lighting-exercise2.vert
├── basic_lighting-exercise3.frag
├── basic_lighting-exercise3.vert
├── basic_lighting.frag
├── basic_lighting.vert
├── basic_lighting_diffuse.frag
├── basic_lighting_gouraud.vert
├── checkerboard.frag
├── coordinate_systems.vert
├── coordinate_systems_qt3d_transform.vert
├── depth_visualize.frag
├── hellotriangle.frag
├── hellotriangle.vert
├── light_casters_directional.frag
├── light_casters_point.frag
├── light_casters_spotlight_hard.frag
├── light_casters_spotlight_soft.frag
├── lighting.frag
├── lighting.vert
├── lighting_maps-exercise2.frag
├── lighting_maps-exercise4.frag
├── lighting_maps.vert
├── lighting_maps_diffuse.frag
├── lighting_maps_specular.frag
├── materials.frag
├── multiple_lights-exercise1.frag
├── multiple_lights.frag
├── outline.frag
├── outline.vert
├── passthrough.vert
├── shaders-exercise1.vert
├── shaders-exercise2.vert
├── shaders-exercise3.vert
├── shaders-interpolated.frag
├── shaders-uniform.frag
├── shaders-uniform.vert
├── texture.frag
├── texture.vert
├── texture2.frag
├── textures-exercise1.frag
├── textures-exercise4.frag
├── textures_combined.frag
├── textures_combined.vert
└── transformations.vert
├── es30
├── basic_lighting-exercise2.frag
├── basic_lighting-exercise2.vert
├── basic_lighting-exercise3.frag
├── basic_lighting-exercise3.vert
├── basic_lighting.frag
├── basic_lighting.vert
├── basic_lighting_diffuse.frag
├── basic_lighting_gouraud.vert
├── checkerboard.frag
├── coordinate_systems.vert
├── coordinate_systems_qt3d_transform.vert
├── depth_visualize.frag
├── hellotriangle.frag
├── hellotriangle.vert
├── light_casters_directional.frag
├── light_casters_point.frag
├── light_casters_spotlight_hard.frag
├── light_casters_spotlight_soft.frag
├── lighting.frag
├── lighting.vert
├── lighting_maps-exercise2.frag
├── lighting_maps-exercise4.frag
├── lighting_maps.vert
├── lighting_maps_diffuse.frag
├── lighting_maps_specular.frag
├── materials.frag
├── multiple_lights-exercise1.frag
├── multiple_lights.frag
├── passthrough.vert
├── shaders-exercise1.vert
├── shaders-exercise2.vert
├── shaders-exercise3.vert
├── shaders-interpolated.frag
├── shaders-uniform.frag
├── shaders-uniform.vert
├── texture.frag
├── texture.vert
├── texture2.frag
├── textures-exercise1.frag
├── textures-exercise4.frag
├── textures_combined.frag
├── textures_combined.vert
└── transformations.vert
└── gl33
├── basic_lighting-exercise2.frag
├── basic_lighting-exercise2.vert
├── basic_lighting-exercise3.frag
├── basic_lighting-exercise3.vert
├── basic_lighting.frag
├── basic_lighting.vert
├── basic_lighting_diffuse.frag
├── basic_lighting_gouraud.vert
├── checkerboard.frag
├── coordinate_systems.vert
├── coordinate_systems_qt3d_transform.vert
├── depth_visualize.frag
├── hellotriangle.frag
├── hellotriangle.vert
├── light_casters_directional.frag
├── light_casters_point.frag
├── light_casters_spotlight_hard.frag
├── light_casters_spotlight_soft.frag
├── lighting.frag
├── lighting.vert
├── lighting_maps-exercise2.frag
├── lighting_maps-exercise4.frag
├── lighting_maps.vert
├── lighting_maps_diffuse.frag
├── lighting_maps_specular.frag
├── materials.frag
├── multiple_lights-exercise1.frag
├── multiple_lights.frag
├── passthrough.vert
├── shaders-exercise1.vert
├── shaders-exercise2.vert
├── shaders-exercise3.vert
├── shaders-interpolated.frag
├── shaders-uniform.frag
├── shaders-uniform.vert
├── texture.frag
├── texture.vert
├── texture2.frag
├── textures-exercise1.frag
├── textures-exercise4.frag
├── textures_combined.frag
├── textures_combined.vert
└── transformations.vert
/.gitignore:
--------------------------------------------------------------------------------
1 | # This file is used to ignore files which are generated
2 | # ----------------------------------------------------------------------------
3 |
4 | *~
5 | *.autosave
6 | *.a
7 | *.core
8 | *.moc
9 | *.o
10 | *.obj
11 | *.orig
12 | *.rej
13 | *.so
14 | *.so.*
15 | *_pch.h.cpp
16 | *_resource.rc
17 | *.qm
18 | .#*
19 | *.*#
20 | core
21 | !core/
22 | tags
23 | .DS_Store
24 | .directory
25 | *.debug
26 | Makefile*
27 | *.prl
28 | *.app
29 | moc_*.cpp
30 | ui_*.h
31 | qrc_*.cpp
32 | Thumbs.db
33 | *.res
34 | *.rc
35 | /.qmake.cache
36 | /.qmake.stash
37 |
38 | # qtcreator generated files
39 | *.pro.user*
40 |
41 | # xemacs temporary files
42 | *.flc
43 |
44 | # Vim temporary files
45 | .*.swp
46 |
47 | # Visual Studio generated files
48 | *.ib_pdb_index
49 | *.idb
50 | *.ilk
51 | *.pdb
52 | *.sln
53 | *.suo
54 | *.vcproj
55 | *.user
56 | *.ncb
57 | *.sdf
58 | *.opensdf
59 | *.vcxproj
60 | *vcxproj.*
61 |
62 | # MinGW generated files
63 | *.Debug
64 | *.Release
65 |
66 | # Python byte code
67 | *.pyc
68 |
69 | # Compiled QML
70 | *.qmlc
71 | *.jsc
72 |
73 | # Binaries
74 | # --------
75 | *.dll
76 | *.exe
77 |
78 | *.backup
79 | *.bak
80 | .directory
81 | build/
82 | shared/assets/model/
83 | shared/assets/texture/skybox/
84 | qml/tests/screenshots
85 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "qml-virtualkey"]
2 | path = qml-virtualkey
3 | url = git@github.com:MidoriYakumo/qml-virtualkey.git
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | learnopengl-qt3d
2 | ================
3 |
4 | Qt3D version of [LearnOpenGL](http://learnopengl.com/) examples, currently works with Qt5.9. I created this repo aiming to understand how Qt3D works.
5 |
6 | 
7 |
8 | Pure QML version, use qmlscene to run with default OpenGL Context:
9 |
10 | **Try it with Qt5.9+:**
11 |
12 | ```shell
13 | qmlscene -I https://raw.githubusercontent.com/MidoriYakumo/qml-virtualkey/master/ https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/master/qml/app.qml
14 | ```
15 |
16 | 
17 |
18 | Compiled version, context set to GL4.3(Desktop)/GLES3.0(Mobile):
19 |
20 | 
21 |
22 | 
23 |
24 | How to launch
25 | -------------
26 |
27 | learnopengl-qt3d can be launched from either qmlscene or compiled binary file.
28 |
29 | Main qml file for qmlscene/binary/QmlCreator:
30 |
31 | - app.qml: main window with line chart FPS display
32 |
33 | - main.qml: QuickItem as root + text FPS display
34 |
35 | With one keyword as arguments you can specify the sample to be launched:
36 |
37 | - qmlscene skybox app.qml
38 | - ./learnopengl-qt3d geometry
39 |
40 | Notes
41 | -----
42 |
43 | - Some large assets from the website is downloaded by qmake script(\*nix only), connection is required at the first build or after updated. Use **CONFIG += no_assets** to skip and enable local filesystem routing(may lead to crash). See [main.cpp](./main.cpp), [assets.pri](./assets.pri), [Resources.qml](./qml/Components/Resources.qml) for details.
44 |
45 | - **leanopengl uses a little different lighting model from Qt3D default model, to rendering models for a better result, please slightly modify the ka, kd in mtl files**
46 |
47 | - run qmltestrunner under qml/tests to perform unit tests or to generate screenshots.
48 |
49 | Content
50 | -------
51 |
52 | ### Getting started
53 |
54 | 1. [Hello Window](doc/Hello-Window.md) : works
55 | 2. [Hello Triangle](doc/Hello-Triangle.md) : works
56 | 3. [Shaders](doc/Shaders.md) : works
57 | 4. [Texture](doc/Texture.md) : works
58 | 5. [Transformations](doc/Transformations.md) : works
59 | 6. [Coordinate Systems](doc/Coordinate-Systems.md) : works
60 | 7. [Camera](doc/Camera.md) : works
61 |
62 | ### Lighting
63 |
64 | 1. [Colors](doc/Colors.md) : works
65 | 2. [Basic Lighting](doc/Basic-Lighting.md) : works
66 | 3. [Materials](doc/Materials.md) : works
67 | 4. [Lighting-maps](doc/Lighting-maps.md) : works
68 | 5. [Light-casters](doc/Light-casters.md) : works
69 |
--------------------------------------------------------------------------------
/assets.pri:
--------------------------------------------------------------------------------
1 | RESOURCES += assets.qrc
2 |
3 | # Use wget because curl get error code 33 for 100% downloading resume
4 |
5 | !exists(shared/assets/texture/skybox) {
6 | message(downloading skybox)
7 | system(wget -cO skybox.rar http://learnopengl.com/img/textures/skybox.rar && unrar x skybox.rar shared/assets/texture/skybox/)
8 | exists(shared/assets/texture/skybox) {
9 | system(mv shared/assets/texture/skybox/left.jpg shared/assets/texture/skybox/skybox_negx.jpg)
10 | system(mv shared/assets/texture/skybox/bottom.jpg shared/assets/texture/skybox/skybox_negy.jpg)
11 | system(mv shared/assets/texture/skybox/front.jpg shared/assets/texture/skybox/skybox_negz.jpg)
12 | system(mv shared/assets/texture/skybox/right.jpg shared/assets/texture/skybox/skybox_posx.jpg)
13 | system(mv shared/assets/texture/skybox/top.jpg shared/assets/texture/skybox/skybox_posy.jpg)
14 | system(mv shared/assets/texture/skybox/back.jpg shared/assets/texture/skybox/skybox_posz.jpg)
15 | system(rm skybox.rar)
16 | message(downloaded skybox)
17 | } else {
18 | message(downloading failed)
19 | }
20 | }
21 |
22 | !exists(shared/assets/model/nanosuit.mtl) {
23 | message(downloading nanosuit)
24 | system(wget -cO nanosuit.rar http://learnopengl.com/data/models/nanosuit.rar && unrar x nanosuit.rar shared/assets/model/)
25 | exists(shared/assets/model/nanosuit.mtl) {
26 | system(rm nanosuit.rar)
27 | message(downloaded nanosuit)
28 | } else {
29 | message(downloading failed)
30 | }
31 | }
32 |
33 | !exists(shared/assets/model/planet.mtl) {
34 | message(downloading planet)
35 | system(wget -cO planet.rar http://learnopengl.com/data/models/planet.rar && unrar x planet.rar shared/assets/model/)
36 | exists(shared/assets/model/planet.mtl) {
37 | system(rm planet.rar)
38 | message(downloaded planet)
39 | } else {
40 | message(downloading failed)
41 | }
42 | }
43 |
44 | !exists(shared/assets/model/rock.mtl) {
45 | message(downloading rock)
46 | system(wget -cO rock.rar http://learnopengl.com/data/models/rock.rar && unrar x rock.rar shared/assets/model/)
47 | exists(shared/assets/model/rock.mtl) {
48 | system(rm rock.rar)
49 | message(downloaded rock)
50 | } else {
51 | message(downloading failed)
52 | }
53 | }
54 |
55 | message(assets checking passed)
56 |
--------------------------------------------------------------------------------
/assets.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | shared/assets/model/arm_dif.png
4 | shared/assets/model/arm_showroom_ddn.png
5 | shared/assets/model/arm_showroom_spec.png
6 | shared/assets/model/body_dif.png
7 | shared/assets/model/body_showroom_ddn.png
8 | shared/assets/model/body_showroom_spec.png
9 | shared/assets/model/glass_ddn.png
10 | shared/assets/model/glass_dif.png
11 | shared/assets/model/hand_dif.png
12 | shared/assets/model/hand_showroom_ddn.png
13 | shared/assets/model/hand_showroom_spec.png
14 | shared/assets/model/helmet_diff.png
15 | shared/assets/model/helmet_showroom_ddn.png
16 | shared/assets/model/helmet_showroom_spec.png
17 | shared/assets/model/leg_dif.png
18 | shared/assets/model/leg_showroom_ddn.png
19 | shared/assets/model/leg_showroom_spec.png
20 | shared/assets/model/nanosuit.mtl
21 | shared/assets/texture/awesomeface.png
22 | shared/assets/texture/container.jpg
23 | shared/assets/texture/container2.png
24 | shared/assets/texture/container2_specular.png
25 | shared/assets/texture/matrix.jpg
26 | shared/assets/model/nanosuit.obj
27 | shared/assets/texture/skybox/skybox_negx.jpg
28 | shared/assets/texture/skybox/skybox_negy.jpg
29 | shared/assets/texture/skybox/skybox_negz.jpg
30 | shared/assets/texture/skybox/skybox_posx.jpg
31 | shared/assets/texture/skybox/skybox_posy.jpg
32 | shared/assets/texture/skybox/skybox_posz.jpg
33 | shared/assets/texture/blending_transparent_window.png
34 | shared/assets/texture/grass.png
35 | shared/assets/model/planet.mtl
36 | shared/assets/model/planet_Quom1200.png
37 | shared/assets/model/Rock-Texture-Surface.jpg
38 | shared/assets/model/rock.mtl
39 | shared/assets/model/planet.obj
40 | shared/assets/model/rock.obj
41 |
42 |
43 |
--------------------------------------------------------------------------------
/deploy_uwp.bat:
--------------------------------------------------------------------------------
1 | hostname > Publisher.obj
2 | set /p Publisher= Tips:
11 | >
12 | > - Qt3D uniform modelNormalMatrix = mat3(transpose(inverse(modelMatrix))) * normalMatrix
13 | > - Qt3D uniform modelViewNormal = mat3(transpose(inverse(view * model)))
14 | > - Internal uniforms can be found at: Src/qt3d/src/render/backend/renderview.cpp:137
15 |
16 | ```c++
17 | RenderView::StandardUniformsPFuncsHash RenderView::initializeStandardUniformSetters()
18 | {
19 | RenderView::StandardUniformsPFuncsHash setters;
20 |
21 | setters.insert(StringToInt::lookupId(QLatin1String("modelMatrix")), &RenderView::modelMatrix);
22 | setters.insert(StringToInt::lookupId(QLatin1String("viewMatrix")), &RenderView::viewMatrix);
23 | setters.insert(StringToInt::lookupId(QLatin1String("projectionMatrix")), &RenderView::projectionMatrix);
24 | setters.insert(StringToInt::lookupId(QLatin1String("modelView")), &RenderView::modelViewMatrix);
25 | setters.insert(StringToInt::lookupId(QLatin1String("modelViewProjection")), &RenderView::modelViewProjectionMatrix);
26 | setters.insert(StringToInt::lookupId(QLatin1String("mvp")), &RenderView::modelViewProjectionMatrix);
27 | setters.insert(StringToInt::lookupId(QLatin1String("inverseModelMatrix")), &RenderView::inverseModelMatrix);
28 | setters.insert(StringToInt::lookupId(QLatin1String("inverseViewMatrix")), &RenderView::inverseViewMatrix);
29 | setters.insert(StringToInt::lookupId(QLatin1String("inverseProjectionMatrix")), &RenderView::inverseProjectionMatrix);
30 | setters.insert(StringToInt::lookupId(QLatin1String("inverseModelView")), &RenderView::inverseModelViewMatrix);
31 | setters.insert(StringToInt::lookupId(QLatin1String("inverseModelViewProjection")), &RenderView::inverseModelViewProjectionMatrix);
32 | setters.insert(StringToInt::lookupId(QLatin1String("modelNormalMatrix")), &RenderView::modelNormalMatrix);
33 | setters.insert(StringToInt::lookupId(QLatin1String("modelViewNormal")), &RenderView::modelViewNormalMatrix);
34 | setters.insert(StringToInt::lookupId(QLatin1String("viewportMatrix")), &RenderView::viewportMatrix);
35 | setters.insert(StringToInt::lookupId(QLatin1String("inverseViewportMatrix")), &RenderView::inverseViewportMatrix);
36 | setters.insert(StringToInt::lookupId(QLatin1String("time")), &RenderView::time);
37 | setters.insert(StringToInt::lookupId(QLatin1String("eyePosition")), &RenderView::eyePosition);
38 |
39 | return setters;
40 | }
41 | ```
42 |
43 | [basic_lighting_specular](../qml/basic_lighting_specular.qml)
44 | -------------------------------------------------------------
45 |
46 | [basic_lighting-exercise1](../qml/basic_lighting-exercise1.qml)
47 | ---------------------------------------------------------------
48 |
49 | [basic_lighting-exercise2](../qml/basic_lighting-exercise2.qml)
50 | ---------------------------------------------------------------
51 |
52 | [basic_lighting-exercise3](../qml/basic_lighting-exercise3.qml)
53 | ---------------------------------------------------------------
54 |
--------------------------------------------------------------------------------
/doc/Camera.md:
--------------------------------------------------------------------------------
1 | Camera
2 | ======
3 |
4 | This section is full of things about mathematics. We just need a QML type to handle camera calculations, there it is, the Camera QML type. But we can DIY fully because it is just math, aha?
5 |
6 | Qt has covered what glm could provided for us:
7 |
8 | > vector, matrix, quaternion...
9 |
10 | Read the examples and documents to unleash your power.
11 |
12 | > Tips:
13 | >
14 | > - Hey, quaternion in QML sucks! I have ported one copy of my javascript geometry library in geo.js for convenience.
15 | > - The Qt3D internal Camera type will be introduced in the next article.
16 |
17 | [camera_circle](../qml/camera_circle.qml)
18 | -----------------------------------------
19 |
20 | [camera_keyboard](../qml/camera_keyboard.qml)
21 | ---------------------------------------------
22 |
23 | Remember FrameSwap? It's true type is FrameAction, the event between frames. We should always adjust geometries and handle physics here.
24 |
25 | 
26 |
27 | > Tips:
28 | >
29 | > - The Qt3D Logic based camera controller helps you handling camera moving, check out in [OurCameraController.qml](../qml/Components/OurCameraController.qml).
30 |
31 | [camera\_keyboard\_dt](../qml/camera_keyboard_dt.qml)
32 | -----------------------------------------------------
33 |
34 | FrameAction support dt internally, we just calculate FPS by this argument.
35 |
36 | [camera_zoom](../qml/camera_zoom.qml)
37 | -------------------------------------
38 |
39 | Use MouseHandler the way you do to MouseArea, modifiers can be passed into mouse event, try to press Shift for accurate aiming. Need zoom for touchscreen? You may need an outer PinchArea.
40 |
41 | 
42 |
43 | [camera_quaternion](../qml/camera_quaternion.qml)
44 | -------------------------------------------------
45 |
46 | To keep upVector up you need to decompose rotation into two axes(yaw and pitch):
47 |
48 | ```javascript
49 | qx = qx.times(rx);
50 | qy = qy.times(ry);
51 | quaternion = qx.times(qy)
52 | ```
53 |
54 | It is more easy to create rotation in quaternion and there is difference with Euler angle, for example you no longer need to **lookAt**!
55 |
56 | ```javascript
57 | var translation = Qt.matrix4x4();
58 | translation.m14 = -position.x;
59 | translation.m24 = -position.y;
60 | translation.m34 = -position.z;
61 | viewMatrix = quaternion.conjugated().toMatrix()
62 | .toQtType().times(translation);
63 | ```
64 |
65 | Try and you can tell it.
66 |
67 | [camera-exercise1](../qml/camera-exercise1.qml)
68 | -----------------------------------------------
69 |
70 | You may enjoy the world you can not fly but infinite jump!
71 |
72 | [camera-exercise2](../qml/camera-exercise2.qml)
73 | -----------------------------------------------
74 |
--------------------------------------------------------------------------------
/doc/Colors.md:
--------------------------------------------------------------------------------
1 | Colors
2 | ======
3 |
4 | I decided to introduce the render types in Qt3D which helps us to create 3D scenes after we had learned all these base concepts. The most important one is, the Camera. In the aspect of render, Qt3D has helped on how to render objects in the scene to our screen, or say the transformations, camera projecton, viewport, to generate what we really wanted: the mvp matrix. By setting up with Qt3D Camera and Qt3D Transform, you can use the mvp uniform in your shaders directly:
5 |
6 | > gl_Position = mvp * vec4(position, 1.);
7 |
8 | These changes are:
9 |
10 | 1. use Qt3D Camera instead of “OurCamera” -> use name viewMatrix and projectionMatrix in shaders
11 | 2. setup CameraSelector in RenderSettings and bind the Camera from Qt3D (**MUST HAVE**, or lead to crash)
12 | 3. use Qt3D Transfrom -> use name modelMatrix in shaders
13 | 4. use Qt3D Mesh -> use name vertexPosition, vertexColor, vertexTexCoord ... (and the layout position?) in shaders
14 |
15 | See [colors_scene.qml](../qml/colors_scene.qml) for details.
16 |
--------------------------------------------------------------------------------
/doc/Coordinate-Systems.md:
--------------------------------------------------------------------------------
1 | Coordinate Systems
2 | ==================
3 |
4 | Qt3D handle your coordinate transform with internal types like Transform and Camera, where Transform gives model matrices and Camera generate view and projection matrices, to add Transform to an object, add it into components:
5 |
6 | ```qml
7 | Entity {
8 | Transform {
9 | id: transform
10 | }
11 | components: [transform]
12 | }
13 | ```
14 |
15 | [coordinate_systems](../qml/coordinate_systems.qml)
16 | ---------------------------------------------------
17 |
18 | [coordinate_systems_with_depth](../qml/coordinate_systems_with_depth.qml)
19 | -------------------------------------------------------------------------
20 |
21 | 1. Qt3D is by default enabled depth test, to disable it we need explicit declare an empty RenderStateSet, when we need depth test, add DepthTest into it:
22 |
23 | 
24 |
25 | [coordinate_systems_multiple_objects](../qml/coordinate_systems_multiple_objects.qml)
26 | -------------------------------------------------------------------------------------
27 |
28 | 1. It is really cool to use QML model delegate, just the same with [Repeater](qthelp://org.qt-project.qtquick.570/qtquick/qml-qtquick-repeater.html), [NodeInstantiator](qthelp://org.qt-project.qt3d.570/qt3d/qml-qt3d-core-nodeinstantiator.html) helps you create mass objects in your scene, we just need to bind different transforms with same geometry and shaders:
29 |
30 | 
31 |
32 | [coordinate_systems-exercise1](../qml/coordinate_systems-exercise1.qml)
33 | -----------------------------------------------------------------------
34 |
35 | [coordinate_systems-exercise2](../qml/coordinate_systems-exercise2.qml)
36 | -----------------------------------------------------------------------
37 |
38 | [coordinate_systems-exercise3](../qml/coordinate_systems-exercise3.qml)
39 | -----------------------------------------------------------------------
40 |
--------------------------------------------------------------------------------
/doc/FreeTalks.md:
--------------------------------------------------------------------------------
1 | # FreeTalks
2 |
3 |
--------------------------------------------------------------------------------
/doc/Hello-Triangle.md:
--------------------------------------------------------------------------------
1 | Hello Triangle
2 | ==============
3 |
4 | Any Qt3D renderable Entity is assembled with three parts:
5 |
6 | > vertices, transforms, shaders
7 |
8 | in qml code:
9 |
10 | ```qml
11 | Entity {
12 |
13 | GeometryRenderer {
14 | id: geometry
15 | }
16 |
17 | Transform {
18 | id: transform
19 | }
20 |
21 | Material {
22 | id: material
23 | }
24 |
25 | components: [geometry, transform, material]
26 | }
27 | ```
28 |
29 | There is no z-order or render order for unassembled entities, thus any following style works:
30 |
31 | ```qml
32 | GeometryRenderer {
33 | id: geometry
34 | }
35 |
36 | Transform {
37 | id: transform
38 | }
39 |
40 | Material {
41 | id: material
42 | }
43 |
44 | Entity {
45 | components: [geometry, transform, material]
46 | }
47 | ```
48 |
49 | ```qml
50 |
51 | Transform {
52 | id: transform
53 | }
54 |
55 | Material {
56 | id: material
57 | }
58 |
59 | Entity {
60 |
61 | GeometryRenderer {
62 | id: geometry
63 | }
64 |
65 | components: [geometry, transform, material]
66 | }
67 | ```
68 |
69 | Flow
70 | ----
71 |
72 | ```
73 | GeometryRenderer <- Geometry <- Attribute <- Buffer <- vertices
74 | | | | |
75 | V ------------------------
76 | Transform ------------------------------------> |
77 | | |
78 | V V
79 | Material -> Effect -> Technique -> RenderPass -> ShaderProgram
80 | ```
81 |
82 | [hellotriangle](../qml/hellotriangle.qml)
83 | -----------------------------------------
84 |
85 | 1. Use ShaderProgram to link shader program flow:
86 |
87 | 
88 |
89 | 2. Use Attribute to bind VBO
90 |
91 | 
92 |
93 | 3. Entity.components assmbles vertices and shader programs to scene graph, RenderPass set the render flow (using default settings).
94 |
95 | 
96 |
97 | > Tips:
98 | >
99 | > - The scene graph nodes can be inspected by tools like [Gammaray](https://www.kdab.com/development-resources/qt-tools/gammaray/).
100 |
101 | [hellotriangle2](../qml/hellotriangle2.qml)
102 | -------------------------------------------
103 |
104 | 1. Corresponding...:
105 |
106 | 
107 |
108 | [hello-triangle-exercise1](../qml/hello-triangle-exercise1.qml)
109 | ---------------------------------------------------------------
110 |
111 | [hello-triangle-exercise2](../qml/hello-triangle-exercise2.qml)
112 | ---------------------------------------------------------------
113 |
114 | You can generate 2 different geometry, bind two entities with them and the same material
115 |
116 | [hello-triangle-exercise3](../qml/hello-triangle-exercise3.qml)
117 | ---------------------------------------------------------------
118 |
119 | loadSource returns QByteArray, you need toString() and then manipulate the string in javascript
120 |
--------------------------------------------------------------------------------
/doc/Light-casters.md:
--------------------------------------------------------------------------------
1 | Light casters
2 | ==============
3 |
4 | [light_casters_directional](../qml/light_casters_directional.qml)
5 | -----------------------------------------------
6 |
7 | [light_casters_point](../qml/light_casters_point.qml)
8 | -----------------------------------------------
9 |
10 | [light_casters_spotlight_hard](../qml/light_casters_spotlight_hard.qml)
11 | -----------------------------------------------
12 |
13 | [light_casters_spotlight_soft](../qml/light_casters_spotlight_soft.qml)
14 | -----------------------------------------------
15 |
--------------------------------------------------------------------------------
/doc/Lighting-maps.md:
--------------------------------------------------------------------------------
1 | Lighting maps
2 | ==============
3 |
4 | [lighting_maps_diffuse](../qml/lighting_maps_diffuse.qml)
5 | -----------------------------------------------
6 |
7 | [lighting_maps_specular](../qml/lighting_maps_specular.qml)
8 | -----------------------------------------------
9 |
10 | [lighting_maps-exercise1](../qml/lighting_maps-exercise1.qml)
11 | -----------------------------------------------
12 |
13 | [lighting_maps-exercise2](../qml/lighting_maps-exercise2.qml)
14 | -----------------------------------------------
15 |
16 | [lighting_maps-exercise3](../qml/lighting_maps-exercise3.qml)
17 | -----------------------------------------------
18 |
19 | [lighting_maps-exercise4](../qml/lighting_maps-exercise4.qml)
20 | -----------------------------------------------
21 |
22 |
--------------------------------------------------------------------------------
/doc/Materials.md:
--------------------------------------------------------------------------------
1 | Materials
2 | ==============
3 |
4 | [materials](../qml/materials.qml)
5 | -----------------------------------------------------------
6 |
7 | Qt3D manage internal simple light types with Qt3D render module, in this case we can choose type PointLight to replace our lighting source, working with Qt3D's Phong's/ADS lighting shaders from Qt3D.Extras PhongMaterial. The core implementation can be found in **Src/qt3d/src/extras/shaders/gl3/light.inc.frag** :
8 |
9 | 
10 |
11 | The complete Lambertian lighting model and the difference will be discussed in later sections.
12 |
13 | [materials-exercise1](../qml/materials.qml)
14 | -------------------------------------------------------------
15 |
16 | The main code has implemented the exercise one, loading model data from web with QtQuick XmlListModel.
--------------------------------------------------------------------------------
/doc/Multiple-lights.md:
--------------------------------------------------------------------------------
1 | Multiple lights
2 | ==============
3 |
4 | [multiple_lights](../qml/multiple_lights.qml)
5 | -----------------------------------------------
6 |
7 | [multiple_lights-exercise1](../qml/multiple_lights-exercise1.qml)
8 | -----------------------------------------------
9 |
10 | [multiple_lights-exercise2](../qml/multiple_lights-exercise2.qml)
11 | -----------------------------------------------
12 |
--------------------------------------------------------------------------------
/doc/Shaders.md:
--------------------------------------------------------------------------------
1 | Shaders
2 | =======
3 |
4 | In Effect, parameters are the binding between uniform variables and QML properties, in single item list, brackets can be ignored:
5 |
6 | ```qml
7 | parameters: Parameter {
8 | name: "uniform"
9 | value: item.value
10 | }
11 | ```
12 |
13 | ```qml
14 | parameters: [
15 | Parameter {},
16 | Parameter {},
17 | Parameter {}
18 | }
19 | ```
20 |
21 | > Tips:
22 | >
23 | > - The filepath of shaders does not accept qml relative paths, a filepath error may lead to program crash.
24 |
25 | [shaders-uniform](../qml/shaders-uniform.qml)
26 | ---------------------------------------------
27 |
28 | 1. Uniform binding:
29 |
30 | 
31 |
32 | [shaders-interpolated](../qml/shaders-interpolated.qml)
33 | -------------------------------------------------------
34 |
35 | 1. Setup size, count, offset, stride for one vertex buffer with several attributes:
36 |
37 | 
38 |
39 | [shaders-exercise1](../qml/shaders-exercise1.qml)
40 | -------------------------------------------------
41 |
42 | [shaders-exercise2](../qml/shaders-exercise2.qml)
43 | -------------------------------------------------
44 |
45 | [shaders-exercise3](../qml/shaders-exercise3.qml)
46 | -------------------------------------------------
47 |
--------------------------------------------------------------------------------
/doc/Texture.md:
--------------------------------------------------------------------------------
1 | Texture
2 | =======
3 |
4 | To use texture in uniform, assign Texture to Parameter value. (Abstract)Texture does not contain any pixmap, one and only one TextureImage is required to attached to it for texture image binding. Currently in Qt3D TextureImage can only be loaded from local files or qrc, neither web resources or QImage/QPixmap memory resources is directly supported.
5 |
6 | ```qml
7 | Parameter {
8 | name: "texture"
9 | value: Texture2D {
10 | TextureImage {
11 | source: "texture.jpg"
12 | }
13 | }
14 | }
15 | ```
16 |
17 | [textures](../qml/textures.qml)
18 | -------------------------------
19 |
20 | 1. Control and adjust texture details in Texture:
21 |
22 | 
23 |
24 | [textures2](../qml/textures2.qml)
25 | ---------------------------------
26 |
27 | [textures_combined](../qml/textures_combined.qml)
28 | -------------------------------------------------
29 |
30 | [textures-exercise1](../qml/textures-exercise1.qml)
31 | ---------------------------------------------------
32 |
33 | [textures-exercise2](../qml/textures-exercise2.qml)
34 | ---------------------------------------------------
35 |
36 | [textures-exercise3](../qml/textures-exercise3.qml)
37 | ---------------------------------------------------
38 |
39 | [textures-exercise4](../qml/textures-exercise4.qml)
40 | ---------------------------------------------------
41 |
42 | This exercise need more knowledge of Qt3D input system.
43 |
44 | To capture keyboard input, we need to import Qt3D.Input, give our Scene focus and setup up the eventSource of root entity to our scene (by InputSettings automatically). Generally it looks like the following:
45 |
46 | ```qml
47 | Scene {
48 | focus: true
49 | aspects: ["input"]
50 |
51 | Entity {
52 | id: root
53 |
54 | InputSettings {}
55 | }
56 | }
57 | ```
58 |
59 | Then a KeyboardDevice is required to add in to root entity, we can now happily handle keyboard events in its KeyboardHandler.
60 |
--------------------------------------------------------------------------------
/doc/Transformations.md:
--------------------------------------------------------------------------------
1 | Transformations
2 | ===============
3 |
4 | In Qt we have types for math calculation such as vector3d, matrix4x4, to generate transformations we can do calculation based on them, for details please see Qt's documentaion about basic QML types.
5 |
6 | ```qml
7 | var m = Qt.matrix4x4()
8 | m.translate(Qt.vector3d(1, -1, 0).times(.5))
9 | m = m.times(rotateAround(joint, angle, Qt.vector3d(0,0,1)))
10 | ```
11 |
12 | > Tips:
13 | >
14 | > - The Qt3D internal Transform type (will be introduced in [Coordinate-Systems#coordinate_systems_with_depth](Coordinate-Systems.md)) only process rigid transform. So cubes will not be sheared or skewed into parallelepiped.
15 |
16 | [transformations](../qml/transformations.qml)
17 | ---------------------------------------------
18 |
19 | 1. Give a mat4 uniform a binding of our time value:
20 |
21 | 
22 |
23 | [transformations-exercise1](../qml/transformations-exercise1.qml)
24 | -----------------------------------------------------------------
25 |
26 | [transformations-exercise2](../qml/transformations-exercise2.qml)
27 | -----------------------------------------------------------------
28 |
29 | The original program makes 2 glDraw call with the same shader, in Qt3D we should change the uniform in another render pass where only transform is changed. By the way, parameters can be set in several rendering classes, this time we put them into RenderPass:
30 |
31 | 
32 |
--------------------------------------------------------------------------------
/doc/img/camera_keyboard.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/camera_keyboard.0.png
--------------------------------------------------------------------------------
/doc/img/camera_zoom.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/camera_zoom.0.png
--------------------------------------------------------------------------------
/doc/img/coordinate_systems_with_depth.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/coordinate_systems_with_depth.0.png
--------------------------------------------------------------------------------
/doc/img/coordinate_systems_with_multiple_objects.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/coordinate_systems_with_multiple_objects.0.png
--------------------------------------------------------------------------------
/doc/img/hellotriangle.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellotriangle.0.png
--------------------------------------------------------------------------------
/doc/img/hellotriangle.1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellotriangle.1.png
--------------------------------------------------------------------------------
/doc/img/hellotriangle.2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellotriangle.2.png
--------------------------------------------------------------------------------
/doc/img/hellotriangle2.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellotriangle2.0.png
--------------------------------------------------------------------------------
/doc/img/hellowindow.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellowindow.0.png
--------------------------------------------------------------------------------
/doc/img/hellowindow.1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellowindow.1.png
--------------------------------------------------------------------------------
/doc/img/hellowindow2.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/hellowindow2.0.png
--------------------------------------------------------------------------------
/doc/img/materials.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/materials.0.png
--------------------------------------------------------------------------------
/doc/img/sc-qt3d-desktop.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/sc-qt3d-desktop.gif
--------------------------------------------------------------------------------
/doc/img/shaders-interpolated.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/shaders-interpolated.0.png
--------------------------------------------------------------------------------
/doc/img/shaders-uniform.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/shaders-uniform.0.png
--------------------------------------------------------------------------------
/doc/img/ss-qml.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/ss-qml.png
--------------------------------------------------------------------------------
/doc/img/ss-qt3d-android.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/ss-qt3d-android.png
--------------------------------------------------------------------------------
/doc/img/ss-qt3d-desktop.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/ss-qt3d-desktop.png
--------------------------------------------------------------------------------
/doc/img/textures.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/textures.0.png
--------------------------------------------------------------------------------
/doc/img/transformations-exercise2.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/transformations-exercise2.0.png
--------------------------------------------------------------------------------
/doc/img/transformations.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/doc/img/transformations.0.png
--------------------------------------------------------------------------------
/learnopengl-qt3d.pro:
--------------------------------------------------------------------------------
1 | include(qml-virtualkey/com_github_midoriyakumo_qmlvirtualkey.pri)
2 |
3 | QT = quick quickcontrols2 charts
4 |
5 | CONFIG += c++11
6 |
7 | SOURCES += main.cpp
8 |
9 | # Additional import path used to resolve QML modules in Qt Creator's code model
10 | QML_IMPORT_PATH = $$PWD/qml-virtualkey
11 |
12 | OTHER_FILES += qml/*.qml
13 |
14 | DISTFILES += shared
15 |
16 | # Do not distribute with assets
17 | CONFIG += no_assets
18 | #CONFIG += no_app
19 |
20 | !contains(CONFIG, no_app) {
21 | RESOURCES += app.qrc
22 | } else {
23 | DEFINES += NO_APP_QRC
24 | message(app.qrc not included.)
25 | }
26 |
27 | unix:!contains(CONFIG, no_assets) {
28 | include(assets.pri)
29 | } else {
30 | DEFINES += NO_ASSETS_QRC
31 | message(assets.qrc not included.)
32 | }
33 |
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include // QtCharts require QtWidgets QApplication ... interesting
2 | #include
3 | #include
4 | #include
5 |
6 | int main(int argc, char* argv[])
7 | {
8 |
9 | #ifdef Q_OS_WIN
10 | QQuickStyle::setStyle("Universal");
11 | #else
12 | QQuickStyle::setStyle("Material");
13 | #endif
14 |
15 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // Good for QuickControls, bad for canvas items
16 | QApplication app(argc, argv); // Opengl dynamic module requires creating app first
17 |
18 | QSurfaceFormat format;
19 | if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { // Learn OpenGL
20 | format.setRenderableType(QSurfaceFormat::OpenGL);
21 | format.setVersion(4, 3);
22 | format.setProfile(QSurfaceFormat::CoreProfile);
23 | format.setSamples(4);
24 | } else if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES) { // Learn OpenGLES??
25 | format.setRenderableType(QSurfaceFormat::OpenGLES);
26 | format.setVersion(3, 0);
27 | format.setSamples(0);
28 | }
29 | format.setAlphaBufferSize(0);
30 | format.setDepthBufferSize(0);
31 | format.setStencilBufferSize(0);
32 | format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
33 | format.setSwapInterval(0); // Full speed rendering
34 | QSurfaceFormat::setDefaultFormat(format);
35 |
36 | QQmlApplicationEngine engine;
37 | engine.addImportPath(QLatin1String("qrc:/com/github/midoriyakumo"));
38 |
39 | #ifdef NO_APP_QRC
40 | #ifdef Q_OS_ANDROID
41 | engine.load(QUrl(QLatin1String("file:/sdcard/Documents/QML Projects/Examples/LearnOpenGL/app.qml")));
42 | #else
43 | engine.load(QUrl(QLatin1String("../qml/app.qml")));
44 | #endif
45 | #else
46 | engine.load(QUrl(QLatin1String("qrc:/qml/app.qml")));
47 | engine.rootObjects().at(0)->setProperty("qrcAppOn", true);
48 | #endif
49 |
50 | #ifndef NO_ASSETS_QRC
51 | engine.rootObjects().at(0)->setProperty("qrcAssetsOn", true);
52 | #endif
53 |
54 | engine.rootObjects().at(0)->setProperty("title", "LearnOpenGL-Qt3D");
55 |
56 | return app.exec();
57 | }
58 |
--------------------------------------------------------------------------------
/qml/Components/AutoShaderProgram.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Render 2.0
2 |
3 | import "."
4 |
5 | ShaderProgram {
6 | property string vertName: "passthrough"
7 | property string fragName: "checkerboard"
8 |
9 | vertexShaderCode: loadSource(Resources.shader(vertName + ".vert"))
10 | fragmentShaderCode: loadSource(Resources.shader(fragName + ".frag"))
11 | }
12 |
--------------------------------------------------------------------------------
/qml/Components/AutoTechnique.qml:
--------------------------------------------------------------------------------
1 | import QtQuick 2.9
2 |
3 | import Qt3D.Render 2.0
4 |
5 | Technique {
6 | /*
7 | Unified routing for Qt5.9
8 | */
9 |
10 | graphicsApiFilter {
11 | profile: GraphicsInfo.profile === GraphicsInfo.CoreProfile ?
12 | GraphicsApiFilter.CoreProfile : GraphicsApiFilter.NoProfile;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/qml/Components/FpsLine.qml:
--------------------------------------------------------------------------------
1 | import QtQuick 2.9
2 | import QtCharts 2.1
3 |
4 | ChartView {
5 | id: app // name it with app in scene to display fps without QML/GL window
6 | anchors.top: parent.top
7 | anchors.right: parent.right
8 | anchors.margins: {
9 | top: vMargin
10 | right: hMargin
11 | }
12 | width: 180
13 | height: 108
14 |
15 | Drag.active: dragger.drag.active
16 |
17 | property int hMargin: 6
18 | property int vMargin: 6
19 |
20 | property real span: d.span
21 | property real maxMargin: .5
22 |
23 | function updateDt(dt) {
24 | var t = d.t + dt;
25 |
26 | ser.append(t, 1. / dt);
27 | while (ser.at(1).x < t - d.span) {
28 | ser.remove(0);
29 | }
30 |
31 | d.t = t;
32 | d.avg = (ser.count - 1) / (t - ser.at(0).x);
33 |
34 | avgSer.clear();
35 | avgSer.append(t - d.span, d.avg);
36 | avgSer.append(t, d.avg);
37 |
38 | // TODO: Math.max(...arr) for ES6
39 | yAxis.max = (d.avg + 1) * (1. + maxMargin);
40 | }
41 |
42 | QtObject {
43 | id: d
44 | property real t: 0.
45 | property real span: 2.
46 | property real avg: 0.
47 | }
48 |
49 | // animationDuration: 100
50 | // animationOptions: ChartView.GridAxisAnimations
51 | animationOptions: ChartView.NoAnimation
52 | backgroundColor: "#801b813e"
53 | backgroundRoundness: 0
54 | margins.top: 2
55 | margins.bottom: 2
56 | margins.left: 2
57 | margins.right: 2
58 | legend.visible: false
59 | antialiasing: true
60 | smooth: true
61 |
62 | ValueAxis {
63 | id: xAxis
64 | min: d.t - d.span
65 | max: d.t
66 | visible: false
67 | }
68 |
69 | ValueAxis {
70 | id: yAxis
71 | min: 0
72 | max: 60
73 | labelsColor: "white"
74 | tickCount: 4
75 | minorGridVisible: false
76 | minorTickCount: 0
77 | shadesVisible: false
78 | labelsFont.pixelSize: 10
79 | labelFormat: "%2d"
80 |
81 | // Behavior on max { // may impact fps
82 | // NumberAnimation {
83 | // duration: 400
84 | // }
85 | // }
86 | }
87 |
88 | LineSeries {
89 | id: avgSer
90 | // useOpenGL : true
91 | color: "#00E2FF"
92 | width: 5
93 | axisX: xAxis
94 | axisY: yAxis
95 | }
96 |
97 | LineSeries {
98 | id: ser
99 | // useOpenGL : true
100 | color: "#ff7e91"
101 | width: 3
102 | axisX: xAxis
103 | axisY: yAxis
104 | XYPoint {
105 | x: 0
106 | y: 0
107 | }
108 | }
109 |
110 | Text {
111 | id: realtime
112 | x: app.plotArea.x + 2
113 | y: app.height - 36
114 | color: "white"
115 | font.pixelSize: 16
116 | text: d.avg.toFixed(1) + "fps"
117 | smooth: false
118 | antialiasing: false
119 | }
120 |
121 | MouseArea {
122 | id: dragger
123 | anchors.fill: parent
124 | drag.target: parent
125 |
126 | drag.onActiveChanged: {
127 | if (!drag.acitve) {
128 | parent.anchors.left = undefined;
129 | parent.anchors.top = undefined;
130 | parent.anchors.right = undefined;
131 | parent.anchors.bottom = undefined;
132 | }
133 | }
134 | }
135 |
136 | onSpanChanged: {
137 | if (span < .1)
138 | d.span = .1;
139 | else if (span > 60)
140 | d.span = 60;
141 | else
142 | d.span = span;
143 |
144 | span = d.span;
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/qml/Components/FrameSwap.qml:
--------------------------------------------------------------------------------
1 | import QtQuick 2.9
2 | import Qt3D.Logic 2.0
3 |
4 | FrameAction {
5 | id: frameSwap
6 |
7 | QtObject {
8 | id: d
9 |
10 | property int cnt
11 | property real accdt
12 | }
13 |
14 | onTriggered: {
15 | try {
16 | var accdt = d.accdt + dt;
17 | var cnt = d.cnt + 1;
18 | if (cnt >= 3) {
19 | app.updateDt(accdt/cnt); // throw
20 | accdt = 0;
21 | cnt = 0;
22 | }
23 | d.accdt = accdt;
24 | d.cnt = cnt;
25 | } catch (e) {
26 |
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/qml/Components/OurCamera.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Render 2.0
2 |
3 | Camera {
4 | id: ourCamera
5 |
6 | property real yaw: 0
7 | property real pitch: 0
8 |
9 | property vector3d frontVector: Qt.vector3d(
10 | Math.cos(pitch * Math.PI / 180.) * Math.sin(yaw * Math.PI / 180.),
11 | -Math.sin(pitch * Math.PI / 180.),
12 | -Math.cos(pitch * Math.PI / 180.) * Math.cos(yaw * Math.PI / 180.)
13 | )
14 | property vector3d rightVector: frontVector.crossProduct(upVector).normalized()
15 |
16 | projectionType: CameraLens.PerspectiveProjection
17 | fieldOfView: 45 // Projection
18 | aspectRatio: scene.width/scene.height
19 | nearPlane : .1
20 | farPlane : 100.
21 | position: "0,0,3"// View
22 | viewCenter: position.plus(frontVector)
23 | upVector: "0,1,0"
24 | }
25 |
--------------------------------------------------------------------------------
/qml/Components/RenderInputSettings0.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 |
5 | Entity {
6 |
7 | property alias mouseSensitivity: cameraController.mouseSensitivity
8 | readonly property alias camera: ourCamera
9 |
10 | RenderSettings2 {
11 | camera: ourCamera
12 | }
13 |
14 | InputSettings {}
15 |
16 | OurCamera {
17 | id: ourCamera
18 | }
19 |
20 | OurCameraController {
21 | id: cameraController
22 |
23 | camera: ourCamera
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/qml/Components/RenderSettings0.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Render 2.0
2 |
3 | RenderSettings {
4 | activeFrameGraph: ClearBuffers { // Simplest FrameGraphNode, see src/ClearBuffers
5 | buffers: ClearBuffers.ColorDepthBuffer // Why not ColorBuffer only??? , result by tests
6 | clearColor: Qt.rgba(0.2, 0.3, 0.3, 1.0)
7 | RenderSurfaceSelector { // Needed by console prompts
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/qml/Components/RenderSettings1.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Render 2.0
2 |
3 | RenderSettings {
4 | activeFrameGraph: ClearBuffers {
5 | buffers: ClearBuffers.ColorDepthBuffer
6 | clearColor: Qt.rgba(0.2, 0.3, 0.3, 1.0)
7 | RenderSurfaceSelector {
8 | RenderStateSet {
9 | renderStates: DepthTest {
10 | depthFunction: DepthTest.Less
11 | }
12 | }
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/qml/Components/RenderSettings2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Render 2.0
2 |
3 | RenderSettings {
4 | id: renderSettings
5 |
6 | property Camera camera
7 |
8 | activeFrameGraph: ClearBuffers {
9 | buffers: ClearBuffers.ColorDepthBuffer
10 | clearColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
11 | RenderSurfaceSelector {
12 | CameraSelector {
13 | /*
14 | CameraSelector gives viewport information so that
15 | renderer can calculate projectionMatrix for Qt3D Camera
16 |
17 | RenderState.DepthTest ???
18 | */
19 |
20 | camera: renderSettings.camera
21 |
22 | RenderStateSet {
23 | renderStates: DepthTest {
24 | depthFunction: DepthTest.Less
25 | }
26 | }
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/qml/Components/Resources.qml:
--------------------------------------------------------------------------------
1 | pragma Singleton
2 |
3 | import QtQuick 2.9
4 |
5 | Item { // This is the global resource router
6 | readonly property var os2Prefix: {
7 | // Compatibility for QML Creator
8 | "android" : "file:/sdcard/Documents/QML Projects/Examples" ,
9 | // "android" : "assets://" ,
10 | "linux" : "file:.." ,
11 | "osx" : "file:.." ,
12 | "unix" : "file:.." ,
13 | "windows" : "file:.." ,
14 | "winrt" : "file:." ,
15 | }
16 |
17 | property bool appRcEnabled: false
18 | property bool assetsRcEnabled: false
19 |
20 | function setGlInfo(info) {
21 | if (info.profile === GraphicsInfo.OpenGLCoreProfile) {
22 | shaderType = "gl33";
23 | } else {
24 | if (info.majorVersion >= 3) {
25 | shaderType = "es30";
26 | } else {
27 | shaderType = "es20";
28 | }
29 | }
30 |
31 | var text = "Open%4 %1.%2 %3".arg(info.majorVersion)
32 | .arg(info.minorVersion).arg({
33 | 0: "NoProfile",
34 | 1: "CoreProfile",
35 | 2: "CompatibilityProfile"
36 | }[info.profile]).arg({
37 | 0: "Unspecified",
38 | 1: "GL",
39 | 2: "GLES"
40 | }[info.renderableType])
41 | ;
42 |
43 | console.log("[Resources] format: %1".arg(text));
44 | console.log("[Resources] shaderType: %1".arg(shaderType));
45 | }
46 |
47 | property string appPrefix: (appRcEnabled?"qrc:":os2Prefix[Qt.platform.os])+ "/"
48 | property string assetsPrefix: (assetsRcEnabled?"qrc:":os2Prefix[Qt.platform.os])+ "/"
49 |
50 | property string shaderType: "gl33"
51 | readonly property string shaderPrefix: appPrefix + "shared/shaders/" + shaderType + "/"
52 | function shader(fn){return shaderPrefix + fn;}
53 |
54 | readonly property string texturePrefix: assetsPrefix + "shared/assets/texture/"
55 | function texture(fn){return texturePrefix + fn;}
56 | readonly property string imagePrefix: assetsPrefix + "shared/assets/image/"
57 | function image(fn){return imagePrefix + fn;}
58 | readonly property string meshPrefix: assetsPrefix + "shared/assets/mesh/"
59 | function mesh(fn){return meshPrefix + fn;}
60 | readonly property string modelPrefix: assetsPrefix + "shared/assets/model/"
61 | function model(fn){return modelPrefix + fn;}
62 |
63 | onAppPrefixChanged: {
64 | console.log("[Resources] app: %1 : %2".arg(appRcEnabled).arg(appPrefix));
65 | }
66 |
67 | onAssetsPrefixChanged: {
68 | console.log("[Resources] assets: %1 : %2".arg(assetsRcEnabled).arg(assetsPrefix));
69 | }
70 |
71 | Component.onCompleted: {
72 | setGlInfo(GraphicsInfo);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/qml/Components/Scene0.qml:
--------------------------------------------------------------------------------
1 | import QtQuick.Scene3D 2.0
2 |
3 | Scene3D {
4 | id: scene
5 | width: 800
6 | height: 600
7 | }
8 |
--------------------------------------------------------------------------------
/qml/Components/Scene1.qml:
--------------------------------------------------------------------------------
1 | import QtQuick.Scene3D 2.0
2 |
3 | Scene3D { // width logic
4 | id: scene
5 | width: 800
6 | height: 600
7 | aspects: ["logic"] // For FPS indicator
8 | }
9 |
--------------------------------------------------------------------------------
/qml/Components/Scene2.qml:
--------------------------------------------------------------------------------
1 | import QtQuick.Scene3D 2.0
2 |
3 | Scene3D {
4 | id: scene
5 | width: 800
6 | height: 600
7 | focus: true // as InputSettings.eventSource, see examples/controls
8 | aspects: ["logic", "input"] // logic is required for inputs, result by tests
9 |
10 | // signal unload
11 |
12 | // onUnload: { // How to unload with InputSetting set to escape from crash?
13 | // focus = false
14 | // }
15 | }
16 |
--------------------------------------------------------------------------------
/qml/Components/Scene3.qml:
--------------------------------------------------------------------------------
1 | import QtQuick.Scene3D 2.0
2 |
3 | Scene3D { // width input
4 | id: scene
5 | width: 800
6 | height: 600
7 | focus: true
8 | aspects: ["input"]
9 | }
10 |
--------------------------------------------------------------------------------
/qml/Components/TextureCubeMesh0.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | GeometryRenderer {
5 | id: mesh
6 | geometry: Geometry {
7 | boundingVolumePositionAttribute: position
8 |
9 | Attribute {
10 | id: position
11 | attributeType: Attribute.VertexAttribute
12 | vertexBaseType: Attribute.Float
13 | vertexSize: 3
14 | count: 36
15 | byteOffset: 0
16 | byteStride: 4 * 5
17 | name: typeof defaultPositionAttributeName === "function" ?
18 | defaultPositionAttributeName():
19 | defaultPositionAttributeName // FIXME: Qt5.8 ???
20 | buffer: vertexBuffer
21 | }
22 |
23 | Attribute {
24 | attributeType: Attribute.VertexAttribute
25 | vertexBaseType: Attribute.Float
26 | vertexSize: 2
27 | count: 36
28 | byteOffset: 4 * 3
29 | byteStride: 4 * 5
30 | name: typeof defaultTextureCoordinateAttributeName === "function" ?
31 | defaultTextureCoordinateAttributeName():
32 | defaultTextureCoordinateAttributeName // FIXME: Qt5.8 ???
33 | buffer: vertexBuffer
34 | }
35 | }
36 |
37 | Buffer {
38 | id: vertexBuffer
39 | type: Buffer.VertexBuffer
40 | data: new Float32Array([
41 | -0.5, -0.5, -0.5, 0.0, 0.0,
42 | 0.5, -0.5, -0.5, 1.0, 0.0,
43 | 0.5, 0.5, -0.5, 1.0, 1.0,
44 | 0.5, 0.5, -0.5, 1.0, 1.0,
45 | -0.5, 0.5, -0.5, 0.0, 1.0,
46 | -0.5, -0.5, -0.5, 0.0, 0.0,
47 |
48 | -0.5, -0.5, 0.5, 0.0, 0.0,
49 | 0.5, -0.5, 0.5, 1.0, 0.0,
50 | 0.5, 0.5, 0.5, 1.0, 1.0,
51 | 0.5, 0.5, 0.5, 1.0, 1.0,
52 | -0.5, 0.5, 0.5, 0.0, 1.0,
53 | -0.5, -0.5, 0.5, 0.0, 0.0,
54 |
55 | -0.5, 0.5, 0.5, 1.0, 0.0,
56 | -0.5, 0.5, -0.5, 1.0, 1.0,
57 | -0.5, -0.5, -0.5, 0.0, 1.0,
58 | -0.5, -0.5, -0.5, 0.0, 1.0,
59 | -0.5, -0.5, 0.5, 0.0, 0.0,
60 | -0.5, 0.5, 0.5, 1.0, 0.0,
61 |
62 | 0.5, 0.5, 0.5, 1.0, 0.0,
63 | 0.5, 0.5, -0.5, 1.0, 1.0,
64 | 0.5, -0.5, -0.5, 0.0, 1.0,
65 | 0.5, -0.5, -0.5, 0.0, 1.0,
66 | 0.5, -0.5, 0.5, 0.0, 0.0,
67 | 0.5, 0.5, 0.5, 1.0, 0.0,
68 |
69 | -0.5, -0.5, -0.5, 0.0, 1.0,
70 | 0.5, -0.5, -0.5, 1.0, 1.0,
71 | 0.5, -0.5, 0.5, 1.0, 0.0,
72 | 0.5, -0.5, 0.5, 1.0, 0.0,
73 | -0.5, -0.5, 0.5, 0.0, 0.0,
74 | -0.5, -0.5, -0.5, 0.0, 1.0,
75 |
76 | -0.5, 0.5, -0.5, 0.0, 1.0,
77 | 0.5, 0.5, -0.5, 1.0, 1.0,
78 | 0.5, 0.5, 0.5, 1.0, 0.0,
79 | 0.5, 0.5, 0.5, 1.0, 0.0,
80 | -0.5, 0.5, 0.5, 0.0, 0.0,
81 | -0.5, 0.5, -0.5, 0.0, 1.0,
82 | ])
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/qml/Components/TextureRectanglePlane0.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | GeometryRenderer {
5 | id: plane
6 | geometry: Geometry {
7 | boundingVolumePositionAttribute: position
8 |
9 | Attribute {
10 | id: position
11 | attributeType: Attribute.VertexAttribute
12 | vertexBaseType: Attribute.Float
13 | vertexSize: 3
14 | count: 4
15 | byteOffset: 0
16 | byteStride: 8 * 4
17 | name: "position"
18 | buffer: vertexBuffer
19 | }
20 |
21 | Attribute {
22 | attributeType: Attribute.VertexAttribute
23 | vertexBaseType: Attribute.Float
24 | vertexSize: 3
25 | count: 4
26 | byteOffset: 3 * 4
27 | byteStride: 8 * 4
28 | name: "color"
29 | buffer: vertexBuffer
30 | }
31 |
32 | Attribute {
33 | attributeType: Attribute.VertexAttribute
34 | vertexBaseType: Attribute.Float
35 | vertexSize: 2
36 | count: 4
37 | byteOffset: 6 * 4
38 | byteStride: 8 * 4
39 | name: "texCoord"
40 | buffer: vertexBuffer
41 | }
42 |
43 | Attribute {
44 | attributeType: Attribute.IndexAttribute
45 | vertexBaseType: Attribute.UnsignedShort
46 | vertexSize: 1
47 | count: 6
48 | buffer: Buffer {
49 | type: Buffer.IndexBuffer
50 | data: new Uint16Array([
51 | 0, 1, 3, // First Triangle
52 | 1, 2, 3, // Second Triangle
53 | ])
54 | }
55 | }
56 | }
57 |
58 | Buffer {
59 | id: vertexBuffer
60 | type: Buffer.VertexBuffer
61 | data: new Float32Array([
62 | // Positions // Colors // Texture Coords
63 | 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
64 | 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
65 | -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
66 | -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left
67 | ])
68 | }
69 | }
70 |
71 |
--------------------------------------------------------------------------------
/qml/Components/Time.qml:
--------------------------------------------------------------------------------
1 | import QtQuick 2.9
2 |
3 | Item {
4 | // NOTE: QtQuick animations are based on swapInterval, so time is required
5 | id: time
6 |
7 | property real value
8 | property bool running: anime.running
9 |
10 | function getTime(){
11 | return new Date().getTime()/1000.;
12 | }
13 |
14 | QtObject {
15 | id: d
16 |
17 | property real offset: 0
18 | property real value // event driver
19 |
20 | onValueChanged: {
21 | time.value = time.getTime() - offset;
22 | //console.log("time.valueChanged", value, time.value);
23 | }
24 | }
25 |
26 | onRunningChanged: {
27 | if (running) {
28 | d.offset += time.getTime();
29 | anime.running = running;
30 | }
31 | else {
32 | anime.running = running;
33 | d.offset -= time.getTime();
34 | }
35 | }
36 |
37 | NumberAnimation {
38 | id: anime
39 | target: d
40 | property: "value"
41 | duration: 1000
42 | from: 0
43 | to: 1
44 |
45 | loops: Animation.Infinite
46 | running: true
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/qml/Components/TrianglePlane0.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | GeometryRenderer {
5 | id: plane
6 | geometry: Geometry {
7 | boundingVolumePositionAttribute: position
8 | Attribute {
9 | id: position
10 | attributeType: Attribute.VertexAttribute
11 | vertexBaseType: Attribute.Float
12 | vertexSize: 3
13 | count: 3
14 | name: "position"
15 | buffer: Buffer {
16 | type: Buffer.VertexBuffer
17 | data: new Float32Array([
18 | -0.5, -0.5, 0.0, // Left
19 | 0.5, -0.5, 0.0, // Right
20 | 0.0, 0.5, 0.0, // Top
21 | ])
22 | }
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/qml/Components/qmldir:
--------------------------------------------------------------------------------
1 |
2 | AssetCheck AssetCheck.qml
3 | AutoShaderProgram AutoShaderProgram.qml
4 | AutoTechnique AutoTechnique.qml
5 | singleton Examples Examples.qml
6 | FpsLine FpsLine.qml
7 | FrameSwap FrameSwap.qml
8 | Geo 1.0 geo.js
9 | NonUniformScaledCuboidMesh0 NonUniformScaledCuboidMesh0.qml
10 | OurCameraController OurCameraController.qml
11 | OurCamera OurCamera.qml
12 | RenderInputSettings0 RenderInputSettings0.qml
13 | RenderSettings0 RenderSettings0.qml
14 | RenderSettings1 RenderSettings1.qml
15 | RenderSettings2 RenderSettings2.qml
16 | singleton Resources Resources.qml
17 | Scene0 Scene0.qml
18 | Scene1 Scene1.qml
19 | Scene2 Scene2.qml
20 | Scene3 Scene3.qml
21 | TextureCube0 TextureCube0.qml
22 | TextureCubeMesh0 TextureCubeMesh0.qml
23 | TextureRectanglePlane0 TextureRectanglePlane0.qml
24 | Time Time.qml
25 | TrianglePlane0 TrianglePlane0.qml
26 | Utils 1.0 utils.js
27 |
--------------------------------------------------------------------------------
/qml/Components/utils.js:
--------------------------------------------------------------------------------
1 | .pragma library
2 |
3 | String.prototype.setCharAt = function(index,chr) { // Create new String
4 | if(index > this.length-1) return this;
5 | return this.substr(0, index) + chr + this.substr(index + 1);
6 | }
7 |
--------------------------------------------------------------------------------
/qml/basic_lighting-exercise1.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 | import Qt3D.Extras 2.0
5 |
6 | import VirtualKey 1.0
7 |
8 | import "Components"
9 |
10 | Scene2 {
11 | id: scene
12 | children: VirtualKeys {
13 | target: scene
14 | gameButtonsEnabled: false
15 | color: "transparent"
16 | centerItem: RowKeys {
17 | keys: [
18 | {text:"Space", key:Qt.Key_Space},
19 | ]
20 | }
21 | }
22 |
23 | Entity {
24 | id: root
25 |
26 | RenderInputSettings0 {
27 | id: renderInputSettings
28 |
29 | mouseSensitivity: .5 / Units.dp
30 | }
31 |
32 | KeyboardDevice {
33 | id: keyboardDevice
34 | }
35 |
36 | KeyboardHandler {
37 | id: keyboardHandler
38 | sourceDevice: keyboardDevice
39 | focus: true
40 |
41 | onSpacePressed: {
42 | root.useGouraudShader = !root.useGouraudShader;
43 | console.log("useGouraudShader:", root.useGouraudShader);
44 | }
45 | }
46 |
47 | property bool useGouraudShader: false
48 |
49 | property vector3d lightPos: Qt.vector3d(
50 | Math.cos(time.v1) * Math.cos(time.v2) * 2.,
51 | Math.cos(time.v1) * Math.sin(time.v2) * 2.,
52 | Math.sin(time.v1) * 2.
53 | )
54 | property vector3d viewPos: renderInputSettings.camera.position
55 | property color lightColor: "white"
56 | property color objectColor: "coral"
57 |
58 | Time {
59 | id: time
60 |
61 | property real v1: value * 1.234
62 | property real v2: value * 1.345
63 | }
64 |
65 | CuboidMesh {
66 | id: mesh
67 | }
68 |
69 | Entity {
70 | id: object
71 |
72 | Transform {
73 | id: objectTransform
74 | }
75 |
76 | Material {
77 | id: objectMaterial
78 | effect: Effect {
79 | techniques: AutoTechnique {
80 | renderPasses: RenderPass {
81 | shaderProgram: AutoShaderProgram {
82 | vertName: root.useGouraudShader?
83 | "basic_lighting_gouraud":
84 | "basic_lighting"
85 | fragName: root.useGouraudShader?
86 | "shaders-interpolated":
87 | "basic_lighting"
88 | }
89 | parameters: [
90 | Parameter {
91 | name: "lightPos"
92 | value: root.lightPos
93 | },
94 | Parameter {
95 | name: "viewPos"
96 | value: root.viewPos
97 | },
98 | Parameter {
99 | name: "lightColor"
100 | value: Qt.vector3d(
101 | root.lightColor.r,
102 | root.lightColor.g,
103 | root.lightColor.b
104 | )
105 | },
106 | Parameter {
107 | name: "objectColor"
108 | value: Qt.vector3d(
109 | root.objectColor.r,
110 | root.objectColor.g,
111 | root.objectColor.b
112 | )
113 | }
114 | ]
115 | }
116 | }
117 | }
118 | }
119 |
120 | components: [mesh, objectTransform, objectMaterial]
121 | }
122 |
123 | Entity {
124 | id: lamp
125 |
126 | Transform {
127 | id: lampTransform
128 | translation: root.lightPos
129 | scale: .2
130 | }
131 |
132 | Material {
133 | id: lampMaterial
134 | effect: Effect {
135 | techniques: AutoTechnique {
136 | renderPasses: RenderPass {
137 | shaderProgram: AutoShaderProgram {
138 | vertName: "basic_lighting"
139 | fragName: "shaders-uniform"
140 | }
141 | parameters: [
142 | Parameter {
143 | name: "ourColor"
144 | value: root.lightColor
145 | }
146 | ]
147 | }
148 | }
149 | }
150 | }
151 |
152 | components: [mesh, lampTransform, lampMaterial]
153 | }
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/qml/basic_lighting-exercise2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Extras 2.0
4 |
5 | import VirtualKey 1.0
6 |
7 | import "Components"
8 |
9 | Scene2 {
10 | id: scene
11 | children: VirtualKeys {
12 | target: scene
13 | gameButtonsEnabled: false
14 | color: "transparent"
15 | }
16 |
17 | Entity {
18 | id: root
19 |
20 | RenderInputSettings0 {
21 | id: renderInputSettings
22 |
23 | mouseSensitivity: .5 / Units.dp
24 | }
25 |
26 | property vector3d lightPos: "1.2, 1.0, 2.0"
27 | property color lightColor: "white"
28 | property color objectColor: "coral"
29 |
30 | CuboidMesh {
31 | id: mesh
32 | }
33 |
34 | Entity {
35 | id: object
36 |
37 | Transform {
38 | id: objectTransform
39 | }
40 |
41 | Material {
42 | id: objectMaterial
43 | effect: Effect {
44 | techniques: AutoTechnique {
45 | renderPasses: RenderPass {
46 | shaderProgram: AutoShaderProgram {
47 | vertName: "basic_lighting-exercise2"
48 | fragName: "basic_lighting-exercise2"
49 | }
50 | parameters: [
51 | Parameter {
52 | name: "lightPos"
53 | value: root.lightPos
54 | },
55 | Parameter {
56 | name: "lightColor"
57 | value: Qt.vector3d(
58 | root.lightColor.r,
59 | root.lightColor.g,
60 | root.lightColor.b
61 | )
62 | },
63 | Parameter {
64 | name: "objectColor"
65 | value: Qt.vector3d(
66 | root.objectColor.r,
67 | root.objectColor.g,
68 | root.objectColor.b
69 | )
70 | }
71 | ]
72 | }
73 | }
74 | }
75 | }
76 |
77 | components: [mesh, objectTransform, objectMaterial]
78 | }
79 |
80 | Entity {
81 | id: lamp
82 |
83 | Transform {
84 | id: lampTransform
85 | translation: root.lightPos
86 | scale: .2
87 | }
88 |
89 | Material {
90 | id: lampMaterial
91 | effect: Effect {
92 | techniques: AutoTechnique {
93 | renderPasses: RenderPass {
94 | shaderProgram: AutoShaderProgram {
95 | vertName: "basic_lighting"
96 | fragName: "shaders-uniform"
97 | }
98 | parameters: [
99 | Parameter {
100 | name: "ourColor"
101 | value: root.lightColor
102 | }
103 | ]
104 | }
105 | }
106 | }
107 | }
108 |
109 | components: [mesh, lampTransform, lampMaterial]
110 | }
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/qml/basic_lighting-exercise3.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 | import Qt3D.Extras 2.0
5 |
6 | import VirtualKey 1.0
7 |
8 | import "Components"
9 |
10 | Scene2 {
11 | id: scene
12 | children: VirtualKeys {
13 | target: scene
14 | gameButtonsEnabled: false
15 | color: "transparent"
16 | centerItem: RowKeys {
17 | keys: [
18 | {text:"Space", key:Qt.Key_Space},
19 | ]
20 | }
21 | }
22 |
23 | Entity {
24 | id: root
25 |
26 | RenderInputSettings0 {
27 | id: renderInputSettings
28 |
29 | mouseSensitivity: .5 / Units.dp
30 | }
31 |
32 | KeyboardDevice {
33 | id: keyboardDevice
34 | }
35 |
36 | KeyboardHandler {
37 | id: keyboardHandler
38 | sourceDevice: keyboardDevice
39 | focus: true
40 |
41 | onSpacePressed: {
42 | root.useGouraudShader = !root.useGouraudShader;
43 | console.log("useGouraudShader:", root.useGouraudShader);
44 | }
45 | }
46 |
47 | property bool useGouraudShader: true
48 |
49 | property vector3d lightPos: "1.2, 1.0, 2.0"
50 | property vector3d viewPos: renderInputSettings.camera.position
51 | property color lightColor: "white"
52 | property color objectColor: "coral"
53 |
54 | CuboidMesh {
55 | id: mesh
56 | }
57 |
58 | Entity {
59 | id: object
60 |
61 | Transform {
62 | id: objectTransform
63 | }
64 |
65 | Material {
66 | id: objectMaterial
67 | effect: Effect {
68 | techniques: AutoTechnique {
69 | renderPasses: RenderPass {
70 | shaderProgram: AutoShaderProgram {
71 | vertName: root.useGouraudShader?
72 | "basic_lighting-exercise3":
73 | "basic_lighting"
74 | fragName: root.useGouraudShader?
75 | "basic_lighting-exercise3":
76 | "basic_lighting"
77 | }
78 | parameters: [
79 | Parameter {
80 | name: "lightPos"
81 | value: root.lightPos
82 | },
83 | Parameter {
84 | name: "viewPos"
85 | value: root.viewPos
86 | },
87 | Parameter {
88 | name: "lightColor"
89 | value: Qt.vector3d(
90 | root.lightColor.r,
91 | root.lightColor.g,
92 | root.lightColor.b
93 | )
94 | },
95 | Parameter {
96 | name: "objectColor"
97 | value: Qt.vector3d(
98 | root.objectColor.r,
99 | root.objectColor.g,
100 | root.objectColor.b
101 | )
102 | }
103 | ]
104 | }
105 | }
106 | }
107 | }
108 |
109 | components: [mesh, objectTransform, objectMaterial]
110 | }
111 |
112 | Entity {
113 | id: lamp
114 |
115 | Transform {
116 | id: lampTransform
117 | translation: root.lightPos
118 | scale: .2
119 | }
120 |
121 | Material {
122 | id: lampMaterial
123 | effect: Effect {
124 | techniques: AutoTechnique {
125 | renderPasses: RenderPass {
126 | shaderProgram: AutoShaderProgram {
127 | vertName: "basic_lighting"
128 | fragName: "shaders-uniform"
129 | }
130 | parameters: [
131 | Parameter {
132 | name: "ourColor"
133 | value: root.lightColor
134 | }
135 | ]
136 | }
137 | }
138 | }
139 | }
140 |
141 | components: [mesh, lampTransform, lampMaterial]
142 | }
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/qml/basic_lighting_specular.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 | import Qt3D.Extras 2.0
5 |
6 | import VirtualKey 1.0
7 |
8 | import "Components"
9 |
10 | Scene2 {
11 | id: scene
12 | children: VirtualKeys {
13 | target: scene
14 | gameButtonsEnabled: false
15 | color: "transparent"
16 | centerItem: RowKeys {
17 | keys: [
18 | {text:"Space", key:Qt.Key_Space},
19 | ]
20 | }
21 | }
22 |
23 | Entity {
24 | id: root
25 |
26 | RenderInputSettings0 {
27 | id: renderInputSettings
28 |
29 | mouseSensitivity: .5 / Units.dp
30 | }
31 |
32 | KeyboardDevice {
33 | id: keyboardDevice
34 | }
35 |
36 | KeyboardHandler {
37 | id: keyboardHandler
38 | sourceDevice: keyboardDevice
39 | focus: true
40 |
41 | onSpacePressed: {
42 | root.useGouraudShader = !root.useGouraudShader;
43 | console.log("useGouraudShader:", root.useGouraudShader);
44 | }
45 | }
46 |
47 | property bool useGouraudShader: false
48 |
49 | property vector3d lightPos: "1.2, 1.0, 2.0"
50 | property vector3d viewPos: renderInputSettings.camera.position
51 | property color lightColor: "white"
52 | property color objectColor: "coral"
53 |
54 | CuboidMesh {
55 | id: mesh
56 | }
57 |
58 | Entity {
59 | id: object
60 |
61 | Transform {
62 | id: objectTransform
63 | }
64 |
65 | Material {
66 | id: objectMaterial
67 | effect: Effect {
68 | techniques: AutoTechnique {
69 | renderPasses: RenderPass {
70 | shaderProgram: AutoShaderProgram {
71 | vertName: root.useGouraudShader?
72 | "basic_lighting_gouraud":
73 | "basic_lighting"
74 | fragName: root.useGouraudShader?
75 | "shaders-interpolated":
76 | "basic_lighting"
77 | }
78 | parameters: [
79 | Parameter {
80 | name: "lightPos"
81 | value: root.lightPos
82 | },
83 | Parameter {
84 | name: "viewPos"
85 | value: root.viewPos
86 | },
87 | Parameter {
88 | name: "lightColor"
89 | value: Qt.vector3d(
90 | root.lightColor.r,
91 | root.lightColor.g,
92 | root.lightColor.b
93 | )
94 | },
95 | Parameter {
96 | name: "objectColor"
97 | value: Qt.vector3d(
98 | root.objectColor.r,
99 | root.objectColor.g,
100 | root.objectColor.b
101 | )
102 | }
103 | ]
104 | }
105 | }
106 | }
107 | }
108 |
109 | components: [mesh, objectTransform, objectMaterial]
110 | }
111 |
112 | Entity {
113 | id: lamp
114 |
115 | Transform {
116 | id: lampTransform
117 | translation: root.lightPos
118 | scale: .2
119 | }
120 |
121 | Material {
122 | id: lampMaterial
123 | effect: Effect {
124 | techniques: AutoTechnique {
125 | renderPasses: RenderPass {
126 | shaderProgram: AutoShaderProgram {
127 | vertName: "basic_lighting"
128 | fragName: "shaders-uniform"
129 | }
130 | parameters: [
131 | Parameter {
132 | name: "ourColor"
133 | value: root.lightColor
134 | }
135 | ]
136 | }
137 | }
138 | }
139 | }
140 |
141 | components: [mesh, lampTransform, lampMaterial]
142 | }
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/qml/camera_circle.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 |
3 | import "Components"
4 |
5 | Scene1 {
6 | id: scene
7 |
8 | Entity {
9 | id: root
10 |
11 | RenderSettings1 {}
12 |
13 | FrameSwap {}
14 |
15 | Time {
16 | id: time
17 | }
18 |
19 | Entity { // Our Camera type !
20 | id: camera
21 |
22 | property vector3d position: Qt.vector3d(10.*Math.sin(time.value), 0, 10.*Math.cos(time.value))
23 | property vector3d viewCenter: "0,0,0"
24 | property vector3d upVector: "0,1,0"
25 |
26 | property matrix4x4 viewMatrix: {
27 | var m = Qt.matrix4x4();
28 | // Use Qt lookAt, reference: qthelp://org.qt-project.qtgui.570/qtgui/qmatrix4x4.html#lookAt
29 | m.lookAt(position, viewCenter, upVector);
30 | return m;
31 | }
32 | property matrix4x4 projectionMatrix: {
33 | var fov = 45;
34 | var aspect = scene.width / scene.height;
35 | var zNear = .1;
36 | var zFar = 100.;
37 | var h = Math.tan(fov * Math.PI / 360) * zNear;
38 | var w = h * aspect;
39 |
40 | var m = Qt.matrix4x4();
41 | m.m11 = zNear / w;
42 | m.m22 = zNear / h;
43 | m.m33 = - (zNear + zFar) / (zFar - zNear);
44 | m.m34 = -2 * zNear * zFar / (zFar - zNear);
45 | m.m43 = -1;
46 | m.m44 = 0;
47 | return m;
48 | }
49 | }
50 |
51 | property var cubePositions: [
52 | Qt.vector3d( 0.0, 0.0, 0.0),
53 | Qt.vector3d( 2.0, 5.0, -15.0),
54 | Qt.vector3d(-1.5, -2.2, -2.5),
55 | Qt.vector3d(-3.8, -2.0, -12.3),
56 | Qt.vector3d( 2.4, -0.4, -3.5),
57 | Qt.vector3d(-1.7, 3.0, -7.5),
58 | Qt.vector3d( 1.3, -2.0, -2.5),
59 | Qt.vector3d( 1.5, 2.0, -2.5),
60 | Qt.vector3d( 1.5, 0.2, -1.5),
61 | Qt.vector3d(-1.3, 1.0, -1.5),
62 | ]
63 |
64 | NodeInstantiator {
65 | model: root.cubePositions
66 | delegate: TextureCube0 { // Moved to Cube type
67 | transform: Transform {
68 | translation: modelData
69 | rotation: fromAxisAndAngle(Qt.vector3d(0.5, 1.0, 0.0), 20 * index)
70 | }
71 | viewMatrix: camera.viewMatrix
72 | projectionMatrix: camera.projectionMatrix
73 | }
74 | }
75 |
76 |
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/qml/coordinate_systems.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | id: scene
8 |
9 | Entity {
10 | id: root
11 |
12 | RenderSettings0 {}
13 |
14 | Entity {
15 | id: plane
16 |
17 | TextureRectanglePlane0 {
18 | id: geometry
19 | }
20 |
21 | Material {
22 | id: material
23 | effect: Effect {
24 | techniques: AutoTechnique {
25 | renderPasses: RenderPass {
26 | renderStates: CullFace { mode: CullFace.NoCulling }
27 | shaderProgram: AutoShaderProgram {
28 | vertName: "coordinate_systems"
29 | fragName: "textures_combined"
30 | }
31 | }
32 | }
33 | }
34 |
35 | parameters: [
36 | Parameter {
37 | name: "ourTexture1"
38 | value: Texture2D {
39 | generateMipMaps: true
40 | minificationFilter: Texture.Linear
41 | magnificationFilter: Texture.Linear
42 | wrapMode {
43 | x: WrapMode.Repeat
44 | y: WrapMode.Repeat
45 | }
46 | TextureImage {
47 | mipLevel: 0
48 | source: Resources.texture("container.jpg")
49 | }
50 | }
51 | },
52 | Parameter {
53 | name: "ourTexture2"
54 | value: Texture2D {
55 | generateMipMaps: true
56 | minificationFilter: Texture.Linear
57 | magnificationFilter: Texture.Linear
58 | wrapMode {
59 | x: WrapMode.Repeat
60 | y: WrapMode.Repeat
61 | }
62 | TextureImage {
63 | mipLevel: 0
64 | source: Resources.texture("awesomeface.png")
65 | }
66 | }
67 | },
68 | Parameter {
69 | name: "model"
70 | value: {
71 | var m = Qt.matrix4x4();
72 | m.rotate(-55, Qt.vector3d(1, 0, 0));
73 | return m;
74 | }
75 | },
76 | Parameter {
77 | name: "view"
78 | value: {
79 | var m = Qt.matrix4x4();
80 | m.translate(0, 0, -3);
81 | return m;
82 | }
83 | },
84 | Parameter {
85 | name: "projection"
86 | value: {
87 | var fov = 45;
88 | var aspect = scene.width / scene.height;
89 | var zNear = .1;
90 | var zFar = 100.;
91 | var h = Math.tan(fov * Math.PI / 360) * zNear;
92 | var w = h * aspect;
93 |
94 | var m = Qt.matrix4x4();
95 | m.m11 = zNear / w;
96 | m.m22 = zNear / h;
97 | m.m33 = - (zNear + zFar) / (zFar - zNear);
98 | m.m34 = -2 * zNear * zFar / (zFar - zNear);
99 | m.m43 = -1;
100 | m.m44 = 0;
101 | return m;
102 | }
103 | }
104 | ]
105 | }
106 |
107 | components: [geometry, material]
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/qml/deploy.sh:
--------------------------------------------------------------------------------
1 | rm tests/screenshots/*
2 |
3 | adb push ./ '/sdcard//Documents/QML Projects/Examples/LearnOpenGL/'
4 | #adb push VirtualKey/ '/sdcard//Documents/QML Projects/Examples/LearnOpenGL/VirtualKey/'
5 | adb push ../shared/ '/sdcard//Documents/QML Projects/Examples/shared/'
6 |
--------------------------------------------------------------------------------
/qml/depth_testing.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Extras 2.0
4 |
5 | import VirtualKey 1.0
6 |
7 | import "Components"
8 |
9 | Scene2 {
10 | id: scene
11 | children: VirtualKeys {
12 | target: scene
13 | gameButtonsEnabled: false
14 | color: "transparent"
15 | centerItem: RowKeys {
16 | keys: [
17 | {text:"Space", key:Qt.Key_Space},
18 | ]
19 | }
20 | }
21 |
22 | Entity {
23 | id: root
24 |
25 | RenderInputSettings0 {
26 | id: renderInputSettings
27 |
28 | mouseSensitivity: .5 / Units.dp
29 | }
30 |
31 | property real fov: renderInputSettings.camera.fieldOfView
32 | property real nearZ: renderInputSettings.camera.nearPlane
33 | property real farZ: renderInputSettings.camera.farPlane
34 |
35 | SphereMesh {
36 | id: mesh
37 | }
38 |
39 | Material {
40 | id: material
41 | effect: Effect {
42 | techniques: AutoTechnique {
43 | renderPasses: RenderPass {
44 | shaderProgram: AutoShaderProgram {
45 | fragName: "depth_visualize"
46 | }
47 | parameters: [
48 | Parameter {
49 | name: "nearZ"
50 | value: root.nearZ
51 | },
52 | Parameter {
53 | name: "farZ"
54 | value: root.farZ
55 | }
56 | ]
57 | }
58 | }
59 | }
60 | }
61 |
62 | NodeInstantiator {
63 | model: 1000
64 | delegate: Entity {
65 | property Transform transform: Transform {
66 | property real z: Math.random() * -root.farZ
67 | translation: Qt.vector3d(
68 | (Math.random()-.5) * z * Math.tan(root.fov / 2.) * 2.,
69 | (Math.random()-.5) * z * Math.tan(root.fov / 2.) * 2.,
70 | z
71 | )
72 | scale: Math.random() * .1 * z + .5
73 | }
74 |
75 | components: [mesh, transform, material]
76 | }
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/qml/hello-triangle-exercise1.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | Entity {
13 | id: plane
14 |
15 | GeometryRenderer {
16 | id: geometry
17 | geometry: Geometry {
18 | Attribute {
19 | attributeType: Attribute.VertexAttribute
20 | vertexBaseType: Attribute.Float
21 | vertexSize: 3
22 | count: 6
23 | name: "position"
24 | buffer: Buffer {
25 | type: Buffer.VertexBuffer
26 | data: new Float32Array([
27 | // First triangle
28 | -0.9, -0.5, 0.0, // Left
29 | -0.0, -0.5, 0.0, // Right
30 | -0.45, 0.5, 0.0, // Top
31 | // Second triangle
32 | 0.0, -0.5, 0.0, // Left
33 | 0.9, -0.5, 0.0, // Right
34 | 0.45, 0.5, 0.0, // Top
35 | ])
36 | }
37 | }
38 | }
39 | }
40 |
41 | Material {
42 | id: material
43 | effect: Effect {
44 | techniques: AutoTechnique {
45 | renderPasses: RenderPass {
46 | shaderProgram: ShaderProgram {
47 | vertexShaderCode: loadSource(Resources.shader("hellotriangle.vert"))
48 | fragmentShaderCode: loadSource(Resources.shader("hellotriangle.frag"))
49 | }
50 | }
51 | }
52 | }
53 | }
54 |
55 | components: [geometry, material]
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/qml/hello-triangle-exercise2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: plane
9 |
10 | RenderSettings0 {}
11 |
12 | GeometryRenderer {
13 | id: geometry0
14 | geometry: Geometry {
15 | Attribute {
16 | attributeType: Attribute.VertexAttribute
17 | vertexBaseType: Attribute.Float
18 | vertexSize: 3
19 | count: 3
20 | name: "position"
21 | buffer: Buffer {
22 | type: Buffer.VertexBuffer
23 | data: new Float32Array([
24 | // First triangle
25 | -0.9, -0.5, 0.0, // Left
26 | -0.0, -0.5, 0.0, // Right
27 | -0.45, 0.5, 0.0, // Top
28 | ])
29 | }
30 | }
31 | }
32 | }
33 |
34 | GeometryRenderer {
35 | id: geometry1
36 | geometry: Geometry {
37 | Attribute {
38 | attributeType: Attribute.VertexAttribute
39 | vertexBaseType: Attribute.Float
40 | vertexSize: 3
41 | count: 3
42 | name: "position"
43 | buffer: Buffer {
44 | type: Buffer.VertexBuffer
45 | data: new Float32Array([
46 | // Second triangle
47 | 0.0, -0.5, 0.0, // Left
48 | 0.9, -0.5, 0.0, // Right
49 | 0.45, 0.5, 0.0, // Top
50 | ])
51 | }
52 | }
53 | }
54 | }
55 |
56 | Material {
57 | id: material
58 | effect: Effect {
59 | techniques: AutoTechnique {
60 | renderPasses: RenderPass {
61 | shaderProgram: ShaderProgram {
62 | vertexShaderCode: loadSource(Resources.shader("hellotriangle.vert"))
63 | fragmentShaderCode: loadSource(Resources.shader("hellotriangle.frag"))
64 | }
65 | }
66 | }
67 | }
68 | }
69 |
70 | // Two objects with two buffers in one renderpass
71 |
72 | Entity {
73 | id: triangle0
74 | components: [geometry0, material]
75 | }
76 |
77 | Entity {
78 | id: triangle1
79 | components: [geometry1, material]
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/qml/hello-triangle-exercise3.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: plane
9 |
10 | RenderSettings0 {}
11 |
12 | GeometryRenderer {
13 | id: geometry0
14 | geometry: Geometry {
15 | Attribute {
16 | attributeType: Attribute.VertexAttribute
17 | vertexBaseType: Attribute.Float
18 | vertexSize: 3
19 | count: 3
20 | name: "position"
21 | buffer: Buffer {
22 | type: Buffer.VertexBuffer
23 | data: new Float32Array([
24 | // First triangle
25 | -0.9, -0.5, 0.0, // Left
26 | -0.0, -0.5, 0.0, // Right
27 | -0.45, 0.5, 0.0, // Top
28 | ])
29 | }
30 | }
31 | }
32 | }
33 |
34 | GeometryRenderer {
35 | id: geometry1
36 | geometry: Geometry {
37 | Attribute {
38 | attributeType: Attribute.VertexAttribute
39 | vertexBaseType: Attribute.Float
40 | vertexSize: 3
41 | count: 3
42 | name: "position" // Auto picked up for boundingPositionAttribute
43 | buffer: Buffer {
44 | type: Buffer.VertexBuffer
45 | data: new Float32Array([
46 | // Second triangle
47 | 0.0, -0.5, 0.0, // Left
48 | 0.9, -0.5, 0.0, // Right
49 | 0.45, 0.5, 0.0, // Top
50 | ])
51 | }
52 | }
53 | }
54 | }
55 |
56 | Material {
57 | id: material0
58 | effect: Effect {
59 | techniques: AutoTechnique {
60 | renderPasses: RenderPass {
61 | shaderProgram: ShaderProgram {
62 | vertexShaderCode: loadSource(Resources.shader("hellotriangle.vert"))
63 | fragmentShaderCode: loadSource(Resources.shader("hellotriangle.frag"))
64 | }
65 | }
66 | }
67 | }
68 | }
69 |
70 | Material {
71 | id: material1
72 | effect: Effect {
73 | techniques: AutoTechnique {
74 | renderPasses: RenderPass {
75 | shaderProgram: ShaderProgram {
76 | // ShaderCodes are bytes! We need javascript strings!
77 | vertexShaderCode: loadSource(Resources.shader("hellotriangle.vert"))
78 | fragmentShaderCode: loadSource(Resources.shader("hellotriangle.frag"))
79 | .toString().replace(".5","1.").replace(".2", "0.")
80 | }
81 | }
82 | }
83 | }
84 | }
85 |
86 | // Two triangles with two materials/shaders
87 |
88 | Entity {
89 | id: triangle0
90 | components: [geometry0, material0]
91 | }
92 |
93 | Entity {
94 | id: triangle1
95 | components: [geometry1, material1]
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/qml/hellotriangle.qml:
--------------------------------------------------------------------------------
1 | import QtQuick 2.9
2 |
3 | import Qt3D.Core 2.0
4 | import Qt3D.Render 2.0
5 |
6 | import "Components"
7 |
8 | Scene0 {
9 | Entity {
10 | id: root
11 |
12 | RenderSettings0 {}
13 |
14 | Entity {
15 | // The object to render: on a 2D surface
16 | id: plane
17 |
18 | GeometryRenderer {
19 | /*
20 | Construct vertices, setup primitive settings -> Vertex Shaders
21 | */
22 |
23 | id: geometry
24 | geometry: Geometry {
25 | /*
26 | Provide vertices
27 | Children are Attribute, the vertex shader attribute bindings
28 | Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qgeometry.html
29 | attributes is list property, either
30 | attributes: Attribute {
31 | }
32 | or
33 | Attribute {
34 | }
35 | works.
36 |
37 | */
38 |
39 | Attribute {
40 | /*
41 | The attribute
42 | Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qattribute.html
43 | */
44 |
45 | attributeType: Attribute.VertexAttribute
46 | vertexBaseType: Attribute.Float
47 | vertexSize: 3
48 | count: 3
49 | name: "position" // Auto picked up as boundingPositionAttribute
50 | buffer: Buffer {
51 | /*
52 | The vertex buffer
53 | Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qbuffer.html
54 | */
55 |
56 | type: Buffer.VertexBuffer
57 | data: new Float32Array([
58 | -0.5, -0.5, 0.0, // Left
59 | 0.5, -0.5, 0.0, // Right
60 | 0.0, 0.5, 0.0, // Top
61 | ]) // Need a C++ array, Javascript Float32Array helps
62 | }
63 | }
64 | }
65 | }
66 |
67 | Material {
68 | /*
69 | Setup shades, ie materials of objects
70 | */
71 |
72 | id: material
73 | effect: Effect {
74 | /*
75 | Link shader and uniforms
76 | Neither techniques or parameters can be directly attached
77 | */
78 |
79 | techniques: Technique {
80 | /*
81 | Technique = GL API, shader, renderpass filter/router
82 | */
83 |
84 | graphicsApiFilter {
85 | profile: GraphicsInfo.profile === GraphicsInfo.CoreProfile ?
86 | GraphicsApiFilter.CoreProfile : GraphicsApiFilter.NoProfile;
87 | }
88 |
89 | renderPasses: RenderPass {
90 | /*
91 | How to render in each pass, setup uniforms and create GL context states
92 | Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qrenderpass.html#renderStates
93 | */
94 |
95 | shaderProgram: ShaderProgram {
96 | /*
97 | Here stores our shaders
98 | Using loadSource() to load shaders from local(will remote be possible?)
99 | Notice that URI should be completed
100 | */
101 |
102 | vertexShaderCode: loadSource(Resources.shader("hellotriangle.vert"))
103 | fragmentShaderCode: loadSource(Resources.shader("hellotriangle.frag"))
104 | }
105 | }
106 | }
107 | }
108 | }
109 |
110 | components: [geometry, material] // The object combines with geometry and material
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/qml/hellotriangle2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | Entity {
13 | id: plane
14 |
15 | GeometryRenderer {
16 | id: geometry
17 | // #TRYIT: Commenting this option will result in filling polygons
18 | primitiveType: GeometryRenderer.LineLoop // Currently linewidth=1 only
19 | geometry: Geometry {
20 | // Now we have two attributes
21 | boundingVolumePositionAttribute: position // Manually set bounding volume
22 |
23 | Attribute {
24 | id: position
25 | attributeType: Attribute.VertexAttribute
26 | vertexBaseType: Attribute.Float
27 | vertexSize: 3
28 | count: 4
29 | name: "position"
30 | buffer: Buffer {
31 | type: Buffer.VertexBuffer
32 | data: new Float32Array([
33 | 0.5, 0.5, 0.0, // Top Right
34 | 0.5, -0.5, 0.0, // Bottom Right
35 | -0.5, -0.5, 0.0, // Bottom Left
36 | -0.5, 0.5, 0.0, // Top Left
37 | ])
38 | }
39 | }
40 |
41 | Attribute {
42 | attributeType: Attribute.IndexAttribute
43 | vertexBaseType: Attribute.UnsignedShort
44 | vertexSize: 1
45 | count: 6
46 | buffer: Buffer {
47 | type: Buffer.IndexBuffer
48 | data: new Uint16Array([
49 | 0, 1, 3, // First Triangle
50 | 1, 2, 3, // Second Triangle
51 | ])
52 | }
53 | }
54 | }
55 | }
56 |
57 | Material {
58 | id: material
59 | effect: Effect {
60 | techniques: AutoTechnique {
61 | renderPasses: RenderPass {
62 | /*
63 | By default Qt3D culls back faces, here we need to disable it
64 | to render triangles in any vertex order
65 | */
66 |
67 | renderStates: // Source: qt3d/src/render/renderstates/renderstates.cpp ?
68 | CullFace {
69 | /*
70 | Cull face setting
71 | Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qcullface.html
72 | */
73 |
74 | mode: CullFace.NoCulling
75 | }
76 | shaderProgram: ShaderProgram {
77 | vertexShaderCode: loadSource(Resources.shader("hellotriangle.vert"))
78 | fragmentShaderCode: loadSource(Resources.shader("hellotriangle.frag"))
79 | }
80 | }
81 | }
82 | }
83 | }
84 |
85 | components: [geometry, material]
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/qml/hellowindow.qml:
--------------------------------------------------------------------------------
1 | import QtQuick.Scene3D 2.0
2 |
3 | import Qt3D.Core 2.0
4 | import Qt3D.Render 2.0
5 |
6 | Scene3D {
7 | /*
8 | Use Scene3D to create Qt3D scene in QtQuick
9 | See example: qthelp://org.qt-project.qt3d.570/qt3d/qt3d-scene3d-example.html
10 | Source:
11 | qt3d/src/quick3d/imports/scene3d/qtquickscene3dplugin.cpp:48
12 | qmlRegisterType(uri, 2, 0, "Scene3D");
13 | qt3d/src/quick3d/imports/scene3d/scene3ditem_p.h
14 |
15 | */
16 |
17 | id: scene
18 | height: 600
19 | width: 800
20 |
21 | Entity {
22 | // Everything starts with this Entity
23 | RenderSettings {
24 | // Setup one renderer output
25 | activeFrameGraph: Viewport {
26 | // to default Viewport
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/qml/hellowindow2.qml:
--------------------------------------------------------------------------------
1 | import QtQuick.Scene3D 2.0
2 |
3 | import Qt3D.Core 2.0
4 | import Qt3D.Render 2.0
5 |
6 | Scene3D {
7 | id: scene
8 | height: 600
9 | width: 800
10 |
11 | Entity {
12 | RenderSettings {
13 | activeFrameGraph: ClearBuffers {
14 | // ClearBuffers inherits from FrameGraphNode, as output framegraph
15 | buffers: ClearBuffers.ColorBuffer // To clear color buffer
16 | clearColor: Qt.rgba(0.2, 0.3, 0.3, 1.0) // = glClearColor
17 | RenderSurfaceSelector {
18 | // Default render output: window surface
19 | }
20 | }
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/qml/learnopengl-qml.qmlproject:
--------------------------------------------------------------------------------
1 | /* File generated by Qt Creator */
2 |
3 | import QmlProject 1.1
4 |
5 | Project {
6 | // mainFile: "app.qml"
7 |
8 | /* Include .qml, .js, and image files from current directory and subdirectories */
9 | QmlFiles {
10 | directory: "."
11 | }
12 | JavaScriptFiles {
13 | directory: "."
14 | }
15 | ImageFiles {
16 | directory: "../shared/assets"
17 | }
18 | Files {
19 | fileter: [ "*.vert", "*.frag" ]
20 | directory: "../shared/shaders"
21 | }
22 | /* List of plugin directories passed to QML runtime */
23 | importPaths: [
24 | "../qml-virtualkey"
25 | ]
26 | }
27 |
--------------------------------------------------------------------------------
/qml/main.qml:
--------------------------------------------------------------------------------
1 | /*
2 | * This is the start up QML for QML Creator
3 | */
4 |
5 | import QtQuick 2.9
6 | import QtQuick.Layouts 1.3
7 | import QtQuick.Controls 2.0
8 |
9 | import "Components"
10 |
11 | Item {
12 | id: main
13 | anchors.fill: parent
14 |
15 | QtObject {
16 | id: app
17 |
18 | function updateDt(dt) {
19 | fps3d.fps = (fps3d.fps * 7 + 1./dt) / 8;
20 | fps3d.text = (fps3d.fps).toFixed(1) + " fps";
21 | }
22 |
23 | }
24 |
25 | Loader {
26 | id: loader
27 | anchors.fill: parent
28 | sourceComponent: glInfo
29 |
30 | onLoaded: {
31 | focus = true;
32 | }
33 | }
34 |
35 | ComboBox {
36 | id: combo
37 | wheelEnabled: true
38 | background.y: 0 // disable extra background (since 5.8)
39 | background.height: height
40 | focusPolicy: Qt.NoFocus
41 | textRole: "text"
42 | width: parent.width
43 | anchors.top: parent.top
44 | model: Examples
45 | currentIndex: -1
46 |
47 | onCurrentIndexChanged: {
48 | load(model.get(currentIndex).source);
49 | }
50 | }
51 |
52 | // FpsItem {
53 | // id: fps2d
54 | // anchors.top: parent.top
55 | // anchors.topMargin: 8
56 | // anchors.left: parent.left
57 | // anchors.leftMargin: 8
58 | // spinnerSource: Resources.image("spinner.png")
59 | // }
60 |
61 | Text {
62 | id: fps3d
63 | anchors.right: parent.right
64 | anchors.rightMargin: 8
65 | anchors.top: combo.bottom
66 | anchors.topMargin: 8
67 | color: "#ff7e91"
68 | style: Text.Outline
69 | styleColor: "#7a2729"
70 | font.pointSize: 18
71 |
72 | property real fps: 60.
73 | }
74 |
75 | Component {
76 | id: glInfo
77 | Rectangle {
78 | color: "black"
79 | Text {
80 | color: "white"
81 | anchors.centerIn: parent
82 | text: "Open%4 %1.%2 %3".arg(
83 | GraphicsInfo.majorVersion).arg(
84 | GraphicsInfo.minorVersion).arg({
85 | 0: "NoProfile",
86 | 1: "CoreProfile",
87 | 2: "CompatibilityProfile"
88 | }[GraphicsInfo.profile]).arg({
89 | 0: "Unspecified",
90 | 1: "GL",
91 | 2: "GLES"
92 | }[GraphicsInfo.renderableType])
93 | styleColor: "#8b8b8b"
94 | style: Text.Sunken
95 | font.pointSize: 24
96 |
97 | onTextChanged: {
98 | Resources.setGlInfo(GraphicsInfo);
99 | }
100 | }
101 |
102 | AssetCheck {}
103 | }
104 | }
105 |
106 | function load(sourceName) {
107 | loader.source = sourceName + ".qml";
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/qml/model.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Extras 2.0
4 |
5 | import VirtualKey 1.0
6 |
7 | import "Components"
8 |
9 | Scene2 {
10 | id: scene
11 | children: VirtualKeys {
12 | target: scene
13 | gameButtonsEnabled: false
14 | color: "transparent"
15 | centerItem: RowKeys {
16 | keys: [
17 | {text:"Space", key:Qt.Key_Space},
18 | ]
19 | }
20 | }
21 |
22 | Entity {
23 | id: root
24 |
25 | property Texture2D diffuseMap: Texture2D {
26 | TextureImage {
27 | source: Resources.model("body_dif.png")
28 | }
29 | }
30 | property Texture2D normalMap: Texture2D {
31 | TextureImage {
32 | source: Resources.model("body_showroom_ddn.png")
33 | }
34 | }
35 | property Texture2D specularMap: Texture2D {
36 | TextureImage {
37 | source: Resources.model("body_showroom_spec.png")
38 | }
39 | }
40 |
41 | RenderInputSettings0 {
42 | id: renderInputSettings
43 |
44 | mouseSensitivity: .5 / Units.dp
45 | }
46 |
47 | Entity {
48 | id: plane
49 | components: [
50 | PlaneMesh {
51 | width: 100
52 | height: 100
53 | },
54 | Transform {
55 | translation: Qt.vector3d(0.0, -1.0, 0.0)
56 | },
57 | PhongMaterial {
58 | ambient: "black"
59 | diffuse: "coral"
60 | }
61 | ]
62 | }
63 |
64 | Entity {
65 | id: wireframeEntity
66 | components: [
67 | Mesh {
68 | source: Resources.model("nanosuit.obj")
69 | primitiveType: GeometryRenderer.Lines
70 | },
71 | Transform {
72 | translation: Qt.vector3d(-1.0, -1.0, 0.0)
73 | rotation: fromAxisAndAngle(Qt.vector3d(0.0 ,1.0 ,0.0), 30.0)
74 | scale: 0.1
75 | },
76 | NormalDiffuseSpecularMapMaterial {
77 | diffuse: root.diffuseMap
78 | normal: root.normalMap
79 | specular: root.specularMap
80 | }
81 | ]
82 | }
83 |
84 | Entity {
85 | id: meshEntity
86 | components: [
87 | Mesh {
88 | source: Resources.model("nanosuit.obj")
89 | },
90 | Transform {
91 | translation: Qt.vector3d(0.0, -1.0, 0.0)
92 | rotation: fromAxisAndAngle(Qt.vector3d(0.0 ,1.0 ,0.0), 0.0)
93 | scale: 0.1
94 | },
95 | NormalDiffuseSpecularMapMaterial {
96 | diffuse: root.diffuseMap
97 | normal: root.normalMap
98 | specular: root.specularMap
99 | }
100 | ]
101 | }
102 |
103 | Entity {
104 | id: modelEntity
105 | components: [
106 | SceneLoader {
107 | source: Resources.model("nanosuit.obj")
108 | },
109 | Transform {
110 | translation: Qt.vector3d(1.0, -1.0, 0.0)
111 | rotation: fromAxisAndAngle(Qt.vector3d(0.0, 1.0, 0.0), -30.0)
112 | scale: 0.1
113 | }
114 | ]
115 | }
116 |
117 | Entity {
118 | id: light
119 | components: DirectionalLight {
120 | color: "white"
121 | intensity: 3
122 | worldDirection: Qt.vector3d(0.0, -1.0, -1.0)
123 | }
124 | }
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/qml/shaders-exercise1.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene1 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | FrameSwap {}
13 |
14 | Entity {
15 | id: plane
16 |
17 | TrianglePlane0 {
18 | id: geometry
19 | }
20 |
21 | Material {
22 | id: material
23 | effect: Effect {
24 | techniques: AutoTechnique {
25 | renderPasses: RenderPass {
26 | renderStates: CullFace { mode: CullFace.NoCulling }
27 | shaderProgram: AutoShaderProgram {
28 | vertName: "shaders-exercise1"
29 | fragName: "shaders-uniform"
30 | }
31 | }
32 | }
33 | }
34 |
35 | parameters: Parameter {
36 | id: ourColor
37 | name: "ourColor"
38 | value: Qt.rgba(0, greenValue, 0, 1)
39 |
40 | property real greenValue: (Math.sin(time.value) / 2.) + .5
41 |
42 | Time {
43 | id: time
44 | }
45 | }
46 | }
47 |
48 | components: [geometry, material]
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/qml/shaders-exercise2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene1 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | FrameSwap {}
13 |
14 | Entity {
15 | id: plane
16 |
17 | TrianglePlane0 {
18 | id: geometry
19 | }
20 |
21 | Material {
22 | id: material
23 | effect: Effect {
24 | techniques: AutoTechnique {
25 | renderPasses: RenderPass {
26 | renderStates: CullFace { mode: CullFace.NoCulling }
27 | shaderProgram: AutoShaderProgram {
28 | vertName: "shaders-exercise2"
29 | fragName: "shaders-uniform"
30 | }
31 | }
32 | }
33 | }
34 |
35 | parameters: [
36 | Parameter {
37 | name: "ourColor"
38 | value: Qt.rgba(0, greenValue, 0, 1)
39 |
40 | property real greenValue: (Math.sin(time.value) / 2.) + .5
41 | },
42 | Parameter {
43 | name: "xOffset"
44 | value: Math.sin(time.value) / 2.
45 | }
46 | ]
47 | }
48 |
49 | components: [geometry, material]
50 | }
51 |
52 | Time {
53 | id: time
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/qml/shaders-exercise3.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | Entity {
13 | id: plane
14 |
15 | TrianglePlane0 {
16 | id: geometry
17 | }
18 |
19 | Material {
20 | id: material
21 | effect: Effect {
22 | techniques: AutoTechnique {
23 | renderPasses: RenderPass {
24 | shaderProgram: AutoShaderProgram {
25 | vertName: "shaders-exercise3"
26 | fragName: "shaders-interpolated"
27 | }
28 | }
29 | }
30 | }
31 | }
32 |
33 | components: [geometry, material]
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/qml/shaders-interpolated.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | Entity {
13 | id: plane
14 |
15 | GeometryRenderer {
16 | id: geometry
17 | geometry: Geometry {
18 | boundingVolumePositionAttribute: position
19 |
20 | Attribute {
21 | id: position
22 | attributeType: Attribute.VertexAttribute
23 | vertexBaseType: Attribute.Float
24 | vertexSize: 3
25 | count: 3
26 | byteOffset: 0
27 | byteStride: 6 * 4
28 | name: "position"
29 | buffer: vertexBuffer
30 | }
31 |
32 | Attribute {
33 | attributeType: Attribute.VertexAttribute
34 | vertexBaseType: Attribute.Float
35 | vertexSize: 3
36 | count: 3
37 | byteOffset: 3 * 4
38 | byteStride: 6 * 4
39 | name: "color"
40 | buffer: vertexBuffer
41 | }
42 | }
43 |
44 | Buffer {
45 | id: vertexBuffer
46 | type: Buffer.VertexBuffer
47 | data: new Float32Array([
48 | 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, // Bottom Right
49 | -0.5, -0.5, 0.0, 0.0, 1.0, 0.0, // Bottom Left
50 | 0.0, 0.5, 0.0, 0.0, 0.0, 1.0, // Top
51 | ])
52 | }
53 | }
54 |
55 | Material {
56 | id: material
57 | effect: Effect {
58 | techniques: AutoTechnique {
59 | renderPasses: RenderPass {
60 | renderStates: CullFace { mode: CullFace.NoCulling }
61 | shaderProgram: AutoShaderProgram {
62 | vertName: "shaders-uniform"
63 | fragName: "shaders-interpolated"
64 | }
65 | }
66 | }
67 | }
68 | }
69 |
70 | components: [geometry, material]
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/qml/shaders-uniform.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene1 {
7 | // Extra configs for FPS indicator
8 |
9 | Entity {
10 | id: root
11 |
12 | RenderSettings0 {}
13 |
14 | FrameSwap {} // My FPS counter injection
15 |
16 | Entity {
17 | id: plane
18 |
19 | TrianglePlane0 {
20 | id: geometry
21 | }
22 |
23 | Material {
24 | /*
25 | Shader uniform can be set in either:
26 | Material:
27 | qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qmaterial.html
28 | Effect:
29 | qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qeffect.html
30 | Technique:
31 | qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qtechnique.html
32 | RenderPass:
33 | qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qrenderpass.html
34 | But in different progress of rendering(see later examples).
35 | */
36 |
37 | id: material
38 | effect: Effect {
39 | techniques: AutoTechnique {
40 | renderPasses: RenderPass {
41 | shaderProgram: AutoShaderProgram {
42 | vertName: "shaders-uniform"
43 | fragName: "shaders-uniform"
44 | }
45 | }
46 | }
47 | }
48 |
49 | parameters: Parameter {
50 | id: ourColor
51 | name: "ourColor"
52 | value: Qt.rgba(0, greenValue, 0, 1)
53 |
54 | property real greenValue: (Math.sin(time.value) / 2.) + .5
55 |
56 | Time { // My time animation generator
57 | id: time
58 | }
59 | }
60 | }
61 |
62 | components: [geometry, material]
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/qml/tests/run_tests.sh:
--------------------------------------------------------------------------------
1 | rm -rf screenshots
2 | mkdir screenshots
3 | qmltestrunner -import ../../qml-virtualkey
4 |
--------------------------------------------------------------------------------
/qml/tests/utils.js:
--------------------------------------------------------------------------------
1 | .pragma library
2 |
3 | function getElementByCriteria(root, criteria) {
4 | var r = []
5 | for (var i in root.children) {
6 | var child = root.children[i]
7 | if (criteria(child))
8 | r.push(child)
9 | r = r.concat(getElementByCriteria(child, criteria))
10 | }
11 | return r
12 | }
13 |
--------------------------------------------------------------------------------
/qml/textures.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | Entity {
13 | id: plane
14 |
15 | GeometryRenderer {
16 | id: geometry
17 | geometry: Geometry {
18 | boundingVolumePositionAttribute: position
19 |
20 | Attribute {
21 | id: position
22 | attributeType: Attribute.VertexAttribute
23 | vertexBaseType: Attribute.Float
24 | vertexSize: 3
25 | count: 4
26 | byteOffset: 0
27 | byteStride: 8 * 4
28 | name: "position"
29 | buffer: vertexBuffer
30 | }
31 |
32 | Attribute {
33 | attributeType: Attribute.VertexAttribute
34 | vertexBaseType: Attribute.Float
35 | vertexSize: 3
36 | count: 4
37 | byteOffset: 3 * 4
38 | byteStride: 8 * 4
39 | name: "color"
40 | buffer: vertexBuffer
41 | }
42 |
43 | Attribute {
44 | attributeType: Attribute.VertexAttribute
45 | vertexBaseType: Attribute.Float
46 | vertexSize: 2
47 | count: 4
48 | byteOffset: 6 * 4
49 | byteStride: 8 * 4
50 | name: "texCoord"
51 | buffer: vertexBuffer
52 | }
53 |
54 | Attribute {
55 | attributeType: Attribute.IndexAttribute
56 | vertexBaseType: Attribute.UnsignedShort
57 | vertexSize: 1
58 | count: 6
59 | buffer: Buffer {
60 | type: Buffer.IndexBuffer
61 | data: new Uint16Array([
62 | 0, 1, 3, // First Triangle
63 | 1, 2, 3, // Second Triangle
64 | ])
65 | }
66 | }
67 | }
68 |
69 | Buffer {
70 | id: vertexBuffer
71 | type: Buffer.VertexBuffer
72 | data: new Float32Array([
73 | // Positions // Colors // Texture Coords
74 | 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
75 | 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
76 | -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
77 | -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left
78 | ])
79 | }
80 | }
81 |
82 | Material {
83 | id: material
84 | effect: Effect {
85 | techniques: AutoTechnique {
86 | renderPasses: RenderPass {
87 | renderStates: CullFace { mode: CullFace.NoCulling }
88 | shaderProgram: AutoShaderProgram {
89 | vertName: "texture"
90 | fragName: "texture"
91 | }
92 | }
93 | }
94 | }
95 |
96 | parameters: Parameter {
97 | name: "ourTexture"
98 | value: Texture2D {
99 | /*
100 | Any texture from AbstractTexture is available, we use 2D texture here
101 | Refer: qthelp://org.qt-project.qt3d.570/qt3d/qt3drender-qtexture2d.html
102 | #TRYIT: change filtering mode
103 | */
104 |
105 | generateMipMaps: true
106 | minificationFilter: Texture.Linear
107 | magnificationFilter: Texture.Linear
108 | wrapMode { // #TRYIT: change wrap mode
109 | x: WrapMode.Repeat
110 | y: WrapMode.Repeat
111 | }
112 | TextureImage {
113 | // Texture image here
114 |
115 | mipLevel: 0
116 | source: Resources.texture("container.jpg")
117 | }
118 | }
119 | }
120 | }
121 |
122 | components: [geometry, material]
123 | }
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/qml/textures2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 |
4 | import "Components"
5 |
6 | Scene0 {
7 | Entity {
8 | id: root
9 |
10 | RenderSettings0 {}
11 |
12 | Entity {
13 | id: plane
14 |
15 | GeometryRenderer {
16 | id: geometry
17 | geometry: Geometry {
18 | boundingVolumePositionAttribute: position
19 |
20 | Attribute {
21 | id: position
22 | attributeType: Attribute.VertexAttribute
23 | vertexBaseType: Attribute.Float
24 | vertexSize: 3
25 | count: 4
26 | byteOffset: 0
27 | byteStride: 8 * 4
28 | name: "position"
29 | buffer: vertexBuffer
30 | }
31 |
32 | Attribute {
33 | attributeType: Attribute.VertexAttribute
34 | vertexBaseType: Attribute.Float
35 | vertexSize: 3
36 | count: 4
37 | byteOffset: 3 * 4
38 | byteStride: 8 * 4
39 | name: "color"
40 | buffer: vertexBuffer
41 | }
42 |
43 | Attribute {
44 | attributeType: Attribute.VertexAttribute
45 | vertexBaseType: Attribute.Float
46 | vertexSize: 2
47 | count: 4
48 | byteOffset: 6 * 4
49 | byteStride: 8 * 4
50 | name: "texCoord"
51 | buffer: vertexBuffer
52 | }
53 |
54 | Attribute {
55 | attributeType: Attribute.IndexAttribute
56 | vertexBaseType: Attribute.UnsignedShort
57 | vertexSize: 1
58 | count: 6
59 | buffer: Buffer {
60 | type: Buffer.IndexBuffer
61 | data: new Uint16Array([
62 | 0, 1, 3, // First Triangle
63 | 1, 2, 3, // Second Triangle
64 | ])
65 | }
66 | }
67 | }
68 |
69 | Buffer {
70 | id: vertexBuffer
71 | type: Buffer.VertexBuffer
72 | data: new Float32Array([
73 | // Positions // Colors // Texture Coords
74 | 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
75 | 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
76 | -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
77 | -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left
78 | ])
79 | }
80 | }
81 |
82 | Material {
83 | id: material
84 | effect: Effect {
85 | techniques: AutoTechnique {
86 | renderPasses: RenderPass {
87 | renderStates: CullFace { mode: CullFace.NoCulling }
88 | shaderProgram: AutoShaderProgram {
89 | vertName: "texture"
90 | fragName: "texture2"
91 | }
92 | }
93 | }
94 | }
95 |
96 | parameters: Parameter {
97 | name: "ourTexture"
98 | value: Texture2D { // #TRYIT: change filtering mode
99 | generateMipMaps: true
100 | minificationFilter: Texture.Linear
101 | magnificationFilter: Texture.Linear
102 | wrapMode { // #TRYIT: change wrap mode
103 | x: WrapMode.Repeat
104 | y: WrapMode.Repeat
105 | }
106 | TextureImage {
107 | mipLevel: 0
108 | source: Resources.texture("container.jpg")
109 | }
110 | }
111 | }
112 | }
113 |
114 | components: [geometry, material]
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/qml/transformations-exercise1.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 |
5 | import VirtualKey 1.0
6 |
7 | import "Components"
8 |
9 | Scene2 {
10 | id: scene
11 | children: VirtualKeys {
12 | target: scene
13 | targetHandler: keyboardHandler
14 | padEnabled: false
15 | gameButtonsEnabled: false
16 | color: "transparent"
17 | centerItem: RowKeys {
18 | keys: [
19 | {text:"Space", key:Qt.Key_Space},
20 | ]
21 | }
22 | }
23 |
24 | Entity {
25 | id: root
26 |
27 | RenderSettings0 {}
28 |
29 | FrameSwap {}
30 |
31 | InputSettings {}
32 |
33 | KeyboardDevice {
34 | id: keyboardDevice
35 | }
36 |
37 | KeyboardHandler {
38 | id: keyboardHandler
39 | focus: true
40 | sourceDevice: keyboardDevice
41 | onSpacePressed: time.running = !time.running
42 | }
43 |
44 | Entity {
45 | id: plane
46 |
47 | TextureRectanglePlane0 {
48 | id: geometry
49 | }
50 |
51 | Material {
52 | id: material
53 | effect: Effect {
54 | techniques: AutoTechnique {
55 | renderPasses: RenderPass {
56 | renderStates: CullFace { mode: CullFace.NoCulling }
57 | shaderProgram: AutoShaderProgram {
58 | vertName: "transformations"
59 | fragName: "textures_combined"
60 | }
61 | }
62 | }
63 | }
64 |
65 | parameters: [
66 | Parameter {
67 | name: "ourTexture1"
68 | value: Texture2D {
69 | generateMipMaps: true
70 | minificationFilter: Texture.Linear
71 | magnificationFilter: Texture.Linear
72 | wrapMode {
73 | x: WrapMode.Repeat
74 | y: WrapMode.Repeat
75 | }
76 | TextureImage {
77 | mipLevel: 0
78 | source: Resources.texture("container.jpg")
79 | }
80 | }
81 | },
82 | Parameter {
83 | name: "ourTexture2"
84 | value: Texture2D {
85 | generateMipMaps: true
86 | minificationFilter: Texture.Linear
87 | magnificationFilter: Texture.Linear
88 | wrapMode {
89 | x: WrapMode.Repeat
90 | y: WrapMode.Repeat
91 | }
92 | TextureImage {
93 | mipLevel: 0
94 | source: Resources.texture("awesomeface.png")
95 | }
96 | }
97 | },
98 | Parameter {
99 | name: "transform"
100 | value: { // Order changed
101 | var m = Qt.matrix4x4();
102 | m.rotate(time.value % 360 * 50, Qt.vector3d(0, 0, 1));
103 | m.translate(Qt.vector3d(0.5, -0.5, 0.0));
104 | return m;
105 | }
106 |
107 | Time {
108 | id: time
109 | }
110 | }
111 | ]
112 | }
113 |
114 | components: [geometry, material]
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/qml/transformations-exercise2.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 |
5 | import VirtualKey 1.0
6 |
7 | import "Components"
8 |
9 | Scene2 {
10 | id: scene
11 | children: VirtualKeys {
12 | target: scene
13 | targetHandler: keyboardHandler
14 | padEnabled: false
15 | gameButtonsEnabled: false
16 | color: "transparent"
17 | centerItem: RowKeys {
18 | keys: [
19 | {text:"Space", key:Qt.Key_Space},
20 | ]
21 | }
22 | }
23 |
24 | Entity {
25 | id: root
26 |
27 | RenderSettings0 {}
28 |
29 | FrameSwap {}
30 |
31 | InputSettings {}
32 |
33 | KeyboardDevice {
34 | id: keyboardDevice
35 | }
36 |
37 | KeyboardHandler {
38 | id: keyboardHandler
39 | focus: true
40 | sourceDevice: keyboardDevice
41 | onSpacePressed: time.running = !time.running
42 | }
43 |
44 | Entity {
45 | id: plane
46 |
47 | TextureRectanglePlane0 {
48 | id: geometry
49 | }
50 |
51 | Material {
52 | id: material
53 | effect: Effect {
54 | techniques: AutoTechnique {
55 | renderPasses: [ // Two render passes
56 | RenderPass {
57 | renderStates: CullFace { mode: CullFace.NoCulling }
58 | shaderProgram: AutoShaderProgram {
59 | vertName: "transformations"
60 | fragName: "textures_combined"
61 | }
62 |
63 | parameters: [
64 | Parameter {
65 | name: "ourTexture1"
66 | value: Texture2D {
67 | generateMipMaps: true
68 | minificationFilter: Texture.Linear
69 | magnificationFilter: Texture.Linear
70 | wrapMode {
71 | x: WrapMode.Repeat
72 | y: WrapMode.Repeat
73 | }
74 | TextureImage {
75 | mipLevel: 0
76 | source: Resources.texture("container.jpg")
77 | }
78 | }
79 | },
80 | Parameter {
81 | name: "ourTexture2"
82 | value: Texture2D {
83 | generateMipMaps: true
84 | minificationFilter: Texture.Linear
85 | magnificationFilter: Texture.Linear
86 | wrapMode {
87 | x: WrapMode.Repeat
88 | y: WrapMode.Repeat
89 | }
90 | TextureImage {
91 | mipLevel: 0
92 | source: Resources.texture("awesomeface.png")
93 | }
94 | }
95 | },
96 | Parameter {
97 | name: "transform"
98 | value: {
99 | var m = Qt.matrix4x4();
100 | m.translate(Qt.vector3d(0.5, -0.5, 0));
101 | m.rotate(time.value % 360 * 10, Qt.vector3d(0, 0, 1));
102 | return m;
103 | }
104 | }
105 | ]
106 | },
107 | RenderPass {
108 | renderStates: CullFace { mode: CullFace.NoCulling }
109 | shaderProgram: AutoShaderProgram {
110 | vertName: "transformations"
111 | fragName: "textures_combined"
112 | }
113 |
114 | parameters: Parameter {
115 | name: "transform"
116 | value: {
117 | var m = Qt.matrix4x4();
118 | m.translate(Qt.vector3d(-0.5, 0.5, 0));
119 | m.scale(Math.sin(time.value));
120 | return m;
121 | }
122 | }
123 | }
124 | ]
125 | }
126 | }
127 | }
128 |
129 | components: [geometry, material]
130 | }
131 |
132 | Time {
133 | id: time
134 | }
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/qml/transformations.qml:
--------------------------------------------------------------------------------
1 | import Qt3D.Core 2.0
2 | import Qt3D.Render 2.0
3 | import Qt3D.Input 2.0
4 |
5 | import VirtualKey 1.0
6 |
7 | import "Components"
8 |
9 | Scene2 {
10 | id: scene
11 | children: VirtualKeys {
12 | target: scene
13 | targetHandler: keyboardHandler
14 | padEnabled: false
15 | gameButtonsEnabled: false
16 | color: "transparent"
17 | centerItem: RowKeys {
18 | keys: [
19 | {text:"Space", key:Qt.Key_Space},
20 | ]
21 | }
22 | }
23 |
24 | Entity {
25 | id: root
26 |
27 | RenderSettings0 {}
28 |
29 | FrameSwap {}
30 |
31 | InputSettings {}
32 |
33 | KeyboardDevice {
34 | id: keyboardDevice
35 | }
36 |
37 | KeyboardHandler {
38 | id: keyboardHandler
39 | focus: true
40 | sourceDevice: keyboardDevice
41 | onSpacePressed: time.running = !time.running
42 | }
43 |
44 | Entity {
45 | id: plane
46 |
47 | TextureRectanglePlane0 {
48 | id: geometry
49 | }
50 |
51 | Material {
52 | id: material
53 | effect: Effect {
54 | techniques: AutoTechnique {
55 | renderPasses: RenderPass {
56 | renderStates: CullFace { mode: CullFace.NoCulling }
57 | shaderProgram: AutoShaderProgram {
58 | vertName: "transformations"
59 | fragName: "textures_combined"
60 | }
61 | }
62 | }
63 | }
64 |
65 | parameters: [
66 | Parameter {
67 | name: "ourTexture1"
68 | value: Texture2D {
69 | generateMipMaps: true
70 | minificationFilter: Texture.Linear
71 | magnificationFilter: Texture.Linear
72 | wrapMode {
73 | x: WrapMode.Repeat
74 | y: WrapMode.Repeat
75 | }
76 | TextureImage {
77 | mipLevel: 0
78 | source: Resources.texture("container.jpg")
79 | }
80 | }
81 | },
82 | Parameter {
83 | name: "ourTexture2"
84 | value: Texture2D {
85 | generateMipMaps: true
86 | minificationFilter: Texture.Linear
87 | magnificationFilter: Texture.Linear
88 | wrapMode {
89 | x: WrapMode.Repeat
90 | y: WrapMode.Repeat
91 | }
92 | TextureImage {
93 | mipLevel: 0
94 | source: Resources.texture("awesomeface.png")
95 | }
96 | }
97 | },
98 | Parameter {
99 | name: "transform"
100 | value: { // We need to pass a matrix4x4
101 | var m = Qt.matrix4x4();
102 | m.translate(Qt.vector3d(0.5, -0.5, 0));
103 | m.rotate(time.value % 360 * 50, Qt.vector3d(0, 0, 1));
104 | return m;
105 | }
106 |
107 | Time {
108 | id: time
109 | }
110 | }
111 | ]
112 | }
113 |
114 | components: [geometry, material]
115 | }
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/shared/assets/texture/awesomeface.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/awesomeface.png
--------------------------------------------------------------------------------
/shared/assets/texture/blending_transparent_window.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/blending_transparent_window.png
--------------------------------------------------------------------------------
/shared/assets/texture/container.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/container.jpg
--------------------------------------------------------------------------------
/shared/assets/texture/container2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/container2.png
--------------------------------------------------------------------------------
/shared/assets/texture/container2_specular.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/container2_specular.png
--------------------------------------------------------------------------------
/shared/assets/texture/grass.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/grass.png
--------------------------------------------------------------------------------
/shared/assets/texture/lighting_maps_specular_color.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/lighting_maps_specular_color.png
--------------------------------------------------------------------------------
/shared/assets/texture/matrix.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MidoriYakumo/learnopengl-qt3d/c2031ed085f13fdbbf6c2ecf9d22a76dd3b734e8/shared/assets/texture/matrix.jpg
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting-exercise2.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 normal;
3 | varying vec3 fragViewPos;
4 | varying vec3 lightViewPos; // Extra in variable, since we need the light position in view space we calculate this in the vertex shader
5 |
6 | uniform vec3 lightColor;
7 | uniform vec3 objectColor;
8 |
9 | void main()
10 | {
11 | // Ambient
12 | float ambientStrength = .1;
13 | vec3 ambient = ambientStrength * lightColor;
14 |
15 | // Diffuse
16 | vec3 norm = normalize(normal);
17 | vec3 lightDir = normalize(lightViewPos - fragViewPos);
18 | float diff = max(dot(norm, lightDir), 0.);
19 | vec3 diffuse = diff * lightColor;
20 |
21 | // Specular
22 | float specularStrength = .5;
23 | vec3 viewDir = normalize(-fragViewPos);
24 | vec3 reflectDir = reflect(-lightDir, norm);
25 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
26 | vec3 specular = specularStrength * spec * lightColor;
27 |
28 | vec3 result = (ambient + diffuse + specular) * objectColor;
29 | gl_FragColor = vec4(result, 1.);
30 | }
31 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting-exercise2.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec3 vertexNormal;
4 |
5 | varying vec3 normal;
6 | varying vec3 fragViewPos;
7 | varying vec3 lightViewPos;
8 |
9 | uniform mat4 mvp;
10 | uniform mat3 modelViewNormal;
11 | uniform mat4 modelView;
12 | uniform mat4 viewMatrix;
13 |
14 | uniform vec3 lightPos; // We now define the uniform in the vertex shader and pass the 'view space' lightpos to the fragment shader. lightPos is currently in world space.
15 |
16 | void main()
17 | {
18 | gl_Position = mvp * vec4(vertexPosition, 1.);
19 | normal = modelViewNormal * vertexNormal;
20 | fragViewPos = vec3(modelView * vec4(vertexPosition, 1.));
21 | lightViewPos = vec3(viewMatrix * vec4(lightPos, 1.)); // Transform world-space light position to view-space light position
22 | }
23 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting-exercise3.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 lightingColor;
3 |
4 | uniform vec3 objectColor;
5 |
6 | void main()
7 | {
8 | gl_FragColor = vec4(lightingColor * objectColor, 1.);
9 | }
10 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting-exercise3.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec3 vertexNormal;
4 |
5 | varying vec3 lightingColor; // Resulting color from lighting calculations
6 |
7 | uniform mat4 mvp;
8 | uniform mat3 modelNormalMatrix;
9 | uniform mat4 modelMatrix;
10 |
11 | uniform vec3 lightPos;
12 | uniform vec3 viewPos;
13 | uniform vec3 lightColor;
14 | uniform vec3 objectColor;
15 |
16 | void main()
17 | {
18 | gl_Position = mvp * vec4(vertexPosition, 1.);
19 |
20 | // Gouraud Shading
21 | // ------------------------
22 | vec3 position = vec3(modelMatrix * vec4(vertexPosition, 1.));
23 | vec3 normal = modelNormalMatrix * vertexNormal;
24 |
25 | // Ambient
26 | float ambientStrength = .1;
27 | vec3 ambient = ambientStrength * lightColor;
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(lightPos - position);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = diff * lightColor;
34 |
35 | // Specular
36 | float specularStrength = 1.; // This is set higher to better show the effect of Gouraud shading
37 | vec3 viewDir = normalize(viewPos - position);
38 | vec3 reflectDir = reflect(-lightDir, norm);
39 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
40 | vec3 specular = specularStrength * spec * lightColor;
41 |
42 | lightingColor = ambient + diffuse + specular;
43 | }
44 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 normal;
3 | varying vec3 fragPos;
4 |
5 | uniform vec3 lightPos;
6 | uniform vec3 viewPos;
7 | uniform vec3 lightColor;
8 | uniform vec3 objectColor;
9 |
10 | void main()
11 | {
12 | // Ambient
13 | float ambientStrength = .1;
14 | vec3 ambient = ambientStrength * lightColor;
15 |
16 | // Diffuse
17 | vec3 norm = normalize(normal);
18 | vec3 lightDir = normalize(lightPos - fragPos);
19 | float diff = max(dot(norm, lightDir), 0.);
20 | vec3 diffuse = diff * lightColor;
21 |
22 | // Specular
23 | float specularStrength = .5;
24 | vec3 viewDir = normalize(viewPos - fragPos);
25 | vec3 reflectDir = reflect(-lightDir, norm);
26 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
27 | vec3 specular = specularStrength * spec * lightColor;
28 |
29 | vec3 result = (ambient + diffuse + specular) * objectColor;
30 | gl_FragColor = vec4(result, 1.);
31 | }
32 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec3 vertexNormal;
4 |
5 | varying vec3 normal;
6 | varying vec3 fragPos;
7 |
8 | uniform mat4 mvp;
9 | uniform mat3 modelNormalMatrix;
10 | uniform mat4 modelMatrix;
11 |
12 | void main()
13 | {
14 | gl_Position = mvp * vec4(vertexPosition, 1.);
15 | normal = modelNormalMatrix * vertexNormal;
16 | fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.));
17 | }
18 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting_diffuse.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 normal;
3 | varying vec3 fragPos;
4 |
5 | uniform vec3 lightPos;
6 | uniform vec3 lightColor;
7 | uniform vec3 objectColor;
8 |
9 | void main()
10 | {
11 | // Ambient
12 | float ambientStrength = .1;
13 | vec3 ambient = ambientStrength * lightColor;
14 |
15 | // Diffuse
16 | vec3 norm = normalize(normal);
17 | vec3 lightDir = normalize(lightPos - fragPos);
18 | float diff = max(dot(norm, lightDir), 0.);
19 | vec3 diffuse = diff * lightColor;
20 |
21 | vec3 result = (ambient + diffuse) * objectColor;
22 | gl_FragColor = vec4(result, 1.);
23 | }
24 |
--------------------------------------------------------------------------------
/shared/shaders/es20/basic_lighting_gouraud.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec3 vertexNormal;
4 | attribute vec2 vertexTexCoord;
5 |
6 | varying vec3 ourColor;
7 |
8 | uniform mat4 mvp;
9 | uniform mat3 modelNormalMatrix;
10 | uniform mat4 modelMatrix;
11 |
12 | uniform vec3 lightPos;
13 | uniform vec3 viewPos;
14 | uniform vec3 lightColor;
15 | uniform vec3 objectColor;
16 |
17 | void main()
18 | {
19 | vec3 position = vec3(modelMatrix * vec4(vertexPosition, 1.));
20 | vec3 normal = modelNormalMatrix * vertexNormal;
21 |
22 | // Ambient
23 | float ambientStrength = .1;
24 | vec3 ambient = ambientStrength * lightColor;
25 |
26 | // Diffuse
27 | vec3 norm = normalize(normal);
28 | vec3 lightDir = normalize(lightPos - position);
29 | float diff = max(dot(norm, lightDir), 0.);
30 | vec3 diffuse = diff * lightColor;
31 |
32 | // Specular
33 | float specularStrength = .5;
34 | vec3 viewDir = normalize(viewPos - position);
35 | vec3 reflectDir = reflect(-lightDir, norm);
36 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
37 | vec3 specular = specularStrength * spec * lightColor;
38 |
39 | ourColor = (ambient + diffuse + specular) * objectColor;
40 | gl_Position = mvp * vec4(vertexPosition, 1.);
41 | }
42 |
--------------------------------------------------------------------------------
/shared/shaders/es20/checkerboard.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec2 texCoord;
3 |
4 | void main()
5 | {
6 | float ckSize = 1./9.;
7 | int ckValue =
8 | ((mod(texCoord.x, ckSize*2.) > ckSize)?0:1) +
9 | ((mod(texCoord.y, ckSize*2.) > ckSize)?0:1);
10 | vec3 color = vec3(ckValue/3.);
11 | gl_FragColor = vec4(color, 1.);
12 | }
13 |
--------------------------------------------------------------------------------
/shared/shaders/es20/coordinate_systems.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec2 texCoord;
4 |
5 | varying vec2 TexCoord;
6 |
7 | uniform mat4 model;
8 | uniform mat4 view;
9 | uniform mat4 projection;
10 |
11 | void main()
12 | {
13 | gl_Position = projection * view * model * vec4(position, 1.);
14 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/es20/coordinate_systems_qt3d_transform.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec2 texCoord;
4 |
5 | varying vec2 TexCoord;
6 |
7 | uniform mat4 modelMatrix;
8 | uniform mat4 view;
9 | uniform mat4 projection;
10 |
11 | void main()
12 | {
13 | gl_Position = projection * view * modelMatrix * vec4(position, 1.);
14 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/es20/depth_visualize.frag:
--------------------------------------------------------------------------------
1 |
2 | uniform float nearZ;
3 | uniform float farZ;
4 |
5 | float LinearizeDepth(float depth)
6 | {
7 | float z = depth * 2. - 1.;
8 | return (2. * nearZ * farZ) / (farZ + nearZ - z * (farZ - nearZ));
9 | }
10 |
11 | void main()
12 | {
13 | float depth = LinearizeDepth(gl_FragCoord.z) / farZ;
14 | gl_FragColor = vec4(vec3(depth), 1.);
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/es20/hellotriangle.frag:
--------------------------------------------------------------------------------
1 |
2 | void main() {
3 | gl_FragColor = vec4(1., .5, .2, 1.);
4 | }
5 |
--------------------------------------------------------------------------------
/shared/shaders/es20/hellotriangle.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 |
4 | void main() {
5 | gl_Position = vec4(position, 1.);
6 | }
7 |
--------------------------------------------------------------------------------
/shared/shaders/es20/light_casters_directional.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 direction;
10 |
11 | vec3 ambient;
12 | vec3 diffuse;
13 | vec3 specular;
14 | };
15 |
16 | varying vec3 normal;
17 | varying vec3 fragPos;
18 | varying vec2 texCoord;
19 |
20 | uniform vec3 viewPos;
21 | uniform Material material;
22 | uniform Light light;
23 |
24 | void main()
25 | {
26 | // Ambient
27 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(-light.direction);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
34 |
35 | // Specular
36 | vec3 viewDir = normalize(viewPos - fragPos);
37 | vec3 reflectDir = reflect(-lightDir, norm);
38 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
39 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
40 |
41 | vec3 result = ambient + diffuse + specular;
42 | gl_FragColor = vec4(result, 1.);
43 | }
44 |
--------------------------------------------------------------------------------
/shared/shaders/es20/light_casters_point.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 position;
10 |
11 | vec3 ambient;
12 | vec3 diffuse;
13 | vec3 specular;
14 |
15 | float constant;
16 | float linear;
17 | float quadratic;
18 | };
19 |
20 | varying vec3 normal;
21 | varying vec3 fragPos;
22 | varying vec2 texCoord;
23 |
24 | uniform vec3 viewPos;
25 | uniform Material material;
26 | uniform Light light;
27 |
28 | void main()
29 | {
30 | vec3 vecToLight = light.position - fragPos;
31 |
32 | // Ambient
33 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
34 |
35 | // Diffuse
36 | vec3 norm = normalize(normal);
37 | vec3 lightDir = normalize(vecToLight);
38 | float diff = max(dot(norm, lightDir), 0.);
39 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
40 |
41 | // Specular
42 | vec3 viewDir = normalize(viewPos - fragPos);
43 | vec3 reflectDir = reflect(-lightDir, norm);
44 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
45 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
46 |
47 | // Attenuation
48 | float distance = length(vecToLight);
49 | float attenuation = 1. / (light.constant + distance * (light.linear + distance * light.quadratic));
50 |
51 | vec3 result = ambient + (diffuse + specular) * attenuation;
52 | gl_FragColor = vec4(result, 1.);
53 | }
54 |
--------------------------------------------------------------------------------
/shared/shaders/es20/light_casters_spotlight_hard.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 position;
10 | vec3 direction;
11 | float cutOff;
12 |
13 | vec3 ambient;
14 | vec3 diffuse;
15 | vec3 specular;
16 |
17 | float constant;
18 | float linear;
19 | float quadratic;
20 | };
21 |
22 | varying vec3 normal;
23 | varying vec3 fragPos;
24 | varying vec2 texCoord;
25 |
26 | uniform vec3 viewPos;
27 | uniform Material material;
28 | uniform Light light;
29 |
30 | void main()
31 | {
32 | vec3 vecToLight = light.position - fragPos;
33 | vec3 lightDir = normalize(vecToLight);
34 | float theta = dot(lightDir, normalize(-light.direction));
35 |
36 | // Ambient
37 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
38 | vec3 result = ambient;
39 |
40 | if (theta > light.cutOff) {
41 | // Diffuse
42 | vec3 norm = normalize(normal);
43 | float diff = max(dot(norm, lightDir), 0.);
44 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
45 |
46 | // Specular
47 | vec3 viewDir = normalize(viewPos - fragPos);
48 | vec3 reflectDir = reflect(-lightDir, norm);
49 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
50 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
51 |
52 | // Attenuation
53 | float distance = length(vecToLight);
54 | float attenuation = 1. / (light.constant + distance * (light.linear + distance * light.quadratic));
55 |
56 | result += (diffuse + specular) * attenuation;
57 | }
58 |
59 | gl_FragColor = vec4(result, 1.);
60 | }
61 |
--------------------------------------------------------------------------------
/shared/shaders/es20/light_casters_spotlight_soft.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 position;
10 | vec3 direction;
11 | float cutOff;
12 | float outerCutOff;
13 |
14 | vec3 ambient;
15 | vec3 diffuse;
16 | vec3 specular;
17 |
18 | float constant;
19 | float linear;
20 | float quadratic;
21 | };
22 |
23 | varying vec3 normal;
24 | varying vec3 fragPos;
25 | varying vec2 texCoord;
26 |
27 | uniform vec3 viewPos;
28 | uniform Material material;
29 | uniform Light light;
30 |
31 | void main()
32 | {
33 | vec3 vecToLight = light.position - fragPos;
34 |
35 | // Ambient
36 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
37 | vec3 result = ambient;
38 |
39 | // Diffuse
40 | vec3 norm = normalize(normal);
41 | vec3 lightDir = normalize(vecToLight);
42 | float diff = max(dot(norm, lightDir), 0.);
43 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
44 |
45 | // Specular
46 | vec3 viewDir = normalize(viewPos - fragPos);
47 | vec3 reflectDir = reflect(-lightDir, norm);
48 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
49 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
50 |
51 | // Spotlight (soft edges)
52 | float theta = dot(lightDir, normalize(-light.direction));
53 | float intensity = smoothstep(light.outerCutOff, light.cutOff, theta);
54 |
55 | // Attenuation
56 | float distance = length(vecToLight);
57 | float attenuation = 1. / (light.constant + distance * (light.linear + distance * light.quadratic));
58 |
59 | result += (diffuse + specular) * intensity * attenuation;
60 |
61 | gl_FragColor = vec4(result, 1.);
62 | }
63 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting.frag:
--------------------------------------------------------------------------------
1 |
2 | uniform vec3 objectColor;
3 | uniform vec3 lightColor;
4 |
5 | void main()
6 | {
7 | gl_FragColor = vec4(lightColor * objectColor, 1.);
8 | }
9 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec2 vertexTexCoord;
4 |
5 | varying vec2 TexCoord;
6 |
7 | uniform mat4 modelMatrix;
8 | uniform mat4 viewMatrix;
9 | uniform mat4 projectionMatrix;
10 |
11 | void main()
12 | {
13 | gl_Position = projectionMatrix * viewMatrix * modelMatrix *
14 | vec4(vertexPosition, 1.);
15 | TexCoord = vec2(vertexTexCoord.x, 1. - vertexTexCoord.y);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting_maps-exercise2.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 position;
10 |
11 | vec3 ambient;
12 | vec3 diffuse;
13 | vec3 specular;
14 | };
15 |
16 | varying vec3 normal;
17 | varying vec3 fragPos;
18 | varying vec2 texCoord;
19 |
20 | uniform vec3 viewPos;
21 | uniform Material material;
22 | uniform Light light;
23 |
24 | void main()
25 | {
26 | // Ambient
27 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(light.position - fragPos);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
34 |
35 | // Specular
36 | vec3 viewDir = normalize(viewPos - fragPos);
37 | vec3 reflectDir = reflect(-lightDir, norm);
38 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
39 | vec3 specular = light.specular * spec *
40 | (vec3(1.) - vec3(texture2D(material.specular, texCoord)));
41 |
42 | vec3 result = ambient + diffuse + specular;
43 | gl_FragColor = vec4(result, 1.);
44 | }
45 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting_maps-exercise4.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | sampler2D emission;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 | };
16 |
17 | varying vec3 normal;
18 | varying vec3 fragPos;
19 | varying vec2 texCoord;
20 |
21 | uniform float time;
22 | uniform vec3 viewPos;
23 | uniform Material material;
24 | uniform Light light;
25 |
26 | void main()
27 | {
28 | // Ambient
29 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
30 |
31 | // Diffuse
32 | vec3 norm = normalize(normal);
33 | vec3 lightDir = normalize(light.position - fragPos);
34 | float diff = max(dot(norm, lightDir), 0.);
35 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
36 |
37 | // Specular
38 | vec3 viewDir = normalize(viewPos - fragPos);
39 | vec3 reflectDir = reflect(-lightDir, norm);
40 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
41 | vec3 specColor = vec3(texture2D(material.specular, texCoord));
42 | vec3 specular = light.specular * spec * specColor;
43 |
44 | // Emission
45 | vec3 emission = length(specColor) < 1e-2 ?
46 | vec3(texture2D(material.emission, texCoord + vec2(0., time))) : vec3(0.);
47 |
48 | vec3 result = ambient + diffuse + specular + emission;
49 | gl_FragColor = vec4(result, 1.);
50 | }
51 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting_maps.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec3 vertexNormal;
4 | attribute vec2 vertexTexCoord;
5 |
6 | varying vec3 normal;
7 | varying vec3 fragPos;
8 | varying vec2 texCoord;
9 |
10 | uniform mat4 mvp;
11 | uniform mat3 modelNormalMatrix;
12 | uniform mat4 modelMatrix;
13 |
14 | void main()
15 | {
16 | gl_Position = mvp * vec4(vertexPosition, 1.);
17 | normal = modelNormalMatrix * vertexNormal;
18 | fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.));
19 | texCoord = vertexTexCoord;
20 | }
21 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting_maps_diffuse.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | vec3 specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 position;
10 |
11 | vec3 ambient;
12 | vec3 diffuse;
13 | vec3 specular;
14 | };
15 |
16 | varying vec3 normal;
17 | varying vec3 fragPos;
18 | varying vec2 texCoord;
19 |
20 | uniform vec3 viewPos;
21 | uniform Material material;
22 | uniform Light light;
23 |
24 | void main()
25 | {
26 | // Ambient
27 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(light.position - fragPos);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
34 |
35 | // Specular
36 | vec3 viewDir = normalize(viewPos - fragPos);
37 | vec3 reflectDir = reflect(-lightDir, norm);
38 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
39 | vec3 specular = light.specular * (spec * material.specular);
40 |
41 | vec3 result = ambient + diffuse + specular;
42 | gl_FragColor = vec4(result, 1.);
43 | }
44 |
--------------------------------------------------------------------------------
/shared/shaders/es20/lighting_maps_specular.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | sampler2D diffuse;
4 | sampler2D specular;
5 | float shininess;
6 | };
7 |
8 | struct Light {
9 | vec3 position;
10 |
11 | vec3 ambient;
12 | vec3 diffuse;
13 | vec3 specular;
14 | };
15 |
16 | varying vec3 normal;
17 | varying vec3 fragPos;
18 | varying vec2 texCoord;
19 |
20 | uniform vec3 viewPos;
21 | uniform Material material;
22 | uniform Light light;
23 |
24 | void main()
25 | {
26 | // Ambient
27 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(light.position - fragPos);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
34 |
35 | // Specular
36 | vec3 viewDir = normalize(viewPos - fragPos);
37 | vec3 reflectDir = reflect(-lightDir, norm);
38 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
39 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
40 |
41 | vec3 result = ambient + diffuse + specular;
42 | gl_FragColor = vec4(result, 1.);
43 | }
44 |
--------------------------------------------------------------------------------
/shared/shaders/es20/materials.frag:
--------------------------------------------------------------------------------
1 |
2 | struct Material {
3 | vec3 ambient;
4 | vec3 diffuse;
5 | vec3 specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 | };
16 |
17 | varying vec3 normal;
18 | varying vec3 fragPos;
19 |
20 | uniform vec3 viewPos;
21 | uniform Material material;
22 | uniform Light light;
23 |
24 | void main()
25 | {
26 | // Ambient
27 | vec3 ambient = light.ambient * material.ambient;
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(light.position - fragPos);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = light.diffuse * (diff * material.diffuse);
34 |
35 | // Specular
36 | vec3 viewDir = normalize(viewPos - fragPos);
37 | vec3 reflectDir = reflect(-lightDir, norm);
38 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
39 | vec3 specular = light.specular * (spec * material.specular);
40 |
41 | vec3 result = ambient + diffuse + specular;
42 | gl_FragColor = vec4(result, 1.);
43 | }
44 |
--------------------------------------------------------------------------------
/shared/shaders/es20/outline.frag:
--------------------------------------------------------------------------------
1 | varying vec2 texCoord;
2 |
3 | void main()
4 | {
5 | vec2 pos = texCoord;
6 | if (pos.x > .5) pos.x = 1.- pos.x;
7 | if (pos.y > .5) pos.y = 1.- pos.y;
8 | float border = min(pos.x, pos.y);
9 | gl_FragColor = vec4(texCoord*border, border, border);
10 | gl_FragColor = vec4(0.5, 0.8, 1.0, 1.0);
11 | }
12 |
--------------------------------------------------------------------------------
/shared/shaders/es20/outline.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec2 vertexTexCoord;
4 |
5 | varying vec2 texCoord;
6 |
7 | uniform mat4 mvp;
8 | uniform float outlineRatio;
9 |
10 | void main()
11 | {
12 | gl_Position = mvp * vec4(vertexPosition * (1. + outlineRatio), 1.);
13 | texCoord = vertexTexCoord;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/es20/passthrough.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 vertexPosition;
3 | attribute vec2 vertexTexCoord;
4 |
5 | varying vec2 texCoord;
6 |
7 | uniform mat4 mvp;
8 |
9 | void main()
10 | {
11 | gl_Position = mvp * vec4(vertexPosition, 1.);
12 | texCoord = vertexTexCoord;
13 | }
14 |
--------------------------------------------------------------------------------
/shared/shaders/es20/shaders-exercise1.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 |
5 | varying vec3 ourColor;
6 |
7 | void main() {
8 | gl_Position = vec4(position.x, -position.y, position.z, 1.);
9 | ourColor = color;
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/es20/shaders-exercise2.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 |
5 | varying vec3 ourColor;
6 |
7 | uniform float xOffset;
8 |
9 | void main() {
10 | gl_Position = vec4(position.x + xOffset, position.y, position.z, 1.);
11 | ourColor = color;
12 | }
13 |
--------------------------------------------------------------------------------
/shared/shaders/es20/shaders-exercise3.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 |
5 | varying vec3 ourColor;
6 |
7 | void main() {
8 | gl_Position = vec4(position, 1.);
9 | ourColor = position;
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/es20/shaders-interpolated.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 ourColor;
3 |
4 | void main()
5 | {
6 | gl_FragColor = vec4(ourColor, 1.);
7 | }
8 |
--------------------------------------------------------------------------------
/shared/shaders/es20/shaders-uniform.frag:
--------------------------------------------------------------------------------
1 |
2 | uniform vec4 ourColor;
3 |
4 | void main() {
5 | gl_FragColor = ourColor;
6 | }
7 |
--------------------------------------------------------------------------------
/shared/shaders/es20/shaders-uniform.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 |
5 | varying vec3 ourColor;
6 |
7 | void main() {
8 | gl_Position = vec4(position, 1.);
9 | ourColor = color;
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/es20/texture.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 ourColor;
3 | varying vec2 TexCoord;
4 |
5 | uniform sampler2D ourTexture;
6 |
7 | void main()
8 | {
9 | gl_FragColor = texture2D(ourTexture, TexCoord);
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/es20/texture.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 | attribute vec2 texCoord;
5 |
6 | varying vec3 ourColor;
7 | varying vec2 TexCoord;
8 |
9 | void main()
10 | {
11 | gl_Position = vec4(position, 1.);
12 | ourColor = color;
13 | TexCoord = texCoord;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/es20/texture2.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 ourColor;
3 | varying vec2 TexCoord;
4 |
5 | uniform sampler2D ourTexture;
6 |
7 | void main()
8 | {
9 | gl_FragColor = texture2D(ourTexture, TexCoord) * vec4(ourColor, 1.);
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/es20/textures-exercise1.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 ourColor;
3 | varying vec2 TexCoord;
4 |
5 | uniform sampler2D ourTexture1;
6 | uniform sampler2D ourTexture2;
7 |
8 | void main()
9 | {
10 | gl_FragColor = mix(texture2D(ourTexture1, TexCoord), texture2D(ourTexture2, vec2(1. - TexCoord.x, TexCoord.y)), .2);
11 | }
12 |
--------------------------------------------------------------------------------
/shared/shaders/es20/textures-exercise4.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 ourColor;
3 | varying vec2 TexCoord;
4 |
5 | uniform float mixValue;
6 |
7 | // Texture samplers
8 | uniform sampler2D ourTexture1;
9 | uniform sampler2D ourTexture2;
10 |
11 | void main()
12 | {
13 | // Linearly interpolate between both textures (second texture is only slightly combined)
14 | gl_FragColor = mix(texture2D(ourTexture1, TexCoord), texture2D(ourTexture2, TexCoord), mixValue);
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/es20/textures_combined.frag:
--------------------------------------------------------------------------------
1 |
2 | varying vec3 ourColor;
3 | varying vec2 TexCoord;
4 |
5 | // Texture samplers
6 | uniform sampler2D ourTexture1;
7 | uniform sampler2D ourTexture2;
8 |
9 | void main()
10 | {
11 | // Linearly interpolate between both textures (second texture is only slightly combined)
12 | gl_FragColor = mix(texture2D(ourTexture1, TexCoord), texture2D(ourTexture2, TexCoord), .2);
13 | }
14 |
--------------------------------------------------------------------------------
/shared/shaders/es20/textures_combined.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 | attribute vec2 texCoord;
5 |
6 | varying vec3 ourColor;
7 | varying vec2 TexCoord;
8 |
9 | void main()
10 | {
11 | gl_Position = vec4(position, 1.);
12 | ourColor = color;
13 | // We swap the y-axis by substracing our coordinates from 1. This is done because most images have the top y-axis inversed with OpenGL's top y-axis.
14 | // TexCoord = texCoord;
15 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/es20/transformations.vert:
--------------------------------------------------------------------------------
1 |
2 | attribute vec3 position;
3 | attribute vec3 color;
4 | attribute vec2 texCoord;
5 |
6 | varying vec3 ourColor;
7 | varying vec2 TexCoord;
8 |
9 | uniform mat4 transform;
10 |
11 | void main()
12 | {
13 | gl_Position = transform * vec4(position, 1.);
14 | ourColor = color;
15 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting-exercise2.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 normal;
9 | in vec3 fragViewPos;
10 | in vec3 lightViewPos; // Extra in variable, since we need the light position in view space we calculate this in the vertex shader
11 |
12 | out vec4 color;
13 |
14 | uniform vec3 lightColor;
15 | uniform vec3 objectColor;
16 |
17 | void main()
18 | {
19 | // Ambient
20 | float ambientStrength = .1;
21 | vec3 ambient = ambientStrength * lightColor;
22 |
23 | // Diffuse
24 | vec3 norm = normalize(normal);
25 | vec3 lightDir = normalize(lightViewPos - fragViewPos);
26 | float diff = max(dot(norm, lightDir), 0.);
27 | vec3 diffuse = diff * lightColor;
28 |
29 | // Specular
30 | float specularStrength = .5;
31 | vec3 viewDir = normalize(-fragViewPos);
32 | vec3 reflectDir = reflect(-lightDir, norm);
33 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
34 | vec3 specular = specularStrength * spec * lightColor;
35 |
36 | vec3 result = (ambient + diffuse + specular) * objectColor;
37 | color = vec4(result, 1.);
38 | }
39 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting-exercise2.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec3 vertexNormal;
8 |
9 | out vec3 normal;
10 | out vec3 fragViewPos;
11 | out vec3 lightViewPos;
12 |
13 | uniform mat4 mvp;
14 | uniform mat3 modelViewNormal;
15 | uniform mat4 modelView;
16 | uniform mat4 viewMatrix;
17 |
18 | uniform vec3 lightPos; // We now define the uniform in the vertex shader and pass the 'view space' lightpos to the fragment shader. lightPos is currently in world space.
19 |
20 | void main()
21 | {
22 | gl_Position = mvp * vec4(vertexPosition, 1.);
23 | normal = modelViewNormal * vertexNormal;
24 | fragViewPos = vec3(modelView * vec4(vertexPosition, 1.));
25 | lightViewPos = vec3(viewMatrix * vec4(lightPos, 1.)); // Transform world-space light position to view-space light position
26 | }
27 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting-exercise3.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 lightingColor;
9 |
10 | out vec4 color;
11 |
12 | uniform vec3 objectColor;
13 |
14 | void main()
15 | {
16 | color = vec4(lightingColor * objectColor, 1.);
17 | }
18 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting-exercise3.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec3 vertexNormal;
8 |
9 | out vec3 lightingColor; // Resulting color from lighting calculations
10 |
11 | uniform mat4 mvp;
12 | uniform mat3 modelNormalMatrix;
13 | uniform mat4 modelMatrix;
14 |
15 | uniform vec3 lightPos;
16 | uniform vec3 viewPos;
17 | uniform vec3 lightColor;
18 | uniform vec3 objectColor;
19 |
20 | void main()
21 | {
22 | gl_Position = mvp * vec4(vertexPosition, 1.);
23 |
24 | // Gouraud Shading
25 | // ------------------------
26 | vec3 position = vec3(modelMatrix * vec4(vertexPosition, 1.));
27 | vec3 normal = modelNormalMatrix * vertexNormal;
28 |
29 | // Ambient
30 | float ambientStrength = .1;
31 | vec3 ambient = ambientStrength * lightColor;
32 |
33 | // Diffuse
34 | vec3 norm = normalize(normal);
35 | vec3 lightDir = normalize(lightPos - position);
36 | float diff = max(dot(norm, lightDir), 0.);
37 | vec3 diffuse = diff * lightColor;
38 |
39 | // Specular
40 | float specularStrength = 1.; // This is set higher to better show the effect of Gouraud shading
41 | vec3 viewDir = normalize(viewPos - position);
42 | vec3 reflectDir = reflect(-lightDir, norm);
43 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
44 | vec3 specular = specularStrength * spec * lightColor;
45 |
46 | lightingColor = ambient + diffuse + specular;
47 | }
48 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 normal;
9 | in vec3 fragPos;
10 |
11 | out vec4 color;
12 |
13 | uniform vec3 lightPos;
14 | uniform vec3 viewPos;
15 | uniform vec3 lightColor;
16 | uniform vec3 objectColor;
17 |
18 | void main()
19 | {
20 | // Ambient
21 | float ambientStrength = .1;
22 | vec3 ambient = ambientStrength * lightColor;
23 |
24 | // Diffuse
25 | vec3 norm = normalize(normal);
26 | vec3 lightDir = normalize(lightPos - fragPos);
27 | float diff = max(dot(norm, lightDir), 0.);
28 | vec3 diffuse = diff * lightColor;
29 |
30 | // Specular
31 | float specularStrength = .5;
32 | vec3 viewDir = normalize(viewPos - fragPos);
33 | vec3 reflectDir = reflect(-lightDir, norm);
34 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
35 | vec3 specular = specularStrength * spec * lightColor;
36 |
37 | vec3 result = (ambient + diffuse + specular) * objectColor;
38 | color = vec4(result, 1.);
39 | }
40 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec3 vertexNormal;
8 |
9 | out vec3 normal;
10 | out vec3 fragPos;
11 |
12 | uniform mat4 mvp;
13 | uniform mat3 modelNormalMatrix;
14 | uniform mat4 modelMatrix;
15 |
16 | void main()
17 | {
18 | gl_Position = mvp * vec4(vertexPosition, 1.);
19 | normal = modelNormalMatrix * vertexNormal;
20 | fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.));
21 | }
22 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting_diffuse.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 normal;
9 | in vec3 fragPos;
10 |
11 | out vec4 color;
12 |
13 | uniform vec3 lightPos;
14 | uniform vec3 lightColor;
15 | uniform vec3 objectColor;
16 |
17 | void main()
18 | {
19 | // Ambient
20 | float ambientStrength = .1;
21 | vec3 ambient = ambientStrength * lightColor;
22 |
23 | // Diffuse
24 | vec3 norm = normalize(normal);
25 | vec3 lightDir = normalize(lightPos - fragPos);
26 | float diff = max(dot(norm, lightDir), 0.);
27 | vec3 diffuse = diff * lightColor;
28 |
29 | vec3 result = (ambient + diffuse) * objectColor;
30 | color = vec4(result, 1.);
31 | }
32 |
--------------------------------------------------------------------------------
/shared/shaders/es30/basic_lighting_gouraud.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec3 vertexNormal;
8 |
9 | out vec3 ourColor;
10 |
11 | uniform mat4 mvp;
12 | uniform mat3 modelNormalMatrix;
13 | uniform mat4 modelMatrix;
14 |
15 | uniform vec3 lightPos;
16 | uniform vec3 viewPos;
17 | uniform vec3 lightColor;
18 | uniform vec3 objectColor;
19 |
20 | void main()
21 | {
22 | vec3 position = vec3(modelMatrix * vec4(vertexPosition, 1.));
23 | vec3 normal = modelNormalMatrix * vertexNormal;
24 |
25 | // Ambient
26 | float ambientStrength = .1;
27 | vec3 ambient = ambientStrength * lightColor;
28 |
29 | // Diffuse
30 | vec3 norm = normalize(normal);
31 | vec3 lightDir = normalize(lightPos - position);
32 | float diff = max(dot(norm, lightDir), 0.);
33 | vec3 diffuse = diff * lightColor;
34 |
35 | // Specular
36 | float specularStrength = .5;
37 | vec3 viewDir = normalize(viewPos - position);
38 | vec3 reflectDir = reflect(-lightDir, norm);
39 | float spec = pow(max(dot(viewDir, reflectDir), 0.), 32.);
40 | vec3 specular = specularStrength * spec * lightColor;
41 |
42 | ourColor = (ambient + diffuse + specular) * objectColor;
43 | gl_Position = mvp * vec4(vertexPosition, 1.);
44 | }
45 |
--------------------------------------------------------------------------------
/shared/shaders/es30/checkerboard.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | float CkSize = 1.0f/9.0f;
9 |
10 | in vec2 texCoord;
11 |
12 | out vec4 color;
13 |
14 | void main()
15 | {
16 | int ckValue =
17 | ((mod(texCoord.x, CkSize*2.) > CkSize)?0:1) +
18 | ((mod(texCoord.y, CkSize*2.) > CkSize)?0:1);
19 | color = vec4(vec3(ckValue/3.), 1.);
20 | }
21 |
--------------------------------------------------------------------------------
/shared/shaders/es30/coordinate_systems.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec2 texCoord;
8 |
9 | out vec2 TexCoord;
10 |
11 | uniform mat4 model;
12 | uniform mat4 view;
13 | uniform mat4 projection;
14 |
15 | void main()
16 | {
17 | gl_Position = projection * view * model * vec4(position, 1.);
18 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
19 | }
20 |
--------------------------------------------------------------------------------
/shared/shaders/es30/coordinate_systems_qt3d_transform.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec2 texCoord;
8 |
9 | out vec2 TexCoord;
10 |
11 | uniform mat4 modelMatrix;
12 | uniform mat4 view;
13 | uniform mat4 projection;
14 |
15 | void main()
16 | {
17 | gl_Position = projection * view * modelMatrix * vec4(position, 1.);
18 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
19 | }
20 |
--------------------------------------------------------------------------------
/shared/shaders/es30/depth_visualize.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | out vec4 color;
9 |
10 | uniform float nearZ;
11 | uniform float farZ;
12 |
13 | float LinearizeDepth(float depth)
14 | {
15 | float z = depth * 2. - 1.;
16 | return (2. * nearZ * farZ) / (farZ + nearZ - z * (farZ - nearZ));
17 | }
18 |
19 | void main()
20 | {
21 | float depth = LinearizeDepth(gl_FragCoord.z) / farZ;
22 | color = vec4(vec3(depth), 1.);
23 | }
24 |
--------------------------------------------------------------------------------
/shared/shaders/es30/hellotriangle.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | out vec4 color;
9 |
10 | void main() {
11 | color = vec4(1., .5, .2, 1.);
12 | }
13 |
--------------------------------------------------------------------------------
/shared/shaders/es30/hellotriangle.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 |
8 | void main() {
9 | gl_Position = vec4(position, 1.);
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/es30/light_casters_directional.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | sampler2D specular;
11 | float shininess;
12 | };
13 |
14 | struct Light {
15 | vec3 direction;
16 |
17 | vec3 ambient;
18 | vec3 diffuse;
19 | vec3 specular;
20 | };
21 |
22 | in vec3 normal;
23 | in vec3 fragPos;
24 | in vec2 texCoord;
25 |
26 | out vec4 color;
27 |
28 | uniform vec3 viewPos;
29 | uniform Material material;
30 | uniform Light light;
31 |
32 | void main()
33 | {
34 | // Ambient
35 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
36 |
37 | // Diffuse
38 | vec3 norm = normalize(normal);
39 | vec3 lightDir = normalize(-light.direction);
40 | float diff = max(dot(norm, lightDir), 0.);
41 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
42 |
43 | // Specular
44 | vec3 viewDir = normalize(viewPos - fragPos);
45 | vec3 reflectDir = reflect(-lightDir, norm);
46 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
47 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
48 |
49 | vec3 result = ambient + diffuse + specular;
50 | color = vec4(result, 1.);
51 | }
52 |
--------------------------------------------------------------------------------
/shared/shaders/es30/light_casters_point.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | sampler2D specular;
11 | float shininess;
12 | };
13 |
14 | struct Light {
15 | vec3 position;
16 |
17 | vec3 ambient;
18 | vec3 diffuse;
19 | vec3 specular;
20 |
21 | float constant;
22 | float linear;
23 | float quadratic;
24 | };
25 |
26 | in vec3 normal;
27 | in vec3 fragPos;
28 | in vec2 texCoord;
29 |
30 | out vec4 color;
31 |
32 | uniform vec3 viewPos;
33 | uniform Material material;
34 | uniform Light light;
35 |
36 | void main()
37 | {
38 | vec3 vecToLight = light.position - fragPos;
39 |
40 | // Ambient
41 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
42 |
43 | // Diffuse
44 | vec3 norm = normalize(normal);
45 | vec3 lightDir = normalize(vecToLight);
46 | float diff = max(dot(norm, lightDir), 0.);
47 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
48 |
49 | // Specular
50 | vec3 viewDir = normalize(viewPos - fragPos);
51 | vec3 reflectDir = reflect(-lightDir, norm);
52 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
53 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
54 |
55 | // Attenuation
56 | float distance = length(vecToLight);
57 | float attenuation = 1. / (light.constant + distance * (light.linear + distance * light.quadratic));
58 |
59 | vec3 result = ambient + (diffuse + specular) * attenuation;
60 | color = vec4(result, 1.);
61 | }
62 |
--------------------------------------------------------------------------------
/shared/shaders/es30/light_casters_spotlight_hard.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | sampler2D specular;
11 | float shininess;
12 | };
13 |
14 | struct Light {
15 | vec3 position;
16 | vec3 direction;
17 | float cutOff;
18 |
19 | vec3 ambient;
20 | vec3 diffuse;
21 | vec3 specular;
22 |
23 | float constant;
24 | float linear;
25 | float quadratic;
26 | };
27 |
28 | in vec3 normal;
29 | in vec3 fragPos;
30 | in vec2 texCoord;
31 |
32 | out vec4 color;
33 |
34 | uniform vec3 viewPos;
35 | uniform Material material;
36 | uniform Light light;
37 |
38 | void main()
39 | {
40 | vec3 vecToLight = light.position - fragPos;
41 | vec3 lightDir = normalize(vecToLight);
42 | float theta = dot(lightDir, normalize(-light.direction));
43 |
44 | // Ambient
45 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
46 | vec3 result = ambient;
47 |
48 | if (theta > light.cutOff) {
49 | // Diffuse
50 | vec3 norm = normalize(normal);
51 | float diff = max(dot(norm, lightDir), 0.);
52 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
53 |
54 | // Specular
55 | vec3 viewDir = normalize(viewPos - fragPos);
56 | vec3 reflectDir = reflect(-lightDir, norm);
57 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
58 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
59 |
60 | // Attenuation
61 | float distance = length(vecToLight);
62 | float attenuation = 1. / (light.constant + distance * (light.linear + distance * light.quadratic));
63 |
64 | result += (diffuse + specular) * attenuation;
65 | }
66 |
67 | color = vec4(result, 1.);
68 | }
69 |
--------------------------------------------------------------------------------
/shared/shaders/es30/light_casters_spotlight_soft.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 | vec3 direction;
12 | float cutOff;
13 | float outerCutOff;
14 |
15 | vec3 ambient;
16 | vec3 diffuse;
17 | vec3 specular;
18 |
19 | float constant;
20 | float linear;
21 | float quadratic;
22 | };
23 |
24 | in vec3 normal;
25 | in vec3 fragPos;
26 | in vec2 texCoord;
27 |
28 | out vec4 color;
29 |
30 | uniform vec3 viewPos;
31 | uniform Material material;
32 | uniform Light light;
33 |
34 | void main()
35 | {
36 | vec3 vecToLight = light.position - fragPos;
37 |
38 | // Ambient
39 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
40 | vec3 result = ambient;
41 |
42 | // Diffuse
43 | vec3 norm = normalize(normal);
44 | vec3 lightDir = normalize(vecToLight);
45 | float diff = max(dot(norm, lightDir), 0.0f);
46 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
47 |
48 | // Specular
49 | vec3 viewDir = normalize(viewPos - fragPos);
50 | vec3 reflectDir = reflect(-lightDir, norm);
51 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
52 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
53 |
54 | // Spotlight (soft edges)
55 | float theta = dot(lightDir, normalize(-light.direction));
56 | float intensity = smoothstep(light.outerCutOff, light.cutOff, theta);
57 |
58 | // Attenuation
59 | float distance = length(vecToLight);
60 | float attenuation = 1.0f / (light.constant + distance * (light.linear + distance * light.quadratic));
61 |
62 | result += (diffuse + specular) * intensity * attenuation;
63 |
64 | color = vec4(result, 1.0f);
65 | }
66 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | out vec4 color;
9 |
10 | uniform vec3 objectColor;
11 | uniform vec3 lightColor;
12 |
13 | void main()
14 | {
15 | color = vec4(lightColor * objectColor, 1.);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec2 vertexTexCoord;
8 |
9 | out vec2 TexCoord;
10 |
11 | uniform mat4 modelMatrix;
12 | uniform mat4 viewMatrix;
13 | uniform mat4 projectionMatrix;
14 |
15 | void main()
16 | {
17 | gl_Position = projectionMatrix * viewMatrix * modelMatrix *
18 | vec4(vertexPosition, 1.);
19 | TexCoord = vec2(vertexTexCoord.x, 1. - vertexTexCoord.y);
20 | }
21 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting_maps-exercise2.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | sampler2D specular;
11 | float shininess;
12 | };
13 |
14 | struct Light {
15 | vec3 position;
16 |
17 | vec3 ambient;
18 | vec3 diffuse;
19 | vec3 specular;
20 | };
21 |
22 | in vec3 normal;
23 | in vec3 fragPos;
24 | in vec2 texCoord;
25 |
26 | out vec4 color;
27 |
28 | uniform vec3 viewPos;
29 | uniform Material material;
30 | uniform Light light;
31 |
32 | void main()
33 | {
34 | // Ambient
35 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
36 |
37 | // Diffuse
38 | vec3 norm = normalize(normal);
39 | vec3 lightDir = normalize(light.position - fragPos);
40 | float diff = max(dot(norm, lightDir), 0.);
41 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
42 |
43 | // Specular
44 | vec3 viewDir = normalize(viewPos - fragPos);
45 | vec3 reflectDir = reflect(-lightDir, norm);
46 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
47 | vec3 specular = light.specular * spec *
48 | (vec3(1.) - vec3(texture2D(material.specular, texCoord)));
49 |
50 | vec3 result = ambient + diffuse + specular;
51 | color = vec4(result, 1.);
52 | }
53 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting_maps-exercise4.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | sampler2D specular;
11 | sampler2D emission;
12 | float shininess;
13 | };
14 |
15 | struct Light {
16 | vec3 position;
17 |
18 | vec3 ambient;
19 | vec3 diffuse;
20 | vec3 specular;
21 | };
22 |
23 | varying vec3 normal;
24 | varying vec3 fragPos;
25 | varying vec2 texCoord;
26 |
27 | out vec4 color;
28 |
29 | uniform float time;
30 | uniform vec3 viewPos;
31 | uniform Material material;
32 | uniform Light light;
33 |
34 | void main()
35 | {
36 | // Ambient
37 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
38 |
39 | // Diffuse
40 | vec3 norm = normalize(normal);
41 | vec3 lightDir = normalize(light.position - fragPos);
42 | float diff = max(dot(norm, lightDir), 0.);
43 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
44 |
45 | // Specular
46 | vec3 viewDir = normalize(viewPos - fragPos);
47 | vec3 reflectDir = reflect(-lightDir, norm);
48 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
49 | vec3 specColor = vec3(texture(material.specular, texCoord));
50 | vec3 specular = light.specular * spec * specColor;
51 |
52 | // Emission
53 | vec3 emission = length(specColor) < 1e-2 ?
54 | vec3(texture(material.emission, texCoord + vec2(0., time))) : vec3(0.);
55 |
56 | vec3 result = ambient + diffuse + specular + emission;
57 | color = vec4(result, 1.);
58 | }
59 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting_maps.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec3 vertexNormal;
8 | in vec2 vertexTexCoord;
9 |
10 | out vec3 normal;
11 | out vec3 fragPos;
12 | out vec2 texCoord;
13 |
14 | uniform mat4 mvp;
15 | uniform mat3 modelNormalMatrix;
16 | uniform mat4 modelMatrix;
17 |
18 | void main()
19 | {
20 | gl_Position = mvp * vec4(vertexPosition, 1.);
21 | normal = modelNormalMatrix * vertexNormal;
22 | fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.));
23 | texCoord = vertexTexCoord;
24 | }
25 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting_maps_diffuse.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | vec3 specular;
11 | float shininess;
12 | };
13 |
14 | struct Light {
15 | vec3 position;
16 |
17 | vec3 ambient;
18 | vec3 diffuse;
19 | vec3 specular;
20 | };
21 |
22 | in vec3 normal;
23 | in vec3 fragPos;
24 | in vec2 texCoord;
25 |
26 | out vec4 color;
27 |
28 | uniform vec3 viewPos;
29 | uniform Material material;
30 | uniform Light light;
31 |
32 | void main()
33 | {
34 | // Ambient
35 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
36 |
37 | // Diffuse
38 | vec3 norm = normalize(normal);
39 | vec3 lightDir = normalize(light.position - fragPos);
40 | float diff = max(dot(norm, lightDir), 0.);
41 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
42 |
43 | // Specular
44 | vec3 viewDir = normalize(viewPos - fragPos);
45 | vec3 reflectDir = reflect(-lightDir, norm);
46 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
47 | vec3 specular = light.specular * (spec * material.specular);
48 |
49 | vec3 result = ambient + diffuse + specular;
50 | color = vec4(result, 1.);
51 | }
52 |
--------------------------------------------------------------------------------
/shared/shaders/es30/lighting_maps_specular.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | sampler2D diffuse;
10 | sampler2D specular;
11 | float shininess;
12 | };
13 |
14 | struct Light {
15 | vec3 position;
16 |
17 | vec3 ambient;
18 | vec3 diffuse;
19 | vec3 specular;
20 | };
21 |
22 | in vec3 normal;
23 | in vec3 fragPos;
24 | in vec2 texCoord;
25 |
26 | out vec4 color;
27 |
28 | uniform vec3 viewPos;
29 | uniform Material material;
30 | uniform Light light;
31 |
32 | void main()
33 | {
34 | // Ambient
35 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
36 |
37 | // Diffuse
38 | vec3 norm = normalize(normal);
39 | vec3 lightDir = normalize(light.position - fragPos);
40 | float diff = max(dot(norm, lightDir), 0.);
41 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
42 |
43 | // Specular
44 | vec3 viewDir = normalize(viewPos - fragPos);
45 | vec3 reflectDir = reflect(-lightDir, norm);
46 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
47 | vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoord));
48 |
49 | vec3 result = ambient + diffuse + specular;
50 | color = vec4(result, 1.);
51 | }
52 |
--------------------------------------------------------------------------------
/shared/shaders/es30/materials.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | struct Material {
9 | vec3 ambient;
10 | vec3 diffuse;
11 | vec3 specular;
12 | float shininess;
13 | };
14 |
15 | struct Light {
16 | vec3 position;
17 |
18 | vec3 ambient;
19 | vec3 diffuse;
20 | vec3 specular;
21 | };
22 |
23 | in vec3 normal;
24 | in vec3 fragPos;
25 |
26 | out vec4 color;
27 |
28 | uniform vec3 viewPos;
29 | uniform Material material;
30 | uniform Light light;
31 |
32 | void main()
33 | {
34 | // Ambient
35 | vec3 ambient = light.ambient * material.ambient;
36 |
37 | // Diffuse
38 | vec3 norm = normalize(normal);
39 | vec3 lightDir = normalize(light.position - fragPos);
40 | float diff = max(dot(norm, lightDir), 0.);
41 | vec3 diffuse = light.diffuse * (diff * material.diffuse);
42 |
43 | // Specular
44 | vec3 viewDir = normalize(viewPos - fragPos);
45 | vec3 reflectDir = reflect(-lightDir, norm);
46 | float spec = pow(max(dot(viewDir, reflectDir), 0.), material.shininess);
47 | vec3 specular = light.specular * (spec * material.specular);
48 |
49 | vec3 result = ambient + diffuse + specular;
50 | color = vec4(result, 1.);
51 | }
52 |
--------------------------------------------------------------------------------
/shared/shaders/es30/passthrough.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 vertexPosition;
7 | in vec2 vertexTexCoord;
8 |
9 | out vec2 texCoord;
10 |
11 | uniform mat4 mvp;
12 |
13 | void main()
14 | {
15 | gl_Position = mvp * vec4(vertexPosition, 1.);
16 | texCoord = vertexTexCoord;
17 | }
18 |
--------------------------------------------------------------------------------
/shared/shaders/es30/shaders-exercise1.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 |
9 | out vec3 ourColor;
10 |
11 | void main() {
12 | gl_Position = vec4(position.x, -position.y, position.z, 1.);
13 | ourColor = color;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/es30/shaders-exercise2.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 |
9 | out vec3 ourColor;
10 |
11 | uniform float xOffset;
12 |
13 | void main() {
14 | gl_Position = vec4(position.x + xOffset, position.y, position.z, 1.);
15 | ourColor = color;
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/es30/shaders-exercise3.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 |
9 | out vec3 ourColor;
10 |
11 | void main() {
12 | gl_Position = vec4(position, 1.);
13 | ourColor = position;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/es30/shaders-interpolated.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 ourColor;
9 |
10 | out vec4 color;
11 |
12 | void main()
13 | {
14 | color = vec4(ourColor, 1.);
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/es30/shaders-uniform.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | out vec4 color;
9 |
10 | uniform vec4 ourColor;
11 |
12 | void main() {
13 | color = ourColor;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/es30/shaders-uniform.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 |
9 | out vec3 ourColor;
10 |
11 | void main() {
12 | gl_Position = vec4(position, 1.);
13 | ourColor = color;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/es30/texture.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 ourColor;
9 | in vec2 TexCoord;
10 |
11 | out vec4 color;
12 |
13 | uniform sampler2D ourTexture;
14 |
15 | void main()
16 | {
17 | color = texture(ourTexture, TexCoord);
18 | }
19 |
--------------------------------------------------------------------------------
/shared/shaders/es30/texture.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 | in vec2 texCoord;
9 |
10 | out vec3 ourColor;
11 | out vec2 TexCoord;
12 |
13 | void main()
14 | {
15 | gl_Position = vec4(position, 1.);
16 | ourColor = color;
17 | TexCoord = texCoord;
18 | }
19 |
--------------------------------------------------------------------------------
/shared/shaders/es30/texture2.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 ourColor;
9 | in vec2 TexCoord;
10 |
11 | out vec4 color;
12 |
13 | uniform sampler2D ourTexture;
14 |
15 | void main()
16 | {
17 | color = texture(ourTexture, TexCoord) * vec4(ourColor, 1.);
18 | }
19 |
--------------------------------------------------------------------------------
/shared/shaders/es30/textures-exercise1.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 ourColor;
9 | in vec2 TexCoord;
10 |
11 | out vec4 color;
12 |
13 | uniform sampler2D ourTexture1;
14 | uniform sampler2D ourTexture2;
15 |
16 | void main()
17 | {
18 | color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, vec2(1. - TexCoord.x, TexCoord.y)), .2);
19 | }
20 |
--------------------------------------------------------------------------------
/shared/shaders/es30/textures-exercise4.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 ourColor;
9 | in vec2 TexCoord;
10 |
11 | out vec4 color;
12 |
13 | uniform float mixValue;
14 |
15 | // Texture samplers
16 | uniform sampler2D ourTexture1;
17 | uniform sampler2D ourTexture2;
18 |
19 | void main()
20 | {
21 | // Linearly interpolate between both textures (second texture is only slightly combined)
22 | color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), mixValue);
23 | }
24 |
--------------------------------------------------------------------------------
/shared/shaders/es30/textures_combined.frag:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | precision lowp float;
7 |
8 | in vec3 ourColor;
9 | in vec2 TexCoord;
10 |
11 | out vec4 color;
12 |
13 | // Texture samplers
14 | uniform sampler2D ourTexture1;
15 | uniform sampler2D ourTexture2;
16 |
17 | void main()
18 | {
19 | // Linearly interpolate between both textures (second texture is only slightly combined)
20 | color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), .2);
21 | }
22 |
--------------------------------------------------------------------------------
/shared/shaders/es30/textures_combined.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 | in vec2 texCoord;
9 |
10 | out vec3 ourColor;
11 | out vec2 TexCoord;
12 |
13 | void main()
14 | {
15 | gl_Position = vec4(position, 1.);
16 | ourColor = color;
17 | // We swap the y-axis by substracing our coordinates from 1. This is done because most images have the top y-axis inversed with OpenGL's top y-axis.
18 | // TexCoord = texCoord;
19 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
20 | }
21 |
--------------------------------------------------------------------------------
/shared/shaders/es30/transformations.vert:
--------------------------------------------------------------------------------
1 | #version 300 es
2 | #undef lowp
3 | #undef mediump
4 | #undef highp
5 |
6 | in vec3 position;
7 | in vec3 color;
8 | in vec2 texCoord;
9 |
10 | out vec3 ourColor;
11 | out vec2 TexCoord;
12 |
13 | uniform mat4 transform;
14 |
15 | void main()
16 | {
17 | gl_Position = transform * vec4(position, 1.);
18 | ourColor = color;
19 | TexCoord = vec2(texCoord.x, 1. - texCoord.y);
20 | }
21 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting-exercise2.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 normal;
4 | in vec3 fragViewPos;
5 | in vec3 lightViewPos; // Extra in variable, since we need the light position in view space we calculate this in the vertex shader
6 |
7 | out vec4 color;
8 |
9 | uniform vec3 lightColor;
10 | uniform vec3 objectColor;
11 |
12 | void main()
13 | {
14 | // Ambient
15 | float ambientStrength = 0.1f;
16 | vec3 ambient = ambientStrength * lightColor;
17 |
18 | // Diffuse
19 | vec3 norm = normalize(normal);
20 | vec3 lightDir = normalize(lightViewPos - fragViewPos);
21 | float diff = max(dot(norm, lightDir), 0.0f);
22 | vec3 diffuse = diff * lightColor;
23 |
24 | // Specular
25 | float specularStrength = 0.5f;
26 | vec3 viewDir = normalize(-fragViewPos);
27 | vec3 reflectDir = reflect(-lightDir, norm);
28 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), 32);
29 | vec3 specular = specularStrength * spec * lightColor;
30 |
31 | vec3 result = (ambient + diffuse + specular) * objectColor;
32 | color = vec4(result, 1.0f);
33 | }
34 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting-exercise2.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 vertexPosition;
4 | layout (location = 1) in vec3 vertexNormal;
5 |
6 | out vec3 normal;
7 | out vec3 fragViewPos;
8 | out vec3 lightViewPos;
9 |
10 | uniform mat4 mvp;
11 | uniform mat3 modelViewNormal;
12 | uniform mat4 modelView;
13 | uniform mat4 viewMatrix;
14 |
15 | uniform vec3 lightPos; // We now define the uniform in the vertex shader and pass the 'view space' lightpos to the fragment shader. lightPos is currently in world space.
16 |
17 | void main()
18 | {
19 | gl_Position = mvp * vec4(vertexPosition, 1.0f);
20 | normal = modelViewNormal * vertexNormal;
21 | fragViewPos = vec3(modelView * vec4(vertexPosition, 1.0f));
22 | lightViewPos = vec3(viewMatrix * vec4(lightPos, 1.0f)); // Transform world-space light position to view-space light position
23 | }
24 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting-exercise3.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 lightingColor;
4 |
5 | out vec4 color;
6 |
7 | uniform vec3 objectColor;
8 |
9 | void main()
10 | {
11 | color = vec4(lightingColor * objectColor, 1.0f);
12 | }
13 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting-exercise3.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 vertexPosition;
4 | layout (location = 1) in vec3 vertexNormal;
5 |
6 | out vec3 lightingColor; // Resulting color from lighting calculations
7 |
8 | uniform mat4 mvp;
9 | uniform mat3 modelNormalMatrix;
10 | uniform mat4 modelMatrix;
11 |
12 | uniform vec3 lightPos;
13 | uniform vec3 viewPos;
14 | uniform vec3 lightColor;
15 | uniform vec3 objectColor;
16 |
17 | void main()
18 | {
19 | gl_Position = mvp * vec4(vertexPosition, 1.0f);
20 |
21 | // Gouraud Shading
22 | // ------------------------
23 | vec3 position = vec3(modelMatrix * vec4(vertexPosition, 1.0f));
24 | vec3 normal = modelNormalMatrix * vertexNormal;
25 |
26 | // Ambient
27 | float ambientStrength = 0.1f;
28 | vec3 ambient = ambientStrength * lightColor;
29 |
30 | // Diffuse
31 | vec3 norm = normalize(normal);
32 | vec3 lightDir = normalize(lightPos - position);
33 | float diff = max(dot(norm, lightDir), 0.0f);
34 | vec3 diffuse = diff * lightColor;
35 |
36 | // Specular
37 | float specularStrength = 1.0f; // This is set higher to better show the effect of Gouraud shading
38 | vec3 viewDir = normalize(viewPos - position);
39 | vec3 reflectDir = reflect(-lightDir, norm);
40 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), 32);
41 | vec3 specular = specularStrength * spec * lightColor;
42 |
43 | lightingColor = ambient + diffuse + specular;
44 | }
45 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 normal;
4 | in vec3 fragPos;
5 |
6 | out vec4 color;
7 |
8 | uniform vec3 lightPos;
9 | uniform vec3 viewPos;
10 | uniform vec3 lightColor;
11 | uniform vec3 objectColor;
12 |
13 | void main()
14 | {
15 | // Ambient
16 | float ambientStrength = 0.1f;
17 | vec3 ambient = ambientStrength * lightColor;
18 |
19 | // Diffuse
20 | vec3 norm = normalize(normal);
21 | vec3 lightDir = normalize(lightPos - fragPos);
22 | float diff = max(dot(norm, lightDir), 0.0f);
23 | vec3 diffuse = diff * lightColor;
24 |
25 | // Specular
26 | float specularStrength = 0.5f;
27 | vec3 viewDir = normalize(viewPos - fragPos);
28 | vec3 reflectDir = reflect(-lightDir, norm);
29 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), 32);
30 | vec3 specular = specularStrength * spec * lightColor;
31 |
32 | vec3 result = (ambient + diffuse + specular) * objectColor;
33 | color = vec4(result, 1.0f);
34 | }
35 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 vertexPosition;
4 | layout (location = 1) in vec3 vertexNormal;
5 |
6 | out vec3 normal;
7 | out vec3 fragPos;
8 |
9 | uniform mat4 mvp;
10 | uniform mat3 modelNormalMatrix;
11 | uniform mat4 modelMatrix;
12 |
13 | void main()
14 | {
15 | gl_Position = mvp * vec4(vertexPosition, 1.0f);
16 | normal = modelNormalMatrix * vertexNormal;
17 | fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.0f));
18 | }
19 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting_diffuse.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 normal;
4 | in vec3 fragPos;
5 |
6 | out vec4 color;
7 |
8 | uniform vec3 lightPos;
9 | uniform vec3 lightColor;
10 | uniform vec3 objectColor;
11 |
12 | void main()
13 | {
14 | // Ambient
15 | float ambientStrength = 0.1f;
16 | vec3 ambient = ambientStrength * lightColor;
17 |
18 | // Diffuse
19 | vec3 norm = normalize(normal);
20 | vec3 lightDir = normalize(lightPos - fragPos);
21 | float diff = max(dot(norm, lightDir), 0.0f);
22 | vec3 diffuse = diff * lightColor;
23 |
24 | vec3 result = (ambient + diffuse) * objectColor;
25 | color = vec4(result, 1.0f);
26 | }
27 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/basic_lighting_gouraud.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 vertexPosition;
4 | layout (location = 1) in vec3 vertexNormal;
5 |
6 | out vec3 ourColor;
7 |
8 | uniform mat4 mvp;
9 | uniform mat3 modelNormalMatrix;
10 | uniform mat4 modelMatrix;
11 |
12 | uniform vec3 lightPos;
13 | uniform vec3 viewPos;
14 | uniform vec3 lightColor;
15 | uniform vec3 objectColor;
16 |
17 | void main()
18 | {
19 | vec3 position = vec3(modelMatrix * vec4(vertexPosition, 1.0f));
20 | vec3 normal = modelNormalMatrix * vertexNormal;
21 |
22 | // Ambient
23 | float ambientStrength = 0.1f;
24 | vec3 ambient = ambientStrength * lightColor;
25 |
26 | // Diffuse
27 | vec3 norm = normalize(normal);
28 | vec3 lightDir = normalize(lightPos - position);
29 | float diff = max(dot(norm, lightDir), 0.0f);
30 | vec3 diffuse = diff * lightColor;
31 |
32 | // Specular
33 | float specularStrength = 0.5f;
34 | vec3 viewDir = normalize(viewPos - position);
35 | vec3 reflectDir = reflect(-lightDir, norm);
36 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), 32);
37 | vec3 specular = specularStrength * spec * lightColor;
38 |
39 | ourColor = (ambient + diffuse + specular) * objectColor;
40 | gl_Position = mvp * vec4(vertexPosition, 1.0f);
41 | }
42 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/checkerboard.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | float CkSize = 1.0f/9.0f;
4 |
5 | in vec2 texCoord;
6 |
7 | out vec4 color;
8 |
9 | void main()
10 | {
11 | int ckValue =
12 | ((mod(texCoord.x, CkSize*2.0f) > CkSize)?0:1) +
13 | ((mod(texCoord.y, CkSize*2.0f) > CkSize)?0:1);
14 | color = vec4(vec3(ckValue/3.0f), 1.0f);
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/coordinate_systems.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 2) in vec2 texCoord;
5 |
6 | out vec2 TexCoord;
7 |
8 | uniform mat4 model;
9 | uniform mat4 view;
10 | uniform mat4 projection;
11 |
12 | void main()
13 | {
14 | gl_Position = projection * view * model * vec4(position, 1.0f);
15 | TexCoord = vec2(texCoord.x, 1.0f - texCoord.y);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/coordinate_systems_qt3d_transform.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 2) in vec2 texCoord;
5 |
6 | out vec2 TexCoord;
7 |
8 | uniform mat4 modelMatrix;
9 | uniform mat4 view;
10 | uniform mat4 projection;
11 |
12 | void main()
13 | {
14 | gl_Position = projection * view * modelMatrix * vec4(position, 1.0f);
15 | TexCoord = vec2(texCoord.x, 1.0f - texCoord.y);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/depth_visualize.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | out vec4 color;
4 |
5 | uniform float nearZ;
6 | uniform float farZ;
7 |
8 | float LinearizeDepth(float depth)
9 | {
10 | float z = depth * 2.0f - 1.0f;
11 | return (2.0f * nearZ * farZ) / (farZ + nearZ - z * (farZ - nearZ));
12 | }
13 |
14 | void main()
15 | {
16 | float depth = LinearizeDepth(gl_FragCoord.z) / farZ;
17 | color = vec4(vec3(depth), 1.0f);
18 | }
19 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/hellotriangle.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | out vec4 color;
4 |
5 | void main() {
6 | color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
7 | }
8 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/hellotriangle.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 |
5 | void main() {
6 | gl_Position = vec4(position.x, position.y, position.z, 1.0f);
7 | }
8 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/light_casters_directional.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 direction;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 | };
16 |
17 | in vec3 normal;
18 | in vec3 fragPos;
19 | in vec2 texCoord;
20 |
21 | out vec4 color;
22 |
23 | uniform vec3 viewPos;
24 | uniform Material material;
25 | uniform Light light;
26 |
27 | void main()
28 | {
29 | // Ambient
30 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
31 |
32 | // Diffuse
33 | vec3 norm = normalize(normal);
34 | vec3 lightDir = normalize(-light.direction);
35 | float diff = max(dot(norm, lightDir), 0.0f);
36 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
37 |
38 | // Specular
39 | vec3 viewDir = normalize(viewPos - fragPos);
40 | vec3 reflectDir = reflect(-lightDir, norm);
41 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
42 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
43 |
44 | vec3 result = ambient + diffuse + specular;
45 | color = vec4(result, 1.0f);
46 | }
47 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/light_casters_point.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 |
16 | float constant;
17 | float linear;
18 | float quadratic;
19 | };
20 |
21 | in vec3 normal;
22 | in vec3 fragPos;
23 | in vec2 texCoord;
24 |
25 | out vec4 color;
26 |
27 | uniform vec3 viewPos;
28 | uniform Material material;
29 | uniform Light light;
30 |
31 | void main()
32 | {
33 | vec3 vecToLight = light.position - fragPos;
34 |
35 | // Ambient
36 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
37 |
38 | // Diffuse
39 | vec3 norm = normalize(normal);
40 | vec3 lightDir = normalize(vecToLight);
41 | float diff = max(dot(norm, lightDir), 0.0f);
42 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
43 |
44 | // Specular
45 | vec3 viewDir = normalize(viewPos - fragPos);
46 | vec3 reflectDir = reflect(-lightDir, norm);
47 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
48 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
49 |
50 | // Attenuation
51 | float distance = length(vecToLight);
52 | float attenuation = 1.0f / (light.constant + distance * (light.linear + distance * light.quadratic));
53 |
54 | vec3 result = ambient + (diffuse + specular) * attenuation;
55 | color = vec4(result, 1.0f);
56 | }
57 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/light_casters_spotlight_hard.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 | vec3 direction;
12 | float cutOff;
13 |
14 | vec3 ambient;
15 | vec3 diffuse;
16 | vec3 specular;
17 |
18 | float constant;
19 | float linear;
20 | float quadratic;
21 | };
22 |
23 | in vec3 normal;
24 | in vec3 fragPos;
25 | in vec2 texCoord;
26 |
27 | out vec4 color;
28 |
29 | uniform vec3 viewPos;
30 | uniform Material material;
31 | uniform Light light;
32 |
33 | void main()
34 | {
35 | vec3 vecToLight = light.position - fragPos;
36 | vec3 lightDir = normalize(vecToLight);
37 | float theta = dot(lightDir, normalize(-light.direction));
38 |
39 | // Ambient
40 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
41 | vec3 result = ambient;
42 |
43 | if (theta > light.cutOff) {
44 | // Diffuse
45 | vec3 norm = normalize(normal);
46 | float diff = max(dot(norm, lightDir), 0.0f);
47 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
48 |
49 | // Specular
50 | vec3 viewDir = normalize(viewPos - fragPos);
51 | vec3 reflectDir = reflect(-lightDir, norm);
52 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
53 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
54 |
55 | // Attenuation
56 | float distance = length(vecToLight);
57 | float attenuation = 1.0f / (light.constant + distance * (light.linear + distance * light.quadratic));
58 |
59 | result += (diffuse + specular) * attenuation;
60 | }
61 |
62 | color = vec4(result, 1.0f);
63 | }
64 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/light_casters_spotlight_soft.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 | vec3 direction;
12 | float cutOff;
13 | float outerCutOff;
14 |
15 | vec3 ambient;
16 | vec3 diffuse;
17 | vec3 specular;
18 |
19 | float constant;
20 | float linear;
21 | float quadratic;
22 | };
23 |
24 | in vec3 normal;
25 | in vec3 fragPos;
26 | in vec2 texCoord;
27 |
28 | out vec4 color;
29 |
30 | uniform vec3 viewPos;
31 | uniform Material material;
32 | uniform Light light;
33 |
34 | void main()
35 | {
36 | vec3 vecToLight = light.position - fragPos;
37 |
38 | // Ambient
39 | vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, texCoord));
40 | vec3 result = ambient;
41 |
42 | // Diffuse
43 | vec3 norm = normalize(normal);
44 | vec3 lightDir = normalize(vecToLight);
45 | float diff = max(dot(norm, lightDir), 0.0f);
46 | vec3 diffuse = light.diffuse * diff * vec3(texture2D(material.diffuse, texCoord));
47 |
48 | // Specular
49 | vec3 viewDir = normalize(viewPos - fragPos);
50 | vec3 reflectDir = reflect(-lightDir, norm);
51 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
52 | vec3 specular = light.specular * spec * vec3(texture2D(material.specular, texCoord));
53 |
54 | // Spotlight (soft edges)
55 | float theta = dot(lightDir, normalize(-light.direction));
56 | float intensity = smoothstep(light.outerCutOff, light.cutOff, theta);
57 |
58 | // Attenuation
59 | float distance = length(vecToLight);
60 | float attenuation = 1.0f / (light.constant + distance * (light.linear + distance * light.quadratic));
61 |
62 | result += (diffuse + specular) * intensity * attenuation;
63 |
64 | color = vec4(result, 1.0f);
65 | }
66 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | out vec4 color;
4 |
5 | uniform vec3 objectColor;
6 | uniform vec3 lightColor;
7 |
8 | void main()
9 | {
10 | color = vec4(lightColor * objectColor, 1.0f);
11 | }
12 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 vertexPosition;
4 | layout (location = 2) in vec2 vertexTexCoord;
5 |
6 | out vec2 TexCoord;
7 |
8 | uniform mat4 modelMatrix;
9 | uniform mat4 viewMatrix;
10 | uniform mat4 projectionMatrix;
11 |
12 | void main()
13 | {
14 | gl_Position = projectionMatrix * viewMatrix * modelMatrix *
15 | vec4(vertexPosition, 1.0f);
16 | TexCoord = vec2(vertexTexCoord.x, 1.0f - vertexTexCoord.y);
17 | }
18 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting_maps-exercise2.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 | };
16 |
17 | in vec3 normal;
18 | in vec3 fragPos;
19 | in vec2 texCoord;
20 |
21 | out vec4 color;
22 |
23 | uniform vec3 viewPos;
24 | uniform Material material;
25 | uniform Light light;
26 |
27 | void main()
28 | {
29 | // Ambient
30 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
31 |
32 | // Diffuse
33 | vec3 norm = normalize(normal);
34 | vec3 lightDir = normalize(light.position - fragPos);
35 | float diff = max(dot(norm, lightDir), 0.0f);
36 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
37 |
38 | // Specular
39 | vec3 viewDir = normalize(viewPos - fragPos);
40 | vec3 reflectDir = reflect(-lightDir, norm);
41 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
42 | vec3 specular = light.specular * spec *
43 | (vec3(1.f) - vec3(texture2D(material.specular, texCoord)));
44 |
45 | vec3 result = ambient + diffuse + specular;
46 | color = vec4(result, 1.0f);
47 | }
48 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting_maps-exercise4.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | sampler2D emission;
7 | float shininess;
8 | };
9 |
10 | struct Light {
11 | vec3 position;
12 |
13 | vec3 ambient;
14 | vec3 diffuse;
15 | vec3 specular;
16 | };
17 |
18 | varying vec3 normal;
19 | varying vec3 fragPos;
20 | varying vec2 texCoord;
21 |
22 | out vec4 color;
23 |
24 | uniform float time;
25 | uniform vec3 viewPos;
26 | uniform Material material;
27 | uniform Light light;
28 |
29 | void main()
30 | {
31 | // Ambient
32 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
33 |
34 | // Diffuse
35 | vec3 norm = normalize(normal);
36 | vec3 lightDir = normalize(light.position - fragPos);
37 | float diff = max(dot(norm, lightDir), 0.0f);
38 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
39 |
40 | // Specular
41 | vec3 viewDir = normalize(viewPos - fragPos);
42 | vec3 reflectDir = reflect(-lightDir, norm);
43 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
44 | vec3 specColor = vec3(texture(material.specular, texCoord));
45 | vec3 specular = light.specular * spec * specColor;
46 |
47 | // Emission
48 | vec3 emission = length(specColor) < 1e-2f ?
49 | vec3(texture(material.emission, texCoord + vec2(0.0f, time))) : vec3(0.0f);
50 |
51 | vec3 result = ambient + diffuse + specular + emission;
52 | color = vec4(result, 1.0f);
53 | }
54 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting_maps.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 vertexPosition;
4 | layout (location = 1) in vec3 vertexNormal;
5 | layout (location = 2) in vec2 vertexTexCoord;
6 |
7 | out vec3 normal;
8 | out vec3 fragPos;
9 | out vec2 texCoord;
10 |
11 | uniform mat4 mvp;
12 | uniform mat3 modelNormalMatrix;
13 | uniform mat4 modelMatrix;
14 |
15 | void main()
16 | {
17 | gl_Position = mvp * vec4(vertexPosition, 1.0f);
18 | normal = modelNormalMatrix * vertexNormal;
19 | fragPos = vec3(modelMatrix * vec4(vertexPosition, 1.0f));
20 | texCoord = vertexTexCoord;
21 | }
22 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting_maps_diffuse.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | vec3 specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 | };
16 |
17 | in vec3 normal;
18 | in vec3 fragPos;
19 | in vec2 texCoord;
20 |
21 | out vec4 color;
22 |
23 | uniform vec3 viewPos;
24 | uniform Material material;
25 | uniform Light light;
26 |
27 | void main()
28 | {
29 | // Ambient
30 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
31 |
32 | // Diffuse
33 | vec3 norm = normalize(normal);
34 | vec3 lightDir = normalize(light.position - fragPos);
35 | float diff = max(dot(norm, lightDir), 0.0f);
36 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
37 |
38 | // Specular
39 | vec3 viewDir = normalize(viewPos - fragPos);
40 | vec3 reflectDir = reflect(-lightDir, norm);
41 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
42 | vec3 specular = light.specular * (spec * material.specular);
43 |
44 | vec3 result = ambient + diffuse + specular;
45 | color = vec4(result, 1.0f);
46 | }
47 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/lighting_maps_specular.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | sampler2D diffuse;
5 | sampler2D specular;
6 | float shininess;
7 | };
8 |
9 | struct Light {
10 | vec3 position;
11 |
12 | vec3 ambient;
13 | vec3 diffuse;
14 | vec3 specular;
15 | };
16 |
17 | in vec3 normal;
18 | in vec3 fragPos;
19 | in vec2 texCoord;
20 |
21 | out vec4 color;
22 |
23 | uniform vec3 viewPos;
24 | uniform Material material;
25 | uniform Light light;
26 |
27 | void main()
28 | {
29 | // Ambient
30 | vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));
31 |
32 | // Diffuse
33 | vec3 norm = normalize(normal);
34 | vec3 lightDir = normalize(light.position - fragPos);
35 | float diff = max(dot(norm, lightDir), 0.0f);
36 | vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));
37 |
38 | // Specular
39 | vec3 viewDir = normalize(viewPos - fragPos);
40 | vec3 reflectDir = reflect(-lightDir, norm);
41 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
42 | vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoord));
43 |
44 | vec3 result = ambient + diffuse + specular;
45 | color = vec4(result, 1.0f);
46 | }
47 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/materials.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | struct Material {
4 | vec3 ambient;
5 | vec3 diffuse;
6 | vec3 specular;
7 | float shininess;
8 | };
9 |
10 | struct Light {
11 | vec3 position;
12 |
13 | vec3 ambient;
14 | vec3 diffuse;
15 | vec3 specular;
16 | };
17 |
18 | in vec3 normal;
19 | in vec3 fragPos;
20 |
21 | out vec4 color;
22 |
23 | uniform vec3 viewPos;
24 | uniform Material material;
25 | uniform Light light;
26 |
27 | void main()
28 | {
29 | // Ambient
30 | vec3 ambient = light.ambient * material.ambient;
31 |
32 | // Diffuse
33 | vec3 norm = normalize(normal);
34 | vec3 lightDir = normalize(light.position - fragPos);
35 | float diff = max(dot(norm, lightDir), 0.0f);
36 | vec3 diffuse = light.diffuse * (diff * material.diffuse);
37 |
38 | // Specular
39 | vec3 viewDir = normalize(viewPos - fragPos);
40 | vec3 reflectDir = reflect(-lightDir, norm);
41 | float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
42 | vec3 specular = light.specular * (spec * material.specular);
43 |
44 | vec3 result = ambient + diffuse + specular;
45 | color = vec4(result, 1.0f);
46 | }
47 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/passthrough.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 vertexPosition;
4 | in vec2 vertexTexCoord;
5 |
6 | out vec2 texCoord;
7 |
8 | uniform mat4 mvp;
9 |
10 | void main()
11 | {
12 | gl_Position = mvp * vec4(vertexPosition, 1.0f);
13 | texCoord = vertexTexCoord;
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/shaders-exercise1.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 |
6 | out vec3 ourColor;
7 |
8 | void main()
9 | {
10 | gl_Position = vec4(position.x, -position.y, position.z, 1.0f);
11 | ourColor = color;
12 | }
13 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/shaders-exercise2.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 |
6 | out vec3 ourColor;
7 |
8 | uniform float xOffset;
9 |
10 | void main() {
11 | gl_Position = vec4(position.x + xOffset, position.y, position.z, 1.0f);
12 | ourColor = color;
13 | }
14 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/shaders-exercise3.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 |
6 | out vec3 ourColor;
7 |
8 | void main() {
9 | gl_Position = vec4(position, 1.0f);
10 | ourColor = position;
11 | }
12 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/shaders-interpolated.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 ourColor;
4 |
5 | out vec4 color;
6 |
7 | void main()
8 | {
9 | color = vec4(ourColor, 1.0f);
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/shaders-uniform.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | out vec4 color;
4 |
5 | uniform vec4 ourColor;
6 |
7 | void main()
8 | {
9 | color = ourColor;
10 | }
11 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/shaders-uniform.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 |
6 | out vec3 ourColor;
7 |
8 | void main()
9 | {
10 | gl_Position = vec4(position, 1.0f);
11 | ourColor = color;
12 | }
13 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/texture.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 ourColor;
4 | in vec2 TexCoord;
5 |
6 | out vec4 color;
7 |
8 | uniform sampler2D ourTexture;
9 |
10 | void main()
11 | {
12 | color = texture(ourTexture, TexCoord);
13 | }
14 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/texture.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 | layout (location = 2) in vec2 texCoord;
6 |
7 | out vec3 ourColor;
8 | out vec2 TexCoord;
9 |
10 | void main()
11 | {
12 | gl_Position = vec4(position, 1.0f);
13 | ourColor = color;
14 | TexCoord = texCoord;
15 | }
16 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/texture2.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 ourColor;
4 | in vec2 TexCoord;
5 |
6 | out vec4 color;
7 |
8 | uniform sampler2D ourTexture;
9 |
10 | void main()
11 | {
12 | color = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0f);
13 | }
14 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/textures-exercise1.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 ourColor;
4 | in vec2 TexCoord;
5 |
6 | out vec4 color;
7 |
8 | uniform sampler2D ourTexture1;
9 | uniform sampler2D ourTexture2;
10 |
11 | void main()
12 | {
13 | color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, vec2(1.0f - TexCoord.x, TexCoord.y)), 0.2f);
14 | }
15 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/textures-exercise4.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 ourColor;
4 | in vec2 TexCoord;
5 |
6 | out vec4 color;
7 |
8 | uniform float mixValue;
9 |
10 | // Texture samplers
11 | uniform sampler2D ourTexture1;
12 | uniform sampler2D ourTexture2;
13 |
14 | void main()
15 | {
16 | // Linearly interpolate between both textures (second texture is only slightly combined)
17 | color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), mixValue);
18 | }
19 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/textures_combined.frag:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | in vec3 ourColor;
4 | in vec2 TexCoord;
5 |
6 | out vec4 color;
7 |
8 | // Texture samplers
9 | uniform sampler2D ourTexture1;
10 | uniform sampler2D ourTexture2;
11 |
12 | void main()
13 | {
14 | // Linearly interpolate between both textures (second texture is only slightly combined)
15 | color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2f);
16 | }
17 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/textures_combined.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 | layout (location = 2) in vec2 texCoord;
6 |
7 | out vec3 ourColor;
8 | out vec2 TexCoord;
9 |
10 | void main()
11 | {
12 | gl_Position = vec4(position, 1.0f);
13 | ourColor = color;
14 | // We swap the y-axis by substracing our coordinates from 1. This is done because most images have the top y-axis inversed with OpenGL's top y-axis.
15 | // TexCoord = texCoord;
16 | TexCoord = vec2(texCoord.x, 1.0f - texCoord.y);
17 | }
18 |
--------------------------------------------------------------------------------
/shared/shaders/gl33/transformations.vert:
--------------------------------------------------------------------------------
1 | #version 330 core
2 |
3 | layout (location = 0) in vec3 position;
4 | layout (location = 1) in vec3 color;
5 | layout (location = 2) in vec2 texCoord;
6 |
7 | out vec3 ourColor;
8 | out vec2 TexCoord;
9 |
10 | uniform mat4 transform;
11 |
12 | void main()
13 | {
14 | gl_Position = transform * vec4(position, 1.0f);
15 | ourColor = color;
16 | TexCoord = vec2(texCoord.x, 1.0f - texCoord.y);
17 | }
18 |
--------------------------------------------------------------------------------