天天看點

(Ubuntu)安裝OpenGL/Glut庫

sudo apt-get install freeglut3 freeglut3-dev

如果Ubuntu的版本在11.10或者更高的話還需要安裝下面的包以解決連接配接問題 // For newer versions of Ubuntu(>=11.10) you have to install another package because the linker does not link anymore. 

sudo apt-get install binutils-gold

1. 編輯 color_cube.cpp

1: // Name     : OpenGL Color Cube      
2: // Author   : Terrence Ma      
3: // Email    : [email protected]      
4: // Web      : http://www.terrence.com      
5: // Date     : 10/25/2001      
6: // Modified : Tutorial sample from Mesa3d.org (http://www.mesa3d.org)      
7:        
8: /*      
9:  * Copyright (c) 1993-1997, Silicon Graphics, Inc.      
10:  * ALL RIGHTS RESERVED       
11:  * Permission to use, copy, modify, and distribute this software for       
12:  * any purpose and without fee is hereby granted, provided that the above      
13:  * copyright notice appear in all copies and that both the copyright notice      
14:  * and this permission notice appear in supporting documentation, and that       
15:  * the name of Silicon Graphics, Inc. not be used in advertising      
16:  * or publicity pertaining to distribution of the software without specific,      
17:  * written prior permission.       
18:  *      
19:  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"      
20:  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,      
21:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR      
22:  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON      
23:  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,      
24:  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY      
25:  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,      
26:  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF      
27:  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN      
28:  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON      
29:  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE      
30:  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.      
31:  *       
32:  * US Government Users Restricted Rights       
33:  * Use, duplication, or disclosure by the Government is subject to      
34:  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph      
35:  * (c)(1)(ii) of the Rights in Technical Data and Computer Software      
36:  * clause at DFARS 252.227-7013 and/or in similar or successor      
37:  * clauses in the FAR or the DOD or NASA FAR Supplement.      
38:  * Unpublished-- rights reserved under the copyright laws of the      
39:  * United States.  Contractor/manufacturer is Silicon Graphics,      
40:  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.      
41:  *      
42:  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.      
43:  */      
44:        
45: /*      
46:  *  aapoly.c      
47:  *  This program draws filled polygons with antialiased      
48:  *  edges.  The special GL_SRC_ALPHA_SATURATE blending       
49:  *  function is used.      
50:  *  Pressing the 't' key turns the antialiasing on and off.      
51:  */      
52: #include <GL/glut.h>      
53: #include <stdlib.h>      
54:        
55: GLboolean polySmooth = GL_TRUE;      
56:        
57: static void init(void)      
58: {      
59:     glCullFace (GL_BACK);      
60:     glEnable (GL_CULL_FACE);      
61:     glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);      
62:     glClearColor (0.0, 0.0, 0.0, 0.0);      
63: }      
64:        
65: #define NFACE 6      
66: #define NVERT 8      
67: void drawCube(GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1,      
68:         GLdouble z0, GLdouble z1)      
69: {      
70:    static GLfloat v[8][3];      
71:    static GLfloat c[8][4] = {      
72:       {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},      
73:       {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},      
74:       {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},      
75:       {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}      
76:    };      
77:        
78: /*  indices of front, top, left, bottom, right, back faces  */      
79:    static GLubyte indices[NFACE][4] = {      
80:       {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},      
81:       {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}      
82:    };      
83:        
84:    v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;      
85:    v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;      
86:    v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;      
87:    v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;      
88:    v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;      
89:    v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;      
90:        
91: #ifdef GL_VERSION_1_1      
92:    glEnableClientState (GL_VERTEX_ARRAY);      
93:    glEnableClientState (GL_COLOR_ARRAY);      
94:    glVertexPointer (3, GL_FLOAT, 0, v);      
95:    glColorPointer (4, GL_FLOAT, 0, c);      
96:    glDrawElements (GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);      
97:    glDisableClientState (GL_VERTEX_ARRAY);      
98:    glDisableClientState (GL_COLOR_ARRAY);      
99: #else      
100:    printf ("If this is GL Version 1.0, ");      
101:    printf ("vertex arrays are not supported.\n");      
102:    exit(1);      
103: #endif      
104: }      
105:        
106: /*  Note:  polygons must be drawn from front to back      
107:  *  for proper blending.      
108:  */      
109: void display(void)      
110: {      
111:    if (polySmooth) {      
112:       glClear (GL_COLOR_BUFFER_BIT);      
113:       glEnable (GL_BLEND);      
114:       glEnable (GL_POLYGON_SMOOTH);      
115:       glDisable (GL_DEPTH_TEST);      
116:    }      
117:    else {       
118:       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);      
119:       glDisable (GL_BLEND);      
120:       glDisable (GL_POLYGON_SMOOTH);      
121:       glEnable (GL_DEPTH_TEST);      
122:    }      
123:        
124:    glPushMatrix ();      
125:       glTranslatef (0.0, 0.0, -8.0);          
126:       glRotatef (30.0, 1.0, 0.0, 0.0);      
127:       glRotatef (60.0, 0.0, 1.0, 0.0);       
128:       drawCube(-0.8, 0.8, -0.8, 0.8, -0.8, 0.8);      
129:    glPopMatrix ();      
130:        
131:    glFlush ();      
132: }      
133:        
134: void reshape(int w, int h)      
135: {      
136:    glViewport(0, 0, (GLsizei) w, (GLsizei) h);      
137:    glMatrixMode(GL_PROJECTION);      
138:    glLoadIdentity();      
139:    gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);      
140:    glMatrixMode(GL_MODELVIEW);      
141:    glLoadIdentity();      
142: }      
143:        
144: /* ARGSUSED1 */      
145: void keyboard(unsigned char key, int x, int y)      
146: {      
147:    switch (key) {      
148:       case 't':      
149:       case 'T':      
150:          polySmooth = !polySmooth;      
151:          glutPostRedisplay();      
152:          break;      
153:       case 27:      
154:          exit(0);  /*  Escape key  */      
155:          break;      
156:       default:      
157:          break;      
158:    }      
159: }      
160:        
161: /*  Main Loop      
162:  */      
163: int main(int argc, char** argv)      
164: {      
165:    glutInit(&argc, argv);      
166:    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB       
167:                         | GLUT_ALPHA | GLUT_DEPTH);      
168:    glutInitWindowSize(400, 400);      
169:    glutCreateWindow("OpenGL Color Cube");      
170:    init ();      
171:    glutReshapeFunc (reshape);      
172:    glutKeyboardFunc (keyboard);      
173:    glutDisplayFunc (display);      
174:    glutMainLoop();      
175:    return 0;      
176: }      
2. 通過連結OpenGL/Glut庫來編譯 color_cube.cpp       

g++ -lGL -lglut color_cube.cpp -o test

3. 運作結果如下

(Ubuntu)安裝OpenGL/Glut庫

繼續閱讀