Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Created March 1, 2020 19:16
Show Gist options
  • Save Trass3r/74e323ce43230b5e4840fab50d93a41d to your computer and use it in GitHub Desktop.
Save Trass3r/74e323ce43230b5e4840fab50d93a41d to your computer and use it in GitHub Desktop.
template for OpenGL with CMake, Conan, glad and glfw
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING "" FORCE)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "")
project(cppgl)
set(CMAKE_CXX_STANDARD 20)
if (NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.14/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt
BASIC_SETUP CMAKE_TARGETS
BUILD missing)
file(GLOB SRCS CONFIGURE_DEPENDS *.cpp *.h *.vert *.frag *.glsl *.geom)
add_executable(cppgl ${SRCS} conanfile.txt)
conan_target_link_libraries(cppgl)
target_precompile_headers(cppgl PRIVATE pch.h)
[requires]
glfw/[>=3.3]@bincrafters/stable
glad/[>=0.1.29]@bincrafters/stable
glm/[>=0.9.9]@g-truc/stable
[options]
glad:profile=core
glad:api_type=gl
glad:api_version=4.6
glad:spec=gl
glad:no_loader=False
[generators]
cmake
cmake_find_package
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
#include <vector>
static void printGLFWInfo(GLFWwindow* window)
{
int profile = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
const char* profileStr = "";
if (profile == GLFW_OPENGL_COMPAT_PROFILE)
profileStr = "OpenGL Compatibility Profile";
else if (profile == GLFW_OPENGL_CORE_PROFILE)
profileStr = "OpenGL Core Profile";
printf("GLFW %s %s\n", glfwGetVersionString(), profileStr);
}
static bool setupOpenGL()
{
if (!gladLoadGL())
{
printf("Could not initialize OpenGL!\n");
return false;
}
printf("OpenGL %d.%d\n", GLVersion.major, GLVersion.minor);
#define C(x) (x ? (const char*)x : "")
std::cout << "GL_VERSION..........: " << C(glGetString(GL_VERSION)) << '\n';
std::cout << "GL_RENDERER.........: " << C(glGetString(GL_RENDERER)) << '\n';
std::cout << "GL_VENDOR...........: " << C(glGetString(GL_VENDOR)) << '\n';
std::cout << "GLSL_VERSION........: " << C(glGetString(GL_SHADING_LANGUAGE_VERSION)) << '\n';
std::cout << "-----------------------\n";
setupStderrDebugCallback();
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
int main()
{
glfwSetErrorCallback([](int error, const char* description) {
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
});
if (!glfwInit())
return;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
GLFWmonitor* monitor = fullscreen ? glfwGetPrimaryMonitor() : nullptr;
window = glfwCreateWindow((int)windowWidth, (int)windowHeight, "Main Window", monitor, nullptr);
if (!window)
{
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
printGLFWInfo(window);
if (!setupOpenGL())
exit(2);
glfwSwapInterval(vsync);
glfwSetKeyCallback(window, &::keyboardFunc);
glfwSetScrollCallback(window, &::scrollFunc);
glfwDestroyWindow(window);
}
@jay-tux
Copy link

jay-tux commented Oct 11, 2021

@Trass3r please update the conanfile for glad.

  • the api_version has changed to gl_version
  • the api_type doesn't seem to exist anymore
  • the profile has changed to gl_profile

Otherwise, thank you for the great help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment