如何将opengl程序从Windows上linux移植windows到linux上

17:48 提问
Linux 下编译c++ opengl的小程序
完整的代码如下
#include &GL/glut.h&
#include &cmath&
#include &utility&
#include &vector&
using std::
using std::
vector&pair&int, int& & my_points(4);
vector&pair&int, int& & current_point(4);
my_points.push_back(make_pair(0, 0));
my_points.push_back(make_pair(50, 0));
my_points.push_back(make_pair(100, 0));
my_points.push_back(make_pair(150, 0));
current_point = my_
struct Degree {
Degree(int a, int b, int c) {
map&int, Degree& position(4);
position[0] = position(0, 0, 0);
position[1] = position(90, 0, 0);
position[2] = position(45, -45, 45);
position[3] = position(-90, 0, 0);
void init();
void display();
void draw_axis();
void reshape(int w, int h);
void draw_frame(void);
void translate(Degree& posi);
void keyboard(unsigned char key, int x, int y);
float matrix_rotat[3][3] = {
// 旋转矩阵
{0.0, -1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, 0.0, 1.0}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
void init(void) {
glClearColor(0.1, 0.2, 0.4, 0.0);
glShadeModel (GL_SMOOTH);
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w &= h)
gluOrtho2D(-200.0, 250.0, -100.0*(GLfloat)h/(GLfloat)w,
200.0*(GLfloat)h/(GLfloat)w);
gluOrtho2D(-200.0*(GLfloat)w/(GLfloat)h,
250.0*(GLfloat)w/(GLfloat)h, -50.0, 200.0);
glColor3f(1, 0.5, 0.4);
glMatrixMode(GL_MODELVIEW);
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0.5, 0.4);
draw_axis();
glLoadIdentity();
draw_frame();
glFlush ();
void draw_frame() {
glShadeModel(GL_SMOOTH);
glBegin (GL_LINE_STRIP);//draw frame
for (int i = 0; i & 4; i++) {
glColor3f(1.0, 0.0, 0.0);
glVertex2f(current_point[i].first, current_point[i].second);
void draw_axis() {
// draw coordinate axis
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-500.0, 40.0);
glVertex2f(500.0, 40.0);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.0, 400.0);
glVertex2f(0.0, -400.0);
// draw scale
glBegin(GL_LINES);
for (int i = -50; i &= 50; i++) {
glColor3f(1.0, 1.0, 1.0);
glVertex2f(10.0*i, 40.0);
glVertex2f(10.0*i, 44.0);
for (int i = -40; i &= 40; i++) {
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.0, 10.0*i);
glVertex2f(4.0, 10.0*i);
void translate(Degree& posi) {
changed into matrix later
vector&vector&double& & trans_matrix(2);
for (int i = 0; i & 2; i++)
for (int j = 0; j & 2; j++)
trans_matrix[i].push_back(0);
double num1[] = {cos(posi.first), -sin(posi.first)};
//initialize the transform matrix
for (int i = 0; i & 2; i++)
trans_matrix[0][i] = num1[i];
double num2 = {sin(posi.first), cos(posi.first)};
for (int i = 0; i & 2; i++)
trans_matrix[1][i] = num2[i];
current_point[0].first = current_point[0].second = 0;
for (int i = 0; i & 2; i++) {
current_point[0].first += (trans_matrix[i][0]*my_points[0].first
+ trans_matrix[i][1]*my_points[1].second);
current_point[0].second += (trans_matrix[i][0]*my_points[0].first
+ trans_matrix[i][1]*my_points[0].second);
glBegin (GL_LINES);//draw frame
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(current_point[0].first, current_point[0].second);
void rotate_frame(void) {
float (*tmp_frame)[7] = new float[3][7];
memset(tmp_frame, 0.0, sizeof(tmp_frame));
frame = tmp_
glLoadIdentity();
draw_frame(); // draw the frame after transformation
glFlush();
void keyboard(unsigned char key, int x, int y) {
switch (key) {
translate(position[0]);
translate(position[1]);
translate(position[2]);
translate(position[3]);
编译器报错>
arm.cpp:12:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(0, 0));
arm.cpp:13:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(50, 0));
arm.cpp:14:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(100, 0));
arm.cpp:15:1: error: ‘my_points’ does not name a type
my_points.push_back(make_pair(150, 0));
arm.cpp:16:1: error: ‘current_point’ does not name a type
current_point = my_
arm.cpp:28:1: error: ‘map’ does not name a type
map position(4);
arm.cpp:30:1: error: ‘position’ does not name a type
position[0] = position(0, 0, 0);
arm.cpp:31:1: error: ‘position’ does not name a type
position[1] = position(90, 0, 0);
arm.cpp:32:1: error: ‘position’ does not name a type
position[2] = position(45, -45, 45);
arm.cpp:33:1: error: ‘position’ does not name a type
position[3] = position(-90, 0, 0);
不知道这里是什么问题,STL为什么不能用,是编译选项的问题吗?
编译命令g++ arm.cpp -o arm -lGL -lglut -lGLU
按赞数排序
不用麻烦各位了,找到问题了
是因为 vector.push_back()不能在非函数内部使用
要把这些全局变量的初始话放到一个函数里面
void init_value() {
my_points.push_back(make_pair(0, 0));
my_points.push_back(make_pair(50, 0));
my_points.push_back(make_pair(100, 0));
my_points.push_back(make_pair(150, 0));
current_point = my_
position.push_back(Degree(0, 0, 0));
position.push_back(Degree(90, 0, 0));
position.push_back(Degree(45, -45, 45));
position.push_back(Degree(-90, 0, 0));
241关注|837收录
1360关注|1280收录
2609关注|145收录
其他相似问题Andorid-graphic system-opengl(6)
说明:在本节涉及的frame buffer是以显示为目的的frame buffer。
为了将GPU绘制结果在窗口中显示出来,必然需要将OpenGL和具体窗口系统相结合。如下图所示,OpenGL spec定义了GL context& /span&的行为,从具体窗口衍生出frame buffer,两者结合,即可完成OpenGL应用程序的显示输出。
当然,GL context的输出格式和Frame buffer的格式要一致才行,否则无法将GL context和frame buffer连接起来。比如,假如GL context将输出depth值,则frame buffer中应该包括depth buffer;假如GL context输出的颜色中的红色分量占8比特,则frame buffer中相应的color buffer的red分量也应该是8比特;假如GL context要求输出到back buffer中,则frame buffer就应该既包括front
buffer,也包括back buffer。
但是OpenGL spec并没有定义如何和具体窗口系统相结合的问题,所以,需要引入新的spec来支持。具体到Windows操作系统,就是WGL;而在Linux/X下,就是GLX。
考察一个OpenGL应用程序在Windows平台的刚开始运行时和WGL相关的流程
1.&&&&&&&& 调用函数CreateWindowEx创建窗口hWnd
2.&&&&&&&& 调用函数GetDC得到hDC,其函数参数是hWnd
3.&&&&&&&& 调用函数ChoosePixelFormat来查询hDC支持的所有像素格式,应用程序同时选择出其将采用哪种像素格式
4.&&&&&&&& 调用函数SetPixelFormat设置hDC的像素格式
5.&&&&&&&& 调用函数wglCreateContext创建hGLRC,其参数是hDC
6.&&&&&&&& 调用函数wglMakeCurrent,其参数是hDC和hGLRC
7.&&&&&&&& 调用glAPI函数绘制
粗略的,我们可以这样理解,第4步完成后将根据hDC衍生出frame buffer;第5步得到的hGLRC本质上是上图中的GL context;而第6步则将应用程序和GL context、GL context和frame buffer连接在一起。从第7步开始,所有的glAPI都将作用于和应用程序连接的GL context;而GL context的输出目的地则是和其连接的frame buffer。
其中,第5步的hDC和第6步的hDC并不需要是同一个值,只需要这两个hDC都设置了相同的像素格式即可,这样就可以保证GL context的输出格式和Frame buffer的格式的一致性。
考察一个OpenGL应用程序在Linux/X下的刚开始运行时和GLX1.4相关的流程
1.&&&&&&&& 调用函数XOpenDisplay(NULL)得到display,可以将其认为是X server对显卡的一个抽象
2.&&&&&&&& 查询display,确定其连接的screen
3.&&&&&&&& 调用函数glxChooseFBConfig查询将在其上绘制的screen所支持的fbconfigs(本质上和windows中的像素格式是相同的),应用程序同时选择出其将采用哪种fbconfig
4.&&&&&&&& 调用函数XCreateWindow创建窗口xWin,其参数包括fbconfig的部分内容
5.&&&&&&&& 调用函数glXCreateWindow创建glxWin,参数是xWin和fbconfig
6.&&&&&&&& 调用函数glXCreateNewContext创建glxCtx,参数是fbconfig
7.&&&&&&&& 调用函数glxMakeContextCurrent,其参数包括glxWin和glxCtx。
8.&&&&&&&& 调用glAPI函数绘制
粗略的,我们可以这样理解,第5步得到的glxWin将衍生出frame buffer;第6步得到的glxCtx本质上就是上图中的GL context;而第7步则是将应用程序和GL context、GL context和frame buffer连接在一起。从第8步开始,所有的glAPI都将作用于和应用程序连接的GL context;而GL context的输出目的地则是和其连接的frame buffer。
其中,第7步调用函数的参数glxWin和glxCtx要求他们在被创建时所传入的fbconfig参数是相同的,这样就可以保证GL context的输出格式和Frame buffer的格式的一致性。
至此,应该可以理解到hGLRC/glxCtx和GL context的关系,它们在本质上是同一回事。hGLRC是在WGL下对GL context的封装,glxCtx是在GLX下对GL context的封装。hGLRC和glxCtx是相对应用程序的概念,而GL context则是OpenGL内部的概念。
不管是Windows系统还是Linux/X,一个应用程序可能会多线程的在多个窗口上用OpenGL绘制。通过讨论这种复杂情况,来加深对wglMakeCurrent/glxMakeContextCurrent函数(以下将简称为MakeCurrent)的理解。
如下图所示,某多线程程序,且已创建多个GL contex和frame buffer。图示状态表示,线程甲已成功调用函数MakeCurrent(GL context 3, Frame buffer B),线程乙已成功调用函数MakeCurrent(GL context 1, Frame buffer A),线程丁已成功调用函数MakeCurrent(GL context 4, Frame buffer C)。
接下去,在线程甲中被执行的glAPI都将作用于GL context3,其绘制结果进入Frame buffer B;在线程乙和线程丁中调用的glAPI也将分别作用于GL context 1和GL context4,其绘制结果分别进入Frame buffer A和Frame buffer C。
假如某个线程不调用OpenGL绘制,那它可以不和任何GL context连接。每个线程同时只能最多和一个GL context连接,因为,假如超过一个的话,该线程接下去的glAPI调用就将无法确定应该作用于哪个GL context连接了。当某个线程调用MakeCurrent函数重新设置和GL context、Frame buffer的连接关系时,原有的连接关系失效。
每个GL context最多可以和一个线程连接,因为,假如超过一个的话,多个线程作用于同一个GL context,其绘制结果显然不是所希望的。每个GL contxt只和一个Frame buffer连接,显然是无法和多个Frame buffer连接的。
每个Frame buffer只和一个GL context连接,似乎spec并没有禁止一个Frame buffer和多个GL context连接,但是,强烈不建议这样做,很有可能驱动程序没有针对这种情况编写代码的。
在默认情况下,不同GL context之间是相互独立的。如果采用share context技术,可以使不同GL context之间共享部分信息,对一个GL context的改动,就会在在另一个GL context中体现出来。
在驱动程序中,为节约显存,frame buffer往往会被延迟到函数MakeCurrent被调用时才被真正创建。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:77797次
积分:1000
积分:1000
排名:千里之外
转载:86篇
评论:14条
(2)(1)(2)(8)(17)(4)(19)(38)下次自动登录
现在的位置:
& 综合 & 正文
【OpenGL】安装和配置OpenGL完美教程(Linux,Win7和Mac)
目的:备份文章
version: Fedora 15
kernel: 2.6.43.5-2.fc15.x86_64
gcc: 4.6.3
version: Red Hat Enterprise Linux Workstation release 6.3 (Santiago)
kernel: 2.6.32-279.el6.x86_64
gcc: 4.4.6
Eclipse 3.2.0
If GLUT is installed, it will be visible as:
library:/usr/lib/libglut.*
header file:/usr/include/GL/glut.h
If OpenGL and/or GLUT is not installed on your system, you can install both with:
# yum install freeglut-devel
which should install all the dependent packages, including OpenGL itself.
#include &GL/glut.h&
You don't need to include gl.h and glu.h, as they are already included in glut.h.
For an example, see the provided sample .
To make a GLUT application on the command line, use the following linker options:
-lGL -lGLU -lglut
Start up Eclipse and choose your workspace, click "OK" ()
Create a new project:
Select "File→New→Project" ()
Choose a wizard for your new project:
"C++→Managed Make C++ Project" ()
Give the project a name, e.g., "sampleapp", click "Next &" ()
On the "Select a type of project" page,
accept "Project Type: Executable (Gnu)" by clicking "Finish" ()
If prompted to "Open Associated Perspective?", click "Yes" ()
Add libraries:
Right click on "sampleapp" on the first line of the "C/C++" pane on the left and
select "Properties" to tell the linker which libraries need to be added ()
On the "Properties for sampleapp" page,
click "C/C++ Build→GCC C++ Linker→Libraries" and
click on the "Libraries (-l) Add" button ()
Type in "GL" and click "OK" ()
Repeat the above two steps to add "GLU" and "glut" libraries then click "Apply" and "OK" ()
Add source files:
Right click on "sampleapp" again and select "Import" ()
On the "Select" page, choose "General→File System" and click "Next &" ()
On the "File system" page, click the "Browse" button next to "From directory:" ()
Choose the directory where the source file resides and click "OK" ()
Click on all the source files on the right pane you want imported, for example,
the provided
(or click the directory name on the left pane to import
all files in the directory), and click "Finish" ()
Click on the play button on the second menu bar (
) to build and run the program:
On the "Create, manage, and run configurations" page, double click on "C/C++ Local Application" ()
On the "sampleapp" configuration page, click "Run" ()
version: Lion 10.7.2 (11C2002)
kernel: Darwin 11.2.0
Xcode: 4.2.1
gcc: 4.2.1
version: SnowLeopard 10.6.4 (10FS69)
kernel: Darwin 10.4.0
Xcode: 3.2.4
gcc: 4.2.1
version: Leopard 10.5.8 (9L30)
kernel: Darwin 9.8.0
Xcode: 3.1.2
gcc: 4.0.1
version: Tiger 10.4.11 (8S165)
kernel: Darwin 8.11.0
Xcode: 2.0
gcc: 4.0.0
Add source files:
Right click on "sampleapp" at the top of the left most pane and select "Add Files to 'sampleapp'" (), select your file, for example, the provided .
You can choose whether to copy added items into the project folder. I've chosen to keep my source files in the project folder in this example. Click "Add" ().
Add libraries:
In the middle pane, click the "Build Phases" tab, open the "Link Binary With Libraries" list, and click the '+' sign at the bottom left corner of the list ().
A list of frameworks should pop up. Scroll down the list to "OpenGL.framework" and click "Add" (). Repeat for "GLUT.framework".
to build and run the program.
Older versions of Xcode:
Graphical step-by-step for:
Create a new project:
Select "File→New Project"
Choose a template for your new project:
Xcode 3.2.4: "Application→Command Line Tool". For the sample code, select project type "C" and click "Choose" ()
Xcode 3.1.2: "Command Line Utility→C++ Tool" and click "Choose" ()
Xcode 2: "Command Line Utility→C++ Tool" and click "Next" ()
Give the project a name, e.g., "sampleapp" and click "Save" () [Xcode 2: click "Finish" ()]
Right click on file "main.cpp" to Delete it ()
Add source files:
Right click on "sampleapp" at the top of the "Groups & Files" pane on the left to "Add→Existing Files" (),
select your file, for example, the provided , and click "Add" (twice)
Add libraries:
Right click on "sampleapp" again to "Add→Existing Frameworks" ()
[Xcode 3.1.2: double click on the "Frameworks" folder ()]
select OpenGL.framework and click "Add" () [Xcode3.1.2: twice]
Repeat the previous step to add GLUT.framework ()
to build and run the program.
7 Enterprise
Vista x64 Enterprise Edition SP2 (Build 6002)
Vista Enterprise Edition SP2 (Build 6002)
XP Professional Edition SP3 5.1 (Build 2600)
Visual Studio 2012 version 11.0.51106.01 Update 1
.NET Framework version 4.5.50709
Visual Studio 2010 version 10.0. SP1Rel
.NET Framework version 4.0.30319 SP1Rel
Visual Studio 2008 version 9.0.21022.8.RTM
.NET Framework version 3.5 SP1
If GLUT is not installed, you can install it by downloading the
() and copying its files as follows:
runtime library:C:\Program Files\Microsoft Visual Studio *\VC\bin\glut32.dll
["Program Files (x86)" for 64-bit W The '*' matches your version of VS: 11.0 for VS for VS for VS2008]
header file:C:\Program Files\Microsoft Visual Studio *\VC\include\GL\glut.h
["Program Files (x86)" for 64-bit W You have to create the "GL" directory]
linker library:C:\Program Files\Microsoft Visual Studio *\VC\lib\glut32.lib
["Program Files (x86)" for 64-bit Windows]
If you don't have a VC folder, try instead:
runtime library:C:\Windows\system\glut32.dll
header file:C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\GL\glut.h
["Program Files (x86)" for 64-bit W VS2008: v6.0A]
linker library:C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\glut32.lib
["Program Files (x86)" for 64-bit W VS2008: v6.0A]
Or you may choose to install .
Later in your OpenGL source files, include the following line:
#include &GL/glut.h&
You don't need to include gl.h and glu.h, as they are already included in glut.h.
For an example, see the provided sample .
Command line make: see the
Visual Studio 08 project (, all the screen shots are from VS2008, but are applicable to the newer versions):
Create a new project:
Select "File→New→Project" ()
Choose a template for your new project:
"Visual C++→Win32→Win32 Console Application", give the project a name, e.g., "sampleapp" and click "OK" ()
On the "Welcome to the Win32 Application Wizard" page, click "Next &" ()
On the "Application Setting" dialog box, under "Additional options" tick "Empty project", then click "Finish" ()
Add source files:
Right click on your project, for example, "sampleapp", on the third (second) line of the "Solution Explorer" pane on the right (left) to "Add→Existing Item" (),
select your source and header file(s), for example, the provided , and click "Add"
Add libraries:
Right click on "sampleapp" again and select "Properties" to tell the linker which libraries need to be added ()
On the "Property Pages", under "Configuration:" tell VS to add the additional libraries to "All Configurations" ()
Select "Configuration Properties→Linker→Input" on the left pane, and enter:
opengl32.glu32.glut32.
[VS2008: with spaces in place of the semicolons]
as "Additional Dependencies" on the top of the right pane, hit RETURN and then click "Apply" ().
To prevent your program from opening a console window, while still on the "Property Pages",
select "Configuration Properties→Linker→Command Line" on the left pane, and enter:
/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup
as "Additional options" at the bottom of the right pane, then click "Apply" ()
You may not want to disable the console window if you print out messages to the console (see next step).
Close the "Property Pages" pane by clicking "Ok", then click on the play button on the second menu bar (
to build and run the program. If you print out messages to the console, run the program using Ctl-F5 instead,
to keep the cmd window from exiting after the program exits.
To distribute your program, include glut32.dll with your distribution as it's most likely not installed on most Windows systems.
version: NT-6.1 1.7.7(0.230/5/3)
version: NT-6.0-WOW64 1.5.25(0.156/4/2)
version: NT-6.0 1.5.25(0.156/4/2)
gcc: 3.4.4
Later in your OpenGL source files, include the following line:
#include &GL/glut.h&
You don't need to include gl.h and glu.h, as they are already included in glut.h.
For an example, see the provided sample .
To make a GLUT application on the command line, include the following linker options:
-lGL -lGLU -lglut
GLX under Win 7 doesn't support GLSL
If your GLSL code seg faults under Cygwin running on Win 7 when calling glCreateProgram(), see .
&&&&推荐文章:
【上篇】【下篇】Posts - 528,
Articles - 0,
Comments - 3
21:23 by htc开发, ... 阅读,
OpenGL是一个工业标准的三维计算机图形软件接口,它由SGI公司发布并广泛应用于Unix、OS/2、Windows/NT等多种平台,当然也包括Linux。在Windows/NT平台上,一般的开发工具如VC、BC、Fortran Powerstation等都支持直接的OpenGL应用的开发;在商用Unix平台上,Motif同样很好的支持OpenGL(毕竟OpenGL最初是工作站上的东西);那么在Linux上呢?    本文不着力于OpenGL编程的方法和技巧,而是把重点放在如何在Linux平台上开发OpenGL程序。介绍支持OpenGL的几个工具包,并辅以详细的实例来阐述。&    1. Linux下OpenGL编程环境简介      OpenGL不是自由软件,它的版权、商标(OpenGL这个名字)都归SGI公司所有。但在Linux下有OpenGL的取代产品:Mesa。Mesa提供和OpenGL几乎完全一致的接口,对利用OpenGL API编程的人来说,几乎感觉不到任何差异。Mesa是遵循GPL协议(部分遵循LGPL协议)的自由软件,而且,正是由于Mesa的自由性,它在对新硬件的支持度等方面都超过了OpenGL。Mesa可以从www.mesa3d.org取得。得到Mesa后,依照说明即可生成编写程序所需要的动态、静态连接库和头文件。    了解OpenGL的读者都知道,OpenGL本身只提供三维图形接口,不具备绘制窗口、接受响应、处理消息等功能。这些功能必须由第三方的开发环境提供,如上面提及的VC等等。有人会想,既然在Motif下可以开发OpenGL程序,那么,使用Linux下的Lesstif也应该可以。是的,的确可以,但不幸的是,Linux下的Lesstif是一个很不成熟的产品,而且也不具有可移植性,所以应用Lesstif开发的人很少。下面我们简单介绍几个常用的工具包。    在Linux下开发OpenGL程序,最常用的工具是GLUT(The OpenGL Utility Toolkit)。它可以创建一个或多个OpenGL窗口,响应、处理用户的交互操作、简单的弹出式菜单以及一些内置的绘图和字体处理功能。GLUT和OpenGL一样,可以移植于多种平台。由于它良好的表现,现在它已经成为Mesa发布的标准套件之一。    另一个很好的开发工具包是FLTK(Fast Light Tool Kit),这是一个用C++编写的图形界面开发工具。和GTK++、KDE不同,它只关注于图形界面的设计,而尽量不牵涉其他的实际应用。这个特点使得它比其他许多开发工具简练和高效。而且,它同样也是一个具有良好移植性的开发工具。事实上,它现在正引来越来越多人的兴趣,许多商业软件(尤其是致力于开发嵌入式桌面系统的软件)都选用了它作为图形界面的开发工具。关于它的详细情况参见作者的另一篇文章《FLTK---一个优秀的图形界面开发工具包》。在FLTK里有一个组件:Fl_Gl_Window是专门的OpenGL窗口,利用它开发OpenGL程序相当方便。    最后要提的是GTK和KDE,它们是目前在Linux下用的最多的开发工具。GTK本身并不直接支持OpenGL(新的版本是否支持,尚不太清楚),但有人开发了支持OpenGL的Widget,叫做GLAREA,需要的读者可以到网上去查找或者与本文作者联系。KDE提供了对OpenGL的支持,但它的缺陷之一是KDE只运行于Linux系统,不具有可移植性。在这里,我将主要向大家介绍前面两个工具包。    2. 用GLUT开发OpenGL程序    2.1 如何获得    GLUT可以从Mesa中获得,读者也可以直接到它的主页去下载它:                      /employees/mjk_asd/glut3/glut3.html。按照说明安装后在OpenGL的头文件GL目录下将会有GLUT的头文件glut.h,同时安装的还有库文件libglut.a或libglut.so。有了它们以后,就可以用GLUT来编程了。    2.2 一个简单的例子  下面,我们先看一个简单的例子。这个例子画一个立体的球。  /* light.c  此程序利用GLUT绘制一个OpenGL窗口,并显示一个加以光照的球。  */  /* 由于头文件glut.h中已经包含了头文件gl.h和glu.h,所以只需要include 此文件*/  # include&  # include&    /* 初始化材料属性、光源属性、光照模型,打开深度缓冲区 */  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&&2 )  {  glBegin( GL_LINES );  glVertex2fv( contours[contour_cnt].p[0] );  glVertex2fv( contours[contour_cnt].p[point_cnt-1] );  contours[contour_cnt].p[point_cnt][0] = -1;  glEnd();  glFinish();  contour_cnt++;  contours[contour_cnt].point_cnt = 0;  }  }    /* 处理鼠标响应的函数,根据按键的类型调用不同的函数:左键和中键。 */  void mouse_clicked( int button, int state, int x, int y )  {  /* 将OpenGL的像素坐标换为背景的网格坐标,背景网格为边长为10的小正方形 */  x -= x%10;  y -= y%10;  switch ( button ) {  case GLUT_LEFT_BUTTON: /* GLUT发现左键被点击 */  if ( state == GLUT_DOWN ) {  left_down( x, y );  }    case GLUT_MIDDLE_BUTTON: /* 中键被点击 */  if ( state == GLUT_DOWN ) {  middle_down( x, y );  }    }  }  /* OpenGL绘制函数,有两种模式 */  void display( void )  {  GLuint i,j;  GLuint point_    glClear( GL_COLOR_BUFFER_BIT );  switch ( mode )  {  case DEFINE: /* 多边形定义阶段 */  /* 绘制网格,单个网格大小为10像素,网格数目取决于OpenGL窗口大小 */  glColor3f ( 0.6, 0.5, 0.5 );  glBegin ( GL_LINES );  for ( i = 0 ; i show();  // 进入消息循环  return Fl::run();  }  我们编译、连接并执行程序,就可以得到如图五所示的结果。从上面的例子我们可以看出使用FLTK编写OpenGL程序的一些优点,和GLUT它结构清晰,使用方便,而且它和GLUT是兼容的。除了glutInit()、glutMainLoop()等少数函数外,大部分GLUT函数可以在FLTK中使用。FLTK本身也提供了许多OpenGL函数,如绘制字符串的gl_draw()等。    4. 结束语      熟悉掌握了Linux下OpenGL的开发环境距离开发OpenGL程序还有很大的距离,毕竟问题的难点是如何很好的使用OpenGL的API。本文为即将在Linux下开发OpenGL的读者作一些铺垫和准备工作,希望并相信对大家有所帮助。

我要回帖

更多关于 linux移植windows 的文章

 

随机推荐