Getting Started with OpenGL and sample programs

Source: Internet
Author: User

Hello everyone, today we will introduce some of OpenGL's basic syntax and a simple example program.

What is OpenGL? To put it simply, it's a graphics package that provides a number of functions for us to invoke. Let's see what Wikipedia says.

Open Graphics Library (English: The Open Graphics Library, abbreviated to OpenGL) is a specification that defines a cross-programming language, cross-platform Application interface (API), which is used to generate two-dimensional, three-dimensional images. This interface consists of nearly 350 different function calls, which are used to draw complex three-dimensional images from simple graph bits. The other program interface system is only for Direct3D on Microsoft Windows. OpenGL is commonly used in CAD, virtual reality, scientific visualization programs, and video game development.

Because different platforms treat graphics differently, they need to be treated separately when programming. But with this normalized OpenGL, we can easily write a cross-platform graphical application. And it is easy to use the functions provided by OpenGL directly, simplifying the development process. The current OpenGL regulatory managers, including Sgi,microsoft,intel,ibm,sun and other well-known companies, have become the actual industry standards. Similar to Microsoft's Direct X and Adobe's PostScript.

OpenGL's drawing process

OpenGL is located between the operating system and application software, providing support for software development.

The level of OpenGL in a computer system

The drawing process for OpenGL is as shown. The software calls OpenGL to provide the API functions, OpenGL functions to draw graphics commands to the command buffer, the command buffer data will be a number of transformations, lighting, projection, the resulting data into the rasterization process, and finally, the rasterized result is sent to the frame cache, the image is displayed on the screen.

A simple OpenGL drawing flowchart

Note: The command and data for the buffer will not be passed to the next stage until the command buffer is emptied, and the OpenGL command will execute.

OpenGL Library of functions

OpenGL is mainly composed of the following library of functions.

    1. OpenGL Core Library. A GL-prefixed support for all OpenGL platforms. Main features: general and core graphics processing.
    2. The OpenGL utility Library, prefixed with Glu, supports all OpenGL platforms. Key Features: Complex functions, such as texture mapping, coordinate transformations, are implemented by invoking the core library.
    3. OpenGL Programming Auxiliary Library, prefixed with aux, does not support all platforms. Mainly used for window management, input and output and simple three-dimensional shape drawing.
    4. The OpenGL Utility Toolkit (GLUT), prefixed with GLUT, supports all platforms. Mainly provides window-based tools, such as creating windows, menus, etc.
    5. Windows dedicated library. WGL prefix.
The law of function naming

General functions follow the < library prefix >< command >< number of parameters >< parameter type > rules.

Some symbolic constants begin with GL.

Data type
OpenGL data type Internal representation defined as Type C c literal suffix
Glbyte 8-bit integer Signed Char B
Glshort 16-bit integer Short S
Glint,glsizei 32-bit integer Long L
Glfloat,glclampf 32-bit floating point number Float F
Gldouble,glclampd 64-bit floating point number Double D
Glubyte,glboolean 8-bit unsigned integer unsigned char Ub
Glshort 16-bit unsigned integer unsigned short us
Gluint,glenum,glbitfield 32-bit unsigned integer unsigned long Ui
A complete OpenGL program

The construction of OpenG development environment can be referenced

VS2013 Express Configuration OpenGL

The following is a program that draws a rectangle.

1. Header files

OpenGL does not include a windowing system, so it is introduced in the header file. If you are under Windows, you can introduce

C<textarea class="crayon-plain print-no" style="OVERFLOW: hidden; FONT-SIZE: 12px !important; ZOOM: 1; FILTER: alpha(opacity=0); Z-INDEX: 0; LINE-HEIGHT: 15px !important; tab-size: 4; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4" readonly="" data-settings="dblclick" jquery11110918721986960388="14">#include <windows.h> #include <gl/gl.h> #include <gl/glu.h></textarea>
123 #include <windows.h> #include <gl/gl.h> #include <gl/glu.h>

If you use glut for window management, you can include glut.h directly, Glut.h ensures that gl.h and Glu.h are included.

2. Window Management

Using glut first requires initialization and processing of command-line arguments.

Glutinit (&ARGC, argv);

You can then create the window, but also set some window styles before creating the window.

