OpenGL development in Linux-preparation

Source: Internet
Author: User
Environment is Fedora 7
First, install the OpenGL development environment. Currently, there are many OpenGL implementations in Linux, and Mesa is included in Fedora 7, which is an open source implementation of OpenGL. If you select the development environment when installing fedora, mesa may have been installed. If you do not select development when installing Mesa, you can install Mesa online through Yum:
First, find the Mesa-related packages:
Yum list Mesa *
The returned result should be the lib and Development Kit related to Mesa. Installed packages
Mesa-libGL.i386 6.5.2-13. fc7 installed
Mesa-libGL-devel.i386 6.5.2-13. fc7 installed
Mesa-libGLU.i386 6.5.2-13. fc7 installed
Mesa-libGLU-devel.i386 6.5.2-13. fc7 installed
Mesa-libGLw.i386 6.5.1-2. fc7 installed
Mesa-libGLw-devel.i386 6.5.1-2. fc7 installed
Mesa-libOSMesa.i386 6.5.2-13. fc7 installed
Mesa-libOSMesa-devel.i386 6.5.2-13. fc7 installed
Mesa-source.i386 6.5.2-13. fc7 installed

Then install these packages:
Yum install Mesa *
After the installation is complete, we have a basic OpenGL development environment.

However, the basic development environment is not enough. In general, we also need to install a library called glut, which is an auxiliary library of OpenGL and allows us to create OpenGL windows unrelated to the platform. Therefore, installation of glut is also necessary. Glut has many implementations. Here I use freeglut, which I found in fedore 7. Of course there are other implementations.
The command for searching the glut library is as follows:
Yum list * glut *
The returned result is loading "installonlyn" plugin.
Installed packages
Freeglut. i386 2.4.0-11. fc7 installed
Freeglut-devel.i386 2.4.0-11. fc7 installed
Available packages
Hugs98-glut.i386 2006.09-3. fc7 Fedora

Then you can select your preferred library from the results for installation. I chose freeglut and the installation command is
Yum install freeglut *

After the installation, we can start to write the OpenGL program under the first Linux.
The Code is as follows (this code is copied from the Internet ): 1/* Light. c
2. This program draws an OpenGL window using GLUT and displays a ball with illumination.
3 */
4/* because the header file glut. h contains the Gl. h and Glu. H, you only need to include this file */
5 # include <Gl/glut. h>
6 # include <stdlib. h>
7
8/* initialize material attributes, light source attributes, and illumination models, and open the depth buffer */
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/* call the glut function to draw a ball */
28 void display (void)
29 {
30 glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
31 maid (1.0, 40, 50 );
32 glflush ();
33}
34
35
36/* defines the reshape function of GLUT. W and H indicate the width and height of the current window respectively */
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/* define the keyboard response function */
52 void keyboard (unsigned char key, int X, int y)
53 {
54/* Press ESC to exit */
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/* initialize the glut environment */
67 gluinit (& argc, argv );
68/* Display Mode Initialization */
69 gluinitdisplaymode (glu_single | glu_rgb | glu_depth );
70/* define the window size */
71 gluinitwindowsize (300,300 );
72/* define the window position */
73 maid (100,100 );
74/* display window with the title of execution function */
75 glucreatewindow (argv [0]);
76/* Call OpenGL initialization function */
77 Init ();
78/* register the OpenGL plot function */
79 gludisplayfunc (Display );
80/* response function when the size of the registration window changes */
81 glureshapefunc (reshape );
82/* register the keyboard response function */
83 glukeyboardfunc (keyboard );
84/* enter the glut message loop and start executing the Program */
85 glumainloop ();
86 return 0;
87}

Compile and run: gcc-lglut-o light. c
./Light

The running effect is as follows:

So far, the first OpenGL program in Linux was born ..
Enjoy it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.