Ubuntu14.04下配置OpenGL及测试代码

前端之家收集整理的这篇文章主要介绍了Ubuntu14.04下配置OpenGL及测试代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ubuntu14.04 64位下,默认是没有安装OpenGL相关依赖库的,若安装,则依次执行如下几条命令即可:

$ sudo apt-get update
$ sudo apt-get install build-essential
$ sudo apt-get install libgl1-mesa-dev
$ sudo apt-get install libglu1-mesa-dev
$ sudo apt-get install freeglut3-dev

以下是验证其正确性的测试代码

#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

namespace {

void Init(void)
{
    glClearColor(1.0,0.0,1.0,0.0);//设置背景颜色为洋红
    glColor3f(0.0f,1.0f,0.0f);//设置绘图颜色为绿色
    glPointSize(4.0);//设置点的大小为4*4像素
    glMatrixMode(GL_PROJECTION);//设置合适的矩阵
    glLoadIdentity();
    gluOrtho2D(0.0,640.0,480.0);
}

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);//清屏
    glBegin(GL_POINTS);
    glVertex2i(289,190);
    glVertex2i(320,128);
    glVertex2i(239,67);
    glVertex2i(194,101);
    glVertex2i(129,83);
    glVertex2i(75,73);
    glVertex2i(74,74);
    glVertex2i(20,10);
    glEnd();
    glFlush();
}

} // namespace

int main(int argc,char* argv[])
{
    glutInit(&argc,argv);//初始化工具包
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设置显示模式
    glutInitWindowSize(640,480);//设置窗口大小
    glutInitWindowPosition(100,150);//设置屏幕上窗口位置
    glutCreateWindow("my first attempt");//打开带标题的窗口
    glutDisplayFunc(&Display);//注册重画回调函数
    Init();
    glutMainLoop();//进入循环

    return 0;
}

CMakeLists.txt文件内容如下:

PROJECT(multi_executable_file)
CMAKE_MINIMUM_required(VERSION 3.0)

# 查找OpenCV
FIND_PACKAGE(OpenCV required)
IF(OpenCV_FOUND)
	MESSAGE("===== support OpenCV =====")
	MESSAGE(STATUS "OpenCV library status:")
	MESSAGE(STATUS "	version: ${OpenCV_VERSION}")
	MESSAGE(STATUS "	include path: ${OpenCV_INCLUDE_DIRS}")
	MESSAGE(STATUS "	install path: ${OpenCV_INSTALL_PATH}")
	MESSAGE(STATUS "	libraries: ${OpenCV_LIBS}")
	INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
ELSE()
	MESSAGE("##### not support OpenCV #####")
ENDIF()

# 查找OpenMP
FIND_PACKAGE(OpenMP required)
IF(OpenMP_FOUND)
	MESSAGE("===== support OpenMP =====")
	MESSAGE(STATUS "OpenMP library status:")
	MESSAGE(STATUS "	version: ${OpenMP_VERSION}")
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ELSE()
	MESSAGE("##### not support OpenMP #####")
ENDIF()

# 查找OpenGL
FIND_PACKAGE(OpenGL required)
IF(OPENGL_FOUND)
	MESSAGE("===== support OpenGL =====")
	MESSAGE(STATUS "OpenGL library status:")
	MESSAGE(STATUS "	include path: ${OPENGL_INCLUDE_DIR}")
	MESSAGE(STATUS "	libraries: ${OPENGL_LIBRARIES}")
	INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
ELSE()
	MESSAGE("##### not support OpenGL #####")
ENDIF()

# 查找GLUT
FIND_PACKAGE(GLUT required)
IF(GLUT_FOUND)
	MESSAGE("===== support GLUT =====")
	MESSAGE(STATUS "	include path: ${GLUT_INCLUDE_DIR}")
	MESSAGE(STATUS "	libraries: ${GLUT_LIBRARIES}")
	INCLUDE_DIRECTORIES(${GLUT_INCLUDE_DIR})
ELSE()
	MESSAGE("##### not support GLUT #####")
ENDIF()

# 支持C++11
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2 -std=c11")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -g -Wall -O2 -std=c++11")

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})

FILE(GLOB samples ${PROJECT_SOURCE_DIR}/*.cpp)

FOREACH (sample ${samples})
	STRING(REGEX MATCH "[^/]+$" sample_file ${sample})
	STRING(REPLACE ".cpp" "" sample_basename ${sample_file})
	ADD_EXECUTABLE(test_${sample_basename} ${sample})
	TARGET_LINK_LIBRARIES(test_${sample_basename} ${OpenCV_LIBS} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} pthread)
ENDFOREACH()

build.sh脚本内容如下:

#! /bin/bash

echo "Note: new create build directory,and executable file in build"
echo ${PWD}
mkdir -p build
cd build
cmake ..
make

进入到multi_executable_file目录后,执行:$ ./build.sh, 会自动生成build目录,并编译当前的.cpp文件,最终针对每个.cpp文件均会生成对应的执行文件,然后进入到build目录下,执行所需的执行文件即可。

执行结果如下:


GitHubhttps://github.com/fengbingchun/Linux_Code_Test

原文链接:https://www.f2er.com/ubuntu/349580.html

猜你在找的Ubuntu相关文章