linux 下openGL的配置

安装编译环境和OpenGL图形驱动

1、安装基本编译环境

sudo apt-get install build-essential

2、安裝OpenGL Library

sudo apt-get install libgl1-mesa-dev

3、安裝OpenGL Utilities

sudo apt-get install libglu1-mesa-dev

4. sudo apt-get install freeglut3 freeglut3-dev
5. sudo apt-get install mesa-common-dev mesa-utils

OpenGL Utilities 是一組建構於 OpenGL Library 之上的工具組,提供許多很方便的函式,使 OpenGL 更強大且更容易使用

也可以:

sudo apt-get install mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev


如果提示:“gl\glew.h”: No such file or directory

sudo apt-cache search glew
sudo <a target=_blank target="_blank" class="inner-link decor-none" href="http://zhidao.baidu.com/search?word=apt-get&fr=qb_search_exp&ie=utf8" rel="nofollow" style="color:rgb(45,100,179); text-decoration:none">apt-get</a> install xxx

  1. /* light.c  
  2.  此程序利用GLUT绘制一个OpenGL窗口,并显示一个加以光照的球。  
  3.  */  
  4.  /* 由于头文件glut.h中已经包含了头文件gl.h和glu.h,所以只需要include 此文件*/  
  5.  # include <GL/glut.h>  
  6.  # include <stdlib.h>  
  7.     
  8.  /* 初始化材料属性、光源属性、光照模型,打开深度缓冲区 */  
  9.  void init ( void )  
  10.  {  
  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.    
  18.      glMaterialfv ( GL_FRONT, GL_SPECULAR, mat_specular);  
  19.      glMaterialfv ( GL_FRONT, GL_SHININESS, mat_shininess);  
  20.      glLightfv ( GL_LIGHT0, GL_POSITION, light_position);  
  21.    
  22.      glEnable (GL_LIGHTING);  
  23.      glEnable (GL_LIGHT0);  
  24.      glEnable (GL_DEPTH_TEST);  
  25.  }  
  26.    
  27.  /*调用GLUT函数,绘制一个球*/  
  28.  void display ( void )  
  29.  {  
  30.      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  31.      glutSolidSphere (1.0, 40, 50);  
  32.      glFlush ();  
  33.  }  
  34.    
  35.    
  36.  /* 定义GLUT的reshape函数,w、h分别是当前窗口的宽和高*/  
  37.  void reshape (int w, int h)  
  38.  {  
  39.      glViewport (0, 0, (GLsizei) w, (GLsizei) h);  
  40.      glMatrixMode (GL_PROJECTION);  
  41.      glLoadIdentity ( );  
  42.      if (w <= h)  
  43.          glOrtho (-1.5, 1.5, -1.5 * ( GLfloat ) h / ( GLfloat ) w, 1.5 * ( GLfloat ) h / ( GLfloat ) w, -10.0, 10.0 );  
  44.      else  
  45.          glOrtho (-1.5 * ( GLfloat ) w / ( GLfloat ) h, 1.5 * ( GLfloat ) w / ( GLfloat ) h, -1.5, 1.5, -10.0, 10.0);  
  46.      glMatrixMode ( GL_MODELVIEW );  
  47.      glLoadIdentity ( ) ;  
  48.  }  
  49.    
  50.    
  51.  /* 定义对键盘的响应函数 */  
  52.  void keyboard ( unsigned char key, int x, int y)  
  53.  {  
  54.      /*按Esc键退出*/  
  55.      switch (key)   
  56.      {  
  57.          case 27:  
  58.          exit ( 0 );  
  59.          break;  
  60.      }  
  61.  }  
  62.    
  63.    
  64.  int main(int argc, char** argv)  
  65.  {  
  66.      /* GLUT环境初始化*/  
  67.      glutInit (&argc, argv);  
  68.      /* 显示模式初始化 */  
  69.      glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);  
  70.      /* 定义窗口大小 */  
  71.      glutInitWindowSize (300, 300);  
  72.      /* 定义窗口位置 */  
  73.      glutInitWindowPosition (100, 100);  
  74.      /* 显示窗口,窗口标题为执行函数名 */  
  75.      glutCreateWindow ( argv [ 0 ] );  
  76.      /* 调用OpenGL初始化函数 */  
  77.      init ( );  
  78.      /* 注册OpenGL绘图函数 */  
  79.      glutDisplayFunc ( display );  
  80.      /* 注册窗口大小改变时的响应函数 */  
  81.      glutReshapeFunc ( reshape );  
  82.      /* 注册键盘响应函数 */  
  83.      glutKeyboardFunc ( keyboard );  
  84.      /* 进入GLUT消息循环,开始执行程序 */  
  85.      glutMainLoop( );  
  86.      return 0;  
  87.  }  

