Last active
January 9, 2019 02:56
-
-
Save caiorss/4e36d7423cfd456f39c26ba476873a3c to your computer and use it in GitHub Desktop.
Example: Cmake C++ project with VCPKG package manager and OpengGL/FreeGlut on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.9) | |
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) | |
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" | |
CACHE STRING "") | |
message(" [INFO] VCPKG CMAKE_TOOLCHAIN_FILE = ${CMAKE_TOOLCHAIN_FILE}") | |
endif() | |
#========== Global Configurations =============# | |
#----------------------------------------------# | |
project(OpengGL_Widnows) | |
set(CMAKE_CXX_STANDARD 17) | |
set(CMAKE_VERBOSE_MAKEFILE ON) | |
find_package(OpenGL REQUIRED) | |
find_package(GLUT REQUIRED) | |
find_package(GLU REQUIRED) | |
#========== Targets Configurations ============# | |
# Build an executable (Unix-like OSes generates ./openglDemo1, on | |
# Windows ./opengDemo1.exe) | |
# .......................................... | |
add_executable(openglDemo1 opengl1.cpp) | |
target_link_libraries(openglDemo1 PRIVATE GLUT::GLUT) | |
if(UNIX) | |
target_link_libraries(openglDemo1 PRIVATE GL) | |
endif() | |
# Add extension .bin to executable name to make it easier | |
# to identify that the binary file is an executable. | |
# So, it turns openglDemo1 becomes openglDemo1.bin | |
if(UNIX) | |
set_target_properties(openglDemo1 PROPERTIES SUFFIX ".bin") | |
endif() | |
# Add target to run executable | |
add_custom_target(run-ex1 | |
COMMAND openglDemo1 | |
DEPENDS openglDemo1 | |
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR} | |
) | |
# It installs (copies) the target files to a path relative to CMAKE_INSTALL_PREFIX | |
# So, it will copy the built files to ${CMAKE_INSTALL_PREFIX}/out | |
message(" Freeglut DLL = ${GLUT_RUNTIME_LIBRARY} ${FREEGLUT_DYNAMIC} ") | |
install(TARGETS openglDemo1 DESTINATION out) | |
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/freeglut.dll" DESTINATION out) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#if defined(_WIN32) | |
#include <windows.h> | |
#endif | |
#include <GL/gl.h> | |
#include <GL/glu.h> | |
#include <GL/glut.h> | |
void renderFunction(); | |
int main(int argc, char ** argv){ | |
std::cerr << "[INFO] Starting OpenGL main loop." << std::endl; | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize(500, 500); | |
glutCreateWindow("Window 1"); | |
// Display Callback Function | |
glutDisplayFunc(&renderFunction); | |
// Start main loop | |
glutMainLoop(); | |
std::cerr << "[INFO] Exit OpenGL main loop." << std::endl; | |
return 0; | |
} | |
void renderFunction(){ | |
std::cerr << "[INFO] Running loop." << std::endl; | |
// Clear the current output buffer | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Rotate 10 degrees counterclockwise around z axis | |
glRotated(10, 0, 0, 1); | |
// Set the current color (RGB) drawing to blue | |
glColor3f(0.0, 0.0, 1.0); | |
// Start polygon | |
glBegin(GL_POLYGON); | |
glVertex3f(-0.5, -0.5, 0); | |
glVertex3f( 0.5, -0.5, 0); | |
glVertex3f( 0.5, 0.5, 0); | |
glVertex3f(-0.5, 0.5, 0); | |
// End polygon | |
glEnd(); | |
glFlush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment