Introduction to OpenGL from scratch

Source: Internet
Author: User

Recently, some special effects have been involved in projects, such as font stroke, scene color, and sprite coloring. In addition, we have been exposed to the OpenGL section and previously experienced unity3d development, I am more interested in OpenGL. I should say that I am interested in 3D. In fact, I have always been interested in the simulation technology and artificial intelligence of virtual reality. I hope I can enter these two fields in the future. If you don't talk much about this nonsense, you can start your OpenGL journey. This is something you have never touched before, and you may not understand it, or it's totally wrong. Please point it out. Thank you.

Currently, the most prominent 3D graphics programming tool is SGI's open Graphics Language (OpenGL), which has become an industrial standard computer 3D graphics software development interface. It is worth mentioning that, although Microsoft has its own 3D Programming Development Tool DirectX, it also provides OpenGL graphics standards. Therefore, OpenGL can be widely used in microcomputer. OpenGL is very close to hardware and is an interface between graphics and hardware. It includes more than 100 graphic functions used to create 3D models and perform 3D real-time interaction.

To put it simply, OpenGL provides functions such as modeling, transformation, color processing, light processing, texture ing, image processing, animation, and object motion blur. The specific content will be learned step by step.

To summarize the rendering process of OpenGL:

① Modeling based on basic elements (OpenGL uses "points, lines, polygon, images, and bitmaps" as basic graphics units ).

② Adjust the model position and set the view observation points.

③ Render colors and add illumination, material, and texture ing.

④ Light Stack: converts the mathematical description and color information of the model into pixels on the computer screen to complete rendering.

Library functions

To develop OpenGL-based applications, you must first understand the library functions of OpenGL. It adopts the C language style and provides a large number of functions for graphic processing and display.

Four types of function libraries are commonly used to provide the required functions: basic library, utility library, auxiliary library, and tool library. We need to remember which database some common functions come from, because the function prefixes of different databases are different.

1. Basic Database: core database

There are 115 included functions, providing the most basic functions with the prefix of "Gl". These functions are used for conventional and core graphics processing. DLL.

For example:

<1> draw-Basic-functions of geometric elements

Glbegain (), glend (), glnormal * (), glvertex *()

<2> matrix operation, geometric transformation, and Projection Transformation Functions

Matrix inbound Stack: glpushmatrix () location transformation: gltranslate * () Projection Transformation functions glortho (), glfrustum ()

Matrix output Stack: glpopmatrix () Rotation Transformation: glrotate * () The glviewport () function ()

Loading matrix: glloadmatrix () scaling transformation: glscale *()

Matrix Multiplication: glmultmatrix ()

Matrix mode: glmatrixmode ()

Matrix standardization: glloadidentity ()

<3> color, illumination, and material functions

Color: glcolor * (), glindex *()

Illumination: gllight * (), gllightmodel *()

Material: glmaterial *()

<4> display list Function

Create: glnewlist ()

End: glendlist ()

Generation: glgenlists ()

Call: glcalllist ()

Delete: gldeletelists ()

<5> Texture ing Function

One-dimensional texture: glteximage1d ()

Two-dimensional texture: glteximage2d ()

Set the texture parameter: gltexparameter *()

Texture environment: gltexenv *()

Texture coordinate: gltetcoord *()

<6> special effect Functions

Glblendfunc (), glhint (), and glfog *().

<7> grating and pixel operation functions

Pixel location: glrasterpos *()

Line width: gllinewidth ()

Polygon draw mode: glpolygonmode ()

Read pixel: glreadpixel ()

Copy pixel: glcopypixel ()

<8> curve and Surface Plotting Functions

The glmap * () and glmapgrid * () functions that generate curves or surfaces, and the glevalcoord * () glevalmesh * () function of the calculator *().

<9> Status settings and query Functions

Glget * (), glable (), glgeterror (), and so on.

 

2. Utility Library (OpenGL utility library, GLU)

