An open API service indexing awesome lists of open source software.

https://github.com/d3m1d0s/opengltemplate

Modern C++20 OpenGL starter template with CMake, GLFW, and GLAD
https://github.com/d3m1d0s/opengltemplate

cmake cmakelists cpp cpp20 cross-platform glad glfw graphics opengl template

Last synced: 27 days ago
JSON representation

Modern C++20 OpenGL starter template with CMake, GLFW, and GLAD

Awesome Lists containing this project

README

          

# OpenGLTemplate

[![CMake](https://github.com/d3m1d0s/OpenGLTemplate/actions/workflows/cmake-multi-platform.yml/badge.svg?branch=main)](https://github.com/d3m1d0s/OpenGLTemplate/actions/workflows/cmake-multi-platform.yml)
![C++20](https://img.shields.io/badge/C%2B%2B-20-blue.svg)
![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)

A modern **C++20 OpenGL starter template** with [GLFW](https://www.glfw.org/) and [GLAD](https://glad.dav1d.de/).
Works **out-of-the-box**: uses preinstalled packages (vcpkg/Conan/system) or auto-fetches sources via CMake.

---

## πŸ“š Table of Contents
- [⚑ Quick Start](#-quick-start)
- [πŸ’» Recommended IDE: CLion](#-recommended-ide-clion)
- [✨ Features](#-features)
- [πŸ“¦ Dependencies](#-dependencies)
- [πŸ–ΌοΈ Example Output](#-example-output)
- [πŸ“‚ Project Structure](#-project-structure)
- [πŸ†˜ Troubleshooting](#-troubleshooting)
- [πŸ“œ License](#-license)
- [πŸ™Œ Attribution](#-attribution)

---

## ⚑ Quick Start
Tip: It’s recommended to preinstall [Dependencies](#-dependencies).
If something goes wrong - check [Troubleshooting](#-troubleshooting).

```bash
git clone https://github.com//.git
cd OpenGLTemplate
cmake -B build -S .
cmake --build build
./build/OpenGLTemplate # Windows: .\build\OpenGLTemplate.exe
```

---

## πŸ’» Recommended IDE: CLion

[JetBrains CLion](https://www.jetbrains.com/clion/) is a great choice for CMake/C++ projects.
Just open this folder in CLion and it will automatically detect the `CMakeLists.txt` and configure everything.

Tip: if you use **vcpkg** with CLion, set a toolchain file in
`Settings β†’ Build, Execution, Deployment β†’ CMake β†’ CMake options` and pick a proper triplet (e.g. `x64-windows-static`):

```bash
-DCMAKE_TOOLCHAIN_FILE=C://scripts/buildsystems/vcpkg.cmake
-DVCPKG_TARGET_TRIPLET=x64-windows-static
```

---

## ✨ Features

* βš™οΈ Minimal CMake (>= 3.20), C++20.
* 🌍 Cross-platform: Windows, Linux, macOS.
* πŸ”€ Dual dependency strategy:

* Uses `find_package` if `glfw3` / `glad` are installed.
* Otherwise `FetchContent` pulls GLFW 3.4 & GLAD 0.1.36.
* πŸ”— Clean linking: `OpenGL::GL`, `glfw`, `glad`.

---

## πŸ“¦ Dependencies

### πŸͺŸ Windows (via vcpkg)

```powershell
vcpkg install glfw3 glad
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static
```

> Static triplets easiest (no DLLs). Dynamic require shipping `glfw3.dll`.

### 🐧 Linux (system packages)

```bash
sudo apt install cmake g++ pkg-config libglfw3-dev libgl1-mesa-dev
sudo apt install libglad-dev # If glad is not available via your package manager, no worries - this template will fetch it automatically.
cmake -B build -S .
cmake --build build
```

### 🍏 macOS (Homebrew)

```bash
brew install cmake glfw
brew install glad # If glad is not available via your package manager, no worries - this template will fetch it automatically.
cmake -B build -S .
cmake --build build
```

> macOS OpenGL is capped at **4.1**.

### πŸ“¦ Self-contained (no pkg manager)

Do nothing - CMake auto-fetches **GLFW 3.4** and **GLAD 0.1.36**.

---

## πŸ–ΌοΈ Example Output

Opens a **800Γ—600 window**, clears to white, prints OpenGL version, e.g.:

```
OpenGL: 3.3.0 - Build 31.0.101.2135
```

---

## πŸ“‚ Project Structure

```
OpenGLTemplate/
β”œβ”€ src/ # C++ sources (main.cpp included)
β”œβ”€ include/ # Headers
β”œβ”€ shaders/ # GLSL files
β”œβ”€ assets/ # Textures, models
β”œβ”€ CMakeLists.txt
└─ README.md
```

> To bump OpenGL version, edit in CMake:
> `set(GLAD_API "gl=4.6")`.

## πŸ†˜ Troubleshooting

- **Windows: mixing toolchains/triplets**
Errors like `__security_cookie`, `sscanf_s`, or `fprintf` usually mean you’re compiling with **MinGW** but linked **MSVC** vcpkg triplets (or vice versa).
Align them:
- MSVC ↔ `x64-windows` / `x64-windows-static`
- MinGW-w64 ↔ `x64-mingw-dynamic` / `x64-mingw-static`

- **CMake can’t find `glfw3Config.cmake`**
If using vcpkg, make sure you pass the toolchain file and a triplet:
```bash
-DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake
-DVCPKG_TARGET_TRIPLET=x64-windows-static # or your platform

- **DLLs missing on Windows (dynamic triplets)**
Copy runtime DLLs (e.g., `glfw3.dll`) next to your executable or use a static triplet.

- **Linux: missing dev packages**
Some distros don’t ship certain OpenGL helpers (e.g., `libglad-dev`).
On Ubuntu/Debian, enable `universe` or just skip it β€” this template will **auto-fetch GLAD** via `FetchContent`.

---
## πŸ“œ License

This project is licensed under the [MIT License](LICENSE).

---
## πŸ™Œ Attribution

This project was originally generated from [d3m1d0s/OpenGLTemplate](https://github.com/d3m1d0s/OpenGLTemplate) - MIT.
If you use this as a template, please keep this line or move it to your `NOTICE` file.