├── Shader ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.jpg │ ├── 3.png │ └── 4.png └── code │ ├── Shader.exe │ ├── opengl_math.h │ ├── shader.fs │ ├── shader.vs │ └── main.cpp ├── index ├── image │ ├── 1.png │ ├── 2.png │ └── 3.png └── code │ ├── index.exe │ ├── main.cpp │ ├── opengl_math.h │ ├── shader.fs │ └── shader.vs ├── Texture ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png └── Texture │ ├── test.png │ ├── Texture.exe │ ├── opengl_math.h │ ├── opengl_pipeline.cpp │ ├── shader.fs │ ├── shader.vs │ ├── opengl_texture.h │ ├── opengl_camera.h │ ├── opengl_pipeline.h │ ├── opengl_camera.cpp │ └── main.cpp ├── Uniform ├── image │ └── 1.png ├── code │ ├── main.cpp │ ├── Uniform.exe │ ├── opengl_math.h │ ├── shader.fs │ └── shader.vs └── OpenGL学习之路5----使用一致变量(Uniform Variables).md ├── hello_dot ├── code │ ├── main.cpp │ ├── hello_dot.exe │ └── opengl_math.h ├── image │ └── 1.png └── OpenGL学习之路2----画一个点.md ├── Camera_Mouse ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ └── 7.png └── code │ ├── Camera_Mouse.exe │ ├── opengl_pipeline.cpp │ ├── shader.fs │ ├── shader.vs │ ├── opengl_camera.h │ ├── opengl_pipeline.h │ ├── opengl_camera.cpp │ └── main.cpp ├── Camera_Space ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ └── 8.png └── code │ ├── main.cpp │ ├── Camera_Space.exe │ ├── opengl_pipeline.cpp │ ├── shader.fs │ ├── shader.vs │ └── opengl_pipeline.h ├── Interpolation ├── image │ └── 1.png └── code │ ├── opengl_math.h │ ├── Interpolation.exe │ ├── shader.fs │ ├── shader.vs │ └── main.cpp ├── Spot_Lighting ├── image │ ├── 1.png │ ├── 2.png │ └── 3.png ├── code │ ├── test.png │ ├── opengl_math.h │ ├── Spot_Lighting.exe │ ├── opengl_pipeline.cpp │ ├── shader.vs │ ├── opengl_texture.h │ ├── opengl_camera.h │ ├── opengl_pipeline.h │ ├── opengl_light.h │ └── opengl_camera.cpp └── OpenGL学习之路19---- 聚光灯光源.md ├── Ambient_Lighting ├── image │ ├── 1.png │ └── 2.png ├── Ambient_Lighting │ ├── test.png │ ├── opengl_math.h │ ├── opengl_pipeline.cpp │ ├── Ambient_Lighting.exe │ ├── shader.vs │ ├── shader.fs │ ├── opengl_texture.h │ ├── opengl_pipeline.h │ ├── opengl_camera.h │ └── main.cpp └── OpenGL学习之路15----环境光.md ├── Camera_Keyboard ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ └── 4.png └── code │ ├── main.cpp │ ├── Camera_Keyboard.exe │ ├── opengl_pipeline.cpp │ ├── shader.fs │ ├── shader.vs │ ├── opengl_camera.cpp │ ├── opengl_camera.h │ └── opengl_pipeline.h ├── Create_a_Window ├── image │ └── 1.png ├── code │ ├── Create_a_Window.exe │ └── main.c └── OpenGL学习之路1----打开一个窗口.md ├── Diffuse_Lighting ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ └── 4.png ├── Diffuse_Lighting │ ├── main.cpp │ ├── test.png │ ├── opengl_math.h │ ├── opengl_pipeline.cpp │ ├── Diffuse_Lighting.exe │ ├── shader.vs │ ├── shader.fs │ ├── opengl_texture.h │ ├── opengl_pipeline.h │ └── opengl_camera.h └── OpenGL学习之路16---- 漫射光.md ├── First_Triangle ├── image │ ├── 1.png │ └── 2.png ├── code │ ├── opengl_math.h │ ├── First_Triangle.exe │ └── main.cpp └── OpenGL学习之路3----画一个三角形.md ├── Point_Lighting ├── code │ ├── test.png │ ├── opengl_math.h │ ├── Point_Lighting.exe │ ├── opengl_pipeline.cpp │ ├── shader.vs │ ├── opengl_texture.h │ ├── opengl_camera.h │ ├── opengl_pipeline.h │ ├── opengl_light.h │ ├── opengl_camera.cpp │ ├── opengl_light.cpp │ └── shader.fs └── image │ ├── 1.png │ ├── 2.png │ └── 3.png ├── Specular_lighting ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png ├── code │ ├── main.cpp │ ├── shader.fs │ ├── test.png │ ├── opengl_math.h │ ├── opengl_pipeline.cpp │ ├── Specular_lighting.exe │ ├── shader.vs │ ├── opengl_texture.h │ ├── opengl_camera.h │ ├── opengl_pipeline.h │ └── opengl_camera.cpp └── OpenGL学习之路17---- 镜面反射光.md ├── prespective_projection ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png └── code │ ├── opengl_pipeline.cpp │ ├── prespective_projection.exe │ ├── shader.fs │ ├── shader.vs │ ├── opengl_pipeline.h │ └── main.cpp ├── Translation_Transformation ├── image │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ ├── 9.png │ ├── 10.png │ ├── 11.png │ └── 12.png └── Translation_Transformation │ ├── shader.fs │ ├── main.cpp │ ├── opengl_math.h │ ├── Translation_Transformation.exe │ └── shader.vs ├── Concatenating_Transformations ├── code │ ├── main.cpp │ ├── opengl_math.h │ ├── shader.fs │ ├── Concatenating_Transformations.exe │ ├── shader.vs │ ├── opengl_pipeline.cpp │ └── opengl_pipeline.h └── image │ ├── 1.png │ ├── 2.png │ └── 3.png └── README.md /Shader/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/image/1.png -------------------------------------------------------------------------------- /Shader/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/image/2.png -------------------------------------------------------------------------------- /Shader/image/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/image/3.jpg -------------------------------------------------------------------------------- /Shader/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/image/3.png -------------------------------------------------------------------------------- /Shader/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/image/4.png -------------------------------------------------------------------------------- /index/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/index/image/1.png -------------------------------------------------------------------------------- /index/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/index/image/2.png -------------------------------------------------------------------------------- /index/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/index/image/3.png -------------------------------------------------------------------------------- /Texture/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/image/1.png -------------------------------------------------------------------------------- /Texture/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/image/2.png -------------------------------------------------------------------------------- /Texture/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/image/3.png -------------------------------------------------------------------------------- /Texture/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/image/4.png -------------------------------------------------------------------------------- /Texture/image/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/image/5.png -------------------------------------------------------------------------------- /Texture/image/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/image/6.png -------------------------------------------------------------------------------- /Uniform/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Uniform/image/1.png -------------------------------------------------------------------------------- /index/code/index.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/index/code/index.exe -------------------------------------------------------------------------------- /index/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/index/code/main.cpp -------------------------------------------------------------------------------- /Shader/code/Shader.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/code/Shader.exe -------------------------------------------------------------------------------- /Uniform/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Uniform/code/main.cpp -------------------------------------------------------------------------------- /hello_dot/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/hello_dot/code/main.cpp -------------------------------------------------------------------------------- /hello_dot/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/hello_dot/image/1.png -------------------------------------------------------------------------------- /Camera_Mouse/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/1.png -------------------------------------------------------------------------------- /Camera_Mouse/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/2.png -------------------------------------------------------------------------------- /Camera_Mouse/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/3.png -------------------------------------------------------------------------------- /Camera_Mouse/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/4.png -------------------------------------------------------------------------------- /Camera_Mouse/image/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/5.png -------------------------------------------------------------------------------- /Camera_Mouse/image/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/6.png -------------------------------------------------------------------------------- /Camera_Mouse/image/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/image/7.png -------------------------------------------------------------------------------- /Camera_Space/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/1.png -------------------------------------------------------------------------------- /Camera_Space/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/2.png -------------------------------------------------------------------------------- /Camera_Space/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/3.png -------------------------------------------------------------------------------- /Camera_Space/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/4.png -------------------------------------------------------------------------------- /Camera_Space/image/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/5.png -------------------------------------------------------------------------------- /Camera_Space/image/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/6.png -------------------------------------------------------------------------------- /Camera_Space/image/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/7.png -------------------------------------------------------------------------------- /Camera_Space/image/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/image/8.png -------------------------------------------------------------------------------- /Interpolation/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Interpolation/image/1.png -------------------------------------------------------------------------------- /Shader/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Shader/code/opengl_math.h -------------------------------------------------------------------------------- /Spot_Lighting/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/image/1.png -------------------------------------------------------------------------------- /Spot_Lighting/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/image/2.png -------------------------------------------------------------------------------- /Spot_Lighting/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/image/3.png -------------------------------------------------------------------------------- /Texture/Texture/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/Texture/test.png -------------------------------------------------------------------------------- /Uniform/code/Uniform.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Uniform/code/Uniform.exe -------------------------------------------------------------------------------- /index/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/index/code/opengl_math.h -------------------------------------------------------------------------------- /Ambient_Lighting/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Ambient_Lighting/image/1.png -------------------------------------------------------------------------------- /Ambient_Lighting/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Ambient_Lighting/image/2.png -------------------------------------------------------------------------------- /Camera_Keyboard/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/image/1.png -------------------------------------------------------------------------------- /Camera_Keyboard/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/image/2.png -------------------------------------------------------------------------------- /Camera_Keyboard/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/image/3.png -------------------------------------------------------------------------------- /Camera_Keyboard/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/image/4.png -------------------------------------------------------------------------------- /Camera_Space/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/code/main.cpp -------------------------------------------------------------------------------- /Create_a_Window/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Create_a_Window/image/1.png -------------------------------------------------------------------------------- /Diffuse_Lighting/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/image/1.png -------------------------------------------------------------------------------- /Diffuse_Lighting/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/image/2.png -------------------------------------------------------------------------------- /Diffuse_Lighting/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/image/3.png -------------------------------------------------------------------------------- /Diffuse_Lighting/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/image/4.png -------------------------------------------------------------------------------- /First_Triangle/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/First_Triangle/image/1.png -------------------------------------------------------------------------------- /First_Triangle/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/First_Triangle/image/2.png -------------------------------------------------------------------------------- /Point_Lighting/code/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/code/test.png -------------------------------------------------------------------------------- /Point_Lighting/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/image/1.png -------------------------------------------------------------------------------- /Point_Lighting/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/image/2.png -------------------------------------------------------------------------------- /Point_Lighting/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/image/3.png -------------------------------------------------------------------------------- /Spot_Lighting/code/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/code/test.png -------------------------------------------------------------------------------- /Texture/Texture/Texture.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/Texture/Texture.exe -------------------------------------------------------------------------------- /Uniform/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Uniform/code/opengl_math.h -------------------------------------------------------------------------------- /hello_dot/code/hello_dot.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/hello_dot/code/hello_dot.exe -------------------------------------------------------------------------------- /hello_dot/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/hello_dot/code/opengl_math.h -------------------------------------------------------------------------------- /Camera_Keyboard/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/code/main.cpp -------------------------------------------------------------------------------- /Specular_lighting/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/image/1.png -------------------------------------------------------------------------------- /Specular_lighting/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/image/2.png -------------------------------------------------------------------------------- /Specular_lighting/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/image/3.png -------------------------------------------------------------------------------- /Specular_lighting/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/image/4.png -------------------------------------------------------------------------------- /Specular_lighting/image/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/image/5.png -------------------------------------------------------------------------------- /Specular_lighting/image/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/image/6.png -------------------------------------------------------------------------------- /Texture/Texture/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/Texture/opengl_math.h -------------------------------------------------------------------------------- /First_Triangle/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/First_Triangle/code/opengl_math.h -------------------------------------------------------------------------------- /Interpolation/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Interpolation/code/opengl_math.h -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/code/opengl_math.h -------------------------------------------------------------------------------- /Specular_lighting/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/code/main.cpp -------------------------------------------------------------------------------- /Specular_lighting/code/shader.fs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/code/shader.fs -------------------------------------------------------------------------------- /Specular_lighting/code/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/code/test.png -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/code/opengl_math.h -------------------------------------------------------------------------------- /Camera_Mouse/code/Camera_Mouse.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/code/Camera_Mouse.exe -------------------------------------------------------------------------------- /Camera_Space/code/Camera_Space.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/code/Camera_Space.exe -------------------------------------------------------------------------------- /Texture/Texture/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Texture/Texture/opengl_pipeline.cpp -------------------------------------------------------------------------------- /prespective_projection/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/image/1.png -------------------------------------------------------------------------------- /prespective_projection/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/image/2.png -------------------------------------------------------------------------------- /prespective_projection/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/image/3.png -------------------------------------------------------------------------------- /prespective_projection/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/image/4.png -------------------------------------------------------------------------------- /prespective_projection/image/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/image/5.png -------------------------------------------------------------------------------- /prespective_projection/image/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/image/6.png -------------------------------------------------------------------------------- /Camera_Mouse/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Mouse/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Camera_Space/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Space/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /First_Triangle/code/First_Triangle.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/First_Triangle/code/First_Triangle.exe -------------------------------------------------------------------------------- /Interpolation/code/Interpolation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Interpolation/code/Interpolation.exe -------------------------------------------------------------------------------- /Point_Lighting/code/Point_Lighting.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/code/Point_Lighting.exe -------------------------------------------------------------------------------- /Specular_lighting/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/code/opengl_math.h -------------------------------------------------------------------------------- /Spot_Lighting/code/Spot_Lighting.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/code/Spot_Lighting.exe -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Spot_Lighting/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Translation_Transformation/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/1.png -------------------------------------------------------------------------------- /Translation_Transformation/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/2.png -------------------------------------------------------------------------------- /Translation_Transformation/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/3.png -------------------------------------------------------------------------------- /Translation_Transformation/image/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/4.png -------------------------------------------------------------------------------- /Translation_Transformation/image/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/5.png -------------------------------------------------------------------------------- /Translation_Transformation/image/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/6.png -------------------------------------------------------------------------------- /Translation_Transformation/image/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/7.png -------------------------------------------------------------------------------- /Translation_Transformation/image/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/8.png -------------------------------------------------------------------------------- /Translation_Transformation/image/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/9.png -------------------------------------------------------------------------------- /Camera_Keyboard/code/Camera_Keyboard.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/code/Camera_Keyboard.exe -------------------------------------------------------------------------------- /Camera_Keyboard/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Camera_Keyboard/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Create_a_Window/code/Create_a_Window.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Create_a_Window/code/Create_a_Window.exe -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Point_Lighting/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Translation_Transformation/image/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/10.png -------------------------------------------------------------------------------- /Translation_Transformation/image/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/11.png -------------------------------------------------------------------------------- /Translation_Transformation/image/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/image/12.png -------------------------------------------------------------------------------- /Uniform/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 FragColor; 4 | 5 | void main() 6 | { 7 | FragColor = vec4(1.0,0.0,0.0,1.0); 8 | } 9 | -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Ambient_Lighting/Ambient_Lighting/test.png -------------------------------------------------------------------------------- /Concatenating_Transformations/code/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Concatenating_Transformations/code/main.cpp -------------------------------------------------------------------------------- /Concatenating_Transformations/image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Concatenating_Transformations/image/1.png -------------------------------------------------------------------------------- /Concatenating_Transformations/image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Concatenating_Transformations/image/2.png -------------------------------------------------------------------------------- /Concatenating_Transformations/image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Concatenating_Transformations/image/3.png -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/Diffuse_Lighting/main.cpp -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/Diffuse_Lighting/test.png -------------------------------------------------------------------------------- /Shader/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 FragColor; 4 | 5 | void main() 6 | { 7 | FragColor = vec4(1.0, 0.0, 0.0, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /Specular_lighting/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Specular_lighting/code/Specular_lighting.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Specular_lighting/code/Specular_lighting.exe -------------------------------------------------------------------------------- /index/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Ambient_Lighting/Ambient_Lighting/opengl_math.h -------------------------------------------------------------------------------- /Concatenating_Transformations/code/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Concatenating_Transformations/code/opengl_math.h -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/Diffuse_Lighting/opengl_math.h -------------------------------------------------------------------------------- /Interpolation/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor=Color; 10 | } -------------------------------------------------------------------------------- /prespective_projection/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/code/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Camera_Keyboard/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } -------------------------------------------------------------------------------- /Camera_Mouse/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } -------------------------------------------------------------------------------- /Camera_Space/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Ambient_Lighting/Ambient_Lighting/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/opengl_pipeline.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/Diffuse_Lighting/opengl_pipeline.cpp -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/Ambient_Lighting.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Ambient_Lighting/Ambient_Lighting/Ambient_Lighting.exe -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/Diffuse_Lighting.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Diffuse_Lighting/Diffuse_Lighting/Diffuse_Lighting.exe -------------------------------------------------------------------------------- /prespective_projection/code/prespective_projection.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/prespective_projection/code/prespective_projection.exe -------------------------------------------------------------------------------- /prespective_projection/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor = Color; 10 | } -------------------------------------------------------------------------------- /Concatenating_Transformations/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec4 Color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() 8 | { 9 | FragColor=Color; 10 | } -------------------------------------------------------------------------------- /Translation_Transformation/Translation_Transformation/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 FragColor; 4 | 5 | void main() 6 | { 7 | FragColor = vec4(1.0,0.0,0.0,1.0); 8 | } -------------------------------------------------------------------------------- /Translation_Transformation/Translation_Transformation/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/Translation_Transformation/main.cpp -------------------------------------------------------------------------------- /Concatenating_Transformations/code/Concatenating_Transformations.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Concatenating_Transformations/code/Concatenating_Transformations.exe -------------------------------------------------------------------------------- /Shader/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | void main() 6 | { 7 | gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0); 8 | } -------------------------------------------------------------------------------- /Translation_Transformation/Translation_Transformation/opengl_math.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/Translation_Transformation/opengl_math.h -------------------------------------------------------------------------------- /Texture/Texture/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec2 TexCoord0; 4 | 5 | out vec4 FragColor; 6 | 7 | uniform sampler2D gSampler; 8 | void main() 9 | { 10 | FragColor=texture2D(gSampler,TexCoord0.st); 11 | } -------------------------------------------------------------------------------- /Translation_Transformation/Translation_Transformation/Translation_Transformation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zach0zhang/OpenGL_Learning/HEAD/Translation_Transformation/Translation_Transformation/Translation_Transformation.exe -------------------------------------------------------------------------------- /Uniform/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout(location = 0) in vec3 Position; 4 | 5 | uniform float gScale; 6 | 7 | void main() 8 | { 9 | gl_Position = vec4(gScale * Position.x, gScale * Position.y, Position.z, 1.0); 10 | } 11 | -------------------------------------------------------------------------------- /index/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | uniform mat4 gWorld; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWorld * vec4(Position,1.0); 12 | Color = vec4(clamp(Position,0.0,1.0),1.0); 13 | } -------------------------------------------------------------------------------- /Camera_Mouse/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | uniform mat4 gWVP; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWVP * vec4(Position,1.0f); 12 | Color = vec4(clamp(Position,0.0,1.0),1.0); 13 | } -------------------------------------------------------------------------------- /Camera_Space/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | uniform mat4 gWVP; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWVP * vec4(Position,1.0); 12 | Color = vec4(clamp(Position,0.0,1.0),1.0); 13 | } -------------------------------------------------------------------------------- /Camera_Keyboard/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | uniform mat4 gWVP; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWVP * vec4(Position,1.0); 12 | Color = vec4(clamp(Position,0.0,1.0),1.0); 13 | } -------------------------------------------------------------------------------- /Interpolation/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | 5 | uniform mat4 gWorld; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWorld * vec4(Position,1.0); 12 | 13 | Color=vec4(clamp(Position,0.0,1.0),1.0); 14 | } -------------------------------------------------------------------------------- /prespective_projection/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | uniform mat4 gWorld; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWorld * vec4(Position,1.0); 12 | Color = vec4(clamp(Position,0.0,1.0),1.0); 13 | } -------------------------------------------------------------------------------- /Concatenating_Transformations/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | 5 | uniform mat4 gWorld; 6 | 7 | out vec4 Color; 8 | 9 | void main() 10 | { 11 | gl_Position = gWorld*vec4(Position, 1.0); 12 | Color = vec4(clamp(Position, 0.0,1.0),1.0); 13 | } -------------------------------------------------------------------------------- /Texture/Texture/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location = 0) in vec3 Position; 4 | layout (location = 1) in vec2 TexCoord; 5 | 6 | uniform mat4 gWVP; 7 | 8 | out vec2 TexCoord0; 9 | 10 | void main() 11 | { 12 | gl_Position= gWVP * vec4(Position,1.0); 13 | TexCoord0=TexCoord; 14 | } -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | layout (location=1) in vec2 TexCoord; 5 | 6 | uniform mat4 gWVP; 7 | 8 | out vec2 TexCoord0; 9 | 10 | void main() 11 | { 12 | gl_Position = gWVP * vec4(Position,1.0); 13 | TexCoord0=TexCoord; 14 | } -------------------------------------------------------------------------------- /Translation_Transformation/Translation_Transformation/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | 5 | uniform mat4 gWorld1; 6 | uniform mat4 gWorld2; 7 | uniform mat4 gWorld3; 8 | 9 | 10 | void main() 11 | { 12 | gl_Position = gWorld1*gWorld2*gWorld3 * vec4(Position,1.0); 13 | } -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | layout (location=1) in vec2 TexCoord; 5 | layout (location=2) in vec3 Normal; 6 | 7 | uniform mat4 gWVP; 8 | uniform mat4 gWorld; 9 | 10 | out vec2 TexCoord0; 11 | out vec3 Normal0; 12 | 13 | void main() 14 | { 15 | gl_Position = gWVP * vec4(Position,1.0); 16 | TexCoord0=TexCoord; 17 | Normal0 = (gWorld * vec4(Normal,0.0)).xyz; 18 | } -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec2 TexCoord0; 4 | 5 | out vec4 FragColor; 6 | 7 | struct DirectionalLight 8 | { 9 | vec3 Color; 10 | float AmbientIntensity; 11 | }; 12 | 13 | uniform DirectionalLight gDirectionalLight; 14 | uniform sampler2D gSampler; 15 | 16 | void main() 17 | { 18 | FragColor = texture2D(gSampler,TexCoord0)*vec4(gDirectionalLight.Color,1.0f)*gDirectionalLight.AmbientIntensity; 19 | } -------------------------------------------------------------------------------- /Point_Lighting/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | layout (location=1) in vec2 TexCoord; 5 | layout (location=2) in vec3 Normal; 6 | 7 | uniform mat4 gWVP; 8 | uniform mat4 gWorld; 9 | 10 | out vec2 TexCoord0; 11 | out vec3 Normal0; 12 | out vec3 WorldPos0; 13 | 14 | void main() 15 | { 16 | gl_Position = gWVP*vec4(Position,1.0); 17 | TexCoord0 = TexCoord; 18 | Normal0= (gWorld * vec4(Normal,0.0)).xyz; 19 | WorldPos0 = (gWorld * vec4(Position, 1.0)).xyz; 20 | } -------------------------------------------------------------------------------- /Spot_Lighting/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | layout (location=1) in vec2 TexCoord; 5 | layout (location=2) in vec3 Normal; 6 | 7 | uniform mat4 gWVP; 8 | uniform mat4 gWorld; 9 | 10 | out vec2 TexCoord0; 11 | out vec3 Normal0; 12 | out vec3 WorldPos0; 13 | 14 | void main() 15 | { 16 | gl_Position = gWVP*vec4(Position,1.0); 17 | TexCoord0 = TexCoord; 18 | Normal0= (gWorld * vec4(Normal,0.0)).xyz; 19 | WorldPos0 = (gWorld * vec4(Position, 1.0)).xyz; 20 | } -------------------------------------------------------------------------------- /Specular_lighting/code/shader.vs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | layout (location=0) in vec3 Position; 4 | layout (location=1) in vec2 TexCoord; 5 | layout (location=2) in vec3 Normal; 6 | 7 | uniform mat4 gWVP; 8 | uniform mat4 gWorld; 9 | 10 | out vec2 TexCoord0; 11 | out vec3 Normal0; 12 | out vec3 WorldPos0; 13 | 14 | void main() 15 | { 16 | gl_Position = gWVP*vec4(Position,1.0); 17 | TexCoord0 = TexCoord; 18 | Normal0= (gWorld * vec4(Normal,0.0)).xyz; 19 | WorldPos0 = (gWorld * vec4(Position, 1.0)).xyz; 20 | } -------------------------------------------------------------------------------- /Create_a_Window/code/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void RenderSceneCB() { 4 | // 5 | glClear(GL_COLOR_BUFFER_BIT); 6 | 7 | glutSwapBuffers(); 8 | } 9 | 10 | int main(int argc, char **argv) 11 | { 12 | glutInit(&argc, argv); 13 | 14 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 15 | 16 | glutInitWindowSize(1024, 768); 17 | glutInitWindowPosition(10, 10); 18 | glutCreateWindow("Create a Window"); 19 | 20 | glutDisplayFunc(RenderSceneCB); 21 | 22 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 23 | 24 | glutMainLoop(); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Concatenating_Transformations/code/opengl_pipeline.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_pipeline.h" 2 | 3 | 4 | const Matrix44f * Pipeline::GetTrans() 5 | { 6 | Matrix44f ScaleTrans, RotateTrans, TranslationTrans, temp; 7 | 8 | ScaleMatrix44(ScaleTrans, m_scale[0], m_scale[1], m_scale[2]); 9 | RotationMatrix44(RotateTrans, m_rotateInfo[0], m_rotateInfo[1], m_rotateInfo[2]); 10 | TranslationMatrix44(TranslationTrans, m_worldPos[0], m_worldPos[1], m_worldPos[2]); 11 | 12 | MatrixMultiply44(temp, TranslationTrans, RotateTrans);//temp = TranslationTrans * RotateTrans 13 | MatrixMultiply44(m_transformation, temp, ScaleTrans); //m_transformation = temp * ScaleTrans 14 | return &m_transformation; 15 | } 16 | -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | in vec2 TexCoord0; 4 | in vec3 Normal0; 5 | 6 | out vec4 FragColor; 7 | 8 | struct DirectionalLight 9 | { 10 | vec3 Color; 11 | float AmbientIntensity; 12 | float DiffuseIntensity; 13 | vec3 Direction; 14 | }; 15 | 16 | uniform DirectionalLight gDirectionalLight; 17 | uniform sampler2D gSampler; 18 | 19 | void main() 20 | { 21 | vec4 AmbientColor = vec4(gDirectionalLight.Color,1.0f)*gDirectionalLight.AmbientIntensity; 22 | 23 | float DiffuseFactor = dot(normalize(Normal0),-gDirectionalLight.Direction); 24 | 25 | vec4 DiffuseColor; 26 | if(DiffuseFactor > 0){ 27 | DiffuseColor = vec4(gDirectionalLight.Color,1.0f) * gDirectionalLight.DiffuseIntensity * DiffuseFactor; 28 | } 29 | else{ 30 | DiffuseColor=vec4(0,0,0,0); 31 | } 32 | FragColor = texture2D(gSampler,TexCoord0)*(AmbientColor+DiffuseColor); 33 | } -------------------------------------------------------------------------------- /Concatenating_Transformations/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | 6 | class Pipeline 7 | { 8 | private: 9 | Vector3f m_scale; 10 | Vector3f m_worldPos; 11 | Vector3f m_rotateInfo; 12 | Matrix44f m_transformation; 13 | public: 14 | Pipeline() { 15 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 16 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 17 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 18 | } 19 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 20 | { 21 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 22 | } 23 | 24 | void WorldPos(float x, float y, float z) 25 | { 26 | LoadVector3(m_worldPos, x, y, z); 27 | } 28 | void Rotate(float RotateX, float RotateY, float RotateZ) 29 | { 30 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 31 | } 32 | 33 | const Matrix44f* GetTrans(); 34 | 35 | }; 36 | 37 | #endif -------------------------------------------------------------------------------- /Camera_Keyboard/code/opengl_camera.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_camera.h" 2 | #include 3 | #include 4 | #define StepSize 1 5 | bool Camera::OnKeyboard(int key) 6 | { 7 | bool Ret = false; 8 | 9 | switch (key) { 10 | case GLUT_KEY_UP: 11 | { 12 | for (int i = 0; i < 3; i++) 13 | m_pos[i] += m_target[i] * StepSize; 14 | Ret = TRUE; 15 | } 16 | break; 17 | case GLUT_KEY_DOWN: 18 | { 19 | for (int i = 0; i < 3; i++) 20 | m_pos[i] -= m_target[i] * StepSize; 21 | Ret = TRUE; 22 | } 23 | break; 24 | case GLUT_KEY_LEFT: 25 | { 26 | Vector3f Left; 27 | CrossProduct3(Left, m_target, m_up); 28 | NormalizeVector3(Left); 29 | for (int i = 0; i < 3; i++) 30 | m_pos[i] += Left[i] * StepSize; 31 | Ret = TRUE; 32 | } 33 | break; 34 | case GLUT_KEY_RIGHT: 35 | { 36 | Vector3f Right; 37 | CrossProduct3(Right, m_up,m_target); 38 | NormalizeVector3(Right); 39 | for (int i = 0; i < 3; i++) 40 | m_pos[i] += Right[i] * StepSize; 41 | Ret = TRUE; 42 | } 43 | break; 44 | } 45 | return Ret; 46 | } -------------------------------------------------------------------------------- /Camera_Keyboard/code/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | class Camera 5 | { 6 | private: 7 | Vector3f m_pos; 8 | Vector3f m_target; 9 | Vector3f m_up; 10 | 11 | int m_windowWidth; 12 | int m_windowHeight; 13 | public: 14 | Camera(int Window_Width, int Window_Height) 15 | { 16 | m_windowWidth = Window_Width; 17 | m_windowHeight = Window_Height; 18 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 19 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 20 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 21 | } 22 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 23 | { 24 | m_windowWidth = Window_Width; 25 | m_windowHeight = Window_Height; 26 | CopyVector3(m_pos, Pos); 27 | CopyVector3(m_target, Target); 28 | CopyVector3(m_up, Up); 29 | } 30 | bool OnKeyboard(int key); 31 | 32 | const Vector3f& GetPos() 33 | { 34 | return m_pos; 35 | } 36 | const Vector3f& GetTarget() 37 | { 38 | return m_target; 39 | } 40 | const Vector3f& GetUp() 41 | { 42 | return m_up; 43 | } 44 | }; 45 | 46 | #endif -------------------------------------------------------------------------------- /Texture/Texture/opengl_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_TEXTURE_H 2 | #define __OPENGL_TEXTURE_H 3 | #define STB_IMAGE_IMPLEMENTATION 4 | #include "stb_image.h" 5 | 6 | class Texture 7 | { 8 | public: 9 | Texture(GLenum TextureTarget, const std::string& FileName) 10 | { 11 | m_textureTarget = TextureTarget; 12 | m_fileName = FileName; 13 | } 14 | bool Load() 15 | { 16 | int width, height, nrChannels; 17 | unsigned char *data = stbi_load(m_fileName.c_str(),&width, &height, &nrChannels, 0); 18 | glGenTextures(1, &m_textureObj); 19 | glBindTexture(m_textureTarget, m_textureObj); 20 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 21 | glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 22 | glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 23 | glBindTexture(m_textureTarget, 0); 24 | 25 | return true; 26 | } 27 | void Bind(GLenum TextureUnit) 28 | { 29 | glActiveTexture(TextureUnit); 30 | glBindTexture(m_textureTarget, m_textureObj); 31 | } 32 | private: 33 | std::string m_fileName; 34 | GLenum m_textureTarget; 35 | GLuint m_textureObj; 36 | }; 37 | #endif -------------------------------------------------------------------------------- /prespective_projection/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | 6 | class Pipeline 7 | { 8 | private: 9 | Vector3f m_scale; 10 | Vector3f m_worldPos; 11 | Vector3f m_rotateInfo; 12 | 13 | Matrix44f m_Wtransformation; 14 | Matrix44f m_ProjTransformation; 15 | Matrix44f m_WPtransformation; 16 | 17 | PersProjInfo m_persProjInfo; 18 | public: 19 | Pipeline() { 20 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 21 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 22 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 23 | } 24 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 25 | { 26 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 27 | } 28 | 29 | void WorldPos(float x, float y, float z) 30 | { 31 | LoadVector3(m_worldPos, x, y, z); 32 | } 33 | void Rotate(float RotateX, float RotateY, float RotateZ) 34 | { 35 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 36 | } 37 | void SetPerspectiveProj(const PersProjInfo& p) 38 | { 39 | m_persProjInfo = p; 40 | } 41 | 42 | const Matrix44f* GetWorldTrans(); 43 | const Matrix44f* GetProjTrans(); 44 | const Matrix44f* GetWPTrans(); 45 | 46 | }; 47 | 48 | #endif -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_TEXTURE_H 2 | #define __OPENGL_TEXTURE_H 3 | #define STB_IMAGE_IMPLEMENTATION 4 | #include 5 | #include 6 | #include "stb_image.h" 7 | 8 | class Texture 9 | { 10 | public: 11 | Texture(GLenum TextureTarget, const std::string& FileName) 12 | { 13 | m_textureTarget = TextureTarget; 14 | m_fileName = FileName; 15 | } 16 | 17 | bool Load() 18 | { 19 | int width, height, nrChannels; 20 | unsigned char *data = stbi_load(m_fileName.c_str(),&width, &height, &nrChannels, 0); 21 | 22 | glGenTextures(1, &m_textureObj); 23 | glBindTexture(m_textureTarget, m_textureObj); 24 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 25 | glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 26 | glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 27 | glBindTexture(m_textureTarget, 0); 28 | 29 | return true; 30 | } 31 | 32 | void Bind(GLenum TextureUnit) 33 | { 34 | glActiveTexture(TextureUnit); 35 | glBindTexture(m_textureTarget, m_textureObj); 36 | } 37 | 38 | private: 39 | std::string m_fileName; 40 | GLenum m_textureTarget; 41 | GLuint m_textureObj; 42 | 43 | 44 | }; 45 | 46 | 47 | 48 | #endif -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_TEXTURE_H 2 | #define __OPENGL_TEXTURE_H 3 | #define STB_IMAGE_IMPLEMENTATION 4 | #include 5 | #include 6 | #include "stb_image.h" 7 | 8 | class Texture 9 | { 10 | public: 11 | Texture(GLenum TextureTarget, const std::string& FileName) 12 | { 13 | m_textureTarget = TextureTarget; 14 | m_fileName = FileName; 15 | } 16 | 17 | bool Load() 18 | { 19 | int width, height, nrChannels; 20 | unsigned char *data = stbi_load(m_fileName.c_str(),&width, &height, &nrChannels, 0); 21 | 22 | glGenTextures(1, &m_textureObj); 23 | glBindTexture(m_textureTarget, m_textureObj); 24 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 25 | glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 26 | glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 27 | glBindTexture(m_textureTarget, 0); 28 | 29 | return true; 30 | } 31 | 32 | void Bind(GLenum TextureUnit) 33 | { 34 | glActiveTexture(TextureUnit); 35 | glBindTexture(m_textureTarget, m_textureObj); 36 | } 37 | 38 | private: 39 | std::string m_fileName; 40 | GLenum m_textureTarget; 41 | GLuint m_textureObj; 42 | 43 | 44 | }; 45 | 46 | 47 | 48 | #endif -------------------------------------------------------------------------------- /Specular_lighting/code/opengl_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_TEXTURE_H 2 | #define __OPENGL_TEXTURE_H 3 | #define STB_IMAGE_IMPLEMENTATION 4 | #include 5 | #include 6 | #include "stb_image.h" 7 | 8 | class Texture 9 | { 10 | public: 11 | Texture(GLenum TextureTarget, const std::string& FileName) 12 | { 13 | m_textureTarget = TextureTarget; 14 | m_fileName = FileName; 15 | } 16 | 17 | bool Load() 18 | { 19 | int width, height, nrChannels; 20 | unsigned char *data = stbi_load(m_fileName.c_str(),&width, &height, &nrChannels, 0); 21 | 22 | glGenTextures(1, &m_textureObj); 23 | glBindTexture(m_textureTarget, m_textureObj); 24 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 25 | glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 26 | glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 27 | glBindTexture(m_textureTarget, 0); 28 | 29 | return true; 30 | } 31 | 32 | void Bind(GLenum TextureUnit) 33 | { 34 | glActiveTexture(TextureUnit); 35 | glBindTexture(m_textureTarget, m_textureObj); 36 | } 37 | 38 | private: 39 | std::string m_fileName; 40 | GLenum m_textureTarget; 41 | GLuint m_textureObj; 42 | 43 | 44 | }; 45 | 46 | 47 | 48 | #endif -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/opengl_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_TEXTURE_H 2 | #define __OPENGL_TEXTURE_H 3 | #define STB_IMAGE_IMPLEMENTATION 4 | #include 5 | #include 6 | #include "stb_image.h" 7 | 8 | class Texture 9 | { 10 | public: 11 | Texture(GLenum TextureTarget, const std::string& FileName) 12 | { 13 | m_textureTarget = TextureTarget; 14 | m_fileName = FileName; 15 | } 16 | 17 | bool Load() 18 | { 19 | int width, height, nrChannels; 20 | unsigned char *data = stbi_load(m_fileName.c_str(),&width, &height, &nrChannels, 0); 21 | 22 | glGenTextures(1, &m_textureObj); 23 | glBindTexture(m_textureTarget, m_textureObj); 24 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 25 | glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 26 | glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 27 | glBindTexture(m_textureTarget, 0); 28 | 29 | return true; 30 | } 31 | 32 | void Bind(GLenum TextureUnit) 33 | { 34 | glActiveTexture(TextureUnit); 35 | glBindTexture(m_textureTarget, m_textureObj); 36 | } 37 | 38 | private: 39 | std::string m_fileName; 40 | GLenum m_textureTarget; 41 | GLuint m_textureObj; 42 | 43 | 44 | }; 45 | 46 | 47 | 48 | #endif -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/opengl_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_TEXTURE_H 2 | #define __OPENGL_TEXTURE_H 3 | #define STB_IMAGE_IMPLEMENTATION 4 | #include 5 | #include 6 | #include "stb_image.h" 7 | 8 | class Texture 9 | { 10 | public: 11 | Texture(GLenum TextureTarget, const std::string& FileName) 12 | { 13 | m_textureTarget = TextureTarget; 14 | m_fileName = FileName; 15 | } 16 | 17 | bool Load() 18 | { 19 | int width, height, nrChannels; 20 | unsigned char *data = stbi_load(m_fileName.c_str(),&width, &height, &nrChannels, 0); 21 | 22 | glGenTextures(1, &m_textureObj); 23 | glBindTexture(m_textureTarget, m_textureObj); 24 | glTexImage2D(m_textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 25 | glTexParameterf(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 26 | glTexParameterf(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 27 | glBindTexture(m_textureTarget, 0); 28 | 29 | return true; 30 | } 31 | 32 | void Bind(GLenum TextureUnit) 33 | { 34 | glActiveTexture(TextureUnit); 35 | glBindTexture(m_textureTarget, m_textureObj); 36 | } 37 | 38 | private: 39 | std::string m_fileName; 40 | GLenum m_textureTarget; 41 | GLuint m_textureObj; 42 | 43 | 44 | }; 45 | 46 | 47 | 48 | #endif -------------------------------------------------------------------------------- /First_Triangle/code/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "opengl_math.h" 5 | 6 | GLuint VBO; 7 | 8 | static void Render() 9 | { 10 | glClear(GL_COLOR_BUFFER_BIT); 11 | 12 | glEnableVertexAttribArray(0); 13 | 14 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 15 | 16 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 17 | 18 | glDrawArrays(GL_TRIANGLES, 0, 3); 19 | 20 | glDisableVertexAttribArray(0); 21 | 22 | glutSwapBuffers(); 23 | } 24 | 25 | static void CreateVertexBuffer() 26 | { 27 | Vector3f Vertices[3]; 28 | 29 | LoadVector3(Vertices[0], -0.5f, -0.5f, 0.0f); 30 | LoadVector3(Vertices[1], 0.5f, -0.5f, 0.0f); 31 | LoadVector3(Vertices[2], 0.0f, 0.5f, 0.0f); 32 | 33 | glGenBuffers(1, &VBO); 34 | 35 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 36 | 37 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 38 | } 39 | int main(int argc, char ** argv) { 40 | 41 | glutInit(&argc, argv); 42 | 43 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 44 | 45 | glutInitWindowSize(480, 320); 46 | glutInitWindowPosition(100, 100); 47 | glutCreateWindow("First Triangle"); 48 | 49 | glutDisplayFunc(Render); 50 | 51 | GLenum res = glewInit(); 52 | if (res != GLEW_OK) { 53 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 54 | return 1; 55 | } 56 | 57 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 58 | 59 | CreateVertexBuffer(); 60 | 61 | glutMainLoop(); 62 | 63 | return 0; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Camera_Mouse/code/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | class Camera 7 | { 8 | private: 9 | Vector3f m_pos; 10 | Vector3f m_target; 11 | Vector3f m_up; 12 | 13 | float m_AngleH; 14 | float m_AngleV; 15 | 16 | bool m_OnUpperEdge; 17 | bool m_OnLowerEdge; 18 | bool m_OnLeftEdge; 19 | bool m_OnRightEdge; 20 | 21 | Vector2i m_mousePos; 22 | 23 | int m_windowWidth; 24 | int m_windowHeight; 25 | public: 26 | Camera(int Window_Width, int Window_Height) 27 | { 28 | m_windowWidth = Window_Width; 29 | m_windowHeight = Window_Height; 30 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 31 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 32 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 33 | } 34 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 35 | { 36 | m_windowWidth = Window_Width; 37 | m_windowHeight = Window_Height; 38 | CopyVector3(m_pos, Pos); 39 | CopyVector3(m_target, Target); 40 | NormalizeVector3(m_target); 41 | CopyVector3(m_up, Up); 42 | NormalizeVector3(m_up); 43 | 44 | Init(); 45 | } 46 | void Init(); 47 | bool OnKeyboard(int key); 48 | void OnMouse(int x, int y); 49 | void Update(); 50 | void OnRender(); 51 | const Vector3f& GetPos() 52 | { 53 | return m_pos; 54 | } 55 | const Vector3f& GetTarget() 56 | { 57 | return m_target; 58 | } 59 | const Vector3f& GetUp() 60 | { 61 | return m_up; 62 | } 63 | }; 64 | 65 | #endif -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | class Camera 7 | { 8 | private: 9 | Vector3f m_pos; 10 | Vector3f m_target; 11 | Vector3f m_up; 12 | 13 | float m_AngleH; 14 | float m_AngleV; 15 | 16 | bool m_OnUpperEdge; 17 | bool m_OnLowerEdge; 18 | bool m_OnLeftEdge; 19 | bool m_OnRightEdge; 20 | 21 | Vector2i m_mousePos; 22 | 23 | int m_windowWidth; 24 | int m_windowHeight; 25 | public: 26 | Camera(int Window_Width, int Window_Height) 27 | { 28 | m_windowWidth = Window_Width; 29 | m_windowHeight = Window_Height; 30 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 31 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 32 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 33 | } 34 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 35 | { 36 | m_windowWidth = Window_Width; 37 | m_windowHeight = Window_Height; 38 | CopyVector3(m_pos, Pos); 39 | CopyVector3(m_target, Target); 40 | NormalizeVector3(m_target); 41 | CopyVector3(m_up, Up); 42 | NormalizeVector3(m_up); 43 | 44 | Init(); 45 | } 46 | void Init(); 47 | bool OnKeyboard(int key); 48 | void OnMouse(int x, int y); 49 | void Update(); 50 | void OnRender(); 51 | const Vector3f& GetPos() 52 | { 53 | return m_pos; 54 | } 55 | const Vector3f& GetTarget() 56 | { 57 | return m_target; 58 | } 59 | const Vector3f& GetUp() 60 | { 61 | return m_up; 62 | } 63 | }; 64 | 65 | #endif -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | class Camera 7 | { 8 | private: 9 | Vector3f m_pos; 10 | Vector3f m_target; 11 | Vector3f m_up; 12 | 13 | float m_AngleH; 14 | float m_AngleV; 15 | 16 | bool m_OnUpperEdge; 17 | bool m_OnLowerEdge; 18 | bool m_OnLeftEdge; 19 | bool m_OnRightEdge; 20 | 21 | Vector2i m_mousePos; 22 | 23 | int m_windowWidth; 24 | int m_windowHeight; 25 | public: 26 | Camera(int Window_Width, int Window_Height) 27 | { 28 | m_windowWidth = Window_Width; 29 | m_windowHeight = Window_Height; 30 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 31 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 32 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 33 | } 34 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 35 | { 36 | m_windowWidth = Window_Width; 37 | m_windowHeight = Window_Height; 38 | CopyVector3(m_pos, Pos); 39 | CopyVector3(m_target, Target); 40 | NormalizeVector3(m_target); 41 | CopyVector3(m_up, Up); 42 | NormalizeVector3(m_up); 43 | 44 | Init(); 45 | } 46 | void Init(); 47 | bool OnKeyboard(int key); 48 | void OnMouse(int x, int y); 49 | void Update(); 50 | void OnRender(); 51 | const Vector3f& GetPos() 52 | { 53 | return m_pos; 54 | } 55 | const Vector3f& GetTarget() 56 | { 57 | return m_target; 58 | } 59 | const Vector3f& GetUp() 60 | { 61 | return m_up; 62 | } 63 | }; 64 | 65 | #endif -------------------------------------------------------------------------------- /Texture/Texture/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | class Camera 7 | { 8 | private: 9 | Vector3f m_pos; 10 | Vector3f m_target; 11 | Vector3f m_up; 12 | 13 | float m_AngleH; 14 | float m_AngleV; 15 | 16 | bool m_OnUpperEdge; 17 | bool m_OnLowerEdge; 18 | bool m_OnLeftEdge; 19 | bool m_OnRightEdge; 20 | 21 | Vector2i m_mousePos; 22 | 23 | int m_windowWidth; 24 | int m_windowHeight; 25 | public: 26 | Camera(int Window_Width, int Window_Height) 27 | { 28 | m_windowWidth = Window_Width; 29 | m_windowHeight = Window_Height; 30 | LoadVector3(m_pos, 0.0f, 0.0f, 0.0f); 31 | LoadVector3(m_target, 0.0f, 0.0f, 1.0f); 32 | LoadVector3(m_target, 0.0f, 1.0f, 0.0f); 33 | } 34 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 35 | { 36 | m_windowWidth = Window_Width; 37 | m_windowHeight = Window_Height; 38 | CopyVector3(m_pos, Pos); 39 | CopyVector3(m_target, Target); 40 | NormalizeVector3(m_target); 41 | CopyVector3(m_up, Up); 42 | NormalizeVector3(m_up); 43 | 44 | Init(); 45 | } 46 | void Init(); 47 | bool OnKeyboard(int key); 48 | void OnMouse(int x, int y); 49 | void Update(); 50 | void OnRender(); 51 | const Vector3f& GetPos() 52 | { 53 | return m_pos; 54 | } 55 | const Vector3f& GetTarget() 56 | { 57 | return m_target; 58 | } 59 | const Vector3f& GetUp() 60 | { 61 | return m_up; 62 | } 63 | }; 64 | 65 | #endif -------------------------------------------------------------------------------- /Specular_lighting/code/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | class Camera 7 | { 8 | private: 9 | Vector3f m_pos; 10 | Vector3f m_target; 11 | Vector3f m_up; 12 | 13 | float m_AngleH; 14 | float m_AngleV; 15 | 16 | bool m_OnUpperEdge; 17 | bool m_OnLowerEdge; 18 | bool m_OnLeftEdge; 19 | bool m_OnRightEdge; 20 | 21 | Vector2i m_mousePos; 22 | 23 | int m_windowWidth; 24 | int m_windowHeight; 25 | public: 26 | Camera(int Window_Width, int Window_Height) 27 | { 28 | m_windowWidth = Window_Width; 29 | m_windowHeight = Window_Height; 30 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 31 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 32 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 33 | } 34 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 35 | { 36 | m_windowWidth = Window_Width; 37 | m_windowHeight = Window_Height; 38 | CopyVector3(m_pos, Pos); 39 | CopyVector3(m_target, Target); 40 | NormalizeVector3(m_target); 41 | CopyVector3(m_up, Up); 42 | NormalizeVector3(m_up); 43 | 44 | Init(); 45 | } 46 | void Init(); 47 | bool OnKeyboard(int key); 48 | void OnMouse(int x, int y); 49 | void Update(); 50 | void OnRender(); 51 | const Vector3f& GetPos() 52 | { 53 | return m_pos; 54 | } 55 | const Vector3f& GetTarget() 56 | { 57 | return m_target; 58 | } 59 | const Vector3f& GetUp() 60 | { 61 | return m_up; 62 | } 63 | }; 64 | 65 | #endif -------------------------------------------------------------------------------- /Camera_Space/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | 6 | class Pipeline 7 | { 8 | private: 9 | Vector3f m_scale; 10 | Vector3f m_worldPos; 11 | Vector3f m_rotateInfo; 12 | 13 | Matrix44f m_Wtransformation; 14 | Matrix44f m_ProjTransformation; 15 | Matrix44f m_Vtransformation; 16 | Matrix44f m_WPtransformation; 17 | Matrix44f m_VPtransformation; 18 | Matrix44f m_WVPtransformation; 19 | 20 | PersProjInfo m_persProjInfo; 21 | 22 | struct { 23 | Vector3f Pos; 24 | Vector3f Target; 25 | Vector3f Up; 26 | } m_camera; 27 | 28 | public: 29 | Pipeline() { 30 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 31 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 32 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 33 | } 34 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 35 | { 36 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 37 | } 38 | 39 | void WorldPos(float x, float y, float z) 40 | { 41 | LoadVector3(m_worldPos, x, y, z); 42 | } 43 | void Rotate(float RotateX, float RotateY, float RotateZ) 44 | { 45 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 46 | } 47 | void SetPerspectiveProj(const PersProjInfo& p) 48 | { 49 | m_persProjInfo = p; 50 | } 51 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 52 | { 53 | for (unsigned int i = 0; i < 3; i++) 54 | { 55 | m_camera.Pos[i] = Pos[i]; 56 | m_camera.Target[i] = Target[i]; 57 | m_camera.Up[i] = Up[i]; 58 | } 59 | } 60 | 61 | const Matrix44f* GetWorldTrans(); 62 | const Matrix44f* GetProjTrans(); 63 | const Matrix44f* GetViewTrans(); 64 | const Matrix44f* GetWPTrans(); 65 | const Matrix44f* GetVPTrans(); 66 | const Matrix44f* GetWVPTrans(); 67 | 68 | }; 69 | 70 | #endif -------------------------------------------------------------------------------- /Camera_Mouse/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | CopyVector3(m_camera.Pos, Pos); 55 | CopyVector3(m_camera.Target, Target); 56 | CopyVector3(m_camera.Up, Up); 57 | } 58 | void SetCamera(Camera& Camera) 59 | { 60 | CopyVector3(m_camera.Pos, Camera.GetPos()); 61 | CopyVector3(m_camera.Target, Camera.GetTarget()); 62 | CopyVector3(m_camera.Up, Camera.GetUp()); 63 | } 64 | 65 | const Matrix44f* GetWorldTrans(); 66 | const Matrix44f* GetProjTrans(); 67 | const Matrix44f* GetViewTrans(); 68 | const Matrix44f* GetWPTrans(); 69 | const Matrix44f* GetVPTrans(); 70 | const Matrix44f* GetWVPTrans(); 71 | 72 | }; 73 | 74 | #endif -------------------------------------------------------------------------------- /Camera_Keyboard/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | CopyVector3(m_camera.Pos, Pos); 55 | CopyVector3(m_camera.Target, Target); 56 | CopyVector3(m_camera.Up, Up); 57 | } 58 | void SetCamera(Camera& Camera) 59 | { 60 | CopyVector3(m_camera.Pos, Camera.GetPos()); 61 | CopyVector3(m_camera.Target, Camera.GetTarget()); 62 | CopyVector3(m_camera.Up, Camera.GetUp()); 63 | } 64 | 65 | const Matrix44f* GetWorldTrans(); 66 | const Matrix44f* GetProjTrans(); 67 | const Matrix44f* GetViewTrans(); 68 | const Matrix44f* GetWPTrans(); 69 | const Matrix44f* GetVPTrans(); 70 | const Matrix44f* GetWVPTrans(); 71 | 72 | }; 73 | 74 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenGL_Learning 2 | 通过学习OpenGL教程——ogldev,自己实现数学库,一步步学习OpenGL 3 | 4 | [原教程链接——ogldev](http://ogldev.atspace.co.uk/index.html) 5 | 6 | 每一子文件夹对应一小节,并且子文件夹中有markdown的学习笔记 7 | 8 | - [1. 打开一个窗口](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Create_a_Window) 9 | - [2. 画一个点](https://github.com/zach0zhang/OpenGL_Learning/tree/master/hello_dot) 10 | - [3. 画一个三角形](https://github.com/zach0zhang/OpenGL_Learning/tree/master/First_Triangle) 11 | - [4. 使用着色器(shader)](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Shader) 12 | - [5. 使用一致变量(Uniform Variables)](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Uniform) 13 | - [6. 平移,旋转和缩放变换](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Translation_Transformation) 14 | - [7. 插值](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Interpolation) 15 | - [8. 索引绘制](https://github.com/zach0zhang/OpenGL_Learning/tree/master/index) 16 | - [9. 混合变换](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Concatenating_Transformations) 17 | - [10. 透视投影](https://github.com/zach0zhang/OpenGL_Learning/tree/master/prespective_projection) 18 | - [11. 相机空间](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Camera_Space) 19 | - [12. 相机控制(键盘操作)](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Camera_Keyboard) 20 | - [13. 相机控制(鼠标操作)](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Camera_Mouse) 21 | - [14. 纹理映射(Texture Mapping)](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Texture) 22 | - [15. 环境光](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Ambient_Lighting) 23 | - [16. 漫射光](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Diffuse_Lighting) 24 | - [17. 镜面反射光](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Specular_lighting) 25 | - [18. 点光源](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Point_Lighting) 26 | - [19. 聚光灯光源](https://github.com/zach0zhang/OpenGL_Learning/tree/master/Spot_Lighting) 27 | 28 | -------------------------------------------------------------------------------- /Texture/Texture/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | /* 55 | for (unsigned int i = 0; i < 3; i++) 56 | { 57 | m_camera.Pos[i] = Pos[i]; 58 | m_camera.Target[i] = Target[i]; 59 | m_camera.Up[i] = Up[i]; 60 | } 61 | */ 62 | CopyVector3(m_camera.Pos, Pos); 63 | CopyVector3(m_camera.Target, Target); 64 | CopyVector3(m_camera.Up, Up); 65 | } 66 | void SetCamera(Camera& Camera) 67 | { 68 | CopyVector3(m_camera.Pos, Camera.GetPos()); 69 | CopyVector3(m_camera.Target, Camera.GetTarget()); 70 | CopyVector3(m_camera.Up, Camera.GetUp()); 71 | } 72 | 73 | const Matrix44f* GetWorldTrans(); 74 | const Matrix44f* GetProjTrans(); 75 | const Matrix44f* GetViewTrans(); 76 | const Matrix44f* GetWPTrans(); 77 | const Matrix44f* GetVPTrans(); 78 | const Matrix44f* GetWVPTrans(); 79 | 80 | }; 81 | 82 | #endif -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | /* 55 | for (unsigned int i = 0; i < 3; i++) 56 | { 57 | m_camera.Pos[i] = Pos[i]; 58 | m_camera.Target[i] = Target[i]; 59 | m_camera.Up[i] = Up[i]; 60 | } 61 | */ 62 | CopyVector3(m_camera.Pos, Pos); 63 | CopyVector3(m_camera.Target, Target); 64 | CopyVector3(m_camera.Up, Up); 65 | } 66 | void SetCamera(Camera& Camera) 67 | { 68 | CopyVector3(m_camera.Pos, Camera.GetPos()); 69 | CopyVector3(m_camera.Target, Camera.GetTarget()); 70 | CopyVector3(m_camera.Up, Camera.GetUp()); 71 | } 72 | 73 | const Matrix44f* GetWorldTrans(); 74 | const Matrix44f* GetProjTrans(); 75 | const Matrix44f* GetViewTrans(); 76 | const Matrix44f* GetWPTrans(); 77 | const Matrix44f* GetVPTrans(); 78 | const Matrix44f* GetWVPTrans(); 79 | 80 | }; 81 | 82 | #endif -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | /* 55 | for (unsigned int i = 0; i < 3; i++) 56 | { 57 | m_camera.Pos[i] = Pos[i]; 58 | m_camera.Target[i] = Target[i]; 59 | m_camera.Up[i] = Up[i]; 60 | } 61 | */ 62 | CopyVector3(m_camera.Pos, Pos); 63 | CopyVector3(m_camera.Target, Target); 64 | CopyVector3(m_camera.Up, Up); 65 | } 66 | void SetCamera(Camera& Camera) 67 | { 68 | CopyVector3(m_camera.Pos, Camera.GetPos()); 69 | CopyVector3(m_camera.Target, Camera.GetTarget()); 70 | CopyVector3(m_camera.Up, Camera.GetUp()); 71 | } 72 | 73 | const Matrix44f* GetWorldTrans(); 74 | const Matrix44f* GetProjTrans(); 75 | const Matrix44f* GetViewTrans(); 76 | const Matrix44f* GetWPTrans(); 77 | const Matrix44f* GetVPTrans(); 78 | const Matrix44f* GetWVPTrans(); 79 | 80 | }; 81 | 82 | #endif -------------------------------------------------------------------------------- /Create_a_Window/OpenGL学习之路1----打开一个窗口.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | 3 | OpenGL(全写Open Graphics Library)是指定义了一个跨编程语言、跨平台的编程接口规格的专业的图形程序接口。它用于三维图像(二维的亦可),是一个功能强大,调用方便的底层图形库。 4 | 5 | 先贴出源码: 6 | 7 | main.c: 8 | 9 | ``` 10 | #include 11 | 12 | void RenderSceneCB() { 13 | glClear(GL_COLOR_BUFFER_BIT); 14 | glutSwapBuffers(); 15 | } 16 | 17 | int main(int argc, char **argv) 18 | { 19 | glutInit(&argc, argv); 20 | 21 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 22 | 23 | glutInitWindowSize(1024, 768); 24 | glutInitWindowPosition(10, 10); 25 | glutCreateWindow("Create a Window"); 26 | 27 | glutDisplayFunc(RenderSceneCB); 28 | 29 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 30 | 31 | glutMainLoop(); 32 | 33 | return 0; 34 | } 35 | ``` 36 | 37 | 38 | ``` 39 | glutInit(&argc, argv); 40 | ``` 41 | 根据函数名就能看出来是初始化GLUT 42 | 43 | 44 | ``` 45 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 46 | ``` 47 | 设置显示模式 48 | 49 | GLUT_DOUBLE意思是两个缓冲区,显示一个缓冲区的时候,在另一个缓冲区绘制(两个缓冲区循环往复显示---绘制) 50 | 51 | GLUT_RGBA显示的颜色模式,RGBA代表Red(红色) Green(绿色) Blue(蓝色)和 Alpha(透明度) 52 | 53 | 54 | ``` 55 | glutInitWindowSize(1024, 768); 56 | glutInitWindowPosition(10, 10); 57 | glutCreateWindow("Create a Window"); 58 | ``` 59 | 设置窗口大小为1024*768,窗口位置以屏幕左上角为原点的(10,10)处,创建窗口并且命名为“Create a Window" 60 | 61 | ``` 62 | glutDisplayFunc(RenderSceneCB); 63 | ``` 64 | 设置显示回调函数为RenderSceneCB,因为程序跑在一个窗口系统里,所以大多数工作通过回调函数完成,这里的RenderSceneCB即是渲染需要的回调函数 65 | 66 | 67 | ``` 68 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 69 | ``` 70 | 设置好"清除颜色"的颜色,从左到右的参数分别是红、绿、蓝、透明度,(0.0f,0.0f,0.0f,0.0f)即为黑 71 | 72 | 73 | ``` 74 | glutMainLoop(); 75 | ``` 76 | 这个函数表明开始内部循环操作,即进入 侦听窗口事件发生---->调用回调函数处理,本程序只有显示回调函数(RenderSceneCB)不停回调执行 77 | 78 | ``` 79 | void RenderSceneCB() { 80 | glClear(GL_COLOR_BUFFER_BIT); 81 | glutSwapBuffers(); 82 | } 83 | ``` 84 | RenderSceneCB回调函数 85 | 86 | 首先使用之前glClearColor()设置好的清除颜色(GL_COLOR_BUFFER_BIT)清除当前窗口颜色, 87 | 88 | 再进行替换当前显示缓冲区,即和glutInitDisplayMode设置的GLUT_DOUBLE模式对应起来,循环显示两个缓冲区 89 | 90 | 91 | **运行结果:** 92 | 93 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Create_a_Window/image/1.png) 94 | -------------------------------------------------------------------------------- /Specular_lighting/code/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | /* 55 | for (unsigned int i = 0; i < 3; i++) 56 | { 57 | m_camera.Pos[i] = Pos[i]; 58 | m_camera.Target[i] = Target[i]; 59 | m_camera.Up[i] = Up[i]; 60 | } 61 | */ 62 | CopyVector3(m_camera.Pos, Pos); 63 | CopyVector3(m_camera.Target, Target); 64 | CopyVector3(m_camera.Up, Up); 65 | } 66 | void SetCamera(Camera& Camera) 67 | { 68 | CopyVector3(m_camera.Pos, Camera.GetPos()); 69 | CopyVector3(m_camera.Target, Camera.GetTarget()); 70 | CopyVector3(m_camera.Up, Camera.GetUp()); 71 | } 72 | 73 | const Matrix44f* GetWorldTrans(); 74 | const Matrix44f* GetProjTrans(); 75 | const Matrix44f* GetViewTrans(); 76 | const Matrix44f* GetWPTrans(); 77 | const Matrix44f* GetVPTrans(); 78 | const Matrix44f* GetWVPTrans(); 79 | 80 | }; 81 | 82 | #endif -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | /* 55 | for (unsigned int i = 0; i < 3; i++) 56 | { 57 | m_camera.Pos[i] = Pos[i]; 58 | m_camera.Target[i] = Target[i]; 59 | m_camera.Up[i] = Up[i]; 60 | } 61 | */ 62 | CopyVector3(m_camera.Pos, Pos); 63 | CopyVector3(m_camera.Target, Target); 64 | CopyVector3(m_camera.Up, Up); 65 | } 66 | void SetCamera(Camera& Camera) 67 | { 68 | CopyVector3(m_camera.Pos, Camera.GetPos()); 69 | CopyVector3(m_camera.Target, Camera.GetTarget()); 70 | CopyVector3(m_camera.Up, Camera.GetUp()); 71 | } 72 | 73 | const Matrix44f* GetWorldTrans(); 74 | const Matrix44f* GetProjTrans(); 75 | const Matrix44f* GetViewTrans(); 76 | const Matrix44f* GetWPTrans(); 77 | const Matrix44f* GetVPTrans(); 78 | const Matrix44f* GetWVPTrans(); 79 | 80 | }; 81 | 82 | #endif -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/opengl_pipeline.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_PIPELINE_H 2 | #define __OPENGL_PIPELINE_H 3 | 4 | #include "opengl_math.h" 5 | #include "opengl_camera.h" 6 | 7 | class Pipeline 8 | { 9 | private: 10 | Vector3f m_scale; 11 | Vector3f m_worldPos; 12 | Vector3f m_rotateInfo; 13 | 14 | Matrix44f m_Wtransformation; 15 | Matrix44f m_ProjTransformation; 16 | Matrix44f m_Vtransformation; 17 | Matrix44f m_WPtransformation; 18 | Matrix44f m_VPtransformation; 19 | Matrix44f m_WVPtransformation; 20 | 21 | PersProjInfo m_persProjInfo; 22 | 23 | struct { 24 | Vector3f Pos; 25 | Vector3f Target; 26 | Vector3f Up; 27 | } m_camera; 28 | 29 | public: 30 | Pipeline() { 31 | LoadVector3(m_scale,1.0f, 1.0f, 1.0f); 32 | LoadVector3(m_worldPos,0.0f, 0.0f, 0.0f); 33 | LoadVector3(m_rotateInfo,0.0f, 0.0f, 0.0f); 34 | } 35 | void Scale(float ScaleX, float ScaleY, float ScaleZ) 36 | { 37 | LoadVector3(m_scale, ScaleX, ScaleY, ScaleZ); 38 | } 39 | 40 | void WorldPos(float x, float y, float z) 41 | { 42 | LoadVector3(m_worldPos, x, y, z); 43 | } 44 | void Rotate(float RotateX, float RotateY, float RotateZ) 45 | { 46 | LoadVector3(m_rotateInfo, RotateX, RotateY, RotateZ); 47 | } 48 | void SetPerspectiveProj(const PersProjInfo& p) 49 | { 50 | m_persProjInfo = p; 51 | } 52 | void SetCamera(Vector3f Pos, Vector3f Target, Vector3f Up) 53 | { 54 | /* 55 | for (unsigned int i = 0; i < 3; i++) 56 | { 57 | m_camera.Pos[i] = Pos[i]; 58 | m_camera.Target[i] = Target[i]; 59 | m_camera.Up[i] = Up[i]; 60 | } 61 | */ 62 | CopyVector3(m_camera.Pos, Pos); 63 | CopyVector3(m_camera.Target, Target); 64 | CopyVector3(m_camera.Up, Up); 65 | } 66 | void SetCamera(Camera& Camera) 67 | { 68 | CopyVector3(m_camera.Pos, Camera.GetPos()); 69 | CopyVector3(m_camera.Target, Camera.GetTarget()); 70 | CopyVector3(m_camera.Up, Camera.GetUp()); 71 | } 72 | 73 | const Matrix44f* GetWorldTrans(); 74 | const Matrix44f* GetProjTrans(); 75 | const Matrix44f* GetViewTrans(); 76 | const Matrix44f* GetWPTrans(); 77 | const Matrix44f* GetVPTrans(); 78 | const Matrix44f* GetWVPTrans(); 79 | 80 | }; 81 | 82 | #endif -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_light.h: -------------------------------------------------------------------------------- 1 | #include "opengl_math.h" 2 | #include 3 | #include 4 | #include 5 | #define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0])) 6 | #define INVALID_UNIFORM_LOCATION 0xffffffff 7 | 8 | struct BaseLight 9 | { 10 | Vector3f Color; 11 | float AmbientIntensity; 12 | float DiffuseIntensity; 13 | 14 | BaseLight() 15 | { 16 | LoadVector3(Color,0.0f, 0.0f, 0.0f); 17 | AmbientIntensity = 0.0f; 18 | DiffuseIntensity = 0.0f; 19 | } 20 | }; 21 | 22 | struct DirectionalLight:public BaseLight 23 | { 24 | 25 | Vector3f Direction; 26 | 27 | DirectionalLight() 28 | { 29 | LoadVector3(Direction,0.0f, 0.0f, 0.0f); 30 | } 31 | }; 32 | 33 | struct PointLight : public BaseLight 34 | { 35 | Vector3f Position; 36 | 37 | struct 38 | { 39 | float Constant; 40 | float Linear; 41 | float Exp; 42 | } Attenuation; 43 | 44 | PointLight() 45 | { 46 | LoadVector3(Position, 0.0f, 0.0f, 0.0f); 47 | Attenuation.Constant = 1.0f; 48 | Attenuation.Linear = 0.0f; 49 | Attenuation.Exp = 0.0f; 50 | } 51 | }; 52 | 53 | class Light 54 | { 55 | public: 56 | Light(GLuint ShaderProgram) 57 | { 58 | m_shaderProg = ShaderProgram; 59 | } 60 | bool GetUniformLocations(); 61 | void SetDirectionalLight(const DirectionalLight& Light); 62 | void SetEyeWorldPos(const Vector3f& EyeWorldPos); 63 | void SetMatSpecularIntensity(float Intensity); 64 | void SetMatSpecularPower(float Power); 65 | void SetPointLights(unsigned int NumLights, const PointLight* pLights); 66 | 67 | private: 68 | static const unsigned int MAX_POINT_LIGHTS = 2; 69 | 70 | GLuint m_shaderProg; 71 | 72 | GLuint m_eyeWorldPosLocation; 73 | GLuint m_matSpecularIntensityLocation; 74 | GLuint m_matSpecularPowerLocation; 75 | GLuint m_numPointLightsLocation; 76 | 77 | struct { 78 | GLuint Color; 79 | GLuint AmbientIntensity; 80 | GLuint DiffuseIntensity; 81 | GLuint Direction; 82 | } m_dirLightLocation; 83 | 84 | struct { 85 | GLuint Color; 86 | GLuint AmbientIntensity; 87 | GLuint DiffuseIntensity; 88 | GLuint Position; 89 | struct 90 | { 91 | GLuint Constant; 92 | GLuint Linear; 93 | GLuint Exp; 94 | } Atten; 95 | } m_pointLightsLocation[MAX_POINT_LIGHTS]; 96 | }; -------------------------------------------------------------------------------- /Ambient_Lighting/OpenGL学习之路15----环境光.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | 3 | ## 环境光 4 | 光照在3D图形领域中有许多模型,常见的模型有:环境光、慢射光、镜面反射光、点光源等等,这次只对环境光进行学习和实现 5 | 6 | 环境光是一个没有光源、没有方向并且对场景中的所有物体产生相同的点亮效果的一种光 7 | 8 | 环境光可以理解成为世界的基本光,即我们这个三维空间的基本亮度。 9 | 10 | **环境光有两个属性:** 11 | - 颜色:即灯光的颜色,比如普通房子里白炽灯房子里就是白色的,特殊性场所红房子里粉红色调就是粉红的 12 | - 强度:即光强,强度越大亮度也就越大 13 | 14 | **环境光的计算:** 15 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Ambient_Lighting/image/1.png) 16 | 17 | 18 | **我们用一个结构体来包括表示环境光的两个属性:** 19 | ``` 20 | struct DirectionalLight 21 | { 22 | Vector3f Color; 23 | float AmbientIntensity; 24 | }; 25 | ``` 26 | - Color:是一个三维向量,三个分量分别代表r(红)、g(绿)、b(蓝)的比例,比如(1.0,0,0)就表示红色 27 | - AmbientIntensity:即光强,最大为1.0 28 | - 可以看到我们结构体的变量名是“平行光 DirectionalLight”,其实我们为的是实现一个平行光 29 | - 结构体在这次只实现环境光,所以结构体中只有环境光的两个属性 30 | 31 | 平行光是有特定方向但是没有特定的光源,就好比我们现实世界的太阳光。 32 | 33 | 太阳光照射到物体上,物体受到光线直直照射的地方光线强,而其它角度光线较弱,背面几乎照不到,这些概念属于漫反射范畴。 34 | 35 | 我们的环境光属于,太阳照到我们物体上,而照到其他物体上也会反射光照到我们物体的背面,而这个基本的光就是我们的环境光。 36 | 37 | ## 环境光的实现 38 | 首先,要在主程序里将环境光的参数数值传递到着色器内,我们使用Uniform变量来传递参数的值 39 | 40 | - 定义两个全局变量用于获得Uniform变量的index索引 main.cpp: 41 | ``` 42 | GLuint m_dirLightColorLocation, m_dirLightAmbientIntensityLocation; 43 | ``` 44 | - 获取Uniform变量的索引: 45 | 46 | ``` 47 | m_dirLightColorLocation = glGetUniformLocation(ShaderProgram,"gDirectionalLight.Color"); 48 | m_dirLightAmbientIntensityLocation = glGetUniformLocation(ShaderProgram,"gDirectionalLight.AmbientIntensity"); 49 | ``` 50 | - 声明一个环境光结构体变量,并赋值 51 | ``` 52 | struct DirectionalLight 53 | { 54 | Vector3f Color; 55 | float AmbientIntensity; 56 | }; 57 | struct DirectionalLight m_directionalLight; 58 | 59 | 60 | LoadVector3(m_directionalLight.Color,1.0f, 1.0f, 1.0f); 61 | m_directionalLight.AmbientIntensity = 0.1f; 62 | ``` 63 | - 在主渲染函数Render()中,逐渐增大光强的值并把环境光的参数传递到着色器中: 64 | 65 | ``` 66 | m_directionalLight.AmbientIntensity *= 1.0001f; 67 | glUniform3f(m_dirLightColorLocation, m_directionalLight.Color[0], m_directionalLight.Color[1], m_directionalLight.Color[2]); 68 | glUniform1f(m_dirLightAmbientIntensityLocation, m_directionalLight.AmbientIntensity); 69 | ``` 70 | - 片元着色器中获取Uniform变量的值,并通过之前提到过的环境光计算的公式计算最终的颜色,shader.fs: 71 | 72 | ``` 73 | #version 330 74 | 75 | in vec2 TexCoord0; 76 | 77 | out vec4 FragColor; 78 | 79 | struct DirectionalLight 80 | { 81 | vec3 Color; 82 | float AmbientIntensity; 83 | }; 84 | 85 | uniform DirectionalLight gDirectionalLight; 86 | uniform sampler2D gSampler; 87 | 88 | void main() 89 | { 90 | FragColor = texture2D(gSampler,TexCoord0)*vec4(gDirectionalLight.Color,1.0f)*gDirectionalLight.AmbientIntensity; 91 | } 92 | ``` 93 | ## 运行结果 94 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Ambient_Lighting/image/2.png) 95 | 可以看到四面体由暗到亮 96 | -------------------------------------------------------------------------------- /First_Triangle/OpenGL学习之路3----画一个三角形.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | 3 | ## 归一化坐标 4 | 我们在屏幕上绘制图形的时候,是在一个归一化的空间里,即为下图 5 | 6 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/First_Triangle/image/1.png) 7 | 8 | 可以看到绘制窗口左下角坐标为(-1.0,-1.0)到右上角坐标为(1.0,1.0) 9 | 10 | ## 代码解释 11 | opengl_math.h: 12 | 13 | ``` 14 | #ifndef __OPENGL_MATH_H 15 | #define __OPENGL_MATH_H 16 | 17 | //向量 18 | typedef float Vector3f[3]; 19 | 20 | //向量赋值 21 | inline void LoadVector3(Vector3f v, const float x, const float y, const float z) 22 | { 23 | v[0] = x; v[1] = y; v[2] = z; 24 | } 25 | 26 | #endif 27 | ``` 28 | 29 | ``` 30 | #include 31 | #include 32 | #include 33 | #include "opengl_math.h" 34 | 35 | GLuint VBO; 36 | 37 | static void Render() 38 | { 39 | glClear(GL_COLOR_BUFFER_BIT); 40 | 41 | glEnableVertexAttribArray(0); 42 | 43 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 44 | 45 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 46 | 47 | glDrawArrays(GL_TRIANGLES, 0, 3); 48 | 49 | glDisableVertexAttribArray(0); 50 | 51 | glutSwapBuffers(); 52 | } 53 | 54 | static void CreateVertexBuffer() 55 | { 56 | Vector3f Vertices[3]; 57 | 58 | LoadVector3(Vertices[0], -0.5f, -0.5f, 0.0f); 59 | LoadVector3(Vertices[1], 0.5f, -0.5f, 0.0f); 60 | LoadVector3(Vertices[2], 0.0f, 0.5f, 0.0f); 61 | 62 | glGenBuffers(1, &VBO); 63 | 64 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 65 | 66 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 67 | } 68 | int main(int argc, char ** argv) { 69 | 70 | glutInit(&argc, argv); 71 | 72 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 73 | 74 | glutInitWindowSize(480, 320); 75 | glutInitWindowPosition(100, 100); 76 | glutCreateWindow("First Triangle"); 77 | 78 | glutDisplayFunc(Render); 79 | 80 | GLenum res = glewInit(); 81 | if (res != GLEW_OK) { 82 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 83 | return 1; 84 | } 85 | 86 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 87 | 88 | CreateVertexBuffer(); 89 | 90 | glutMainLoop(); 91 | 92 | return 0; 93 | } 94 | 95 | 96 | ``` 97 | 这一节的代码跟上一节只有一点区别,这里只说区别。 98 | 99 | 100 | ``` 101 | Vector3f Vertices[3]; 102 | 103 | LoadVector3(Vertices[0], -0.5f, -0.5f, 0.0f); 104 | LoadVector3(Vertices[1], 0.5f, -0.5f, 0.0f); 105 | LoadVector3(Vertices[2], 0.0f, 0.5f, 0.0f); 106 | ``` 107 | 这里定义了包含三个顶点的数组,并且给分别赋了值 108 | 109 | ``` 110 | glDrawArrays(GL_TRIANGLES, 0, 3); 111 | ``` 112 | 绘制几何图形的第一个参数变成了绘制三角形,定点数变为了3个 113 | 114 | ## 运行结果: 115 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/First_Triangle/image/2.png) 116 | -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_light.h: -------------------------------------------------------------------------------- 1 | #include "opengl_math.h" 2 | #include 3 | #include 4 | #include 5 | #define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0])) 6 | #define INVALID_UNIFORM_LOCATION 0xffffffff 7 | 8 | struct BaseLight 9 | { 10 | Vector3f Color; 11 | float AmbientIntensity; 12 | float DiffuseIntensity; 13 | 14 | BaseLight() 15 | { 16 | LoadVector3(Color,0.0f, 0.0f, 0.0f); 17 | AmbientIntensity = 0.0f; 18 | DiffuseIntensity = 0.0f; 19 | } 20 | }; 21 | 22 | struct DirectionalLight:public BaseLight 23 | { 24 | 25 | Vector3f Direction; 26 | 27 | DirectionalLight() 28 | { 29 | LoadVector3(Direction,0.0f, 0.0f, 0.0f); 30 | } 31 | }; 32 | 33 | struct PointLight : public BaseLight 34 | { 35 | Vector3f Position; 36 | 37 | struct 38 | { 39 | float Constant; 40 | float Linear; 41 | float Exp; 42 | } Attenuation; 43 | 44 | PointLight() 45 | { 46 | LoadVector3(Position, 0.0f, 0.0f, 0.0f); 47 | Attenuation.Constant = 1.0f; 48 | Attenuation.Linear = 0.0f; 49 | Attenuation.Exp = 0.0f; 50 | } 51 | }; 52 | 53 | struct SpotLight : public PointLight 54 | { 55 | Vector3f Direction; 56 | float Cutoff; 57 | 58 | SpotLight() 59 | { 60 | LoadVector3(Direction,0.0f, 0.0f, 0.0f); 61 | Cutoff = 0.0f; 62 | } 63 | }; 64 | 65 | class Light 66 | { 67 | public: 68 | Light(GLuint ShaderProgram) 69 | { 70 | m_shaderProg = ShaderProgram; 71 | } 72 | bool GetUniformLocations(); 73 | void SetDirectionalLight(const DirectionalLight& Light); 74 | void SetEyeWorldPos(const Vector3f& EyeWorldPos); 75 | void SetMatSpecularIntensity(float Intensity); 76 | void SetMatSpecularPower(float Power); 77 | void SetPointLights(unsigned int NumLights, const PointLight* pLights); 78 | void SetSpotLights(unsigned int NumLights, const SpotLight* pLights); 79 | 80 | private: 81 | static const unsigned int MAX_POINT_LIGHTS = 2; 82 | static const unsigned int MAX_SPOT_LIGHTS = 2; 83 | 84 | GLuint m_shaderProg; 85 | 86 | GLuint m_eyeWorldPosLocation; 87 | GLuint m_matSpecularIntensityLocation; 88 | GLuint m_matSpecularPowerLocation; 89 | GLuint m_numPointLightsLocation; 90 | GLuint m_numSpotLightsLocation; 91 | 92 | struct { 93 | GLuint Color; 94 | GLuint AmbientIntensity; 95 | GLuint DiffuseIntensity; 96 | GLuint Direction; 97 | } m_dirLightLocation; 98 | 99 | struct { 100 | GLuint Color; 101 | GLuint AmbientIntensity; 102 | GLuint DiffuseIntensity; 103 | GLuint Position; 104 | struct 105 | { 106 | GLuint Constant; 107 | GLuint Linear; 108 | GLuint Exp; 109 | } Atten; 110 | } m_pointLightsLocation[MAX_POINT_LIGHTS]; 111 | 112 | struct { 113 | GLuint Color; 114 | GLuint AmbientIntensity; 115 | GLuint DiffuseIntensity; 116 | GLuint Position; 117 | GLuint Direction; 118 | GLuint Cutoff; 119 | struct { 120 | GLuint Constant; 121 | GLuint Linear; 122 | GLuint Exp; 123 | } Atten; 124 | } m_spotLightsLocation[MAX_SPOT_LIGHTS]; 125 | }; -------------------------------------------------------------------------------- /Camera_Mouse/code/opengl_camera.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_camera.h" 2 | #include 3 | #include 4 | #define StepSize 1 5 | const static float STEP_SCALE = 1.0f; 6 | const static float EDGE_STEP = 0.5f; 7 | const static int MARGIN = 10; 8 | bool Camera::OnKeyboard(int key) 9 | { 10 | bool Ret = false; 11 | 12 | switch (key) { 13 | case GLUT_KEY_UP: 14 | { 15 | for (int i = 0; i < 3; i++) 16 | m_pos[i] += m_target[i] * StepSize; 17 | Ret = TRUE; 18 | } 19 | break; 20 | case GLUT_KEY_DOWN: 21 | { 22 | for (int i = 0; i < 3; i++) 23 | m_pos[i] -= m_target[i] * StepSize; 24 | Ret = TRUE; 25 | } 26 | break; 27 | case GLUT_KEY_LEFT: 28 | { 29 | Vector3f Left; 30 | CrossProduct3(Left, m_target, m_up); 31 | NormalizeVector3(Left); 32 | for (int i = 0; i < 3; i++) 33 | m_pos[i] += Left[i] * StepSize; 34 | Ret = TRUE; 35 | } 36 | break; 37 | case GLUT_KEY_RIGHT: 38 | { 39 | Vector3f Right; 40 | CrossProduct3(Right, m_up, m_target); 41 | NormalizeVector3(Right); 42 | for (int i = 0; i < 3; i++) 43 | m_pos[i] += Right[i] * StepSize; 44 | Ret = TRUE; 45 | } 46 | break; 47 | } 48 | return Ret; 49 | } 50 | void Camera::Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH = RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH = 90.0f + RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | void Camera::OnMouse(int x, int y) 92 | { 93 | const int DeltaX = x - m_mousePos[0]; 94 | const int DeltaY = y - m_mousePos[1]; 95 | 96 | m_mousePos[0] = x; 97 | m_mousePos[1] = y; 98 | 99 | m_AngleH += (float)DeltaX / 20.0f; 100 | m_AngleV += (float)DeltaY / 20.0f; 101 | 102 | if (DeltaX == 0) { 103 | if (x <= MARGIN) { 104 | m_OnLeftEdge = true; 105 | } 106 | else if (x >= (m_windowWidth - MARGIN)) { 107 | m_OnRightEdge = true; 108 | } 109 | } 110 | else { 111 | m_OnLeftEdge = false; 112 | m_OnRightEdge = false; 113 | } 114 | if (DeltaY == 0) { 115 | if (y <= MARGIN) { 116 | m_OnUpperEdge = true; 117 | } 118 | else if (y >= (m_windowHeight - MARGIN)) { 119 | m_OnLowerEdge = true; 120 | } 121 | } 122 | else { 123 | m_OnUpperEdge = false; 124 | m_OnLowerEdge = false; 125 | } 126 | Update(); 127 | } 128 | void Camera::Update() 129 | { 130 | Vector3f Vaxis; 131 | LoadVector3(Vaxis, 0.0f, 1.0f, 0.0f); 132 | // Rotate the view vector by the horizontal angle around the vertical axis 133 | Vector3f View; 134 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 135 | RotateVector3(View, m_AngleH, Vaxis); 136 | NormalizeVector3(View); 137 | 138 | // Rotate the view vector by the vertical angle around the horizontal axis 139 | Vector3f Haxis; 140 | CrossProduct3(Haxis, Vaxis, View); 141 | NormalizeVector3(Haxis); 142 | 143 | RotateVector3(View, m_AngleV, Haxis); 144 | 145 | CopyVector3(m_target, View); 146 | NormalizeVector3(m_target); 147 | 148 | CrossProduct3(m_up, m_target, Haxis); 149 | NormalizeVector3(m_up); 150 | } 151 | void Camera::OnRender() 152 | { 153 | bool ShouldUpdate = false; 154 | 155 | if (m_OnLeftEdge) { 156 | m_AngleH -= EDGE_STEP; 157 | ShouldUpdate = true; 158 | } 159 | else if (m_OnRightEdge) { 160 | m_AngleH += EDGE_STEP; 161 | ShouldUpdate = true; 162 | } 163 | 164 | if (m_OnUpperEdge) { 165 | if (m_AngleV > -90.0f) { 166 | m_AngleV -= EDGE_STEP; 167 | ShouldUpdate = true; 168 | } 169 | } 170 | else if (m_OnLowerEdge) { 171 | if (m_AngleV < 90.0f) { 172 | m_AngleV += EDGE_STEP; 173 | ShouldUpdate = true; 174 | } 175 | } 176 | if (ShouldUpdate) { 177 | Update(); 178 | } 179 | } -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_camera.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_camera.h" 2 | #include 3 | #include 4 | #define StepSize 1 5 | const static float STEP_SCALE = 1.0f; 6 | const static float EDGE_STEP = 0.5f; 7 | const static int MARGIN = 10; 8 | bool Camera::OnKeyboard(int key) 9 | { 10 | bool Ret = false; 11 | 12 | switch (key) { 13 | case GLUT_KEY_UP: 14 | { 15 | for (int i = 0; i < 3; i++) 16 | m_pos[i] += m_target[i] * StepSize; 17 | Ret = TRUE; 18 | } 19 | break; 20 | case GLUT_KEY_DOWN: 21 | { 22 | for (int i = 0; i < 3; i++) 23 | m_pos[i] -= m_target[i] * StepSize; 24 | Ret = TRUE; 25 | } 26 | break; 27 | case GLUT_KEY_LEFT: 28 | { 29 | Vector3f Left; 30 | CrossProduct3(Left, m_target, m_up); 31 | NormalizeVector3(Left); 32 | for (int i = 0; i < 3; i++) 33 | m_pos[i] += Left[i] * StepSize; 34 | Ret = TRUE; 35 | } 36 | break; 37 | case GLUT_KEY_RIGHT: 38 | { 39 | Vector3f Right; 40 | CrossProduct3(Right, m_up, m_target); 41 | NormalizeVector3(Right); 42 | for (int i = 0; i < 3; i++) 43 | m_pos[i] += Right[i] * StepSize; 44 | Ret = TRUE; 45 | } 46 | break; 47 | } 48 | return Ret; 49 | } 50 | void Camera::Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH = RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH = 90.0f + RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | void Camera::OnMouse(int x, int y) 92 | { 93 | const int DeltaX = x - m_mousePos[0]; 94 | const int DeltaY = y - m_mousePos[1]; 95 | 96 | m_mousePos[0] = x; 97 | m_mousePos[1] = y; 98 | 99 | m_AngleH += (float)DeltaX / 20.0f; 100 | m_AngleV += (float)DeltaY / 20.0f; 101 | 102 | if (DeltaX == 0) { 103 | if (x <= MARGIN) { 104 | m_OnLeftEdge = true; 105 | } 106 | else if (x >= (m_windowWidth - MARGIN)) { 107 | m_OnRightEdge = true; 108 | } 109 | } 110 | else { 111 | m_OnLeftEdge = false; 112 | m_OnRightEdge = false; 113 | } 114 | if (DeltaY == 0) { 115 | if (y <= MARGIN) { 116 | m_OnUpperEdge = true; 117 | } 118 | else if (y >= (m_windowHeight - MARGIN)) { 119 | m_OnLowerEdge = true; 120 | } 121 | } 122 | else { 123 | m_OnUpperEdge = false; 124 | m_OnLowerEdge = false; 125 | } 126 | Update(); 127 | } 128 | void Camera::Update() 129 | { 130 | Vector3f Vaxis; 131 | LoadVector3(Vaxis, 0.0f, 1.0f, 0.0f); 132 | // Rotate the view vector by the horizontal angle around the vertical axis 133 | Vector3f View; 134 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 135 | RotateVector3(View, m_AngleH, Vaxis); 136 | NormalizeVector3(View); 137 | 138 | // Rotate the view vector by the vertical angle around the horizontal axis 139 | Vector3f Haxis; 140 | CrossProduct3(Haxis, Vaxis, View); 141 | NormalizeVector3(Haxis); 142 | 143 | RotateVector3(View, m_AngleV, Haxis); 144 | 145 | CopyVector3(m_target, View); 146 | NormalizeVector3(m_target); 147 | 148 | CrossProduct3(m_up, m_target, Haxis); 149 | NormalizeVector3(m_up); 150 | } 151 | void Camera::OnRender() 152 | { 153 | bool ShouldUpdate = false; 154 | 155 | if (m_OnLeftEdge) { 156 | m_AngleH -= EDGE_STEP; 157 | ShouldUpdate = true; 158 | } 159 | else if (m_OnRightEdge) { 160 | m_AngleH += EDGE_STEP; 161 | ShouldUpdate = true; 162 | } 163 | 164 | if (m_OnUpperEdge) { 165 | if (m_AngleV > -90.0f) { 166 | m_AngleV -= EDGE_STEP; 167 | ShouldUpdate = true; 168 | } 169 | } 170 | else if (m_OnLowerEdge) { 171 | if (m_AngleV < 90.0f) { 172 | m_AngleV += EDGE_STEP; 173 | ShouldUpdate = true; 174 | } 175 | } 176 | if (ShouldUpdate) { 177 | Update(); 178 | } 179 | } -------------------------------------------------------------------------------- /Spot_Lighting/code/opengl_camera.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_camera.h" 2 | #include 3 | #include 4 | #define StepSize 1 5 | const static float STEP_SCALE = 1.0f; 6 | const static float EDGE_STEP = 0.5f; 7 | const static int MARGIN = 10; 8 | bool Camera::OnKeyboard(int key) 9 | { 10 | bool Ret = false; 11 | 12 | switch (key) { 13 | case GLUT_KEY_UP: 14 | { 15 | for (int i = 0; i < 3; i++) 16 | m_pos[i] += m_target[i] * StepSize; 17 | Ret = TRUE; 18 | } 19 | break; 20 | case GLUT_KEY_DOWN: 21 | { 22 | for (int i = 0; i < 3; i++) 23 | m_pos[i] -= m_target[i] * StepSize; 24 | Ret = TRUE; 25 | } 26 | break; 27 | case GLUT_KEY_LEFT: 28 | { 29 | Vector3f Left; 30 | CrossProduct3(Left, m_target, m_up); 31 | NormalizeVector3(Left); 32 | for (int i = 0; i < 3; i++) 33 | m_pos[i] += Left[i] * StepSize; 34 | Ret = TRUE; 35 | } 36 | break; 37 | case GLUT_KEY_RIGHT: 38 | { 39 | Vector3f Right; 40 | CrossProduct3(Right, m_up, m_target); 41 | NormalizeVector3(Right); 42 | for (int i = 0; i < 3; i++) 43 | m_pos[i] += Right[i] * StepSize; 44 | Ret = TRUE; 45 | } 46 | break; 47 | } 48 | return Ret; 49 | } 50 | void Camera::Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH = RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH = 90.0f + RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | void Camera::OnMouse(int x, int y) 92 | { 93 | const int DeltaX = x - m_mousePos[0]; 94 | const int DeltaY = y - m_mousePos[1]; 95 | 96 | m_mousePos[0] = x; 97 | m_mousePos[1] = y; 98 | 99 | m_AngleH += (float)DeltaX / 20.0f; 100 | m_AngleV += (float)DeltaY / 20.0f; 101 | 102 | if (DeltaX == 0) { 103 | if (x <= MARGIN) { 104 | m_OnLeftEdge = true; 105 | } 106 | else if (x >= (m_windowWidth - MARGIN)) { 107 | m_OnRightEdge = true; 108 | } 109 | } 110 | else { 111 | m_OnLeftEdge = false; 112 | m_OnRightEdge = false; 113 | } 114 | if (DeltaY == 0) { 115 | if (y <= MARGIN) { 116 | m_OnUpperEdge = true; 117 | } 118 | else if (y >= (m_windowHeight - MARGIN)) { 119 | m_OnLowerEdge = true; 120 | } 121 | } 122 | else { 123 | m_OnUpperEdge = false; 124 | m_OnLowerEdge = false; 125 | } 126 | Update(); 127 | } 128 | void Camera::Update() 129 | { 130 | Vector3f Vaxis; 131 | LoadVector3(Vaxis, 0.0f, 1.0f, 0.0f); 132 | // Rotate the view vector by the horizontal angle around the vertical axis 133 | Vector3f View; 134 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 135 | RotateVector3(View, m_AngleH, Vaxis); 136 | NormalizeVector3(View); 137 | 138 | // Rotate the view vector by the vertical angle around the horizontal axis 139 | Vector3f Haxis; 140 | CrossProduct3(Haxis, Vaxis, View); 141 | NormalizeVector3(Haxis); 142 | 143 | RotateVector3(View, m_AngleV, Haxis); 144 | 145 | CopyVector3(m_target, View); 146 | NormalizeVector3(m_target); 147 | 148 | CrossProduct3(m_up, m_target, Haxis); 149 | NormalizeVector3(m_up); 150 | } 151 | void Camera::OnRender() 152 | { 153 | bool ShouldUpdate = false; 154 | 155 | if (m_OnLeftEdge) { 156 | m_AngleH -= EDGE_STEP; 157 | ShouldUpdate = true; 158 | } 159 | else if (m_OnRightEdge) { 160 | m_AngleH += EDGE_STEP; 161 | ShouldUpdate = true; 162 | } 163 | 164 | if (m_OnUpperEdge) { 165 | if (m_AngleV > -90.0f) { 166 | m_AngleV -= EDGE_STEP; 167 | ShouldUpdate = true; 168 | } 169 | } 170 | else if (m_OnLowerEdge) { 171 | if (m_AngleV < 90.0f) { 172 | m_AngleV += EDGE_STEP; 173 | ShouldUpdate = true; 174 | } 175 | } 176 | if (ShouldUpdate) { 177 | Update(); 178 | } 179 | } -------------------------------------------------------------------------------- /Texture/Texture/opengl_camera.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_camera.h" 2 | #include 3 | #include 4 | #define StepSize 1 5 | const static float STEP_SCALE = 1.0f; 6 | const static float EDGE_STEP = 0.5f; 7 | const static int MARGIN = 10; 8 | bool Camera::OnKeyboard(int key) 9 | { 10 | bool Ret = false; 11 | 12 | switch (key) { 13 | case GLUT_KEY_UP: 14 | { 15 | for (int i = 0; i < 3; i++) 16 | m_pos[i] += m_target[i] * StepSize; 17 | Ret = TRUE; 18 | } 19 | break; 20 | case GLUT_KEY_DOWN: 21 | { 22 | for (int i = 0; i < 3; i++) 23 | m_pos[i] -= m_target[i] * StepSize; 24 | Ret = TRUE; 25 | } 26 | break; 27 | case GLUT_KEY_LEFT: 28 | { 29 | Vector3f Left; 30 | CrossProduct3(Left, m_target, m_up); 31 | NormalizeVector3(Left); 32 | for (int i = 0; i < 3; i++) 33 | m_pos[i] += Left[i] * StepSize; 34 | Ret = TRUE; 35 | } 36 | break; 37 | case GLUT_KEY_RIGHT: 38 | { 39 | Vector3f Right; 40 | CrossProduct3(Right, m_up, m_target); 41 | NormalizeVector3(Right); 42 | for (int i = 0; i < 3; i++) 43 | m_pos[i] += Right[i] * StepSize; 44 | Ret = TRUE; 45 | } 46 | break; 47 | } 48 | return Ret; 49 | } 50 | void Camera::Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH = RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH = 90.0f + RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | void Camera::OnMouse(int x, int y) 92 | { 93 | const int DeltaX = x - m_mousePos[0]; 94 | const int DeltaY = y - m_mousePos[1]; 95 | 96 | m_mousePos[0] = x; 97 | m_mousePos[1] = y; 98 | 99 | m_AngleH += (float)DeltaX / 20.0f; 100 | m_AngleV += (float)DeltaY / 20.0f; 101 | 102 | if (DeltaX == 0) { 103 | if (x <= MARGIN) { 104 | m_OnLeftEdge = true; 105 | } 106 | else if (x >= (m_windowWidth - MARGIN)) { 107 | m_OnRightEdge = true; 108 | } 109 | } 110 | else { 111 | m_OnLeftEdge = false; 112 | m_OnRightEdge = false; 113 | } 114 | if (DeltaY == 0) { 115 | if (y <= MARGIN) { 116 | m_OnUpperEdge = true; 117 | } 118 | else if (y >= (m_windowHeight - MARGIN)) { 119 | m_OnLowerEdge = true; 120 | } 121 | } 122 | else { 123 | m_OnUpperEdge = false; 124 | m_OnLowerEdge = false; 125 | } 126 | Update(); 127 | } 128 | void Camera::Update() 129 | { 130 | Vector3f Vaxis; 131 | LoadVector3(Vaxis, 0.0f, 1.0f, 0.0f); 132 | // Rotate the view vector by the horizontal angle around the vertical axis 133 | Vector3f View; 134 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 135 | RotateVector3(View, m_AngleH, Vaxis); 136 | NormalizeVector3(View); 137 | 138 | // Rotate the view vector by the vertical angle around the horizontal axis 139 | Vector3f Haxis; 140 | CrossProduct3(Haxis, Vaxis, View); 141 | NormalizeVector3(Haxis); 142 | 143 | RotateVector3(View, m_AngleV, Haxis); 144 | 145 | CopyVector3(m_target, View); 146 | NormalizeVector3(m_target); 147 | 148 | CrossProduct3(m_up, m_target, Haxis); 149 | NormalizeVector3(m_up); 150 | } 151 | void Camera::OnRender() 152 | { 153 | bool ShouldUpdate = false; 154 | 155 | if (m_OnLeftEdge) { 156 | m_AngleH -= EDGE_STEP; 157 | ShouldUpdate = true; 158 | } 159 | else if (m_OnRightEdge) { 160 | m_AngleH += EDGE_STEP; 161 | ShouldUpdate = true; 162 | } 163 | 164 | if (m_OnUpperEdge) { 165 | if (m_AngleV > -90.0f) { 166 | m_AngleV -= EDGE_STEP; 167 | ShouldUpdate = true; 168 | } 169 | } 170 | else if (m_OnLowerEdge) { 171 | if (m_AngleV < 90.0f) { 172 | m_AngleV += EDGE_STEP; 173 | ShouldUpdate = true; 174 | } 175 | } 176 | if (ShouldUpdate) { 177 | Update(); 178 | } 179 | } -------------------------------------------------------------------------------- /Specular_lighting/code/opengl_camera.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_camera.h" 2 | #include 3 | #include 4 | #define StepSize 1 5 | const static float STEP_SCALE = 1.0f; 6 | const static float EDGE_STEP = 0.5f; 7 | const static int MARGIN = 10; 8 | bool Camera::OnKeyboard(int key) 9 | { 10 | bool Ret = false; 11 | 12 | switch (key) { 13 | case GLUT_KEY_UP: 14 | { 15 | for (int i = 0; i < 3; i++) 16 | m_pos[i] += m_target[i] * StepSize; 17 | Ret = TRUE; 18 | } 19 | break; 20 | case GLUT_KEY_DOWN: 21 | { 22 | for (int i = 0; i < 3; i++) 23 | m_pos[i] -= m_target[i] * StepSize; 24 | Ret = TRUE; 25 | } 26 | break; 27 | case GLUT_KEY_LEFT: 28 | { 29 | Vector3f Left; 30 | CrossProduct3(Left, m_target, m_up); 31 | NormalizeVector3(Left); 32 | for (int i = 0; i < 3; i++) 33 | m_pos[i] += Left[i] * StepSize; 34 | Ret = TRUE; 35 | } 36 | break; 37 | case GLUT_KEY_RIGHT: 38 | { 39 | Vector3f Right; 40 | CrossProduct3(Right, m_up, m_target); 41 | NormalizeVector3(Right); 42 | for (int i = 0; i < 3; i++) 43 | m_pos[i] += Right[i] * StepSize; 44 | Ret = TRUE; 45 | } 46 | break; 47 | } 48 | return Ret; 49 | } 50 | void Camera::Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH = RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH = 90.0f + RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | void Camera::OnMouse(int x, int y) 92 | { 93 | const int DeltaX = x - m_mousePos[0]; 94 | const int DeltaY = y - m_mousePos[1]; 95 | 96 | m_mousePos[0] = x; 97 | m_mousePos[1] = y; 98 | 99 | m_AngleH += (float)DeltaX / 20.0f; 100 | m_AngleV += (float)DeltaY / 20.0f; 101 | 102 | if (DeltaX == 0) { 103 | if (x <= MARGIN) { 104 | m_OnLeftEdge = true; 105 | } 106 | else if (x >= (m_windowWidth - MARGIN)) { 107 | m_OnRightEdge = true; 108 | } 109 | } 110 | else { 111 | m_OnLeftEdge = false; 112 | m_OnRightEdge = false; 113 | } 114 | if (DeltaY == 0) { 115 | if (y <= MARGIN) { 116 | m_OnUpperEdge = true; 117 | } 118 | else if (y >= (m_windowHeight - MARGIN)) { 119 | m_OnLowerEdge = true; 120 | } 121 | } 122 | else { 123 | m_OnUpperEdge = false; 124 | m_OnLowerEdge = false; 125 | } 126 | Update(); 127 | } 128 | void Camera::Update() 129 | { 130 | Vector3f Vaxis; 131 | LoadVector3(Vaxis, 0.0f, 1.0f, 0.0f); 132 | // Rotate the view vector by the horizontal angle around the vertical axis 133 | Vector3f View; 134 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 135 | RotateVector3(View, m_AngleH, Vaxis); 136 | NormalizeVector3(View); 137 | 138 | // Rotate the view vector by the vertical angle around the horizontal axis 139 | Vector3f Haxis; 140 | CrossProduct3(Haxis, Vaxis, View); 141 | NormalizeVector3(Haxis); 142 | 143 | RotateVector3(View, m_AngleV, Haxis); 144 | 145 | CopyVector3(m_target, View); 146 | NormalizeVector3(m_target); 147 | 148 | CrossProduct3(m_up, m_target, Haxis); 149 | NormalizeVector3(m_up); 150 | } 151 | void Camera::OnRender() 152 | { 153 | bool ShouldUpdate = false; 154 | 155 | if (m_OnLeftEdge) { 156 | m_AngleH -= EDGE_STEP; 157 | ShouldUpdate = true; 158 | } 159 | else if (m_OnRightEdge) { 160 | m_AngleH += EDGE_STEP; 161 | ShouldUpdate = true; 162 | } 163 | 164 | if (m_OnUpperEdge) { 165 | if (m_AngleV > -90.0f) { 166 | m_AngleV -= EDGE_STEP; 167 | ShouldUpdate = true; 168 | } 169 | } 170 | else if (m_OnLowerEdge) { 171 | if (m_AngleV < 90.0f) { 172 | m_AngleV += EDGE_STEP; 173 | ShouldUpdate = true; 174 | } 175 | } 176 | if (ShouldUpdate) { 177 | Update(); 178 | } 179 | } -------------------------------------------------------------------------------- /Shader/code/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "opengl_math.h" 7 | 8 | using namespace std; 9 | GLuint VBO; 10 | 11 | const char* pVSFileName = "shader.vs"; 12 | const char* pFSFileName = "shader.fs"; 13 | 14 | 15 | 16 | static void Render() 17 | { 18 | glClear(GL_COLOR_BUFFER_BIT); 19 | 20 | glEnableVertexAttribArray(0); 21 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 22 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 23 | 24 | glDrawArrays(GL_TRIANGLES, 0, 3); 25 | 26 | glDisableVertexAttribArray(0); 27 | 28 | glutSwapBuffers(); 29 | } 30 | 31 | 32 | static void InitializeGlutCallbacks() 33 | { 34 | glutDisplayFunc(Render); 35 | } 36 | 37 | static void CreateVertexBuffer() 38 | { 39 | Vector3f Vertices[3]; 40 | 41 | LoadVector3(Vertices[0], -1.0f, -1.0f, 0.0f); 42 | LoadVector3(Vertices[1], 1.0f, -1.0f, 0.0f); 43 | LoadVector3(Vertices[2], 0.0f, 1.0f, 0.0f); 44 | 45 | glGenBuffers(1, &VBO); 46 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 47 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 48 | } 49 | 50 | bool ReadFile(const char* pFileName, string &outFile) 51 | { 52 | ifstream f(pFileName); 53 | 54 | bool ret = false; 55 | 56 | if (f.is_open()) { 57 | string line; 58 | while (getline(f, line)) { 59 | outFile.append(line); 60 | outFile.append("\n"); 61 | } 62 | f.close(); 63 | ret = true; 64 | } 65 | else { 66 | fprintf(stderr, "%s:%d: unable to open file `%s`\n", __FILE__,__LINE__,pFileName); 67 | } 68 | return ret; 69 | } 70 | 71 | static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType) 72 | { 73 | GLuint ShaderObj = glCreateShader(ShaderType); 74 | //check if it is successful 75 | if (ShaderObj == 0) { 76 | fprintf(stderr, "Error creating shader type %d\n", ShaderType); 77 | exit(0); 78 | } 79 | 80 | //define shader code source 81 | const GLchar* p[1]; 82 | p[0] = pShaderText; 83 | GLint Lengths[1]; 84 | Lengths[0] = strlen(pShaderText); 85 | glShaderSource(ShaderObj, 1, p, Lengths); 86 | //Compiler shader object 87 | glCompileShader(ShaderObj); 88 | 89 | //check the error about shader 90 | GLint success; 91 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 92 | if (!success) { 93 | GLchar InfoLog[1024]; 94 | glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog); 95 | fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog); 96 | exit(1); 97 | } 98 | //bound the shader object to shader program 99 | glAttachShader(ShaderProgram, ShaderObj); 100 | } 101 | 102 | static void CompilerShaders() 103 | { 104 | //Create Shaders 105 | GLuint ShaderProgram = glCreateProgram(); 106 | 107 | //Check yes or not success 108 | if (ShaderProgram == 0) { 109 | fprintf(stderr, "Error creating shader program\n"); 110 | exit(1); 111 | } 112 | 113 | //the buffer of shader texts 114 | string vs, fs; 115 | //read the text of shader texts to buffer 116 | if (!ReadFile(pVSFileName, vs)) { 117 | exit(1); 118 | } 119 | if (!ReadFile(pFSFileName, fs)) { 120 | exit(1); 121 | } 122 | 123 | //add vertex shader and fragment shader 124 | AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER); 125 | AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER); 126 | 127 | //Link the shader program, and check the error 128 | GLint Success = 0; 129 | GLchar ErrorLog[1024] = { 0 }; 130 | glLinkProgram(ShaderProgram); 131 | glGetProgramiv(ShaderProgram,GL_LINK_STATUS,&Success); 132 | if (Success == 0) { 133 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 134 | fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog); 135 | exit(1); 136 | } 137 | 138 | //check if it can be execute 139 | glValidateProgram(ShaderProgram); 140 | glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success); 141 | if (!Success) { 142 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 143 | fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog); 144 | exit(1); 145 | } 146 | 147 | //use program 148 | glUseProgram(ShaderProgram); 149 | 150 | } 151 | 152 | int main(int argc, char **argv) 153 | { 154 | glutInit(&argc, argv); 155 | 156 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 157 | glutInitWindowSize(1024, 768); 158 | glutInitWindowPosition(10, 10); 159 | glutCreateWindow("Shader"); 160 | 161 | InitializeGlutCallbacks(); 162 | 163 | GLenum res = glewInit(); 164 | if (res != GLEW_OK) { 165 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 166 | return 1; 167 | } 168 | 169 | printf("GL version: %s\n", glGetString(GL_VERSION)); 170 | 171 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 172 | 173 | CreateVertexBuffer(); 174 | 175 | CompilerShaders(); 176 | 177 | glutMainLoop(); 178 | 179 | return 0; 180 | } -------------------------------------------------------------------------------- /Interpolation/code/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "opengl_math.h" 9 | 10 | using namespace std; 11 | 12 | GLuint VBO; 13 | GLuint gWorldLocation; 14 | 15 | const char* pVSFileName = "shader.vs"; 16 | const char* pFSFileName = "shader.fs"; 17 | 18 | bool ReadFile(const char* FileName, string &outFile) 19 | { 20 | ifstream f(FileName); 21 | bool ret = false; 22 | 23 | if (f.is_open()) { 24 | string line; 25 | while (getline(f, line)) { 26 | outFile.append(line); 27 | outFile.append("\n"); 28 | } 29 | f.close(); 30 | ret = true; 31 | } 32 | else { 33 | fprintf(stderr, "%s:%d: unable to open file '%s'\n", __FILE__, __LINE__, FileName); 34 | system("pause"); 35 | } 36 | return ret; 37 | } 38 | 39 | static void Render() 40 | { 41 | glClear(GL_COLOR_BUFFER_BIT); 42 | 43 | static float Scale = 0.0f; 44 | Scale += 0.001f; 45 | 46 | Matrix44f World; 47 | ScaleMatrix44(World, sinf(Scale), sinf(Scale), sinf(Scale)); 48 | 49 | glUniformMatrix4fv(gWorldLocation, 1, GL_FALSE, &World[0]); 50 | 51 | glEnableVertexAttribArray(0); 52 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 53 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 54 | glDrawArrays(GL_TRIANGLES, 0, 3); 55 | glDisableVertexAttribArray(0); 56 | 57 | glutSwapBuffers(); 58 | } 59 | 60 | static void InitializeGlutCallbacks() 61 | { 62 | glutDisplayFunc(Render); 63 | glutIdleFunc(Render); 64 | } 65 | 66 | static void CreateVertexBuffer() 67 | { 68 | Vector3f Vertices[3]; 69 | LoadVector3(Vertices[0], -1.0f, -1.0f, 0.0f); 70 | LoadVector3(Vertices[1], 1.0f, -1.0f, 0.0f); 71 | LoadVector3(Vertices[2], 0.0f, 1.0f, 0.0f); 72 | 73 | glGenBuffers(1, &VBO); 74 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 75 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 76 | } 77 | 78 | static void AdderShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType) 79 | { 80 | GLuint ShaderObj = glCreateShader(ShaderType); 81 | if (!ShaderObj) { 82 | fprintf(stderr, "Error creating shader ype %d\n", ShaderType); 83 | system("pause"); 84 | exit(1); 85 | } 86 | 87 | const GLchar* p[1]; 88 | p[0] = pShaderText; 89 | GLint Lengths[1]; 90 | Lengths[0] = strlen(pShaderText); 91 | glShaderSource(ShaderObj, 1, p, Lengths); 92 | glCompileShader(ShaderObj); 93 | GLint success; 94 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 95 | if (!success) { 96 | GLchar InfoLog[1024]; 97 | glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog); 98 | fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType,InfoLog); 99 | system("pause"); 100 | exit(1); 101 | } 102 | glAttachShader(ShaderProgram, ShaderObj); 103 | } 104 | 105 | static void CompileShaders() 106 | { 107 | GLuint ShaderProgram = glCreateProgram(); 108 | if (ShaderProgram == 0) { 109 | fprintf(stderr, "Error creating shader program\n"); 110 | system("pause"); 111 | exit(1); 112 | } 113 | 114 | string vs, fs; 115 | 116 | if (!ReadFile(pVSFileName, vs)) { 117 | exit(1); 118 | } 119 | if (!ReadFile(pFSFileName, fs)) { 120 | exit(1); 121 | } 122 | 123 | AdderShader(ShaderProgram,vs.c_str(),GL_VERTEX_SHADER); 124 | AdderShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER); 125 | 126 | GLint Success = 0; 127 | GLchar ErrorLog[1024] = { 0 }; 128 | 129 | glLinkProgram(ShaderProgram); 130 | glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success); 131 | if (!Success) { 132 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 133 | fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog); 134 | exit(1); 135 | } 136 | 137 | glValidateProgram(ShaderProgram); 138 | glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success); 139 | if (!Success) { 140 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 141 | fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog); 142 | exit(1); 143 | } 144 | 145 | glUseProgram(ShaderProgram); 146 | 147 | gWorldLocation = glGetUniformLocation(ShaderProgram, "gWorld"); 148 | assert(gWorldLocation != 0xFFFFFFFF); 149 | } 150 | 151 | int main(int argc, char **argv) 152 | { 153 | glutInit(&argc, argv); 154 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 155 | glutInitWindowPosition(10, 10); 156 | glutInitWindowSize(1024, 768); 157 | glutCreateWindow("Interpolation"); 158 | 159 | InitializeGlutCallbacks(); 160 | 161 | GLenum res = glewInit(); 162 | if (res != GLEW_OK) { 163 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 164 | system("pause"); 165 | return 1; 166 | } 167 | 168 | printf("GL version: %s \n", glGetString(GL_VERSION)); 169 | 170 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 171 | 172 | CreateVertexBuffer(); 173 | 174 | CompileShaders(); 175 | 176 | glutMainLoop(); 177 | 178 | return 0; 179 | 180 | } -------------------------------------------------------------------------------- /Point_Lighting/code/opengl_light.cpp: -------------------------------------------------------------------------------- 1 | #include "opengl_light.h" 2 | bool Light::GetUniformLocations() 3 | { 4 | m_eyeWorldPosLocation = glGetUniformLocation(m_shaderProg, "gEyeWorldPos"); 5 | m_dirLightLocation.Color = glGetUniformLocation(m_shaderProg, "gDirectionalLight.Base.Color"); 6 | m_dirLightLocation.AmbientIntensity = glGetUniformLocation(m_shaderProg, "gDirectionalLight.Base.AmbientIntensity"); 7 | m_dirLightLocation.Direction = glGetUniformLocation(m_shaderProg, "gDirectionalLight.Direction"); 8 | m_dirLightLocation.DiffuseIntensity = glGetUniformLocation(m_shaderProg, "gDirectionalLight.Base.DiffuseIntensity"); 9 | m_matSpecularIntensityLocation = glGetUniformLocation(m_shaderProg, "gMatSpecularIntensity"); 10 | m_matSpecularPowerLocation = glGetUniformLocation(m_shaderProg, "gSpecularPower"); 11 | m_numPointLightsLocation = glGetUniformLocation(m_shaderProg, "gNumPointLights"); 12 | 13 | for (unsigned int i = 0; i < ARRAY_SIZE_IN_ELEMENTS(m_pointLightsLocation); i++) 14 | { 15 | char Name[128]; 16 | memset(Name, 0, sizeof(Name)); 17 | snprintf(Name, sizeof(Name), "gPointLights[%d].Base.Color", i); 18 | m_pointLightsLocation[i].Color = glGetUniformLocation(m_shaderProg, Name); 19 | 20 | snprintf(Name, sizeof(Name), "gPointLights[%d].Base.AmbientIntensity", i); 21 | m_pointLightsLocation[i].AmbientIntensity = glGetUniformLocation(m_shaderProg, Name); 22 | 23 | snprintf(Name, sizeof(Name), "gPointLights[%d].Position", i); 24 | m_pointLightsLocation[i].Position = glGetUniformLocation(m_shaderProg, Name); 25 | 26 | snprintf(Name, sizeof(Name), "gPointLights[%d].Base.DiffuseIntensity", i); 27 | m_pointLightsLocation[i].DiffuseIntensity = glGetUniformLocation(m_shaderProg, Name); 28 | 29 | snprintf(Name, sizeof(Name), "gPointLights[%d].Atten.Constant", i); 30 | m_pointLightsLocation[i].Atten.Constant = glGetUniformLocation(m_shaderProg, Name); 31 | 32 | snprintf(Name, sizeof(Name), "gPointLights[%d].Atten.Linear", i); 33 | m_pointLightsLocation[i].Atten.Linear = glGetUniformLocation(m_shaderProg, Name); 34 | 35 | snprintf(Name, sizeof(Name), "gPointLights[%d].Atten.Exp", i); 36 | m_pointLightsLocation[i].Atten.Exp = glGetUniformLocation(m_shaderProg, Name); 37 | 38 | if (m_pointLightsLocation[i].Color == INVALID_UNIFORM_LOCATION || 39 | m_pointLightsLocation[i].AmbientIntensity == INVALID_UNIFORM_LOCATION || 40 | m_pointLightsLocation[i].Position == INVALID_UNIFORM_LOCATION || 41 | m_pointLightsLocation[i].DiffuseIntensity == INVALID_UNIFORM_LOCATION || 42 | m_pointLightsLocation[i].Atten.Constant == INVALID_UNIFORM_LOCATION || 43 | m_pointLightsLocation[i].Atten.Linear == INVALID_UNIFORM_LOCATION || 44 | m_pointLightsLocation[i].Atten.Exp == INVALID_UNIFORM_LOCATION) { 45 | return false; 46 | } 47 | } 48 | if (m_dirLightLocation.AmbientIntensity == INVALID_UNIFORM_LOCATION || 49 | m_eyeWorldPosLocation == INVALID_UNIFORM_LOCATION || 50 | m_dirLightLocation.Color == INVALID_UNIFORM_LOCATION || 51 | m_dirLightLocation.DiffuseIntensity == INVALID_UNIFORM_LOCATION || 52 | m_dirLightLocation.Direction == INVALID_UNIFORM_LOCATION || 53 | m_matSpecularIntensityLocation == INVALID_UNIFORM_LOCATION || 54 | m_matSpecularPowerLocation == INVALID_UNIFORM_LOCATION || 55 | m_numPointLightsLocation == INVALID_UNIFORM_LOCATION) { 56 | return false; 57 | } 58 | return true; 59 | } 60 | 61 | void Light::SetDirectionalLight(const DirectionalLight& Light) 62 | { 63 | glUniform3f(m_dirLightLocation.Color, Light.Color[0], Light.Color[1], Light.Color[2]); 64 | glUniform1f(m_dirLightLocation.AmbientIntensity, Light.AmbientIntensity); 65 | Vector3f Direction; 66 | CopyVector3(Direction, Light.Direction); 67 | NormalizeVector3(Direction); 68 | glUniform3f(m_dirLightLocation.Direction, Direction[0], Direction[1], Direction[2]); 69 | glUniform1f(m_dirLightLocation.DiffuseIntensity, Light.DiffuseIntensity); 70 | } 71 | 72 | void Light::SetEyeWorldPos(const Vector3f& EyeWorldPos) 73 | { 74 | glUniform3f(m_eyeWorldPosLocation, EyeWorldPos[0], EyeWorldPos[1], EyeWorldPos[2]); 75 | } 76 | 77 | void Light::SetMatSpecularIntensity(float Intensity) 78 | { 79 | glUniform1f(m_matSpecularIntensityLocation, Intensity); 80 | } 81 | 82 | void Light::SetMatSpecularPower(float Power) 83 | { 84 | glUniform1f(m_matSpecularPowerLocation, Power); 85 | } 86 | 87 | void Light::SetPointLights(unsigned int NumLights, const PointLight* pLights) 88 | { 89 | glUniform1i(m_numPointLightsLocation, NumLights); 90 | 91 | for (unsigned int i = 0; i < NumLights; i++) { 92 | glUniform3f(m_pointLightsLocation[i].Color, pLights[i].Color[0], pLights[i].Color[1], pLights[i].Color[2]); 93 | glUniform1f(m_pointLightsLocation[i].AmbientIntensity, pLights[i].AmbientIntensity); 94 | glUniform1f(m_pointLightsLocation[i].DiffuseIntensity, pLights[i].DiffuseIntensity); 95 | glUniform3f(m_pointLightsLocation[i].Position, pLights[i].Position[0], pLights[i].Position[1], pLights[i].Position[2]); 96 | glUniform1f(m_pointLightsLocation[i].Atten.Constant, pLights[i].Attenuation.Constant); 97 | glUniform1f(m_pointLightsLocation[i].Atten.Linear, pLights[i].Attenuation.Linear); 98 | glUniform1f(m_pointLightsLocation[i].Atten.Exp, pLights[i].Attenuation.Exp); 99 | } 100 | } -------------------------------------------------------------------------------- /hello_dot/OpenGL学习之路2----画一个点.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | ## 一、简述 3 | 这一节比上一节《打开一个窗口》,多了需要使用glew库和3d数学库 4 | 5 | glew是OpenGL的扩展库,用于帮助C/C++开发者初始化扩展(OpenGL扩展功能)并书写可移植的应用程序。这个可以通过上网搜教程自行安装引用。 6 | 7 | 3d函数库可以用网上的,也可以用源教程里的。这里我是根据需要开始自己写了一个3d数学的头文件,很好理解,也更利于学习计算机图形学,根据每章教程需要用的不同数学表达,逐渐增加我的3d数学文件。 8 | 9 | ## 二、代码解释 10 | 11 | opengl_math.h: 12 | 13 | ``` 14 | #ifndef __OPENGL_MATH_H 15 | #define __OPENGL_MATH_H 16 | 17 | //向量 18 | typedef float Vector3f[3]; 19 | 20 | //向量赋值 21 | inline void LoadVector3(Vector3f v, const float x, const float y, const float z) 22 | { 23 | v[0] = x; v[1] = y; v[2] = z; 24 | } 25 | 26 | #endif 27 | ``` 28 | main.c: 29 | 30 | ``` 31 | #include 32 | #include // GLEW扩展库,这个引用要放在freeglut.h前 33 | #include // freeGLUT图形库 34 | #include "opengl_math.h" 35 | 36 | GLuint VBO; 37 | 38 | static void RenderScenceCB() { 39 | glClear(GL_COLOR_BUFFER_BIT); 40 | 41 | // 绑定GL_ARRAY_BUFFER缓冲器 42 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 43 | // 告诉管线怎样解析bufer中的数据 44 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 45 | 46 | // 开始绘制几何图形(绘制一个点) 47 | glDrawArrays(GL_POINTS, 0, 1); 48 | 49 | // 交换前后缓存 50 | glutSwapBuffers(); 51 | } 52 | static void CreateVertexBuffer() 53 | { 54 | // 创建含有一个顶点的顶点数组 55 | Vector3f Vertices[1]; 56 | // 将点置于屏幕中央 57 | LoadVector3(Vertices[0], 0.0f, 0.0f, 0.0f); 58 | 59 | // 创建缓冲器 60 | glGenBuffers(1, &VBO); 61 | // 绑定GL_ARRAY_BUFFER缓冲器 62 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 63 | // 绑定顶点数据 64 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 65 | } 66 | 67 | int main(int argc, char ** argv) { 68 | 69 | // 初始化GLUT 70 | glutInit(&argc, argv); 71 | 72 | // 显示模式:双缓冲、RGBA 73 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 74 | 75 | // 窗口设置 76 | glutInitWindowSize(480, 320); // 窗口尺寸 77 | glutInitWindowPosition(100, 100); // 窗口位置 78 | glutCreateWindow("Hello dot"); // 窗口标题 79 | 80 | // 开始渲染 81 | glutDisplayFunc(RenderScenceCB); 82 | 83 | // 检查GLEW是否就绪,必须要在GLUT初始化之后! 84 | GLenum res = glewInit(); 85 | if (res != GLEW_OK) { 86 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 87 | return 1; 88 | } 89 | 90 | // 缓存清空后的颜色值 91 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 92 | 93 | // 创建顶点缓冲器 94 | CreateVertexBuffer(); 95 | 96 | // 开始GLUT的内部循环 97 | glutMainLoop(); 98 | 99 | return 0; 100 | } 101 | ``` 102 | 103 | ### 2.1 3d数学库 104 | ``` 105 | typedef float Vector3f[3]; 106 | ``` 107 | 首先是3d数学库,本次只用到了三维向量,我们使用一个长度为3的浮点数数组来表示一个三维向量 108 | 109 | ``` 110 | inline void LoadVector3(Vector3f v, const float x, const float y, const float z) 111 | { 112 | v[0] = x; v[1] = y; v[2] = z; 113 | } 114 | ``` 115 | ### 2.2 创建顶点缓冲器 116 | 通过一个内联函数,可以给三维向量(长度为三的浮点数数组)赋值 117 | 118 | 119 | ``` 120 | Vector3f Vertices[1]; 121 | LoadVector3(Vertices[0], 0.0f, 0.0f, 0.0f); 122 | ``` 123 | CreateVertexBuffer()创建顶点缓冲器函数里,创建了一个只有一个顶点的顶点数组,再给那个顶点赋值为(0.0f,0.0f,0.0f)即在空间的位置是原点(这里会绘制在屏幕中心) 124 | 125 | 126 | ``` 127 | 128 | glGenBuffers(1, &VBO); 129 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 130 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 131 | ``` 132 | 在创建顶点缓冲器(以后的教程还会创建索引缓冲期)的时候,这三个函数一般都连续出现。 133 | 134 | 135 | ``` 136 | void glGenBuffers(GLsizei n ,GLuint *buffers) 137 | ``` 138 | 139 | - 返回n个当前未使用的缓存对象名称,并保存到buffers数组中。返回到buffers中的名称不一定是连续的整型数据。如果n是负数,那么产生GL_INVALID_VALUE错误。 140 | - 所以glGenBuffers(1, &VBO)是返回1个当前未使用的缓存对象的名称,并保存到VBO里 141 | - VBO是一个全局GLuint(其实就是unsigned int)型的变量 142 | 143 | 144 | ``` 145 | void glBindBuffer(GLenum target,GLuint buffer) 146 | ``` 147 | - 这个函数的功能是指定当前激活的对象 148 | - target是缓存对象的类型(GL_ARRAY_BUFFER,GL_ATOMIC_COUNTER_BUFFER,GL_ELEMENT_ARRAY_BUFFER等等...),这里使用GL_ARRAY_BUFFER代表绑定顶点缓冲区对象 149 | - buffer设置的是要绑定的缓存对象的名称,这里用刚才创建的缓存对象VBO 150 | - 此时VBO成为当前target中被激活的对象,现在程序小,只有一个缓存对象,以后程序大起来,每次对缓存对象进行操作时都需要先使用这个函数激活缓存对象 151 | 152 | ``` 153 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 154 | ``` 155 | 创建了缓冲器,绑定了激活了它,现在就是把顶点的位置信息绑定在它上面,第一个参数是缓存对象类型,第二个参数是数据的长度,第三个参数是数据的地址,最后一个参数说明是静态数据,顶点数据一经初始化便不会改变 156 | 157 | ### 2.3 glew初始化 158 | 159 | ``` 160 | GLenum res = glewInit(); 161 | if (res != GLEW_OK) { 162 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 163 | return 1; 164 | } 165 | ``` 166 | 在glut初始化后,检查glew是否就绪,如果glew初始化没什么问题,程序可以继续运行,否则退出 167 | 168 | ### 2.4 渲染函数 169 | 170 | ``` 171 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 172 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 173 | glDrawArrays(GL_POINTS, 0, 1); 174 | ``` 175 | 先是绑定缓冲对象到GL_ARRAY_BUFFER缓冲器上,之后对其操作 176 | 177 | ``` 178 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 179 | ``` 180 | - 第一个参数是属性的索引,这里只有一组数据默认为0,如果开始使用shader着色器时,我们既要明确的设置着色器中的属性索引同时也要检索它 181 | - 第二个参数指属性中元素的个数,这里是3个,我们长度为3的浮点数数组,同时也代表XYZ的坐标 182 | - 第三个参数指元素的数据类型,这里时FLOAT 183 | - 第四个参数指明我们是否想让我们的属性在被管线使用之前被单位化,这里数据不变所以设为0 184 | - 第五个参数是偏移距离,我们通常不止存储位置信息,假如我们存储的数据是一组(X,Y,Z,R,G,B),而这一组RGB的偏移位置就是X+Y+Z的大小(以后用到的时候会理解的) 185 | 186 | ``` 187 | glDrawArrays(GL_POINTS, 0, 1); 188 | ``` 189 | - 最后调用这个函数绘制几何图形,第一个参数定义画点,即每一个顶点只表示一个点 190 | - 第二个从拿书是绘制顶点的索引,因为就一个顶点数据,所以从最开始绘制,设置为0 191 | - 第三个参数是绘制的顶点数,1个顶点所以设置为1 192 | 193 | ## 运行结果 194 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/hello_dot/image/1.png) 195 | -------------------------------------------------------------------------------- /prespective_projection/code/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "opengl_math.h" 9 | #include "opengl_pipeline.h" 10 | 11 | using namespace std; 12 | 13 | GLuint VBO; 14 | GLuint IBO; 15 | GLuint gWorldLocation; 16 | 17 | PersProjInfo gPersProjInfo; 18 | 19 | const char* pVSFileName = "shader.vs"; 20 | const char* pFSFileName = "shader.fs"; 21 | 22 | bool ReadFile(const char* pFileName, string &outFile) 23 | { 24 | ifstream f(pFileName); 25 | bool ret = FALSE; 26 | if (f.is_open()) { 27 | string line; 28 | while (getline(f, line)) { 29 | outFile.append(line); 30 | outFile.append("\n"); 31 | } 32 | f.close(); 33 | ret = TRUE; 34 | } 35 | else { 36 | fprintf(stderr, "%s:%d: unable to open file '%s'\n", __FILE__, __LINE__, pFileName); 37 | system("pause"); 38 | exit(1); 39 | } 40 | return ret; 41 | } 42 | 43 | static void Render() 44 | { 45 | glClear(GL_COLOR_BUFFER_BIT); 46 | static float Scale = 0.0f; 47 | Scale += 0.1f; 48 | 49 | Pipeline p; 50 | p.Rotate(0.0f, Scale, 0.0f); 51 | p.WorldPos(0.0f, 0.0f, 5.0f); 52 | p.SetPerspectiveProj(gPersProjInfo); 53 | 54 | glUniformMatrix4fv(gWorldLocation, 1, GL_FALSE, (const GLfloat*)p.GetWPTrans()); 55 | 56 | glEnableVertexAttribArray(0); 57 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 58 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 59 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 60 | 61 | glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); 62 | glDisableVertexAttribArray(0); 63 | 64 | glutSwapBuffers(); 65 | 66 | 67 | } 68 | 69 | static void InitializeGlutCallbacks() 70 | { 71 | glutDisplayFunc(Render); 72 | 73 | glutIdleFunc(Render); 74 | } 75 | static void CreateVertexBuffer() 76 | { 77 | Vector3f Vertices[4]; 78 | 79 | LoadVector3(Vertices[0], -1.0f, -1.0f, 0.5773f); 80 | LoadVector3(Vertices[1], 0.0f, -1.0f, -1.15475f); 81 | LoadVector3(Vertices[2], 1.0f, -1.0f, 0.5773f); 82 | LoadVector3(Vertices[3], 0.0f, 1.0f, 0.0f); 83 | 84 | glGenBuffers(1, &VBO); 85 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 86 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 87 | } 88 | static void CreateIndexBuffer() 89 | { 90 | unsigned int Indices[] = { 91 | 0,3,1, 92 | 1,3,2, 93 | 2,3,0, 94 | 0,1,2 95 | }; 96 | 97 | glGenBuffers(1, &IBO); 98 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 99 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); 100 | } 101 | static void AddShader(GLuint ShaderProgram, const char* ShaderText, GLenum ShaderType) 102 | { 103 | GLuint ShaderObj = glCreateShader(ShaderType); 104 | if (!ShaderObj) { 105 | fprintf(stderr, "Error creating shader object"); 106 | system("pause"); 107 | exit(1); 108 | } 109 | 110 | const GLchar* p[1]; 111 | p[0] = ShaderText; 112 | GLint Lengths[1]; 113 | Lengths[0] = strlen(ShaderText); 114 | glShaderSource(ShaderObj, 1, p, Lengths); 115 | glCompileShader(ShaderObj); 116 | GLint success; 117 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 118 | if (!success) { 119 | GLchar InfoLog[1024]; 120 | glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog); 121 | fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog); 122 | system("pause"); 123 | exit(1); 124 | } 125 | 126 | glAttachShader(ShaderProgram, ShaderObj); 127 | } 128 | 129 | static void ComileShaders() 130 | { 131 | GLuint ShaderProgram = glCreateProgram(); 132 | if (!ShaderProgram) { 133 | fprintf(stderr, "Error creating shader program"); 134 | system("pause"); 135 | exit(1); 136 | } 137 | 138 | string vs, fs; 139 | if (!ReadFile(pVSFileName, vs)) { 140 | exit(1); 141 | } 142 | if (!ReadFile(pFSFileName, fs)) { 143 | exit(1); 144 | } 145 | 146 | AddShader(ShaderProgram,vs.c_str(),GL_VERTEX_SHADER); 147 | AddShader(ShaderProgram,fs.c_str(),GL_FRAGMENT_SHADER); 148 | 149 | GLint Success; 150 | GLchar ErrorLog[1024] = { 0 }; 151 | 152 | glLinkProgram(ShaderProgram); 153 | glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success); 154 | if (!Success) { 155 | glGetProgramInfoLog(ShaderProgram, 1024, NULL, ErrorLog); 156 | fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog); 157 | system("pause"); 158 | exit(1); 159 | } 160 | 161 | glValidateProgram(ShaderProgram); 162 | glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success); 163 | if (!Success) { 164 | glGetProgramInfoLog(ShaderProgram, 1024, NULL, ErrorLog); 165 | fprintf(stderr, "Invalid shader program: '%s' \n", ErrorLog); 166 | system("pause"); 167 | exit(1); 168 | } 169 | 170 | glUseProgram(ShaderProgram); 171 | 172 | gWorldLocation = glGetUniformLocation(ShaderProgram, "gWorld"); 173 | assert(gWorldLocation != 0xFFFFFFFF); 174 | } 175 | 176 | int main(int argc,char **argv) 177 | { 178 | glutInit(&argc, argv); 179 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 180 | glutInitWindowPosition(10, 10); 181 | glutInitWindowSize(1024, 768); 182 | glutCreateWindow("prespective_projection"); 183 | 184 | InitializeGlutCallbacks(); 185 | 186 | GLenum res = glewInit(); 187 | if (res != GLEW_OK) { 188 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 189 | system("pause"); 190 | return 1; 191 | } 192 | 193 | printf("GL version: %s\n", glGetString(GL_VERSION)); 194 | 195 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 196 | 197 | CreateVertexBuffer(); 198 | CreateIndexBuffer(); 199 | 200 | ComileShaders(); 201 | 202 | gPersProjInfo.FOV = 30.0f; 203 | gPersProjInfo.Height = 768; 204 | gPersProjInfo.Width = 1024; 205 | gPersProjInfo.zNear = 1.0f; 206 | gPersProjInfo.zFar = 100.0f; 207 | 208 | 209 | 210 | glutMainLoop(); 211 | 212 | return 0; 213 | } -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | const static float STEP_SCALE = 1.0f; 7 | const static float EDGE_STEP = 0.5f; 8 | const static int MARGIN = 10; 9 | 10 | class Camera 11 | { 12 | private: 13 | Vector3f m_pos; 14 | Vector3f m_target; 15 | Vector3f m_up; 16 | 17 | float m_AngleH; 18 | float m_AngleV; 19 | 20 | bool m_OnUpperEdge; 21 | bool m_OnLowerEdge; 22 | bool m_OnLeftEdge; 23 | bool m_OnRightEdge; 24 | 25 | Vector2i m_mousePos; 26 | 27 | int m_windowWidth; 28 | int m_windowHeight; 29 | public: 30 | Camera(int Window_Width, int Window_Height) 31 | { 32 | m_windowWidth = Window_Width; 33 | m_windowHeight = Window_Height; 34 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 35 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 36 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 37 | } 38 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 39 | { 40 | m_windowWidth = Window_Width; 41 | m_windowHeight = Window_Height; 42 | CopyVector3(m_pos, Pos); 43 | CopyVector3(m_target, Target); 44 | NormalizeVector3(m_target); 45 | CopyVector3(m_up, Up); 46 | NormalizeVector3(m_up); 47 | 48 | Init(); 49 | } 50 | void Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH= RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH=90.0f+ RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | bool OnKeyboard(int key) 92 | { 93 | bool Ret = false; 94 | 95 | switch (key) { 96 | case GLUT_KEY_UP: 97 | { 98 | for (int i = 0; i < 3; i++) 99 | m_pos[i] += m_target[i]; 100 | Ret = TRUE; 101 | } 102 | break; 103 | case GLUT_KEY_DOWN: 104 | { 105 | for (int i = 0; i < 3; i++) 106 | m_pos[i] -= m_target[i]; 107 | Ret = TRUE; 108 | } 109 | break; 110 | case GLUT_KEY_LEFT: 111 | { 112 | Vector3f Left; 113 | CrossProduct3(Left, m_up, m_target); 114 | NormalizeVector3(Left); 115 | for (int i = 0; i < 3; i++) 116 | m_pos[i] += Left[i]; 117 | Ret = TRUE; 118 | } 119 | break; 120 | case GLUT_KEY_RIGHT: 121 | { 122 | Vector3f Right; 123 | CrossProduct3(Right,m_target,m_up); 124 | NormalizeVector3(Right); 125 | for (int i = 0; i < 3; i++) 126 | m_pos[i] += Right[i]; 127 | Ret = TRUE; 128 | } 129 | break; 130 | } 131 | return Ret; 132 | } 133 | void OnMouse(int x, int y) 134 | { 135 | const int DeltaX = x - m_mousePos[0]; 136 | const int DeltaY = y - m_mousePos[1]; 137 | 138 | m_mousePos[0] = x; 139 | m_mousePos[1] = y; 140 | 141 | m_AngleH += (float)DeltaX / 20.0f; 142 | m_AngleV += (float)DeltaY / 20.0f; 143 | 144 | if (DeltaX == 0) { 145 | if (x <= MARGIN) { 146 | // m_AngleH -= 1.0f; 147 | m_OnLeftEdge = true; 148 | } 149 | else if (x >= (m_windowWidth - MARGIN)) { 150 | // m_AngleH += 1.0f; 151 | m_OnRightEdge = true; 152 | } 153 | } 154 | else { 155 | m_OnLeftEdge = false; 156 | m_OnRightEdge = false; 157 | } 158 | if (DeltaY == 0) { 159 | if (y <= MARGIN) { 160 | m_OnUpperEdge = true; 161 | } 162 | else if (y >= (m_windowHeight - MARGIN)) { 163 | m_OnLowerEdge = true; 164 | } 165 | } 166 | else { 167 | m_OnUpperEdge = false; 168 | m_OnLowerEdge = false; 169 | } 170 | Update(); 171 | } 172 | void Update() 173 | { 174 | Vector3f Vaxis; 175 | LoadVector3(Vaxis,0.0f, 1.0f, 0.0f); 176 | // Rotate the view vector by the horizontal angle around the vertical axis 177 | Vector3f View; 178 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 179 | RotateVector3(View, m_AngleH, Vaxis); 180 | NormalizeVector3(View); 181 | 182 | // Rotate the view vector by the vertical angle around the horizontal axis 183 | Vector3f Haxis; 184 | CrossProduct3(Haxis,Vaxis,View); 185 | NormalizeVector3(Haxis); 186 | 187 | RotateVector3(View, m_AngleV, Haxis); 188 | 189 | CopyVector3(m_target, View); 190 | NormalizeVector3(m_target); 191 | 192 | CrossProduct3(m_up, m_target, Haxis); 193 | NormalizeVector3(m_up); 194 | } 195 | void OnRender() 196 | { 197 | bool ShouldUpdate = false; 198 | 199 | if (m_OnLeftEdge) { 200 | m_AngleH -= EDGE_STEP; 201 | ShouldUpdate = true; 202 | } 203 | else if (m_OnRightEdge) { 204 | m_AngleH += EDGE_STEP; 205 | ShouldUpdate = true; 206 | } 207 | 208 | if (m_OnUpperEdge) { 209 | if (m_AngleV > -90.0f) { 210 | m_AngleV -= EDGE_STEP; 211 | ShouldUpdate = true; 212 | } 213 | } 214 | else if (m_OnLowerEdge) { 215 | if (m_AngleV < 90.0f) { 216 | m_AngleV += EDGE_STEP; 217 | ShouldUpdate = true; 218 | } 219 | } 220 | 221 | if (ShouldUpdate) { 222 | Update(); 223 | } 224 | } 225 | const Vector3f& GetPos() 226 | { 227 | return m_pos; 228 | } 229 | const Vector3f& GetTarget() 230 | { 231 | return m_target; 232 | } 233 | const Vector3f& GetUp() 234 | { 235 | return m_up; 236 | } 237 | }; 238 | 239 | 240 | 241 | #endif -------------------------------------------------------------------------------- /Diffuse_Lighting/Diffuse_Lighting/opengl_camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __OPENGL_CAMERA_H 2 | #define __OPENGL_CAMERA_H 3 | #include "opengl_math.h" 4 | #include 5 | 6 | const static float STEP_SCALE = 1.0f; 7 | const static float EDGE_STEP = 0.5f; 8 | const static int MARGIN = 10; 9 | 10 | class Camera 11 | { 12 | private: 13 | Vector3f m_pos; 14 | Vector3f m_target; 15 | Vector3f m_up; 16 | 17 | float m_AngleH; 18 | float m_AngleV; 19 | 20 | bool m_OnUpperEdge; 21 | bool m_OnLowerEdge; 22 | bool m_OnLeftEdge; 23 | bool m_OnRightEdge; 24 | 25 | Vector2i m_mousePos; 26 | 27 | int m_windowWidth; 28 | int m_windowHeight; 29 | public: 30 | Camera(int Window_Width, int Window_Height) 31 | { 32 | m_windowWidth = Window_Width; 33 | m_windowHeight = Window_Height; 34 | LoadVector3(m_pos,0.0f, 0.0f, 0.0f); 35 | LoadVector3(m_target,0.0f, 0.0f, 1.0f); 36 | LoadVector3(m_target,0.0f, 1.0f, 0.0f); 37 | } 38 | Camera(int Window_Width, int Window_Height, const Vector3f& Pos, const Vector3f& Target, const Vector3f& Up) 39 | { 40 | m_windowWidth = Window_Width; 41 | m_windowHeight = Window_Height; 42 | CopyVector3(m_pos, Pos); 43 | CopyVector3(m_target, Target); 44 | NormalizeVector3(m_target); 45 | CopyVector3(m_up, Up); 46 | NormalizeVector3(m_up); 47 | 48 | Init(); 49 | } 50 | void Init() 51 | { 52 | Vector3f HTarget; 53 | LoadVector3(HTarget, m_target[0], 0.0, m_target[2]); 54 | NormalizeVector3(HTarget); 55 | 56 | if (HTarget[2] >= 0.0f) 57 | { 58 | if (HTarget[0] >= 0.0f) 59 | { 60 | m_AngleH = 360.0f - RadToDeg(asin(HTarget[2])); 61 | } 62 | else 63 | { 64 | m_AngleH = 180.0f + RadToDeg(asin(HTarget[2])); 65 | } 66 | } 67 | else 68 | { 69 | if (HTarget[0] >= 0.0f) 70 | { 71 | m_AngleH= RadToDeg(asin(-HTarget[2])); 72 | } 73 | else 74 | { 75 | m_AngleH=90.0f+ RadToDeg(asin(-HTarget[2])); 76 | } 77 | } 78 | 79 | m_AngleV = -RadToDeg(asin(m_target[1])); 80 | 81 | m_OnUpperEdge = false; 82 | m_OnLowerEdge = false; 83 | m_OnLeftEdge = false; 84 | m_OnRightEdge = false; 85 | m_mousePos[0] = m_windowWidth / 2; 86 | m_mousePos[1] = m_windowHeight / 2; 87 | 88 | glutWarpPointer(m_mousePos[0], m_mousePos[1]); 89 | 90 | } 91 | bool OnKeyboard(int key) 92 | { 93 | bool Ret = false; 94 | 95 | switch (key) { 96 | case GLUT_KEY_UP: 97 | { 98 | for (int i = 0; i < 3; i++) 99 | m_pos[i] += m_target[i]; 100 | Ret = TRUE; 101 | } 102 | break; 103 | case GLUT_KEY_DOWN: 104 | { 105 | for (int i = 0; i < 3; i++) 106 | m_pos[i] -= m_target[i]; 107 | Ret = TRUE; 108 | } 109 | break; 110 | case GLUT_KEY_LEFT: 111 | { 112 | Vector3f Left; 113 | CrossProduct3(Left, m_up, m_target); 114 | NormalizeVector3(Left); 115 | for (int i = 0; i < 3; i++) 116 | m_pos[i] += Left[i]; 117 | Ret = TRUE; 118 | } 119 | break; 120 | case GLUT_KEY_RIGHT: 121 | { 122 | Vector3f Right; 123 | CrossProduct3(Right,m_target,m_up); 124 | NormalizeVector3(Right); 125 | for (int i = 0; i < 3; i++) 126 | m_pos[i] += Right[i]; 127 | Ret = TRUE; 128 | } 129 | break; 130 | } 131 | return Ret; 132 | } 133 | void OnMouse(int x, int y) 134 | { 135 | const int DeltaX = x - m_mousePos[0]; 136 | const int DeltaY = y - m_mousePos[1]; 137 | 138 | m_mousePos[0] = x; 139 | m_mousePos[1] = y; 140 | 141 | m_AngleH += (float)DeltaX / 15.0f; 142 | m_AngleV += (float)DeltaY / 15.0f; 143 | 144 | if (DeltaX == 0) { 145 | if (x <= MARGIN) { 146 | // m_AngleH -= 1.0f; 147 | m_OnLeftEdge = true; 148 | } 149 | else if (x >= (m_windowWidth - MARGIN)) { 150 | // m_AngleH += 1.0f; 151 | m_OnRightEdge = true; 152 | } 153 | } 154 | else { 155 | m_OnLeftEdge = false; 156 | m_OnRightEdge = false; 157 | } 158 | if (DeltaY == 0) { 159 | if (y <= MARGIN) { 160 | m_OnUpperEdge = true; 161 | } 162 | else if (y >= (m_windowHeight - MARGIN)) { 163 | m_OnLowerEdge = true; 164 | } 165 | } 166 | else { 167 | m_OnUpperEdge = false; 168 | m_OnLowerEdge = false; 169 | } 170 | Update(); 171 | } 172 | void Update() 173 | { 174 | Vector3f Vaxis; 175 | LoadVector3(Vaxis,0.0f, 1.0f, 0.0f); 176 | // Rotate the view vector by the horizontal angle around the vertical axis 177 | Vector3f View; 178 | LoadVector3(View, 1.0f, 0.0f, 0.0f); 179 | RotateVector3(View, m_AngleH, Vaxis); 180 | NormalizeVector3(View); 181 | 182 | // Rotate the view vector by the vertical angle around the horizontal axis 183 | Vector3f Haxis; 184 | CrossProduct3(Haxis,Vaxis,View); 185 | NormalizeVector3(Haxis); 186 | 187 | RotateVector3(View, m_AngleV, Haxis); 188 | 189 | CopyVector3(m_target, View); 190 | NormalizeVector3(m_target); 191 | 192 | CrossProduct3(m_up, m_target, Haxis); 193 | NormalizeVector3(m_up); 194 | } 195 | void OnRender() 196 | { 197 | bool ShouldUpdate = false; 198 | 199 | if (m_OnLeftEdge) { 200 | m_AngleH -= EDGE_STEP; 201 | ShouldUpdate = true; 202 | } 203 | else if (m_OnRightEdge) { 204 | m_AngleH += EDGE_STEP; 205 | ShouldUpdate = true; 206 | } 207 | 208 | if (m_OnUpperEdge) { 209 | if (m_AngleV > -90.0f) { 210 | m_AngleV -= EDGE_STEP; 211 | ShouldUpdate = true; 212 | } 213 | } 214 | else if (m_OnLowerEdge) { 215 | if (m_AngleV < 90.0f) { 216 | m_AngleV += EDGE_STEP; 217 | ShouldUpdate = true; 218 | } 219 | } 220 | 221 | if (ShouldUpdate) { 222 | Update(); 223 | } 224 | } 225 | const Vector3f& GetPos() 226 | { 227 | return m_pos; 228 | } 229 | const Vector3f& GetTarget() 230 | { 231 | return m_target; 232 | } 233 | const Vector3f& GetUp() 234 | { 235 | return m_up; 236 | } 237 | }; 238 | 239 | 240 | 241 | #endif -------------------------------------------------------------------------------- /Camera_Mouse/code/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "opengl_math.h" 9 | #include "opengl_pipeline.h" 10 | 11 | #define Window_Width 1024 12 | #define Window_Height 768 13 | 14 | using namespace std; 15 | 16 | const char* pVSFileName = "shader.vs"; 17 | const char* pFSFileName = "shader.fs"; 18 | 19 | GLuint VBO, IBO; 20 | GLuint gWVPLocation; 21 | 22 | Camera *GameCamera = NULL; 23 | 24 | PersProjInfo gPersProjInfo; 25 | 26 | 27 | 28 | bool ReadFile(const char* FileName, string &outFile) 29 | { 30 | ifstream f(FileName); 31 | bool ret = FALSE; 32 | 33 | if (f.is_open()) { 34 | string line; 35 | while (getline(f, line)) { 36 | outFile.append(line); 37 | outFile.append("\n"); 38 | } 39 | f.close(); 40 | ret = TRUE; 41 | } 42 | else { 43 | fprintf(stderr, "%s:%b unable to openfile: %s", __FILE__, __LINE__, FileName); 44 | system("pause"); 45 | } 46 | return ret; 47 | } 48 | 49 | static void Render() 50 | { 51 | GameCamera->OnRender(); 52 | 53 | glClear(GL_COLOR_BUFFER_BIT); 54 | 55 | static float Scale = 0.0f; 56 | Scale += 0.1f; 57 | 58 | Pipeline p; 59 | p.Rotate(0.0f, Scale, 0.0f); 60 | p.SetCamera(*GameCamera); 61 | p.SetPerspectiveProj(gPersProjInfo); 62 | 63 | glUniformMatrix4fv(gWVPLocation, 1, GL_FALSE, (const GLfloat*)p.GetWVPTrans()); 64 | 65 | glEnableVertexAttribArray(0); 66 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 67 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 68 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 69 | 70 | glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); 71 | 72 | glDisableVertexAttribArray(0); 73 | 74 | glutSwapBuffers(); 75 | } 76 | 77 | static void Keyboard(int key,int x,int y) 78 | { 79 | GameCamera->OnKeyboard(key); 80 | } 81 | static void Mouse(int x, int y) 82 | { 83 | GameCamera->OnMouse(x, y); 84 | } 85 | static void InitializeGlutCallbacks() 86 | { 87 | glutDisplayFunc(Render); 88 | glutIdleFunc(Render); 89 | 90 | glutPassiveMotionFunc(Mouse); 91 | glutSpecialFunc(Keyboard); 92 | 93 | } 94 | static void AddShader(GLuint ShaderProgram,const char* ShaderText,GLenum ShaderType) 95 | { 96 | GLuint ShaderObj = glCreateShader(ShaderType); 97 | if (!ShaderObj) { 98 | fprintf(stderr, "Error creating shader object"); 99 | system("pause"); 100 | exit(1); 101 | } 102 | 103 | const GLchar* p[1]; 104 | p[0] = ShaderText; 105 | GLint Length[1]; 106 | Length[0]= strlen(ShaderText); 107 | 108 | glShaderSource(ShaderObj,1,p,Length); 109 | glCompileShader(ShaderObj); 110 | 111 | GLint success; 112 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 113 | if (!success) { 114 | GLchar InfoLog[1024]; 115 | glGetShaderInfoLog(ShaderObj, sizeof(InfoLog), NULL, InfoLog); 116 | fprintf(stderr, "Error compiling shader obj:'%s'", InfoLog); 117 | system("pause"); 118 | exit(1); 119 | } 120 | 121 | glAttachShader(ShaderProgram,ShaderObj); 122 | } 123 | static void CreateVertexBuffer() 124 | { 125 | Vector3f Vertices[4]; 126 | LoadVector3(Vertices[0], -1.0f, -1.0f, 0.5773f); 127 | LoadVector3(Vertices[1], 0.0f, -1.0f, -0.5f); 128 | LoadVector3(Vertices[2], 1.0f, -1.0f, 0.5773f); 129 | LoadVector3(Vertices[3], 0.0f, 1.0f, 0.0f); 130 | 131 | glGenBuffers(1, &VBO); 132 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 133 | glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices),Vertices,GL_STATIC_DRAW); 134 | } 135 | static void CreateIndexBuffer() 136 | { 137 | unsigned int Indices[] = 138 | { 139 | 0,3,1, 140 | 1,3,2, 141 | 2,3,0, 142 | 0,1,2 143 | }; 144 | 145 | glGenBuffers(1, &IBO); 146 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 147 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); 148 | 149 | } 150 | 151 | static void CompileShader() 152 | { 153 | GLuint ShaderProgram = glCreateProgram(); 154 | if (!ShaderProgram) { 155 | fprintf(stderr, "Error creating shader program"); 156 | system("pause"); 157 | exit(1); 158 | } 159 | 160 | string vs, fs; 161 | if (!ReadFile(pVSFileName, vs)) { 162 | exit(1); 163 | } 164 | if (!ReadFile(pFSFileName, fs)) { 165 | exit(1); 166 | } 167 | 168 | AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER); 169 | AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER); 170 | 171 | GLchar ErrorLog[1024] = { 0 }; 172 | GLint Success; 173 | 174 | glLinkProgram(ShaderProgram); 175 | glGetProgramiv(ShaderProgram,GL_LINK_STATUS,&Success); 176 | if (!Success) { 177 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 178 | fprintf(stderr, "Error linking shader program: '%s'", ErrorLog); 179 | system("pause"); 180 | exit(1); 181 | } 182 | 183 | glValidateProgram(ShaderProgram); 184 | glGetProgramiv(ShaderProgram,GL_VALIDATE_STATUS,&Success); 185 | if (!Success) { 186 | glGetProgramInfoLog(ShaderProgram,sizeof(ErrorLog),NULL,ErrorLog); 187 | fprintf(stderr, "Valid to shader program:'%s'", ErrorLog); 188 | system("pause"); 189 | exit(1); 190 | } 191 | 192 | glUseProgram(ShaderProgram); 193 | 194 | gWVPLocation = glGetUniformLocation(ShaderProgram, "gWVP"); 195 | assert(gWVPLocation != 0xFFFFFFFF); 196 | } 197 | int main(int argc,char **argv) 198 | { 199 | glutInit(&argc, argv); 200 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 201 | glutInitWindowPosition(10, 10); 202 | glutInitWindowSize(Window_Width, Window_Height); 203 | glutCreateWindow("Camera Mouse"); 204 | glutGameModeString("1920x1200@32"); 205 | glutEnterGameMode(); 206 | 207 | InitializeGlutCallbacks(); 208 | 209 | GLenum res = glewInit(); 210 | if (res != GLEW_OK) { 211 | fprintf(stderr, "Error about use glew"); 212 | system("pause"); 213 | exit(1); 214 | } 215 | 216 | printf("GL version %s\n", glGetString(GL_VERSION)); 217 | 218 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 219 | 220 | CreateVertexBuffer(); 221 | CreateIndexBuffer(); 222 | 223 | CompileShader(); 224 | 225 | gPersProjInfo.FOV = 60.0f; 226 | gPersProjInfo.Height = Window_Height; 227 | gPersProjInfo.Width = Window_Width; 228 | gPersProjInfo.zNear = 1.0f; 229 | gPersProjInfo.zFar = 100.0f; 230 | 231 | Vector3f CameraPos, CameraTarget, CameraUp; 232 | LoadVector3(CameraPos, 0.0f, 0.0f, -4.0f); 233 | LoadVector3(CameraTarget, 0.0f, 0.0f, 1.0f); 234 | LoadVector3(CameraUp, 0.0f, 1.0f, 0.0f); 235 | GameCamera = new Camera(Window_Width, Window_Height, CameraPos, CameraTarget, CameraUp); 236 | 237 | glutMainLoop(); 238 | 239 | return 0; 240 | 241 | 242 | } -------------------------------------------------------------------------------- /Point_Lighting/code/shader.fs: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | const int MAX_POINT_LIGHTS=2; 4 | in vec2 TexCoord0; 5 | in vec3 Normal0; 6 | in vec3 WorldPos0; 7 | 8 | out vec4 FragColor; 9 | 10 | struct BaseLight 11 | { 12 | vec3 Color; 13 | float AmbientIntensity; 14 | float DiffuseIntensity; 15 | }; 16 | 17 | struct DirectionalLight 18 | { 19 | BaseLight Base; 20 | vec3 Direction; 21 | }; 22 | struct Attenuation 23 | { 24 | float Constant; 25 | float Linear; 26 | float Exp; 27 | }; 28 | 29 | struct PointLight 30 | { 31 | BaseLight Base; 32 | vec3 Position; 33 | Attenuation Atten; 34 | }; 35 | 36 | uniform int gNumPointLights; 37 | uniform DirectionalLight gDirectionalLight; 38 | uniform PointLight gPointLights[MAX_POINT_LIGHTS]; 39 | uniform float gMatSpecularIntensity; 40 | uniform float gSpecularPower; 41 | uniform vec3 gEyeWorldPos; 42 | 43 | uniform sampler2D gSampler; 44 | 45 | vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal) 46 | { 47 | vec4 AmbientColor = vec4(Light.Color * Light.AmbientIntensity, 1.0f); 48 | float DiffuseFactor = dot(Normal, -LightDirection); 49 | 50 | vec4 DiffuseColor = vec4(0, 0, 0, 0); 51 | vec4 SpecularColor = vec4(0, 0, 0, 0); 52 | 53 | if (DiffuseFactor > 0) { 54 | DiffuseColor = vec4(Light.Color * Light.DiffuseIntensity * DiffuseFactor, 1.0f); 55 | 56 | vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos0); 57 | vec3 LightReflect = normalize(reflect(LightDirection, Normal)); 58 | float SpecularFactor = dot(VertexToEye, LightReflect); 59 | if (SpecularFactor > 0) { 60 | SpecularFactor = pow(SpecularFactor, gSpecularPower); 61 | SpecularColor = vec4(Light.Color * gMatSpecularIntensity * SpecularFactor, 1.0f); 62 | } 63 | } 64 | 65 | return (AmbientColor + DiffuseColor + SpecularColor); 66 | } 67 | 68 | vec4 CalcDirectionalLight(vec3 Normal) 69 | { 70 | return CalcLightInternal(gDirectionalLight.Base, gDirectionalLight.Direction, Normal); 71 | } 72 | 73 | vec4 CalcPointLight(int Index, vec3 Normal) 74 | { 75 | vec3 LightDirection = WorldPos0 - gPointLights[Index].Position; 76 | float Distance = length(LightDirection); 77 | LightDirection = normalize(LightDirection); 78 | 79 | vec4 Color = CalcLightInternal(gPointLights[Index].Base, LightDirection, Normal); 80 | float Attenuation = gPointLights[Index].Atten.Constant + 81 | gPointLights[Index].Atten.Linear * Distance + 82 | gPointLights[Index].Atten.Exp * Distance * Distance; 83 | 84 | return Color / Attenuation; 85 | } 86 | 87 | void main() 88 | { 89 | vec3 Normal = normalize(Normal0); 90 | vec4 TotalLight = CalcDirectionalLight(Normal); 91 | 92 | for (int i = 0 ; i < gNumPointLights ; i++) { 93 | TotalLight += CalcPointLight(i, Normal); 94 | } 95 | 96 | FragColor = texture2D(gSampler, TexCoord0.xy) * TotalLight; 97 | } -------------------------------------------------------------------------------- /Spot_Lighting/OpenGL学习之路19---- 聚光灯光源.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步开始,记录学习历程 2 | 3 | ## 聚光灯光源 4 | 聚光灯光源是结合了平行光方向的特性和点光源随距离衰减的特性,并且离光源越远,照亮圆形区域越大,即光源呈锥形。 在现实中一个例子为手电筒。 5 | 6 | 如图所示,即为聚光灯光源的锥形效果: 7 | 8 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Spot_Lighting/image/1.png) 9 | 10 | - L为光源方向 11 | - 我们只想让光源照亮红色夹角之间的区域,即2α的角度 12 | - V为光源到某个像素的向量 13 | - 如果β大于α则该像素不位于照亮区域内 14 | - 为了得到夹角的余弦值,可以使用向量间点积来得到 15 | 16 | 如果按照在区域内就能照亮,区域外就不能,则会在照亮区域和不照亮区域之间形成很明显的边界,没有过渡的效果。 17 | 18 | 聚光灯光源从照亮区域的中心向边缘慢慢衰减,如果L和V向量重合时,夹角为0,点积(夹角余弦值)为1,随着夹角增大,余弦值减小。但是夹角从0到一个比较小的角,余弦值变化很缓慢,衰减不明显。 19 | 20 | 比如聚光灯夹角为20°,余弦值为0.939,[0.939,1]这个变化范围太小,不好作为衰减参数,应该使得衰减参数范围为[0,1],所以把余弦值范围映射到[0,1]方法如下: 21 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Spot_Lighting/image/2.png) 22 | 23 | 计算原理即为:使用大范围——[0,1],和小范围——[cosα,1]的比例——(1-0)/(1-cosα)=d 24 | 25 | 对小范围进行映射扩张,如果是α<=β<=1 —— 1-(1-cosβ)*((1-0)/(1-cosα)) 26 | 27 | ## 实现聚光灯光源 28 | 29 | 首先定义聚光灯光源的结构体,聚光灯光源集成点光源的结构体,并且多出了光源的方向向量和照亮的夹角两个参数 30 | opengl_light.h: 31 | ``` 32 | struct SpotLight : public PointLight 33 | { 34 | Vector3f Direction; 35 | float Cutoff; 36 | 37 | SpotLight() 38 | { 39 | LoadVector3(Direction,0.0f, 0.0f, 0.0f); 40 | Cutoff = 0.0f; 41 | } 42 | }; 43 | ``` 44 | 之后在Light类中加入用于获取聚光灯光源Uniform变量的索引 45 | opengl_light.h: 46 | ``` 47 | GLuint m_numSpotLightsLocation; 48 | struct { 49 | GLuint Color; 50 | GLuint AmbientIntensity; 51 | GLuint DiffuseIntensity; 52 | GLuint Position; 53 | GLuint Direction; 54 | GLuint Cutoff; 55 | struct { 56 | GLuint Constant; 57 | GLuint Linear; 58 | GLuint Exp; 59 | } Atten; 60 | } m_spotLightsLocation[MAX_SPOT_LIGHTS]; 61 | ``` 62 | 在Light类中获取Uniform变量的方法中加入获取聚光灯光源的代码 63 | opengl_light.cpp: 64 | ``` 65 | for (unsigned int i = 0; i < ARRAY_SIZE_IN_ELEMENTS(m_spotLightsLocation); i++) { 66 | char Name[128]; 67 | memset(Name, 0, sizeof(Name)); 68 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Base.Color", i); 69 | m_spotLightsLocation[i].Color = glGetUniformLocation(m_shaderProg,Name); 70 | 71 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Base.AmbientIntensity", i); 72 | m_spotLightsLocation[i].AmbientIntensity = glGetUniformLocation(m_shaderProg, Name); 73 | 74 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Position", i); 75 | m_spotLightsLocation[i].Position = glGetUniformLocation(m_shaderProg, Name); 76 | 77 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Direction", i); 78 | m_spotLightsLocation[i].Direction = glGetUniformLocation(m_shaderProg, Name); 79 | 80 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Cutoff", i); 81 | m_spotLightsLocation[i].Cutoff = glGetUniformLocation(m_shaderProg, Name); 82 | 83 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Base.DiffuseIntensity", i); 84 | m_spotLightsLocation[i].DiffuseIntensity = glGetUniformLocation(m_shaderProg, Name); 85 | 86 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Atten.Constant", i); 87 | m_spotLightsLocation[i].Atten.Constant = glGetUniformLocation(m_shaderProg, Name); 88 | 89 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Atten.Linear", i); 90 | m_spotLightsLocation[i].Atten.Linear = glGetUniformLocation(m_shaderProg, Name); 91 | 92 | snprintf(Name, sizeof(Name), "gSpotLights[%d].Base.Atten.Exp", i); 93 | m_spotLightsLocation[i].Atten.Exp = glGetUniformLocation(m_shaderProg, Name); 94 | 95 | if (m_spotLightsLocation[i].Color == INVALID_UNIFORM_LOCATION || 96 | m_spotLightsLocation[i].AmbientIntensity == INVALID_UNIFORM_LOCATION || 97 | m_spotLightsLocation[i].Position == INVALID_UNIFORM_LOCATION || 98 | m_spotLightsLocation[i].Direction == INVALID_UNIFORM_LOCATION || 99 | m_spotLightsLocation[i].Cutoff == INVALID_UNIFORM_LOCATION || 100 | m_spotLightsLocation[i].DiffuseIntensity == INVALID_UNIFORM_LOCATION || 101 | m_spotLightsLocation[i].Atten.Constant == INVALID_UNIFORM_LOCATION || 102 | m_spotLightsLocation[i].Atten.Linear == INVALID_UNIFORM_LOCATION || 103 | m_spotLightsLocation[i].Atten.Exp == INVALID_UNIFORM_LOCATION) { 104 | return false; 105 | } 106 | } 107 | ``` 108 | 在主程序中的主渲染函数Render()中增加两个聚光灯光源 109 | main.cpp: 110 | 111 | ``` 112 | SpotLight sl[2]; 113 | sl[0].DiffuseIntensity = 0.9f; 114 | LoadVector3(sl[0].Color,1.0f, 1.0f, 1.0f); 115 | CopyVector3(sl[0].Position, pGameCamera->GetPos()); 116 | CopyVector3(sl[0].Direction, pGameCamera->GetTarget()); 117 | sl[0].Attenuation.Linear = 0.1f; 118 | sl[0].Cutoff = 10.0f; 119 | 120 | sl[1].DiffuseIntensity = 0.9f; 121 | LoadVector3(sl[1].Color,1.0f, 1.0f, 1.0f); 122 | LoadVector3(sl[1].Position,5.0f, 3.0f, 10.0f); 123 | LoadVector3(sl[1].Direction,0.0f, -1.0f, 0.0f); 124 | sl[1].Attenuation.Linear = 0.1f; 125 | sl[1].Cutoff = 20.0f; 126 | pLight->SetSpotLights(2, sl); 127 | ``` 128 | - 可以看到增加了两个聚光灯光源 129 | - 第一个位置在相机位置,随着相机移动而移动,从相机朝向射出,漫射光强度为0.9,线性衰弱为0.1,照射角度为10度 130 | - 第二个位置固定在(5.0,3.0,10.0),方向朝(0.0,-1.0f,0.0),即在我们场景的中心上方朝正下方照射,照射角度为20度 131 | 132 | 顶点着色器没有变化,片元着色器接收主程序传来的聚光灯光源的Uniform变量来计算得到最终的像素颜色。 133 | 134 | 先是在片元着色器中定义了聚光灯光源的结构体,包括一个点光源的所有参数、一个方向向量和一个照射范围角度 135 | shader.fs: 136 | ``` 137 | struct SpotLight 138 | { 139 | PointLight Base; 140 | vec3 Direction; 141 | float Cutoff; 142 | }; 143 | ``` 144 | 之后声明Uniform变量,接收从主程序传来的聚光灯光源个数和聚光灯光源参数 145 | shader.fs: 146 | ``` 147 | uniform int gNumSpotLights; 148 | uniform SpotLight gSpotLights[MAX_SPOT_LIGHTS]; 149 | ``` 150 | 增加计算聚光灯光源颜色的函数CalcSpotLight() 151 | shader.fs: 152 | ``` 153 | vec4 CalcSpotLight(SpotLight l, vec3 Normal) 154 | { 155 | vec3 LightToPixel = normalize(WorldPos0 - l.Base.Position); 156 | float SpotFactor = dot(LightToPixel, l.Direction); 157 | if (SpotFactor > l.Cutoff) { 158 | vec4 Color = CalcPointLight(l.Base, Normal); 159 | return Color * (1.0 - (1.0 - SpotFactor) * 1.0/(1.0 - l.Cutoff)); 160 | } 161 | else { 162 | return vec4(0,0,0,0); 163 | } 164 | } 165 | ``` 166 | - 计算出聚光灯光源到像素点位置的方向向量LightToPixel(单位化) 167 | - 将聚光灯光源照射方向和光源到像素的方向向量点积得到夹角的余弦值SpotFactor 168 | - 如果夹角小于照射角范围,即余弦值大于照射角余弦值(SpotFactor > l.Cutoff)则计算聚光灯颜色,如果夹角大于照射范围,则聚光灯光源不影响,即颜色为(0,0,0,0) 169 | - 先用计算点光源光照颜色函数CalcPointLight()计算出光源到像素点距离后的衰减颜色 170 | - 再根据上面提到的公式,用光源到像素方向向量和光源照射方向的夹角计算出最终衰减颜色Color\*SpotFactor*1/(1-l.Cutoff) 171 | 172 | 最后在main()函数中根据纹理颜色、平行光颜色、点光源颜色和聚光灯光源颜色共同决定像素颜色 173 | shader.fs: 174 | ``` 175 | void main() 176 | { 177 | vec3 Normal = normalize(Normal0); 178 | vec4 TotalLight = CalcDirectionalLight(Normal); 179 | for (int i = 0 ; i < gNumPointLights ; i++) { 180 | TotalLight += CalcPointLight(gPointLights[i], Normal); 181 | } 182 | for (int i = 0 ; i < gNumSpotLights ; i++) { 183 | TotalLight += CalcSpotLight(gSpotLights[i], Normal); 184 | 185 | } 186 | FragColor = texture2D(gSampler, TexCoord0.xy) * TotalLight; 187 | 188 | } 189 | ``` 190 | ## 运行结果 191 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Spot_Lighting/image/3.png) 192 | 可以看到一个正中心的聚光灯光源照射和一个随着相机移动而移动的聚光灯光源 193 | -------------------------------------------------------------------------------- /Specular_lighting/OpenGL学习之路17---- 镜面反射光.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | 3 | 之前完成环境光和漫射光的学习。环境光的计算只由光强来决定,场景中所有位置是同一亮度;漫射光的计算由光强和光的方向一同决定,相关博文如下: 4 | 5 | [OpenGL学习之路15----环境光](https://blog.csdn.net/zach_z/article/details/80188764) 6 | 7 | [OpenGL学习之路16---- 漫射光](https://blog.csdn.net/Zach_z/article/details/80199389) 8 | 9 | ## 镜面反射光 10 | 镜面反射光是当光线以一定的角度照射到物体表面后,从法线的另一侧堆成的角度反射出去的光线 11 | 12 | 所以这个光线就与相机的位置有关,如果相机的位置正好在反射光线上则能感受到反射光线的照射,如果相机离反射光线有一定的角度则感受到反射光线的照射效果稍弱。 13 | 14 | 观察下图: 15 | 16 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Specular_lighting/image/1.png) 17 | 18 | - I:入射光 19 | - N:表面法向量 20 | - R:反射光,与I关于法线对称,方向相反 21 | - V:入射点到相机视点的向量,代表观察视线 22 | - α:反射光R和观察视线V的夹角 23 | 24 | 由图可知当α(即反射光和观察视线的夹角)为0时,反射光的强度最大;随着α的角度不断增大,反射光的强度逐渐减小,当α为90°(即反射光和观察者视线垂直)时,感受不到反射光。 25 | 26 | 引入α的余弦值,正好可以表示这种关系: 27 | 28 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Specular_lighting/image/2.png) 29 | 30 | - 当α为0°时,α的余弦值为1,此时反射光强最大 31 | - 随着α值逐渐增大,余弦值逐渐减小 32 | - α为90°时,余弦值为0,继续增大值为负数则判定光强也为0 33 | 34 | **计算夹角α:** 35 | 夹角α可以用,R(反射光)和V(观察视线)的点积来得到,关于点积的相关知识之前相机空间的博文中有讲到: 36 | [OpenGL学习之路11----相机空间](https://blog.csdn.net/zach_z/article/details/80102219) 37 | 38 | **计算观察视线V:** 可以通过相机位置与入射点位置的差得到。(单位化) 39 | 40 | **计算反射光R:** 41 | 42 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Specular_lighting/image/3.png) 43 | 44 | 根据图中所示,我们可以引入一个向量W来计算反射光R: 45 | ``` 46 | R = I + W 47 | ``` 48 | 而W的长度即为,两倍的向量I在单位法向量N的相反向量(-N)上的投影的长度,而这个投影长度可以使用点积得到,W的方向跟单位法向量相同,所以向量W: 49 | ``` 50 | W = 2 * N * (-N · I) 51 | ``` 52 | 所以,反射光结合两个等式可以得到: 53 | ``` 54 | R = I + 2 * N * (-N · I) 55 | = I - 2 * N * ( N · I) 56 | ``` 57 | GLSL中提供了一个内部函数reflect(),用来做上述运算,即求一个向量经过法线堆成后的反射向量 58 | 59 | **镜面反射的最终公式:** 60 | 61 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Specular_lighting/image/4.png) 62 | 63 | - 将光的颜色与物体表面颜色相乘 64 | - M:代表材料的镜面反射强度,这是由材料的物理特性决定的(比如木头就为0,金属的话这个值就大) 65 | - p:镜面发光参数,用来增强反射光区域边缘强度,这个值也是材料物体特性决定的 66 | 67 | ## 镜面反射光的实现 68 | 在之前平行光结构体的基础上,增加两个属性,分别代表我们的镜面反射强度M和镜面发光参数p: 69 | 70 | ``` 71 | struct DirectionalLight 72 | { 73 | Vector3f Color; 74 | float AmbientIntensity; 75 | Vector3f Direction; 76 | float DiffuseIntensity; 77 | 78 | float SpecularIntensity; // 镜面反射强度 79 | float SpecularPower; //镜面发光参数 80 | }; 81 | 82 | ``` 83 | 定义三个GLuint类型的全局变量,以便获得Uniform变量的Index索引: 84 | 85 | ``` 86 | GLuint m_eyeWorldPosLocation; //相机视点 87 | GLuint m_matSpecularIntensityLocation; // 镜面反射强度 88 | GLuint m_matSpecularPowerLocation; //镜面发光参数 89 | ``` 90 | 获取Uniform变量的索引: 91 | 92 | ``` 93 | m_eyeWorldPosLocation = glGetUniformLocation(ShaderProgram,"gEyeWorldPos"); 94 | m_matSpecularIntensityLocation = glGetUniformLocation(ShaderProgram,"gDirectionalLight.SpecularIntensity"); 95 | m_matSpecularPowerLocation = glGetUniformLocation(ShaderProgram, "gDirectionalLight.SpecularPower"); 96 | ``` 97 | 在主函数中给镜面反射强度和镜面发光参数赋初值: 98 | 99 | ``` 100 | m_directionalLight.SpecularIntensity = 1.0f; 101 | m_directionalLight.SpecularPower = 32.0f; 102 | ``` 103 | 在主渲染函数Render()中传递三个Uniform变量的值,分别是相机视点的位置,镜面反射强度和镜面发光参数: 104 | 105 | ``` 106 | Vector3f EyeWorldPos; 107 | CopyVector3(EyeWorldPos, pGameCamera->GetPos()); 108 | glUniform3f(m_eyeWorldPosLocation, EyeWorldPos[0], EyeWorldPos[1], EyeWorldPos[2]); 109 | glUniform1f(m_matSpecularIntensityLocation, m_directionalLight.SpecularIntensity); 110 | glUniform1f(m_matSpecularPowerLocation, m_directionalLight.SpecularPower); 111 | ``` 112 | 在顶点着色器中计算每个像素的世界坐标位置并作为输出变量传递到片元着色器中,**shader.vs:** 113 | 114 | ``` 115 | #version 330 116 | 117 | layout (location=0) in vec3 Position; 118 | layout (location=1) in vec2 TexCoord; 119 | layout (location=2) in vec3 Normal; 120 | 121 | uniform mat4 gWVP; 122 | uniform mat4 gWorld; 123 | 124 | out vec2 TexCoord0; 125 | out vec3 Normal0; 126 | out vec3 WorldPos0; 127 | 128 | void main() 129 | { 130 | gl_Position = gWVP*vec4(Position,1.0); 131 | TexCoord0 = TexCoord; 132 | Normal0= (gWorld * vec4(Normal,0.0)).xyz; 133 | WorldPos0 = (gWorld * vec4(Position, 1.0)).xyz; 134 | } 135 | ``` 136 | main()函数中的最后一句即为计算,每个像素点的世界坐标位置 137 | 138 | 最后在片元着色器中计算最终的颜色。shader.fs: 139 | 140 | ``` 141 | #version 330 142 | 143 | in vec2 TexCoord0; 144 | in vec3 Normal0; 145 | in vec3 WorldPos0; 146 | 147 | out vec4 FragColor; 148 | 149 | struct DirectionalLight 150 | { 151 | vec3 Color; 152 | float AmbientIntensity; 153 | float DiffuseIntensity; 154 | vec3 Direction; 155 | float SpecularIntensity; // 镜面反射强度 156 | float SpecularPower; //镜面发光参数 157 | }; 158 | 159 | uniform DirectionalLight gDirectionalLight; 160 | uniform sampler2D gSampler; 161 | uniform vec3 gEyeWorldPos; 162 | 163 | 164 | void main() 165 | { 166 | vec4 AmbientColor = vec4(gDirectionalLight.Color * gDirectionalLight.AmbientIntensity, 1.0f); 167 | vec3 LightDirection = -gDirectionalLight.Direction; 168 | vec3 Normal = normalize(Normal0); 169 | 170 | float DiffuseFactor = dot(Normal, LightDirection); 171 | 172 | vec4 DiffuseColor = vec4(0, 0, 0, 0); 173 | vec4 SpecularColor = vec4(0, 0, 0, 0); 174 | 175 | if (DiffuseFactor > 0) { 176 | DiffuseColor = vec4(gDirectionalLight.Color * gDirectionalLight.DiffuseIntensity * DiffuseFactor, 1.0f); 177 | 178 | vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos0); 179 | vec3 LightReflect = normalize(reflect(gDirectionalLight.Direction, Normal)); 180 | float SpecularFactor = dot(VertexToEye, LightReflect); 181 | if (SpecularFactor > 0) { 182 | SpecularFactor = pow(SpecularFactor, gDirectionalLight.SpecularPower); 183 | SpecularColor = vec4(gDirectionalLight.Color * gDirectionalLight.SpecularIntensity * SpecularFactor, 1.0f); 184 | } 185 | } 186 | FragColor = texture2D(gSampler, TexCoord0.xy) * (AmbientColor + DiffuseColor + SpecularColor); 187 | } 188 | ``` 189 | - 环境光(AmbientColor)计算没有发生变化,先计算出环境光颜色 190 | - 再对入射光线取反,这里原因在上一节有讲,因为比如垂直入射到物体表面,入射角α应该为0°,然而法向量竖直向上是180°所以要取反一下 191 | - 对每个像素的顶点法线(Normal0)单位化,因为经过顶点着色器计算后不是一定是单位向量 192 | - 漫射光计算也跟上一节一样,计算出入射角与法线的角度余弦值(DiffuseFactor),通过入射光向量和顶点法向量的点积得到 193 | - 如果入射角小于90°即(余弦值DiffuseFactor大于0),则计算出漫射光颜色(DiffuseColor),即光的颜色乘慢射光强乘入射角余弦值; 入射角如果大于90°则光线没有影响即光颜色为(0,0,0,0) 194 | - **按照前面推理的镜面反射光计算方法来计算镜面反射光:** 195 | 196 | 得到观察视线的单位向量:相机位置-像素位置,再单位化: 197 | ``` 198 | vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos0); 199 | ``` 200 | 得到入射光经过平面得到的反射光向量:使用GLSL内置函数得到入射光与法向量对应的反射光向量,再单位化: 201 | ``` 202 | vec3 LightReflect = normalize(reflect(gDirectionalLight.Direction, Normal)); 203 | ``` 204 | 得到观察视线与反射光之间的夹角:点积后得到: 205 | ``` 206 | float SpecularFactor = dot(VertexToEye, LightReflect); 207 | ``` 208 | 如果夹角小于90°,则正面镜面反射光有效,此时根据上面的镜面反射光计算公式来得出镜面反射光颜色: 209 | ``` 210 | if (SpecularFactor > 0) { 211 | SpecularFactor = pow(SpecularFactor, gDirectionalLight.SpecularPower); 212 | SpecularColor = vec4(gDirectionalLight.Color * gDirectionalLight.SpecularIntensity * SpecularFactor, 1.0f); 213 | } 214 | ``` 215 | - 最后根据纹理颜色,环境光颜色、漫射光颜色和镜面反射光颜色共同得出像素颜色: 216 | ``` 217 | FragColor = texture2D(gSampler, TexCoord0.xy) * (AmbientColor + DiffuseColor + SpecularColor); 218 | ``` 219 | ## 运行结果 220 | 在之前调环境光和漫射光的基础上,增加了调镜面反射强度和镜面发光参数的按键,来观察变化 221 | 222 | 光从(1,0,0)入射时: 223 | 224 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Specular_lighting/image/5.png) 225 | 226 | 光从(1,1,0)入射时: 227 | 228 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Specular_lighting/image/6.png) 229 | 230 | 并且也可以通过调整镜面发光参数可以看出来,镜面发光参数越大,反射的面积集中,镜面发光参数越小,反射的光越发散 231 | -------------------------------------------------------------------------------- /Texture/Texture/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include ] 4 | #include 5 | #include 6 | #include 7 | 8 | #include "opengl_math.h" 9 | #include "opengl_pipeline.h" 10 | #include "opengl_texture.h" 11 | 12 | #define Window_Width 1024 13 | #define Window_Height 768 14 | using namespace std; 15 | 16 | GLuint VBO,IBO; 17 | GLuint gWVPLocation; 18 | GLuint gSampler; 19 | Camera* GameCamera = NULL; 20 | Texture* pTexture = NULL; 21 | PersProjInfo gPersProjInfo; 22 | 23 | const char* pVSFileName = "shader.vs"; 24 | const char* pFSFileName = "shader.fs"; 25 | 26 | struct Vertex 27 | { 28 | Vector3f m_pos; 29 | Vector2f m_tex; 30 | 31 | Vertex() {} 32 | 33 | Vertex(Vector3f pos, Vector2f tex) 34 | { 35 | CopyVector3(m_pos, pos); 36 | CopyVector2(m_tex, tex); 37 | } 38 | }; 39 | 40 | static void Render() 41 | { 42 | GameCamera->OnRender(); 43 | glClear(GL_COLOR_BUFFER_BIT); 44 | 45 | static float Scale = 0.0f; 46 | Scale += 0.1f; 47 | 48 | Pipeline p; 49 | p.Rotate(0.0f, Scale, 0.0f); 50 | p.WorldPos(0.0f, 0.0f, 3.0f); 51 | p.SetCamera(*GameCamera); 52 | p.SetPerspectiveProj(gPersProjInfo); 53 | 54 | glUniformMatrix4fv(gWVPLocation, 1, GL_FALSE, (const GLfloat*)p.GetWVPTrans()); 55 | 56 | glEnableVertexAttribArray(0); 57 | glEnableVertexAttribArray(1); 58 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 59 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); 60 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)sizeof(Vector3f)); 61 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 62 | pTexture->Bind(GL_TEXTURE0); 63 | 64 | glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); 65 | 66 | glDisableVertexAttribArray(0); 67 | glDisableVertexAttribArray(1); 68 | 69 | glutSwapBuffers(); 70 | } 71 | static void Keyboard(int key, int x, int y) 72 | { 73 | GameCamera->OnKeyboard(key); 74 | } 75 | static void Mouse(int x, int y) 76 | { 77 | GameCamera->OnMouse(x, y); 78 | } 79 | static void InitializeCallbacks() 80 | { 81 | glutDisplayFunc(Render); 82 | glutIdleFunc(Render); 83 | 84 | glutPassiveMotionFunc(Mouse); 85 | glutSpecialFunc(Keyboard); 86 | } 87 | static void CreateVertexBuffer() 88 | { 89 | Vector3f Vertices3f[4]; 90 | Vector2f Vertices2f[4]; 91 | LoadVector3(Vertices3f[0], -1.0f, -1.0f, 0.5773f); LoadVector2(Vertices2f[0], 0.0f, 0.0f); 92 | LoadVector3(Vertices3f[1], 0.0f, -1.0f, -1.15475f); LoadVector2(Vertices2f[1], 0.5f, 0.0f); 93 | LoadVector3(Vertices3f[2], 1.0f, -1.0f, 0.5773f); LoadVector2(Vertices2f[2], 1.0f, 0.0f); 94 | LoadVector3(Vertices3f[3], 0.0f, 1.0f, 0.0f); LoadVector2(Vertices2f[3], 0.5f, 1.0f); 95 | Vertex Vertices[4] = { Vertex(Vertices3f[0],Vertices2f[0]), 96 | Vertex(Vertices3f[1],Vertices2f[1]), 97 | Vertex(Vertices3f[2],Vertices2f[2]), 98 | Vertex(Vertices3f[3],Vertices2f[3]) }; 99 | 100 | glGenBuffers(1, &VBO); 101 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 102 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 103 | } 104 | static void CreateIndexBuffer() 105 | { 106 | unsigned int Indices[] = 107 | { 108 | 0,3,1, 109 | 1,3,2, 110 | 2,3,0, 111 | 0,1,2 112 | }; 113 | 114 | glGenBuffers(1, &IBO); 115 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 116 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); 117 | } 118 | bool ReadFile(const char* FileName, string &outFile) 119 | { 120 | ifstream f(FileName); 121 | bool ret = FALSE; 122 | 123 | if (f.is_open()) { 124 | string line; 125 | while (getline(f, line)) { 126 | outFile.append(line); 127 | outFile.append("\n"); 128 | } 129 | f.close(); 130 | ret = TRUE; 131 | } 132 | else { 133 | fprintf(stderr, "%s : %d unable to read file: %s", __FILE__, __LINE__, FileName); 134 | system("pause"); 135 | exit(1); 136 | } 137 | return ret; 138 | } 139 | static void Addshader(GLuint ShaderProgram,const char* ShaderText,GLenum ShaderType) 140 | { 141 | GLuint ShaderObj = glCreateShader(ShaderType); 142 | if (!ShaderObj) { 143 | fprintf(stderr, "Error creating shder object"); 144 | system("pause"); 145 | exit(1); 146 | } 147 | 148 | const GLchar* p[1]; 149 | p[0] = ShaderText; 150 | GLint Length[1]; 151 | Length[0] = strlen(ShaderText); 152 | glShaderSource(ShaderObj, 1, p, Length); 153 | glCompileShader(ShaderObj); 154 | 155 | GLint success; 156 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 157 | if (!success) { 158 | GLchar InfoLog[1024]; 159 | glGetShaderInfoLog(ShaderObj,sizeof(InfoLog),NULL,InfoLog); 160 | fprintf(stderr, "Error compiling shader object: '%s'", InfoLog); 161 | system("pause"); 162 | exit(1); 163 | } 164 | 165 | glAttachShader(ShaderProgram, ShaderObj); 166 | 167 | } 168 | static void CompileShader() 169 | { 170 | GLuint ShaderProgram = glCreateProgram(); 171 | if (!ShaderProgram) { 172 | fprintf(stderr, "Error creating shader program"); 173 | system("pause"); 174 | exit(1); 175 | } 176 | 177 | string vs, fs; 178 | 179 | if (!ReadFile(pVSFileName, vs)) { 180 | exit(1); 181 | } 182 | if (!ReadFile(pFSFileName, fs)) { 183 | exit(1); 184 | } 185 | 186 | Addshader(ShaderProgram,vs.c_str(),GL_VERTEX_SHADER); 187 | Addshader(ShaderProgram,fs.c_str(),GL_FRAGMENT_SHADER); 188 | 189 | GLchar ErrorLog[1024] = { 0 }; 190 | GLint Success; 191 | 192 | glLinkProgram(ShaderProgram); 193 | glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success); 194 | if (!Success) { 195 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 196 | fprintf(stderr, "Error linking shader program: '%s'", ErrorLog); 197 | system("pause"); 198 | exit(1); 199 | } 200 | 201 | glValidateProgram(ShaderProgram); 202 | glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success); 203 | if (!Success) { 204 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 205 | fprintf(stderr, "Valied to shader program:'%s'", ErrorLog); 206 | system("pause"); 207 | exit(1); 208 | } 209 | 210 | glUseProgram(ShaderProgram); 211 | 212 | gWVPLocation = glGetUniformLocation(ShaderProgram, "gWVP"); 213 | assert(gWVPLocation != 0xFFFFFFFF); 214 | gSampler = glGetUniformLocation(ShaderProgram, "gSampler"); 215 | assert(gSampler != 0xFFFFFFFF); 216 | } 217 | int main(int argc, char** argv) 218 | { 219 | glutInit(&argc, argv); 220 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 221 | glutInitWindowPosition(10, 10); 222 | glutInitWindowSize(Window_Width, Window_Height); 223 | glutCreateWindow("Texture"); 224 | glutGameModeString("1280x1024@32"); 225 | InitializeCallbacks(); 226 | 227 | GLenum res = glewInit(); 228 | if (res != GLEW_OK) { 229 | fprintf(stderr, "unable to init glew"); 230 | system("pause"); 231 | exit(1); 232 | } 233 | 234 | printf("GL version : '%s'", glGetString(GL_VERSION)); 235 | 236 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 237 | glFrontFace(GL_CW); 238 | glCullFace(GL_BACK); 239 | glEnable(GL_CULL_FACE); 240 | 241 | CreateVertexBuffer(); 242 | CreateIndexBuffer(); 243 | 244 | CompileShader(); 245 | 246 | glUniform1i(gSampler, 0); 247 | 248 | pTexture = new Texture(GL_TEXTURE_2D, "test.png"); 249 | 250 | if (!pTexture->Load()) { 251 | return 1; 252 | } 253 | 254 | gPersProjInfo.FOV = 60.0f; 255 | gPersProjInfo.Height = Window_Height; 256 | gPersProjInfo.Width = Window_Width; 257 | gPersProjInfo.zNear = 1.0f; 258 | gPersProjInfo.zFar = 100.0f; 259 | 260 | Vector3f CameraPos, CameraTarget, CameraUp; 261 | LoadVector3(CameraPos, 0.0f, 0.0f, -4.0f); 262 | LoadVector3(CameraTarget, 0.0f, 0.0f, 1.0f); 263 | LoadVector3(CameraUp, 0.0f, 1.0f, 0.0f); 264 | GameCamera = new Camera(Window_Width, Window_Height, CameraPos, CameraTarget, CameraUp); 265 | 266 | glutMainLoop(); 267 | 268 | return 0; 269 | } -------------------------------------------------------------------------------- /Uniform/OpenGL学习之路5----使用一致变量(Uniform Variables).md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | 3 | ## 一、uniform 存储限制符 4 | - 在着色器运行之前,uniform修饰符可以指定一个在应用程序中设置好的变量,**它不会在图元处理过程中发生变化** 5 | - uniform变量在所有可用的着色阶段之间都是共享的,**它必须定义为全局变量** 6 | - 任何类型的变量(包括结构体和数组)都可以设置为uniform变量 7 | - 着色器无法写入到uniform变量,也无法改变它的值 8 | 9 | example:在着色器中声明一个uniform变量 10 | ``` 11 | uniform float gScale; 12 | ``` 13 | 在着色器中可以根据名字引用gScale这个变量,如果需要在用户应用程序中设置它的值,则需要两步: 14 | 1. 使用glGetUniformLocation()函数获得gScale在列表中的索引 15 | 2. 通过glUniform*()或者glUniformMatrix*()系列函数来设置uniform变量的值(gScale是基本float类型的变量所以用glUniform1f()) 16 | 17 | 18 | 简单来说,Uniform变量就是应用程序用来向着色器中传递的一个类型和着色器运行时不变的值。 19 | 20 | ## 二、代码解释 21 | ### 2.1 opengl_math: 22 | 23 | ``` 24 | #ifndef __OPENGL_MATH_H 25 | #define __OPENGL_MATH_H 26 | 27 | //向量 28 | typedef float Vector3f[3]; 29 | 30 | //向量赋值 31 | inline void LoadVector3(Vector3f v, const float x, const float y, const float z) 32 | { 33 | v[0] = x; v[1] = y; v[2] = z; 34 | } 35 | 36 | #endif 37 | ``` 38 | 跟上一节没有区别,因为这一节只是增加了uniform存储限制符的应用 39 | 40 | ### 2.2 main.c: 41 | ``` 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include "opengl_math.h" 50 | 51 | using namespace std; 52 | 53 | GLuint VBO; 54 | GLint gScaleLocation; //位置中间变量 55 | 56 | const char* pVSFileName = "shader.vs"; 57 | const char* pFSFileName = "shader.fs"; 58 | 59 | bool ReadFile(const char* pFileName, string &outFile) 60 | { 61 | ifstream f(pFileName); 62 | 63 | bool ret = false; 64 | 65 | if (f.is_open()) { 66 | string line; 67 | while (getline(f, line)) { 68 | outFile.append(line); 69 | outFile.append("\n"); 70 | } 71 | f.close(); 72 | ret = true; 73 | } 74 | else { 75 | fprintf(stderr, "%s:%d: unable to open file `%s`\n", __FILE__, __LINE__, pFileName); 76 | } 77 | return ret; 78 | } 79 | 80 | 81 | static void Render() 82 | { 83 | glClear(GL_COLOR_BUFFER_BIT); 84 | 85 | static float Scale = 0.0f; 86 | Scale += 0.01f; 87 | //将值传递给shader 88 | glUniform1f(gScaleLocation, sinf(Scale)); 89 | 90 | glEnableVertexAttribArray(0); 91 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 92 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 93 | 94 | glDrawArrays(GL_TRIANGLES, 0, 3); 95 | glDisableVertexAttribArray(0); 96 | 97 | glutSwapBuffers(); 98 | } 99 | 100 | 101 | static void InitializeGlutCallbacks() 102 | { 103 | glutDisplayFunc(Render); 104 | 105 | //将渲染回调注册为全局闲置回调 106 | glutIdleFunc(Render); 107 | } 108 | 109 | static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType) 110 | { 111 | GLuint ShaderObj = glCreateShader(ShaderType); 112 | 113 | if (ShaderObj == 0) { 114 | fprintf(stderr, "Error creating shader type %d\n", ShaderType); 115 | exit(1); 116 | } 117 | 118 | const GLchar* p[1]; 119 | p[0] = pShaderText; 120 | GLint Lengths[1]; 121 | Lengths[0] = strlen(pShaderText); 122 | glShaderSource(ShaderObj, 1, p, Lengths); 123 | glCompileShader(ShaderObj); 124 | GLint success; 125 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 126 | if (!success) { 127 | GLchar InfoLog[1024]; 128 | glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog); 129 | fprintf(stderr, "Error compiling shader type %d: %s\n", ShaderType, InfoLog); 130 | exit(1); 131 | } 132 | 133 | glAttachShader(ShaderProgram, ShaderObj); 134 | } 135 | 136 | 137 | 138 | static void CompileShaders() 139 | { 140 | GLuint ShaderProgram = glCreateProgram(); 141 | 142 | if (ShaderProgram == 0) { 143 | fprintf(stderr, "Error creating shader program\n"); 144 | exit(1); 145 | } 146 | 147 | string vs, fs; 148 | 149 | if (!ReadFile(pVSFileName, vs)) { 150 | exit(1); 151 | } 152 | 153 | if (!ReadFile(pFSFileName, fs)) { 154 | exit(1); 155 | } 156 | 157 | AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER); 158 | AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER); 159 | 160 | GLint Success = 0; 161 | GLchar ErrorLog[1024] = { 0 }; 162 | 163 | glLinkProgram(ShaderProgram); 164 | glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success); 165 | if (Success == 0) { 166 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 167 | fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog); 168 | exit(1); 169 | } 170 | 171 | glValidateProgram(ShaderProgram); 172 | glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success); 173 | if (!Success) { 174 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 175 | fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog); 176 | exit(1); 177 | } 178 | 179 | glUseProgram(ShaderProgram); 180 | 181 | //查询获取一致变量的位置 182 | gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale"); 183 | //检查错误 184 | assert(gScaleLocation != 0xFFFFFFFF); 185 | } 186 | 187 | 188 | static void CreateVertexBuffer() 189 | { 190 | Vector3f Vertices[3]; 191 | 192 | LoadVector3(Vertices[0], -1.0f, -1.0f, 0.0f); 193 | LoadVector3(Vertices[1], 1.0f, -1.0f, 0.0f); 194 | LoadVector3(Vertices[2], 0.0f, 1.0f, 0.0f); 195 | 196 | glGenBuffers(1, &VBO); 197 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 198 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 199 | } 200 | 201 | int main(int argc, char **argv) 202 | { 203 | glutInit(&argc, argv); 204 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 205 | glutInitWindowSize(1024, 768); 206 | glutInitWindowPosition(10, 10); 207 | glutCreateWindow("Uniform"); 208 | 209 | InitializeGlutCallbacks(); 210 | 211 | GLenum res = glewInit(); 212 | if (res != GLEW_OK) { 213 | fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res)); 214 | return 1; 215 | } 216 | 217 | printf("GL version: %s\n", glGetString(GL_VERSION)); 218 | 219 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 220 | 221 | CreateVertexBuffer(); 222 | 223 | CompileShaders(); 224 | 225 | glutMainLoop(); 226 | 227 | return 0; 228 | } 229 | ``` 230 | 这里只看相比于上一节不同的部分。 231 | 232 | ### 2.2.1 全局闲置回调函数 233 | 注册绘图函数之后增加了一句 234 | ``` 235 | glutIdleFunc(Render); 236 | ``` 237 | glutIdleFunc()用于设置全局闲置回调函数,即在程序空闲的时候(没有窗口事件到达时)调用函数Render() 238 | 239 | ### 2.2.2 获取Uniform变量的位置 240 | 241 | ``` 242 | //查询获取一致变量的位置 243 | gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale"); 244 | //检查错误 assert(gScaleLocation != 0xFFFFFFFF); 245 | ``` 246 | 正如之前提到过,要想在应用程序中设置uniform变量的值,第一步先要找到uniform变量在列表中的索引 247 | 248 | ``` 249 | GLint glGetUniformLocation (GLuint program , 250 | constchar* name); 251 | 252 | ``` 253 | 返回着色器程序中uniform变量name对应的索引值,如果name与启用的着色器程序中的**所有uniform变量都不相符,或者name是一个内部保留的着色器变量的名称,返回值为-1** 254 | 255 | ``` 256 | assert(gScaleLocation != 0xFFFFFFFF); 257 | ``` 258 | 如果返回的是-1,则向stderr打印一条信息,然后通过调用 abort 来终止程序运行(即报错和终止程序) 259 | 260 | ### 2.2.3 设置Uniform变量的值 261 | ``` 262 | static float Scale = 0.0f; 263 | Scale += 0.01f; 264 | glUniform1f(gScaleLocation, sinf(Scale)); 265 | ``` 266 | 在Render()渲染函数中,通过glUniform1f,传送一个不断增大的静态浮点数的sin值,即sinf(Scale)的值在-1到1之间循环改变 267 | 268 | ### 2.3 shader 269 | shader.vs: 270 | ``` 271 | #version 330 272 | 273 | layout(location = 0) in vec3 Position; 274 | 275 | uniform float gScale; 276 | 277 | void main() 278 | { 279 | gl_Position = vec4(gScale * Position.x, gScale * Position.y, Position.z, 1.0); 280 | } 281 | 282 | ``` 283 | 顶点着色器中使用uniform变量,并且通过它,改变三角形的三个顶点的位置 284 | - uniform float gScale:声明gScale变量 285 | - gl_Position = vec4(gScale * Position.x, gScale * Position.y, Position.z, 1.0): 通过使用gScale名字来使用gScale变量 286 | 287 | shader.fs: 288 | 289 | ``` 290 | #version 330 291 | 292 | out vec4 FragColor; 293 | 294 | void main() 295 | { 296 | FragColor = vec4(1.0,0.0,0.0,1.0); 297 | } 298 | 299 | ``` 300 | 片元着色器没有变化 301 | 302 | ## 三、运行结果 303 | 304 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Uniform/image/1.png) 305 | 可以看到三角形从大变小再由小变大,并且每一次从大变小会翻转位置,因为sinf(Scale)的值是-1到1之间变化 306 | -------------------------------------------------------------------------------- /Ambient_Lighting/Ambient_Lighting/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "opengl_math.h" 9 | #include "opengl_pipeline.h" 10 | #include "opengl_texture.h" 11 | #include "opengl_camera.h" 12 | 13 | #define Window_Width 1024 14 | #define Window_Height 768 15 | 16 | using namespace std; 17 | 18 | GLuint VBO, IBO; 19 | GLuint gWVPLocation, gSampler; 20 | GLuint m_dirLightColorLocation, m_dirLightAmbientIntensityLocation; 21 | 22 | Camera *pGameCamera = NULL; 23 | Texture *pTexture = NULL; 24 | PersProjInfo gPersProjInfo; 25 | 26 | 27 | const char* pVSFileName = "shader.vs"; 28 | const char* pFSFileName = "shader.fs"; 29 | 30 | struct DirectionalLight 31 | { 32 | Vector3f Color; 33 | float AmbientIntensity; 34 | }; 35 | struct DirectionalLight m_directionalLight; 36 | struct Vertex 37 | { 38 | Vector3f m_pos; 39 | Vector2f m_tex; 40 | 41 | Vertex() {} 42 | 43 | Vertex(Vector3f pos, Vector2f tex) 44 | { 45 | CopyVector3(m_pos, pos); 46 | CopyVector2(m_tex, tex); 47 | } 48 | }; 49 | 50 | static void CreateVertexBuffer() 51 | { 52 | Vector3f Vertices3f[4]; 53 | Vector2f Vertices2f[4]; 54 | LoadVector3(Vertices3f[0], -1.0f, -1.0f, 0.5773f); LoadVector2(Vertices2f[0], 0.0f, 0.0f); 55 | LoadVector3(Vertices3f[1], 0.0f, -1.0f, -1.15475f); LoadVector2(Vertices2f[1], 0.5f, 0.0f); 56 | LoadVector3(Vertices3f[2], 1.0f, -1.0f, 0.5773f); LoadVector2(Vertices2f[2], 1.0f, 0.0f); 57 | LoadVector3(Vertices3f[3], 0.0f, 1.0f, 0.0f); LoadVector2(Vertices2f[3], 0.5f, 1.0f); 58 | Vertex Vertices[4] = { Vertex(Vertices3f[0],Vertices2f[0]), 59 | Vertex(Vertices3f[1],Vertices2f[1]), 60 | Vertex(Vertices3f[2],Vertices2f[2]), 61 | Vertex(Vertices3f[3],Vertices2f[3]) }; 62 | 63 | glGenBuffers(1, &VBO); 64 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 65 | glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 66 | } 67 | static void CreateIndexBuffer() 68 | { 69 | unsigned int Indices[] = 70 | { 71 | 0,3,1, 72 | 1,3,2, 73 | 2,3,0, 74 | 0,1,2 75 | }; 76 | 77 | glGenBuffers(1, &IBO); 78 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 79 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); 80 | } 81 | bool ReadFile(const char* FileName, string &outFile) 82 | { 83 | ifstream f(FileName); 84 | bool ret = FALSE; 85 | 86 | if (f.is_open()) { 87 | string line; 88 | while (getline(f, line)) { 89 | outFile.append(line); 90 | outFile.append("\n"); 91 | } 92 | f.close(); 93 | ret = TRUE; 94 | } 95 | else { 96 | fprintf(stderr, "%s,%d: unable to read file: %s\n", __FILE__, __LINE__, FileName); 97 | system("pasue"); 98 | } 99 | return ret; 100 | } 101 | static void AddShader(GLuint ShaderProgram, const char* ShaderText, GLenum ShaderType) 102 | { 103 | GLuint ShaderObj = glCreateShader(ShaderType); 104 | if (!ShaderObj) { 105 | fprintf(stderr, "Error creating shader obj"); 106 | system("pause"); 107 | exit(1); 108 | } 109 | 110 | const GLchar* p[1]; 111 | p[0] = ShaderText; 112 | GLint Length[1]; 113 | Length[0] = strlen(ShaderText); 114 | glShaderSource(ShaderObj,1,p,Length); 115 | glCompileShader(ShaderObj); 116 | GLint success; 117 | glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); 118 | if (!success) { 119 | GLchar InfoLog[1024]; 120 | glGetShaderInfoLog(ShaderObj, sizeof(InfoLog), NULL, InfoLog); 121 | fprintf(stderr, "Error compiling shader obj:'%s'\n", InfoLog); 122 | system("pause"); 123 | exit(1); 124 | } 125 | 126 | glAttachShader(ShaderProgram, ShaderObj); 127 | } 128 | static void CompileShader() 129 | { 130 | GLuint ShaderProgram = glCreateProgram(); 131 | if (!ShaderProgram) { 132 | fprintf(stderr, "Error creating shader program"); 133 | system("pause"); 134 | exit(1); 135 | } 136 | 137 | string vs, fs; 138 | if (!ReadFile(pVSFileName,vs)) { 139 | exit(1); 140 | } 141 | if (!ReadFile(pFSFileName,fs)) { 142 | exit(1); 143 | } 144 | 145 | AddShader(ShaderProgram,vs.c_str(),GL_VERTEX_SHADER); 146 | AddShader(ShaderProgram,fs.c_str(),GL_FRAGMENT_SHADER); 147 | 148 | GLchar ErrorLog[1024] = { 0 }; 149 | GLint Success; 150 | 151 | glLinkProgram(ShaderProgram); 152 | glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success); 153 | if (!Success) { 154 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 155 | fprintf(stderr, "Error linking shader program: '%s'", ErrorLog); 156 | system("pause"); 157 | exit(1); 158 | } 159 | 160 | glValidateProgram(ShaderProgram); 161 | glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success); 162 | if (!Success) { 163 | glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog); 164 | fprintf(stderr, "Valied to shader program:'%s'", ErrorLog); 165 | system("pause"); 166 | exit(1); 167 | } 168 | glUseProgram(ShaderProgram); 169 | 170 | gWVPLocation = glGetUniformLocation(ShaderProgram, "gWVP"); 171 | assert(gWVPLocation != 0xFFFFFFFF); 172 | gSampler = glGetUniformLocation(ShaderProgram, "gSampler"); 173 | assert(gSampler != 0xFFFFFFFF); 174 | m_dirLightColorLocation = glGetUniformLocation(ShaderProgram,"gDirectionalLight.Color"); 175 | m_dirLightAmbientIntensityLocation = glGetUniformLocation(ShaderProgram,"gDirectionalLight.AmbientIntensity"); 176 | } 177 | static void Render() 178 | { 179 | pGameCamera->OnRender(); 180 | glClear(GL_COLOR_BUFFER_BIT); 181 | 182 | static float Scale = 0.0f; 183 | Scale += 0.1f; 184 | 185 | Pipeline p; 186 | p.Rotate(0.0f, Scale, 0.0f); 187 | p.WorldPos(0.0f, 0.0f, 3.0f); 188 | p.SetPerspectiveProj(gPersProjInfo); 189 | p.SetCamera(*pGameCamera); 190 | 191 | glUniformMatrix4fv(gWVPLocation, 1, GL_FALSE, (const GLfloat*)p.GetWVPTrans()); 192 | 193 | m_directionalLight.AmbientIntensity *= 1.0001f; 194 | glUniform3f(m_dirLightColorLocation, m_directionalLight.Color[0], m_directionalLight.Color[1], m_directionalLight.Color[2]); 195 | glUniform1f(m_dirLightAmbientIntensityLocation, m_directionalLight.AmbientIntensity); 196 | 197 | glEnableVertexAttribArray(0); 198 | glEnableVertexAttribArray(1); 199 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 200 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); 201 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)sizeof(Vector3f)); 202 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); 203 | pTexture->Bind(GL_TEXTURE); 204 | 205 | glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); 206 | 207 | glDisableVertexAttribArray(0); 208 | glDisableVertexAttribArray(1); 209 | 210 | glutSwapBuffers(); 211 | } 212 | static void Keyboard(int key, int i, int y) 213 | { 214 | pGameCamera->OnKeyboard(key); 215 | } 216 | 217 | static void Mouse(int x, int y) 218 | { 219 | pGameCamera->OnMouse(x, y); 220 | } 221 | static void InitializeCallbacks() 222 | { 223 | glutDisplayFunc(Render); 224 | glutIdleFunc(Render); 225 | 226 | glutSpecialFunc(Keyboard); 227 | glutPassiveMotionFunc(Mouse); 228 | 229 | } 230 | int main(int argc, char **argv) 231 | { 232 | glutInit(&argc, argv); 233 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 234 | glutInitWindowPosition(10, 10); 235 | glutInitWindowSize(Window_Width, Window_Height); 236 | glutCreateWindow("Ambient Lighting"); 237 | glutGameModeString("1028x1024@32"); 238 | 239 | InitializeCallbacks(); 240 | 241 | GLenum res = glewInit(); 242 | if (res != GLEW_OK) { 243 | fprintf(stderr, "Error init glew"); 244 | system("pause"); 245 | exit(1); 246 | } 247 | 248 | printf("GL version: %s\n", glGetString(GL_VERSION)); 249 | 250 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 251 | glFrontFace(GL_CW); 252 | glCullFace(GL_BACK); 253 | glEnable(GL_CULL_FACE); 254 | 255 | CreateVertexBuffer(); 256 | CreateIndexBuffer(); 257 | 258 | CompileShader(); 259 | 260 | glUniform1i(gSampler, 0); 261 | 262 | pTexture = new Texture(GL_TEXTURE_2D, "test.png"); 263 | if (!pTexture->Load()) { 264 | return 1; 265 | } 266 | 267 | gPersProjInfo.FOV = 60.0f; 268 | gPersProjInfo.Height = Window_Height; 269 | gPersProjInfo.Width = Window_Width; 270 | gPersProjInfo.zFar = 100.0f; 271 | gPersProjInfo.zNear = 1.0f; 272 | 273 | Vector3f CameraPos, CameraTarget, CameraUp; 274 | LoadVector3(CameraPos, 0.0f, 0.0f, -4.0f); 275 | LoadVector3(CameraTarget, 0.0f, 0.0f, 1.0f); 276 | LoadVector3(CameraUp, 0.0f, 1.0f, 0.0f); 277 | pGameCamera = new Camera(Window_Width, Window_Height, CameraPos, CameraTarget, CameraUp); 278 | 279 | LoadVector3(m_directionalLight.Color,1.0f, 1.0f, 1.0f); 280 | m_directionalLight.AmbientIntensity = 0.1f; 281 | 282 | glutMainLoop(); 283 | 284 | return 0; 285 | 286 | } -------------------------------------------------------------------------------- /Diffuse_Lighting/OpenGL学习之路16---- 漫射光.md: -------------------------------------------------------------------------------- 1 | 根据教程:[ogldev](http://ogldev.atspace.co.uk/index.html)一步步从零开始,记录学习历程 2 | 3 | 上一节学习了怎么渲染环境光,相关博文: 4 | [OpenGL学习之路15----环境光](https://blog.csdn.net/zach_z/article/details/80188764) 5 | 6 | ## 漫射光 7 | 之前我们学习到的环境光忽视光线的方向,如果只有环境光,整个场景是被均匀照亮的 8 | 9 | 漫射光跟环境光唯一的区别是漫射光依赖光线的方向,漫射光照射到物体上,被照射的一面要比背面亮 10 | 11 | 漫射光的强度,与光线入射的角度有关,如果光线垂直物体,则漫射光强最大,随着入射光线与法线夹角的增大,慢射光强逐渐减小 12 | 13 | 如图所示,随着当α为0°时,即垂直照射到物体表面,和法线成180°,漫射光强最大;而随着α增大,漫射光强逐渐减小: 14 | 15 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Diffuse_Lighting/image/1.png) 16 | 17 | 计算光的强度,使用光线和物体表面法线的夹角α的余弦值作为一个参数,下图中表示了四个不同角度照射到表面上的情况: 18 | 19 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Diffuse_Lighting/image/2.png) 20 | 21 | - A的强度是最大的,因为α为0,余弦值最大为1 22 | - B的强度是,cos(α) 23 | - C从表面的一侧入射,此时光线和法线垂直,所以α角度为90,余弦值为0,即光线C对表面照亮没有任何效果 24 | - D从表面的背面入射,α是一个钝角,余弦值为负,光线D也对物体表面没有照亮作用 25 | 26 | **结构体表示漫反射:** 27 | 28 | ``` 29 | struct DirectionalLight 30 | { 31 | Vector3f Color; 32 | float AmbientIntensity; 33 | Vector3f Direction; 34 | float DiffuseIntensity; 35 | }; 36 | ``` 37 | - 可以看到我们平行光结构体,在上一节环境光两个属性参数的基础上增加了两个代表着漫反射属性参数的变量 38 | - Direction:代表光线入射的方向向量 39 | - DiffuseIntensity:表示为漫反射的光线强度 40 | 41 | ## 计算顶点法线 42 | 多边形面上分布的任意法向量都是相同的,所以一个面取一个法向量即可代表平面法向量。 **但是这样会导致一个平面上光线强度一样,导致多边形几个面之间光线不平滑。** 43 | 44 | 这时引入“顶点法线”的概念:**顶点法线即是共用一个顶点的所有三角形法线的平均值** 45 | 46 | 我们在顶点着色器中根据顶点法线顶点计算出在世界空间里每个点的法向量的值,并传递到片元着色器中计算出每个像素法向量对应的光照强度 47 | 48 | 如下图,左边是两个平面各自的法向量,而右边是由顶点法线计算后得到每个点的法向量的样子: 49 | 50 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Diffuse_Lighting/image/3.png) 51 | 52 | 计算顶点法线的代码: 53 | 54 | ``` 55 | static void CalcNormals(const unsigned int* pIndices, unsigned int IndexCount, Vertex* pVertices, unsigned int VertexCount) 56 | { 57 | for (unsigned int i = 0; i < IndexCount; i += 3) { 58 | unsigned int Index0 = pIndices[i]; 59 | unsigned int Index1 = pIndices[i+1]; 60 | unsigned int Index2 = pIndices[i+2]; 61 | Vector3f v1, v2; 62 | SubVector3(v1, pVertices[Index1].m_pos, pVertices[Index0].m_pos); 63 | SubVector3(v2, pVertices[Index2].m_pos, pVertices[Index0].m_pos); 64 | Vector3f Normal; 65 | CrossProduct3(Normal, v1, v2); 66 | NormalizeVector3(Normal); 67 | 68 | AddVector3(pVertices[Index0].m_normal, pVertices[Index0].m_normal, Normal); 69 | AddVector3(pVertices[Index1].m_normal, pVertices[Index1].m_normal, Normal); 70 | AddVector3(pVertices[Index2].m_normal, pVertices[Index2].m_normal, Normal); 71 | } 72 | for (unsigned int i = 0; i < VertexCount; i++) { 73 | NormalizeVector3(pVertices[i].m_normal); 74 | } 75 | } 76 | ``` 77 | - 先根据索引取出每个三角形的三个顶点计算其法向量 78 | - 每个三角形法向量都是从第一个顶点到其他两个顶点两条边向量进行差积 79 | - 差积之后得到的三角形法向量进行单位化 80 | - 把单位化后的三角形法向量与顶点法向量累加 81 | - 最后再把所有顶点的顶点法向量单位化 82 | 83 | ## 漫射光的实现 84 | - 扩充顶点结构体,假如顶点法向量变量 85 | 86 | ``` 87 | struct Vertex 88 | { 89 | Vector3f m_pos; 90 | Vector2f m_tex; 91 | Vector3f m_normal; 92 | 93 | Vertex() {} 94 | 95 | Vertex(Vector3f pos, Vector2f tex) 96 | { 97 | CopyVector3(m_pos, pos); 98 | CopyVector2(m_tex, tex); 99 | LoadVector3(m_normal, 0.0f, 0.0f, 0.0f); 100 | } 101 | }; 102 | ``` 103 | - 扩充平行光结构体数组,假如漫反射变量参数,射入方向向量和光强 104 | 105 | ``` 106 | struct DirectionalLight 107 | { 108 | Vector3f Color; 109 | float AmbientIntensity; 110 | Vector3f Direction; 111 | float DiffuseIntensity; 112 | }; 113 | ``` 114 | - 声明一个平行光结构体变量,并赋予初值,光线颜色为白色(1.0f,1.0f,1.0f),环境光强为0.1f,漫反射光强为0.75f,漫反射光方向为(1.0f,0.0,0.0)即射向X轴正方向 115 | 116 | ``` 117 | struct DirectionalLight m_directionalLight; 118 | 119 | LoadVector3(m_directionalLight.Color,1.0f, 1.0f, 1.0f); 120 | m_directionalLight.AmbientIntensity = 0.1f; 121 | m_directionalLight.DiffuseIntensity = 0.75f; 122 | LoadVector3(m_directionalLight.Direction,1.0f, 0.0, 0.0); 123 | ``` 124 | 125 | - 计算出顶点法向量的值,放在各个顶点结构体中,存入顶点缓冲区,CalcNormals()函数用来计算每个顶点的顶点法向量 126 | 127 | ``` 128 | CalcNormals(Indices, sizeof(Indices)/sizeof(Indices[0]), Vertices, sizeof(Vertices)/sizeof(Vertices[0])); 129 | ``` 130 | - 定义三个全局变量用于获得Uniform变量的index索引 131 | 132 | ``` 133 | GLuint m_WorldMatrixLocation; 134 | GLuint m_dirLightDirectionLocation, m_dirLightDiffuseIntensityLocation; 135 | ``` 136 | - 获取Uniform变量的索引: 137 | 138 | ``` 139 | m_WorldMatrixLocation = glGetUniformLocation(ShaderProgram,"gWorld"); 140 | m_dirLightDirectionLocation = glGetUniformLocation(ShaderProgram, "gDirectionalLight.Direction"); 141 | assert(m_dirLightDirectionLocation != 0xFFFFFFFF); 142 | m_dirLightDiffuseIntensityLocation = glGetUniformLocation(ShaderProgram, "gDirectionalLight.DiffuseIntensity"); 143 | assert(m_dirLightDiffuseIntensityLocation != 0xFFFFFFFF); 144 | ``` 145 | - 在主渲染函数Render()中传递Uniform变量的值到着色器,即传递漫反射的方向和强度,以及世界坐标的变化矩阵 146 | [(在混合变换那一节讲到) 147 | ](https://blog.csdn.net/zach_z/article/details/80086523) 148 | ``` 149 | glUniformMatrix4fv(m_WorldMatrixLocation, 1, GL_FALSE, (const GLfloat*)p.GetWorldTrans()); 150 | Vector3f Direction; 151 | CopyVector3(Direction, m_directionalLight.Direction); 152 | NormalizeVector3(Direction); 153 | glUniform3f(m_dirLightDirectionLocation, Direction[0], Direction[1], Direction[2]); 154 | glUniform1f(m_dirLightDiffuseIntensityLocation, m_directionalLight.DiffuseIntensity); 155 | ``` 156 | - 把顶点法向量传送到着色器,这里启动一个顶点属性2用于顶点法向量,偏移量为一个三位向量(顶点位置)和一个二维向量纹理坐标: 157 | 158 | ``` 159 | glEnableVertexAttribArray(2); 160 | ... 161 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)(sizeof(Vector3f)+sizeof(Vector2f))); 162 | ... 163 | glDisableVertexAttribArray(2); 164 | ``` 165 | 在顶点着色器中使用layout(location=2)与之对应获取顶点法向量 166 | - 顶点着色器增加了世界坐标变换矩阵Uniform变量的值和顶点法向量的值作为输入变量 167 | shader.vs: 168 | 169 | ``` 170 | #version 330 171 | 172 | layout (location=0) in vec3 Position; 173 | layout (location=1) in vec2 TexCoord; 174 | layout (location=2) in vec3 Normal; 175 | 176 | uniform mat4 gWVP; 177 | uniform mat4 gWorld; 178 | 179 | out vec2 TexCoord0; 180 | out vec3 Normal0; 181 | 182 | void main() 183 | { 184 | gl_Position = gWVP * vec4(Position,1.0); 185 | TexCoord0=TexCoord; 186 | Normal0 = (gWorld * vec4(Normal,0.0)).xyz; 187 | } 188 | ``` 189 | 这里使用世界坐标变换矩阵和顶点法向量相乘获得具体位置的法向量,并且传递到片元着色器当中 190 | - 片元着色器中扩充了了平行光结构体,并且使用Uniform变量接收从主程序中传来的漫射光的参数,并计算漫射光在物体上具体的颜色,结合环境光颜色与纹理颜色相乘得到最终颜色。shader.fs: 191 | 192 | ``` 193 | #version 330 194 | 195 | in vec2 TexCoord0; 196 | in vec3 Normal0; 197 | 198 | out vec4 FragColor; 199 | 200 | struct DirectionalLight 201 | { 202 | vec3 Color; 203 | float AmbientIntensity; 204 | float DiffuseIntensity; 205 | vec3 Direction; 206 | }; 207 | 208 | uniform DirectionalLight gDirectionalLight; 209 | uniform sampler2D gSampler; 210 | 211 | void main() 212 | { 213 | vec4 AmbientColor = vec4(gDirectionalLight.Color,1.0f)*gDirectionalLight.AmbientIntensity; 214 | 215 | float DiffuseFactor = dot(normalize(Normal0),-gDirectionalLight.Direction); 216 | 217 | vec4 DiffuseColor; 218 | if(DiffuseFactor > 0){ 219 | DiffuseColor = vec4(gDirectionalLight.Color,1.0f) * gDirectionalLight.DiffuseIntensity * DiffuseFactor; 220 | } 221 | else{ 222 | DiffuseColor=vec4(0,0,0,0); 223 | } 224 | FragColor = texture2D(gSampler,TexCoord0)*(AmbientColor+DiffuseColor); 225 | } 226 | ``` 227 | 可以看到环境光的计算没有发生变化 228 | 229 | ``` 230 | vec4 AmbientColor = vec4(gDirectionalLight.Color,1.0f)*gDirectionalLight.AmbientIntensity; 231 | ``` 232 | 随后计算法向量与光线入射方向的角度的余弦值,用点积来得到余弦值,两个单位向量长度的点积结果为夹角的余弦值,相关博文: 233 | [OpenGL学习之路11----相机空间](https://blog.csdn.net/zach_z/article/details/80102219) 234 | 235 | 只用单位化法向量,因为传来的漫射光方向向量已经是单位化过了的 236 | 237 | 并且还需要将光源方向取反,这点其实已经在之前说过了,比如垂直入射到物体表面,入射角α应该为0°,然而法向量竖直向上是180°所以要取反一下 238 | 239 | 根据α的余弦值,即DiffuseFactor的值判断角度大小,如果是正则计算漫反射在物体上的光照颜色,如果余弦值为负,则证明入射方向垂直法向量或者在反面,则不产生作用 240 | 241 | ``` 242 | if(DiffuseFactor > 0){ 243 | DiffuseColor = vec4(gDirectionalLight.Color,1.0f) * gDirectionalLight.DiffuseIntensity * DiffuseFactor; 244 | } 245 | else{ 246 | DiffuseColor=vec4(0,0,0,0); 247 | } 248 | ``` 249 | 最后根据纹理颜色,环境光颜色和漫反射颜色得到最终颜色: 250 | 251 | ``` 252 | FragColor = texture2D(gSampler,TexCoord0)*(AmbientColor+DiffuseColor); 253 | ``` 254 | 255 | ## 运行结果 256 | 我们在主程序中假如按键控制来体验环境光和漫射光强度的变化 257 | 258 | ``` 259 | static void Keyboard(int key, int i, int y) 260 | { 261 | pGameCamera->OnKeyboard(key); 262 | switch (key) { 263 | case GLUT_KEY_F1: 264 | m_directionalLight.AmbientIntensity += 0.05f; 265 | break; 266 | 267 | case GLUT_KEY_F2: 268 | m_directionalLight.AmbientIntensity -= 0.05f; 269 | break; 270 | 271 | case GLUT_KEY_F3: 272 | m_directionalLight.DiffuseIntensity += 0.05f; 273 | break; 274 | 275 | case GLUT_KEY_F4: 276 | m_directionalLight.DiffuseIntensity -= 0.05f; 277 | break; 278 | } 279 | ``` 280 | - F1键:增大环境光光强 281 | - F2键:减小环境光光强 282 | - F3键:增大漫反射光强 283 | - F4键:减小漫反射光强 284 | 285 | 通过按键控制可以体验效果 286 | 287 | ![image](https://github.com/zach0zhang/OpenGL_Learning/blob/master/Diffuse_Lighting/image/4.png) 288 | --------------------------------------------------------------------------------