OpenGL basic graphics programming-OpenGL BASIC program structure

Source: Internet
Author: User
Tags microsoft c
OpenGL basic graphics programming-OpenGL BASIC program structure
Author: Unknown Article Source: game developers in China hits: 1043 updated:

The program structure compiled with OpenGL is similar to the program written in other languages. In fact, OpenGL is a rich library of 3D graphics functions. It is not difficult to compile OpenGL programs. You only need to call these functions in basic C language. The usage is similar to that of Turbo C and Microsoft C, but there are also many differences.
All programs in this Guide are compiled and connected in the Microsoft Visual C ++ integrated environment of Windows NT. Some header files and functions are used in this environment, for example, identify the header file "Glos. H ". In addition, in order to help readers get started quickly and master the basic methods and skills of OpenGL programming in a short time, the examples in the Guide should be written using standard ansi c to call OpenGL functions, all routines only use the window system in the auxiliary library attached to OpenGL. In addition, this also facilitates program transplantation between platforms, especially when porting to the UNIX operating system of the workstation, you only need to change the header file and a few other parts. A simple OpenGL program is listed below:

  Example 4-1 simple OpenGL routine(Simple. c)

# Include <Gl/Gl. h>
# Include <Gl/Glaux. h>
# Include "Glos. H"

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0, 500,500 );
Auxinitwindow ("simple ");

Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Glclear (gl_color_buffer_bit );

Glcolor3f (1.0, 0.0, 0.0 );
Glrectf (-0.5,-0.5, 0.5, 0.5 );

Glflush ();
_ Sleep (1000 );
}

The program running result is to draw a Red Square in the screen window.
The following is a detailed analysis of the entire program structure: first, the OpenGL header file is used at the beginning of the program: <Gl/Gl. h> and <Gl/Glaux. h>. The first is the header file of the GL library, and the second is the header file of the auxiliary library. In addition, the other two OpenGL header files, <Gl/Glu. h> the header file of the utility library, and the other is <Gl/Glx. h> X Window extension library header file (this is often used on workstation ). Next, the main function main () is defined. The general program structure is to define a window first:

Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0, 500,500 );
Auxinitwindow ("simple ");

Auxinitdisplaymode (aux_single | aux_rgba) sets the window display mode to rgba, that is, the color mode, and the image cache is a single buffer ). Auxinitposition (0, 0,500,500) defines the initial position of the window. The first two parameters (0, 0) are the screen coordinates of the upper left corner of the window, and the last two parameters (500,500) the window width and height. Auxinitwindow ("simple") is the window initialization, and the character parameter is the window name.
Then, clear the screen in the window:

Glclearcolor (0.0, 0.0, 0.0, 0.0); glclear (gl_color_buffer_bit );

The first sentence is to clear the window as black, and the second sentence is to clear the color buffer as the color set by the glclearcolor (0.0, 0.0, 0.0, 0.0) command, that is, the same as the background color of the window.
Next, draw an object in the window:

Glcolor3f (1.0, 0.0, 0.0 );
Glrectf (-0.5,-0.5, 0.5, 0.5 );

Obviously, the first sentence sets the object color. The first three parameters in the function are R, G, and B respectively. The last parameter is the Alpha value, ranging from 0 to 1; draw a two-dimensional rectangle in the second sentence.Note:: OpenGL is used for 3D graphics. Therefore, when using OpenGL to program and draw objects, you must realize that any object is three-dimensional and spatial, the objects displayed on the screen are three-dimensional objects projected on a two-dimensional plane.
On the surface, the above program code is very simple. In fact, the default projection form (normal projection) has been used ). Then, let's look at the glflush () function, which indicates that the plotting is completed. The last sentence _ sleep (1000), the unit of the parameter is millisecond. The whole sentence means to keep the current status for one second, and then end the program running. This function is a library function of VC ++.
All in all, the basic structure of the OpenGL program is to define a window, clear a window, draw an object, and end the operation.

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.