zoukankan      html  css  js  c++  java
  • Ubuntu16.04OPENGL初体验

    由于工作项目需要开始接触 OPENGL ,进行图像处理与可视化方面软件的开发。之前完全没有学习过相关知识,现在才知道原来有些 3D 图像是通过 OpenGL/Direct3D 等编程接口来做的。

    市面上最好的两本 OpenGL 的书应该是《 OpenGL 编程指南》(红宝书)和《 OpenGL 编程宝典》(蓝宝书)。有机会的话再系统地学习研究。

    • 运行环境配置

    首先我们需要配置环境,安装 OPENGL 及相关的开发包

    ~$ sudo apt-get install build-essential 
    libgl1-mesa-dev 
    freeglut3-dev 
    libglew-dev 
    libsdl2-dev 
    libsdl2-image-dev 
    libglm-dev 
    libfreetype6-dev
    • 实例上手体验

    先找个例子实验一下

     1 // @filename: test.cc
     2 // 该程序利用GLUT绘制一个OpenGL窗口
     3 // 同时显示一个添加光照的球
     4 // 头文件glut.h中已经包含了gl.h和glu.h
     5 // 这里只需要包含glut.h即可
     6 #include <stdlib.h>
     7 #include <GL/glut.h>
     8 
     9 // 初始化材料属性/光源属性/光照模型,并打开深度缓冲区
    10 void init(void) {
    11     GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
    12     GLfloat mat_shininess[] = {50.0};
    13     GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
    14     
    15     glClearColor(0.0, 0.0, 0.0, 0.0);
    16     glShadeModel(GL_SMOOTH);
    17     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    18     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    19     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    20     
    21     glEnable(GL_LIGHTING);
    22     glEnable(GL_LIGHT0);
    23     glEnable(GL_DEPTH_TEST);
    24 }
    25 
    26 // 调用GLUT函数,绘制一个球
    27 void display(void) {
    28     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    29     glutSolidSphere(1.0, 40, 50);
    30     glFlush();
    31 }
    32 
    33 int main(int argc, char **argv) {
    34     // GLUT环境初始化
    35     glutInit(&argc, argv);
    36 
    37     // 显示模式初始化
    38     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    39 
    40     // 定义窗口大小
    41     glutInitWindowSize(300, 300);
    42 
    43     // 定义窗口位置
    44     glutInitWindowPosition(100, 100);
    45 
    46     // 显示窗口,窗口标题为执行函数名
    47     glutCreateWindow(argv[0]);
    48 
    49     // 调用OpenGL初始化函数
    50     init();
    51 
    52     // 注册OpenGL绘图函数
    53     glutDisplayFunc(display);
    54 
    55     // 进入GLUT消息循环,开始执行程序
    56     glutMainLoop();
    57 
    58     return 0;
    59 }

    文件结构展示如下

    playground
    ├── build
    ├── CMakeLists.txt
    ├── Readme.md
    └── src
        └── test.cc

    其中 CMakeLists.txt 内容如下所示

     1 cmake_minimum_required( VERSION 2.8 )
     2 
     3 project( mytest )
     4 
     5 find_package( GLUT REQUIRED )
     6 include_directories( ${GLUT_INCLUDE_DIRS} )
     7 link_directories( ${GLUT_LIBRARY_DIRS} )
     8 add_definitions( ${GLUT_DEFINITIONS} )
     9 if ( NOT GLUT_FOUND )
    10     message( ERROR "glut not found!!" )
    11 endif ( NOT GLUT_FOUND)
    12 
    13 find_package( OpenGL REQUIRED )
    14 include_directories( ${OpenGL_INCLUDE_DIRS} )
    15 link_directories( ${OpenGL_LIBRARY_DIRS} )
    16 add_definitions( ${OpenGL_DEFINITIONS} )
    17 if (NOT OPENGL_FOUND)
    18     message( ERROR "opengl not found!!" )
    19 endif (NOT OPENGL_FOUND)
    20 
    21 add_executable( mytest src/test.cc )
    22 target_link_libraries( mytest ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
    • 遇到的问题及解决方法

    然后命令行进行 cmake 时出现了一点问题

    ~/playgroud/build$ cmake ..                                   
    -- The C compiler identification is GNU 5.4.0
    -- The CXX compiler identification is GNU 5.4.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Found GLUT: /usr/lib/x86_64-linux-gnu/libglut.so  
    -- Found OpenGL: /usr/lib/x86_64-linux-gnu/libGL.so  
    CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
    Please set them or make sure they are set and tested correctly in the CMake files:
    GLUT_Xmu_LIBRARY (ADVANCED)
        linked by target "mytest" in directory /home/cv/playgroud
    
    -- Configuring incomplete, errors occurred!
    See also "/home/cv/playgroud/build/CMakeFiles/CMakeOutput.log".

    大概搜了 cmake 中 target_link_libraries 的用法

    TARGET_LINK_LIBRARIES( 设置要链接的库文件的名称 )
    
    比如连接libhello.so库,以下写法都可以:
    TARGET_LINK_LIBRARIES(myProject hello)
    TARGET_LINK_LIBRARIES(myProject libhello.a)
    TARGET_LINK_LIBRARIES(myProject libhello.so)
     
    甚至下面这样的写法也是有效的:
    TARGET_LINK_LIBRARIES(myProject TARGET_LINK_LIBRARIES(myProject -leng)

    后来找到一个类似的问题,并根据其解决方案最终解决了自己的问题

    // 问题:找不到xi和xmu库
    
    // 具体错误信息
    CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
    Please set them or make sure they are set and tested correctly in the CMake files:
    GLUT_Xi_LIBRARY (ADVANCED)
        linked by target "WindowsTest" in directory /home/me/devl/c++/game/2DGame/src
    GLUT_Xmu_LIBRARY (ADVANCED)
        linked by target "WindowsTest" in directory /home/me/devl/c++/game/2DGame/src
    
    // 解决方法
    sudo apt-get install libxmu-dev libxi-dev

    然后再进行 cmake 和 make,顺利通过编译,运行生成的可执行文件得到了预期结果。

    另外要注意一下, CMakeLists.txt 中三个 mytest 要保持一致,均应为最终生成的可执行文件的名称,不能使用 test 的名称,因为会出现下面的问题,改一下就没问题了。

    CMake Warning (dev) at CMakeLists.txt:21 (add_executable):
      Policy CMP0037 is not set: Target names should not be reserved and should
      match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
      details.  Use the cmake_policy command to set the policy and suppress this
      warning.
    
      The target name "test" is reserved or not valid for certain CMake features,
      such as generator expressions, and may result in undefined behavior.
    This warning is for project developers.  Use -Wno-dev to suppress it.
    • 运行结果

    Reference

    [1] OpenGL学习之路(一)

    [2] Ubuntu 16.04 安装OpenGL

    [3] 使用CMakeLists.txt创建一个简单的opengl程序

    [4] CMake使用总结(一)

    [5] Eigen,OpenGL/GLUT在Ubuntu下的配置

  • 相关阅读:
    Shell脚本编程-02-----shell编程之条件语句
    ELK 简介
    Linux 下的网卡文件配置
    Tomcat 简介
    Docker 基本操作
    zabbix 介绍
    CentOS 上搭建 Kubernetes 集群
    Docker 简介
    yum 源的配置安装
    Docker 入门
  • 原文地址:https://www.cnblogs.com/phillee/p/10986921.html
Copyright © 2011-2022 走看看