OpenGL learning-------Visual Studio 2010 configuration and the first OpenGL program explained

Source: Internet
Author: User
Tags visual studio 2010

OpenGL, one of the most popular graphics APIs, has features that are superior to DirectX in some situations.

1. Close integration with C language.

OpenGL commands are initially described in C language functions, and OpenGL is easy to understand and learn for those who have studied C language. If you have ever contacted TC Graphics.h, you will find that using OpenGL is even simpler than TC.

2, the powerful portability.

Microsoft's Direct3D is also a very good graphics API, but it is only for Windows systems (now add an Xbox console). OpenGL is not only used for Windows, but also for other systems such as Unix/linux, and it is used even in mainframe computers and various professional machines, such as medical display devices. Also, OpenGL's basic commands are hardware-independent, even platform-agnostic.

3, high-performance graphics rendering.

OpenGL is an industry standard, its technology keep up with the times, today, the graphics card manufacturers do not have a strong support for OpenGL, the fierce competition has led to OpenGL performance has been leading.

In short, OpenGL is a very NB graphics software interface. As for the number of NB, to see DOOM3 and QUAKE4 and other professional games will know.

OpenGL official website (US site)

http://www.opengl.org

The following is a brief introduction to OpenGL programming under Windows.

Preparation before learning OpenGL

The first step is to select a compilation environment

Now the main Windows System compiler environment has visual Studio,broland C + + builder,dev-c++, and so on, they all support OpenGL. But here we choose Visual Studio 2005 as the environment for learning OpenGL.

Step two, install the Glut toolkit

Glut is not necessary for OpenGL, but it will give us a certain convenience in learning, recommended installation.

Glut in the Windows environment: (approximately 150k in size)

Http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

If you cannot download from the above address, please use the following connection:

Http://upload.programfan.com/upfile/200607311626279.zip

Steps to install glut in a Windows environment:

1, will download the compressed package to untie, will get 5 files

2. Search for "gl.h" in "My Computer" and locate 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 into this folder.

3. Put the extracted glut.lib and glut32.lib into the folder where the static function library is located (if it is VisualStudio2005, it should be the "Vc\lib" folder under its installation directory).

4. Put the extracted glut.dll and Glut32.dll in the System32 folder under the operating system directory. (Typical position: C:\Windows\System32)

Step three, build an OpenGL project

Here take VisualStudio2005 as an example.

Select File->new->project, then select Win32 Console Application, select a name, then press OK.

On the left side of the dialog box, click Application Settings, find the empty project and tick on it and select Finish.

Then add a code file to the project named "OPENGL.C" and pay attention to using. C as the end of the file.

Done, it is no different from the usual project.

First OpenGL Program

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

#include <GL/glut.h>voidMydisplay (void) {glclear (gl_color_buffer_bit);//clear. Gl_color_buffer_bit means clear color, glclear function can also clear other things, but this is not introduced here. GLRECTF (-0.5f, -0.5f,0.5f,0.5f);//draw a rectangle. The four parameters represent the horizontal and vertical coordinates of two points located on the diagonal. Glflush ();//ensure that the previous OpenGL commands execute immediately (instead of having them wait in the buffer). The effect is similar to Fflush (stdout). }intMainintargcChar*argv[]) {Glutinit (&AMP;ARGC, argv);//to initialize the glut, this function must be called once before other glut are used. Its format is more rigid, generally copy this sentence glutinit (&ARGC, argv) on the can. Glutinitdisplaymode (Glut_rgb| Glut_single);//sets the display mode, where GLUT_RGB indicates the use of RGB colors, and also Glut_index (which indicates the use of indexed colors). //The Glut_single represents the use of a single buffer, which corresponds to a glut_double (using double buffering). For more information, please Google yourself. Of course, later tutorials will also have some explanation. Glutinitwindowposition ( -, -);//this simple, set the position of the window in the screen. Glutinitwindowsize ( -, -);//This is also simple, set the size of the windowGlutcreatewindow ("First OpenGL program");//creates a window based on the information previously set. The parameter is used as the caption of the window. Note: When the window is created, it is not immediately displayed to the screen. You need to call Glutmainloop to see the windowGlutdisplayfunc (&mydisplay);//set a function that will be called when a drawing is required. (This statement is not accurate enough, but the exact statement may not be very good for beginners to understand, for the time being said). Glutmainloop ();//make a message loop. (This may not be easy for beginners to understand, but it's enough to know that the function can show the window and wait for the window to close before it returns.) )     return 0;}

The function of the program is to draw a white rectangle in the center of a black window. Each line statement is described below.

First, you need to include the header file # include <gl/glut.h> this is the glut header file.

Originally OpenGL program will also include <GL/gl.h> and <gl/glu.h>, but the GLUT header file has automatically included these two files, do not have to include again.

Then look at the main function.

int main (int argc, char *argv[]), this is the main function with command line arguments, you should have seen it? Comrades who have not seen please turn over the book, and so on to understand and then look down.

Note that the statements in the main function, except for the last return, all begin with glut. This function, which starts with glut, is the function provided by the GLUT Toolkit, which describes several functions that are used.

In the Glutdisplayfunc function, we set the "Call Mydisplay function when drawing is required." So the Mydisplay function is used to draw. Observe the three function calls in Mydisplay and find that they all start with GL. This function, which begins with GL, is the standard function of OpenGL, and the functions used are described below.

OpenGL learning-------Visual Studio 2010 configuration and the first OpenGL program explained

Related Article

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.