OpenGL (1)

Source: Internet
Author: User
When talking about programming and plotting, many people may think of TC # include <graphics. h>, right?
But have you ever wondered how those colorful PC games were written? Is it based on TC's poor 640*480 resolution and 16 colors? Obviously, this is not the case.

The purpose of this post is to give up TC's old graphic interface and let everyone get in touch with new things.

As one of the mainstream graphics APIs, OpenGL has superior features in some cases than DirectX.
1. closely integrated with C.
OpenGL commands are initially described using C language functions. For those who have learned C language, OpenGL is easy to understand and learn. If you have been familiar with graphics. h of TC, you will find that it is even easier to plot Using OpenGL than TC.
2. Powerful portability.
Microsoft's direct3d is also a very good graphic API, but it is only used in Windows systems (now an Xbox game machine is added ). OpenGL is not only used for windows, but also for Unix/Linux and other systems. It is applicable to large computers and various specialized computers (such as medical display devices. In addition, the basic commands of OpenGL are hardware-independent or even platform-independent.
3. High-Performance graphic rendering.
OpenGL is an industrial standard, and its technology keeps up with the times. Currently, all graphics card manufacturers provide strong support for OpenGL, and the fierce competition leads OpenGL Performance.
In short, OpenGL is a very Nb graphic software interface. As to whether there are many Nb games, check out professional games such as doom3 and quake4.
OpenGL official website (English)
Http://www.opengl.org

Next I will give a brief introduction to OpenGL programming in windows.

Preparations before learning OpenGL
Step 1: select a compiling environment
Currently, mainstream windows compiling environments include Visual Studio, broland C ++ builder, Dev-C ++, and so on. They all support OpenGL. However, Visual Studio 2005 is selected as the learning environment for OpenGL.
Step 2: Install the glut Toolkit
Glut is not required by OpenGL, but it will bring some convenience to our learning. We recommend that you install it.
Glut in Windows: (the size is about 150 K)
Http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
If you cannot download from the above address, use the following connection:
Http://upload.programfan.com/upfile/200607311626279.zip
To install glut in Windows:
1. Unpack the downloaded package and obtain 5 files.
2. Search for "Gl. h, and find the folder where it is located (if it is visualstudio2005, it should be the "Vc \ platformsdk \ include \ GL folder" under its installation directory "). Put the extracted glut. h in this folder.
3. Put the extracted glut. lib and glu32.lib in the folder where the static library is located (for visualstudio2005, it should be the "Vc \ Lib" folder under its installation directory ).
4. Put the extracted glut. dll and glu32.dll In the System32 folder under the operating system directory. (Typical location: c: \ windows \ system32)
Step 3: Create an OpenGL Project
The following uses visualstudio2005 as an example.
Select File-> New-> Project, select Win32 console application, select a name, and then press OK.
Click application settings on the left of the displayed dialog box, locate the empty project, and click Finish.
Add a code file named "OpenGL. c" to the project, and use. C as the end of the file.
It's just like the normal project.


The first OpenGL program

A simple OpenGL program is as follows: (Note that if you need to compile and run the program, you must install glut correctly. The installation method is described above)

# Include <Gl/glut. h>

Void mydisplay (void)
{
Glclear (gl_color_buffer_bit );
Glrectf (-0.5f,-0.5f, 0.5f, 0.5f );
Glflush ();
}

Int main (INT argc, char * argv [])
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowposition (100,100 );
Gluinitwindowsize (400,400 );
Valley createwindow ("first OpenGL program ");
Gludisplayfunc (& mydisplay );
Glumainloop ();
Return 0;
}

The function of this program is to draw a White Rectangle in the center of a black window. Each row of statements is described below.


How is it? The code is not long yet, right?

First, you need to include the header file # include <Gl/glut. h>, which is the header file of glut.
Originally, OpenGL programs generally contain <Gl/Gl. h> and <Gl/Glu. h>, but the glut header file automatically contains these two files and does not need to be included again.

Then look at the main function.
Int main (INT argc, char * argv []). This is a main function with command line parameters. Have you ever seen it? If you have never seen any other comrades, please read more books and wait until you understand them.
Note that all the statements in the main function, except the last return, start with glut. The functions starting with glut are all functions provided by the glut toolkit. The following describes several functions used.
1. gluinit: Initialize the glut. This function must be called once before other gluts are used. The format is relatively rigid. You can copy this keyword according to the format of gluinit (& argc, argv.
2. Set the display mode of the glutinitdisplaymode. In the display mode, the ng_rgb color indicates that the RGB color is used, and the corresponding value is the ng_index (indicating that the index color is used ). Glu_single indicates that a single buffer is used, and the corresponding value is glu_double (dual buffering is used ). For more information, Google. Of course, there will be some explanations in future tutorials.
3. Set the position of the window on the screen.
4. It is also simple to set the window size.
5. Create a window based on the information set above. The parameter is used as the title of the window. Note: The window is not displayed on the screen immediately after it is created. You need to call the glumainloop function to view the window.
6. Set a function to map a graph. The function is called. (This statement is not accurate enough, but it may be difficult for beginners to understand it. Let's talk about it for the time being ).
7. A message loop is implemented using the glumainloop. (This may not be quite understandable for beginners. Now you only need to know that this function can display a window and will return it only after the window is closed .)

In the gludisplayfunc function, we set "Call the mydisplay function when drawing is required ". The mydisplay function is used for drawing. Observe the three function calls in mydisplay and find that they all start with GL. These functions starting with GL are all OpenGL standard functions, which are described below.
1. glclear: Clear. Gl_color_buffer_bit indicates to clear the color. The glclear function can also clear other things, but it is not described here.
2. glrectf: Draw a rectangle. The four parameters represent the horizontal and vertical coordinates of the two points on the diagonal line.
3. glflush: ensure that the previous OpenGL commands are executed immediately (instead of waiting them in the buffer ). Its role is similar to that of fflush (stdout.

Now, the first lesson is over.

You can set the compiler, download glut, and compile the sample code as described above.
Wish you success ~

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.