Skip to content

Instantly share code, notes, and snippets.

@caiorss
Created December 27, 2018 03:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caiorss/fd9e61887e9b2575f2275a80af44fac2 to your computer and use it in GitHub Desktop.
Save caiorss/fd9e61887e9b2575f2275a80af44fac2 to your computer and use it in GitHub Desktop.
Minimal CMake project with OpenGL
cmake_minimum_required(VERSION 3.9)
# Proeject name - should not contain whitespace
project(OpengGL_CPP)
#========== Global Configurations =============#
#----------------------------------------------#
# Set the C++ standard for all targets (It sets the flags
# (-std=c++11, -std=c++14 ...) on Clang or GCC. and /std=c++17 on MSVC
# OPTIONAL:
#---------------------------------
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
#========== Targets Configurations ============#
# Build an executable (Unix-like OSes generates ./openglDemo1, on
# Windows ./opengDemo1.exe)
# ..........................................
add_executable(openglDemo1 opengl1.cpp)
# Set executable dependency libraries
# Equivalent to pass flags -lGL, -lGLU and -lglut
target_link_libraries(openglDemo1 GL GLU glut)
# 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}
)
#include <iostream>
#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();
}
@Magoninho
Copy link

Thanks! ^^

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