├── ep22-imgui ├── .gitignore ├── vendor │ ├── stb_image │ │ └── stb_image.cpp │ └── imgui │ │ └── imgui_impl_glfw_gl3.h ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ └── Basic.shader ├── VertexBuffer.h ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── Shader.h ├── Texture.cpp ├── VertexBufferLayout.h └── Debug.h ├── .gitignore ├── ep23-rendering-multiple-objects ├── .gitignore ├── vendor │ ├── stb_image │ │ └── stb_image.cpp │ └── imgui │ │ └── imgui_impl_glfw_gl3.h ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ └── Basic.shader ├── VertexBuffer.h ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── Shader.h ├── Texture.cpp ├── VertexBufferLayout.h └── Debug.h ├── ep24-test-framework ├── .gitignore ├── vendor │ ├── stb_image │ │ └── stb_image.cpp │ └── imgui │ │ └── imgui_impl_glfw_gl3.h ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ ├── Simple.shader │ │ ├── Uniform.shader │ │ └── Complex.shader ├── VertexBuffer.h ├── tests │ ├── Test.h │ ├── TestClearColor.h │ ├── TestClearColor.cpp │ ├── TestTriangle.h │ ├── TestUniform.h │ ├── TestTriangle.cpp │ ├── TestMultipleObjects.h │ ├── TestUniform.cpp │ └── TestMultipleObjects.cpp ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── Shader.h ├── Texture.cpp ├── VertexBufferLayout.h ├── Debug.h └── Application.cpp ├── ep02-window ├── meson.build └── main.cpp ├── ep08-shaders ├── meson.build └── res │ └── shaders │ └── Basic.shader ├── ep03-modern-opengl ├── meson.build └── src │ └── main.cpp ├── ep09-index-buffers ├── meson.build └── res │ └── shaders │ └── Basic.shader ├── ep04-vertex-buffers ├── meson.build └── src │ └── main.cpp ├── ep05-vertex-attributes ├── meson.build └── src │ └── main.cpp ├── ep07-write-a-shader ├── meson.build └── src │ └── main.cpp ├── ep10-errors ├── playground └── res │ └── shaders │ └── Basic.shader ├── ep17-textures ├── vendor │ └── stb_image │ │ └── stb_image.cpp ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ └── Basic.shader ├── VertexBuffer.h ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── Shader.h ├── VertexArray.cpp ├── Texture.cpp ├── VertexBufferLayout.h ├── Debug.h └── Application.cpp ├── ep19-math ├── vendor │ └── stb_image │ │ └── stb_image.cpp ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ └── Basic.shader ├── VertexBuffer.h ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── Shader.h ├── Texture.cpp ├── VertexBufferLayout.h ├── Debug.h └── Application.cpp ├── ep11-uniforms ├── playground └── res │ └── shaders │ └── Basic.shader ├── ep12-vertex-arrays ├── playground └── res │ └── shaders │ └── Basic.shader ├── ep20-projection-matrices ├── vendor │ └── stb_image │ │ └── stb_image.cpp ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ └── Basic.shader ├── VertexBuffer.h ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── Shader.h ├── Texture.cpp ├── VertexBufferLayout.h ├── Debug.h └── Application.cpp ├── ep21-model-view-projection ├── vendor │ └── stb_image │ │ └── stb_image.cpp ├── res │ ├── textures │ │ ├── phone.png │ │ └── gold-dollar.png │ └── shaders │ │ └── Basic.shader ├── VertexBuffer.h ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── Texture.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── Shader.h ├── Texture.cpp ├── VertexBufferLayout.h ├── Debug.h └── Application.cpp ├── ep13-opengl-abstraction ├── Renderer.h ├── VertexBuffer.h ├── res │ └── shaders │ │ └── Basic.shader ├── IndexBuffer.h ├── Makefile ├── VertexBuffer.cpp ├── IndexBuffer.cpp └── Renderer.cpp ├── Makefile ├── ep16-renderer ├── VertexBuffer.h ├── res │ └── shaders │ │ └── Basic.shader ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── Renderer.cpp ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── Shader.h ├── VertexArray.cpp ├── VertexBufferLayout.h ├── Debug.h └── Application.cpp ├── ep15-shader-abstraction ├── VertexBuffer.h ├── res │ └── shaders │ │ └── Basic.shader ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── Shader.h ├── VertexArray.cpp ├── VertexBufferLayout.h ├── Renderer.cpp └── Application.cpp ├── ep14-buffer-layout-abstraction ├── VertexBuffer.h ├── res │ └── shaders │ │ └── Basic.shader ├── Renderer.h ├── IndexBuffer.h ├── VertexArray.h ├── VertexBuffer.cpp ├── IndexBuffer.cpp ├── Makefile ├── VertexArray.cpp ├── VertexBufferLayout.h └── Renderer.cpp ├── meson.build ├── next-episode.sh └── README.md /ep22-imgui/.gitignore: -------------------------------------------------------------------------------- 1 | imgui.ini 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /nbproject 3 | tags 4 | .tags 5 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/.gitignore: -------------------------------------------------------------------------------- 1 | imgui.ini 2 | -------------------------------------------------------------------------------- /ep24-test-framework/.gitignore: -------------------------------------------------------------------------------- 1 | imgui.ini 2 | .tags 3 | -------------------------------------------------------------------------------- /ep02-window/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep08-shaders/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'src/main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep03-modern-opengl/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'src/main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep09-index-buffers/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'src/main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep04-vertex-buffers/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'src/main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep05-vertex-attributes/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'src/main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep07-write-a-shader/meson.build: -------------------------------------------------------------------------------- 1 | executable('app', 'src/main.cpp', dependencies: deps) 2 | -------------------------------------------------------------------------------- /ep10-errors/playground: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep10-errors/playground -------------------------------------------------------------------------------- /ep17-textures/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep19-math/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep22-imgui/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep11-uniforms/playground: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep11-uniforms/playground -------------------------------------------------------------------------------- /ep24-test-framework/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep12-vertex-arrays/playground: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep12-vertex-arrays/playground -------------------------------------------------------------------------------- /ep20-projection-matrices/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep21-model-view-projection/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep19-math/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep19-math/res/textures/phone.png -------------------------------------------------------------------------------- /ep22-imgui/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep22-imgui/res/textures/phone.png -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/vendor/stb_image/stb_image.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" 3 | -------------------------------------------------------------------------------- /ep17-textures/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep17-textures/res/textures/phone.png -------------------------------------------------------------------------------- /ep19-math/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep19-math/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep22-imgui/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep22-imgui/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep17-textures/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep17-textures/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep24-test-framework/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep24-test-framework/res/textures/phone.png -------------------------------------------------------------------------------- /ep20-projection-matrices/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep20-projection-matrices/res/textures/phone.png -------------------------------------------------------------------------------- /ep24-test-framework/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep24-test-framework/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep21-model-view-projection/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep21-model-view-projection/res/textures/phone.png -------------------------------------------------------------------------------- /ep20-projection-matrices/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep20-projection-matrices/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep21-model-view-projection/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep21-model-view-projection/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/res/textures/phone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep23-rendering-multiple-objects/res/textures/phone.png -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/res/textures/gold-dollar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajsahae/thecherno_opengl/HEAD/ep23-rendering-multiple-objects/res/textures/gold-dollar.png -------------------------------------------------------------------------------- /ep13-opengl-abstraction/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define ASSERT(x) if (!(x)) assert(false) 7 | 8 | #define GLCall(x) GLClearError();\ 9 | x;\ 10 | ASSERT(GLCheckError()) 11 | 12 | void GLClearError(); 13 | bool GLCheckError(); 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BUILD_DIR = build 2 | 3 | default: build 4 | 5 | ${BUILD_DIR}/build.ninja: 6 | meson setup ${BUILD_DIR} 7 | 8 | .PHONY: clean reset build 9 | clean: 10 | ninja -C ${BUILD_DIR} clean 11 | 12 | reset: 13 | meson setup --wipe ${BUILD_DIR} 14 | 15 | build: | ${BUILD_DIR}/build.ninja 16 | ninja -C ${BUILD_DIR} 17 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | public: 6 | VertexBuffer(const void* data, unsigned int size); 7 | ~VertexBuffer(); 8 | 9 | void Bind(); 10 | void Unbind(); 11 | 12 | private: 13 | unsigned int m_RendererID; 14 | }; 15 | -------------------------------------------------------------------------------- /ep19-math/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep22-imgui/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep16-renderer/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep17-textures/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep24-test-framework/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep10-errors/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | void main() 13 | { 14 | color = vec4(0.2, 0.3, 0.8, 1.0); 15 | } 16 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep20-projection-matrices/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep21-model-view-projection/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep08-shaders/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | void main() 13 | { 14 | color = vec4(0.2, 0.3, 0.8, 1.0); 15 | } 16 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | public: 6 | VertexBuffer(const void* data, unsigned int size); 7 | ~VertexBuffer(); 8 | 9 | void Bind() const; 10 | void Unbind() const; 11 | 12 | private: 13 | unsigned int m_RendererID; 14 | }; 15 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/VertexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class VertexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | 8 | public: 9 | VertexBuffer(const void* data, unsigned int size); 10 | ~VertexBuffer(); 11 | 12 | void Bind() const; 13 | void Unbind() const; 14 | }; 15 | -------------------------------------------------------------------------------- /ep09-index-buffers/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | void main() 13 | { 14 | color = vec4(0.2, 0.3, 0.8, 1.0); 15 | } 16 | -------------------------------------------------------------------------------- /ep24-test-framework/res/shaders/Simple.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | void main() 13 | { 14 | color = vec4(0.2, 0.3, 0.8, 1.0); 15 | } 16 | -------------------------------------------------------------------------------- /ep11-uniforms/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | uniform vec4 u_Color; 13 | void main() 14 | { 15 | color = u_Color; 16 | } 17 | -------------------------------------------------------------------------------- /ep16-renderer/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | uniform vec4 u_Color; 13 | void main() 14 | { 15 | color = u_Color; 16 | } 17 | -------------------------------------------------------------------------------- /ep12-vertex-arrays/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | uniform vec4 u_Color; 13 | void main() 14 | { 15 | color = u_Color; 16 | } 17 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | uniform vec4 u_Color; 13 | void main() 14 | { 15 | color = u_Color; 16 | } 17 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | uniform vec4 u_Color; 13 | void main() 14 | { 15 | color = u_Color; 16 | } 17 | -------------------------------------------------------------------------------- /ep19-math/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep22-imgui/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/Test.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace test 4 | { 5 | class Test 6 | { 7 | public: 8 | Test() {} 9 | virtual ~Test() {} 10 | 11 | virtual void OnUpdate(float deltaTime) {} 12 | virtual void OnRender() {} 13 | virtual void OnImGuiRender() {} 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | layout(location = 0) out vec4 color; 12 | uniform vec4 u_Color; 13 | void main() 14 | { 15 | color = u_Color; 16 | } 17 | -------------------------------------------------------------------------------- /ep16-renderer/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep17-textures/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep24-test-framework/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep24-test-framework/res/shaders/Uniform.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | void main() 5 | { 6 | gl_Position = position; 7 | } 8 | 9 | #shader fragment 10 | #version 330 core 11 | 12 | layout(location = 0) out vec4 color; 13 | uniform vec4 u_Color; 14 | 15 | void main() 16 | { 17 | color = u_Color; 18 | } 19 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "Debug.h" 6 | #include "VertexArray.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | 10 | class Renderer 11 | { 12 | public: 13 | void Clear() const; 14 | void Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const; 15 | }; 16 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('the-cheno-opengl', 'cpp') 2 | 3 | gl = dependency('gl') 4 | glfw3 = dependency('glfw3') 5 | glew = dependency('glew') 6 | 7 | deps = [gl, glfw3, glew] 8 | subdir('ep02-window') 9 | subdir('ep03-modern-opengl') 10 | subdir('ep04-vertex-buffers') 11 | subdir('ep05-vertex-attributes') 12 | subdir('ep07-write-a-shader') 13 | subdir('ep08-shaders') 14 | subdir('ep09-index-buffers') 15 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define ASSERT(x) if (!(x)) assert(false) 8 | 9 | #ifdef DEBUG 10 | #define GLCall(x) GLClearError();\ 11 | x;\ 12 | ASSERT(GLCheckError()) 13 | #else 14 | #define GLCall(x) x 15 | #endif 16 | 17 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 18 | 19 | void GLClearError(); 20 | bool GLCheckError(); 21 | -------------------------------------------------------------------------------- /ep16-renderer/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep17-textures/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep19-math/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep19-math/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep22-imgui/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep22-imgui/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep16-renderer/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep17-textures/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | public: 6 | IndexBuffer(const unsigned int* indices, unsigned int count); 7 | ~IndexBuffer(); 8 | 9 | void Bind() const; 10 | void Unbind() const; 11 | 12 | inline unsigned int GetCount() const { return m_Count; } 13 | 14 | private: 15 | unsigned int m_RendererID; 16 | unsigned int m_Count; 17 | }; 18 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/Renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define ASSERT(x) if (!(x)) assert(false) 8 | 9 | #ifdef DEBUG 10 | #define GLCall(x) GLClearError();\ 11 | x;\ 12 | ASSERT(GLCheckError()) 13 | #else 14 | #define GLCall(x) x 15 | #endif 16 | 17 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 18 | 19 | void GLClearError(); 20 | bool GLCheckError(); 21 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep24-test-framework/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep24-test-framework/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep20-projection-matrices/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep20-projection-matrices/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep21-model-view-projection/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | public: 6 | IndexBuffer(const unsigned int* indices, unsigned int count); 7 | ~IndexBuffer(); 8 | 9 | void Bind() const; 10 | void Unbind() const; 11 | 12 | inline unsigned int GetCount() const { return m_Count; } 13 | 14 | private: 15 | unsigned int m_RendererID; 16 | unsigned int m_Count; 17 | }; 18 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep21-model-view-projection/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/IndexBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class IndexBuffer 4 | { 5 | private: 6 | unsigned int m_RendererID; 7 | unsigned int m_Count; 8 | 9 | public: 10 | IndexBuffer(const unsigned int* indices, unsigned int count); 11 | ~IndexBuffer(); 12 | 13 | void Bind() const; 14 | void Unbind() const; 15 | 16 | inline unsigned int GetCount() const { return m_Count; } 17 | }; 18 | -------------------------------------------------------------------------------- /ep16-renderer/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep17-textures/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep19-math/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep22-imgui/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/VertexArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "VertexBuffer.h" 4 | #include "VertexBufferLayout.h" 5 | 6 | class VertexArray 7 | { 8 | private: 9 | unsigned int m_RendererID; 10 | 11 | public: 12 | VertexArray(); 13 | ~VertexArray(); 14 | 15 | void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); 16 | void Bind() const; 17 | void Unbind() const; 18 | }; 19 | -------------------------------------------------------------------------------- /ep24-test-framework/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void Renderer::Clear() const 5 | { 6 | GLCall( glClear( GL_COLOR_BUFFER_BIT ) ); 7 | } 8 | 9 | void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const 10 | { 11 | shader.Bind(); 12 | va.Bind(); 13 | ib.Bind(); 14 | GLCall( glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr) ); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestClearColor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "tests/Test.h" 4 | 5 | 6 | namespace test 7 | { 8 | class TestClearColor : public Test 9 | { 10 | public: 11 | TestClearColor(); 12 | ~TestClearColor(); 13 | 14 | void OnUpdate(float deltaTime) override; 15 | void OnRender() override; 16 | void OnImGuiRender() override; 17 | 18 | private: 19 | float m_ClearColor[4]; 20 | }; 21 | }; 22 | -------------------------------------------------------------------------------- /next-episode.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ROOT=$(dirname "$0") 4 | 5 | latest_episode=$1 6 | next_episode=$2 7 | 8 | latest_path=$(realpath "$latest_episode") 9 | next_path=$(realpath "$next_episode") 10 | 11 | latest_name=$(basename "$latest_path") 12 | next_name=$(basename "$next_path") 13 | 14 | echo Copying "$latest_name -> $next_name" 15 | cp --recursive "$latest_path" "$next_path" 16 | if ! grep -q "$next_name" "$ROOT/meson.build" ; then 17 | echo "subdir('$next_name')" >> "$ROOT/meson.build" 18 | fi 19 | -------------------------------------------------------------------------------- /ep17-textures/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | void main() 9 | { 10 | gl_Position = position; 11 | v_TexCoord = texCoord; 12 | } 13 | 14 | #shader fragment 15 | #version 330 core 16 | 17 | in vec2 v_TexCoord; 18 | layout(location = 0) out vec4 color; 19 | 20 | uniform sampler2D u_Texture; 21 | 22 | void main() 23 | { 24 | vec4 texColor = texture(u_Texture, v_TexCoord); 25 | color = texColor; 26 | } 27 | -------------------------------------------------------------------------------- /ep19-math/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep22-imgui/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep17-textures/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep24-test-framework/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Texture.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Renderer.h" 4 | 5 | class Texture 6 | { 7 | private: 8 | unsigned int m_RendererID; 9 | std::string m_FilePath; 10 | unsigned char* m_LocalBuffer; 11 | int m_Width, m_Height, m_BPP; 12 | 13 | public: 14 | Texture(const std::string& path); 15 | ~Texture(); 16 | 17 | void Bind(unsigned int slot = 0) const; 18 | void Unbind() const; 19 | 20 | inline int GetWidth() const { return m_Width; } 21 | inline int GetHeight() const { return m_Height; } 22 | }; 23 | -------------------------------------------------------------------------------- /ep24-test-framework/res/shaders/Complex.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | uniform mat4 u_MVP; 9 | 10 | void main() 11 | { 12 | gl_Position = u_MVP * position; 13 | v_TexCoord = texCoord; 14 | } 15 | 16 | #shader fragment 17 | #version 330 core 18 | 19 | layout(location = 0) out vec4 color; 20 | 21 | in vec2 v_TexCoord; 22 | 23 | uniform sampler2D u_Texture; 24 | 25 | void main() 26 | { 27 | vec4 texColor = texture(u_Texture, v_TexCoord); 28 | color = texColor; 29 | } 30 | -------------------------------------------------------------------------------- /ep19-math/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | uniform mat4 u_MVP; 9 | 10 | void main() 11 | { 12 | gl_Position = u_MVP * position; 13 | v_TexCoord = texCoord; 14 | } 15 | 16 | #shader fragment 17 | #version 330 core 18 | 19 | layout(location = 0) out vec4 color; 20 | 21 | in vec2 v_TexCoord; 22 | 23 | uniform vec4 u_Color; 24 | uniform sampler2D u_Texture; 25 | 26 | void main() 27 | { 28 | vec4 texColor = texture(u_Texture, v_TexCoord); 29 | color = texColor; 30 | } 31 | -------------------------------------------------------------------------------- /ep22-imgui/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | uniform mat4 u_MVP; 9 | 10 | void main() 11 | { 12 | gl_Position = u_MVP * position; 13 | v_TexCoord = texCoord; 14 | } 15 | 16 | #shader fragment 17 | #version 330 core 18 | 19 | layout(location = 0) out vec4 color; 20 | 21 | in vec2 v_TexCoord; 22 | 23 | uniform vec4 u_Color; 24 | uniform sampler2D u_Texture; 25 | 26 | void main() 27 | { 28 | vec4 texColor = texture(u_Texture, v_TexCoord); 29 | color = texColor; 30 | } 31 | -------------------------------------------------------------------------------- /ep20-projection-matrices/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | uniform mat4 u_MVP; 9 | 10 | void main() 11 | { 12 | gl_Position = u_MVP * position; 13 | v_TexCoord = texCoord; 14 | } 15 | 16 | #shader fragment 17 | #version 330 core 18 | 19 | layout(location = 0) out vec4 color; 20 | 21 | in vec2 v_TexCoord; 22 | 23 | uniform vec4 u_Color; 24 | uniform sampler2D u_Texture; 25 | 26 | void main() 27 | { 28 | vec4 texColor = texture(u_Texture, v_TexCoord); 29 | color = texColor; 30 | } 31 | -------------------------------------------------------------------------------- /ep21-model-view-projection/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | uniform mat4 u_MVP; 9 | 10 | void main() 11 | { 12 | gl_Position = u_MVP * position; 13 | v_TexCoord = texCoord; 14 | } 15 | 16 | #shader fragment 17 | #version 330 core 18 | 19 | layout(location = 0) out vec4 color; 20 | 21 | in vec2 v_TexCoord; 22 | 23 | uniform vec4 u_Color; 24 | uniform sampler2D u_Texture; 25 | 26 | void main() 27 | { 28 | vec4 texColor = texture(u_Texture, v_TexCoord); 29 | color = texColor; 30 | } 31 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/res/shaders/Basic.shader: -------------------------------------------------------------------------------- 1 | #shader vertex 2 | #version 330 core 3 | layout(location = 0) in vec4 position; 4 | layout(location = 1) in vec2 texCoord; 5 | 6 | out vec2 v_TexCoord; 7 | 8 | uniform mat4 u_MVP; 9 | 10 | void main() 11 | { 12 | gl_Position = u_MVP * position; 13 | v_TexCoord = texCoord; 14 | } 15 | 16 | #shader fragment 17 | #version 330 core 18 | 19 | layout(location = 0) out vec4 color; 20 | 21 | in vec2 v_TexCoord; 22 | 23 | uniform vec4 u_Color; 24 | uniform sampler2D u_Texture; 25 | 26 | void main() 27 | { 28 | vec4 texColor = texture(u_Texture, v_TexCoord); 29 | color = texColor; 30 | } 31 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | LDFLAGS=-L$(current_directory)/../lib 11 | LDFLAGS+=-lglfw3 12 | LDFLAGS+=-lGLEW 13 | 14 | SOURCES=$(wildcard *.cpp) 15 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 16 | 17 | 18 | %.o: %.cpp 19 | $(CC) $(CFLAGS) -c -o $@ $^ 20 | 21 | app: $(OBJECTS) 22 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o app $(OBJECTS) 23 | 24 | all: app 25 | 26 | .PHONY: clean 27 | clean: 28 | rm -f *.o app 29 | -------------------------------------------------------------------------------- /ep19-math/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep22-imgui/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Renderer.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep16-renderer/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep17-textures/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep24-test-framework/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Renderer.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep20-projection-matrices/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep21-model-view-projection/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Debug.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexBuffer.h" 2 | #include "Renderer.h" 3 | 4 | VertexBuffer::VertexBuffer(const void* data, unsigned int size) 5 | { 6 | GLCall( glGenBuffers(1, &m_RendererID) ); 7 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 8 | GLCall( glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW) ); 9 | } 10 | 11 | VertexBuffer::~VertexBuffer() 12 | { 13 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 14 | } 15 | 16 | void VertexBuffer::Bind() const 17 | { 18 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_RendererID) ); 19 | } 20 | 21 | void VertexBuffer::Unbind() const 22 | { 23 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 24 | } 25 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestClearColor.cpp: -------------------------------------------------------------------------------- 1 | #include "tests/TestClearColor.h" 2 | 3 | #include "Debug.h" 4 | #include "imgui.h" 5 | 6 | namespace test 7 | { 8 | TestClearColor::TestClearColor() 9 | : m_ClearColor { 0.2f, 0.3f, 0.8f, 1.0f } 10 | { 11 | } 12 | 13 | TestClearColor::~TestClearColor() 14 | { 15 | } 16 | 17 | void TestClearColor::OnUpdate(float deltaTime) 18 | { 19 | } 20 | 21 | void TestClearColor::OnRender() 22 | { 23 | GLCall(glClearColor(m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3])); 24 | GLCall(glClear(GL_COLOR_BUFFER_BIT)); 25 | } 26 | 27 | void TestClearColor::OnImGuiRender() 28 | { 29 | ImGui::ColorEdit4("Clear Color", m_ClearColor); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestTriangle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "tests/Test.h" 4 | #include "VertexArray.h" 5 | #include "VertexBuffer.h" 6 | #include "IndexBuffer.h" 7 | #include "Shader.h" 8 | #include "Renderer.h" 9 | 10 | namespace test 11 | { 12 | class TestTriangle : public Test 13 | { 14 | public: 15 | TestTriangle(); 16 | ~TestTriangle(); 17 | 18 | void OnUpdate(float deltaTime) override; 19 | void OnRender() override; 20 | void OnImGuiRender() override; 21 | 22 | private: 23 | float m_ClearColor[4]; 24 | float m_Positions[6]; 25 | 26 | VertexArray m_va; 27 | VertexBuffer m_vb; 28 | VertexBufferLayout m_layout; 29 | Shader m_shader; 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /ep19-math/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep22-imgui/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep16-renderer/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep17-textures/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep24-test-framework/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Renderer.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Renderer.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep20-projection-matrices/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep21-model-view-projection/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Debug.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/IndexBuffer.cpp: -------------------------------------------------------------------------------- 1 | #include "IndexBuffer.h" 2 | #include "Renderer.h" 3 | 4 | IndexBuffer::IndexBuffer(const unsigned int* indices, unsigned int count) 5 | : 6 | m_Count(count) 7 | { 8 | ASSERT(sizeof(unsigned int) == sizeof(GLuint)); 9 | 10 | GLCall( glGenBuffers(1, &m_RendererID) ); 11 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 12 | GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), indices, GL_STATIC_DRAW) ); 13 | } 14 | 15 | IndexBuffer::~IndexBuffer() 16 | { 17 | GLCall( glDeleteBuffers(1, &m_RendererID) ); 18 | } 19 | 20 | void IndexBuffer::Bind() const 21 | { 22 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID) ); 23 | } 24 | 25 | void IndexBuffer::Unbind() const 26 | { 27 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 28 | } 29 | -------------------------------------------------------------------------------- /ep16-renderer/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | LDFLAGS=-L$(current_directory)/../lib 11 | LDFLAGS+=-lglfw3 12 | LDFLAGS+=-lGLEW 13 | 14 | SOURCES=$(wildcard *.cpp) 15 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 16 | 17 | 18 | %.o: %.cpp 19 | $(CC) $(CFLAGS) -c -o $@ $^ 20 | 21 | default: debug 22 | 23 | app: $(OBJECTS) 24 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 25 | 26 | # Define debug and -g enables debug symbols 27 | debug: CFLAGS+=-DDEBUG -g 28 | debug: app 29 | 30 | release: app 31 | 32 | .PHONY: clean 33 | clean: 34 | rm -f *.o app 35 | 36 | .PHONY: debugger 37 | debugger: debug 38 | PATH=/usr/bin /usr/bin/lldb ./app 39 | 40 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | LDFLAGS=-L$(current_directory)/../lib 11 | LDFLAGS+=-lglfw3 12 | LDFLAGS+=-lGLEW 13 | 14 | SOURCES=$(wildcard *.cpp) 15 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 16 | 17 | 18 | %.o: %.cpp 19 | $(CC) $(CFLAGS) -c -o $@ $^ 20 | 21 | default: debug 22 | 23 | app: $(OBJECTS) 24 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 25 | 26 | # Define debug and -g enables debug symbols 27 | debug: CFLAGS+=-DDEBUG -g 28 | debug: app 29 | 30 | release: app 31 | 32 | .PHONY: clean 33 | clean: 34 | rm -f *.o app 35 | 36 | .PHONY: debugger 37 | debugger: debug 38 | PATH=/usr/bin /usr/bin/lldb ./app 39 | 40 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | LDFLAGS=-L$(current_directory)/../lib 11 | LDFLAGS+=-lglfw3 12 | LDFLAGS+=-lGLEW 13 | 14 | SOURCES=$(wildcard *.cpp) 15 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 16 | 17 | 18 | %.o: %.cpp 19 | $(CC) $(CFLAGS) -c -o $@ $^ 20 | 21 | default: debug 22 | 23 | app: $(OBJECTS) 24 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 25 | 26 | # Define debug and -g enables debug symbols 27 | debug: CFLAGS+=-DDEBUG -g 28 | debug: app 29 | 30 | release: app 31 | 32 | .PHONY: clean 33 | clean: 34 | rm -f *.o app 35 | 36 | .PHONY: debugger 37 | debugger: debug 38 | PATH=/usr/bin /usr/bin/lldb ./app 39 | 40 | -------------------------------------------------------------------------------- /ep19-math/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 21 | 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o app 40 | 41 | .PHONY: debugger 42 | debugger: debug 43 | PATH=/usr/bin /usr/bin/lldb ./app 44 | 45 | -------------------------------------------------------------------------------- /ep17-textures/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 21 | 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o app 40 | 41 | .PHONY: debugger 42 | debugger: debug 43 | PATH=/usr/bin /usr/bin/lldb ./app 44 | 45 | -------------------------------------------------------------------------------- /ep02-window/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | GLFWwindow* window; 6 | 7 | /* Initialize the library */ 8 | if (!glfwInit()) 9 | return -1; 10 | 11 | /* Create a windowed mode window and its OpenGL context */ 12 | window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 13 | if (!window) 14 | { 15 | glfwTerminate(); 16 | return -1; 17 | } 18 | 19 | /* Make the window's context current */ 20 | glfwMakeContextCurrent(window); 21 | 22 | /* Loop until the user closes the window */ 23 | while (!glfwWindowShouldClose(window)) 24 | { 25 | /* Render here */ 26 | glClear(GL_COLOR_BUFFER_BIT); 27 | 28 | glBegin(GL_TRIANGLES); 29 | glVertex2f(-0.5f, -0.5f); 30 | glVertex2f(0.5f, -0.5f); 31 | glVertex2f(0.0f, 0.5f); 32 | glEnd(); 33 | /* Swap front and back buffers */ 34 | glfwSwapBuffers(window); 35 | 36 | /* Poll for and process events */ 37 | glfwPollEvents(); 38 | } 39 | 40 | glfwTerminate(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /ep20-projection-matrices/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 21 | 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o app 40 | 41 | .PHONY: debugger 42 | debugger: debug 43 | PATH=/usr/bin /usr/bin/lldb ./app 44 | 45 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestUniform.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "tests/Test.h" 4 | 5 | #include "VertexArray.h" 6 | #include "VertexBuffer.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | #include "Renderer.h" 10 | 11 | namespace test 12 | { 13 | class TestUniform : public Test 14 | { 15 | public: 16 | TestUniform(); 17 | ~TestUniform(); 18 | 19 | void OnUpdate(float deltaTime) override; 20 | void OnRender() override; 21 | void OnImGuiRender() override; 22 | 23 | private: 24 | float m_ClearColor[4]; 25 | float m_ObjectColor[4]; 26 | float m_Positions[8]; 27 | unsigned int m_Indices[6]; 28 | float m_Step; 29 | int m_Direction; 30 | bool m_Oscillate; 31 | 32 | VertexArray m_va; 33 | IndexBuffer m_ib; 34 | VertexBuffer m_vb; 35 | VertexBufferLayout m_layout; 36 | Shader m_shader; 37 | Renderer m_renderer; 38 | }; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 21 | 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o app 40 | 41 | .PHONY: debugger 42 | debugger: debug 43 | PATH=/usr/bin /usr/bin/lldb ./app 44 | 45 | -------------------------------------------------------------------------------- /ep22-imgui/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 21 | 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o 40 | rm -f vendor/*/*.o 41 | rm -f app 42 | rm -f imgui.ini 43 | 44 | .PHONY: debugger 45 | debugger: debug 46 | PATH=/usr/bin /usr/bin/lldb ./app 47 | 48 | -------------------------------------------------------------------------------- /ep16-renderer/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | struct ShaderProgramSource 7 | { 8 | std::string VertexSource; 9 | std::string FragmentSource; 10 | }; 11 | 12 | class Shader 13 | { 14 | private: 15 | unsigned int m_RendererID; 16 | std::string m_FilePath; 17 | std::unordered_map m_UniformLocationCache; 18 | 19 | public: 20 | Shader(const std::string& filepath); 21 | ~Shader(); 22 | 23 | void Bind() const; 24 | void Unbind() const; 25 | 26 | // Set uniforms 27 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 28 | void SetUniform1f(const std::string& name, float value); 29 | 30 | private: 31 | int GetUniformLocation(const std::string& name); 32 | struct ShaderProgramSource ParseShader(const std::string& filepath); 33 | unsigned int CompileShader(unsigned int type, const std::string& source); 34 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 35 | 36 | }; 37 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | struct ShaderProgramSource 7 | { 8 | std::string VertexSource; 9 | std::string FragmentSource; 10 | }; 11 | 12 | class Shader 13 | { 14 | private: 15 | unsigned int m_RendererID; 16 | std::string m_FilePath; 17 | std::unordered_map m_UniformLocationCache; 18 | 19 | public: 20 | Shader(const std::string& filepath); 21 | ~Shader(); 22 | 23 | void Bind() const; 24 | void Unbind() const; 25 | 26 | // Set uniforms 27 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 28 | void SetUniform1f(const std::string& name, float value); 29 | 30 | private: 31 | int GetUniformLocation(const std::string& name); 32 | struct ShaderProgramSource ParseShader(const std::string& filepath); 33 | unsigned int CompileShader(unsigned int type, const std::string& source); 34 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 35 | 36 | }; 37 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 21 | 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o 40 | rm -f vendor/*/*.o 41 | rm -f app 42 | rm -f imgui.ini 43 | 44 | .PHONY: debugger 45 | debugger: debug 46 | PATH=/usr/bin /usr/bin/lldb ./app 47 | 48 | -------------------------------------------------------------------------------- /ep24-test-framework/Makefile: -------------------------------------------------------------------------------- 1 | CC=clang++ 2 | current_directory=$(shell pwd) 3 | 4 | FRAMEWORKS=-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo 5 | 6 | CFLAGS=-std=c++11 7 | CFLAGS+=-I$(current_directory) 8 | CFLAGS+=-I$(current_directory)/../external 9 | 10 | VENDOR_DIRS=$(wildcard $(current_directory)/vendor/*) 11 | VENDOR_INCLUDES=$(patsubst %, -I%, $(VENDOR_DIRS)) 12 | CFLAGS+=$(VENDOR_INCLUDES) 13 | 14 | LDFLAGS=-L$(current_directory)/../lib 15 | LDFLAGS+=-lglfw3 16 | LDFLAGS+=-lGLEW 17 | 18 | SOURCES=$(wildcard *.cpp) 19 | SOURCES+=$(wildcard vendor/*/*.cpp) 20 | SOURCES+=$(wildcard tests/*.cpp) 21 | OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES)) 22 | 23 | %.o: %.cpp 24 | $(CC) $(CFLAGS) -c -o $@ $^ 25 | 26 | default: debug 27 | 28 | app: $(OBJECTS) 29 | $(CC) $(CFLAGS) $(LDFLAGS) $(FRAMEWORKS) -o $@ $(OBJECTS) 30 | 31 | # Define debug and -g enables debug symbols 32 | debug: CFLAGS+=-DDEBUG -g 33 | debug: app 34 | 35 | release: app 36 | 37 | .PHONY: clean 38 | clean: 39 | rm -f *.o 40 | rm -f vendor/*/*.o 41 | rm -f app 42 | rm -f imgui.ini 43 | rm -f tests/*.o 44 | 45 | .PHONY: debugger 46 | debugger: debug 47 | PATH=/usr/bin /usr/bin/lldb ./app 48 | 49 | -------------------------------------------------------------------------------- /ep19-math/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep16-renderer/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep17-textures/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | struct ShaderProgramSource 7 | { 8 | std::string VertexSource; 9 | std::string FragmentSource; 10 | }; 11 | 12 | class Shader 13 | { 14 | private: 15 | unsigned int m_RendererID; 16 | std::string m_FilePath; 17 | std::unordered_map m_UniformLocationCache; 18 | 19 | public: 20 | Shader(const std::string& filepath); 21 | ~Shader(); 22 | 23 | void Bind() const; 24 | void Unbind() const; 25 | 26 | // Set uniforms 27 | void SetUniform1i(const std::string& name, int value); 28 | void SetUniform1f(const std::string& name, float value); 29 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 30 | 31 | private: 32 | int GetUniformLocation(const std::string& name); 33 | struct ShaderProgramSource ParseShader(const std::string& filepath); 34 | unsigned int CompileShader(unsigned int type, const std::string& source); 35 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 36 | 37 | }; 38 | -------------------------------------------------------------------------------- /ep17-textures/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep22-imgui/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep24-test-framework/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep20-projection-matrices/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Renderer.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep21-model-view-projection/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Renderer.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | #include "VertexArray.h" 2 | #include "Debug.h" 3 | 4 | VertexArray::VertexArray() 5 | { 6 | GLCall( glGenVertexArrays(1, &m_RendererID) ); 7 | } 8 | 9 | VertexArray::~VertexArray() 10 | { 11 | GLCall( glDeleteVertexArrays(1, &m_RendererID) ); 12 | } 13 | 14 | void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) 15 | { 16 | Bind(); 17 | vb.Bind(); 18 | const std::vector elements = layout.GetElements(); 19 | unsigned int offset = 0; 20 | for (unsigned int i = 0; i < elements.size() ; i++) 21 | { 22 | const VertexBufferElement element = elements[i]; 23 | GLCall( glEnableVertexAttribArray(i) ); 24 | GLCall( glVertexAttribPointer(i, element.count, element.type, element.normalized, 25 | layout.GetStride(), INT2VOIDP(offset)) ); 26 | offset += element.count * VertexBufferElement::GetSizeOfType(element.type); 27 | } 28 | } 29 | 30 | void VertexArray::Bind() const 31 | { 32 | GLCall( glBindVertexArray(m_RendererID) ); 33 | } 34 | 35 | void VertexArray::Unbind() const 36 | { 37 | GLCall( glBindVertexArray(0) ); 38 | }; 39 | -------------------------------------------------------------------------------- /ep19-math/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "glm/glm.hpp" 7 | 8 | struct ShaderProgramSource 9 | { 10 | std::string VertexSource; 11 | std::string FragmentSource; 12 | }; 13 | 14 | class Shader 15 | { 16 | private: 17 | unsigned int m_RendererID; 18 | std::string m_FilePath; 19 | std::unordered_map m_UniformLocationCache; 20 | 21 | public: 22 | Shader(const std::string& filepath); 23 | ~Shader(); 24 | 25 | void Bind() const; 26 | void Unbind() const; 27 | 28 | // Set uniforms 29 | void SetUniform1i(const std::string& name, int value); 30 | void SetUniform1f(const std::string& name, float value); 31 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 32 | void SetUniformMat4f(const std::string& name, const glm::mat4& matrix); 33 | 34 | private: 35 | int GetUniformLocation(const std::string& name); 36 | struct ShaderProgramSource ParseShader(const std::string& filepath); 37 | unsigned int CompileShader(unsigned int type, const std::string& source); 38 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /ep22-imgui/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "glm/glm.hpp" 7 | 8 | struct ShaderProgramSource 9 | { 10 | std::string VertexSource; 11 | std::string FragmentSource; 12 | }; 13 | 14 | class Shader 15 | { 16 | private: 17 | unsigned int m_RendererID; 18 | std::string m_FilePath; 19 | std::unordered_map m_UniformLocationCache; 20 | 21 | public: 22 | Shader(const std::string& filepath); 23 | ~Shader(); 24 | 25 | void Bind() const; 26 | void Unbind() const; 27 | 28 | // Set uniforms 29 | void SetUniform1i(const std::string& name, int value); 30 | void SetUniform1f(const std::string& name, float value); 31 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 32 | void SetUniformMat4f(const std::string& name, const glm::mat4& matrix); 33 | 34 | private: 35 | int GetUniformLocation(const std::string& name); 36 | struct ShaderProgramSource ParseShader(const std::string& filepath); 37 | unsigned int CompileShader(unsigned int type, const std::string& source); 38 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /ep24-test-framework/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "glm/glm.hpp" 7 | 8 | struct ShaderProgramSource 9 | { 10 | std::string VertexSource; 11 | std::string FragmentSource; 12 | }; 13 | 14 | class Shader 15 | { 16 | private: 17 | unsigned int m_RendererID; 18 | std::string m_FilePath; 19 | std::unordered_map m_UniformLocationCache; 20 | 21 | public: 22 | Shader(const std::string& filepath); 23 | ~Shader(); 24 | 25 | void Bind() const; 26 | void Unbind() const; 27 | 28 | // Set uniforms 29 | void SetUniform1i(const std::string& name, int value); 30 | void SetUniform1f(const std::string& name, float value); 31 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 32 | void SetUniformMat4f(const std::string& name, const glm::mat4& matrix); 33 | 34 | private: 35 | int GetUniformLocation(const std::string& name); 36 | struct ShaderProgramSource ParseShader(const std::string& filepath); 37 | unsigned int CompileShader(unsigned int type, const std::string& source); 38 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "glm/glm.hpp" 7 | 8 | struct ShaderProgramSource 9 | { 10 | std::string VertexSource; 11 | std::string FragmentSource; 12 | }; 13 | 14 | class Shader 15 | { 16 | private: 17 | unsigned int m_RendererID; 18 | std::string m_FilePath; 19 | std::unordered_map m_UniformLocationCache; 20 | 21 | public: 22 | Shader(const std::string& filepath); 23 | ~Shader(); 24 | 25 | void Bind() const; 26 | void Unbind() const; 27 | 28 | // Set uniforms 29 | void SetUniform1i(const std::string& name, int value); 30 | void SetUniform1f(const std::string& name, float value); 31 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 32 | void SetUniformMat4f(const std::string& name, const glm::mat4& matrix); 33 | 34 | private: 35 | int GetUniformLocation(const std::string& name); 36 | struct ShaderProgramSource ParseShader(const std::string& filepath); 37 | unsigned int CompileShader(unsigned int type, const std::string& source); 38 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "glm/glm.hpp" 7 | 8 | struct ShaderProgramSource 9 | { 10 | std::string VertexSource; 11 | std::string FragmentSource; 12 | }; 13 | 14 | class Shader 15 | { 16 | private: 17 | unsigned int m_RendererID; 18 | std::string m_FilePath; 19 | std::unordered_map m_UniformLocationCache; 20 | 21 | public: 22 | Shader(const std::string& filepath); 23 | ~Shader(); 24 | 25 | void Bind() const; 26 | void Unbind() const; 27 | 28 | // Set uniforms 29 | void SetUniform1i(const std::string& name, int value); 30 | void SetUniform1f(const std::string& name, float value); 31 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 32 | void SetUniformMat4f(const std::string& name, const glm::mat4& matrix); 33 | 34 | private: 35 | int GetUniformLocation(const std::string& name); 36 | struct ShaderProgramSource ParseShader(const std::string& filepath); 37 | unsigned int CompileShader(unsigned int type, const std::string& source); 38 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "glm/glm.hpp" 7 | 8 | struct ShaderProgramSource 9 | { 10 | std::string VertexSource; 11 | std::string FragmentSource; 12 | }; 13 | 14 | class Shader 15 | { 16 | private: 17 | unsigned int m_RendererID; 18 | std::string m_FilePath; 19 | std::unordered_map m_UniformLocationCache; 20 | 21 | public: 22 | Shader(const std::string& filepath); 23 | ~Shader(); 24 | 25 | void Bind() const; 26 | void Unbind() const; 27 | 28 | // Set uniforms 29 | void SetUniform1i(const std::string& name, int value); 30 | void SetUniform1f(const std::string& name, float value); 31 | void SetUniform4f(const std::string& name, float f0, float f1, float f2, float f3); 32 | void SetUniformMat4f(const std::string& name, const glm::mat4& matrix); 33 | 34 | private: 35 | int GetUniformLocation(const std::string& name); 36 | struct ShaderProgramSource ParseShader(const std::string& filepath); 37 | unsigned int CompileShader(unsigned int type, const std::string& source); 38 | unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include "tests/TestTriangle.h" 2 | 3 | #include "Debug.h" 4 | #include "imgui.h" 5 | 6 | namespace test 7 | { 8 | TestTriangle::TestTriangle() : 9 | m_ClearColor { 0.8f, 0.3f, 0.2f, 1.0f }, 10 | m_Positions { 11 | -0.5f, -0.5f, // 0 12 | 0.5f, -0.5f, // 1 13 | 0.5f, 0.5f, // 2 14 | }, 15 | m_va(), 16 | m_vb(m_Positions, 3 * 2 * sizeof(float)), 17 | m_layout(), 18 | m_shader("res/shaders/Simple.shader") 19 | { 20 | m_layout.AddFloat(2); 21 | m_va.AddBuffer(m_vb, m_layout); 22 | } 23 | 24 | TestTriangle::~TestTriangle() 25 | { 26 | m_shader.Unbind(); 27 | } 28 | 29 | void TestTriangle::OnUpdate(float deltaTime) 30 | { 31 | } 32 | 33 | void TestTriangle::OnRender() 34 | { 35 | GLCall(glClear(GL_COLOR_BUFFER_BIT)); 36 | GLCall(glClearColor(m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3])); 37 | 38 | m_va.Bind(); 39 | m_shader.Bind(); 40 | 41 | // 3 indices starting at 0 -> 1 triangle 42 | GLCall( glDrawArrays(GL_TRIANGLES, 0, 3) ); 43 | } 44 | 45 | void TestTriangle::OnImGuiRender() 46 | { 47 | ImGui::ColorEdit4("Clear Color", m_ClearColor); 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /ep19-math/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /ep22-imgui/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # the-cherno 2 | My code-along for TheCherno tutorials. 3 | 4 | **THIS PORT IS STILL A WORK IN PROGRESS** 5 | ## Episodes ported 6 | * ep02 7 | * ep03 8 | * ep04 9 | * ep05 10 | * ep07 11 | * ep08 12 | 13 | The code here is intended to run on Linux. If you want to compile on MacOS, see the corresponding branch named MacOS. 14 | For Windows you will need to come up with a different build system on your own. 15 | 16 | Any differences between TheCherno's code and mine is likely a result of my compilation environment. 17 | YMMV. 18 | 19 | The repo has been reworked to use meson/ninja. 20 | To compile any episode, cd to that directory and run `make`. 21 | To run the resulting app, run `make run`. 22 | 23 | All episodes compile into a binary named **app**. 24 | 25 | I chose to install glfw , glew and glut dependencies via package manager instead of moving them into the repo. To get those, run: 26 | 27 | sudo apt install libglfw3-dev freeglut3-dev libglew-dev 28 | 29 | At some point, I may bring those libraries in or try to add them as meson subprojects. 30 | 31 | Currently working through [TheCherno's OpenGL tutorial](https://www.youtube.com/watch?v=W3gAzLwfIP0&list=PLlrATfBNZ98foTJPJ_Ev03o2oq3-GGOS2 "TheCherno OpenGL Tutorial"). 32 | 33 | See all of his videos [here](https://www.youtube.com/channel/UCQ-W1KE9EYfdxhL6S4twUNw "TheChernoProject"). 34 | 35 | Support him on [his Patreon page](https://www.patreon.com/thecherno/overview "TheCherno Patreon") if you want to see more! 36 | -------------------------------------------------------------------------------- /ep17-textures/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestMultipleObjects.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "tests/Test.h" 4 | 5 | #include "VertexArray.h" 6 | #include "VertexBuffer.h" 7 | #include "IndexBuffer.h" 8 | #include "Shader.h" 9 | #include "Texture.h" 10 | #include "Renderer.h" 11 | 12 | #include "glm/glm.hpp" 13 | #include "glm/gtc/matrix_transform.hpp" 14 | 15 | namespace test 16 | { 17 | class TestMultipleObjects : public Test 18 | { 19 | public: 20 | TestMultipleObjects(); 21 | ~TestMultipleObjects(); 22 | 23 | void OnUpdate(float deltaTime) override; 24 | void OnRender() override; 25 | void OnImGuiRender() override; 26 | 27 | private: 28 | // data members 29 | float m_ClearColor[4]; 30 | float m_Positions[16]; 31 | unsigned int m_Indices[6]; 32 | 33 | // opengl members 34 | VertexArray m_va; 35 | IndexBuffer m_ib; 36 | VertexBuffer m_vb; 37 | VertexBufferLayout m_layout; 38 | Shader m_shader; 39 | Texture m_texture; 40 | Renderer m_renderer; 41 | 42 | // MVP members 43 | glm::mat4 m_proj; 44 | glm::mat4 m_view; 45 | // model and mvp will be created per object per draw 46 | 47 | // Translation members 48 | glm::vec3 m_translationA; 49 | glm::vec3 m_translationB; 50 | }; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /ep24-test-framework/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Texture.cpp: -------------------------------------------------------------------------------- 1 | #include "Texture.h" 2 | 3 | #include "vendor/stb_image/stb_image.h" 4 | 5 | Texture::Texture(const std::string& path) 6 | : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) 7 | { 8 | stbi_set_flip_vertically_on_load(1); 9 | m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); 10 | GLCall( glGenTextures(1, &m_RendererID) ); 11 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); // Bind without slot selection 12 | 13 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); 14 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); 15 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) ); 16 | GLCall( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) ); 17 | 18 | GLCall( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer) ); 19 | Unbind(); 20 | 21 | if (m_LocalBuffer) 22 | stbi_image_free(m_LocalBuffer); 23 | }; 24 | 25 | Texture::~Texture() 26 | { 27 | GLCall( glDeleteTextures(1, &m_RendererID) ); 28 | } 29 | 30 | void Texture::Bind(unsigned int slot) const 31 | { 32 | GLCall( glActiveTexture(GL_TEXTURE0 + slot) ); 33 | GLCall( glBindTexture(GL_TEXTURE_2D, m_RendererID) ); 34 | } 35 | 36 | void Texture::Unbind() const 37 | { 38 | GLCall( glBindTexture(GL_TEXTURE_2D, 0) ); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /ep03-modern-opengl/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | GLFWwindow* window; 9 | 10 | /* Initialize the library */ 11 | if (!glfwInit()) 12 | return -1; 13 | 14 | /* Create a windowed mode window and its OpenGL context */ 15 | window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 16 | if (!window) 17 | { 18 | glfwTerminate(); 19 | return -1; 20 | } 21 | 22 | /* Make the window's context current */ 23 | glfwMakeContextCurrent(window); 24 | 25 | GLenum err = glewInit(); 26 | if (GLEW_OK != err) 27 | { 28 | // Problem: glewInit failed 29 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); 30 | return 1; 31 | } 32 | fprintf(stdout, "GLEW version: %s\n", glewGetString(GLEW_VERSION)); 33 | fprintf(stdout, "GL version: %s\n", glGetString(GL_VERSION)); 34 | 35 | /* Loop until the user closes the window */ 36 | while (!glfwWindowShouldClose(window)) 37 | { 38 | /* Render here */ 39 | glClear(GL_COLOR_BUFFER_BIT); 40 | 41 | glBegin(GL_TRIANGLES); 42 | glVertex2f(-0.5f, -0.5f); 43 | glVertex2f(0.5f, -0.5f); 44 | glVertex2f(0.0f, 0.5f); 45 | glEnd(); 46 | /* Swap front and back buffers */ 47 | glfwSwapBuffers(window); 48 | 49 | /* Poll for and process events */ 50 | glfwPollEvents(); 51 | } 52 | 53 | glfwTerminate(); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Renderer.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 33 | { 34 | m_Elements.push_back({type, count, normalized}); 35 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 36 | }; 37 | 38 | public: 39 | VertexBufferLayout() : 40 | m_Stride(0) { } 41 | 42 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 43 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 44 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 45 | 46 | inline const std::vector GetElements() const { return m_Elements; }; 47 | inline unsigned int GetStride() const { return m_Stride; }; 48 | }; 49 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Renderer.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | m_Elements.push_back({type, count, normalized}); 47 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 48 | }; 49 | }; 50 | -------------------------------------------------------------------------------- /ep16-renderer/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep17-textures/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep19-math/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep22-imgui/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep24-test-framework/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep20-projection-matrices/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep21-model-view-projection/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/VertexBufferLayout.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "Debug.h" 6 | 7 | struct VertexBufferElement 8 | { 9 | unsigned int type; 10 | unsigned int count; 11 | unsigned char normalized; 12 | 13 | static unsigned int GetSizeOfType(unsigned int type) 14 | { 15 | switch (type) 16 | { 17 | case GL_FLOAT : return sizeof(GLfloat); 18 | case GL_UNSIGNED_INT : return sizeof(GLuint); 19 | case GL_UNSIGNED_BYTE : return sizeof(GLbyte); 20 | } 21 | ASSERT(false); 22 | return 0; 23 | } 24 | }; 25 | 26 | class VertexBufferLayout 27 | { 28 | private: 29 | unsigned int m_Stride; 30 | std::vector m_Elements; 31 | 32 | public: 33 | VertexBufferLayout() : 34 | m_Stride(0) { } 35 | 36 | void AddFloat(unsigned int count) { Push(GL_FLOAT, count, GL_FALSE); } 37 | void AddUnsignedInt(unsigned int count) { Push(GL_UNSIGNED_INT, count, GL_FALSE); } 38 | void AddUnsignedByte(unsigned int count) { Push(GL_UNSIGNED_BYTE, count, GL_TRUE); } 39 | 40 | inline const std::vector GetElements() const { return m_Elements; }; 41 | inline unsigned int GetStride() const { return m_Stride; }; 42 | 43 | private: 44 | void Push(unsigned int type, unsigned int count, unsigned char normalized) 45 | { 46 | struct VertexBufferElement vbe = {type, count, normalized}; 47 | m_Elements.push_back(vbe); 48 | m_Stride += count * VertexBufferElement::GetSizeOfType(type); 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /ep04-vertex-buffers/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | GLFWwindow* window; 9 | 10 | /* Initialize the library */ 11 | if (!glfwInit()) 12 | return -1; 13 | 14 | /* Create a windowed mode window and its OpenGL context */ 15 | window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 16 | if (!window) 17 | { 18 | glfwTerminate(); 19 | return -1; 20 | } 21 | 22 | /* Make the window's context current */ 23 | glfwMakeContextCurrent(window); 24 | 25 | GLenum err = glewInit(); 26 | if (GLEW_OK != err) 27 | { 28 | // Problem: glewInit failed 29 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); 30 | return 1; 31 | } 32 | fprintf(stdout, "GLEW version: %s\n", glewGetString(GLEW_VERSION)); 33 | fprintf(stdout, "GL version: %s\n", glGetString(GL_VERSION)); 34 | 35 | float positions[6] = { 36 | -0.5f, -0.5f, 37 | 0.5f, -0.5f, 38 | 0.0f, 0.5f 39 | }; 40 | 41 | unsigned int buffer; 42 | glGenBuffers(1, &buffer); 43 | glBindBuffer(GL_ARRAY_BUFFER, buffer); 44 | glBufferData(GL_ARRAY_BUFFER, sizeof(float)*6, positions, GL_STATIC_DRAW); 45 | 46 | /* Loop until the user closes the window */ 47 | while (!glfwWindowShouldClose(window)) 48 | { 49 | /* Render here */ 50 | glClear(GL_COLOR_BUFFER_BIT); 51 | 52 | glDrawArrays(GL_TRIANGLES, 0, 3); 53 | 54 | /* Swap front and back buffers */ 55 | glfwSwapBuffers(window); 56 | 57 | /* Poll for and process events */ 58 | glfwPollEvents(); 59 | } 60 | 61 | glfwTerminate(); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /ep05-vertex-attributes/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | GLFWwindow* window; 9 | 10 | /* Initialize the library */ 11 | if (!glfwInit()) 12 | return -1; 13 | 14 | /* Create a windowed mode window and its OpenGL context */ 15 | window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 16 | if (!window) 17 | { 18 | glfwTerminate(); 19 | return -1; 20 | } 21 | 22 | /* Make the window's context current */ 23 | glfwMakeContextCurrent(window); 24 | 25 | GLenum err = glewInit(); 26 | if (GLEW_OK != err) 27 | { 28 | // Problem: glewInit failed 29 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); 30 | return 1; 31 | } 32 | fprintf(stdout, "GLEW version: %s\n", glewGetString(GLEW_VERSION)); 33 | fprintf(stdout, "GL version: %s\n", glGetString(GL_VERSION)); 34 | 35 | float positions[6] = { 36 | -0.5f, -0.5f, 37 | 0.5f, -0.5f, 38 | 0.0f, 0.5f 39 | }; 40 | 41 | unsigned int buffer; 42 | glGenBuffers(1, &buffer); 43 | glBindBuffer(GL_ARRAY_BUFFER, buffer); 44 | glBufferData(GL_ARRAY_BUFFER, sizeof(float)*6, positions, GL_STATIC_DRAW); 45 | 46 | glEnableVertexAttribArray(0); 47 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0); 48 | 49 | glBindBuffer(GL_ARRAY_BUFFER, 0); 50 | 51 | /* Loop until the user closes the window */ 52 | while (!glfwWindowShouldClose(window)) 53 | { 54 | /* Render here */ 55 | glClear(GL_COLOR_BUFFER_BIT); 56 | 57 | glDrawArrays(GL_TRIANGLES, 0, 3); 58 | 59 | /* Swap front and back buffers */ 60 | glfwSwapBuffers(window); 61 | 62 | /* Poll for and process events */ 63 | glfwPollEvents(); 64 | } 65 | 66 | glfwTerminate(); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /ep13-opengl-abstraction/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void GLClearError() 5 | { 6 | while (glGetError() != GL_NO_ERROR); 7 | } 8 | 9 | bool GLCheckError() 10 | { 11 | while (GLenum error = glGetError()) 12 | { 13 | 14 | std::cout << "[OpenGL Error] "; 15 | switch(error) { 16 | case GL_INVALID_ENUM : 17 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 18 | break; 19 | case GL_INVALID_VALUE : 20 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 21 | break; 22 | case GL_INVALID_OPERATION : 23 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 24 | break; 25 | case GL_INVALID_FRAMEBUFFER_OPERATION : 26 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 27 | break; 28 | case GL_OUT_OF_MEMORY : 29 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 30 | break; 31 | case GL_STACK_UNDERFLOW : 32 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 33 | break; 34 | case GL_STACK_OVERFLOW : 35 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 36 | break; 37 | default : 38 | std::cout << "Unrecognized error" << error; 39 | } 40 | std::cout << std::endl; 41 | return false; 42 | } 43 | return true; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void GLClearError() 5 | { 6 | while (glGetError() != GL_NO_ERROR); 7 | } 8 | 9 | bool GLCheckError() 10 | { 11 | while (GLenum error = glGetError()) 12 | { 13 | 14 | std::cout << "[OpenGL Error] "; 15 | switch(error) { 16 | case GL_INVALID_ENUM : 17 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 18 | break; 19 | case GL_INVALID_VALUE : 20 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 21 | break; 22 | case GL_INVALID_OPERATION : 23 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 24 | break; 25 | case GL_INVALID_FRAMEBUFFER_OPERATION : 26 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 27 | break; 28 | case GL_OUT_OF_MEMORY : 29 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 30 | break; 31 | case GL_STACK_UNDERFLOW : 32 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 33 | break; 34 | case GL_STACK_OVERFLOW : 35 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 36 | break; 37 | default : 38 | std::cout << "Unrecognized error" << error; 39 | } 40 | std::cout << std::endl; 41 | return false; 42 | } 43 | return true; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /ep14-buffer-layout-abstraction/Renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.h" 2 | #include 3 | 4 | void GLClearError() 5 | { 6 | while (glGetError() != GL_NO_ERROR); 7 | } 8 | 9 | bool GLCheckError() 10 | { 11 | while (GLenum error = glGetError()) 12 | { 13 | 14 | std::cout << "[OpenGL Error] "; 15 | switch(error) { 16 | case GL_INVALID_ENUM : 17 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 18 | break; 19 | case GL_INVALID_VALUE : 20 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 21 | break; 22 | case GL_INVALID_OPERATION : 23 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 24 | break; 25 | case GL_INVALID_FRAMEBUFFER_OPERATION : 26 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 27 | break; 28 | case GL_OUT_OF_MEMORY : 29 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 30 | break; 31 | case GL_STACK_UNDERFLOW : 32 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 33 | break; 34 | case GL_STACK_OVERFLOW : 35 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 36 | break; 37 | default : 38 | std::cout << "Unrecognized error" << error; 39 | } 40 | std::cout << std::endl; 41 | return false; 42 | } 43 | return true; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /ep16-renderer/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep17-textures/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep19-math/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep22-imgui/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep24-test-framework/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define ASSERT(x) if (!(x)) assert(false) 10 | #define INT2VOIDP(i) (void*)(uintptr_t)(i) 11 | 12 | void inline GLClearError() 13 | { 14 | while (glGetError() != GL_NO_ERROR); 15 | } 16 | 17 | bool inline GLCheckError() 18 | { 19 | while (GLenum error = glGetError()) 20 | { 21 | 22 | std::cout << "[OpenGL Error] "; 23 | switch(error) { 24 | case GL_INVALID_ENUM : 25 | std::cout << "GL_INVALID_ENUM : An unacceptable value is specified for an enumerated argument."; 26 | break; 27 | case GL_INVALID_VALUE : 28 | std::cout << "GL_INVALID_OPERATION : A numeric argument is out of range."; 29 | break; 30 | case GL_INVALID_OPERATION : 31 | std::cout << "GL_INVALID_OPERATION : The specified operation is not allowed in the current state."; 32 | break; 33 | case GL_INVALID_FRAMEBUFFER_OPERATION : 34 | std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION : The framebuffer object is not complete."; 35 | break; 36 | case GL_OUT_OF_MEMORY : 37 | std::cout << "GL_OUT_OF_MEMORY : There is not enough memory left to execute the command."; 38 | break; 39 | case GL_STACK_UNDERFLOW : 40 | std::cout << "GL_STACK_UNDERFLOW : An attempt has been made to perform an operation that would cause an internal stack to underflow."; 41 | break; 42 | case GL_STACK_OVERFLOW : 43 | std::cout << "GL_STACK_OVERFLOW : An attempt has been made to perform an operation that would cause an internal stack to overflow."; 44 | break; 45 | default : 46 | std::cout << "Unrecognized error" << error; 47 | } 48 | std::cout << std::endl; 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | 55 | #ifdef DEBUG 56 | #define GLCall(x) GLClearError();\ 57 | x;\ 58 | ASSERT(GLCheckError()) 59 | #else 60 | #define GLCall(x) x 61 | #endif 62 | -------------------------------------------------------------------------------- /ep22-imgui/vendor/imgui/imgui_impl_glfw_gl3.h: -------------------------------------------------------------------------------- 1 | // ImGui GLFW binding with OpenGL3 + shaders 2 | // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) 3 | // (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.) 4 | 5 | // Implemented features: 6 | // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. 7 | // [X] Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. 8 | 9 | // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. 10 | // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). 11 | // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. 12 | // https://github.com/ocornut/imgui 13 | 14 | struct GLFWwindow; 15 | 16 | IMGUI_API bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks, const char* glsl_version = NULL); 17 | IMGUI_API void ImGui_ImplGlfwGL3_Shutdown(); 18 | IMGUI_API void ImGui_ImplGlfwGL3_NewFrame(); 19 | IMGUI_API void ImGui_ImplGlfwGL3_RenderDrawData(ImDrawData* draw_data); 20 | 21 | // Use if you want to reset your rendering device without losing ImGui state. 22 | IMGUI_API void ImGui_ImplGlfwGL3_InvalidateDeviceObjects(); 23 | IMGUI_API bool ImGui_ImplGlfwGL3_CreateDeviceObjects(); 24 | 25 | // GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization) 26 | // Provided here if you want to chain callbacks. 27 | // You can also handle inputs yourself and use those as a reference. 28 | IMGUI_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); 29 | IMGUI_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); 30 | IMGUI_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); 31 | IMGUI_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); 32 | -------------------------------------------------------------------------------- /ep24-test-framework/vendor/imgui/imgui_impl_glfw_gl3.h: -------------------------------------------------------------------------------- 1 | // ImGui GLFW binding with OpenGL3 + shaders 2 | // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) 3 | // (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.) 4 | 5 | // Implemented features: 6 | // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. 7 | // [X] Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. 8 | 9 | // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. 10 | // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). 11 | // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. 12 | // https://github.com/ocornut/imgui 13 | 14 | struct GLFWwindow; 15 | 16 | IMGUI_API bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks, const char* glsl_version = NULL); 17 | IMGUI_API void ImGui_ImplGlfwGL3_Shutdown(); 18 | IMGUI_API void ImGui_ImplGlfwGL3_NewFrame(); 19 | IMGUI_API void ImGui_ImplGlfwGL3_RenderDrawData(ImDrawData* draw_data); 20 | 21 | // Use if you want to reset your rendering device without losing ImGui state. 22 | IMGUI_API void ImGui_ImplGlfwGL3_InvalidateDeviceObjects(); 23 | IMGUI_API bool ImGui_ImplGlfwGL3_CreateDeviceObjects(); 24 | 25 | // GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization) 26 | // Provided here if you want to chain callbacks. 27 | // You can also handle inputs yourself and use those as a reference. 28 | IMGUI_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); 29 | IMGUI_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); 30 | IMGUI_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); 31 | IMGUI_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); 32 | -------------------------------------------------------------------------------- /ep23-rendering-multiple-objects/vendor/imgui/imgui_impl_glfw_gl3.h: -------------------------------------------------------------------------------- 1 | // ImGui GLFW binding with OpenGL3 + shaders 2 | // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) 3 | // (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.) 4 | 5 | // Implemented features: 6 | // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. 7 | // [X] Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. 8 | 9 | // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. 10 | // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown(). 11 | // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. 12 | // https://github.com/ocornut/imgui 13 | 14 | struct GLFWwindow; 15 | 16 | IMGUI_API bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks, const char* glsl_version = NULL); 17 | IMGUI_API void ImGui_ImplGlfwGL3_Shutdown(); 18 | IMGUI_API void ImGui_ImplGlfwGL3_NewFrame(); 19 | IMGUI_API void ImGui_ImplGlfwGL3_RenderDrawData(ImDrawData* draw_data); 20 | 21 | // Use if you want to reset your rendering device without losing ImGui state. 22 | IMGUI_API void ImGui_ImplGlfwGL3_InvalidateDeviceObjects(); 23 | IMGUI_API bool ImGui_ImplGlfwGL3_CreateDeviceObjects(); 24 | 25 | // GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization) 26 | // Provided here if you want to chain callbacks. 27 | // You can also handle inputs yourself and use those as a reference. 28 | IMGUI_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); 29 | IMGUI_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); 30 | IMGUI_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); 31 | IMGUI_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); 32 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestUniform.cpp: -------------------------------------------------------------------------------- 1 | #include "tests/TestUniform.h" 2 | 3 | #include "Debug.h" 4 | #include "imgui.h" 5 | 6 | namespace test 7 | { 8 | TestUniform::TestUniform() : 9 | m_ClearColor { 0.8f, 0.3f, 0.2f, 1.0f }, 10 | m_ObjectColor { 0.2f, 0.3f, 0.8f, 1.0f }, 11 | m_Positions { 12 | -0.5f, -0.5f, // 0 13 | 0.5f, -0.5f, // 1 14 | 0.5f, 0.5f, // 2 15 | -0.5f, 0.5f // 3 16 | }, 17 | m_Indices { 18 | 0, 1, 2, 19 | 2, 3, 0 20 | }, 21 | m_Step(0.05), 22 | m_Direction(1), 23 | m_Oscillate(false), 24 | m_va(), 25 | m_vb(m_Positions, 4 * 2 * sizeof(float)), 26 | m_ib(m_Indices, 6), 27 | m_layout(), 28 | m_shader("res/shaders/Uniform.shader") 29 | { 30 | 31 | m_layout.AddFloat(2); 32 | m_va.AddBuffer(m_vb, m_layout); 33 | } 34 | 35 | TestUniform::~TestUniform() 36 | { 37 | m_va.Unbind(); 38 | m_shader.Unbind(); 39 | } 40 | 41 | void TestUniform::OnUpdate(float deltaTime) 42 | { 43 | // Ideally we would actually specify a "rate" instead of a step, and then we would normalize the points stepped based on the 44 | // input deltaTime, i.e. if rate = 100 points / sec, or 100 points / 1000 ms, or 0.1 points / ms, then we pass in delta time 45 | // in ms and the points we should step are 0.1 * deltaTime * direction. 46 | if (m_Oscillate) 47 | { 48 | // increment red 49 | float *red = m_ObjectColor; 50 | if (*red < 0.0f) 51 | m_Direction = 1; 52 | if (*red > 1.0f) 53 | m_Direction = -1; 54 | *red += m_Step * m_Direction; 55 | } 56 | } 57 | 58 | void TestUniform::OnRender() 59 | { 60 | m_renderer.Clear(); 61 | GLCall(glClearColor(m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3])); 62 | 63 | m_shader.Bind(); 64 | m_shader.SetUniform4f("u_Color", 65 | m_ObjectColor[0], 66 | m_ObjectColor[1], 67 | m_ObjectColor[2], 68 | m_ObjectColor[3]); 69 | 70 | m_renderer.Draw(m_va, m_ib, m_shader); 71 | } 72 | 73 | void TestUniform::OnImGuiRender() 74 | { 75 | ImGui::ColorEdit4("Clear Color", m_ClearColor); 76 | ImGui::ColorEdit4("Object Color", m_ObjectColor); 77 | ImGui::Checkbox("Oscillate Red", &m_Oscillate); 78 | ImGui::SliderFloat("Red Oscillation Speed", &m_Step, 0.0f, 0.25f); 79 | } 80 | }; 81 | -------------------------------------------------------------------------------- /ep24-test-framework/tests/TestMultipleObjects.cpp: -------------------------------------------------------------------------------- 1 | #include "tests/TestMultipleObjects.h" 2 | 3 | #include "Debug.h" 4 | #include "imgui.h" 5 | 6 | #include "glm/glm.hpp" 7 | #include "glm/gtc/matrix_transform.hpp" 8 | 9 | namespace test 10 | { 11 | TestMultipleObjects::TestMultipleObjects() : 12 | m_ClearColor { 0.8f, 0.3f, 0.2f, 1.0f }, 13 | m_Positions { 14 | -50.0f, -50.0f, 0.0f, 0.0f, // 0 15 | 50.0f, -50.0f, 1.0f, 0.0f, // 1 16 | 50.0f, 50.0f, 1.0f, 1.0f, // 2 17 | -50.0f, 50.0f, 0.0f, 1.0f // 3 18 | }, 19 | m_Indices { 20 | 0, 1, 2, 21 | 2, 3, 0 22 | }, 23 | m_va(), 24 | m_ib(m_Indices, 6), 25 | m_vb(m_Positions, 4 * 4 * sizeof(float)), 26 | m_layout(), 27 | m_shader("res/shaders/Complex.shader"), 28 | m_texture("res/textures/phone.png"), 29 | m_renderer(), 30 | m_proj(glm::ortho(0.0f, 960.0f, 0.0f, 540.0f, -1.0f, 1.0f)), 31 | m_view(glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 0))), 32 | m_translationA(200, 200, 0), 33 | m_translationB(400, 200, 0) 34 | { 35 | m_layout.AddFloat(2); 36 | m_layout.AddFloat(2); 37 | m_va.AddBuffer(m_vb, m_layout); 38 | 39 | m_texture.Bind(); 40 | m_shader.SetUniform1i("u_Texture", 0); 41 | } 42 | 43 | TestMultipleObjects::~TestMultipleObjects() 44 | { 45 | m_va.Unbind(); 46 | m_shader.Unbind(); 47 | } 48 | 49 | void TestMultipleObjects::OnUpdate(float deltaTime) 50 | { 51 | } 52 | 53 | void TestMultipleObjects::OnRender() 54 | { 55 | m_renderer.Clear(); 56 | GLCall(glClearColor(m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3])); 57 | 58 | { 59 | glm::mat4 model = glm::translate(glm::mat4(1.0f), m_translationA); 60 | glm::mat4 mvp = m_proj * m_view * model; 61 | m_shader.Bind(); 62 | m_shader.SetUniformMat4f("u_MVP", mvp); 63 | m_renderer.Draw(m_va, m_ib, m_shader); 64 | } 65 | 66 | { 67 | glm::mat4 model = glm::translate(glm::mat4(1.0f), m_translationB); 68 | glm::mat4 mvp = m_proj * m_view * model; 69 | m_shader.Bind(); 70 | m_shader.SetUniformMat4f("u_MVP", mvp); 71 | m_renderer.Draw(m_va, m_ib, m_shader); 72 | } 73 | } 74 | 75 | void TestMultipleObjects::OnImGuiRender() 76 | { 77 | ImGui::ColorEdit4("Clear Color", m_ClearColor); 78 | ImGui::SliderFloat3("TranslationA", &m_translationA.x, 0.0f, 960.0f); 79 | ImGui::SliderFloat3("TranslationB", &m_translationB.x, 0.0f, 960.0f); 80 | } 81 | }; 82 | -------------------------------------------------------------------------------- /ep16-renderer/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "VertexBuffer.h" 10 | #include "VertexArray.h" 11 | #include "IndexBuffer.h" 12 | #include "Shader.h" 13 | #include "Renderer.h" 14 | 15 | GLFWwindow* InitWindow() 16 | { 17 | // Initialise GLFW 18 | if( !glfwInit() ) 19 | { 20 | fprintf( stderr, "Failed to initialize GLFW\n" ); 21 | getchar(); 22 | return nullptr; 23 | } 24 | 25 | glfwWindowHint(GLFW_SAMPLES, 4); 26 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 27 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 28 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 29 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 30 | 31 | // Open a window and create its OpenGL context 32 | GLFWwindow* window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL); 33 | if( window == NULL ){ 34 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 35 | getchar(); 36 | glfwTerminate(); 37 | return nullptr; 38 | 39 | } 40 | glfwMakeContextCurrent(window); 41 | 42 | // Initialize GLEW 43 | glewExperimental = true; // Needed for core profile 44 | if (glewInit() != GLEW_OK) { 45 | fprintf(stderr, "Failed to initialize GLEW\n"); 46 | getchar(); 47 | glfwTerminate(); 48 | return nullptr; 49 | } 50 | 51 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 52 | 53 | // Ensure we can capture the escape key being pressed below 54 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 55 | 56 | return window; 57 | } 58 | 59 | int main( void ) 60 | { 61 | GLFWwindow* window = InitWindow(); 62 | if (!window) 63 | return -1; 64 | 65 | float positions[] = { 66 | -0.5f, -0.5f, // 0 67 | 0.5f, -0.5f, // 1 68 | 0.5f, 0.5f, // 2 69 | -0.5f, 0.5f // 3 70 | }; 71 | 72 | unsigned int indices[] = { 73 | 0, 1, 2, 74 | 2, 3, 0 75 | }; 76 | 77 | { 78 | VertexArray va; 79 | VertexBuffer vb(positions, 4 * 2 * sizeof(float)); 80 | IndexBuffer ib(indices, 6); 81 | 82 | VertexBufferLayout layout; 83 | layout.AddFloat(2); 84 | 85 | va.AddBuffer(vb, layout); 86 | 87 | Shader shader("res/shaders/Basic.shader"); 88 | shader.Bind(); 89 | 90 | float red = 0.0f; 91 | float step = 0.05f; 92 | 93 | Renderer renderer; 94 | 95 | do { 96 | renderer.Clear(); 97 | 98 | shader.Bind(); 99 | shader.SetUniform4f("u_Color", red, 0.3, 0.8, 1.0); 100 | 101 | renderer.Draw(va, ib, shader); 102 | 103 | // Swap buffers 104 | glfwSwapBuffers(window); 105 | glfwPollEvents(); 106 | 107 | // increment red 108 | if (red < 0.0f || red > 1.0f) 109 | step *= -1.0; 110 | red += step; 111 | 112 | } // Check if the ESC key was pressed or the window was closed 113 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 114 | glfwWindowShouldClose(window) == 0 ); 115 | } 116 | 117 | // Close OpenGL window and terminate GLFW 118 | glfwTerminate(); 119 | 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /ep17-textures/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "VertexBuffer.h" 10 | #include "VertexArray.h" 11 | #include "IndexBuffer.h" 12 | #include "Shader.h" 13 | #include "Renderer.h" 14 | #include "Texture.h" 15 | 16 | GLFWwindow* InitWindow() 17 | { 18 | // Initialise GLFW 19 | if( !glfwInit() ) 20 | { 21 | fprintf( stderr, "Failed to initialize GLFW\n" ); 22 | getchar(); 23 | return nullptr; 24 | } 25 | 26 | glfwWindowHint(GLFW_SAMPLES, 4); 27 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 28 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 29 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 30 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 31 | 32 | // Open a window and create its OpenGL context 33 | GLFWwindow* window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL); 34 | if( window == NULL ){ 35 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 36 | getchar(); 37 | glfwTerminate(); 38 | return nullptr; 39 | 40 | } 41 | glfwMakeContextCurrent(window); 42 | 43 | // Initialize GLEW 44 | glewExperimental = true; // Needed for core profile 45 | if (glewInit() != GLEW_OK) { 46 | fprintf(stderr, "Failed to initialize GLEW\n"); 47 | getchar(); 48 | glfwTerminate(); 49 | return nullptr; 50 | } 51 | 52 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 53 | 54 | // Ensure we can capture the escape key being pressed below 55 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 56 | 57 | return window; 58 | } 59 | 60 | int main( void ) 61 | { 62 | GLFWwindow* window = InitWindow(); 63 | if (!window) 64 | return -1; 65 | 66 | float positions[] = { 67 | -0.5f, -0.5f, 0.0f, 0.0f, // 0 68 | 0.5f, -0.5f, 1.0f, 0.0f, // 1 69 | 0.5f, 0.5f, 1.0f, 1.0f, // 2 70 | -0.5f, 0.5f, 0.0f, 1.0f // 3 71 | }; 72 | 73 | unsigned int indices[] = { 74 | 0, 1, 2, 75 | 2, 3, 0 76 | }; 77 | 78 | GLCall( glEnable(GL_BLEND) ); 79 | GLCall( glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); 80 | 81 | { 82 | VertexArray va; 83 | VertexBuffer vb(positions, 4 * 4 * sizeof(float)); 84 | IndexBuffer ib(indices, 6); 85 | 86 | VertexBufferLayout layout; 87 | layout.AddFloat(2); 88 | layout.AddFloat(2); 89 | 90 | va.AddBuffer(vb, layout); 91 | 92 | Shader shader("res/shaders/Basic.shader"); 93 | shader.Bind(); 94 | 95 | Texture texture("res/textures/phone.png"); 96 | texture.Bind(); 97 | shader.SetUniform1i("u_Texture", 0); 98 | 99 | Renderer renderer; 100 | 101 | do { 102 | renderer.Clear(); 103 | renderer.Draw(va, ib, shader); 104 | 105 | // Swap buffers 106 | glfwSwapBuffers(window); 107 | glfwPollEvents(); 108 | } // Check if the ESC key was pressed or the window was closed 109 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 110 | glfwWindowShouldClose(window) == 0 ); 111 | } 112 | 113 | // Close OpenGL window and terminate GLFW 114 | glfwTerminate(); 115 | 116 | return 0; 117 | } 118 | 119 | -------------------------------------------------------------------------------- /ep07-write-a-shader/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static unsigned int CompileShader(unsigned int type, const std::string& source) 10 | { 11 | unsigned int id = glCreateShader(type); 12 | const char* src = source.c_str(); 13 | glShaderSource(id, 1, &src, nullptr); 14 | glCompileShader(id); 15 | 16 | int result; 17 | glGetShaderiv(id, GL_COMPILE_STATUS, &result); 18 | if ( result == GL_FALSE ) 19 | { 20 | int length; 21 | glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); 22 | char* message = (char*)alloca(length * sizeof(char)); 23 | glGetShaderInfoLog(id, length, &length, message); 24 | std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader: " << message << std::endl; 25 | glDeleteShader(id); 26 | return 0; 27 | } 28 | 29 | return id; 30 | } 31 | 32 | static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) 33 | { 34 | unsigned int program = glCreateProgram(); 35 | unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader); 36 | unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader); 37 | 38 | glAttachShader(program, vs); 39 | glAttachShader(program, fs); 40 | glLinkProgram(program); 41 | glValidateProgram(program); 42 | 43 | glDeleteShader(vs); 44 | glDeleteShader(fs); 45 | 46 | return program; 47 | } 48 | 49 | int main(void) 50 | { 51 | GLFWwindow* window; 52 | 53 | /* Initialize the library */ 54 | if (!glfwInit()) 55 | return -1; 56 | 57 | /* Create a windowed mode window and its OpenGL context */ 58 | window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); 59 | if (!window) 60 | { 61 | glfwTerminate(); 62 | return -1; 63 | } 64 | 65 | /* Make the window's context current */ 66 | glfwMakeContextCurrent(window); 67 | 68 | GLenum err = glewInit(); 69 | if (GLEW_OK != err) 70 | { 71 | // Problem: glewInit failed 72 | fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); 73 | return 1; 74 | } 75 | fprintf(stdout, "GLEW version: %s\n", glewGetString(GLEW_VERSION)); 76 | fprintf(stdout, "GL version: %s\n", glGetString(GL_VERSION)); 77 | 78 | float positions[6] = { 79 | -0.5f, -0.5f, 80 | 0.5f, -0.5f, 81 | 0.0f, 0.5f 82 | }; 83 | 84 | unsigned int buffer; 85 | glGenBuffers(1, &buffer); 86 | glBindBuffer(GL_ARRAY_BUFFER, buffer); 87 | glBufferData(GL_ARRAY_BUFFER, sizeof(float)*6, positions, GL_STATIC_DRAW); 88 | 89 | glEnableVertexAttribArray(0); 90 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0); 91 | 92 | std::string vertexShader = 93 | "#version 330 core\n" 94 | "\n" 95 | "layout(location = 0) in vec4 position;\n" 96 | "void main()\n" 97 | "{\n" 98 | " gl_Position = position;\n" 99 | "}\n"; 100 | 101 | std::string fragmentShader = 102 | "#version 330 core\n" 103 | "\n" 104 | "layout(location = 0) out vec4 color;\n" 105 | "void main()\n" 106 | "{\n" 107 | " color = vec4(1.0, 0.0, 0.0, 1.0);\n" 108 | "}\n"; 109 | 110 | unsigned int shader = CreateShader(vertexShader, fragmentShader); 111 | glUseProgram(shader); 112 | 113 | glBindBuffer(GL_ARRAY_BUFFER, 0); 114 | 115 | /* Loop until the user closes the window */ 116 | while (!glfwWindowShouldClose(window)) 117 | { 118 | /* Render here */ 119 | glClear(GL_COLOR_BUFFER_BIT); 120 | 121 | glDrawArrays(GL_TRIANGLES, 0, 3); 122 | 123 | /* Swap front and back buffers */ 124 | glfwSwapBuffers(window); 125 | 126 | /* Poll for and process events */ 127 | glfwPollEvents(); 128 | } 129 | 130 | glDeleteProgram(shader); 131 | 132 | glfwTerminate(); 133 | return 0; 134 | } 135 | -------------------------------------------------------------------------------- /ep15-shader-abstraction/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "Renderer.h" 10 | #include "VertexBuffer.h" 11 | #include "VertexArray.h" 12 | #include "IndexBuffer.h" 13 | #include "Shader.h" 14 | 15 | GLFWwindow* InitWindow() 16 | { 17 | // Initialise GLFW 18 | if( !glfwInit() ) 19 | { 20 | fprintf( stderr, "Failed to initialize GLFW\n" ); 21 | getchar(); 22 | return nullptr; 23 | } 24 | 25 | glfwWindowHint(GLFW_SAMPLES, 4); 26 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 27 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 28 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 29 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 30 | 31 | // Open a window and create its OpenGL context 32 | GLFWwindow* window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL); 33 | if( window == NULL ){ 34 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 35 | getchar(); 36 | glfwTerminate(); 37 | return nullptr; 38 | 39 | } 40 | glfwMakeContextCurrent(window); 41 | 42 | // Initialize GLEW 43 | glewExperimental = true; // Needed for core profile 44 | if (glewInit() != GLEW_OK) { 45 | fprintf(stderr, "Failed to initialize GLEW\n"); 46 | getchar(); 47 | glfwTerminate(); 48 | return nullptr; 49 | } 50 | 51 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 52 | 53 | // Ensure we can capture the escape key being pressed below 54 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 55 | 56 | return window; 57 | } 58 | 59 | void ClearAll() 60 | { 61 | GLCall( glUseProgram(0) ); 62 | GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) ); 63 | GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); 64 | GLCall( glBindVertexArray(0) ); 65 | } 66 | 67 | int main( void ) 68 | { 69 | GLFWwindow* window = InitWindow(); 70 | if (!window) 71 | return -1; 72 | 73 | float positions[] = { 74 | -0.5f, -0.5f, // 0 75 | 0.5f, -0.5f, // 1 76 | 0.5f, 0.5f, // 2 77 | -0.5f, 0.5f // 3 78 | }; 79 | 80 | unsigned int indices[] = { 81 | 0, 1, 2, 82 | 2, 3, 0 83 | }; 84 | 85 | { 86 | VertexArray va; 87 | VertexBuffer vb(positions, 4 * 2 * sizeof(float)); 88 | IndexBuffer ib(indices, 6); 89 | 90 | VertexBufferLayout layout; 91 | layout.AddFloat(2); 92 | 93 | va.AddBuffer(vb, layout); 94 | 95 | Shader shader("res/shaders/Basic.shader"); 96 | shader.Bind(); 97 | 98 | float red = 0.0f; 99 | float step = 0.05f; 100 | 101 | ClearAll(); 102 | 103 | do { 104 | // Clear the screen 105 | GLCall( glClear( GL_COLOR_BUFFER_BIT ); 106 | 107 | // set shader and set uniform color 108 | shader.Bind(); 109 | shader.SetUniform4f("u_Color", red, 0.3, 0.8, 1.0); 110 | 111 | // Bind index buffer 112 | va.Bind(); 113 | ib.Bind(); 114 | 115 | // Draw 116 | GLCall( glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)) ); 117 | 118 | // Swap buffers 119 | glfwSwapBuffers(window); 120 | glfwPollEvents(); 121 | 122 | // increment red 123 | if (red < 0.0f || red > 1.0f) 124 | step *= -1.0; 125 | red += step; 126 | 127 | } // Check if the ESC key was pressed or the window was closed 128 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 129 | glfwWindowShouldClose(window) == 0 ); 130 | } 131 | 132 | // Close OpenGL window and terminate GLFW 133 | glfwTerminate(); 134 | 135 | return 0; 136 | } 137 | 138 | -------------------------------------------------------------------------------- /ep19-math/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "VertexBuffer.h" 10 | #include "VertexArray.h" 11 | #include "IndexBuffer.h" 12 | #include "Shader.h" 13 | #include "Renderer.h" 14 | #include "Texture.h" 15 | 16 | #include "glm/glm.hpp" 17 | #include "glm/gtc/matrix_transform.hpp" 18 | 19 | GLFWwindow* InitWindow() 20 | { 21 | // Initialise GLFW 22 | if( !glfwInit() ) 23 | { 24 | fprintf( stderr, "Failed to initialize GLFW\n" ); 25 | getchar(); 26 | return nullptr; 27 | } 28 | 29 | glfwWindowHint(GLFW_SAMPLES, 4); 30 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 31 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 32 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 33 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 34 | 35 | // Open a window and create its OpenGL context 36 | GLFWwindow* window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL); 37 | if( window == NULL ){ 38 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 39 | getchar(); 40 | glfwTerminate(); 41 | return nullptr; 42 | 43 | } 44 | glfwMakeContextCurrent(window); 45 | 46 | // Initialize GLEW 47 | glewExperimental = true; // Needed for core profile 48 | if (glewInit() != GLEW_OK) { 49 | fprintf(stderr, "Failed to initialize GLEW\n"); 50 | getchar(); 51 | glfwTerminate(); 52 | return nullptr; 53 | } 54 | 55 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 56 | 57 | // Ensure we can capture the escape key being pressed below 58 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 59 | 60 | return window; 61 | } 62 | 63 | int main( void ) 64 | { 65 | GLFWwindow* window = InitWindow(); 66 | if (!window) 67 | return -1; 68 | 69 | float positions[] = { 70 | -0.5f, -0.5f, 0.0f, 0.0f, // 0 71 | 0.5f, -0.5f, 1.0f, 0.0f, // 1 72 | 0.5f, 0.5f, 1.0f, 1.0f, // 2 73 | -0.5f, 0.5f, 0.0f, 1.0f // 3 74 | }; 75 | 76 | unsigned int indices[] = { 77 | 0, 1, 2, 78 | 2, 3, 0 79 | }; 80 | 81 | GLCall( glEnable(GL_BLEND) ); 82 | GLCall( glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); 83 | 84 | { 85 | VertexArray va; 86 | VertexBuffer vb(positions, 4 * 4 * sizeof(float)); 87 | IndexBuffer ib(indices, 6); 88 | 89 | glm::mat4 proj = glm::ortho(-2.0f, 2.0f, -1.5f, 1.5f, -1.0f, 1.0f); 90 | 91 | VertexBufferLayout layout; 92 | layout.AddFloat(2); 93 | layout.AddFloat(2); 94 | 95 | va.AddBuffer(vb, layout); 96 | 97 | Shader shader("res/shaders/Basic.shader"); 98 | shader.Bind(); 99 | shader.SetUniform4f("u_Color", 0.0f, 0.3f, 0.8f, 1.0f); 100 | shader.SetUniformMat4f("u_MVP", proj); 101 | 102 | Texture texture("res/textures/phone.png"); 103 | texture.Bind(); 104 | shader.SetUniform1i("u_Texture", 0); 105 | 106 | float red = 0.0f; 107 | float step = 0.05f; 108 | 109 | Renderer renderer; 110 | 111 | do { 112 | renderer.Clear(); 113 | 114 | shader.Bind(); 115 | shader.SetUniform4f("u_Color", red, 0.3, 0.8, 1.0); 116 | 117 | renderer.Draw(va, ib, shader); 118 | 119 | // Swap buffers 120 | glfwSwapBuffers(window); 121 | glfwPollEvents(); 122 | 123 | // increment red 124 | if (red < 0.0f || red > 1.0f) 125 | step *= -1.0; 126 | red += step; 127 | 128 | } // Check if the ESC key was pressed or the window was closed 129 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 130 | glfwWindowShouldClose(window) == 0 ); 131 | } 132 | 133 | // Close OpenGL window and terminate GLFW 134 | glfwTerminate(); 135 | 136 | return 0; 137 | } 138 | 139 | -------------------------------------------------------------------------------- /ep20-projection-matrices/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "VertexBuffer.h" 10 | #include "VertexArray.h" 11 | #include "IndexBuffer.h" 12 | #include "Shader.h" 13 | #include "Renderer.h" 14 | #include "Texture.h" 15 | 16 | #include "glm/glm.hpp" 17 | #include "glm/gtc/matrix_transform.hpp" 18 | 19 | GLFWwindow* InitWindow() 20 | { 21 | // Initialise GLFW 22 | if( !glfwInit() ) 23 | { 24 | fprintf( stderr, "Failed to initialize GLFW\n" ); 25 | getchar(); 26 | return nullptr; 27 | } 28 | 29 | glfwWindowHint(GLFW_SAMPLES, 4); 30 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 31 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 32 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 33 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 34 | 35 | // Open a window and create its OpenGL context 36 | GLFWwindow* window = glfwCreateWindow( 960, 540, "Tutorial 02 - Red triangle", NULL, NULL); 37 | if( window == NULL ){ 38 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 39 | getchar(); 40 | glfwTerminate(); 41 | return nullptr; 42 | 43 | } 44 | glfwMakeContextCurrent(window); 45 | 46 | // Initialize GLEW 47 | glewExperimental = true; // Needed for core profile 48 | if (glewInit() != GLEW_OK) { 49 | fprintf(stderr, "Failed to initialize GLEW\n"); 50 | getchar(); 51 | glfwTerminate(); 52 | return nullptr; 53 | } 54 | 55 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 56 | 57 | // Ensure we can capture the escape key being pressed below 58 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 59 | 60 | return window; 61 | } 62 | 63 | int main( void ) 64 | { 65 | GLFWwindow* window = InitWindow(); 66 | if (!window) 67 | return -1; 68 | 69 | float positions[] = { 70 | 100.0f, 100.0f, 0.0f, 0.0f, // 0 71 | 200.0f, 100.0f, 1.0f, 0.0f, // 1 72 | 200.0f, 200.0f, 1.0f, 1.0f, // 2 73 | 100.0f, 200.0f, 0.0f, 1.0f // 3 74 | }; 75 | 76 | unsigned int indices[] = { 77 | 0, 1, 2, 78 | 2, 3, 0 79 | }; 80 | 81 | GLCall( glEnable(GL_BLEND) ); 82 | GLCall( glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); 83 | 84 | { 85 | VertexArray va; 86 | VertexBuffer vb(positions, 4 * 4 * sizeof(float)); 87 | IndexBuffer ib(indices, 6); 88 | 89 | glm::mat4 proj = glm::ortho(0.0f, 960.0f, 0.0f, 540.0f, -1.0f, 1.0f); 90 | glm::vec4 vp(100.0f, 100.0f, 0.0f, 1.0f); 91 | 92 | glm::vec4 result = proj * vp; 93 | 94 | VertexBufferLayout layout; 95 | layout.AddFloat(2); 96 | layout.AddFloat(2); 97 | 98 | va.AddBuffer(vb, layout); 99 | 100 | Shader shader("res/shaders/Basic.shader"); 101 | shader.Bind(); 102 | shader.SetUniform4f("u_Color", 0.0f, 0.3f, 0.8f, 1.0f); 103 | shader.SetUniformMat4f("u_MVP", proj); 104 | 105 | Texture texture("res/textures/phone.png"); 106 | texture.Bind(); 107 | shader.SetUniform1i("u_Texture", 0); 108 | 109 | float red = 0.0f; 110 | float step = 0.05f; 111 | 112 | Renderer renderer; 113 | 114 | do { 115 | renderer.Clear(); 116 | 117 | shader.Bind(); 118 | shader.SetUniform4f("u_Color", red, 0.3, 0.8, 1.0); 119 | 120 | renderer.Draw(va, ib, shader); 121 | 122 | // Swap buffers 123 | glfwSwapBuffers(window); 124 | glfwPollEvents(); 125 | 126 | // increment red 127 | if (red < 0.0f || red > 1.0f) 128 | step *= -1.0; 129 | red += step; 130 | 131 | } // Check if the ESC key was pressed or the window was closed 132 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 133 | glfwWindowShouldClose(window) == 0 ); 134 | } 135 | 136 | // Close OpenGL window and terminate GLFW 137 | glfwTerminate(); 138 | 139 | return 0; 140 | } 141 | 142 | -------------------------------------------------------------------------------- /ep24-test-framework/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "imgui.h" 10 | #include "imgui_impl_glfw_gl3.h" 11 | 12 | #include "tests/TestClearColor.h" 13 | #include "tests/TestTriangle.h" 14 | #include "tests/TestUniform.h" 15 | #include "tests/TestMultipleObjects.h" 16 | 17 | #include "Debug.h" 18 | 19 | GLFWwindow* InitWindow() 20 | { 21 | // Initialise GLFW 22 | if( !glfwInit() ) 23 | { 24 | fprintf( stderr, "Failed to initialize GLFW\n" ); 25 | getchar(); 26 | return nullptr; 27 | } 28 | 29 | glfwWindowHint(GLFW_SAMPLES, 4); 30 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 31 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 32 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 33 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 34 | 35 | // Open a window and create its OpenGL context 36 | GLFWwindow* window = glfwCreateWindow( 960, 540, "Tutorial 02 - Red triangle", NULL, NULL); 37 | if( window == NULL ){ 38 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 39 | getchar(); 40 | glfwTerminate(); 41 | return nullptr; 42 | 43 | } 44 | glfwMakeContextCurrent(window); 45 | 46 | // Initialize GLEW 47 | glewExperimental = true; // Needed for core profile 48 | if (glewInit() != GLEW_OK) { 49 | fprintf(stderr, "Failed to initialize GLEW\n"); 50 | getchar(); 51 | glfwTerminate(); 52 | return nullptr; 53 | } 54 | 55 | std::cout << "Using GLEW Version: " << glewGetString(GLEW_VERSION) << std::endl; 56 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 57 | 58 | // Ensure we can capture the escape key being pressed below 59 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 60 | 61 | return window; 62 | } 63 | 64 | int main( void ) 65 | { 66 | GLFWwindow* window = InitWindow(); 67 | if (!window) 68 | return -1; 69 | 70 | GLCall( glEnable(GL_BLEND) ); 71 | GLCall( glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); 72 | 73 | IMGUI_CHECKVERSION(); 74 | ImGui::CreateContext(); 75 | ImGui_ImplGlfwGL3_Init(window, true); 76 | ImGui::StyleColorsDark(); 77 | 78 | int currentSelection = -1; 79 | int radioSelection = 0; 80 | test::Test *test; 81 | 82 | do { 83 | ImGui_ImplGlfwGL3_NewFrame(); 84 | { 85 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); 86 | ImGui::RadioButton("ClearColor", &radioSelection, 0); ImGui::SameLine(); 87 | ImGui::RadioButton("Triangle", &radioSelection, 1); ImGui::SameLine(); 88 | ImGui::RadioButton("Uniform", &radioSelection, 2); ImGui::SameLine(); 89 | ImGui::RadioButton("MultipleObjects", &radioSelection, 3); 90 | } 91 | 92 | if (currentSelection != radioSelection) 93 | { 94 | switch(radioSelection) 95 | { 96 | case 0 : delete test; 97 | test = new test::TestClearColor(); 98 | break; 99 | case 1 : delete test; 100 | test = new test::TestTriangle(); 101 | break; 102 | case 2 : delete test; 103 | test = new test::TestUniform(); 104 | break; 105 | case 3 : delete test; 106 | test = new test::TestMultipleObjects(); 107 | break; 108 | } 109 | currentSelection = radioSelection; 110 | } 111 | 112 | test->OnUpdate(0.0f); 113 | test->OnRender(); 114 | test->OnImGuiRender(); 115 | 116 | ImGui::Render(); 117 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData()); 118 | 119 | glfwSwapBuffers(window); 120 | glfwPollEvents(); 121 | 122 | } // Check if the ESC key was pressed or the window was closed 123 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 124 | glfwWindowShouldClose(window) == 0 ); 125 | 126 | // Close OpenGL window and terminate GLFW 127 | ImGui_ImplGlfwGL3_Shutdown(); 128 | ImGui::DestroyContext(); 129 | glfwTerminate(); 130 | 131 | return 0; 132 | } 133 | 134 | -------------------------------------------------------------------------------- /ep21-model-view-projection/Application.cpp: -------------------------------------------------------------------------------- 1 | // Include GLEW 2 | #include 3 | 4 | // Include GLFW 5 | #include 6 | 7 | #include 8 | 9 | #include "VertexBuffer.h" 10 | #include "VertexArray.h" 11 | #include "IndexBuffer.h" 12 | #include "Shader.h" 13 | #include "Renderer.h" 14 | #include "Texture.h" 15 | 16 | #include "glm/glm.hpp" 17 | #include "glm/gtc/matrix_transform.hpp" 18 | 19 | GLFWwindow* InitWindow() 20 | { 21 | // Initialise GLFW 22 | if( !glfwInit() ) 23 | { 24 | fprintf( stderr, "Failed to initialize GLFW\n" ); 25 | getchar(); 26 | return nullptr; 27 | } 28 | 29 | glfwWindowHint(GLFW_SAMPLES, 4); 30 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 31 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 32 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 33 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 34 | 35 | // Open a window and create its OpenGL context 36 | GLFWwindow* window = glfwCreateWindow( 960, 540, "Tutorial 02 - Red triangle", NULL, NULL); 37 | if( window == NULL ){ 38 | fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 39 | getchar(); 40 | glfwTerminate(); 41 | return nullptr; 42 | 43 | } 44 | glfwMakeContextCurrent(window); 45 | 46 | // Initialize GLEW 47 | glewExperimental = true; // Needed for core profile 48 | if (glewInit() != GLEW_OK) { 49 | fprintf(stderr, "Failed to initialize GLEW\n"); 50 | getchar(); 51 | glfwTerminate(); 52 | return nullptr; 53 | } 54 | 55 | std::cout << "Using GL Version: " << glGetString(GL_VERSION) << std::endl; 56 | 57 | // Ensure we can capture the escape key being pressed below 58 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 59 | 60 | return window; 61 | } 62 | 63 | int main( void ) 64 | { 65 | GLFWwindow* window = InitWindow(); 66 | if (!window) 67 | return -1; 68 | 69 | float positions[] = { 70 | 100.0f, 100.0f, 0.0f, 0.0f, // 0 71 | 200.0f, 100.0f, 1.0f, 0.0f, // 1 72 | 200.0f, 200.0f, 1.0f, 1.0f, // 2 73 | 100.0f, 200.0f, 0.0f, 1.0f // 3 74 | }; 75 | 76 | unsigned int indices[] = { 77 | 0, 1, 2, 78 | 2, 3, 0 79 | }; 80 | 81 | GLCall( glEnable(GL_BLEND) ); 82 | GLCall( glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ); 83 | 84 | { 85 | VertexArray va; 86 | VertexBuffer vb(positions, 4 * 4 * sizeof(float)); 87 | IndexBuffer ib(indices, 6); 88 | 89 | glm::mat4 proj = glm::ortho(0.0f, 960.0f, 0.0f, 540.0f, -1.0f, 1.0f); 90 | 91 | glm::mat4 ident = glm::mat4(1.0f); 92 | glm::vec3 trvec = glm::vec3(-100, 0, 0); 93 | glm::mat4 view = glm::translate(ident, trvec); 94 | 95 | glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(200, 200, 0)); 96 | 97 | glm::mat4 mvp = proj * view * model; 98 | 99 | // projection matrix 100 | // view matrix 101 | // model matrix 102 | 103 | VertexBufferLayout layout; 104 | layout.AddFloat(2); 105 | layout.AddFloat(2); 106 | 107 | va.AddBuffer(vb, layout); 108 | 109 | Shader shader("res/shaders/Basic.shader"); 110 | shader.Bind(); 111 | shader.SetUniform4f("u_Color", 0.0f, 0.3f, 0.8f, 1.0f); 112 | shader.SetUniformMat4f("u_MVP", mvp); 113 | 114 | Texture texture("res/textures/phone.png"); 115 | texture.Bind(); 116 | shader.SetUniform1i("u_Texture", 0); 117 | 118 | float red = 0.0f; 119 | float step = 0.05f; 120 | 121 | Renderer renderer; 122 | 123 | do { 124 | renderer.Clear(); 125 | 126 | shader.Bind(); 127 | shader.SetUniform4f("u_Color", red, 0.3, 0.8, 1.0); 128 | 129 | renderer.Draw(va, ib, shader); 130 | 131 | // Swap buffers 132 | glfwSwapBuffers(window); 133 | glfwPollEvents(); 134 | 135 | // increment red 136 | if (red < 0.0f || red > 1.0f) 137 | step *= -1.0; 138 | red += step; 139 | 140 | } // Check if the ESC key was pressed or the window was closed 141 | while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 142 | glfwWindowShouldClose(window) == 0 ); 143 | } 144 | 145 | // Close OpenGL window and terminate GLFW 146 | glfwTerminate(); 147 | 148 | return 0; 149 | } 150 | 151 | --------------------------------------------------------------------------------