Public Template
Watch 1
Fork 0
You've already forked opengl-template
Use this template

Graphics class project (CMake)

Copy this entire directory to your other machine. It does not depend on the original Visual Studio project or the sibling Libraries directory.

Requires CMake 3.20+, a C/C++ compiler supporting C++20, and a graphics driver supporting OpenGL 3.3. GLAD, GLM headers, and LodePNG are bundled. CMake uses an installed GLFW 3.4+ package if available; otherwise it downloads and builds GLFW 3.4 from source. That first configuration needs internet access. For offline use, install GLFW first or pass -DFETCHCONTENT_SOURCE_DIR_GLFW=/path/to/glfw-3.4 with an unpacked GLFW 3.4 source release.

The course target is OpenGL 3.3 core / GLSL 330: the framework requests a 3.3 context and both shaders in Skeleton.cpp declare #version 330. Keep these versions when editing assignments. GLAD's generated headers contain newer API declarations too; their presence does not change the requested context or shader language version. A driver may supply a newer compatible context.

Prerequisites

Ubuntu/Debian (both GLFW Linux backends):

sudo apt install build-essential cmake xorg-dev libwayland-dev libxkbcommon-dev

macOS: install Xcode Command Line Tools (xcode-select --install) and CMake (brew install cmake if you use Homebrew).

Windows with MSYS2 UCRT64: install the native toolchain and dependencies from the MSYS2 UCRT64 terminal:

pacman -S --needed mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-ninja mingw-w64-ucrt-x86_64-glfw

The ucrt64 preset assumes MSYS2 is installed at C:/msys64. It selects UCRT64 GCC and Ninja explicitly and puts their DLL directory on PATH during builds. With GLFW installed this way, no GitHub download is needed.

MSVC is also supported: install Visual Studio's Desktop development with C++ workload and configure from a Developer PowerShell in a separate build directory.

See the GLFW compilation guide for other distributions. To build only X11 on Linux, configure with -DGLFW_BUILD_WAYLAND=OFF; for only Wayland, use -DGLFW_BUILD_X11=OFF.

Build and run

Add an assignment (no CMake edits)

GreenTriangle is only the supplied example. Each folder containing a Skeleton.cpp directly under sources/ becomes a separate executable:

sources/
  framework.cpp, framework.h, glad.c, lodepng.cpp, lodepng.h  (shared)
  GreenTriangle/Skeleton.cpp                               (example)
  Assignment1/Skeleton.cpp                                 (your work)
  Assignment2/Skeleton.cpp                                 (another program)
  1. Copy sources/GreenTriangle to sources/Assignment1 (or create that folder and put your course source in it, named Skeleton.cpp).
  2. Edit the new Skeleton.cpp. Keep #include "framework.h"; the framework supplies main(). Each program has its own application instance.
  3. Build. CMake discovers new folders automatically with Ninja. In VS Code, run CMake: Configure to refresh the target list, then select Assignment1 as the build/run target. Building all targets also builds the example.

Use unique folder names such as Assignment1 or Lab_02 (letters, digits, underscores; avoid CMake names like all, test, and course_framework). The filename Skeleton.cpp is the discovery convention; extra .cpp files are not compiled automatically. This matches the course's single-file format.

cmake --build --preset ucrt64 --target Assignment1
./build/ucrt64/Assignment1.exe

For the portable Ninja preset, replace ucrt64 with ninja and omit .exe on Linux/macOS. Delete an assignment folder to remove its target. The shared framework and libraries are compiled once and reused by every executable.

VS Code / Ninja / UCRT64 GCC

Open this directory in VS Code with the CMake Tools extension. Run CMake: Select Configure Preset, choose Ninja + MSYS2 UCRT64 GCC (Windows), then CMake: Build. If presets are disabled in your VS Code settings, set cmake.useCMakePresets to always.

Equivalent commands from the UCRT64 terminal (or PowerShell with its bin directory on PATH):

cmake --preset ucrt64
cmake --build --preset ucrt64 --parallel
./build/ucrt64/GreenTriangle.exe

Keep C:/msys64/ucrt64/bin on PATH when running too, for the GCC runtime DLLs and an installed GLFW DLL. The portable ninja preset uses the system compiler on Linux/macOS: cmake --preset ninja then cmake --build --preset ninja.

Each preset uses its own build directory. CMake cannot switch generators or compiler toolchains inside an already configured build directory.

Other generators

From this directory:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release --parallel

Linux/macOS:

./build/GreenTriangle

Windows with the Visual Studio generator:

.\build\Release\GreenTriangle.exe

Smoke check: run the executable in a graphical desktop session; a window titled Green triangle should show a green triangle on black. Close it with the window close button. Rebuild after editing sources/GreenTriangle/Skeleton.cpp.

Conversion notes

  • The example's Skeleton.cpp is unchanged.
  • framework.h uses standard C++ filesystem instead of Microsoft's _HAS_CXX17 check, which selected the wrong namespace with GCC and Clang.
  • framework.cpp requests a forward-compatible core context on macOS, as required by GLFW's macOS guidance.
  • CMake builds the existing GLAD C source and LodePNG implementation, links GLFW's platform dependencies and the system dynamic-loader library, and retains the bundled optional GLM headers.
  • The GLFW download comes from the upstream 3.4 release. Third-party license notices are retained with the bundled sources.

Do not transfer build/ between machines; generate a fresh build on each one.

Validation: built with MSVC (Release) and Ninja + UCRT64 GCC 16.1 (Debug) on Windows, using temporary package configurations for the supplied GLFW binaries. The GCC check used the GLFW DLL import library, not the MSVC static library. GitHub downloads were blocked in the test environment. Linux/macOS builds and graphical runtime have not been verified here.

The assignment discovery check builds the example and a temporary second program:

python tests/check_assignments.py build/ucrt64

Run it after configuring that build directory. Python is only needed for this check, not for building or using the project.