首页 > 代码库 > Mac Cmake OpenGL(Glut)

Mac Cmake OpenGL(Glut)

Main.cpp

#include <GLUT/glut.h>
#include <cstdlib> /* for exit */

using namespace std;

static void display()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_POLYGON);
  glVertex2f(-0.5, -0.5);
  glVertex2f(-0.5, 0.5);
  glVertex2f(0.5, 0.5);
  glVertex2f(0.5, -0.5);
  glEnd();
  glFlush();
}

static void init()
{
  glClearColor(1.0, 0.9, 0.65, 0.0);
  glColor3f(0.5,0.1, 0.1);
}

static void keyboard(unsigned char key, int x, int y)
{
  switch(key) {
  case 'q':
  case 'Q':
  case 27: // ESC key
    exit(0);
    break;
  }
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(0,0);
  glutCreateWindow("template");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);
  init();
  glutMainLoop();
  return 0;
}

CMakeLists.txt

cmake_minimum_required (VERSION 2.6)

project(OpenGL)

find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})

find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIR})

set(CMAKE_CXX_FLAGS "-g -Wall")

add_executable(OpenGL Main.cpp)
target_link_libraries(OpenGL ${GLUT_LIBRARY} ${OPENGL_LIBRARY})

在Main.cpp 和 CMakeLists.txt所在目录 打开终端 依次执行

mkdir build

cd build

cmake -G "Unix Makefiles" ..

make

./OpenGL

Mac Cmake OpenGL(Glut)