博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ubuntu系统OPENGL初体验
阅读量:4959 次
发布时间:2019-06-12

本文共 5295 字,大约阅读时间需要 17 分钟。

由于工作项目需要开始接触 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 
7 #include
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/srcGLUT_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] 

[2] 

[3] 

[4] 

[5] 

转载于:https://www.cnblogs.com/phillee/p/10986921.html

你可能感兴趣的文章
WebLogic 12c 多节点Cluster静默安装
查看>>
win8中如何禁用屏幕旋转的快捷键
查看>>
Solution 23: 判断矩形和圆是否相交
查看>>
Qt And MFC Mouse Over Tips
查看>>
JSP/Servlet 中的汉字编码问题
查看>>
《构建之法》(十)
查看>>
django之信号
查看>>
[noip2013]货车运输(kruskal + 树上倍增)
查看>>
简单工厂模式
查看>>
#hashMap冲突原理#详细
查看>>
基于单片机定时器---年月日时分秒的算法
查看>>
linux中IDE和SATA硬盘的区别
查看>>
关于清理缓存的解决方案
查看>>
编译时获得系统的日期和时间
查看>>
Unity3D写雷电游戏(一)
查看>>
Mybatis之使用注解开发CRUD
查看>>
C语言错误:request for member ‘xxx’ in something not a structure or union
查看>>
[LintCode] Pow(x, n) 求x的n次方
查看>>
冒泡排序逐步详解相关笔记(一)
查看>>
sql server split 分割 两种方法
查看>>