C<textarea class="crayon-plain print-no" style="OVERFLOW: hidden; FONT-SIZE: 12px !important; ZOOM: 1; FILTER: alpha(opacity=0); Z-INDEX: 0; LINE-HEIGHT: 15px !important; tab-size: 4; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4" readonly="" data-settings="dblclick" jquery11110918721986960388="41">Glutinitdisplaymode (Glut_single | GLUT_RGB); Refers to the order cache using the RGB color model glutinitwindowsize (400, 300); Window size glutinitwindowposition (100, 120); The top left corner of the window starts with 0,0</textarea>
123 Glutinitdisplaymode(Glut_single| Glut_rgb);//Specify single cache using RGB color modelglutinitwindowsize( -, -);//Window sizeglutinitwindowposition( -, -);//window start position upper left corner is 0,0

If the window size and position is not set, it is the default setting

You can create a window after you set the style

Glutcreatewindow ("My first OpenGL demo");

The parameter of the function is the caption of the window.

After creating the window, you need to specify what the window will display. Usually the content to be displayed is written in a display callback function.

Glutdisplayfunc (Display);

The display function is the function that displays the contents of the current window. The display function is called every time the screen is refreshed and the window is redrawn.

The last step is required, calling the function Glutmainloop (); Let the window run and let the callback function work.

3. OpenGL Drawing

You can initialize some of the window's settings before drawing.

Glclearcolor (1.0f, 1.0f, 1.0f, 1.0f);

Specifies that the window background color is white. The four parameters are red-green-blue and alpha-component (transparent). Because this program is not mixed, use opaque.

After you set the background color, you can use the function glclear (gl_color_buffer_bit) and apply the background color to the window.

Similarly, you can use Glcolor to set the drawing color.

Because OpenGL handles graphics in a three-dimensional way, you need to set projections to two-dimensional

Glmatrixmode (gl_projection);
gluortho2d (0.0, 200.0, 0.0, 150.0);

The two functions use a positive projection to project the area within the horizontal axis 0-200 ordinate 0-150 to the window.

Then you can draw the rectangle.

GLRECTF (50.0f, 100.0f, 150.0f, 50.0f);

Draw a rectangle at the top left corner of 50,100 150 50 in the lower right corner.

Finally in the callback function, you need to use Glflush (); the function empties the command cache, forcing all commands in the command buffer,

We are generally used to write some functions that are only performed once into an initialization function, and write the drawing function in the callback function.

The full source code

C<textarea class="crayon-plain print-no" style="OVERFLOW: hidden; FONT-SIZE: 12px !important; ZOOM: 1; FILTER: alpha(opacity=0); Z-INDEX: 0; LINE-HEIGHT: 15px !important; tab-size: 4; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4" readonly="" data-settings="dblclick" jquery11110918721986960388="68">#include <gl/glut.h>void Initial (void) {Glclearcolor (1.0f, 1.0f, 1.0f, 1.0f); Glmatrixmode (gl_projection); gluortho2d (0.0, 200.0, 0.0, 150.0);} void Display (void) {glclear (gl_color_buffer_bit); glcolor3f (1.0f, 0.0f, 0.0f); GLRECTF (50.0f, 100.0f, 150.0f, 50.0f); Glflush ();} int main (int argc, char * argv[]) {glutinit (&ARGC, argv); Glutinitdisplaymode (Glut_single | GLUT_RGB); Glutinitwindowsize (+); glutinitwindowposition (+); Glutcreatewindow ("My first OpenGL demo"); Glutdisplayfunc (Display); Initial (); Glutmainloop (); return 0;}</textarea>
123456789101112131415161718192021222324252627282930 #include <gl/glut.h>voidInitial(void){Glclearcolor(1.0f,1.0f,1.0f,1.0f);Glmatrixmode(gl_projection);gluortho2d(0.0,200.0,0.0,150.0);}void Display(void){Glclear(Gl_color_buffer_bit);glcolor3f(1.0f,0.0f,0.0f);GLRECTF(50.0f,100.0f,150.0f,50.0f);Glflush();}int Main(intargc,Char *argv[]){Glutinit(&argc,argv);Glutinitdisplaymode(Glut_single| Glut_rgb);glutinitwindowsize( -, -);glutinitwindowposition( -, -);Glutcreatewindow("My First OpenGL Demo");Glutdisplayfunc(Display);Initial();Glutmainloop();return0;}

Test program is running normally

If your notebook does not display graphics, it may be because of problems with dual graphics, you can refer to

OpenGL Draw rectangle does not show rectangle display

Getting Started with OpenGL and sample programs

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.