Winterpixel
8/14/2019 - 4:35 AM

CMakeLists.txt

cmake_minimum_required (VERSION 3.7.2)
project(GLUT_DEMO)
set(CMAKE_CXX_STANDARD 17)

# Include all .cpp and .h in src and include directories respectively
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
file(GLOB_RECURSE INCLUDE_FILES "src/*.hpp")

set (INCLUDE_DIRS "")
foreach (_headerFile ${INCLUDE_FILES})
    get_filename_component(_dir ${_headerFile} PATH)
    list (APPEND INCLUDE_DIRS ${_dir})
endforeach()
list(REMOVE_DUPLICATES INCLUDE_DIRS)

# OpenGL and GLUT
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})

add_executable(GLUT_DEMO ${SRC_FILES})
target_include_directories(GLUT_DEMO PRIVATE ${INCLUDE_DIRS})
target_link_libraries(GLUT_DEMO ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
#include <GL/gl.h>
#include <GL/glut.h>   // freeglut.h might be a better alternative, if available.
 

void display() {  // Display function will draw the image.
 
    glClearColor( 0, 0, 0, 1 );  // (In fact, this is the default.)
    glClear( GL_COLOR_BUFFER_BIT );
    
    glBegin(GL_TRIANGLES);
    glColor3f( 1, 0, 0 ); // red
    glVertex2f( -0.8, -0.8 );
    glColor3f( 0, 1, 0 ); // green
    glVertex2f( 0.8, -0.8 );
    glColor3f( 0, 0, 1 ); // blue
    glVertex2f( 0, 0.9 );
    glEnd(); 
    
    glutSwapBuffers(); // Required to copy color buffer onto the screen.
 
}


int main( int argc, char** argv ) {  

    glutInit(&argc, argv);               // Initialize GLUT
    glutInitDisplayMode(GLUT_SINGLE);    // Use single color buffer and no depth buffer.
    glutInitWindowSize(500,500);         // Size of display area, in pixels.
    glutInitWindowPosition(100,100);     // Location of window in screen coordinates.
    glutCreateWindow("GL RGB Triangle"); // Parameter is window title.
    glutDisplayFunc(display);            // Called when the window needs to be redrawn.
    
    glutMainLoop(); // Run the event loop!  This function does not return.
                    // Program ends when user closes the window.
    return 0;

}