然后:

g++ light.cpp -o sample -lglut -lGL -lGLU


其他需要的库文件:

OpenGL 下有一些重用的辅助库,比如glut,glee,glew等等,在windows平台下需要自己安装,因为微软为了推广自己的DX,在windows下只支持openGL 1.1版本。你可以打开\Microsoft Visual Studio X.X\VC\include\gl\gl.h 或 \Microsoft  SDKs\Windows\v6.1\Include\gl\gl.h 文件,查看微软默认支持的openGL版本号是:

 

/* Version */

#define GL_VERSION_1_1                    1

 

现在openGL已经发展到3.0了,因此我们需要自己下载配置这些库,在这里我们来安装glut, glee, glew这三个库,以及一些OpenGL扩展支持。

 

glut : 提供对窗口的封装,这是跨平台窗口的,我们就不必自己去编写烦琐的窗口代码。

glee : 方便用来判断当前系统是不是支持某项OpenGL特性,我们就不用自己去写烦琐的先取函数地址然后再判断的代码了。

glew : 因为windows默认只支持OpenGL 1.1,你要想用更高版本的OpenGL,你就需要安装它,它能自动识别你的平台所支持的全部OpenGL高级扩展函数。

 

1,安装 glut

 

GLUT3.7下载地址:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip


点击上面的链接下载最新的GLUT,最新的GLUT版本是3.7,解压,将 glut32.dll 和 glut.dll 拷贝到 c:\windows\system32 下面,将 glut32.lib 和 glut.lib 拷贝到 VC 安装目录下的 lib 目录下(如:\Microsoft Visual Studio 9.0\VC\lib\下),将 glut.h 拷贝到VC安装目录下的 \include\gl\ 目录下(如:\Microsoft Visual Studio 9.0\VC\include\gl\下)。在程序中我们只需要把

 

#include <GL/gl.h>

#include <GL/glu.h>

 

 

#include <GL/glut.h>

 

替换就可以了。因为在头文件 glut.h 中已经包含这些头文件,并导入了必要的库:

 

#pragma comment (lib, "winmm.lib")     /* link with Windows MultiMedia lib */

#pragma comment (lib, "opengl32.lib")  /* link with Microsoft OpenGL lib */

#pragma comment (lib, "glu32.lib")     /* link with OpenGL Utility lib */

#pragma comment (lib, "glut32.lib")    /* link with Win32 GLUT lib */

 

2,安装 glew


下载链接:https://sourceforge.net/project/downloading.php?group_id=67586&filename=glew-1.5.1-win32.zip

 

点击上面的链接下载最新的GLEW(支持OpenGL 3.0),解压,将 \bin\glew32.dll 拷贝到 c:\windows\system32 下面,将 \lib\glew32.lib 拷贝到VC安装目录下的 lib 目录下(如:\Microsoft Visual Studio 9.0\VC\lib\下),将 \include\glew.h 和 \include\wglew.h 拷贝到 VC 安装目录下的 \include\gl\ 目录下(如:\Microsoft Visual Studio 9.0\VC\include\gl\下)。在程序中我们只需要在包含gl,glu 或 glut.h 之前包含 glew.h就可以了(注意:一定要先包含 glew.h),在在代码中加上这么一句:

 

#pragma comment (lib, "glew32.lib")  

 

示例:

 

#include <GL/glew.h>

#include <GL/glut.h>

 

#progrma comment(lib, "glew32.lib")

 

在创建OpenGL渲染context之后,调用 glewInit(); 初始化glew就可以了。

 

 

3,安装 glee

 

GLee 主页:http://elf-stone.com/glee.php

下载链接:http://elf-stone.com/getfile.php?title=GLee

 

点击上面的链接下载最新的GLee,解压,将 GLee.lib 拷贝到 VC 安装目录下的 lib 目录下(如:\Microsoft Visual Studio 9.0\VC\lib\下),将 GLee.h 拷贝到VC安装目录下的 \include\gl\ 目录下(如:\Microsoft Visual Studio 9.0\VC\include\gl\下)。在应用程序中,我们就可以像如下来使用:

 

#include <GL/GLee.h>

#progrma comment(lib, "GLee.lib")

 

if( GLEE_ARB_multitexture ) // is multitexture support available?

{

      glMultiTexCoord2fARB(...);    // safe to use multitexture

}

else 

{

      // fallback

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值