OpenGL programming in Linux

Source: Internet
Author: User
Tags gtk

OpenGL is an industrial standard 3D computer graphics software interface. It is released by SGI and widely used on UNIX, OS/2, Windows/NT, and other platforms, including Linux. On Windows/NT platforms, general development tools such as Vc, BC, and Fortran PowerStation support the development of direct OpenGL applications. on commercial UNIX platforms, motif also supports OpenGL (after all, OpenGL was originally a workstation). What about Linux?
This article focuses not on OpenGL programming methods and techniques, but on how to develop OpenGL programs on the Linux platform. This section describes several tools that support OpenGL, supplemented by detailed examples.

1. Introduction to the OpenGL programming environment in Linux

OpenGL is not a free software. Its copyright and trademark (OpenGL) are all owned by SGI. But in Linux, there is a product replaced by OpenGL: Mesa. Mesa provides almost identical interfaces with OpenGL. For openglapi programmers, there is almost no difference. Mesa is a free software that complies with the GPL protocol (partially follows the lgpl Protocol). It is precisely because of Mesa's freedom that it outperforms OpenGL in terms of new hardware support. Mesa can be obtained from www.mesa3d.org. After obtaining Mesa, follow the instructions to generate the dynamic and static Connection Library and header files required for programming.
Anyone who knows about OpenGL knows that OpenGL only provides 3D graphic interfaces and does not have functions such as drawing windows, receiving responses, and processing messages. These functions must be provided by a third-party development environment, such as the VC mentioned above. Some people may think that since OpenGL can be developed in motif, it should also be possible to use le tif in Linux. Yes, indeed, but unfortunately, le TIF in Linux is a very immature product and is not portable, so there are very few developers who use le tif. Below we will briefly introduce several commonly used toolkit.
The most common tool for developing OpenGL programs in Linux is glut (theopenglutility Toolkit ). It can create one or more OpenGL windows, respond to and process user interaction operations, simple pop-up menus, and some built-in drawing and Font Processing functions. Like OpenGL, Glut can be transplanted to multiple platforms. Thanks to its outstanding performance, it has now become one of the standard suites released by Mesa.
Another good development kit is fltk (fast light tool kit), a graphical interface development tool written in C. Unlike GTK and KDE, Kde focuses only on the graphic interface design and does not involve other practical applications. This feature makes it more concise and efficient than many other development tools. It is also a development tool with good portability. In fact, it is attracting more and more people's interest. Many commercial software (especially the software dedicated to developing embedded desktop systems) have chosen it as a graphical interface development tool. For more information, see the author's article fltk-an excellent graphic interface development kit. There is a component in fltk: fl_gl_window is a specialized OpenGL window. It is quite convenient to use it to develop OpenGL programs.
GTK and KDE are the most widely used development tools in Linux. GTK itself does not directly support OpenGL (whether the new version supports OpenGL is not clear yet), but some people have developed widgets that support OpenGL, called glarea, you can search online or contact the author of this article. KDE provides support for OpenGL, but one of its drawbacks is that KDE only runs on Linux and is not portable. Here, I will mainly introduce the first two toolkit.

2. Use glut to develop OpenGL programs

2.1 How to obtain
GLUT can be obtained from Mesa. You can also download it from its homepage: http://reality.sgi.com/employees/mjk_asd/glu3/glu3.html. After installation, the glut header file glut. H will be installed under the GL directory of the OpenGL header file, and the library file libglut. A or libglut. So will also be installed. With them, you can use glut for programming.

2.2 A simple example
Next, let's take a look at a simple example. In this example, draw a three-dimensional ball.
/* Light. c
This program draws an OpenGL window using GLUT and displays a ball for illumination.
*/
/* Because the header file glut. h contains the Gl. h and Glu. H, you only need to include this file */
# Include <Gl/glut. h>
# Include <stdlib. h>

/* Initialize the material attributes, light source attributes, and illumination model, and open the depth buffer */
Void Init (void)
{
Glfloat mat _ ecular [] = {1.0, 1.0, 1.0, 1.0 };
Glfloat mat_shinine [] ={ 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 _ ecular, mat _ ecular );
Glmaterialfv (gl_front, gl_shinine, mat_shinine );
Gllightfv (gl_light0, gl_position, light_position );

Glable (gl_lighting );
Glable (gl_light0 );
Glenable (gl_depth_test );
}
/* Call the glut function to draw a ball */
Void di lay (void)
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
Glusolid here (1.0, 40, 50 );
Glflush ();
}
/* Defines the reshape function of GLUT. W and H indicate the width and height of the current window respectively */
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 ();
}

/* Define the keyboard response function */
Void keyboard (U igned char key, int X, int y)
{
/* Press ESC to exit */
Switch (key ){
Case 27:
Exit (0 );
Break;
}
}

Int main (INT argc, char ** argv)
{
/* Initialize the glut environment */
Gluinit (& AM argc, argv );
/* Display Mode Initialization */
Fig );
/* Define the window size */
Gluinitwindowsize (300,300 );
/* Define the window position */
Gluinitwindowposition (100,100 );
/* Display window with the title of execution function */
Ngcreatewindow (argv [0]);
/* Call the OpenGL initialization function */
Init ();
/* Register the OpenGL plot function */
Gludi layfunc (di lay );
/* Response function when the size of the registration window changes */
Glureshapefunc (reshape );
/* Register the keyboard response function */
Glukeyboardfunc (keyboard );
/* Enter the glut message loop and start executing the Program */
Glumainloop ();
Return 0;
}
From the above example, we can see that glut uses a function registration mechanism to implement OpenGL plotting. Its general process is as written in the preceding annotations. It first initializes the function, defines the window, and then executes the OpenGL initialization program. This is mainly the environment variables that need to be set globally. The next step is to register the corresponding event functions, including the rendering program that completes the actual drawing work, the response function that changes the size of the OpenGL window, the keyboard event response function, and the mouse time response function. Finally, call the glumainloop () function, execute the function registered in the glureshapefunc and the gludi layfunc, and enter the message loop. When a user uses a keyboard to interact with the mouse, it calls the corresponding function.
Compile the source file named light. C above. Assume that the header file (directory GL) is stored in the directory/usr/local/include, and the library file (dynamic library libgl. so. * libglu. so. * And libglut. so. *) in the/usr/local/lib directory, and ldconfig has been run, the compilation command is:
Gcc-I/usr/local/include-L/usr/local/lib-L/usr/x11r6/lib-lglut-lglu-lgl
-Lx11-lxext-lxmu-LXI-LM light. C-o light
-Lx11-lxert-LXI-LM is the Library of X required for drawing the window. They are in the/usr/x11r6/lib directory by default. Figure 1 below shows the result of running light. When the ESC key is pressed, the program exits. Adjust the window size and the image is automatically re-painted. Note that in the above reshape function, it is a common technique to compare the scene conversion given by W and H values.

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.