The functions are more advanced, such as complex curve and surface drawing, advanced coordinate transformation, and polygon splitting. There are 43 functions with the prefix "Glu ".

By calling the functions of the core library, the Glu function provides developers with relatively simple usage and implements some complicated operations, namely, an encapsulation.

Common functions in the Library:

<1> auxiliary texture map function

Gluscaleimage (), glubuild1dmipmaps (), and glubuild2dmipmaps ().

<2> Coordinate Transformation and Projection Transformation Functions

Compared with the basic library, the transformation functions here are more advanced. My personal feelings are reflected in different dimensions, which are more complex than 3D functions.

Perspective Projection: gluperspective ()

Orthogonal projection: gluortho2d ()

3D viewpoint (observation point): glulookat ()

<3> quadratic surface rendering tool

Create and delete a Quadratic Surface: glunewquadric (), gludeletequadric ()

Sphere: glusphere ()

Cylindrical Surface: glucylinder ()

<4> error feedback tools

Returns the string gluerrorstring () of the error message.

 

3. OpenGL auxiliarylibrary (Glaux)

Including simple window management, input event processing, and some complex 3D object rendering functions. There are 31 functions prefixed with aux.

The aux library is created to learn and write OpenGL programs. It is more like a preliminary pipeline for testing creativity.

The aux library has many errors in Windows implementation, so it is easy to cause frequent crashes. In cross-platform programming examples and demonstrations, aux has been largely replaced by the glut library. The auxiliary libraries in OpenGL cannot run on all OpenGL platforms.

Therefore, we will not study the tool library that focuses on it.

4. OpenGL utility Toolkit)

Contains more than 30 functions with the prefix "glut". This function is interpreted and executed by glut. dll.

Common functions include:

<1> window operation functions

Window initialization: gluinit (), which initializes glut. This function must be called once before other gluts are used. The format is relatively rigid.Gluinit (& argc, argv)You can.

Window Display Mode: gluinitdisplaymode (). Set the display mode.

Window Size: gluinitwindowsize ().

Window Location: Maid ()

Window Creation: glucreatewindow ("window title"). Create a window based on the information set above. After the window is created, it is not immediately displayed on the screen. You need to call the glumainloop function to see the window.

Set the plotting function: gludisplayfunc (). Input a function for drawing to draw.

Enable the plotting cycle: glumainloop, endless loop, and listening for all event processing.

<2> callback function

Response to refresh messages, keyboard messages, mouse messages, timer functions, and so on ().

<3> create complex 3D objects

Solid ball: Maid ()

Mesh sphere: FIG ()

<4> menu functions

Create the functions for adding a menu, such as glucreatemenu (), glusetmenu (), gluaddmenuentry (), gluaddsubmenu (), and gluattachmenu ().

 

OpenGL Data Type

It is similar to the type in C, except that the prefix: Gl is added before the type name.

For example, glint integer, glfloat floating point, and glvoid empty type.

OpenGL function naming rules

Take the vertex Series function name as an example: glvertex * () Series

Glvertex2d

Glvertex2f

Glvertex3f

Glvertex3fv

A number indicates the number of parameters. 2 indicates two parameters. 3 indicates three parameters. 4 indicates four parameters.

Letter indicates the parameter type:

  SA short integer that represents a 16-bit integer (OpenGL defines this type as glshort ),

  IInteger, indicating a 32-bit integer (OpenGL defines this type as glint and glsizei ),

  FFloating point type, which indicates a 32-bit floating point number (OpenGL defines this type as glfloat and glclampf ),

  DDouble-precision floating point type, indicating 64-bit floating point numbers (OpenGL defines this type as gldouble and glclampd ).

  VArray type, indicating that several parameters passed will use the pointer method.

Glfloat vertexarr3 [] = {1.0f, 3.0f, 0.0f}; // these three parameters are passed as arrays.

Glvertex3fv (vertexarr3 );

 

Having said so much is a basic concept. In the next article, we will start to sort out each knowledge point and write a complete small example.

Introduction to OpenGL from scratch

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.