安裝編譯環境和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
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 apt-get install xxx
/* light.c
此程序利用GLUT繪製一個OpenGL窗口,並顯示一個加以光照的球。
*/
/* 由於頭文件glut.h中已經包含了頭文件gl.h和glu.h,所以只需要include 此文件*/
# include <GL/glut.h>
# include <stdlib.h>
/* 初始化材料屬性、光源屬性、光照模型,打開深度緩衝區 */
void init ( void )
{
GLfloat mat_specular [ ] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess [ ] = { 50.0 };
GLfloat light_position [ ] = { 1.0, 1.0, 1.0, 0.0 };
glClearColor ( 0.0, 0.0, 0.0, 0.0 );
glShadeModel ( GL_SMOOTH );
glMaterialfv ( GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv ( GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv ( GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_DEPTH_TEST);
}
/*調用GLUT函數,繪製一個球*/
void display ( void )
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 40, 50);
glFlush ();
}
/* 定義GLUT的reshape函數,w、h分別是當前窗口的寬和高*/
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
if (w <= h)
glOrtho (-1.5, 1.5, -1.5 * ( GLfloat ) h / ( GLfloat ) w, 1.5 * ( GLfloat ) h / ( GLfloat ) w, -10.0, 10.0 );
else
glOrtho (-1.5 * ( GLfloat ) w / ( GLfloat ) h, 1.5 * ( GLfloat ) w / ( GLfloat ) h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( ) ;
}
/* 定義對鍵盤的響應函數 */
void keyboard ( unsigned char key, int x, int y)
{
/*按Esc鍵退出*/
switch (key)
{
case 27:
exit ( 0 );
break;
}
}
int main(int argc, char** argv)
{
/* GLUT環境初始化*/
glutInit (&argc, argv);
/* 顯示模式初始化 */
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
/* 定義窗口大小 */
glutInitWindowSize (300, 300);
/* 定義窗口位置 */
glutInitWindowPosition (100, 100);
/* 顯示窗口,窗口標題爲執行函數名 */
glutCreateWindow ( argv [ 0 ] );
/* 調用OpenGL初始化函數 */
init ( );
/* 註冊OpenGL繪圖函數 */
glutDisplayFunc ( display );
/* 註冊窗口大小改變時的響應函數 */
glutReshapeFunc ( reshape );
/* 註冊鍵盤響應函數 */
glutKeyboardFunc ( keyboard );
/* 進入GLUT消息循環,開始執行程序 */
glutMainLoop( );
return 0;
}
然後:
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
}