From 1ee93ff0f0ea481874cfadfd857ff5de0ec38f98 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Sat, 30 Mar 2024 22:01:02 -0400 Subject: [PATCH] Got it compiling on macOS --- CMakeLists.txt | 13 ++++++++++--- main.cpp | 12 +++++++++--- triangle.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 triangle.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a8f8c41..f72781c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ project(OpengGL_Example) set(CMAKE_CXX_STANDARD 17) set(CMAKE_VERBOSE_MAKEFILE ON) +find_package(OpenGL REQUIRED) +find_package(GLUT REQUIRED) + set( NAME_SRC main.cpp ) @@ -12,6 +15,10 @@ set( NAME_SRC set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) add_executable( opengl_example ${NAME_SRC} ${NAME_HEADERS} ) -# Set executable dependency libraries -# Equivalent to pass flags -lGL, -lGLU and -lglut -target_link_libraries(opengl_example GL GLU glut) \ No newline at end of file +if (APPLE) + # Equivalent to pass flags -framework OpenGL + target_link_libraries(opengl_example OpenGL::GL GLUT::GLUT) +else() + # Equivalent to pass flags -lGL, -lGLU and -lglut + target_link_libraries(opengl_example GL GLU glut ) +endif() \ No newline at end of file diff --git a/main.cpp b/main.cpp index d32fe61..563d54a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,13 @@ // http://www.codebind.com/linux-tutorials/install-opengl-ubuntu-linux/ -#include -#include - +#ifdef __APPLE__ + #include + #include + #include +#else + #include + #include +#endif + void displayMe(void) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/triangle.cpp b/triangle.cpp new file mode 100644 index 0000000..57d097f --- /dev/null +++ b/triangle.cpp @@ -0,0 +1,52 @@ +// A simple introductory program; its main window contains a static picture +// of a triangle, whose three vertices are red, green and blue. The program +// illustrates viewing with default viewing parameters only. + +#ifdef __APPLE_CC__ +#include +#else +#include +#endif + +// Clears the current window and draws a triangle. +void display() { + + // Set every pixel in the frame buffer to the current clear color. + glClear(GL_COLOR_BUFFER_BIT); + + // Drawing is done by specifying a sequence of vertices. The way these + // vertices are connected (or not connected) depends on the argument to + // glBegin. GL_POLYGON constructs a filled polygon. + glBegin(GL_POLYGON); + glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5); + glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0); + glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0); + glEnd(); + + // Flush drawing command buffer to make drawing happen as soon as possible. + glFlush(); +} + +// Initializes GLUT, the display mode, and main window; registers callbacks; +// enters the main event loop. +int main(int argc, char** argv) { + + // Use a single buffered window in RGB mode (as opposed to a double-buffered + // window or color-index mode). + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + + // Position window at (80,80)-(480,380) and give it a title. + glutInitWindowPosition(80, 80); + glutInitWindowSize(400, 300); + glutCreateWindow("A Simple Triangle"); + + // Tell GLUT that whenever the main window needs to be repainted that it + // should call the function display(). + glutDisplayFunc(display); + + // Tell GLUT to start reading and processing events. This function + // never returns; the program only exits when the user closes the main + // window or kills the process. + glutMainLoop(); +} \ No newline at end of file