├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── chapter01 ├── CMakeLists.txt ├── main.cpp ├── scenebasic.cpp ├── scenebasic.h ├── scenebasic_attrib.cpp ├── scenebasic_attrib.h ├── scenebasic_debug.cpp ├── scenebasic_debug.h ├── scenebasic_uniform.cpp ├── scenebasic_uniform.h ├── scenebasic_uniformblock.cpp ├── scenebasic_uniformblock.h └── shader │ ├── basic.frag │ ├── basic.vert │ ├── basic_uniform.frag │ ├── basic_uniform.vert │ ├── basic_uniformblock.frag │ ├── basic_uniformblock.vert │ ├── basic_uniformblock_41.frag │ └── basic_uniformblock_41.vert ├── chapter02 ├── CMakeLists.txt ├── main.cpp ├── sceneads.cpp ├── sceneads.h ├── scenediffuse.cpp ├── scenediffuse.h ├── scenediscard.cpp ├── scenediscard.h ├── sceneflat.cpp ├── sceneflat.h ├── scenesubroutine.cpp ├── scenesubroutine.h ├── scenetwoside.cpp ├── scenetwoside.h └── shader │ ├── diffuse.frag │ ├── diffuse.vert │ ├── discard.frag │ ├── discard.vert │ ├── flat.frag │ ├── flat.vert │ ├── function.frag │ ├── function.vert │ ├── phong.frag │ ├── phong.vert │ ├── subroutine.frag │ ├── subroutine.vert │ ├── twoside.frag │ └── twoside.vert ├── chapter03 ├── CMakeLists.txt ├── main.cpp ├── scenedirectional.cpp ├── scenedirectional.h ├── scenefog.cpp ├── scenefog.h ├── scenemultilight.cpp ├── scenemultilight.h ├── sceneperfragment.cpp ├── sceneperfragment.h ├── scenespot.cpp ├── scenespot.h ├── scenetoon.cpp ├── scenetoon.h └── shader │ ├── directional.fs │ ├── directional.vs │ ├── fog.fs │ ├── fog.vs │ ├── halfway.fs │ ├── halfway.vs │ ├── multilight.frag │ ├── multilight.vert │ ├── perfrag.fs │ ├── perfrag.vs │ ├── spot.fs │ ├── spot.vs │ ├── toon.fs │ └── toon.vs ├── chapter04 ├── CMakeLists.txt ├── main.cpp ├── scenealphatest.cpp ├── scenealphatest.h ├── scenemultitex.cpp ├── scenemultitex.h ├── scenenormalmap.cpp ├── scenenormalmap.h ├── sceneprojtex.cpp ├── sceneprojtex.h ├── scenereflectcube.cpp ├── scenereflectcube.h ├── scenerefractcube.cpp ├── scenerefractcube.h ├── scenerendertotex.cpp ├── scenerendertotex.h ├── scenesamplerobj.cpp ├── scenesamplerobj.h ├── scenetexture.cpp ├── scenetexture.h ├── shader │ ├── alphatest.fs │ ├── alphatest.vs │ ├── alphatest_41.fs │ ├── alphatest_41.vs │ ├── cubemap_reflect.fs │ ├── cubemap_reflect.vs │ ├── cubemap_reflect_41.fs │ ├── cubemap_reflect_41.vs │ ├── cubemap_refract.fs │ ├── cubemap_refract.vs │ ├── cubemap_refract_41.fs │ ├── cubemap_refract_41.vs │ ├── multitex.fs │ ├── multitex.vs │ ├── multitex_41.fs │ ├── multitex_41.vs │ ├── normalmap.fs │ ├── normalmap.vs │ ├── normalmap_41.fs │ ├── normalmap_41.vs │ ├── projtex.fs │ ├── projtex.vs │ ├── projtex_41.fs │ ├── projtex_41.vs │ ├── rendertotex.fs │ ├── rendertotex.vs │ ├── texture.fs │ ├── texture.vs │ ├── texture_41.fs │ └── texture_41.vs ├── skybox.cpp └── skybox.h ├── chapter05 ├── CMakeLists.txt ├── main.cpp ├── sceneblur.cpp ├── sceneblur.h ├── scenedeferred.cpp ├── scenedeferred.h ├── sceneedge.cpp ├── sceneedge.h ├── scenegamma.cpp ├── scenegamma.h ├── scenehdrbloom.cpp ├── scenehdrbloom.h ├── scenemsaa.cpp ├── scenemsaa.h ├── sceneoit.cpp ├── sceneoit.h ├── scenetonemap.cpp ├── scenetonemap.h └── shader │ ├── blur.fs │ ├── blur.vs │ ├── blur_41.fs │ ├── blur_41.vs │ ├── centroid.fs │ ├── centroid.vs │ ├── deferred.fs │ ├── deferred.vs │ ├── deferred_41.fs │ ├── deferred_41.vs │ ├── edge.fs │ ├── edge.vs │ ├── edge_41.fs │ ├── edge_41.vs │ ├── gamma.fs │ ├── gamma.vs │ ├── hdrbloom.fs │ ├── hdrbloom.vs │ ├── hdrbloom_41.fs │ ├── hdrbloom_41.vs │ ├── oit.fs │ ├── oit.vs │ ├── tonemap.fs │ ├── tonemap.vs │ ├── tonemap_41.fs │ └── tonemap_41.vs ├── chapter06 ├── CMakeLists.txt ├── main.cpp ├── scenebezcurve.cpp ├── scenebezcurve.h ├── scenepointsprite.cpp ├── scenepointsprite.h ├── scenequadtess.cpp ├── scenequadtess.h ├── sceneshadewire.cpp ├── sceneshadewire.h ├── scenesilhouette.cpp ├── scenesilhouette.h ├── scenetessteapot.cpp ├── scenetessteapot.h ├── scenetessteapotdepth.cpp ├── scenetessteapotdepth.h └── shader │ ├── bezcurve.fs │ ├── bezcurve.tcs │ ├── bezcurve.tes │ ├── bezcurve.vs │ ├── pointsprite.fs │ ├── pointsprite.gs │ ├── pointsprite.vs │ ├── quadtess.fs │ ├── quadtess.gs │ ├── quadtess.tcs │ ├── quadtess.tes │ ├── quadtess.vs │ ├── shadewire.fs │ ├── shadewire.gs │ ├── shadewire.vs │ ├── silhouette.fs │ ├── silhouette.gs │ ├── silhouette.vs │ ├── solid.fs │ ├── solid.vs │ ├── tessteapot.fs │ ├── tessteapot.gs │ ├── tessteapot.tcs │ ├── tessteapot.tes │ ├── tessteapot.vs │ ├── tessteapotdepth.fs │ ├── tessteapotdepth.gs │ ├── tessteapotdepth.tcs │ ├── tessteapotdepth.tes │ └── tessteapotdepth.vs ├── chapter07 ├── CMakeLists.txt ├── frustum.cpp ├── frustum.h ├── main.cpp ├── sceneao.cpp ├── sceneao.h ├── scenejitter.cpp ├── scenejitter.h ├── scenepcf.cpp ├── scenepcf.h ├── sceneshadowmap.cpp ├── sceneshadowmap.h ├── sceneshadowvolume.cpp ├── sceneshadowvolume.h └── shader │ ├── ao.fs │ ├── ao.vs │ ├── jitter.fs │ ├── jitter.vs │ ├── pcf.fs │ ├── pcf.vs │ ├── shadowmap.fs │ ├── shadowmap.vs │ ├── shadowvolume-comp.fs │ ├── shadowvolume-comp.vs │ ├── shadowvolume-render.fs │ ├── shadowvolume-render.vs │ ├── shadowvolume-vol.fs │ ├── shadowvolume-vol.gs │ ├── shadowvolume-vol.vs │ ├── solid.fs │ └── solid.vs ├── chapter08 ├── CMakeLists.txt ├── main.cpp ├── noisetex.cpp ├── noisetex.h ├── scenedecay.cpp ├── scenedecay.h ├── scenenightvision.cpp ├── scenenightvision.h ├── scenenoise.cpp ├── scenenoise.h ├── scenepaint.cpp ├── scenepaint.h ├── scenesky.cpp ├── scenesky.h ├── scenewood.cpp ├── scenewood.h └── shader │ ├── decay.fs │ ├── decay.vs │ ├── nightvision.fs │ ├── nightvision.vs │ ├── noisetex.fs │ ├── noisetex.vs │ ├── paint.fs │ ├── paint.vs │ ├── sky.fs │ ├── sky.vs │ ├── wood.fs │ └── wood.vs ├── chapter09 ├── CMakeLists.txt ├── main.cpp ├── scenefire.cpp ├── scenefire.h ├── sceneparticles.cpp ├── sceneparticles.h ├── sceneparticlesfeedback.cpp ├── sceneparticlesfeedback.h ├── sceneparticlesinstanced.cpp ├── sceneparticlesinstanced.h ├── scenesmoke.cpp ├── scenesmoke.h ├── scenewave.cpp ├── scenewave.h └── shader │ ├── fire.fs │ ├── fire.vs │ ├── particleinstanced.fs │ ├── particleinstanced.vs │ ├── particles.fs │ ├── particles.vs │ ├── smoke.fs │ ├── smoke.vs │ ├── transfeedback.fs │ ├── transfeedback.vs │ ├── wave.fs │ └── wave.vs ├── chapter10 ├── CMakeLists.txt ├── main.cpp ├── scenecloth.cpp ├── scenecloth.h ├── sceneedge.cpp ├── sceneedge.h ├── scenemandelbrot.cpp ├── scenemandelbrot.h ├── sceneparticles.cpp ├── sceneparticles.h └── shader │ ├── ads.fs │ ├── ads.vs │ ├── cloth.cs │ ├── cloth_normal.cs │ ├── edge.cs │ ├── edge.fs │ ├── edge.vs │ ├── mandelbrot.cs │ ├── particles.cs │ ├── particles.fs │ └── particles.vs ├── ingredients ├── CMakeLists.txt ├── aabb.h ├── cookbookogl.h ├── cube.cpp ├── cube.h ├── drawable.h ├── glad │ ├── include │ │ ├── KHR │ │ │ └── khrplatform.h │ │ └── glad │ │ │ └── glad.h │ └── src │ │ └── glad.c ├── glslprogram.cpp ├── glslprogram.h ├── glutils.cpp ├── glutils.h ├── objmesh.cpp ├── objmesh.h ├── plane.cpp ├── plane.h ├── scene.h ├── scenerunner.h ├── sphere.cpp ├── sphere.h ├── stb │ └── stb_image.h ├── teapot.cpp ├── teapot.h ├── teapotdata.h ├── teapotpatch.cpp ├── teapotpatch.h ├── texture.cpp ├── texture.h ├── torus.cpp ├── torus.h ├── trianglemesh.cpp ├── trianglemesh.h └── utils.h └── media ├── bs_ears.obj ├── building.obj ├── pig_triangulated.obj ├── spot ├── README.txt ├── spot_texture.png └── spot_triangulated.obj └── texture ├── ao_ears.png ├── bluewater.png ├── brick1.jpg ├── cement.jpg ├── cubemap_night ├── night_negx.png ├── night_negy.png ├── night_negz.png ├── night_posx.png ├── night_posy.png └── night_posz.png ├── fire.png ├── flower.png ├── me_textile.png ├── moss.png ├── ogre_diffuse.png ├── ogre_normalmap.png ├── smoke.png └── star.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.suo 3 | *~ 4 | *.user 5 | *.aps 6 | *.ipch 7 | *.pdb 8 | *.ilk 9 | *.exe 10 | *.swp 11 | 12 | # cmake build/output 13 | CMakeFiles/ 14 | cmake_install.cmake 15 | CMakeCache.txt 16 | Makefile 17 | **/*.dir/ 18 | cmake-build-*/ 19 | 20 | # Compiled output 21 | ingredients/libingredients.a 22 | chapter??/chapter?? 23 | chapter??/chapter??.exe 24 | 25 | .idea/ 26 | *.cbp 27 | 28 | # Visual Studio projects, solutions, build folders, etc. 29 | *.vcxproj 30 | *.sln 31 | *.vcxproj.filters 32 | .vs/ 33 | x64/ 34 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.0) 2 | set (CMAKE_CXX_STANDARD 11) 3 | 4 | project (GLSLCOOKBOOK) 5 | 6 | if(MSVC) 7 | add_definitions(/DNOMINMAX) 8 | endif() 9 | 10 | find_package( glm CONFIG REQUIRED ) 11 | find_package( glfw3 CONFIG REQUIRED ) 12 | find_package( OpenGL REQUIRED ) 13 | 14 | include_directories( ingredients ) 15 | 16 | add_subdirectory( ingredients ) 17 | add_subdirectory( chapter01 ) 18 | add_subdirectory( chapter02 ) 19 | add_subdirectory( chapter03 ) 20 | add_subdirectory( chapter04 ) 21 | add_subdirectory( chapter05 ) 22 | add_subdirectory( chapter06 ) 23 | add_subdirectory( chapter07 ) 24 | add_subdirectory( chapter08 ) 25 | add_subdirectory( chapter09 ) 26 | add_subdirectory( chapter10 ) 27 | 28 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/media DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /chapter01/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target chapter01) 2 | 3 | set( chapter01_SOURCES 4 | scenebasic.cpp 5 | main.cpp 6 | scenebasic_uniformblock.cpp 7 | scenebasic_attrib.cpp 8 | scenebasic_uniform.cpp 9 | scenebasic_debug.cpp ) 10 | 11 | add_executable( ${target} ${chapter01_SOURCES} ) 12 | 13 | target_compile_definitions(${target} 14 | PRIVATE 15 | ${DEFAULT_COMPILE_DEFINITIONS} 16 | GLFW_INCLUDE_NONE 17 | ) 18 | 19 | target_link_libraries( ${target} 20 | PRIVATE 21 | ingredients 22 | glfw 23 | ${OPENGL_gl_LIBRARY} 24 | ) 25 | 26 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 27 | -------------------------------------------------------------------------------- /chapter01/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scene.h" 2 | #include "scenerunner.h" 3 | #include "scenebasic.h" 4 | #include "scenebasic_attrib.h" 5 | #include "scenebasic_debug.h" 6 | #include "scenebasic_uniform.h" 7 | #include "scenebasic_uniformblock.h" 8 | 9 | #include 10 | 11 | static std::map< std::string, std::string > sceneData = { 12 | {"basic", "Basic scene."}, 13 | {"basic-attrib", "Prints active attributes."}, 14 | {"basic-debug", "Display debug messages."}, 15 | {"basic-uniform", "Basic scene with a uniform variable."}, 16 | {"basic-uniform-block", "Scene with a uniform block variable."} 17 | }; 18 | 19 | int main(int argc, char *argv[]) 20 | { 21 | std::string recipe = SceneRunner::parseCLArgs(argc, argv, sceneData); 22 | 23 | SceneRunner runner("Chapter 1 - " + recipe, 500, 500); 24 | 25 | std::unique_ptr scene; 26 | if( recipe == "basic" ) { 27 | scene = std::unique_ptr(new SceneBasic()); 28 | } else if( recipe == "basic-attrib") { 29 | scene = std::unique_ptr(new SceneBasic_Attrib()); 30 | } else if( recipe == "basic-uniform") { 31 | scene = std::unique_ptr(new SceneBasic_Uniform()); 32 | } else if( recipe == "basic-uniform-block" ) { 33 | scene = std::unique_ptr(new SceneBasic_UniformBlock()); 34 | } else if( recipe == "basic-debug" ) { 35 | scene = std::unique_ptr(new SceneBasic_Debug()); 36 | } 37 | 38 | return runner.run(*scene); 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter01/scenebasic.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBASIC_H 2 | #define SCENEBASIC_H 3 | 4 | #include "scene.h" 5 | 6 | #include "cookbookogl.h" 7 | 8 | class SceneBasic : public Scene 9 | { 10 | private: 11 | GLuint vaoHandle; 12 | 13 | void linkMe(GLint vertShader, GLint fragShader); 14 | 15 | public: 16 | SceneBasic(); 17 | 18 | void initScene(); 19 | void update( float t ); 20 | void render(); 21 | void resize(int, int); 22 | }; 23 | 24 | #endif // SCENEBASIC_H 25 | -------------------------------------------------------------------------------- /chapter01/scenebasic_attrib.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBASIC_ATTRIB_H 2 | #define SCENEBASIC_ATTRIB_H 3 | 4 | #include "scene.h" 5 | 6 | #include "cookbookogl.h" 7 | #include "glslprogram.h" 8 | 9 | class SceneBasic_Attrib : public Scene 10 | { 11 | private: 12 | GLuint vaoHandle; 13 | GLSLProgram prog; 14 | 15 | void compileShaderProgram(); 16 | 17 | public: 18 | SceneBasic_Attrib(); 19 | 20 | void initScene(); 21 | void update( float t ); 22 | void render(); 23 | void resize(int, int); 24 | }; 25 | 26 | #endif // SCENEBASIC_ATTRIB_H 27 | -------------------------------------------------------------------------------- /chapter01/scenebasic_debug.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBASIC_DEBUG_H 2 | #define SCENEBASIC_DEBUG_H 3 | 4 | #include "scene.h" 5 | 6 | #include "cookbookogl.h" 7 | #include "glslprogram.h" 8 | 9 | class SceneBasic_Debug : public Scene 10 | { 11 | private: 12 | GLuint vaoHandle; 13 | GLSLProgram prog; 14 | 15 | void compileShaderProgram(); 16 | 17 | public: 18 | SceneBasic_Debug(); 19 | 20 | void initScene(); 21 | void update( float t ); 22 | void render(); 23 | void resize(int, int); 24 | }; 25 | 26 | #endif // SCENEBASIC_DEBUG_H 27 | -------------------------------------------------------------------------------- /chapter01/scenebasic_uniform.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBASIC_UNIFORM_H 2 | #define SCENEBASIC_UNIFORM_H 3 | 4 | #include "scene.h" 5 | 6 | #include "cookbookogl.h" 7 | #include "glslprogram.h" 8 | 9 | #include 10 | 11 | class SceneBasic_Uniform : public Scene 12 | { 13 | private: 14 | GLuint vaoHandle; 15 | GLSLProgram prog; 16 | float angle; 17 | 18 | glm::mat4 rotationMatrix; 19 | 20 | void compile(); 21 | 22 | public: 23 | SceneBasic_Uniform(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEBASIC_UNIFORM_H 32 | -------------------------------------------------------------------------------- /chapter01/scenebasic_uniformblock.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBASIC_UNIFORMBLOCK_H 2 | #define SCENEBASIC_UNIFORMBLOCK_H 3 | 4 | #include "scene.h" 5 | 6 | #include "cookbookogl.h" 7 | #include "glslprogram.h" 8 | 9 | #include 10 | 11 | class SceneBasic_UniformBlock : public Scene 12 | { 13 | private: 14 | GLuint vaoHandle; 15 | GLSLProgram prog; 16 | float angle; 17 | 18 | void compile(); 19 | void initUniformBlockBuffer(); 20 | 21 | public: 22 | SceneBasic_UniformBlock(); 23 | 24 | void initScene(); 25 | void update( float t ); 26 | void render(); 27 | void resize(int, int); 28 | }; 29 | 30 | #endif // SCENEBASIC_UNIFORMBLOCK_H 31 | -------------------------------------------------------------------------------- /chapter01/shader/basic.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Color; 4 | layout (location=0) out vec4 FragColor; 5 | 6 | void main() { 7 | FragColor = vec4(Color, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /chapter01/shader/basic.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location=0) in vec3 VertexPosition; 4 | layout (location=1) in vec3 VertexColor; 5 | 6 | out vec3 Color; 7 | 8 | void main() 9 | { 10 | Color = VertexColor; 11 | 12 | gl_Position = vec4(VertexPosition,1.0); 13 | } 14 | -------------------------------------------------------------------------------- /chapter01/shader/basic_uniform.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Color; 4 | layout (location = 0) out vec4 FragColor; 5 | 6 | void main() { 7 | FragColor = vec4(Color, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /chapter01/shader/basic_uniform.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexColor; 5 | 6 | out vec3 Color; 7 | 8 | //uniform struct { 9 | // mat4 RotationMatrix; 10 | // mat4 ViewMatrix; } MyMats; 11 | 12 | uniform mat4 RotationMatrix; 13 | 14 | //uniform mat4 Mats[2]; 15 | 16 | void main() 17 | { 18 | Color = VertexColor; 19 | gl_Position = RotationMatrix * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter01/shader/basic_uniformblock.frag: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 TexCoord; 4 | layout (location = 0) out vec4 FragColor; 5 | 6 | layout (binding = 1) uniform BlobSettings { 7 | vec4 InnerColor; 8 | vec4 OuterColor; 9 | float RadiusInner; 10 | float RadiusOuter; 11 | } Blob; 12 | 13 | void main() { 14 | float dx = TexCoord.x - 0.5; 15 | float dy = TexCoord.y - 0.5; 16 | float dist = sqrt(dx * dx + dy * dy); 17 | FragColor = 18 | mix( Blob.InnerColor, Blob.OuterColor, 19 | smoothstep( Blob.RadiusInner, Blob.RadiusOuter, dist ) 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /chapter01/shader/basic_uniformblock.vert: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexTexCoord; 5 | 6 | out vec3 TexCoord; 7 | 8 | void main() 9 | { 10 | TexCoord = VertexTexCoord; 11 | gl_Position = vec4(VertexPosition,1.0); 12 | } 13 | -------------------------------------------------------------------------------- /chapter01/shader/basic_uniformblock_41.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 TexCoord; 4 | layout (location = 0) out vec4 FragColor; 5 | 6 | uniform BlobSettings { 7 | vec4 InnerColor; 8 | vec4 OuterColor; 9 | float RadiusInner; 10 | float RadiusOuter; 11 | } Blob; 12 | 13 | void main() { 14 | float dx = TexCoord.x - 0.5; 15 | float dy = TexCoord.y - 0.5; 16 | float dist = sqrt(dx * dx + dy * dy); 17 | FragColor = 18 | mix( Blob.InnerColor, Blob.OuterColor, 19 | smoothstep( Blob.RadiusInner, Blob.RadiusOuter, dist ) 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /chapter01/shader/basic_uniformblock_41.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexTexCoord; 5 | 6 | out vec3 TexCoord; 7 | 8 | void main() 9 | { 10 | TexCoord = VertexTexCoord; 11 | gl_Position = vec4(VertexPosition,1.0); 12 | } 13 | -------------------------------------------------------------------------------- /chapter02/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set( target chapter02 ) 2 | set( chapter02_SOURCES 3 | main.cpp 4 | scenesubroutine.cpp 5 | scenediffuse.cpp 6 | scenetwoside.cpp 7 | sceneflat.cpp 8 | scenediscard.cpp 9 | sceneads.cpp ) 10 | 11 | add_executable( ${target} ${chapter02_SOURCES} ) 12 | 13 | target_compile_definitions(${target} 14 | PRIVATE 15 | GLFW_INCLUDE_NONE 16 | ) 17 | 18 | target_link_libraries( ${target} 19 | PRIVATE 20 | ingredients 21 | glfw 22 | ${OPENGL_gl_LIBRARY} 23 | ) 24 | 25 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 26 | -------------------------------------------------------------------------------- /chapter02/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scene.h" 2 | #include "scenerunner.h" 3 | #include "sceneads.h" 4 | #include "scenediffuse.h" 5 | #include "scenediscard.h" 6 | #include "sceneflat.h" 7 | #include "scenesubroutine.h" 8 | #include "scenetwoside.h" 9 | 10 | #include 11 | 12 | static std::map< std::string, std::string > sceneData = { 13 | {"ads", "Ambient, diffuse, and specular shading."}, 14 | {"diffuse", "Diffuse shading only"}, 15 | {"discard", "Example of discarding fragments"}, 16 | {"flat", "Flat shading"}, 17 | {"subroutine", "Using a shader subroutine"}, 18 | {"two-side", "Two-sided lighting"} 19 | }; 20 | 21 | int main( int argc, char ** argv) { 22 | 23 | std::string recipeName = SceneRunner::parseCLArgs(argc, argv, sceneData); 24 | SceneRunner runner("Chapter 02 - " + recipeName); 25 | 26 | std::unique_ptr scene; 27 | if(recipeName == "ads") { 28 | scene = std::unique_ptr(new SceneADS()); 29 | } 30 | else if( recipeName == "diffuse") { 31 | scene = std::unique_ptr(new SceneDiffuse()); 32 | } 33 | else if( recipeName == "discard" ) { 34 | scene = std::unique_ptr(new SceneDiscard()); 35 | } 36 | else if( recipeName == "flat" ) { 37 | scene = std::unique_ptr(new SceneFlat()); 38 | } 39 | else if( recipeName == "subroutine") { 40 | scene = std::unique_ptr(new SceneSubroutine()); 41 | } 42 | else if( recipeName == "two-side" ) { 43 | scene = std::unique_ptr(new SceneTwoSide()); 44 | } 45 | 46 | return runner.run(*scene); 47 | } -------------------------------------------------------------------------------- /chapter02/sceneads.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEADS_H 2 | #define SCENEADS_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "torus.h" 7 | #include "cookbookogl.h" 8 | 9 | class SceneADS : public Scene 10 | { 11 | private: 12 | GLSLProgram prog; 13 | 14 | Torus torus; 15 | float angle; 16 | 17 | void setMatrices(); 18 | void compileAndLinkShader(); 19 | 20 | public: 21 | SceneADS(); 22 | 23 | void initScene(); 24 | void update( float t ); 25 | void render(); 26 | void resize(int, int); 27 | }; 28 | 29 | #endif // SCENEADS_H 30 | -------------------------------------------------------------------------------- /chapter02/scenediffuse.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEDIFFUSE_H 2 | #define SCENEDIFFUSE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "torus.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneDiffuse : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | Torus torus; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneDiffuse(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEDIFFUSE_H 32 | -------------------------------------------------------------------------------- /chapter02/scenediscard.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEDISCARD_H 2 | #define SCENEDISCARD_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapot.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | class SceneDiscard : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | Teapot teapot; 16 | 17 | void setMatrices(); 18 | void compileAndLinkShader(); 19 | 20 | public: 21 | SceneDiscard(); 22 | 23 | void initScene(); 24 | void update( float t ); 25 | void render(); 26 | void resize(int, int); 27 | }; 28 | 29 | #endif // SCENEDISCARD_H 30 | -------------------------------------------------------------------------------- /chapter02/sceneflat.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEFLAT_H 2 | #define SCENEFLAT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "objmesh.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | class SceneFlat : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | std::unique_ptr ogre; 16 | float angle; 17 | 18 | void setMatrices(); 19 | void compileAndLinkShader(); 20 | 21 | public: 22 | SceneFlat(); 23 | 24 | void initScene(); 25 | void update( float t ); 26 | void render(); 27 | void resize(int, int); 28 | }; 29 | 30 | #endif // SCENEFLAT_H 31 | -------------------------------------------------------------------------------- /chapter02/scenesubroutine.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESUBROUTINE_H 2 | #define SCENESUBROUTINE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapot.h" 7 | #include "torus.h" 8 | 9 | #include "cookbookogl.h" 10 | 11 | class SceneSubroutine : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | Teapot teapot; 17 | 18 | void setMatrices(); 19 | void compileAndLinkShader(); 20 | 21 | public: 22 | SceneSubroutine(); 23 | 24 | void initScene(); 25 | void update( float t ); 26 | void render(); 27 | void resize(int, int); 28 | }; 29 | 30 | #endif // SCENESUBROUTINE_H 31 | -------------------------------------------------------------------------------- /chapter02/scenetwoside.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENETWOSIDE_H 2 | #define SCENETWOSIDE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapot.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | class SceneTwoSide : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | Teapot teapot; 16 | float angle; 17 | 18 | void setMatrices(); 19 | void compileAndLinkShader(); 20 | 21 | public: 22 | SceneTwoSide(); 23 | 24 | void initScene(); 25 | void update( float t ); 26 | void render(); 27 | void resize(int, int); 28 | }; 29 | 30 | #endif // SCENETWOSIDE_H 31 | -------------------------------------------------------------------------------- /chapter02/shader/diffuse.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 LightIntensity; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(LightIntensity, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter02/shader/diffuse.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 LightIntensity; 7 | 8 | uniform vec4 LightPosition; // Light position in eye coords. 9 | uniform vec3 Kd; // Diffuse reflectivity 10 | uniform vec3 Ld; // Diffuse light intensity 11 | 12 | uniform mat4 ModelViewMatrix; 13 | uniform mat3 NormalMatrix; 14 | uniform mat4 ProjectionMatrix; 15 | uniform mat4 MVP; 16 | 17 | void main() 18 | { 19 | vec3 tnorm = normalize( NormalMatrix * VertexNormal); 20 | vec4 eyeCoords = ModelViewMatrix * vec4(VertexPosition,1.0); 21 | vec3 s = normalize(vec3(LightPosition - eyeCoords)); 22 | 23 | LightIntensity = Ld * Kd * max( dot( s, tnorm ), 0.0 ); 24 | 25 | gl_Position = MVP * vec4(VertexPosition,1.0); 26 | } 27 | -------------------------------------------------------------------------------- /chapter02/shader/discard.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 FrontColor; 4 | in vec3 BackColor; 5 | in vec2 TexCoord; 6 | 7 | layout( location = 0 ) out vec4 FragColor; 8 | 9 | void main() { 10 | const float scale = 15.0; 11 | 12 | bvec2 toDiscard = greaterThan( fract(TexCoord * scale), vec2(0.2,0.2) ); 13 | 14 | if( all(toDiscard) ) 15 | discard; 16 | else { 17 | if( gl_FrontFacing ) 18 | FragColor = vec4(FrontColor, 1.0); 19 | else 20 | FragColor = vec4(BackColor, 1.0); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter02/shader/flat.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | flat in vec3 LightIntensity; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(LightIntensity, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter02/shader/flat.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | flat out vec3 LightIntensity; 7 | 8 | struct LightInfo { 9 | vec4 Position; // Light position in eye coords. 10 | vec3 La; // Ambient light intensity 11 | vec3 Ld; // Diffuse light intensity 12 | vec3 Ls; // Specular light intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Kd; // Diffuse reflectivity 19 | vec3 Ks; // Specular reflectivity 20 | float Shininess; // Specular shininess factor 21 | }; 22 | uniform MaterialInfo Material; 23 | 24 | uniform mat4 ModelViewMatrix; 25 | uniform mat3 NormalMatrix; 26 | uniform mat4 ProjectionMatrix; 27 | uniform mat4 MVP; 28 | 29 | void main() 30 | { 31 | vec3 tnorm = normalize( NormalMatrix * VertexNormal); 32 | vec4 eyeCoords = ModelViewMatrix * vec4(VertexPosition,1.0); 33 | vec3 s = normalize(vec3(Light.Position - eyeCoords)); 34 | vec3 v = normalize(-eyeCoords.xyz); 35 | vec3 r = reflect( -s, tnorm ); 36 | float sDotN = max( dot(s,tnorm), 0.0 ); 37 | vec3 ambient = Light.La * Material.Ka; 38 | vec3 diffuse = Light.Ld * Material.Kd * sDotN; 39 | vec3 spec = vec3(0.0); 40 | if( sDotN > 0.0 ) 41 | spec = Light.Ls * Material.Ks * 42 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 43 | 44 | LightIntensity = ambient + diffuse + spec; 45 | gl_Position = MVP * vec4(VertexPosition,1.0); 46 | } 47 | -------------------------------------------------------------------------------- /chapter02/shader/function.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 LightIntensity; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(LightIntensity, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter02/shader/phong.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 LightIntensity; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(LightIntensity, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter02/shader/phong.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 LightIntensity; 7 | 8 | struct LightInfo { 9 | vec4 Position; // Light position in eye coords. 10 | vec3 La; // Ambient light intensity 11 | vec3 Ld; // Diffuse light intensity 12 | vec3 Ls; // Specular light intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Kd; // Diffuse reflectivity 19 | vec3 Ks; // Specular reflectivity 20 | float Shininess; // Specular shininess factor 21 | }; 22 | uniform MaterialInfo Material; 23 | 24 | uniform mat4 ModelViewMatrix; 25 | uniform mat3 NormalMatrix; 26 | uniform mat4 ProjectionMatrix; 27 | uniform mat4 MVP; 28 | 29 | void main() 30 | { 31 | vec3 tnorm = normalize( NormalMatrix * VertexNormal); 32 | vec4 eyeCoords = ModelViewMatrix * vec4(VertexPosition,1.0); 33 | vec3 s = normalize(vec3(Light.Position - eyeCoords)); 34 | vec3 v = normalize(-eyeCoords.xyz); 35 | vec3 r = reflect( -s, tnorm ); 36 | float sDotN = max( dot(s,tnorm), 0.0 ); 37 | vec3 ambient = Light.La * Material.Ka; 38 | vec3 diffuse = Light.Ld * Material.Kd * sDotN; 39 | vec3 spec = vec3(0.0); 40 | if( sDotN > 0.0 ) 41 | spec = Light.Ls * Material.Ks * 42 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 43 | 44 | LightIntensity = ambient + diffuse + spec; 45 | gl_Position = MVP * vec4(VertexPosition,1.0); 46 | } 47 | -------------------------------------------------------------------------------- /chapter02/shader/subroutine.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 LightIntensity; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(LightIntensity, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter02/shader/twoside.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 FrontColor; 4 | in vec3 BackColor; 5 | 6 | layout( location = 0 ) out vec4 FragColor; 7 | 8 | void main() { 9 | 10 | if( gl_FrontFacing ) { 11 | FragColor = vec4(FrontColor, 1.0); 12 | } else { 13 | //FragColor = mix( vec4(BackColor, 1.0), vec4(1.0,0.0,0.0,1.0), 0.7 ); 14 | FragColor = vec4(BackColor, 1.0); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter03/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target chapter03) 2 | set( chapter03_SOURCES 3 | scenemultilight.cpp 4 | sceneperfragment.cpp 5 | main.cpp 6 | scenefog.cpp 7 | scenedirectional.cpp 8 | scenetoon.cpp 9 | scenespot.cpp ) 10 | 11 | add_executable( ${target} ${chapter03_SOURCES} ) 12 | 13 | target_compile_definitions(${target} 14 | PRIVATE 15 | GLFW_INCLUDE_NONE 16 | ) 17 | 18 | target_link_libraries( ${target} 19 | PRIVATE 20 | ingredients 21 | glfw 22 | ${OPENGL_gl_LIBRARY} 23 | ) 24 | 25 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 26 | -------------------------------------------------------------------------------- /chapter03/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scene.h" 2 | #include "scenerunner.h" 3 | #include "scenedirectional.h" 4 | #include "scenefog.h" 5 | #include "scenemultilight.h" 6 | #include "sceneperfragment.h" 7 | #include "scenespot.h" 8 | #include "scenetoon.h" 9 | 10 | 11 | std::map sceneInfo = { 12 | { "directional", "Directional light source" }, 13 | { "fog", "Fog" }, 14 | { "multi-light", "Multiple light sources" }, 15 | { "per-frag", "Per-fragment shading" }, 16 | { "spot", "Spot light" }, 17 | { "toon", "Toon shading" } 18 | }; 19 | 20 | 21 | int main(int argc, char *argv[]) 22 | { 23 | std::string recipe = SceneRunner::parseCLArgs(argc, argv, sceneInfo); 24 | 25 | SceneRunner runner("Chapter 3 - " + recipe); 26 | std::unique_ptr scene; 27 | if( recipe == "directional" ) { 28 | scene = std::unique_ptr( new SceneDirectional() ); 29 | } else if( recipe == "fog") { 30 | scene = std::unique_ptr( new SceneFog() ); 31 | } else if( recipe == "multi-light") { 32 | scene = std::unique_ptr( new SceneMultiLight() ); 33 | } else if( recipe == "per-frag" ) { 34 | scene = std::unique_ptr( new ScenePerFragment() ); 35 | } else if( recipe == "spot") { 36 | scene = std::unique_ptr( new SceneSpot() ); 37 | } else if( recipe == "toon" ) { 38 | scene = std::unique_ptr( new SceneToon() ); 39 | } 40 | 41 | return runner.run(*scene); 42 | } 43 | -------------------------------------------------------------------------------- /chapter03/scenedirectional.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEDIRECTIONAL_H 2 | #define SCENEDIRECTIONAL_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "torus.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneDirectional : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | Torus torus; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneDirectional(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEDIRECTIONAL_H 32 | -------------------------------------------------------------------------------- /chapter03/scenefog.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEFOG_H 2 | #define SCENEFOG_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | 9 | #include "cookbookogl.h" 10 | 11 | #include 12 | 13 | class SceneFog : public Scene 14 | { 15 | private: 16 | GLSLProgram prog; 17 | 18 | float tPrev; 19 | Plane plane; 20 | Teapot teapot; 21 | 22 | float angle; 23 | 24 | void setMatrices(); 25 | void compileAndLinkShader(); 26 | 27 | public: 28 | SceneFog(); 29 | 30 | void initScene(); 31 | void update( float t ); 32 | void render(); 33 | void resize(int, int); 34 | }; 35 | 36 | #endif // SCENEFog_H 37 | -------------------------------------------------------------------------------- /chapter03/scenemultilight.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEMULTILIGHT_H 2 | #define SCENEMULTILIGHT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "objmesh.h" 8 | 9 | #include "cookbookogl.h" 10 | 11 | #include 12 | 13 | class SceneMultiLight : public Scene 14 | { 15 | private: 16 | GLSLProgram prog; 17 | 18 | Plane plane; 19 | std::unique_ptr mesh; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | 24 | public: 25 | SceneMultiLight(); 26 | 27 | void initScene(); 28 | void update( float t ); 29 | void render(); 30 | void resize(int, int); 31 | }; 32 | 33 | #endif // SCENEMULTILIGHT_H 34 | -------------------------------------------------------------------------------- /chapter03/sceneperfragment.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPERFRAGMENT_H 2 | #define SCENEPERFRAGMENT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | 9 | #include "cookbookogl.h" 10 | 11 | #include 12 | 13 | class ScenePerFragment : public Scene 14 | { 15 | private: 16 | GLSLProgram prog; 17 | 18 | float tPrev; 19 | Plane plane; 20 | Teapot teapot; 21 | 22 | float angle; 23 | 24 | void setMatrices(); 25 | void compileAndLinkShader(); 26 | 27 | public: 28 | ScenePerFragment(); 29 | 30 | void initScene(); 31 | void update( float t ); 32 | void render(); 33 | void resize(int, int); 34 | }; 35 | 36 | #endif // SCENEPERFRAGMENT_H 37 | -------------------------------------------------------------------------------- /chapter03/scenespot.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESPOT_H 2 | #define SCENESPOT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | #include "torus.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneSpot : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | float tPrev; 20 | Plane plane; 21 | Teapot teapot; 22 | Torus torus; 23 | 24 | float angle; 25 | 26 | void setMatrices(); 27 | void compileAndLinkShader(); 28 | 29 | public: 30 | SceneSpot(); 31 | 32 | void initScene(); 33 | void update( float t ); 34 | void render(); 35 | void resize(int, int); 36 | }; 37 | 38 | #endif // SCENESPOT_H 39 | -------------------------------------------------------------------------------- /chapter03/scenetoon.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENETOON_H 2 | #define SCENETOON_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | #include "torus.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneToon : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | float tPrev; 20 | Plane plane; 21 | Teapot teapot; 22 | Torus torus; 23 | 24 | float angle; 25 | 26 | void setMatrices(); 27 | void compileAndLinkShader(); 28 | 29 | public: 30 | SceneToon(); 31 | 32 | void initScene(); 33 | void update( float t ); 34 | void render(); 35 | void resize(int, int); 36 | }; 37 | 38 | #endif // SCENETOON_H 39 | -------------------------------------------------------------------------------- /chapter03/shader/directional.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Color; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(Color, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter03/shader/directional.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Color; 7 | 8 | // Normalized direction towards light source in eye coords. 9 | uniform vec4 LightPosition; 10 | uniform vec3 LightIntensity; 11 | 12 | uniform vec3 Kd; // Diffuse reflectivity 13 | uniform vec3 Ka; // Ambient reflectivity 14 | uniform vec3 Ks; // Specular reflectivity 15 | uniform float Shininess; // Specular shininess factor 16 | 17 | uniform mat4 ModelViewMatrix; 18 | uniform mat3 NormalMatrix; 19 | uniform mat4 ProjectionMatrix; 20 | uniform mat4 MVP; 21 | 22 | vec3 ads( vec4 position, vec3 norm ) 23 | { 24 | vec3 s; 25 | if( LightPosition.w == 0.0 ) 26 | s = normalize(vec3(LightPosition)); 27 | else 28 | s = normalize( vec3(LightPosition - position) ); 29 | 30 | vec3 v = normalize(vec3(-position)); 31 | vec3 r = reflect( -s, norm ); 32 | 33 | return 34 | LightIntensity * ( Ka + 35 | Kd * max( dot(s, norm), 0.0 ) + 36 | Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ); 37 | } 38 | 39 | void main() 40 | { 41 | vec3 eyeNorm = normalize( NormalMatrix * VertexNormal); 42 | vec4 eyePosition = ModelViewMatrix * vec4(VertexPosition,1.0);; 43 | 44 | // Evaluate the lighting equation 45 | Color = ads( eyePosition, eyeNorm ); 46 | 47 | gl_Position = MVP * vec4(VertexPosition,1.0); 48 | } 49 | -------------------------------------------------------------------------------- /chapter03/shader/fog.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | 6 | struct LightInfo { 7 | vec4 position; 8 | vec3 intensity; 9 | }; 10 | uniform LightInfo Light; 11 | 12 | struct FogInfo { 13 | float maxDist; 14 | float minDist; 15 | vec3 color; 16 | }; 17 | uniform FogInfo Fog; 18 | 19 | uniform vec3 Kd; // Diffuse reflectivity 20 | uniform vec3 Ka; // Ambient reflectivity 21 | uniform vec3 Ks; // Specular reflectivity 22 | uniform float Shininess; // Specular shininess factor 23 | 24 | layout( location = 0 ) out vec4 FragColor; 25 | 26 | vec3 ads( ) 27 | { 28 | vec3 s = normalize( Light.position.xyz - Position.xyz ); 29 | vec3 v = normalize(vec3(-Position)); 30 | vec3 h = normalize( v + s ); 31 | 32 | vec3 ambient = Ka; 33 | vec3 diffuse = Kd * max(0.0, dot(s, Normal) ); 34 | vec3 spec = Ks * pow( max( 0.0, dot( h, Normal) ), Shininess ); 35 | 36 | return Light.intensity * (ambient + diffuse + spec); 37 | } 38 | 39 | void main() { 40 | float dist = abs( Position.z ); 41 | float fogFactor = (Fog.maxDist - dist) / 42 | (Fog.maxDist - Fog.minDist); 43 | fogFactor = clamp( fogFactor, 0.0, 1.0 ); 44 | vec3 shadeColor = ads(); 45 | vec3 color = mix( Fog.color, shadeColor, fogFactor ); 46 | 47 | FragColor = vec4(color, 1.0); 48 | } 49 | -------------------------------------------------------------------------------- /chapter03/shader/fog.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter03/shader/halfway.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | 6 | uniform vec4 LightPosition; 7 | uniform vec3 LightIntensity; 8 | 9 | uniform vec3 Kd; // Diffuse reflectivity 10 | uniform vec3 Ka; // Ambient reflectivity 11 | uniform vec3 Ks; // Specular reflectivity 12 | uniform float Shininess; // Specular shininess factor 13 | 14 | layout( location = 0 ) out vec4 FragColor; 15 | 16 | vec3 ads( ) 17 | { 18 | vec3 s = normalize( vec3(LightPosition) - Position ); 19 | vec3 v = normalize(vec3(-Position)); 20 | vec3 h = normalize( v + s ); 21 | 22 | return 23 | LightIntensity * (Ka + 24 | Kd * max( dot(s, Normal), 0.0 ) + 25 | Ks * pow( max( dot(h,Normal), 0.0 ), Shininess ) ); 26 | } 27 | 28 | void main() { 29 | FragColor = vec4(ads(), 1.0); 30 | } 31 | -------------------------------------------------------------------------------- /chapter03/shader/halfway.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter03/shader/multilight.frag: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Color; 4 | 5 | layout( location = 0 ) out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = vec4(Color, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /chapter03/shader/multilight.vert: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Color; 7 | 8 | struct LightInfo { 9 | vec4 Position; // Light position in eye coords. 10 | vec3 Intensity; // Light intesity (amb., diff., and spec.) 11 | }; 12 | uniform LightInfo lights[5]; 13 | 14 | // Material parameters 15 | uniform vec3 Kd; // Diffuse reflectivity 16 | uniform vec3 Ka; // Ambient reflectivity 17 | uniform vec3 Ks; // Specular reflectivity 18 | uniform float Shininess; // Specular shininess factor 19 | 20 | uniform mat4 ModelViewMatrix; 21 | uniform mat3 NormalMatrix; 22 | uniform mat4 ProjectionMatrix; 23 | uniform mat4 MVP; 24 | 25 | vec3 ads( int lightIndex, vec4 position, vec3 norm ) 26 | { 27 | vec3 s = normalize( vec3(lights[lightIndex].Position - position) ); 28 | vec3 v = normalize(vec3(-position)); 29 | vec3 r = reflect( -s, norm ); 30 | vec3 I = lights[lightIndex].Intensity; 31 | return 32 | I * ( Ka + 33 | Kd * max( dot(s, norm), 0.0 ) + 34 | Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ); 35 | } 36 | 37 | void main() 38 | { 39 | vec3 eyeNorm = normalize( NormalMatrix * VertexNormal); 40 | vec4 eyePosition = ModelViewMatrix * vec4(VertexPosition,1.0); 41 | 42 | // Evaluate the lighting equation, for each light 43 | Color = vec3(0.0); 44 | for( int i = 0; i < 5; i++ ) 45 | Color += ads( i, eyePosition, eyeNorm ); 46 | 47 | gl_Position = MVP * vec4(VertexPosition,1.0); 48 | } 49 | -------------------------------------------------------------------------------- /chapter03/shader/perfrag.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | 6 | uniform vec4 LightPosition; 7 | uniform vec3 LightIntensity; 8 | 9 | uniform vec3 Kd; // Diffuse reflectivity 10 | uniform vec3 Ka; // Ambient reflectivity 11 | uniform vec3 Ks; // Specular reflectivity 12 | uniform float Shininess; // Specular shininess factor 13 | 14 | layout( location = 0 ) out vec4 FragColor; 15 | 16 | vec3 ads( ) 17 | { 18 | vec3 n = normalize(Normal); 19 | vec3 s = normalize( vec3(LightPosition) - Position ); 20 | vec3 v = normalize(vec3(-Position)); 21 | vec3 r = reflect( -s, n ); 22 | 23 | return 24 | LightIntensity * ( Ka + 25 | Kd * max( dot(s, n), 0.0 ) + 26 | Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ); 27 | } 28 | 29 | void main() { 30 | FragColor = vec4(ads(), 1.0); 31 | } 32 | -------------------------------------------------------------------------------- /chapter03/shader/perfrag.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter03/shader/spot.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | 6 | struct SpotLightInfo { 7 | vec4 position; // Position in eye coords 8 | vec3 intensity; 9 | vec3 direction; // Direction of the spotlight in eye coords. 10 | float exponent; // Angular attenuation exponent 11 | float cutoff; // Cutoff angle (between 0 and 90) 12 | }; 13 | uniform SpotLightInfo Spot; 14 | 15 | uniform vec3 Kd; // Diffuse reflectivity 16 | uniform vec3 Ka; // Ambient reflectivity 17 | uniform vec3 Ks; // Specular reflectivity 18 | uniform float Shininess; // Specular shininess factor 19 | 20 | layout( location = 0 ) out vec4 FragColor; 21 | 22 | vec3 adsWithSpotlight( ) 23 | { 24 | vec3 n = normalize(Normal); 25 | vec3 s = normalize( vec3( Spot.position) - Position ); 26 | vec3 spotDir = normalize( Spot.direction); 27 | float angle = acos( dot(-s, spotDir) ); 28 | float cutoff = radians( clamp( Spot.cutoff, 0.0, 90.0 ) ); 29 | vec3 ambient = Spot.intensity * Ka; 30 | 31 | if( angle < cutoff ) { 32 | float spotFactor = pow( dot(-s, spotDir), Spot.exponent ); 33 | vec3 v = normalize(vec3(-Position)); 34 | vec3 h = normalize( v + s ); 35 | 36 | return 37 | ambient + 38 | spotFactor * Spot.intensity * ( 39 | Kd * max( dot(s, n), 0.0 ) + 40 | Ks * pow( max( dot(h,n), 0.0 ), Shininess ) 41 | ); 42 | } else { 43 | return ambient; 44 | } 45 | } 46 | 47 | void main() { 48 | FragColor = vec4(adsWithSpotlight(), 1.0); 49 | } 50 | -------------------------------------------------------------------------------- /chapter03/shader/spot.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter03/shader/toon.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | 6 | struct LightInfo { 7 | vec4 position; 8 | vec3 intensity; 9 | }; 10 | uniform LightInfo Light; 11 | 12 | uniform vec3 Kd; // Diffuse reflectivity 13 | uniform vec3 Ka; // Ambient reflectivity 14 | 15 | const int levels = 3; 16 | const float scaleFactor = 1.0 / levels; 17 | 18 | layout( location = 0 ) out vec4 FragColor; 19 | 20 | vec3 toonShade( ) 21 | { 22 | vec3 n = normalize(Normal); 23 | vec3 s = normalize( Light.position.xyz - Position.xyz ); 24 | vec3 ambient = Ka; 25 | float cosine = dot( s, n ); 26 | vec3 diffuse = Kd * floor( cosine * levels ) * scaleFactor; 27 | 28 | return Light.intensity * (ambient + diffuse); 29 | } 30 | 31 | void main() { 32 | FragColor = vec4(toonShade(), 1.0); 33 | } 34 | -------------------------------------------------------------------------------- /chapter03/shader/toon.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter04/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set( target chapter04 ) 2 | set( chapter04_SOURCES 3 | scenetexture.cpp 4 | main.cpp 5 | scenerendertotex.cpp 6 | scenemultitex.cpp 7 | scenealphatest.cpp 8 | scenereflectcube.cpp 9 | skybox.cpp 10 | scenenormalmap.cpp 11 | sceneprojtex.cpp 12 | scenerefractcube.cpp 13 | scenesamplerobj.cpp ) 14 | 15 | add_executable( ${target} ${chapter04_SOURCES} ) 16 | target_compile_definitions(${target} 17 | PRIVATE 18 | GLFW_INCLUDE_NONE 19 | ) 20 | 21 | target_link_libraries( ${target} 22 | PRIVATE 23 | ingredients 24 | glfw 25 | ${OPENGL_gl_LIBRARY} 26 | ) 27 | 28 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 29 | -------------------------------------------------------------------------------- /chapter04/scenealphatest.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEALPHATEST_H 2 | #define SCENEALPHATEST_H 3 | 4 | #include "scene.h" 5 | #include "teapot.h" 6 | #include "glslprogram.h" 7 | 8 | #include 9 | 10 | class SceneAlphaTest : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | Teapot teapot; 16 | 17 | float angle, tPrev, rotSpeed; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneAlphaTest(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEALPHATEST_H 32 | -------------------------------------------------------------------------------- /chapter04/scenemultitex.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEMULTITEX_H 2 | #define SCENEMULTITEX_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cube.h" 7 | 8 | #include 9 | 10 | class SceneMultiTex : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | Cube cube; 16 | 17 | void setMatrices(); 18 | void compileAndLinkShader(); 19 | 20 | public: 21 | SceneMultiTex(); 22 | 23 | void initScene(); 24 | void update( float t ); 25 | void render(); 26 | void resize(int, int); 27 | }; 28 | 29 | #endif // SCENEMULTITEX_H 30 | -------------------------------------------------------------------------------- /chapter04/scenenormalmap.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENENORMALMAP_H 2 | #define SCENENORMALMAP_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "objmesh.h" 7 | 8 | #include 9 | 10 | class SceneNormalMap : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | std::unique_ptr ogre; 16 | 17 | float angle, tPrev, rotSpeed; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneNormalMap(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENENORMALMAP_H 32 | -------------------------------------------------------------------------------- /chapter04/sceneprojtex.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPROJTEX_H 2 | #define SCENEPROJTEX_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | 9 | #include 10 | 11 | class SceneProjTex : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | Teapot teapot; 17 | Plane plane; 18 | 19 | float angle, tPrev, rotSpeed; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | 24 | public: 25 | SceneProjTex(); 26 | 27 | void initScene(); 28 | void update( float t ); 29 | void render(); 30 | void resize(int, int); 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /chapter04/scenereflectcube.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEREFLECTCUBE_H 2 | #define SCENEREFLECTCUBE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapot.h" 7 | #include "skybox.h" 8 | 9 | #include 10 | 11 | class SceneReflectCube : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | Teapot teapot; 17 | SkyBox sky; 18 | 19 | float angle, tPrev, rotSpeed; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | void loadCubeMap( const char * baseFileName ); 24 | 25 | public: 26 | SceneReflectCube(); 27 | 28 | void initScene(); 29 | void update( float t ); 30 | void render(); 31 | void resize(int, int); 32 | }; 33 | 34 | #endif // SCENEREFLECTCUBE_H 35 | -------------------------------------------------------------------------------- /chapter04/scenerefractcube.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEREFRACTCUBE_H 2 | #define SCENEREFRACTCUBE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapot.h" 7 | #include "skybox.h" 8 | 9 | #include 10 | 11 | class SceneRefractCube : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | Teapot teapot; 17 | SkyBox sky; 18 | 19 | float angle, tPrev, rotSpeed; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | void loadCubeMap( const char * baseFileName ); 24 | 25 | public: 26 | SceneRefractCube(); 27 | 28 | void initScene(); 29 | void update( float t ); 30 | void render(); 31 | void resize(int, int); 32 | }; 33 | 34 | #endif // SCENEREFRACTCUBE_H 35 | -------------------------------------------------------------------------------- /chapter04/scenerendertotex.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENERENDERTOTEX_H 2 | #define SCENERENDERTOTEX_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cube.h" 7 | #include "teapot.h" 8 | 9 | #include 10 | 11 | class SceneRenderToTex : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | GLuint fboHandle; 17 | Cube cube; 18 | Teapot teapot; 19 | 20 | float angle; 21 | float tPrev, rotSpeed; 22 | 23 | void setMatrices(); 24 | void compileAndLinkShader(); 25 | void setupFBO(); 26 | void renderToTexture(); 27 | void renderScene(); 28 | 29 | public: 30 | SceneRenderToTex(); 31 | 32 | void initScene(); 33 | void update( float t ); 34 | void render(); 35 | void resize(int, int); 36 | }; 37 | 38 | #endif // SCENERENDERTOTEX_H 39 | -------------------------------------------------------------------------------- /chapter04/scenesamplerobj.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESAMPLEROBJ_H 2 | #define SCENESAMPLEROBJ_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | 8 | #include 9 | 10 | class SceneSamplerObj : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | Plane plane; 16 | 17 | float angle; 18 | GLuint linearSampler, nearestSampler; 19 | 20 | void setMatrices(); 21 | void compileAndLinkShader(); 22 | 23 | public: 24 | SceneSamplerObj(); 25 | 26 | void initScene(); 27 | void update( float t ); 28 | void render(); 29 | void resize(int, int); 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /chapter04/scenetexture.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENETEXTURE_H 2 | #define SCENETEXTURE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cube.h" 7 | 8 | #include 9 | 10 | class SceneTexture : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | Cube cube; 16 | 17 | float angle; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneTexture(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEPERFRAGMENT_H 32 | -------------------------------------------------------------------------------- /chapter04/shader/alphatest.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | layout(binding=0) uniform sampler2D BaseTex; 8 | layout(binding=1) uniform sampler2D AlphaTex; 9 | 10 | struct LightInfo { 11 | vec4 Position; // Light position in eye coords. 12 | vec3 Intensity; // A,D,S intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Kd; // Diffuse reflectivity 19 | vec3 Ks; // Specular reflectivity 20 | float Shininess; // Specular shininess factor 21 | }; 22 | uniform MaterialInfo Material; 23 | 24 | layout( location = 0 ) out vec4 FragColor; 25 | 26 | vec3 phongModel( vec3 pos, vec3 norm ) { 27 | vec3 s = normalize(vec3(Light.Position) - pos); 28 | vec3 v = normalize(-pos.xyz); 29 | vec3 r = reflect( -s, norm ); 30 | vec3 ambient = Light.Intensity * Material.Ka; 31 | float sDotN = max( dot(s,norm), 0.0 ); 32 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 33 | vec3 spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | 38 | return ambient + diffuse + spec; 39 | } 40 | 41 | void main() { 42 | vec4 baseColor = texture( BaseTex, TexCoord ); 43 | vec4 alphaMap = texture( AlphaTex, TexCoord ); 44 | 45 | if(alphaMap.a < 0.15 ) 46 | discard; 47 | else { 48 | FragColor = vec4( phongModel(Position,Normal), 1.0 ) * 49 | baseColor; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter04/shader/alphatest.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/shader/alphatest_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform sampler2D BaseTex; 8 | uniform sampler2D AlphaTex; 9 | 10 | struct LightInfo { 11 | vec4 Position; // Light position in eye coords. 12 | vec3 Intensity; // A,D,S intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Kd; // Diffuse reflectivity 19 | vec3 Ks; // Specular reflectivity 20 | float Shininess; // Specular shininess factor 21 | }; 22 | uniform MaterialInfo Material; 23 | 24 | layout( location = 0 ) out vec4 FragColor; 25 | 26 | vec3 phongModel( vec3 pos, vec3 norm ) { 27 | vec3 s = normalize(vec3(Light.Position) - pos); 28 | vec3 v = normalize(-pos.xyz); 29 | vec3 r = reflect( -s, norm ); 30 | vec3 ambient = Light.Intensity * Material.Ka; 31 | float sDotN = max( dot(s,norm), 0.0 ); 32 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 33 | vec3 spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | 38 | return ambient + diffuse + spec; 39 | } 40 | 41 | void main() { 42 | vec4 baseColor = texture( BaseTex, TexCoord ); 43 | vec4 alphaMap = texture( AlphaTex, TexCoord ); 44 | 45 | if(alphaMap.a < 0.15 ) 46 | discard; 47 | else { 48 | FragColor = vec4( phongModel(Position,Normal), 1.0 ) * 49 | baseColor; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter04/shader/alphatest_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_reflect.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 ReflectDir; 4 | 5 | layout(binding=0) uniform samplerCube CubeMapTex; 6 | 7 | uniform bool DrawSkyBox; 8 | uniform float ReflectFactor; 9 | uniform vec4 MaterialColor; 10 | 11 | layout( location = 0 ) out vec4 FragColor; 12 | 13 | void main() { 14 | // Access the cube map texture 15 | vec4 cubeMapColor = texture(CubeMapTex, ReflectDir); 16 | 17 | if( DrawSkyBox ) 18 | FragColor = cubeMapColor; 19 | else 20 | FragColor = mix( MaterialColor, cubeMapColor, ReflectFactor); 21 | } 22 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_reflect.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 ReflectDir; 8 | 9 | uniform bool DrawSkyBox; 10 | uniform vec3 WorldCameraPosition; 11 | 12 | uniform mat4 ModelViewMatrix; 13 | uniform mat4 ModelMatrix; 14 | uniform mat3 NormalMatrix; 15 | uniform mat4 ProjectionMatrix; 16 | uniform mat4 MVP; 17 | 18 | void main() 19 | { 20 | if( DrawSkyBox ) { 21 | ReflectDir = VertexPosition; 22 | } else { 23 | vec3 worldPos = vec3( ModelMatrix * vec4(VertexPosition,1.0) ); 24 | vec3 worldNorm = vec3(ModelMatrix * vec4(VertexNormal, 0.0)); 25 | vec3 worldView = normalize( WorldCameraPosition - worldPos ); 26 | 27 | ReflectDir = reflect(-worldView, worldNorm ); 28 | } 29 | 30 | gl_Position = MVP * vec4(VertexPosition,1.0); 31 | } 32 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_reflect_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 ReflectDir; 4 | 5 | uniform samplerCube CubeMapTex; 6 | 7 | uniform bool DrawSkyBox; 8 | uniform float ReflectFactor; 9 | uniform vec4 MaterialColor; 10 | 11 | layout( location = 0 ) out vec4 FragColor; 12 | 13 | void main() { 14 | // Access the cube map texture 15 | vec4 cubeMapColor = texture(CubeMapTex, ReflectDir); 16 | 17 | if( DrawSkyBox ) 18 | FragColor = cubeMapColor; 19 | else 20 | FragColor = mix( MaterialColor, cubeMapColor, ReflectFactor); 21 | } 22 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_reflect_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 ReflectDir; 8 | 9 | uniform bool DrawSkyBox; 10 | uniform vec3 WorldCameraPosition; 11 | 12 | uniform mat4 ModelViewMatrix; 13 | uniform mat4 ModelMatrix; 14 | uniform mat3 NormalMatrix; 15 | uniform mat4 ProjectionMatrix; 16 | uniform mat4 MVP; 17 | 18 | void main() 19 | { 20 | if( DrawSkyBox ) { 21 | ReflectDir = VertexPosition; 22 | } else { 23 | vec3 worldPos = vec3( ModelMatrix * vec4(VertexPosition,1.0) ); 24 | vec3 worldNorm = vec3(ModelMatrix * vec4(VertexNormal, 0.0)); 25 | vec3 worldView = normalize( WorldCameraPosition - worldPos ); 26 | 27 | ReflectDir = reflect(-worldView, worldNorm ); 28 | } 29 | 30 | gl_Position = MVP * vec4(VertexPosition,1.0); 31 | } 32 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_refract.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 ReflectDir; 4 | in vec3 RefractDir; 5 | 6 | layout(binding=0) uniform samplerCube CubeMapTex; 7 | 8 | uniform bool DrawSkyBox; 9 | 10 | struct MaterialInfo { 11 | float Eta; // Index of refraction 12 | float ReflectionFactor; // Percentage of reflected light 13 | }; 14 | uniform MaterialInfo Material; 15 | 16 | layout( location = 0 ) out vec4 FragColor; 17 | 18 | void main() { 19 | // Access the cube map texture 20 | vec4 reflectColor = texture(CubeMapTex, ReflectDir); 21 | vec4 refractColor = texture(CubeMapTex, RefractDir); 22 | 23 | if( DrawSkyBox ) 24 | FragColor = reflectColor; 25 | else 26 | FragColor = mix(refractColor, reflectColor, Material.ReflectionFactor); 27 | } 28 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_refract.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 ReflectDir; 8 | out vec3 RefractDir; 9 | 10 | struct MaterialInfo { 11 | float Eta; // Index of refraction 12 | float ReflectionFactor; // Percentage of reflected light 13 | }; 14 | uniform MaterialInfo Material; 15 | 16 | uniform bool DrawSkyBox; 17 | 18 | uniform vec3 WorldCameraPosition; 19 | uniform mat4 ModelViewMatrix; 20 | uniform mat4 ModelMatrix; 21 | uniform mat3 NormalMatrix; 22 | uniform mat4 ProjectionMatrix; 23 | uniform mat4 MVP; 24 | 25 | void main() 26 | { 27 | if( DrawSkyBox ) { 28 | ReflectDir = VertexPosition; 29 | } else { 30 | vec3 worldPos = vec3( ModelMatrix * vec4(VertexPosition,1.0) ); 31 | vec3 worldNorm = vec3(ModelMatrix * vec4(VertexNormal, 0.0)); 32 | vec3 worldView = normalize( WorldCameraPosition - worldPos ); 33 | 34 | ReflectDir = reflect(-worldView, worldNorm ); 35 | RefractDir = refract(-worldView, worldNorm, Material.Eta ); 36 | } 37 | gl_Position = MVP * vec4(VertexPosition,1.0); 38 | } 39 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_refract_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 ReflectDir; 4 | in vec3 RefractDir; 5 | 6 | uniform samplerCube CubeMapTex; 7 | 8 | uniform bool DrawSkyBox; 9 | 10 | struct MaterialInfo { 11 | float Eta; // Index of refraction 12 | float ReflectionFactor; // Percentage of reflected light 13 | }; 14 | uniform MaterialInfo Material; 15 | 16 | layout( location = 0 ) out vec4 FragColor; 17 | 18 | void main() { 19 | // Access the cube map texture 20 | vec4 reflectColor = texture(CubeMapTex, ReflectDir); 21 | vec4 refractColor = texture(CubeMapTex, RefractDir); 22 | 23 | if( DrawSkyBox ) 24 | FragColor = reflectColor; 25 | else 26 | FragColor = mix(refractColor, reflectColor, Material.ReflectionFactor); 27 | } 28 | -------------------------------------------------------------------------------- /chapter04/shader/cubemap_refract_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 ReflectDir; 8 | out vec3 RefractDir; 9 | 10 | struct MaterialInfo { 11 | float Eta; // Index of refraction 12 | float ReflectionFactor; // Percentage of reflected light 13 | }; 14 | uniform MaterialInfo Material; 15 | 16 | uniform bool DrawSkyBox; 17 | 18 | uniform vec3 WorldCameraPosition; 19 | uniform mat4 ModelViewMatrix; 20 | uniform mat4 ModelMatrix; 21 | uniform mat3 NormalMatrix; 22 | uniform mat4 ProjectionMatrix; 23 | uniform mat4 MVP; 24 | 25 | void main() 26 | { 27 | if( DrawSkyBox ) { 28 | ReflectDir = VertexPosition; 29 | } else { 30 | vec3 worldPos = vec3( ModelMatrix * vec4(VertexPosition,1.0) ); 31 | vec3 worldNorm = vec3(ModelMatrix * vec4(VertexNormal, 0.0)); 32 | vec3 worldView = normalize( WorldCameraPosition - worldPos ); 33 | 34 | ReflectDir = reflect(-worldView, worldNorm ); 35 | RefractDir = refract(-worldView, worldNorm, Material.Eta ); 36 | } 37 | gl_Position = MVP * vec4(VertexPosition,1.0); 38 | } 39 | -------------------------------------------------------------------------------- /chapter04/shader/multitex.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | layout(binding=0) uniform sampler2D BrickTex; 8 | layout(binding=1) uniform sampler2D MossTex; 9 | 10 | struct LightInfo { 11 | vec4 Position; // Light position in eye coords. 12 | vec3 Intensity; // A,D,S intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Kd; // Diffuse reflectivity 19 | vec3 Ks; // Specular reflectivity 20 | float Shininess; // Specular shininess factor 21 | }; 22 | uniform MaterialInfo Material; 23 | 24 | layout( location = 0 ) out vec4 FragColor; 25 | 26 | void phongModel( vec3 pos, vec3 norm, out vec3 ambAndDiff, out vec3 spec ) { 27 | vec3 s = normalize(vec3(Light.Position) - pos); 28 | vec3 v = normalize(-pos.xyz); 29 | vec3 r = reflect( -s, norm ); 30 | vec3 ambient = Light.Intensity * Material.Ka; 31 | float sDotN = max( dot(s,norm), 0.0 ); 32 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 33 | spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | ambAndDiff = ambient + diffuse; 38 | } 39 | 40 | 41 | void main() { 42 | vec3 ambAndDiff, spec; 43 | vec4 brickTexColor = texture( BrickTex, TexCoord ); 44 | vec4 mossTexColor = texture( MossTex, TexCoord ); 45 | phongModel(Position, Normal, ambAndDiff, spec ); 46 | vec4 texColor = mix(brickTexColor, mossTexColor, mossTexColor.a); 47 | FragColor = vec4(ambAndDiff, 1.0 ) * texColor + vec4(spec,1); 48 | } 49 | -------------------------------------------------------------------------------- /chapter04/shader/multitex.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/shader/multitex_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform sampler2D BrickTex; 8 | uniform sampler2D MossTex; 9 | 10 | struct LightInfo { 11 | vec4 Position; // Light position in eye coords. 12 | vec3 Intensity; // A,D,S intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Kd; // Diffuse reflectivity 19 | vec3 Ks; // Specular reflectivity 20 | float Shininess; // Specular shininess factor 21 | }; 22 | uniform MaterialInfo Material; 23 | 24 | layout( location = 0 ) out vec4 FragColor; 25 | 26 | void phongModel( vec3 pos, vec3 norm, out vec3 ambAndDiff, out vec3 spec ) { 27 | vec3 s = normalize(vec3(Light.Position) - pos); 28 | vec3 v = normalize(-pos.xyz); 29 | vec3 r = reflect( -s, norm ); 30 | vec3 ambient = Light.Intensity * Material.Ka; 31 | float sDotN = max( dot(s,norm), 0.0 ); 32 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 33 | spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | ambAndDiff = ambient + diffuse; 38 | } 39 | 40 | 41 | void main() { 42 | vec3 ambAndDiff, spec; 43 | vec4 brickTexColor = texture( BrickTex, TexCoord ); 44 | vec4 mossTexColor = texture( MossTex, TexCoord ); 45 | phongModel(Position, Normal, ambAndDiff, spec ); 46 | vec4 texColor = mix(brickTexColor, mossTexColor, mossTexColor.a); 47 | FragColor = vec4(ambAndDiff, 1.0 ) * texColor + vec4(spec,1); 48 | } 49 | -------------------------------------------------------------------------------- /chapter04/shader/multitex_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/shader/normalmap.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 LightDir; 4 | in vec2 TexCoord; 5 | in vec3 ViewDir; 6 | 7 | layout(binding=0) uniform sampler2D ColorTex; 8 | layout(binding=1) uniform sampler2D NormalMapTex; 9 | 10 | struct LightInfo { 11 | vec4 Position; // Light position in eye coords. 12 | vec3 Intensity; // A,D,S intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Ks; // Specular reflectivity 19 | float Shininess; // Specular shininess factor 20 | }; 21 | uniform MaterialInfo Material; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | vec3 phongModel( vec3 norm, vec3 diffR ) { 26 | vec3 r = reflect( -LightDir, norm ); 27 | vec3 ambient = Light.Intensity * Material.Ka; 28 | float sDotN = max( dot(LightDir, norm), 0.0 ); 29 | vec3 diffuse = Light.Intensity * diffR * sDotN; 30 | 31 | vec3 spec = vec3(0.0); 32 | if( sDotN > 0.0 ) 33 | spec = Light.Intensity * Material.Ks * 34 | pow( max( dot(r,ViewDir), 0.0 ), Material.Shininess ); 35 | 36 | return ambient + diffuse + spec; 37 | } 38 | 39 | void main() { 40 | // Lookup the normal from the normal map 41 | vec4 normal = 2.0 * texture( NormalMapTex, TexCoord ) - 1.0; 42 | 43 | vec4 texColor = texture( ColorTex, TexCoord ); 44 | FragColor = vec4( phongModel(normal.xyz, texColor.rgb), 1.0 ); 45 | } 46 | -------------------------------------------------------------------------------- /chapter04/shader/normalmap.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | layout (location = 2) in vec2 VertexTexCoord; 6 | layout (location = 3) in vec4 VertexTangent; 7 | 8 | struct LightInfo { 9 | vec4 Position; // Light position in eye coords. 10 | vec3 Intensity; // A,D,S intensity 11 | }; 12 | uniform LightInfo Light; 13 | 14 | out vec3 LightDir; 15 | out vec2 TexCoord; 16 | out vec3 ViewDir; 17 | 18 | uniform mat4 ModelViewMatrix; 19 | uniform mat3 NormalMatrix; 20 | uniform mat4 ProjectionMatrix; 21 | uniform mat4 MVP; 22 | 23 | void main() 24 | { 25 | // Transform normal and tangent to eye space 26 | vec3 norm = normalize( NormalMatrix * VertexNormal ); 27 | vec3 tang = normalize( NormalMatrix * vec3(VertexTangent) ); 28 | // Compute the binormal 29 | vec3 binormal = normalize( cross( norm, tang ) ) * VertexTangent.w; 30 | 31 | // Matrix for transformation to tangent space 32 | mat3 toObjectLocal = mat3( 33 | tang.x, binormal.x, norm.x, 34 | tang.y, binormal.y, norm.y, 35 | tang.z, binormal.z, norm.z ) ; 36 | 37 | // Transform light direction and view direction to tangent space 38 | vec3 pos = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 39 | LightDir = normalize( toObjectLocal * (Light.Position.xyz - pos) ); 40 | 41 | ViewDir = toObjectLocal * normalize(-pos); 42 | 43 | TexCoord = VertexTexCoord; 44 | 45 | gl_Position = MVP * vec4(VertexPosition,1.0); 46 | } 47 | -------------------------------------------------------------------------------- /chapter04/shader/normalmap_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 LightDir; 4 | in vec2 TexCoord; 5 | in vec3 ViewDir; 6 | 7 | uniform sampler2D ColorTex; 8 | uniform sampler2D NormalMapTex; 9 | 10 | struct LightInfo { 11 | vec4 Position; // Light position in eye coords. 12 | vec3 Intensity; // A,D,S intensity 13 | }; 14 | uniform LightInfo Light; 15 | 16 | struct MaterialInfo { 17 | vec3 Ka; // Ambient reflectivity 18 | vec3 Ks; // Specular reflectivity 19 | float Shininess; // Specular shininess factor 20 | }; 21 | uniform MaterialInfo Material; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | vec3 phongModel( vec3 norm, vec3 diffR ) { 26 | vec3 r = reflect( -LightDir, norm ); 27 | vec3 ambient = Light.Intensity * Material.Ka; 28 | float sDotN = max( dot(LightDir, norm), 0.0 ); 29 | vec3 diffuse = Light.Intensity * diffR * sDotN; 30 | 31 | vec3 spec = vec3(0.0); 32 | if( sDotN > 0.0 ) 33 | spec = Light.Intensity * Material.Ks * 34 | pow( max( dot(r,ViewDir), 0.0 ), Material.Shininess ); 35 | 36 | return ambient + diffuse + spec; 37 | } 38 | 39 | void main() { 40 | // Lookup the normal from the normal map 41 | vec4 normal = 2.0 * texture( NormalMapTex, TexCoord ) - 1.0; 42 | 43 | vec4 texColor = texture( ColorTex, TexCoord ); 44 | FragColor = vec4( phongModel(normal.xyz, texColor.rgb), 1.0 ); 45 | } 46 | -------------------------------------------------------------------------------- /chapter04/shader/normalmap_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | layout (location = 2) in vec2 VertexTexCoord; 6 | layout (location = 3) in vec4 VertexTangent; 7 | 8 | struct LightInfo { 9 | vec4 Position; // Light position in eye coords. 10 | vec3 Intensity; // A,D,S intensity 11 | }; 12 | uniform LightInfo Light; 13 | 14 | out vec3 LightDir; 15 | out vec2 TexCoord; 16 | out vec3 ViewDir; 17 | 18 | uniform mat4 ModelViewMatrix; 19 | uniform mat3 NormalMatrix; 20 | uniform mat4 ProjectionMatrix; 21 | uniform mat4 MVP; 22 | 23 | void main() 24 | { 25 | // Transform normal and tangent to eye space 26 | vec3 norm = normalize( NormalMatrix * VertexNormal ); 27 | vec3 tang = normalize( NormalMatrix * vec3(VertexTangent) ); 28 | // Compute the binormal 29 | vec3 binormal = normalize( cross( norm, tang ) ) * VertexTangent.w; 30 | 31 | // Matrix for transformation to tangent space 32 | mat3 toObjectLocal = mat3( 33 | tang.x, binormal.x, norm.x, 34 | tang.y, binormal.y, norm.y, 35 | tang.z, binormal.z, norm.z ) ; 36 | 37 | // Transform light direction and view direction to tangent space 38 | vec3 pos = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 39 | LightDir = normalize( toObjectLocal * (Light.Position.xyz - pos) ); 40 | 41 | ViewDir = toObjectLocal * normalize(-pos); 42 | 43 | TexCoord = VertexTexCoord; 44 | 45 | gl_Position = MVP * vec4(VertexPosition,1.0); 46 | } 47 | -------------------------------------------------------------------------------- /chapter04/shader/projtex.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 EyeNormal; // Normal in eye coordinates 4 | in vec4 EyePosition; // Position in eye coordinates 5 | in vec4 ProjTexCoord; 6 | 7 | layout(binding=0) uniform sampler2D ProjectorTex; 8 | 9 | struct MaterialInfo { 10 | vec3 Kd; 11 | vec3 Ks; 12 | vec3 Ka; 13 | float Shininess; 14 | }; 15 | uniform MaterialInfo Material; 16 | 17 | struct LightInfo { 18 | vec3 Intensity; 19 | vec4 Position; 20 | }; 21 | uniform LightInfo Light; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | vec3 phongModel( vec3 pos, vec3 norm ) { 26 | vec3 s = normalize(vec3(Light.Position) - pos); 27 | vec3 v = normalize(-pos.xyz); 28 | vec3 r = reflect( -s, norm ); 29 | vec3 ambient = Light.Intensity * Material.Ka; 30 | float sDotN = max( dot(s,norm), 0.0 ); 31 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 32 | vec3 spec = vec3(0.0); 33 | if( sDotN > 0.0 ) 34 | spec = Light.Intensity * Material.Ks * 35 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 36 | 37 | return ambient + diffuse + spec; 38 | } 39 | 40 | void main() { 41 | vec3 color = phongModel(vec3(EyePosition), normalize(EyeNormal)); 42 | 43 | vec4 projTexColor = vec4(0.0); 44 | if( ProjTexCoord.z > 0.0 ) 45 | projTexColor = textureProj( ProjectorTex, ProjTexCoord ); 46 | 47 | FragColor = vec4(color,1.0) + projTexColor * 0.5; 48 | } 49 | -------------------------------------------------------------------------------- /chapter04/shader/projtex.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 EyeNormal; // Normal in eye coordinates 7 | out vec4 EyePosition; // Position in eye coordinates 8 | out vec4 ProjTexCoord; 9 | 10 | uniform mat4 ProjectorMatrix; 11 | 12 | uniform vec3 WorldCameraPosition; 13 | uniform mat4 ModelViewMatrix; 14 | uniform mat4 ModelMatrix; 15 | uniform mat3 NormalMatrix; 16 | uniform mat4 ProjectionMatrix; 17 | uniform mat4 MVP; 18 | 19 | void main() 20 | { 21 | vec4 pos4 = vec4(VertexPosition,1.0); 22 | 23 | EyeNormal = normalize(NormalMatrix * VertexNormal); 24 | EyePosition = ModelViewMatrix * pos4; 25 | ProjTexCoord = ProjectorMatrix * (ModelMatrix * pos4); 26 | gl_Position = MVP * pos4; 27 | } 28 | -------------------------------------------------------------------------------- /chapter04/shader/projtex_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 EyeNormal; // Normal in eye coordinates 4 | in vec4 EyePosition; // Position in eye coordinates 5 | in vec4 ProjTexCoord; 6 | 7 | uniform sampler2D ProjectorTex; 8 | 9 | struct MaterialInfo { 10 | vec3 Kd; 11 | vec3 Ks; 12 | vec3 Ka; 13 | float Shininess; 14 | }; 15 | uniform MaterialInfo Material; 16 | 17 | struct LightInfo { 18 | vec3 Intensity; 19 | vec4 Position; 20 | }; 21 | uniform LightInfo Light; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | vec3 phongModel( vec3 pos, vec3 norm ) { 26 | vec3 s = normalize(vec3(Light.Position) - pos); 27 | vec3 v = normalize(-pos.xyz); 28 | vec3 r = reflect( -s, norm ); 29 | vec3 ambient = Light.Intensity * Material.Ka; 30 | float sDotN = max( dot(s,norm), 0.0 ); 31 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 32 | vec3 spec = vec3(0.0); 33 | if( sDotN > 0.0 ) 34 | spec = Light.Intensity * Material.Ks * 35 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 36 | 37 | return ambient + diffuse + spec; 38 | } 39 | 40 | void main() { 41 | vec3 color = phongModel(vec3(EyePosition), normalize(EyeNormal)); 42 | 43 | vec4 projTexColor = vec4(0.0); 44 | if( ProjTexCoord.z > 0.0 ) 45 | projTexColor = textureProj( ProjectorTex, ProjTexCoord ); 46 | 47 | FragColor = vec4(color,1.0) + projTexColor * 0.5; 48 | } 49 | -------------------------------------------------------------------------------- /chapter04/shader/projtex_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 EyeNormal; // Normal in eye coordinates 7 | out vec4 EyePosition; // Position in eye coordinates 8 | out vec4 ProjTexCoord; 9 | 10 | uniform mat4 ProjectorMatrix; 11 | 12 | uniform vec3 WorldCameraPosition; 13 | uniform mat4 ModelViewMatrix; 14 | uniform mat4 ModelMatrix; 15 | uniform mat3 NormalMatrix; 16 | uniform mat4 ProjectionMatrix; 17 | uniform mat4 MVP; 18 | 19 | void main() 20 | { 21 | vec4 pos4 = vec4(VertexPosition,1.0); 22 | 23 | EyeNormal = normalize(NormalMatrix * VertexNormal); 24 | EyePosition = ModelViewMatrix * pos4; 25 | ProjTexCoord = ProjectorMatrix * (ModelMatrix * pos4); 26 | gl_Position = MVP * pos4; 27 | } 28 | -------------------------------------------------------------------------------- /chapter04/shader/rendertotex.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform sampler2D RenderTex; 8 | 9 | struct LightInfo { 10 | vec4 Position; // Light position in eye coords. 11 | vec3 Intensity; // A,D,S intensity 12 | }; 13 | uniform LightInfo Light; 14 | 15 | struct MaterialInfo { 16 | vec3 Ka; // Ambient reflectivity 17 | vec3 Kd; // Diffuse reflectivity 18 | vec3 Ks; // Specular reflectivity 19 | float Shininess; // Specular shininess factor 20 | }; 21 | uniform MaterialInfo Material; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | vec3 phongModel( vec3 pos, vec3 norm ) { 26 | vec3 s = normalize(vec3(Light.Position) - pos); 27 | vec3 v = normalize(-pos.xyz); 28 | vec3 r = reflect( -s, norm ); 29 | vec3 ambient = Light.Intensity * Material.Ka; 30 | float sDotN = max( dot(s,norm), 0.0 ); 31 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 32 | vec3 spec = vec3(0.0); 33 | if( sDotN > 0.0 ) 34 | spec = Light.Intensity * Material.Ks * 35 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 36 | 37 | return ambient + diffuse + spec; 38 | } 39 | 40 | void main() { 41 | vec4 texColor = texture( RenderTex, TexCoord ); 42 | FragColor = vec4( phongModel(Position,Normal), 1.0 ) * 43 | texColor; 44 | } 45 | -------------------------------------------------------------------------------- /chapter04/shader/rendertotex.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/shader/texture.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | layout(binding=0) uniform sampler2D Tex1; 8 | 9 | struct LightInfo { 10 | vec4 Position; // Light position in eye coords. 11 | vec3 Intensity; // A,D,S intensity 12 | }; 13 | uniform LightInfo Light; 14 | 15 | struct MaterialInfo { 16 | vec3 Ka; // Ambient reflectivity 17 | vec3 Kd; // Diffuse reflectivity 18 | vec3 Ks; // Specular reflectivity 19 | float Shininess; // Specular shininess factor 20 | }; 21 | uniform MaterialInfo Material; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | void phongModel( vec3 pos, vec3 norm, out vec3 ambAndDiff, out vec3 spec ) { 26 | vec3 s = normalize(vec3(Light.Position) - pos); 27 | vec3 v = normalize(-pos.xyz); 28 | vec3 r = reflect( -s, norm ); 29 | vec3 ambient = Light.Intensity * Material.Ka; 30 | float sDotN = max( dot(s,norm), 0.0 ); 31 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 32 | spec = vec3(0.0); 33 | if( sDotN > 0.0 ) 34 | spec = Light.Intensity * Material.Ks * 35 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 36 | ambAndDiff = ambient + diffuse; 37 | } 38 | 39 | void main() { 40 | vec3 ambAndDiff, spec; 41 | vec4 texColor = texture( Tex1, TexCoord ); 42 | phongModel( Position, Normal, ambAndDiff, spec ); 43 | FragColor = (vec4( ambAndDiff, 1.0 ) * texColor) + vec4(spec,1.0); 44 | } 45 | -------------------------------------------------------------------------------- /chapter04/shader/texture.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/shader/texture_41.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform sampler2D Tex1; 8 | 9 | struct LightInfo { 10 | vec4 Position; // Light position in eye coords. 11 | vec3 Intensity; // A,D,S intensity 12 | }; 13 | uniform LightInfo Light; 14 | 15 | struct MaterialInfo { 16 | vec3 Ka; // Ambient reflectivity 17 | vec3 Kd; // Diffuse reflectivity 18 | vec3 Ks; // Specular reflectivity 19 | float Shininess; // Specular shininess factor 20 | }; 21 | uniform MaterialInfo Material; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | void phongModel( vec3 pos, vec3 norm, out vec3 ambAndDiff, out vec3 spec ) { 26 | vec3 s = normalize(vec3(Light.Position) - pos); 27 | vec3 v = normalize(-pos.xyz); 28 | vec3 r = reflect( -s, norm ); 29 | vec3 ambient = Light.Intensity * Material.Ka; 30 | float sDotN = max( dot(s,norm), 0.0 ); 31 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 32 | spec = vec3(0.0); 33 | if( sDotN > 0.0 ) 34 | spec = Light.Intensity * Material.Ks * 35 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 36 | ambAndDiff = ambient + diffuse; 37 | } 38 | 39 | void main() { 40 | vec3 ambAndDiff, spec; 41 | vec4 texColor = texture( Tex1, TexCoord ); 42 | phongModel( Position, Normal, ambAndDiff, spec ); 43 | FragColor = (vec4( ambAndDiff, 1.0 ) * texColor) + vec4(spec,1.0); 44 | } 45 | -------------------------------------------------------------------------------- /chapter04/shader/texture_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter04/skybox.h: -------------------------------------------------------------------------------- 1 | #ifndef SKYBOX_H 2 | #define SKYBOX_H 3 | 4 | #include "drawable.h" 5 | 6 | class SkyBox : public Drawable 7 | { 8 | private: 9 | unsigned int vaoHandle; 10 | 11 | public: 12 | SkyBox(); 13 | 14 | void render() const; 15 | }; 16 | 17 | 18 | #endif // SKYBOX_H 19 | -------------------------------------------------------------------------------- /chapter05/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target chapter05) 2 | set( chapter05_SOURCES 3 | main.cpp 4 | sceneblur.cpp 5 | scenedeferred.cpp 6 | sceneedge.cpp 7 | scenegamma.cpp 8 | scenemsaa.cpp 9 | scenetonemap.cpp 10 | scenehdrbloom.cpp 11 | sceneoit.cpp) 12 | 13 | add_executable( chapter05 ${chapter05_SOURCES} ) 14 | 15 | target_compile_definitions(${target} 16 | PRIVATE 17 | GLFW_INCLUDE_NONE 18 | ) 19 | 20 | target_link_libraries( ${target} 21 | PRIVATE 22 | ingredients 23 | glfw 24 | ${OPENGL_gl_LIBRARY} 25 | ) 26 | 27 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 28 | -------------------------------------------------------------------------------- /chapter05/sceneblur.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBLUR_H 2 | #define SCENEBLUR_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "cube.h" 8 | #include "torus.h" 9 | #include "teapot.h" 10 | 11 | #include "cookbookogl.h" 12 | 13 | #include 14 | 15 | class SceneBlur : public Scene 16 | { 17 | private: 18 | GLSLProgram prog; 19 | 20 | GLuint fsQuad, pass1Index, pass2Index, pass3Index; 21 | GLuint renderFBO, intermediateFBO; 22 | GLuint renderTex, intermediateTex; 23 | 24 | Plane plane; 25 | Torus torus; 26 | Teapot teapot; 27 | Cube cube; 28 | 29 | float angle, tPrev, rotSpeed; 30 | 31 | void setMatrices(); 32 | void compileAndLinkShader(); 33 | void setupFBO(); 34 | void pass1(); 35 | void pass2(); 36 | void pass3(); 37 | float gauss(float, float); 38 | 39 | public: 40 | SceneBlur(); 41 | 42 | void initScene(); 43 | void update( float t ); 44 | void render(); 45 | void resize(int, int); 46 | }; 47 | 48 | #endif // SCENEBLUR_H 49 | -------------------------------------------------------------------------------- /chapter05/scenedeferred.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEDEFERRED_H 2 | #define SCENEDEFERRED_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "torus.h" 8 | #include "teapot.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneDeferred : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | GLuint deferredFBO; 20 | GLuint quad, pass1Index, pass2Index; 21 | 22 | Plane plane; 23 | Torus torus; 24 | Teapot teapot; 25 | 26 | float angle, tPrev, rotSpeed; 27 | 28 | void setMatrices(); 29 | void compileAndLinkShader(); 30 | void setupFBO(); 31 | void createGBufTex(GLenum, GLenum, GLuint &); 32 | void pass1(); 33 | void pass2(); 34 | 35 | public: 36 | SceneDeferred(); 37 | 38 | void initScene(); 39 | void update( float t ); 40 | void render(); 41 | void resize(int, int); 42 | }; 43 | 44 | #endif // SCENEDEFERRED_H 45 | -------------------------------------------------------------------------------- /chapter05/sceneedge.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEEDGE_H 2 | #define SCENEEDGE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "torus.h" 8 | #include "teapot.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneEdge : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | GLuint fsQuad, pass1Index, pass2Index, fboHandle, renderTex; 20 | 21 | Plane plane; 22 | Torus torus; 23 | Teapot teapot; 24 | 25 | float angle, tPrev, rotSpeed; 26 | 27 | void setMatrices(); 28 | void compileAndLinkShader(); 29 | void setupFBO(); 30 | void pass1(); 31 | void pass2(); 32 | 33 | public: 34 | SceneEdge(); 35 | 36 | void initScene(); 37 | void update( float t ); 38 | void render(); 39 | void resize(int, int); 40 | }; 41 | 42 | #endif // SCENEEDGE_H 43 | -------------------------------------------------------------------------------- /chapter05/scenegamma.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEGAMMA_H 2 | #define SCENEGAMMA_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "torus.h" 8 | #include "objmesh.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneGamma : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | Plane plane; 20 | Torus torus; 21 | std::unique_ptr ogre; 22 | 23 | float angle, tPrev, rotSpeed; 24 | 25 | void setMatrices(); 26 | void compileAndLinkShader(); 27 | void setupFBO(); 28 | 29 | public: 30 | SceneGamma(); 31 | 32 | void initScene(); 33 | void update( float t ); 34 | void render(); 35 | void resize(int, int); 36 | }; 37 | 38 | #endif // SCENEGAMMA_H 39 | -------------------------------------------------------------------------------- /chapter05/scenehdrbloom.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEHDRBLOOM_H 2 | #define SCENEHDRBLOOM_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | #include "sphere.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneHdrBloom : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | GLuint fsQuad, pass1Index, pass2Index, pass3Index, pass4Index, pass5Index; 20 | GLuint hdrFbo, blurFbo; 21 | GLuint hdrTex, tex1, tex2; 22 | GLuint linearSampler, nearestSampler; 23 | 24 | Plane plane; 25 | Teapot teapot; 26 | Sphere sphere; 27 | 28 | float angle, tPrev, rotSpeed; 29 | int bloomBufWidth, bloomBufHeight; 30 | 31 | void setMatrices(); 32 | void compileAndLinkShader(); 33 | void setupFBO(); 34 | void pass1(); 35 | void pass2(); 36 | void pass3(); 37 | void pass4(); 38 | void pass5(); 39 | float gauss(float, float); 40 | void drawScene(); 41 | void computeLogAveLuminance(); 42 | 43 | public: 44 | SceneHdrBloom(); 45 | 46 | void initScene(); 47 | void update( float t ); 48 | void render(); 49 | void resize(int, int); 50 | }; 51 | 52 | #endif // SCENEHDRBLOOM_H 53 | -------------------------------------------------------------------------------- /chapter05/scenemsaa.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEMSAA_H 2 | #define SCENEMSAA_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | 7 | #include "cookbookogl.h" 8 | 9 | #include 10 | 11 | class SceneMsaa : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | GLuint quad; 17 | 18 | float angle, tPrev, rotSpeed; 19 | 20 | void setMatrices(); 21 | void compileAndLinkShader(); 22 | void setupFBO(); 23 | 24 | public: 25 | SceneMsaa(); 26 | 27 | void initScene(); 28 | void update( float t ); 29 | void render(); 30 | void resize(int, int); 31 | }; 32 | 33 | #endif // SCENEMSAA_H 34 | -------------------------------------------------------------------------------- /chapter05/sceneoit.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEOIT_H 2 | #define SCENEOIT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cube.h" 7 | #include "sphere.h" 8 | 9 | #include "cookbookogl.h" 10 | 11 | #include 12 | 13 | enum BufferNames { 14 | COUNTER_BUFFER = 0, 15 | LINKED_LIST_BUFFER 16 | }; 17 | 18 | struct ListNode { 19 | glm::vec4 color; 20 | GLfloat depth; 21 | GLuint next; 22 | }; 23 | 24 | class SceneOit : public Scene 25 | { 26 | private: 27 | GLSLProgram prog; 28 | 29 | GLuint buffers[2], fsQuad, headPtrTex; 30 | GLuint pass1Index, pass2Index; 31 | GLuint clearBuf; 32 | 33 | Cube cube; 34 | Sphere sphere; 35 | 36 | float angle, tPrev, rotSpeed; 37 | 38 | void setMatrices(); 39 | void compileAndLinkShader(); 40 | void drawScene(); 41 | void initShaderStorage(); 42 | void pass1(); 43 | void pass2(); 44 | void clearBuffers(); 45 | 46 | public: 47 | SceneOit(); 48 | 49 | void initScene(); 50 | void update( float t ); 51 | void render(); 52 | void resize(int, int); 53 | 54 | }; 55 | 56 | #endif // SCENEOIT_H 57 | -------------------------------------------------------------------------------- /chapter05/scenetonemap.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENETONEMAP_H 2 | #define SCENETONEMAP_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "teapot.h" 8 | #include "sphere.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneToneMap : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | GLuint hdrFBO; 20 | GLuint quad, pass1Index, pass2Index; 21 | GLuint hdrTex, avgTex; 22 | 23 | Plane plane; 24 | Teapot teapot; 25 | Sphere sphere; 26 | 27 | float angle, tPrev, rotSpeed; 28 | 29 | void setMatrices(); 30 | void compileAndLinkShader(); 31 | void setupFBO(); 32 | void pass1(); 33 | void pass2(); 34 | void computeLogAveLuminance(); 35 | void drawScene(); 36 | 37 | public: 38 | SceneToneMap(); 39 | 40 | void initScene(); 41 | void update( float t ); 42 | void render(); 43 | void resize(int, int); 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /chapter05/shader/blur.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter05/shader/blur_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter05/shader/centroid.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | centroid in vec2 TexCoord; 4 | //in vec2 TexCoord; 5 | 6 | layout( location = 0 ) out vec4 FragColor; 7 | 8 | void main() 9 | { 10 | vec3 yellow = vec3(1.0,1.0,0.0); 11 | vec3 blue = vec3(0.0,0.0,1.0); 12 | vec3 color = blue; 13 | if( TexCoord.x > 1.0 ) color = yellow; 14 | FragColor = vec4( color , 1.0 ); 15 | } 16 | -------------------------------------------------------------------------------- /chapter05/shader/centroid.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | layout (location = 2) in vec2 VertexTexCoord; 6 | 7 | centroid out vec2 TexCoord; 8 | //out vec2 TexCoord; 9 | 10 | uniform mat4 MVP; 11 | 12 | void main() 13 | { 14 | TexCoord = VertexTexCoord; 15 | gl_Position = MVP * vec4(VertexPosition,1.0); 16 | } 17 | -------------------------------------------------------------------------------- /chapter05/shader/deferred.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | Normal = normalize( NormalMatrix * VertexNormal); 19 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 20 | TexCoord = VertexTexCoord; 21 | gl_Position = MVP * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter05/shader/deferred_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | Normal = normalize( NormalMatrix * VertexNormal); 19 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 20 | TexCoord = VertexTexCoord; 21 | gl_Position = MVP * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter05/shader/edge.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter05/shader/edge_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter05/shader/gamma.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform float Gamma; 8 | 9 | struct LightInfo { 10 | vec4 Position; // Light position in eye coords. 11 | vec3 Intensity; // A,D,S intensity 12 | }; 13 | uniform LightInfo Light; 14 | 15 | struct MaterialInfo { 16 | vec3 Ka; // Ambient reflectivity 17 | vec3 Kd; // Diffuse reflectivity 18 | vec3 Ks; // Specular reflectivity 19 | float Shininess; // Specular shininess factor 20 | }; 21 | uniform MaterialInfo Material; 22 | 23 | layout( location = 0 ) out vec4 FragColor; 24 | 25 | vec3 phongModel( vec3 pos, vec3 norm ) 26 | { 27 | vec3 s = normalize(vec3(Light.Position) - pos); 28 | vec3 v = normalize(-pos.xyz); 29 | vec3 r = reflect( -s, norm ); 30 | vec3 ambient = Light.Intensity * Material.Ka; 31 | float sDotN = max( dot(s,norm), 0.0 ); 32 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 33 | vec3 spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | 38 | return ambient + diffuse + spec; 39 | } 40 | 41 | void main() 42 | { 43 | vec3 color = phongModel(Position,Normal); 44 | FragColor = vec4( pow( color, vec3(1.0/Gamma) ), 1.0 ); 45 | } 46 | -------------------------------------------------------------------------------- /chapter05/shader/gamma.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter05/shader/hdrbloom.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter05/shader/hdrbloom_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter05/shader/oit.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | Normal = normalize( NormalMatrix * VertexNormal); 17 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 18 | 19 | gl_Position = MVP * vec4(VertexPosition,1.0); 20 | } 21 | -------------------------------------------------------------------------------- /chapter05/shader/tonemap.vs: -------------------------------------------------------------------------------- 1 | #version 430 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | Normal = normalize( NormalMatrix * VertexNormal); 19 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 20 | TexCoord = VertexTexCoord; 21 | gl_Position = MVP * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter05/shader/tonemap_41.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | Normal = normalize( NormalMatrix * VertexNormal); 19 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 20 | TexCoord = VertexTexCoord; 21 | gl_Position = MVP * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter06/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target chapter06) 2 | set( chapter06_SOURCES 3 | scenebezcurve.cpp 4 | main.cpp 5 | scenepointsprite.cpp 6 | scenequadtess.cpp 7 | sceneshadewire.cpp 8 | scenesilhouette.cpp 9 | scenetessteapot.cpp 10 | scenetessteapotdepth.cpp ) 11 | 12 | add_executable( ${target} ${chapter06_SOURCES} ) 13 | 14 | target_compile_definitions(${target} 15 | PRIVATE 16 | GLFW_INCLUDE_NONE 17 | ) 18 | 19 | target_link_libraries( ${target} 20 | PRIVATE 21 | ingredients 22 | glfw 23 | ${OPENGL_gl_LIBRARY} 24 | ) 25 | 26 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 27 | -------------------------------------------------------------------------------- /chapter06/scenebezcurve.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEBEZCURVE_H 2 | #define SCENEBEZCURVE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | 7 | #include "cookbookogl.h" 8 | 9 | #include 10 | 11 | class SceneBezCurve : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | GLSLProgram solidProg; 16 | 17 | GLuint vaoHandle; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneBezCurve(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEBEZCURVE_H 32 | -------------------------------------------------------------------------------- /chapter06/scenepointsprite.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPOINTSPRITE_H 2 | #define SCENEPOINTSPRITE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | 7 | #include "cookbookogl.h" 8 | 9 | class ScenePointSprite : public Scene 10 | { 11 | private: 12 | GLSLProgram prog; 13 | 14 | GLuint sprites; 15 | int numSprites; 16 | float *locations; 17 | 18 | void setMatrices(); 19 | void compileAndLinkShader(); 20 | void setupFBO(); 21 | 22 | public: 23 | ScenePointSprite(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEPOINTSPRITE_H 32 | -------------------------------------------------------------------------------- /chapter06/scenequadtess.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEQUADTESS_H 2 | #define SCENEQUADTESS_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | 7 | #include "cookbookogl.h" 8 | 9 | #include 10 | 11 | class SceneQuadTess : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | GLuint vaoHandle; 17 | glm::mat4 viewport; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | 22 | public: 23 | SceneQuadTess(); 24 | 25 | void initScene(); 26 | void update( float t ); 27 | void render(); 28 | void resize(int, int); 29 | }; 30 | 31 | #endif // SCENEQUADTESS_H 32 | -------------------------------------------------------------------------------- /chapter06/sceneshadewire.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESHADEWIRE_H 2 | #define SCENESHADEWIRE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "objmesh.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneShadeWire : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | std::unique_ptr ogre; 18 | 19 | glm::mat4 viewport; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | void setupFBO(); 24 | 25 | public: 26 | SceneShadeWire(); 27 | 28 | void initScene(); 29 | void update( float t ); 30 | void render(); 31 | void resize(int, int); 32 | }; 33 | 34 | #endif // SCENESHADEWIRE_H 35 | -------------------------------------------------------------------------------- /chapter06/scenesilhouette.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESILHOUETTE_H 2 | #define SCENESILHOUETTE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "objmesh.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneSilhouette : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | std::unique_ptr ogre; 18 | float angle, tPrev, rotSpeed; 19 | 20 | void setMatrices(); 21 | void compileAndLinkShader(); 22 | void setupFBO(); 23 | 24 | public: 25 | SceneSilhouette(); 26 | 27 | void initScene(); 28 | void update( float t ); 29 | void render(); 30 | void resize(int, int); 31 | }; 32 | 33 | #endif // SCENESILHOUETTE_H 34 | -------------------------------------------------------------------------------- /chapter06/scenetessteapot.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENETESSTEAPOT_H 2 | #define SCENETESSTEAPOT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapotpatch.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneTessTeapot : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | GLuint vaoHandle; 18 | 19 | TeapotPatch teapot; 20 | glm::mat4 viewport; 21 | float angle, tPrev, rotSpeed; 22 | 23 | void setMatrices(); 24 | void compileAndLinkShader(); 25 | 26 | public: 27 | SceneTessTeapot(); 28 | 29 | void initScene(); 30 | void update( float t ); 31 | void render(); 32 | void resize(int, int); 33 | }; 34 | 35 | #endif // SCENETESSTEAPOT_H 36 | -------------------------------------------------------------------------------- /chapter06/scenetessteapotdepth.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENETESSTEAPOTDEPTH_H 2 | #define SCENETESSTEAPOTDEPTH_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapotpatch.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneTessTeapotDepth : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | GLuint vaoHandle; 18 | 19 | TeapotPatch teapot; 20 | 21 | glm::mat4 viewport; 22 | 23 | void setMatrices(); 24 | void compileAndLinkShader(); 25 | 26 | public: 27 | SceneTessTeapotDepth(); 28 | 29 | void initScene(); 30 | void update( float t ); 31 | void render(); 32 | void resize(int, int); 33 | }; 34 | 35 | #endif // SCENETESSTEAPOTDEPTH_H 36 | -------------------------------------------------------------------------------- /chapter06/shader/bezcurve.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform vec4 LineColor; 4 | 5 | layout ( location = 0 ) out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = LineColor; 10 | } 11 | -------------------------------------------------------------------------------- /chapter06/shader/bezcurve.tcs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( vertices=4 ) out; 4 | 5 | uniform int NumSegments; 6 | uniform int NumStrips; 7 | 8 | void main() 9 | { 10 | // Pass along the vertex position unmodified 11 | gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; 12 | 13 | // Some drivers (e.g. Intel) treat these levels incorrectly. The OpenGL spec 14 | // says that level 0 should be the number of strips and level 1 should be 15 | // the number of segments per strip. Unfortunately, not all drivers do this. 16 | // If this example doesn't work for you, try switching the right 17 | // hand side of the two assignments below. 18 | gl_TessLevelOuter[0] = float(NumStrips); 19 | gl_TessLevelOuter[1] = float(NumSegments); 20 | } 21 | -------------------------------------------------------------------------------- /chapter06/shader/bezcurve.tes: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( isolines ) in; 4 | 5 | uniform mat4 MVP; 6 | 7 | void main() 8 | { 9 | float u = gl_TessCoord.x; 10 | 11 | vec3 p0 = gl_in[0].gl_Position.xyz; 12 | vec3 p1 = gl_in[1].gl_Position.xyz; 13 | vec3 p2 = gl_in[2].gl_Position.xyz; 14 | vec3 p3 = gl_in[3].gl_Position.xyz; 15 | 16 | float u1 = (1.0 - u); 17 | float u2 = u * u; 18 | 19 | // Bernstein polynomials 20 | float b3 = u2 * u; 21 | float b2 = 3.0 * u2 * u1; 22 | float b1 = 3.0 * u * u1 * u1; 23 | float b0 = u1 * u1 * u1; 24 | 25 | // Cubic Bezier interpolation 26 | vec3 p = p0 * b0 + p1 * b1 + p2 * b2 + p3 * b3; 27 | 28 | gl_Position = MVP * vec4(p, 1.0); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter06/shader/bezcurve.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec2 VertexPosition; 4 | 5 | void main() 6 | { 7 | gl_Position = vec4(VertexPosition, 0.0, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /chapter06/shader/pointsprite.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | in vec2 TexCoord; 4 | 5 | uniform sampler2D SpriteTex; 6 | 7 | layout( location = 0 ) out vec4 FragColor; 8 | 9 | void main() 10 | { 11 | FragColor = texture(SpriteTex, TexCoord); 12 | } 13 | -------------------------------------------------------------------------------- /chapter06/shader/pointsprite.gs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( points ) in; 4 | layout( triangle_strip, max_vertices = 4 ) out; 5 | 6 | uniform float Size2; // Half the width of the quad 7 | 8 | uniform mat4 ProjectionMatrix; 9 | 10 | out vec2 TexCoord; 11 | 12 | void main() 13 | { 14 | mat4 m = ProjectionMatrix; 15 | 16 | gl_Position = m * (vec4(-Size2,-Size2,0.0,0.0) + gl_in[0].gl_Position); 17 | TexCoord = vec2(0.0,0.0); 18 | EmitVertex(); 19 | 20 | gl_Position = m * (vec4(Size2,-Size2,0.0,0.0) + gl_in[0].gl_Position); 21 | TexCoord = vec2(1.0,0.0); 22 | EmitVertex(); 23 | 24 | gl_Position = m * (vec4(-Size2,Size2,0.0,0.0) + gl_in[0].gl_Position); 25 | TexCoord = vec2(0.0,1.0); 26 | EmitVertex(); 27 | 28 | gl_Position = m * (vec4(Size2,Size2,0.0,0.0) + gl_in[0].gl_Position); 29 | TexCoord = vec2(1.0,1.0); 30 | EmitVertex(); 31 | 32 | EndPrimitive(); 33 | } 34 | -------------------------------------------------------------------------------- /chapter06/shader/pointsprite.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | 5 | uniform mat4 ModelViewMatrix; 6 | uniform mat3 NormalMatrix; 7 | uniform mat4 ProjectionMatrix; 8 | 9 | void main() 10 | { 11 | gl_Position = ModelViewMatrix * vec4(VertexPosition,1.0); 12 | } 13 | -------------------------------------------------------------------------------- /chapter06/shader/quadtess.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform float LineWidth; 4 | uniform vec4 LineColor; 5 | uniform vec4 QuadColor; 6 | 7 | noperspective in vec3 EdgeDistance; 8 | 9 | layout ( location = 0 ) out vec4 FragColor; 10 | 11 | float edgeMix() 12 | { 13 | // Find the smallest distance 14 | float d = min( min( EdgeDistance.x, EdgeDistance.y ), EdgeDistance.z ); 15 | 16 | if( d < LineWidth - 1 ) { 17 | return 1.0; 18 | } else if( d > LineWidth + 1 ) { 19 | return 0.0; 20 | } else { 21 | float x = d - (LineWidth - 1); 22 | return exp2(-2.0 * (x*x)); 23 | } 24 | } 25 | 26 | void main() 27 | { 28 | float mixVal = edgeMix(); 29 | 30 | FragColor = mix( QuadColor, LineColor, mixVal ); 31 | } 32 | -------------------------------------------------------------------------------- /chapter06/shader/quadtess.gs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( triangles ) in; 4 | layout( triangle_strip, max_vertices = 3 ) out; 5 | 6 | noperspective out vec3 EdgeDistance; 7 | 8 | uniform mat4 ViewportMatrix; // Viewport matrix 9 | 10 | void main() 11 | { 12 | // Transform each vertex into viewport space 13 | vec3 p0 = vec3(ViewportMatrix * (gl_in[0].gl_Position / gl_in[0].gl_Position.w)); 14 | vec3 p1 = vec3(ViewportMatrix * (gl_in[1].gl_Position / gl_in[1].gl_Position.w)); 15 | vec3 p2 = vec3(ViewportMatrix * (gl_in[2].gl_Position / gl_in[2].gl_Position.w)); 16 | 17 | float a = length(p1 - p2); 18 | float b = length(p2 - p0); 19 | float c = length(p1 - p0); 20 | float alpha = acos( (b*b + c*c - a*a) / (2.0*b*c) ); 21 | float beta = acos( (a*a + c*c - b*b) / (2.0*a*c) ); 22 | float ha = abs( c * sin( beta ) ); 23 | float hb = abs( c * sin( alpha ) ); 24 | float hc = abs( b * sin( alpha ) ); 25 | 26 | EdgeDistance = vec3( ha, 0, 0 ); 27 | gl_Position = gl_in[0].gl_Position; 28 | EmitVertex(); 29 | 30 | EdgeDistance = vec3( 0, hb, 0 ); 31 | gl_Position = gl_in[1].gl_Position; 32 | EmitVertex(); 33 | 34 | EdgeDistance = vec3( 0, 0, hc ); 35 | gl_Position = gl_in[2].gl_Position; 36 | EmitVertex(); 37 | 38 | EndPrimitive(); 39 | } 40 | -------------------------------------------------------------------------------- /chapter06/shader/quadtess.tcs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( vertices=4 ) out; 4 | 5 | uniform int Outer; 6 | uniform int Inner; 7 | 8 | void main() 9 | { 10 | // Pass along the vertex position unmodified 11 | gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; 12 | 13 | gl_TessLevelOuter[0] = float(Outer); 14 | gl_TessLevelOuter[1] = float(Outer); 15 | gl_TessLevelOuter[2] = float(Outer); 16 | gl_TessLevelOuter[3] = float(Outer); 17 | 18 | gl_TessLevelInner[0] = float(Inner); 19 | gl_TessLevelInner[1] = float(Inner); 20 | } 21 | -------------------------------------------------------------------------------- /chapter06/shader/quadtess.tes: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( quads, equal_spacing, ccw ) in; 4 | 5 | uniform mat4 MVP; 6 | 7 | void main() 8 | { 9 | float u = gl_TessCoord.x; 10 | float v = gl_TessCoord.y; 11 | 12 | vec4 p00 = gl_in[0].gl_Position; 13 | vec4 p10 = gl_in[1].gl_Position; 14 | vec4 p11 = gl_in[2].gl_Position; 15 | vec4 p01 = gl_in[3].gl_Position; 16 | 17 | // Linear interpolation 18 | gl_Position = 19 | p00 * (1.0-u) * (1.0-v) + 20 | p10 * u * (1.0-v) + 21 | p01 * v * (1.0-u) + 22 | p11 * u * v; 23 | 24 | // Transform to clip coordinates 25 | gl_Position = MVP * gl_Position; 26 | } 27 | -------------------------------------------------------------------------------- /chapter06/shader/quadtess.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec2 VertexPosition; 4 | 5 | void main() 6 | { 7 | gl_Position = vec4(VertexPosition, 0.0, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /chapter06/shader/shadewire.gs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( triangles ) in; 4 | layout( triangle_strip, max_vertices = 3 ) out; 5 | 6 | out vec3 GNormal; 7 | out vec3 GPosition; 8 | noperspective out vec3 GEdgeDistance; 9 | 10 | in vec3 VNormal[]; 11 | in vec3 VPosition[]; 12 | 13 | uniform mat4 ViewportMatrix; // Viewport matrix 14 | 15 | void main() 16 | { 17 | // Transform each vertex into viewport space 18 | vec2 p0 = vec2(ViewportMatrix * (gl_in[0].gl_Position / gl_in[0].gl_Position.w)); 19 | vec2 p1 = vec2(ViewportMatrix * (gl_in[1].gl_Position / gl_in[1].gl_Position.w)); 20 | vec2 p2 = vec2(ViewportMatrix * (gl_in[2].gl_Position / gl_in[2].gl_Position.w)); 21 | 22 | float a = length(p1 - p2); 23 | float b = length(p2 - p0); 24 | float c = length(p1 - p0); 25 | float alpha = acos( (b*b + c*c - a*a) / (2.0*b*c) ); 26 | float beta = acos( (a*a + c*c - b*b) / (2.0*a*c) ); 27 | float ha = abs( c * sin( beta ) ); 28 | float hb = abs( c * sin( alpha ) ); 29 | float hc = abs( b * sin( alpha ) ); 30 | 31 | GEdgeDistance = vec3( ha, 0, 0 ); 32 | GNormal = VNormal[0]; 33 | GPosition = VPosition[0]; 34 | gl_Position = gl_in[0].gl_Position; 35 | EmitVertex(); 36 | 37 | GEdgeDistance = vec3( 0, hb, 0 ); 38 | GNormal = VNormal[1]; 39 | GPosition = VPosition[1]; 40 | gl_Position = gl_in[1].gl_Position; 41 | EmitVertex(); 42 | 43 | GEdgeDistance = vec3( 0, 0, hc ); 44 | GNormal = VNormal[2]; 45 | GPosition = VPosition[2]; 46 | gl_Position = gl_in[2].gl_Position; 47 | EmitVertex(); 48 | 49 | EndPrimitive(); 50 | } 51 | -------------------------------------------------------------------------------- /chapter06/shader/shadewire.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | layout (location = 1 ) in vec3 VertexNormal; 5 | 6 | out vec3 VNormal; 7 | out vec3 VPosition; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | VNormal = normalize( NormalMatrix * VertexNormal); 17 | VPosition = vec3(ModelViewMatrix * vec4(VertexPosition,1.0)); 18 | gl_Position = MVP * vec4(VertexPosition,1.0); 19 | } 20 | -------------------------------------------------------------------------------- /chapter06/shader/silhouette.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | struct LightInfo { 4 | vec4 Position; // Light position in eye coords. 5 | vec3 Intensity; // A,D,S intensity 6 | }; 7 | uniform LightInfo Light; 8 | 9 | struct MaterialInfo { 10 | vec3 Ka; // Ambient reflectivity 11 | vec3 Kd; // Diffuse reflectivity 12 | vec3 Ks; // Specular reflectivity 13 | float Shininess; // Specular shininess factor 14 | }; 15 | uniform MaterialInfo Material; 16 | 17 | uniform vec4 LineColor; 18 | 19 | in vec3 GPosition; 20 | in vec3 GNormal; 21 | 22 | flat in int GIsEdge; 23 | 24 | layout( location = 0 ) out vec4 FragColor; 25 | 26 | const int levels = 3; 27 | const float scaleFactor = 1.0 / levels; 28 | 29 | vec3 toonShade( ) 30 | { 31 | vec3 s = normalize( Light.Position.xyz - GPosition.xyz ); 32 | vec3 ambient = Material.Ka; 33 | float cosine = dot( s, GNormal ); 34 | vec3 diffuse = Material.Kd * ceil( cosine * levels ) * scaleFactor; 35 | 36 | return Light.Intensity * (ambient + diffuse); 37 | } 38 | 39 | void main() { 40 | if( GIsEdge == 1 ) { 41 | FragColor = LineColor; 42 | } else { 43 | FragColor = vec4( toonShade(), 1.0 ); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /chapter06/shader/silhouette.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | layout (location = 1 ) in vec3 VertexNormal; 5 | 6 | out vec3 VNormal; 7 | out vec3 VPosition; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjectionMatrix; 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | VNormal = normalize( NormalMatrix * VertexNormal); 17 | VPosition = vec3(ModelViewMatrix * vec4(VertexPosition,1.0)); 18 | gl_Position = MVP * vec4(VertexPosition,1.0); 19 | } 20 | -------------------------------------------------------------------------------- /chapter06/shader/solid.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform vec4 Color; 4 | 5 | layout ( location = 0 ) out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } 11 | -------------------------------------------------------------------------------- /chapter06/shader/solid.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec2 VertexPosition; 4 | 5 | uniform mat4 MVP; 6 | 7 | void main() 8 | { 9 | gl_Position = MVP * vec4(VertexPosition, 0.0, 1.0); 10 | } 11 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapot.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform float LineWidth; 4 | uniform vec4 LineColor; 5 | uniform vec4 LightPosition; 6 | uniform vec3 LightIntensity; 7 | uniform vec3 Kd; 8 | 9 | noperspective in vec3 EdgeDistance; 10 | in vec3 Normal; 11 | in vec4 Position; 12 | 13 | layout ( location = 0 ) out vec4 FragColor; 14 | 15 | vec3 diffuseModel( vec3 pos, vec3 norm ) 16 | { 17 | vec3 s = normalize(vec3(LightPosition) - pos); 18 | float sDotN = max( dot(s,norm), 0.0 ); 19 | vec3 diffuse = LightIntensity * Kd * sDotN; 20 | 21 | return diffuse; 22 | } 23 | 24 | float edgeMix() 25 | { 26 | // Find the smallest distance 27 | float d = min( min( EdgeDistance.x, EdgeDistance.y ), EdgeDistance.z ); 28 | 29 | if( d < LineWidth - 1 ) { 30 | return 1.0; 31 | } else if( d > LineWidth + 1 ) { 32 | return 0.0; 33 | } else { 34 | float x = d - (LineWidth - 1); 35 | return exp2(-2.0 * (x*x)); 36 | } 37 | } 38 | 39 | void main() 40 | { 41 | float mixVal = edgeMix(); 42 | vec4 color = vec4( diffuseModel( Position.xyz, Normal ), 1.0); 43 | color = pow( color, vec4(1.0/2.2) ); 44 | FragColor = mix( color, LineColor, mixVal ); 45 | } 46 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapot.gs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( triangles ) in; 4 | layout( triangle_strip, max_vertices = 3 ) out; 5 | 6 | in vec3 TENormal[]; 7 | in vec4 TEPosition[]; 8 | noperspective out vec3 EdgeDistance; 9 | 10 | out vec3 Normal; 11 | out vec4 Position; 12 | 13 | uniform mat4 ViewportMatrix; // Viewport matrix 14 | 15 | void main() 16 | { 17 | // Transform each vertex into viewport space 18 | vec3 p0 = vec3(ViewportMatrix * (gl_in[0].gl_Position / gl_in[0].gl_Position.w)); 19 | vec3 p1 = vec3(ViewportMatrix * (gl_in[1].gl_Position / gl_in[1].gl_Position.w)); 20 | vec3 p2 = vec3(ViewportMatrix * (gl_in[2].gl_Position / gl_in[2].gl_Position.w)); 21 | 22 | float a = length(p1 - p2); 23 | float b = length(p2 - p0); 24 | float c = length(p1 - p0); 25 | float alpha = acos( (b*b + c*c - a*a) / (2.0*b*c) ); 26 | float beta = acos( (a*a + c*c - b*b) / (2.0*a*c) ); 27 | float ha = abs( c * sin( beta ) ); 28 | float hb = abs( c * sin( alpha ) ); 29 | float hc = abs( b * sin( alpha ) ); 30 | 31 | EdgeDistance = vec3( ha, 0, 0 ); 32 | Normal = TENormal[0]; 33 | Position = TEPosition[0]; 34 | gl_Position = gl_in[0].gl_Position; 35 | EmitVertex(); 36 | 37 | EdgeDistance = vec3( 0, hb, 0 ); 38 | Normal = TENormal[1]; 39 | Position = TEPosition[1]; 40 | gl_Position = gl_in[1].gl_Position; 41 | EmitVertex(); 42 | 43 | EdgeDistance = vec3( 0, 0, hc ); 44 | Normal = TENormal[2]; 45 | Position = TEPosition[2]; 46 | gl_Position = gl_in[2].gl_Position; 47 | EmitVertex(); 48 | 49 | EndPrimitive(); 50 | } 51 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapot.tcs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( vertices=16 ) out; 4 | 5 | uniform int TessLevel; 6 | 7 | void main() 8 | { 9 | // Pass along the vertex position unmodified 10 | gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; 11 | 12 | gl_TessLevelOuter[0] = float(TessLevel); 13 | gl_TessLevelOuter[1] = float(TessLevel); 14 | gl_TessLevelOuter[2] = float(TessLevel); 15 | gl_TessLevelOuter[3] = float(TessLevel); 16 | 17 | gl_TessLevelInner[1] = float(TessLevel); 18 | gl_TessLevelInner[0] = float(TessLevel); 19 | } 20 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapot.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | 5 | void main() 6 | { 7 | gl_Position = vec4(VertexPosition, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapotdepth.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform float LineWidth; 4 | uniform vec4 LineColor; 5 | uniform vec4 LightPosition; 6 | uniform vec3 LightIntensity; 7 | uniform vec3 Kd; 8 | 9 | noperspective in vec3 EdgeDistance; 10 | in vec3 Normal; 11 | in vec4 Position; 12 | 13 | layout ( location = 0 ) out vec4 FragColor; 14 | 15 | vec3 diffuseModel( vec3 pos, vec3 norm ) 16 | { 17 | vec3 s = normalize(vec3(LightPosition) - pos); 18 | float sDotN = max( dot(s,norm), 0.0 ); 19 | vec3 diffuse = LightIntensity * Kd * sDotN; 20 | 21 | return diffuse; 22 | } 23 | 24 | float edgeMix() 25 | { 26 | // Find the smallest distance 27 | float d = min( min( EdgeDistance.x, EdgeDistance.y ), EdgeDistance.z ); 28 | 29 | if( d < LineWidth - 1 ) { 30 | return 1.0; 31 | } else if( d > LineWidth + 1 ) { 32 | return 0.0; 33 | } else { 34 | float x = d - (LineWidth - 1); 35 | return exp2(-2.0 * (x*x)); 36 | } 37 | } 38 | 39 | void main() 40 | { 41 | float mixVal = edgeMix(); 42 | vec4 color = vec4( diffuseModel( Position.xyz, Normal ), 1.0); 43 | color = pow( color, vec4( 1.0/2.2 ) ); 44 | FragColor = mix( color, LineColor, mixVal ); 45 | } 46 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapotdepth.gs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( triangles ) in; 4 | layout( triangle_strip, max_vertices = 3 ) out; 5 | 6 | in vec3 TENormal[]; 7 | in vec4 TEPosition[]; 8 | noperspective out vec3 EdgeDistance; 9 | 10 | out vec3 Normal; 11 | out vec4 Position; 12 | 13 | uniform mat4 ViewportMatrix; // Viewport matrix 14 | 15 | void main() 16 | { 17 | // Transform each vertex into viewport space 18 | vec3 p0 = vec3(ViewportMatrix * (gl_in[0].gl_Position / gl_in[0].gl_Position.w)); 19 | vec3 p1 = vec3(ViewportMatrix * (gl_in[1].gl_Position / gl_in[1].gl_Position.w)); 20 | vec3 p2 = vec3(ViewportMatrix * (gl_in[2].gl_Position / gl_in[2].gl_Position.w)); 21 | 22 | float a = length(p1 - p2); 23 | float b = length(p2 - p0); 24 | float c = length(p1 - p0); 25 | float alpha = acos( (b*b + c*c - a*a) / (2.0*b*c) ); 26 | float beta = acos( (a*a + c*c - b*b) / (2.0*a*c) ); 27 | float ha = abs( c * sin( beta ) ); 28 | float hb = abs( c * sin( alpha ) ); 29 | float hc = abs( b * sin( alpha ) ); 30 | 31 | EdgeDistance = vec3( ha, 0, 0 ); 32 | Normal = TENormal[0]; 33 | Position = TEPosition[0]; 34 | gl_Position = gl_in[0].gl_Position; 35 | EmitVertex(); 36 | 37 | EdgeDistance = vec3( 0, hb, 0 ); 38 | Normal = TENormal[1]; 39 | Position = TEPosition[1]; 40 | gl_Position = gl_in[1].gl_Position; 41 | EmitVertex(); 42 | 43 | EdgeDistance = vec3( 0, 0, hc ); 44 | Normal = TENormal[2]; 45 | Position = TEPosition[2]; 46 | gl_Position = gl_in[2].gl_Position; 47 | EmitVertex(); 48 | 49 | EndPrimitive(); 50 | } 51 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapotdepth.tcs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout( vertices=16 ) out; 4 | 5 | uniform int MinTessLevel; 6 | uniform int MaxTessLevel; 7 | uniform float MaxDepth; 8 | uniform float MinDepth; 9 | uniform mat4 ModelViewMatrix; 10 | 11 | void main() 12 | { 13 | // Position in camera coordinates 14 | vec4 p = ModelViewMatrix * gl_in[gl_InvocationID].gl_Position; 15 | 16 | // Distance from camera scaled between 0 and 1 17 | float depth = clamp( (abs(p.z) - MinDepth) / (MaxDepth - MinDepth), 0.0, 1.0 ); 18 | 19 | float tessLevel = mix(MaxTessLevel, MinTessLevel, depth); 20 | 21 | gl_TessLevelOuter[0] = tessLevel; 22 | gl_TessLevelOuter[1] = tessLevel; 23 | gl_TessLevelOuter[2] = tessLevel; 24 | gl_TessLevelOuter[3] = tessLevel; 25 | 26 | gl_TessLevelInner[0] = tessLevel; 27 | gl_TessLevelInner[1] = tessLevel; 28 | 29 | gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; 30 | } 31 | -------------------------------------------------------------------------------- /chapter06/shader/tessteapotdepth.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | 5 | void main() 6 | { 7 | gl_Position = vec4(VertexPosition, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /chapter07/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set( target chapter07 ) 2 | set( chapter07_SOURCES 3 | frustum.cpp 4 | main.cpp 5 | sceneao.cpp 6 | scenejitter.cpp 7 | scenepcf.cpp 8 | sceneshadowmap.cpp 9 | sceneshadowvolume.cpp ) 10 | 11 | add_executable( ${target} ${chapter07_SOURCES} ) 12 | 13 | target_compile_definitions(${target} 14 | PRIVATE 15 | GLFW_INCLUDE_NONE 16 | ) 17 | 18 | target_link_libraries( ${target} 19 | PRIVATE 20 | ingredients 21 | glfw 22 | ${OPENGL_gl_LIBRARY} 23 | ) 24 | 25 | 26 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 27 | -------------------------------------------------------------------------------- /chapter07/frustum.h: -------------------------------------------------------------------------------- 1 | #ifndef FRUSTUM_H 2 | #define FRUSTUM_H 3 | 4 | #include "cookbookogl.h" 5 | #include "drawable.h" 6 | 7 | #include 8 | #include 9 | 10 | class Frustum : public Drawable 11 | { 12 | private: 13 | GLuint vao; 14 | glm::vec3 center, u, v, n; 15 | float mNear, mFar, fovy, ar; 16 | std::vector buffers; 17 | 18 | public: 19 | Frustum(); 20 | ~Frustum(); 21 | 22 | void orient( const glm::vec3 &pos, const glm::vec3& a, const glm::vec3& u ); 23 | void setPerspective( float , float , float , float ); 24 | 25 | glm::mat4 getViewMatrix() const; 26 | glm::mat4 getInverseViewMatrix() const; 27 | glm::mat4 getProjectionMatrix() const; 28 | glm::vec3 getOrigin() const; 29 | 30 | void render() const override; 31 | void deleteBuffers(); 32 | }; 33 | 34 | #endif // FRUSTUM_H 35 | -------------------------------------------------------------------------------- /chapter07/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scenerunner.h" 2 | #include "sceneao.h" 3 | #include "scenejitter.h" 4 | #include "scenepcf.h" 5 | #include "sceneshadowmap.h" 6 | #include "sceneshadowvolume.h" 7 | 8 | std::map sceneInfo = { 9 | { "ao", "Ambient occlusion from a texture" }, 10 | { "jitter", "Blur shadow map edges using a random jitter" }, 11 | { "pcf", "Blur shadow map edges using percentage-closer-filtering" }, 12 | { "shadow-map", "Simple shadow map" }, 13 | { "shadow-volume", "Shadow Volumes using geometry shader." } 14 | }; 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | std::string recipe = SceneRunner::parseCLArgs(argc, argv, sceneInfo); 19 | 20 | SceneRunner runner("Chapter 7 - " + recipe); 21 | std::unique_ptr scene; 22 | if( recipe == "ao" ) { 23 | scene = std::unique_ptr( new SceneAo() ); 24 | } else if( recipe == "jitter") { 25 | scene = std::unique_ptr( new SceneJitter() ); 26 | } else if( recipe == "pcf") { 27 | scene = std::unique_ptr( new ScenePcf() ); 28 | } else if( recipe == "shadow-map" ) { 29 | scene = std::unique_ptr( new SceneShadowMap() ); 30 | } else if( recipe == "shadow-volume" ) { 31 | scene = std::unique_ptr( new SceneShadowVolume() ); 32 | } 33 | 34 | return runner.run(*scene); 35 | } -------------------------------------------------------------------------------- /chapter07/sceneao.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEAO_H 2 | #define SCENEAO_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "objmesh.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneAo : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | std::unique_ptr ogre; 18 | glm::vec4 lightPos; 19 | float angle; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | void drawScene(); 24 | 25 | public: 26 | SceneAo(); 27 | 28 | void initScene(); 29 | void update( float t ); 30 | void render(); 31 | void resize(int, int); 32 | }; 33 | 34 | #endif // SCENEAO_H 35 | -------------------------------------------------------------------------------- /chapter07/scenejitter.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEJITTER_H 2 | #define SCENEJITTER_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "objmesh.h" 8 | #include "frustum.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneJitter : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | GLuint shadowFBO, pass1Index, pass2Index; 19 | 20 | Plane plane; 21 | std::unique_ptr mesh; 22 | Frustum lightFrustum; 23 | 24 | int samplesU, samplesV; 25 | int jitterMapSize; 26 | float radius; 27 | int shadowMapWidth, shadowMapHeight; 28 | 29 | glm::mat4 lightPV; 30 | glm::mat4 shadowScale; 31 | glm::vec3 lightPos; 32 | float angle; 33 | 34 | void setMatrices(); 35 | void compileAndLinkShader(); 36 | void setupFBO(); 37 | void drawScene(); 38 | float jitter(); 39 | void buildJitterTex(); 40 | void drawBuildingScene(); 41 | 42 | public: 43 | SceneJitter(); 44 | 45 | void initScene(); 46 | void update( float t ); 47 | void render(); 48 | void resize(int, int); 49 | }; 50 | 51 | #endif // SCENEJITTER_H 52 | -------------------------------------------------------------------------------- /chapter07/scenepcf.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPCF_H 2 | #define SCENEPCF_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "objmesh.h" 8 | #include "frustum.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class ScenePcf : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | GLuint shadowFBO, pass1Index, pass2Index; 19 | 20 | Plane plane; 21 | std::unique_ptr mesh; 22 | 23 | Frustum lightFrustum; 24 | 25 | int shadowMapWidth, shadowMapHeight; 26 | float tPrev; 27 | 28 | glm::mat4 lightPV; 29 | glm::mat4 shadowScale; 30 | glm::vec3 lightPos; 31 | float angle; 32 | 33 | void setMatrices(); 34 | void compileAndLinkShader(); 35 | void setupFBO(); 36 | void drawBuildingScene(); 37 | 38 | public: 39 | ScenePcf(); 40 | 41 | void initScene(); 42 | void update( float t ); 43 | void render(); 44 | void resize(int, int); 45 | }; 46 | 47 | #endif // SCENEPCF_H 48 | -------------------------------------------------------------------------------- /chapter07/sceneshadowmap.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESHADOWMAP_H 2 | #define SCENESHADOWMAP_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "torus.h" 8 | #include "teapot.h" 9 | #include "frustum.h" 10 | 11 | #include "cookbookogl.h" 12 | 13 | #include 14 | 15 | class SceneShadowMap : public Scene 16 | { 17 | private: 18 | GLSLProgram prog, solidProg; 19 | GLuint shadowFBO, pass1Index, pass2Index; 20 | 21 | Teapot teapot; 22 | Plane plane; 23 | Torus torus; 24 | 25 | int shadowMapWidth, shadowMapHeight; 26 | float tPrev; 27 | 28 | glm::mat4 lightPV, shadowBias; 29 | float angle; 30 | 31 | Frustum lightFrustum; 32 | 33 | void setMatrices(); 34 | void compileAndLinkShader(); 35 | void setupFBO(); 36 | void drawScene(); 37 | void spitOutDepthBuffer(); 38 | 39 | public: 40 | SceneShadowMap(); 41 | 42 | void initScene(); 43 | void update( float t ); 44 | void render(); 45 | void resize(int, int); 46 | }; 47 | 48 | #endif // SCENESHADOWMAP_H 49 | -------------------------------------------------------------------------------- /chapter07/sceneshadowvolume.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESHADOWVOLUME_H 2 | #define SCENESHADOWVOLUME_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "objmesh.h" 8 | 9 | #include "cookbookogl.h" 10 | 11 | #include 12 | 13 | class SceneShadowVolume : public Scene 14 | { 15 | private: 16 | GLSLProgram volumeProg, renderProg, compProg; 17 | GLuint colorDepthFBO, fsQuad; 18 | GLuint spotTex, brickTex; 19 | 20 | Plane plane; 21 | std::unique_ptr spot; 22 | 23 | glm::vec4 lightPos; 24 | float angle, tPrev, rotSpeed; 25 | 26 | void setMatrices(GLSLProgram &); 27 | void compileAndLinkShader(); 28 | void setupFBO(); 29 | void drawScene(GLSLProgram &, bool); 30 | void pass1(); 31 | void pass2(); 32 | void pass3(); 33 | void updateLight(); 34 | 35 | public: 36 | SceneShadowVolume(); 37 | 38 | void initScene(); 39 | void update( float t ); 40 | void render(); 41 | void resize(int, int); 42 | }; 43 | 44 | #endif // SCENESHADOWVOLUME_H 45 | -------------------------------------------------------------------------------- /chapter07/shader/ao.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform struct LightInfo { 4 | vec4 Position; 5 | vec3 Intensity; 6 | } Light; 7 | 8 | in vec3 Position; 9 | in vec3 Normal; 10 | in vec2 TexCoord; 11 | 12 | layout (location = 0) out vec4 FragColor; 13 | 14 | uniform sampler2D AOTex; 15 | uniform sampler2D DiffTex; 16 | 17 | vec3 phongModelDiffuse() 18 | { 19 | vec3 n = Normal; 20 | vec3 s = normalize(vec3(Light.Position) - Position); 21 | float sDotN = max( dot(s,n), 0.0 ); 22 | vec3 diffColor = texture(DiffTex, TexCoord).rgb; 23 | return Light.Intensity * diffColor * sDotN; 24 | } 25 | 26 | void main() { 27 | vec3 diffuse = phongModelDiffuse(); 28 | 29 | vec4 aoFactor = texture(AOTex, TexCoord); 30 | 31 | FragColor = vec4( diffuse * aoFactor.r, 1.0); 32 | } 33 | -------------------------------------------------------------------------------- /chapter07/shader/ao.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | layout (location = 2) in vec2 TexCoord0; 6 | 7 | out vec3 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 19 | Normal = NormalMatrix * VertexNormal; 20 | TexCoord = TexCoord0; 21 | gl_Position = MVP * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter07/shader/jitter.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location=0) in vec3 VertexPosition; 4 | layout (location=1) in vec3 VertexNormal; 5 | 6 | out vec3 Normal; 7 | out vec3 Position; 8 | out vec4 ShadowCoord; 9 | 10 | uniform mat4 ModelViewMatrix; 11 | uniform mat3 NormalMatrix; 12 | uniform mat4 MVP; 13 | uniform mat4 ShadowMatrix; 14 | 15 | void main() 16 | { 17 | Position = (ModelViewMatrix * vec4(VertexPosition,1.0)).xyz; 18 | Normal = normalize( NormalMatrix * VertexNormal ); 19 | ShadowCoord = ShadowMatrix * vec4(VertexPosition,1.0); 20 | gl_Position = MVP * vec4(VertexPosition,1.0); 21 | } 22 | -------------------------------------------------------------------------------- /chapter07/shader/pcf.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location=0) in vec3 VertexPosition; 4 | layout (location=1) in vec3 VertexNormal; 5 | 6 | out vec3 Normal; 7 | out vec3 Position; 8 | out vec4 ShadowCoord; 9 | 10 | uniform mat4 ModelViewMatrix; 11 | uniform mat3 NormalMatrix; 12 | uniform mat4 MVP; 13 | uniform mat4 ShadowMatrix; 14 | 15 | void main() 16 | { 17 | Position = (ModelViewMatrix * vec4(VertexPosition,1.0)).xyz; 18 | Normal = normalize( NormalMatrix * VertexNormal ); 19 | ShadowCoord = ShadowMatrix * vec4(VertexPosition,1.0); 20 | gl_Position = MVP * vec4(VertexPosition,1.0); 21 | } 22 | -------------------------------------------------------------------------------- /chapter07/shader/shadowmap.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location=0) in vec3 VertexPosition; 4 | layout (location=1) in vec3 VertexNormal; 5 | 6 | out vec3 Normal; 7 | out vec3 Position; 8 | out vec4 ShadowCoord; 9 | 10 | uniform mat4 ModelViewMatrix; 11 | uniform mat3 NormalMatrix; 12 | uniform mat4 MVP; 13 | uniform mat4 ShadowMatrix; 14 | 15 | void main() 16 | { 17 | Position = (ModelViewMatrix * vec4(VertexPosition,1.0)).xyz; 18 | Normal = normalize( NormalMatrix * VertexNormal ); 19 | ShadowCoord = ShadowMatrix * vec4(VertexPosition,1.0); 20 | gl_Position = MVP * vec4(VertexPosition,1.0); 21 | } 22 | -------------------------------------------------------------------------------- /chapter07/shader/shadowvolume-comp.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | 6 | uniform sampler2D DiffSpecTex; 7 | layout( location = 0 ) out vec4 FragColor; 8 | 9 | void main() { 10 | vec4 diffSpec = texelFetch(DiffSpecTex, ivec2(gl_FragCoord), 0); 11 | 12 | FragColor = vec4(diffSpec.xyz, 1); 13 | } 14 | -------------------------------------------------------------------------------- /chapter07/shader/shadowvolume-comp.vs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | 6 | out vec3 Position; 7 | out vec3 Normal; 8 | 9 | uniform mat4 ModelViewMatrix; 10 | uniform mat3 NormalMatrix; 11 | uniform mat4 ProjMatrix; 12 | 13 | void main() 14 | { 15 | Normal = normalize( NormalMatrix * VertexNormal); 16 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 17 | 18 | gl_Position = ProjMatrix * ModelViewMatrix * vec4(VertexPosition,1.0); 19 | } 20 | -------------------------------------------------------------------------------- /chapter07/shader/shadowvolume-render.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform vec4 LightPosition; 8 | uniform vec3 LightIntensity; 9 | 10 | uniform sampler2D Tex; 11 | 12 | uniform vec3 Kd; // Diffuse reflectivity 13 | uniform vec3 Ka; // Ambient reflectivity 14 | uniform vec3 Ks; // Specular reflectivity 15 | uniform float Shininess; // Specular shininess factor 16 | 17 | layout( location = 0 ) out vec4 Ambient; 18 | layout( location = 1 ) out vec4 DiffSpec; 19 | 20 | void shade( ) 21 | { 22 | vec3 s = normalize( vec3(LightPosition) - Position ); 23 | vec3 v = normalize(vec3(-Position)); 24 | vec3 r = reflect( -s, Normal ); 25 | vec4 texColor = texture(Tex, TexCoord); 26 | 27 | Ambient = vec4(texColor.rgb * LightIntensity * Ka, 1.0); 28 | DiffSpec = vec4(texColor.rgb * LightIntensity * 29 | ( Kd * max( dot(s, Normal), 0.0 ) + 30 | Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ) , 31 | 1.0 ); 32 | } 33 | 34 | void main() { 35 | shade(); 36 | } 37 | -------------------------------------------------------------------------------- /chapter07/shader/shadowvolume-render.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjMatrix; 14 | 15 | void main() 16 | { 17 | TexCoord = VertexTexCoord; 18 | Normal = normalize( NormalMatrix * VertexNormal); 19 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 20 | 21 | gl_Position = ProjMatrix * ModelViewMatrix * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter07/shader/shadowvolume-vol.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | void main() { 4 | // Nothing to see here, move along 5 | } 6 | -------------------------------------------------------------------------------- /chapter07/shader/shadowvolume-vol.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 VPosition; 8 | out vec3 VNormal; 9 | 10 | uniform mat4 ModelViewMatrix; 11 | uniform mat3 NormalMatrix; 12 | uniform mat4 ProjMatrix; 13 | 14 | void main() 15 | { 16 | VNormal = NormalMatrix * VertexNormal; 17 | VPosition = (ModelViewMatrix * vec4(VertexPosition,1.0)).xyz; 18 | gl_Position = ProjMatrix * ModelViewMatrix * vec4(VertexPosition,1.0); 19 | } 20 | -------------------------------------------------------------------------------- /chapter07/shader/solid.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform vec4 Color; 4 | 5 | layout ( location = 0 ) out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } 11 | -------------------------------------------------------------------------------- /chapter07/shader/solid.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | 5 | uniform mat4 MVP; 6 | 7 | void main() 8 | { 9 | gl_Position = MVP * vec4(VertexPosition, 1.0); 10 | } 11 | -------------------------------------------------------------------------------- /chapter08/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target chapter08) 2 | set( chapter08_SOURCES 3 | noisetex.cpp 4 | main.cpp 5 | scenesky.cpp 6 | scenedecay.cpp 7 | scenenightvision.cpp 8 | scenepaint.cpp 9 | scenewood.cpp 10 | scenenoise.cpp 11 | ) 12 | 13 | add_executable( ${target} ${chapter08_SOURCES} ) 14 | 15 | target_compile_definitions(${target} 16 | PRIVATE 17 | GLFW_INCLUDE_NONE 18 | ) 19 | 20 | target_link_libraries( ${target} 21 | PRIVATE 22 | ingredients 23 | glfw 24 | ${OPENGL_gl_LIBRARY} 25 | ) 26 | 27 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 28 | -------------------------------------------------------------------------------- /chapter08/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scenerunner.h" 2 | #include "scenedecay.h" 3 | #include "scenenightvision.h" 4 | #include "scenepaint.h" 5 | #include "scenesky.h" 6 | #include "scenewood.h" 7 | #include "scenenoise.h" 8 | 9 | #include 10 | 11 | std::map sceneInfo = { 12 | { "noise", "just display the raw noise texture" }, 13 | { "decay", "decay of a teapot" }, 14 | { "night-vision", "night vision goggles" }, 15 | { "paint", "paint spatters on a teapot" }, 16 | { "sky", "clouds and sky" }, 17 | { "wood", "wood " } 18 | }; 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | std::string recipe = SceneRunner::parseCLArgs(argc, argv, sceneInfo); 23 | 24 | SceneRunner runner("Chapter 8 - " + recipe); 25 | 26 | std::unique_ptr scene; 27 | if( recipe == "decay" ) { 28 | scene = std::unique_ptr( new SceneDecay() ); 29 | } else if( recipe == "noise" ) { 30 | scene = std::unique_ptr( new SceneNoise() ); 31 | } else if( recipe == "night-vision") { 32 | scene = std::unique_ptr( new SceneNightVision() ); 33 | } else if( recipe == "paint") { 34 | scene = std::unique_ptr( new ScenePaint() ); 35 | } else if( recipe == "sky" ) { 36 | scene = std::unique_ptr( new SceneSky() ); 37 | } else if( recipe == "wood" ) { 38 | scene = std::unique_ptr( new SceneWood() ); 39 | } else { 40 | printf("Unknown recipe: %s\n", recipe.c_str()); 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | return runner.run(*scene); 45 | } 46 | -------------------------------------------------------------------------------- /chapter08/noisetex.h: -------------------------------------------------------------------------------- 1 | #ifndef NOISETEX_H 2 | #define NOISETEX_H 3 | 4 | #include "cookbookogl.h" 5 | 6 | namespace NoiseTex 7 | { 8 | int storeTex( GLubyte * data, int w, int h ); 9 | 10 | int generate2DTex(float baseFreq = 4.0f, float persistence = 0.5f, int w = 128, int h = 128, bool periodic = false); 11 | 12 | int generatePeriodic2DTex(float baseFreq = 4.0f, float persist = 0.5f, int w = 128, int h = 128); 13 | }; 14 | 15 | #endif // NOISETEX_H 16 | -------------------------------------------------------------------------------- /chapter08/scenedecay.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEDECAY_H 2 | #define SCENEDECAY_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "teapot.h" 7 | 8 | #include "cookbookogl.h" 9 | 10 | #include 11 | 12 | class SceneDecay : public Scene 13 | { 14 | private: 15 | GLSLProgram prog; 16 | 17 | Teapot teapot; 18 | GLuint quad; 19 | 20 | glm::vec4 lightPos; 21 | float angle; 22 | 23 | void setMatrices(); 24 | void compileAndLinkShader(); 25 | void drawScene(); 26 | 27 | public: 28 | SceneDecay(); 29 | 30 | void initScene(); 31 | void update( float t ); 32 | void render(); 33 | void resize(int, int); 34 | }; 35 | 36 | #endif // SCENEDECAY_H 37 | -------------------------------------------------------------------------------- /chapter08/scenenightvision.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENENIGHTVISION_H 2 | #define SCENENIGHTVISION_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "torus.h" 8 | #include "teapot.h" 9 | 10 | #include "cookbookogl.h" 11 | 12 | #include 13 | 14 | class SceneNightVision : public Scene 15 | { 16 | private: 17 | GLSLProgram prog; 18 | 19 | GLuint fsQuad, pass1Index, pass2Index; 20 | GLuint renderFBO; 21 | GLuint renderTex; 22 | GLuint noiseTex; 23 | 24 | Plane plane; 25 | Torus torus; 26 | Teapot teapot; 27 | 28 | float angle; 29 | 30 | void setMatrices(); 31 | void compileAndLinkShader(); 32 | void setupFBO(); 33 | void pass1(); 34 | void pass2(); 35 | 36 | public: 37 | SceneNightVision(); 38 | 39 | void initScene(); 40 | void update( float t ); 41 | void render(); 42 | void resize(int, int); 43 | }; 44 | 45 | #endif // SCENENIGHTVISION_H 46 | -------------------------------------------------------------------------------- /chapter08/scenenoise.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENENOISE_H 2 | #define SCENENOISE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | 7 | #include "cookbookogl.h" 8 | 9 | #include 10 | 11 | class SceneNoise : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | GLuint quad; 17 | 18 | glm::vec3 lightPos; 19 | float angle; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | void drawScene(); 24 | 25 | public: 26 | SceneNoise(); 27 | 28 | void initScene(); 29 | void update( float t ); 30 | void render(); 31 | void resize(int, int); 32 | }; 33 | 34 | #endif // SCENENOISE_H 35 | -------------------------------------------------------------------------------- /chapter08/scenepaint.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPAINT_H 2 | #define SCENEPAINT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | #include "teapot.h" 8 | 9 | #include 10 | 11 | class ScenePaint : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | Teapot teapot; 17 | GLuint quad; 18 | 19 | glm::vec4 lightPos; 20 | float angle; 21 | 22 | void setMatrices(); 23 | void compileAndLinkShader(); 24 | void drawScene(); 25 | 26 | public: 27 | ScenePaint(); 28 | 29 | void initScene(); 30 | void update( float t ); 31 | void render(); 32 | void resize(int, int); 33 | }; 34 | 35 | #endif // SCENEPAINT_H 36 | -------------------------------------------------------------------------------- /chapter08/scenesky.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESKY_H 2 | #define SCENESKY_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneSky : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | GLuint quad; 16 | 17 | glm::vec3 lightPos; 18 | float angle; 19 | 20 | void setMatrices(); 21 | void compileAndLinkShader(); 22 | void drawScene(); 23 | 24 | public: 25 | SceneSky(); 26 | 27 | void initScene(); 28 | void update( float t ); 29 | void render(); 30 | void resize(int, int); 31 | }; 32 | 33 | #endif // SCENESKY_H 34 | -------------------------------------------------------------------------------- /chapter08/scenewood.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEWOOD_H 2 | #define SCENEWOOD_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneWood : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | GLuint quad; 16 | glm::vec3 lightPos; 17 | float angle; 18 | 19 | void setMatrices(); 20 | void compileAndLinkShader(); 21 | void drawScene(); 22 | 23 | public: 24 | SceneWood(); 25 | 26 | void initScene(); 27 | void update( float t ); 28 | void render(); 29 | void resize(int, int); 30 | }; 31 | 32 | #endif // SCENEWOOD_H 33 | -------------------------------------------------------------------------------- /chapter08/shader/decay.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform struct LightInfo { 4 | vec4 Position; 5 | vec3 Intensity; 6 | } Light; 7 | 8 | uniform struct MaterialInfo { 9 | vec3 Ka; 10 | vec3 Kd; 11 | vec3 Ks; 12 | float Shininess; 13 | } Material; 14 | 15 | uniform sampler2D NoiseTex; 16 | 17 | in vec4 Position; 18 | in vec3 Normal; 19 | in vec2 TexCoord; 20 | 21 | uniform float LowThreshold; 22 | uniform float HighThreshold; 23 | 24 | layout ( location = 0 ) out vec4 FragColor; 25 | 26 | vec3 phongModel() { 27 | vec3 n = Normal; 28 | vec3 s = normalize(Light.Position.xyz - Position.xyz); 29 | vec3 v = normalize(-Position.xyz); 30 | vec3 r = reflect( -s, n ); 31 | float sDotN = max( dot(s,n), 0.0 ); 32 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 33 | vec3 spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | 38 | return diffuse + spec; 39 | } 40 | 41 | void main() 42 | { 43 | vec4 noise = texture( NoiseTex, TexCoord ); 44 | 45 | if( noise.a < LowThreshold ) discard; 46 | if( noise.a > HighThreshold ) discard; 47 | 48 | vec3 color = phongModel(); 49 | FragColor = vec4( color , 1.0 ); 50 | } 51 | -------------------------------------------------------------------------------- /chapter08/shader/decay.vs: -------------------------------------------------------------------------------- 1 | #version 400 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 vec4 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 MVP; 14 | 15 | void main() 16 | { 17 | Position = ModelViewMatrix * vec4(VertexPosition,1.0); 18 | Normal = NormalMatrix * VertexNormal; 19 | TexCoord = VertexTexCoord; 20 | gl_Position = MVP * vec4(VertexPosition, 1.0); 21 | } 22 | -------------------------------------------------------------------------------- /chapter08/shader/nightvision.vs: -------------------------------------------------------------------------------- 1 | #version 410 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter08/shader/noisetex.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform sampler2D NoiseTex; 4 | 5 | in vec2 TexCoord; 6 | 7 | layout ( location = 0 ) out vec4 FragColor; 8 | 9 | void main() 10 | { 11 | vec4 noise = texture(NoiseTex, TexCoord); 12 | FragColor = vec4( noise.aaa , 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /chapter08/shader/noisetex.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 2) in vec2 VertexTexCoord; 5 | 6 | out vec2 TexCoord; 7 | 8 | uniform mat4 MVP; 9 | 10 | void main() 11 | { 12 | TexCoord = VertexTexCoord; 13 | gl_Position = MVP * vec4(VertexPosition, 1.0); 14 | } 15 | -------------------------------------------------------------------------------- /chapter08/shader/paint.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform struct LightInfo { 4 | vec4 Position; 5 | vec3 Intensity; 6 | } Light; 7 | 8 | uniform struct MaterialInfo { 9 | vec3 Ka; 10 | vec3 Kd; 11 | vec3 Ks; 12 | float Shininess; 13 | } Material; 14 | 15 | uniform sampler2D NoiseTex; 16 | 17 | in vec4 Position; 18 | in vec3 Normal; 19 | in vec2 TexCoord; 20 | 21 | uniform vec3 PaintColor = vec3(1.0); 22 | uniform float Threshold = 0.62; 23 | 24 | layout ( location = 0 ) out vec4 FragColor; 25 | 26 | vec3 phongModel(vec3 kd) { 27 | vec3 n = Normal; 28 | vec3 s = normalize(Light.Position.xyz - Position.xyz); 29 | vec3 v = normalize(-Position.xyz); 30 | vec3 r = reflect( -s, n ); 31 | float sDotN = max( dot(s,n), 0.0 ); 32 | vec3 diffuse = Light.Intensity * kd * sDotN; 33 | vec3 spec = vec3(0.0); 34 | if( sDotN > 0.0 ) 35 | spec = Light.Intensity * Material.Ks * 36 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 37 | 38 | return diffuse + spec; 39 | } 40 | 41 | void main() 42 | { 43 | vec4 noise = texture( NoiseTex, TexCoord ); 44 | vec3 color = Material.Kd; 45 | if( noise.a > Threshold ) color = PaintColor; 46 | 47 | FragColor = vec4( phongModel(color) , 1.0 ); 48 | } 49 | -------------------------------------------------------------------------------- /chapter08/shader/paint.vs: -------------------------------------------------------------------------------- 1 | #version 400 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 vec4 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 MVP; 14 | 15 | void main() 16 | { 17 | Position = ModelViewMatrix * vec4(VertexPosition,1.0); 18 | Normal = NormalMatrix * VertexNormal; 19 | TexCoord = VertexTexCoord; 20 | gl_Position = MVP * vec4(VertexPosition, 1.0); 21 | } 22 | -------------------------------------------------------------------------------- /chapter08/shader/sky.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | #define PI 3.14159265 4 | 5 | uniform vec4 Color; 6 | 7 | uniform sampler2D NoiseTex; 8 | 9 | uniform vec4 SkyColor = vec4( 0.3, 0.3, 0.9, 1.0 ); 10 | uniform vec4 CloudColor = vec4( 1.0, 1.0, 1.0, 1.0 ); 11 | 12 | in vec2 TexCoord; 13 | 14 | layout ( location = 0 ) out vec4 FragColor; 15 | 16 | void main() 17 | { 18 | vec4 noise = texture(NoiseTex, TexCoord); 19 | float t = (cos( noise.a * PI ) + 1.0) / 2.0; 20 | vec4 color = mix( SkyColor, CloudColor, t ); 21 | 22 | FragColor = vec4( color.rgb , 1.0 ); 23 | } 24 | -------------------------------------------------------------------------------- /chapter08/shader/sky.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | layout (location = 2) in vec2 VertexTexCoord; 5 | 6 | out vec2 TexCoord; 7 | 8 | uniform mat4 MVP; 9 | 10 | void main() 11 | { 12 | TexCoord = VertexTexCoord; 13 | gl_Position = MVP * vec4(VertexPosition, 1.0); 14 | } 15 | -------------------------------------------------------------------------------- /chapter08/shader/wood.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform sampler2D NoiseTex; 4 | 5 | uniform vec4 DarkWoodColor = vec4( 0.8, 0.5, 0.1, 1.0 ); 6 | uniform vec4 LightWoodColor = vec4( 1.0, 0.75, 0.25, 1.0 ); 7 | uniform mat4 Slice; 8 | 9 | in vec2 TexCoord; 10 | 11 | layout ( location = 0 ) out vec4 FragColor; 12 | 13 | void main() 14 | { 15 | // Transform the texture coordinates to define the 16 | // "slice" of the log. 17 | vec2 tc = TexCoord; 18 | if( tc.s > 0.5 ) tc.s = 1.0 - tc.s; 19 | vec4 cyl = Slice * vec4( tc, 0.0, 1.0 ); 20 | 21 | // The distance from the log's y axis 22 | float dist = length(cyl.xz); 23 | 24 | // Perturb the distance using the noise texture. 25 | vec4 noise = texture(NoiseTex, tc); 26 | dist += noise.b * 2.5; 27 | 28 | // Determine the color as a mixture of the light and 29 | // dark wood colors 30 | float t = 1.0 - abs( fract( dist ) * 2.0 - 1.0 ); 31 | t = smoothstep( 0.2, 0.5, t ); 32 | vec4 color = mix( DarkWoodColor, LightWoodColor, t ); 33 | 34 | FragColor = vec4( color.rgb , 1.0 ); 35 | } 36 | -------------------------------------------------------------------------------- /chapter08/shader/wood.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0 ) in vec3 VertexPosition; 4 | layout (location = 2) in vec2 VertexTexCoord; 5 | 6 | out vec2 TexCoord; 7 | 8 | uniform mat4 MVP; 9 | 10 | void main() 11 | { 12 | TexCoord = VertexTexCoord; 13 | gl_Position = MVP * vec4(VertexPosition, 1.0); 14 | } 15 | -------------------------------------------------------------------------------- /chapter09/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target chapter09) 2 | set( chapter09_SOURCES 3 | main.cpp 4 | scenefire.cpp 5 | sceneparticles.cpp 6 | sceneparticlesfeedback.cpp 7 | sceneparticlesinstanced.cpp 8 | scenesmoke.cpp 9 | scenewave.cpp 10 | ) 11 | 12 | add_executable( ${target} ${chapter09_SOURCES} ) 13 | target_compile_definitions(${target} 14 | PRIVATE 15 | GLFW_INCLUDE_NONE 16 | ) 17 | 18 | target_link_libraries( ${target} 19 | PRIVATE 20 | ingredients 21 | glfw 22 | ${OPENGL_gl_LIBRARY} 23 | ) 24 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 25 | -------------------------------------------------------------------------------- /chapter09/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scenerunner.h" 2 | #include "scenefire.h" 3 | #include "sceneparticles.h" 4 | #include "sceneparticlesfeedback.h" 5 | #include "sceneparticlesinstanced.h" 6 | #include "scenesmoke.h" 7 | #include "scenewave.h" 8 | 9 | #include 10 | 11 | std::map sceneInfo = { 12 | { "fire", "particles simulating fire" }, 13 | { "particles", "a fountain of particles" }, 14 | { "particles-feedback", "a fountain of particles implemented with transform feedback" }, 15 | { "particles-instanced", "a fountain of instanced particles, mmmm.. donuts" }, 16 | { "smoke", "particles simulating smoke" }, 17 | { "wave", "a plane wave displacement animation" } 18 | }; 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | std::string recipe = SceneRunner::parseCLArgs(argc, argv, sceneInfo); 23 | 24 | SceneRunner runner("Chapter 9 - " + recipe); 25 | 26 | std::unique_ptr scene; 27 | if( recipe == "fire" ) { 28 | scene = std::unique_ptr( new SceneFire() ); 29 | } else if( recipe == "particles") { 30 | scene = std::unique_ptr( new SceneParticles() ); 31 | } else if( recipe == "particles-feedback") { 32 | scene = std::unique_ptr( new SceneParticlesFeedback() ); 33 | } else if( recipe == "particles-instanced" ) { 34 | scene = std::unique_ptr( new SceneParticlesInstanced() ); 35 | } else if( recipe == "smoke" ) { 36 | scene = std::unique_ptr( new SceneSmoke() ); 37 | } else if( recipe == "wave" ) { 38 | scene = std::unique_ptr( new SceneWave() ); 39 | } else { 40 | printf("Unknown recipe: %s\n", recipe.c_str()); 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | return runner.run(*scene); 45 | } 46 | -------------------------------------------------------------------------------- /chapter09/scenefire.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEFIRE_H 2 | #define SCENEFIRE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneFire : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | GLuint posBuf[2], velBuf[2]; 16 | GLuint particleArray[2]; 17 | GLuint feedback[2], initVel, startTime[2]; 18 | GLuint drawBuf; 19 | GLuint renderSub, updateSub; 20 | 21 | int nParticles; 22 | 23 | float angle; 24 | float time, deltaT; 25 | 26 | void setMatrices(); 27 | void compileAndLinkShader(); 28 | void initBuffers(); 29 | float randFloat(); 30 | 31 | public: 32 | SceneFire(); 33 | 34 | void initScene(); 35 | void update( float t ); 36 | void render(); 37 | void resize(int, int); 38 | }; 39 | 40 | #endif // SCENEPARTICLESFEEDBACK_H 41 | -------------------------------------------------------------------------------- /chapter09/sceneparticles.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPARTICLES_H 2 | #define SCENEPARTICLES_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneParticles : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | GLuint initVel, startTime, particles; 16 | GLuint nParticles; 17 | 18 | float angle; 19 | float time; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | void initBuffers(); 24 | float randFloat(); 25 | 26 | public: 27 | SceneParticles(); 28 | 29 | void initScene(); 30 | void update( float t ); 31 | void render(); 32 | void resize(int, int); 33 | }; 34 | 35 | #endif // SCENEPARTICLES_H 36 | -------------------------------------------------------------------------------- /chapter09/sceneparticlesfeedback.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPARTICLESFEEDBACK_H 2 | #define SCENEPARTICLESFEEDBACK_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneParticlesFeedback : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | GLuint posBuf[2], velBuf[2]; 16 | GLuint particleArray[2]; 17 | GLuint feedback[2], initVel, startTime[2]; 18 | GLuint drawBuf, query; 19 | GLuint renderSub, updateSub; 20 | 21 | int nParticles; 22 | 23 | float angle; 24 | float time, deltaT; 25 | 26 | void setMatrices(); 27 | void compileAndLinkShader(); 28 | void initBuffers(); 29 | float randFloat(); 30 | 31 | public: 32 | SceneParticlesFeedback(); 33 | 34 | void initScene(); 35 | void update( float t ); 36 | void render(); 37 | void resize(int, int); 38 | }; 39 | 40 | #endif // SCENEPARTICLESFEEDBACK_H 41 | -------------------------------------------------------------------------------- /chapter09/sceneparticlesinstanced.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPARTICLESINSTANCED_H 2 | #define SCENEPARTICLESINSTANCED_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | #include "torus.h" 8 | 9 | #include 10 | 11 | class SceneParticlesInstanced : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | //GLuint particleArray; 17 | GLuint initVel, startTime; 18 | Torus torus; 19 | 20 | int nParticles; 21 | float angle; 22 | float time, deltaT; 23 | 24 | void setMatrices(); 25 | void compileAndLinkShader(); 26 | void initBuffers(); 27 | float randFloat(); 28 | 29 | public: 30 | SceneParticlesInstanced(); 31 | 32 | void initScene(); 33 | void update( float t ); 34 | void render(); 35 | void resize(int, int); 36 | }; 37 | 38 | #endif // SCENEPARTICLESINSTANCED_H 39 | -------------------------------------------------------------------------------- /chapter09/scenesmoke.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENESMOKE_H 2 | #define SCENESMOKE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneSmoke : public Scene 11 | { 12 | private: 13 | GLSLProgram prog; 14 | 15 | GLuint posBuf[2], velBuf[2]; 16 | GLuint particleArray[2]; 17 | GLuint feedback[2], initVel, startTime[2]; 18 | GLuint drawBuf; 19 | GLuint renderSub, updateSub; 20 | 21 | int nParticles; 22 | float angle; 23 | float time, deltaT; 24 | 25 | void setMatrices(); 26 | void compileAndLinkShader(); 27 | void initBuffers(); 28 | float randFloat(); 29 | 30 | public: 31 | SceneSmoke(); 32 | 33 | void initScene(); 34 | void update( float t ); 35 | void render(); 36 | void resize(int, int); 37 | }; 38 | 39 | #endif // SCENESMOKE_H 40 | -------------------------------------------------------------------------------- /chapter09/scenewave.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEWAVE_H 2 | #define SCENEWAVE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "cookbookogl.h" 8 | 9 | #include 10 | 11 | class SceneWave : public Scene 12 | { 13 | private: 14 | GLSLProgram prog; 15 | 16 | Plane plane; 17 | 18 | float angle; 19 | float time; 20 | 21 | void setMatrices(); 22 | void compileAndLinkShader(); 23 | 24 | public: 25 | SceneWave(); 26 | 27 | void initScene(); 28 | void update( float t ); 29 | void render(); 30 | void resize(int, int); 31 | }; 32 | 33 | #endif // SCENEWAVE_H 34 | -------------------------------------------------------------------------------- /chapter09/shader/fire.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform sampler2D ParticleTex; 4 | 5 | in float Transp; 6 | 7 | layout ( location = 0 ) out vec4 FragColor; 8 | 9 | void main() 10 | { 11 | FragColor = texture(ParticleTex, gl_PointCoord); 12 | FragColor = vec4(mix( vec3(0,0,0), FragColor.xyz, Transp ), FragColor.a); 13 | FragColor.a *= Transp; 14 | } 15 | -------------------------------------------------------------------------------- /chapter09/shader/particleinstanced.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | struct LightInfo { 4 | vec4 Position; // Light position in eye coords. 5 | vec3 Intensity; // A,D,S intensity 6 | }; 7 | uniform LightInfo Light; 8 | 9 | struct MaterialInfo { 10 | vec3 Ka; // Ambient reflectivity 11 | vec3 Kd; // Diffuse reflectivity 12 | vec3 Ks; // Specular reflectivity 13 | float Shininess; // Specular shininess factor 14 | }; 15 | uniform MaterialInfo Material; 16 | 17 | in vec3 Position; 18 | in vec3 Normal; 19 | 20 | layout ( location = 0 ) out vec4 FragColor; 21 | 22 | vec3 phongModel( vec3 pos, vec3 norm ) 23 | { 24 | vec3 s = normalize(vec3(Light.Position) - pos); 25 | vec3 v = normalize(-pos.xyz); 26 | vec3 r = reflect( -s, norm ); 27 | vec3 ambient = Light.Intensity * Material.Ka; 28 | float sDotN = max( dot(s,norm), 0.0 ); 29 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 30 | vec3 spec = vec3(0.0); 31 | if( sDotN > 0.0 ) 32 | spec = Light.Intensity * Material.Ks * 33 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 34 | 35 | return ambient + diffuse + spec; 36 | } 37 | 38 | void main() 39 | { 40 | FragColor = vec4(phongModel(Position,Normal), 1.0); 41 | } 42 | -------------------------------------------------------------------------------- /chapter09/shader/particleinstanced.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | layout (location = 2) in vec3 VertexTexCoord; 6 | layout (location = 3) in vec3 VertexInitialVelocity; // world coords 7 | layout (location = 4) in float StartTime; 8 | 9 | out vec3 Position; 10 | out vec3 Normal; 11 | 12 | uniform float Time; 13 | uniform vec3 Gravity; // world coords 14 | uniform float ParticleLifetime; 15 | 16 | uniform mat4 ModelViewMatrix; 17 | uniform mat3 NormalMatrix; 18 | uniform mat4 ProjectionMatrix; 19 | uniform mat4 MVP; 20 | 21 | void main() 22 | { 23 | // Work in camera coordinates 24 | Position = (ModelViewMatrix * vec4(VertexPosition,1.0)).xyz; 25 | Normal = NormalMatrix * VertexNormal; 26 | vec3 g = NormalMatrix * Gravity; 27 | vec3 v0 = NormalMatrix * VertexInitialVelocity; 28 | 29 | if( Time > StartTime ) { 30 | float t = Time - StartTime; 31 | 32 | if( t < ParticleLifetime ) { 33 | Position += v0 * t + Gravity * t * t; 34 | } 35 | } 36 | 37 | // Draw at the current position 38 | gl_Position = ProjectionMatrix * vec4(Position, 1.0); 39 | } 40 | -------------------------------------------------------------------------------- /chapter09/shader/particles.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | in float Transp; 4 | uniform sampler2D ParticleTex; 5 | 6 | layout ( location = 0 ) out vec4 FragColor; 7 | 8 | void main() 9 | { 10 | FragColor = texture(ParticleTex, gl_PointCoord); 11 | FragColor.a *= Transp; 12 | } 13 | -------------------------------------------------------------------------------- /chapter09/shader/particles.vs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | layout (location = 0) in vec3 VertexInitVel; // Particle initial velocity 4 | layout (location = 1) in float StartTime; // Particle "birth" time 5 | 6 | out float Transp; // Transparency of the particle 7 | 8 | uniform float Time; // Animation time 9 | uniform vec3 Gravity = vec3(0.0,-0.05,0.0); // world coords 10 | uniform float ParticleLifetime; // Max particle lifetime 11 | 12 | uniform mat4 MVP; 13 | 14 | void main() 15 | { 16 | // Assume the initial position is (0,0,0). 17 | vec3 pos = vec3(0.0); 18 | Transp = 0.0; 19 | 20 | // Particle dosen't exist until the start time 21 | if( Time > StartTime ) { 22 | float t = Time - StartTime; 23 | 24 | if( t < ParticleLifetime ) { 25 | pos = VertexInitVel * t + Gravity * t * t; 26 | Transp = 1.0 - t / ParticleLifetime; 27 | } 28 | } 29 | 30 | // Draw at the current position 31 | gl_Position = MVP * vec4(pos, 1.0); 32 | } 33 | -------------------------------------------------------------------------------- /chapter09/shader/smoke.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | uniform sampler2D ParticleTex; 4 | 5 | in float Transp; 6 | 7 | layout ( location = 0 ) out vec4 FragColor; 8 | 9 | void main() 10 | { 11 | FragColor = texture(ParticleTex, gl_PointCoord); 12 | FragColor.a *= Transp; 13 | } 14 | -------------------------------------------------------------------------------- /chapter09/shader/transfeedback.fs: -------------------------------------------------------------------------------- 1 | #version 410 2 | 3 | uniform sampler2D ParticleTex; 4 | 5 | in float Transp; 6 | 7 | layout ( location = 0 ) out vec4 FragColor; 8 | 9 | void main() 10 | { 11 | FragColor = texture(ParticleTex, gl_PointCoord); 12 | FragColor.a *= Transp; 13 | } 14 | -------------------------------------------------------------------------------- /chapter09/shader/wave.fs: -------------------------------------------------------------------------------- 1 | #version 400 2 | 3 | uniform struct LightInfo { 4 | vec4 Position; 5 | vec3 Intensity; 6 | } Light; 7 | 8 | uniform struct MaterialInfo { 9 | vec3 Ka; 10 | vec3 Kd; 11 | vec3 Ks; 12 | float Shininess; 13 | } Material; 14 | 15 | in vec4 Position; 16 | in vec3 Normal; 17 | in vec2 TexCoord; 18 | 19 | uniform float Time; 20 | 21 | layout ( location = 0 ) out vec4 FragColor; 22 | 23 | vec3 phongModel(vec3 kd) { 24 | vec3 n = Normal; 25 | vec3 s = normalize(Light.Position.xyz - Position.xyz); 26 | vec3 v = normalize(-Position.xyz); 27 | vec3 r = reflect( -s, n ); 28 | float sDotN = max( dot(s,n), 0.0 ); 29 | vec3 diffuse = Light.Intensity * kd * sDotN; 30 | vec3 spec = vec3(0.0); 31 | if( sDotN > 0.0 ) 32 | spec = Light.Intensity * Material.Ks * 33 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 34 | 35 | return Material.Ka * Light.Intensity + diffuse + spec; 36 | } 37 | 38 | void main() 39 | { 40 | FragColor = vec4( phongModel(Material.Kd) , 1.0 ); 41 | } 42 | -------------------------------------------------------------------------------- /chapter09/shader/wave.vs: -------------------------------------------------------------------------------- 1 | #version 400 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 vec4 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform float Time; 12 | uniform float Freq = 2.5; 13 | uniform float Velocity = 2.5; 14 | uniform float Amp = 0.6; 15 | 16 | uniform mat4 ModelViewMatrix; 17 | uniform mat3 NormalMatrix; 18 | uniform mat4 MVP; 19 | 20 | void main() 21 | { 22 | vec4 pos = vec4(VertexPosition,1.0); 23 | 24 | float u = Freq * pos.x - Velocity * Time; 25 | pos.y = Amp * sin( u ); 26 | 27 | vec3 n = vec3(0.0); 28 | n.xy = normalize(vec2(cos( u ), 1.0)); 29 | 30 | Position = ModelViewMatrix * pos; 31 | Normal = NormalMatrix * n; 32 | TexCoord = VertexTexCoord; 33 | gl_Position = MVP * pos; 34 | } 35 | -------------------------------------------------------------------------------- /chapter10/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set( target chapter10 ) 2 | 3 | set( chapter10_SOURCES 4 | main.cpp 5 | scenemandelbrot.cpp 6 | scenecloth.cpp 7 | sceneparticles.cpp 8 | sceneedge.cpp 9 | ) 10 | 11 | add_executable( ${target} ${chapter10_SOURCES} ) 12 | 13 | target_compile_definitions(${target} 14 | PRIVATE 15 | GLFW_INCLUDE_NONE 16 | ) 17 | 18 | target_link_libraries( ${target} 19 | PRIVATE 20 | ingredients 21 | glfw 22 | ${OPENGL_gl_LIBRARY} 23 | ) 24 | 25 | file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shader DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) 26 | -------------------------------------------------------------------------------- /chapter10/main.cpp: -------------------------------------------------------------------------------- 1 | #include "scenerunner.h" 2 | #include "scenemandelbrot.h" 3 | #include "scenecloth.h" 4 | #include "sceneparticles.h" 5 | #include "sceneedge.h" 6 | 7 | #include 8 | 9 | std::map sceneInfo = { 10 | { "particles", "Simple particle simulation" }, 11 | { "mandelbrot", "Mandelbrot set with compute shader" }, 12 | { "cloth", "Cloth simulation with compute shader" }, 13 | { "edge", "Edge detection filter using compute shader" } 14 | }; 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | std::string recipe = SceneRunner::parseCLArgs(argc, argv, sceneInfo); 19 | 20 | SceneRunner runner("Chapter 10 - " + recipe); 21 | 22 | std::unique_ptr scene; 23 | if( recipe == "mandelbrot" ) { 24 | scene = std::unique_ptr( new SceneMandelbrot() ); 25 | } else if( recipe == "cloth" ) { 26 | scene = std::unique_ptr( new SceneCloth() ); 27 | } else if( recipe == "particles" ) { 28 | scene = std::unique_ptr( new SceneParticles() ); 29 | } else if( recipe == "edge" ) { 30 | scene = std::unique_ptr( new SceneEdge() ); 31 | } else { 32 | printf("Unknown recipe: %s\n", recipe.c_str()); 33 | exit(EXIT_FAILURE); 34 | } 35 | 36 | return runner.run(*scene); 37 | } 38 | -------------------------------------------------------------------------------- /chapter10/scenecloth.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENECLOTH_H 2 | #define SCENECLOTH_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneCloth : public Scene 11 | { 12 | private: 13 | GLSLProgram renderProg, computeProg, computeProgNorm; 14 | 15 | GLuint clothVao; 16 | GLuint numElements; 17 | 18 | glm::ivec2 nParticles; // Number of particles in each dimension 19 | glm::vec2 clothSize; // Size of cloth in x and y 20 | 21 | float time, deltaT, speed; 22 | GLuint readBuf; 23 | GLuint posBufs[2], velBufs[2]; 24 | GLuint normBuf, elBuf, tcBuf; 25 | 26 | void compileAndLinkShader(); 27 | void initBuffers(); 28 | void setMatrices(); 29 | 30 | public: 31 | SceneCloth(); 32 | 33 | void initScene(); 34 | void update( float t ); 35 | void render(); 36 | void resize(int, int); 37 | }; 38 | 39 | #endif // SCENECLOTH_H 40 | -------------------------------------------------------------------------------- /chapter10/sceneedge.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEEDGE_H 2 | #define SCENEEDGE_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "plane.h" 7 | #include "torus.h" 8 | #include "teapot.h" 9 | #include "cookbookogl.h" 10 | 11 | #include 12 | 13 | class SceneEdge : public Scene 14 | { 15 | private: 16 | GLSLProgram prog, computeProg; 17 | 18 | GLuint fsQuad, pass1Index, pass2Index, fboHandle; 19 | 20 | Plane plane; 21 | Torus torus; 22 | Teapot teapot; 23 | 24 | float angle, tPrev, rotSpeed; 25 | 26 | void setMatrices(); 27 | void compileAndLinkShader(); 28 | void setupFBO(); 29 | void pass1(); 30 | void pass2(); 31 | 32 | public: 33 | SceneEdge(); 34 | 35 | void initScene(); 36 | void update( float t ); 37 | void render(); 38 | void resize(int, int); 39 | }; 40 | 41 | #endif // SCENEEDGE_H 42 | -------------------------------------------------------------------------------- /chapter10/scenemandelbrot.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEMANDELBROT_H 2 | #define SCENEMANDELBROT_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | #include "cube.h" 8 | 9 | #include 10 | 11 | class SceneMandelbrot : public Scene 12 | { 13 | private: 14 | GLSLProgram renderProg, computeProg; 15 | 16 | GLuint dataBuf, fsQuad; 17 | Cube cube; 18 | 19 | glm::vec2 center; 20 | float cheight; 21 | 22 | float time, deltaT, speed; 23 | float angle, rotSpeed; 24 | 25 | void compileAndLinkShader(); 26 | void initBuffers(); 27 | void setWindow(); 28 | 29 | public: 30 | SceneMandelbrot(); 31 | 32 | void initScene(); 33 | void update( float t ); 34 | void render(); 35 | void resize(int, int); 36 | }; 37 | 38 | #endif // SCENEMANDELBROT_H 39 | -------------------------------------------------------------------------------- /chapter10/sceneparticles.h: -------------------------------------------------------------------------------- 1 | #ifndef SCENEPARTICLES_H 2 | #define SCENEPARTICLES_H 3 | 4 | #include "scene.h" 5 | #include "glslprogram.h" 6 | #include "cookbookogl.h" 7 | 8 | #include 9 | 10 | class SceneParticles : public Scene 11 | { 12 | private: 13 | GLSLProgram renderProg, computeProg; 14 | 15 | glm::ivec3 nParticles; 16 | GLuint totalParticles; 17 | 18 | float time, deltaT, speed, angle; 19 | GLuint particlesVao; 20 | GLuint bhVao, bhBuf; // black hole VAO and buffer 21 | glm::vec4 bh1, bh2; 22 | 23 | void compileAndLinkShader(); 24 | void initBuffers(); 25 | void setMatrices(); 26 | 27 | public: 28 | SceneParticles(); 29 | 30 | void initScene(); 31 | void update( float t ); 32 | void render(); 33 | void resize(int, int); 34 | }; 35 | 36 | #endif // SceneParticles_H 37 | -------------------------------------------------------------------------------- /chapter10/shader/ads.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | uniform vec4 LightPosition; 8 | uniform vec3 LightIntensity; 9 | 10 | uniform vec3 Kd; // Diffuse reflectivity 11 | uniform vec3 Ka; // Ambient reflectivity 12 | uniform vec3 Ks; // Specular reflectivity 13 | uniform float Shininess; // Specular shininess factor 14 | 15 | layout( binding = 0 ) uniform sampler2D Tex; 16 | 17 | layout( location = 0 ) out vec4 FragColor; 18 | 19 | vec3 ads( ) 20 | { 21 | vec3 s = normalize( vec3(LightPosition) - Position ); 22 | vec3 v = normalize(vec3(-Position)); 23 | vec3 r = reflect( -s, Normal ); 24 | 25 | return 26 | LightIntensity * ( Ka + 27 | Kd * max( dot(s, Normal), 0.0 ) + 28 | Ks * pow( max( dot(r,v), 0.0 ), Shininess ) ); 29 | } 30 | 31 | void main() { 32 | vec4 texColor = texture( Tex, TexCoord ); 33 | FragColor = vec4(ads(), 1.0) * texColor; 34 | } 35 | -------------------------------------------------------------------------------- /chapter10/shader/ads.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec3 VertexPosition; 4 | layout (location = 1) in vec3 VertexNormal; 5 | layout (location = 2) in vec2 VTexCoord; 6 | 7 | out vec3 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 MVP; 14 | 15 | void main() 16 | { 17 | Normal = normalize( NormalMatrix * VertexNormal); 18 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 19 | TexCoord = VTexCoord; 20 | 21 | gl_Position = MVP * vec4(VertexPosition,1.0); 22 | } 23 | -------------------------------------------------------------------------------- /chapter10/shader/cloth_normal.cs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout( local_size_x = 10, local_size_y = 10 ) in; 4 | 5 | layout(std430, binding=0) buffer PosIn { 6 | vec4 Position[]; 7 | }; 8 | layout(std430, binding=4) buffer NormOut { 9 | vec4 Normal[]; 10 | }; 11 | 12 | void main() { 13 | uvec3 nParticles = gl_NumWorkGroups * gl_WorkGroupSize; 14 | uint idx = gl_GlobalInvocationID.y * nParticles.x + gl_GlobalInvocationID.x; 15 | 16 | vec3 p = vec3(Position[idx]); 17 | vec3 n = vec3(0); 18 | vec3 a, b, c; 19 | 20 | if( gl_GlobalInvocationID.y < nParticles.y - 1) { 21 | c = Position[idx + nParticles.x].xyz - p; 22 | if( gl_GlobalInvocationID.x < nParticles.x - 1 ) { 23 | a = Position[idx + 1].xyz - p; 24 | b = Position[idx + nParticles.x + 1].xyz - p; 25 | n += cross(a,b); 26 | n += cross(b,c); 27 | } 28 | if( gl_GlobalInvocationID.x > 0 ) { 29 | a = c; 30 | b = Position[idx + nParticles.x - 1].xyz - p; 31 | c = Position[idx - 1].xyz - p; 32 | n += cross(a,b); 33 | n += cross(b,c); 34 | } 35 | } 36 | 37 | if( gl_GlobalInvocationID.y > 0 ) { 38 | c = Position[idx - nParticles.x].xyz - p; 39 | if( gl_GlobalInvocationID.x > 0 ) { 40 | a = Position[idx - 1].xyz - p; 41 | b = Position[idx - nParticles.x - 1].xyz - p; 42 | n += cross(a,b); 43 | n += cross(b,c); 44 | } 45 | if( gl_GlobalInvocationID.x < nParticles.x - 1 ) { 46 | a = c; 47 | b = Position[idx - nParticles.x + 1].xyz - p; 48 | c = Position[idx + 1].xyz - p; 49 | n += cross(a,b); 50 | n += cross(b,c); 51 | } 52 | } 53 | 54 | Normal[idx] = vec4(normalize(n), 0.0); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /chapter10/shader/edge.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 Position; 4 | in vec3 Normal; 5 | in vec2 TexCoord; 6 | 7 | layout( binding = 0 ) uniform sampler2D EdgeTex; 8 | 9 | subroutine vec4 RenderPassType(); 10 | subroutine uniform RenderPassType RenderPass; 11 | 12 | struct LightInfo { 13 | vec4 Position; // Light position in eye coords. 14 | vec3 Intensity; // A,D,S intensity 15 | }; 16 | uniform LightInfo Light; 17 | 18 | struct MaterialInfo { 19 | vec3 Ka; // Ambient reflectivity 20 | vec3 Kd; // Diffuse reflectivity 21 | vec3 Ks; // Specular reflectivity 22 | float Shininess; // Specular shininess factor 23 | }; 24 | uniform MaterialInfo Material; 25 | 26 | layout( location = 0 ) out vec4 FragColor; 27 | 28 | vec3 phongModel( vec3 pos, vec3 norm ) 29 | { 30 | vec3 s = normalize(vec3(Light.Position) - pos); 31 | vec3 v = normalize(-pos.xyz); 32 | vec3 r = reflect( -s, norm ); 33 | vec3 ambient = Light.Intensity * Material.Ka; 34 | float sDotN = max( dot(s,norm), 0.0 ); 35 | vec3 diffuse = Light.Intensity * Material.Kd * sDotN; 36 | vec3 spec = vec3(0.0); 37 | if( sDotN > 0.0 ) 38 | spec = Light.Intensity * Material.Ks * 39 | pow( max( dot(r,v), 0.0 ), Material.Shininess ); 40 | 41 | return ambient + diffuse + spec; 42 | } 43 | 44 | subroutine (RenderPassType) 45 | vec4 pass1() 46 | { 47 | return vec4(phongModel( Position, Normal ),1.0); 48 | } 49 | 50 | subroutine( RenderPassType ) 51 | vec4 pass2() 52 | { 53 | return texture(EdgeTex, TexCoord); 54 | } 55 | 56 | void main() 57 | { 58 | // This will call either pass1() or pass2() 59 | FragColor = RenderPass(); 60 | } 61 | -------------------------------------------------------------------------------- /chapter10/shader/edge.vs: -------------------------------------------------------------------------------- 1 | #version 400 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 Position; 8 | out vec3 Normal; 9 | out vec2 TexCoord; 10 | 11 | uniform mat4 ModelViewMatrix; 12 | uniform mat3 NormalMatrix; 13 | uniform mat4 ProjectionMatrix; 14 | uniform mat4 MVP; 15 | 16 | void main() 17 | { 18 | TexCoord = VertexTexCoord; 19 | Normal = normalize( NormalMatrix * VertexNormal); 20 | Position = vec3( ModelViewMatrix * vec4(VertexPosition,1.0) ); 21 | 22 | gl_Position = MVP * vec4(VertexPosition,1.0); 23 | } 24 | -------------------------------------------------------------------------------- /chapter10/shader/mandelbrot.cs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | #define MAX_ITERATIONS 100 4 | 5 | layout( local_size_x = 32, local_size_y = 32 ) in; 6 | layout( binding = 0, rgba8) uniform image2D ColorImg; 7 | uniform vec4 CompWindow; 8 | uniform uint Width = 256; 9 | uniform uint Height = 256; 10 | 11 | uint mandelbrot( vec2 c ) { 12 | vec2 z = vec2(0.0,0.0); 13 | uint i = 0; 14 | while( i < MAX_ITERATIONS && (z.x*z.x + z.y*z.y) < 4.0 ) { 15 | z = vec2( z.x*z.x - z.y*z.y + c.x, 2 * z.x *z.y + c.y ); 16 | i++; 17 | } 18 | 19 | return i; 20 | } 21 | 22 | void main() { 23 | float dx = (CompWindow.z - CompWindow.x) / Width; 24 | float dy = (CompWindow.w - CompWindow.y) / Height; 25 | 26 | vec2 c = vec2( dx * gl_GlobalInvocationID.x + CompWindow.x, 27 | dy * gl_GlobalInvocationID.y + CompWindow.y); 28 | 29 | uint i = mandelbrot(c); 30 | vec4 color = vec4(0.0,0.5,0.5,1); 31 | if( i < MAX_ITERATIONS ) { 32 | if( i < 5 ) color = vec4(float(i)/5.0,0,0,1); 33 | else if( i < 10 ) color = vec4((float(i)-5.0)/5.0,1,0,1); 34 | else if( i < 15 ) color = vec4(1,0,(float(i)-10.0)/5.0,1); 35 | else color = vec4(0,0,1,0); 36 | } 37 | else 38 | color = vec4(0,0,0,1); 39 | 40 | imageStore(ColorImg, ivec2(gl_GlobalInvocationID.xy), color); 41 | } 42 | -------------------------------------------------------------------------------- /chapter10/shader/particles.cs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout( local_size_x = 1000 ) in; 4 | 5 | uniform float Gravity1 = 1000.0; 6 | uniform vec3 BlackHolePos1 = vec3(5,0,0); 7 | 8 | uniform float Gravity2 = 1000.0; 9 | uniform vec3 BlackHolePos2 = vec3(-5,0,0); 10 | 11 | uniform float ParticleMass = 0.1; 12 | uniform float ParticleInvMass = 1.0 / 0.1; 13 | uniform float DeltaT = 0.0005; 14 | uniform float MaxDist = 45.0; 15 | 16 | layout(std430, binding=0) buffer Pos { 17 | vec4 Position[]; 18 | }; 19 | layout(std430, binding=1) buffer Vel { 20 | vec4 Velocity[]; 21 | }; 22 | 23 | void main() { 24 | uint idx = gl_GlobalInvocationID.x; 25 | 26 | vec3 p = Position[idx].xyz; 27 | 28 | // Force from black hole #1 29 | vec3 d = BlackHolePos1 - p; 30 | float dist = length(d); 31 | vec3 force = (Gravity1 / dist) * normalize(d); 32 | 33 | // Force from black hole #2 34 | d = BlackHolePos2 - p; 35 | dist = length(d); 36 | force += (Gravity2 / dist) * normalize(d); 37 | 38 | // Reset particles that get too far from the attractors 39 | if( dist > MaxDist ) { 40 | Position[idx] = vec4(0,0,0,1); 41 | } else { 42 | // Apply simple Euler integrator 43 | vec3 a = force * ParticleInvMass; 44 | Position[idx] = vec4( 45 | p + Velocity[idx].xyz * DeltaT + 0.5 * a * DeltaT * DeltaT, 1.0); 46 | Velocity[idx] = vec4( Velocity[idx].xyz + a * DeltaT, 0.0); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /chapter10/shader/particles.fs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | in vec3 Position; 4 | 5 | uniform vec4 Color; 6 | 7 | layout( location = 0 ) out vec4 FragColor; 8 | 9 | void main() { 10 | FragColor = Color; 11 | } 12 | -------------------------------------------------------------------------------- /chapter10/shader/particles.vs: -------------------------------------------------------------------------------- 1 | #version 430 2 | 3 | layout (location = 0) in vec4 VertexPosition; 4 | 5 | out vec3 Position; 6 | 7 | uniform mat4 ModelViewMatrix; 8 | uniform mat3 NormalMatrix; 9 | uniform mat4 MVP; 10 | 11 | void main() 12 | { 13 | Position = (ModelViewMatrix * VertexPosition).xyz; 14 | gl_Position = MVP * VertexPosition; 15 | } 16 | -------------------------------------------------------------------------------- /ingredients/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(target ingredients) 2 | 3 | set(ingredients_SOURCES 4 | teapotpatch.cpp 5 | teapot.cpp 6 | torus.cpp 7 | glutils.cpp 8 | objmesh.cpp 9 | glslprogram.cpp 10 | cube.cpp 11 | plane.cpp 12 | sphere.cpp 13 | cookbookogl.h 14 | scene.h 15 | teapotdata.h 16 | glad/src/glad.c trianglemesh.cpp trianglemesh.h aabb.h 17 | scenerunner.h 18 | texture.h texture.cpp 19 | utils.h) 20 | 21 | add_library(${target} STATIC ${ingredients_SOURCES}) 22 | 23 | target_include_directories(${target} PUBLIC glad/include) 24 | 25 | target_link_libraries(${target} PUBLIC glm) 26 | 27 | set_property(TARGET ${target} PROPERTY CXX_STANDARD 17) 28 | set_property(TARGET ${target} PROPERTY CXX_STANDARD_REQUIRED ON) 29 | 30 | -------------------------------------------------------------------------------- /ingredients/aabb.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | // Axis-aligned bounding box 7 | class Aabb { 8 | 9 | public: 10 | glm::vec3 min, max; 11 | 12 | Aabb() { 13 | reset(); 14 | } 15 | 16 | Aabb( const glm::vec3 & pt ) { 17 | reset(); 18 | add(pt); 19 | } 20 | 21 | void add( glm::vec3 & pt ) { 22 | min.x = std::fmin(min.x, pt.x); 23 | min.y = std::fmin(min.y, pt.y); 24 | min.z = std::fmin(min.z, pt.z); 25 | 26 | max.x = std::fmax(max.x, pt.x); 27 | max.y = std::fmax(max.y, pt.y); 28 | max.z = std::fmax(max.z, pt.z); 29 | } 30 | 31 | void add(const Aabb & other) { 32 | min.x = std::fmin(min.x, other.min.x); 33 | min.y = std::fmin(min.y, other.min.y); 34 | min.z = std::fmin(min.z, other.min.z); 35 | 36 | max.x = std::fmax(max.x, other.max.x); 37 | max.y = std::fmax(max.y, other.max.y); 38 | max.z = std::fmax(max.z, other.max.z); 39 | } 40 | 41 | void reset() { 42 | min = glm::vec3( std::numeric_limits::max()); 43 | max = glm::vec3(-std::numeric_limits::max()); 44 | } 45 | 46 | glm::vec3 diagonal() { 47 | return max - min; 48 | } 49 | }; -------------------------------------------------------------------------------- /ingredients/cookbookogl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | -------------------------------------------------------------------------------- /ingredients/cube.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drawable.h" 4 | #include "trianglemesh.h" 5 | 6 | class Cube : public TriangleMesh 7 | { 8 | public: 9 | Cube(GLfloat size = 1.0f); 10 | }; 11 | -------------------------------------------------------------------------------- /ingredients/drawable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Drawable 4 | { 5 | public: 6 | virtual void render() const = 0; 7 | }; 8 | -------------------------------------------------------------------------------- /ingredients/glutils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cookbookogl.h" 4 | 5 | namespace GLUtils 6 | { 7 | int checkForOpenGLError(const char *, int); 8 | 9 | void dumpGLInfo(bool dumpExtensions = false); 10 | 11 | void APIENTRY debugCallback( GLenum source, GLenum type, GLuint id, 12 | GLenum severity, GLsizei length, const GLchar * msg, const void * param ); 13 | } 14 | -------------------------------------------------------------------------------- /ingredients/plane.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "trianglemesh.h" 4 | 5 | class Plane : public TriangleMesh 6 | { 7 | public: 8 | Plane(float xsize, float zsize, int xdivs, int zdivs, float smax = 1.0f, float tmax = 1.0f); 9 | }; 10 | -------------------------------------------------------------------------------- /ingredients/scene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class Scene 6 | { 7 | protected: 8 | glm::mat4 model, view, projection; 9 | 10 | public: 11 | int width; 12 | int height; 13 | 14 | Scene() : m_animate(true), width(800), height(600) { } 15 | virtual ~Scene() {} 16 | 17 | void setDimensions( int w, int h ) { 18 | width = w; 19 | height = h; 20 | } 21 | 22 | /** 23 | Load textures, initialize shaders, etc. 24 | */ 25 | virtual void initScene() = 0; 26 | 27 | /** 28 | This is called prior to every frame. Use this 29 | to update your animation. 30 | */ 31 | virtual void update( float t ) = 0; 32 | 33 | /** 34 | Draw your scene. 35 | */ 36 | virtual void render() = 0; 37 | 38 | /** 39 | Called when screen is resized 40 | */ 41 | virtual void resize(int, int) = 0; 42 | 43 | void animate( bool value ) { m_animate = value; } 44 | bool animating() { return m_animate; } 45 | 46 | protected: 47 | bool m_animate; 48 | }; 49 | -------------------------------------------------------------------------------- /ingredients/sphere.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "trianglemesh.h" 4 | #include "cookbookogl.h" 5 | 6 | class Sphere : public TriangleMesh 7 | { 8 | public: 9 | Sphere(float rad, GLuint sl, GLuint st); 10 | }; 11 | -------------------------------------------------------------------------------- /ingredients/teapotpatch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "trianglemesh.h" 4 | #include 5 | 6 | class TeapotPatch : public TriangleMesh 7 | { 8 | public: 9 | TeapotPatch(); 10 | 11 | virtual void render() const; 12 | 13 | private: 14 | void generatePatches(std::vector & pts); 15 | void buildPatchReflect(int patchNum, 16 | std::vector & p, int &index, 17 | bool reflectX, bool reflectY); 18 | void buildPatch(glm::vec3 patch[][4], 19 | std::vector & pts, int &index, glm::mat3 reflect ); 20 | void getPatch( int patchNum, glm::vec3 patch[][4], bool reverseV ); 21 | 22 | void initBuffers(std::vector & pts); 23 | }; 24 | -------------------------------------------------------------------------------- /ingredients/texture.h: -------------------------------------------------------------------------------- 1 | 2 | #include "cookbookogl.h" 3 | #include 4 | 5 | class Texture { 6 | public: 7 | static GLuint loadTexture( const std::string & fName ); 8 | static GLuint loadCubeMap( const std::string & baseName ); 9 | static unsigned char * loadPixels( const std::string & fName, int & w, int & h ); 10 | }; -------------------------------------------------------------------------------- /ingredients/torus.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "trianglemesh.h" 4 | #include "cookbookogl.h" 5 | 6 | class Torus : public TriangleMesh 7 | { 8 | public: 9 | Torus(GLfloat outerRadius, GLfloat innerRadius, GLuint nsides, GLuint nrings); 10 | }; 11 | -------------------------------------------------------------------------------- /ingredients/trianglemesh.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cookbookogl.h" 6 | #include "drawable.h" 7 | 8 | class TriangleMesh : public Drawable { 9 | 10 | protected: 11 | 12 | GLuint nVerts; // Number of vertices 13 | GLuint vao; // The Vertex Array Object 14 | 15 | // Vertex buffers 16 | std::vector buffers; 17 | 18 | virtual void initBuffers( 19 | std::vector * indices, 20 | std::vector * points, 21 | std::vector * normals, 22 | std::vector * texCoords = nullptr, 23 | std::vector * tangents = nullptr 24 | ); 25 | 26 | virtual void deleteBuffers(); 27 | 28 | public: 29 | virtual ~TriangleMesh(); 30 | virtual void render() const; 31 | GLuint getVao() const { return vao; } 32 | }; 33 | -------------------------------------------------------------------------------- /ingredients/utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class Utils { 6 | public: 7 | static void trimString( std::string & str ) { 8 | const char * whiteSpace = " \t\n\r"; 9 | size_t location; 10 | location = str.find_first_not_of(whiteSpace); 11 | str.erase(0,location); 12 | location = str.find_last_not_of(whiteSpace); 13 | str.erase(location + 1); 14 | } 15 | }; -------------------------------------------------------------------------------- /media/spot/README.txt: -------------------------------------------------------------------------------- 1 | Spot 2 | ==== 3 | contact: Keenan Crane (keenan@cs.caltech.edu) 4 | 5 | Files 6 | -------------------------------------------- 7 | This archive contains a description of the surface "Spot," including: 8 | 9 | 1. the original Catmull-Clark control mesh [spot_control_mesh.obj] 10 | 2. the original texture map as a vector image [spot_texture.svg] 11 | 3. triangular and quadrilateral tessellations [spot_triangulated.obj, spot_quadrangulated.obj] 12 | 4. the texture map as a raster image [spot_texture.png] 13 | 14 | Note that (3) and (4) can be generated from (1) and (2), and are provided only 15 | for convenience. Meshes are stored in the Wavefront OBJ format. All meshes 16 | are manifold, genus-0 embeddings. The texture map should be interpreted as a 17 | unit square in the positive quadrant [0,1]x[0,1]. 18 | 19 | License 20 | -------------------------------------------- 21 | 22 | As the sole author of this data, I hereby release it into the public domain. 23 | 24 | -------------------------------------------------------------------------------- /media/spot/spot_texture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/spot/spot_texture.png -------------------------------------------------------------------------------- /media/texture/ao_ears.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/ao_ears.png -------------------------------------------------------------------------------- /media/texture/bluewater.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/bluewater.png -------------------------------------------------------------------------------- /media/texture/brick1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/brick1.jpg -------------------------------------------------------------------------------- /media/texture/cement.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cement.jpg -------------------------------------------------------------------------------- /media/texture/cubemap_night/night_negx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cubemap_night/night_negx.png -------------------------------------------------------------------------------- /media/texture/cubemap_night/night_negy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cubemap_night/night_negy.png -------------------------------------------------------------------------------- /media/texture/cubemap_night/night_negz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cubemap_night/night_negz.png -------------------------------------------------------------------------------- /media/texture/cubemap_night/night_posx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cubemap_night/night_posx.png -------------------------------------------------------------------------------- /media/texture/cubemap_night/night_posy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cubemap_night/night_posy.png -------------------------------------------------------------------------------- /media/texture/cubemap_night/night_posz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/cubemap_night/night_posz.png -------------------------------------------------------------------------------- /media/texture/fire.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/fire.png -------------------------------------------------------------------------------- /media/texture/flower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/flower.png -------------------------------------------------------------------------------- /media/texture/me_textile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/me_textile.png -------------------------------------------------------------------------------- /media/texture/moss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/moss.png -------------------------------------------------------------------------------- /media/texture/ogre_diffuse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/ogre_diffuse.png -------------------------------------------------------------------------------- /media/texture/ogre_normalmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/ogre_normalmap.png -------------------------------------------------------------------------------- /media/texture/smoke.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/smoke.png -------------------------------------------------------------------------------- /media/texture/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daw42/glslcookbook/3a13cd803f88672cc56d6139013710d2f305b863/media/texture/star.png --------------------------------------------------------------------------------