Basic OpenGL graphics programming-basic use of OpenGL auxiliary Libraries

Source: Internet
Author: User
OpenGL is an open system independent of any window system or operating system. Although it contains many graphical functions, it does not have window functions or functions that read events from the keyboard or mouse. Therefore, it is quite difficult for beginners to write a complete graphic program. In addition, OpenGL graphic functions only provide the basic ry: points, lines, and polygon. Therefore, it is not easy to create basic 3D ry such as balls and cones. The OpenGL auxiliary library is specially designed to solve these basic problems. It provides some basic window management functions and 3D graphics rendering functions to help beginners enter the OpenGL world as soon as possible, master the key 3D graphics technology and experience the wonderful fun. However, for complex applications, these functions are far from enough and can only be used as a reference.

6.1 auxiliary Library Function Classification
This section can be viewed as a manual, so beginners do not have to go into it.
Auxiliary library functions are roughly divided into six categories:

  6.1.1 window initialization and exit
There are three related functions, which are mentioned in Chapter 1 and will be described in detail here:

Void auxinitwindow (glbyte * titlestring)

Open a window specified by auxinitdisplaymode () and auxinitposition. The function parameter is the window title. The default color of the window background is the color of the 0 color palette in the black or color table (color_index) of rgba. Press the Escape key to turn off the window, end the program, and clear all the screen.

Void auxinitdisplaymode (glbitfield mask)

Set the window display mode. The basic mode is rgba or color table, single or double cache. You can also specify other additional modes: depth, template, or cumulative cache (depth, stencel, and/or accumulation buffer ). The mask parameter is the Union (take or) of a set of BITs, aux_rgba, aux_index, aux_single, or aux_double, and other valid flags aux_depth, aux_stenpencil, and aux_accum.

Void auxinitposition (glint X, glint y, glsizei width, glsizei height)

Set the window position and size. The parameter (X, Y) is the screen coordinate of the upper-left corner of the window. The parameter (width, height) is the width and height of the window, in pixels. The default value is (0, 0,100,100 ).

  6.1.2 window handling and event Input
After the window is created and is in the main function loop, you should register the following callback functions ):

Void auxreshapefunc (void (* function) (glsizei, glsizei ))

Defines the shape redefinition function when the window is changed. The Parameter Function is a function pointer. This function has two parameters, namely, the new width and height after the window is changed. Normally, the function is glviewport (), which displays the new size after the cut and redefinition of the projection matrix to match the ratio of the projected image to the viewpoint to avoid the imbalance. If you do not call auxreshapefunc (), the default function to reset the object shape is to call a two-dimensional Normal projection matrix. With the auxiliary library, the window will be automatically re-drawn after each event changes.

Void auxkeyfunction (glint key, void (* function) (void ))

Define the keyboard response function. The parameter function is the function pointer called when the key is pressed. The auxiliary database defines several constants for the parameter key: aux_0 to aux_9, aux_a to aux_z, aux_a to aux_z, aux_left, aux_right, aux_up, aux_down (direction key), aux_escape, aux_space or aux_return.

Void auxmousefunc (glint button, glint mode, void (* function) (aux_eventrec *))

Define the mouse response function. The function parameter is the function called when the mouse acts on the button in mode. The parameter buttons include aux_leftbutton, aux_middlebutton, and aux_rightbutton (based on the right hand ). The mode parameter indicates the mouse touch status. When the mouse is hit, the value aux_mousedown and the value aux_mouseup is released. The function parameter must contain a parameter, which is a pointer to the structure aux_evenntrec. When the function auxmousefunc () is called, the corresponding memory is allocated to this structure. The general usage is similar to the following:

Void function (aux_eventrec * event)
{
Glint X, Y;
X = event-> data [aux_mousex];
Y = event-> data [aux_mousey];
...
}

  6.1.3 color table Loading
Because OpenGL does not have a window system, the color search table cannot be loaded because it depends on the Color ing of the window system. If you use the color table mode, you need to use a single color index function defined by the RGB value provided by the auxiliary database:

Void auxsetonecolor (glint index, glfloat red, glfloat green, glfloat blue)

Set the index of the custom color. The index parameter is the index number. The values of red, green, and blue are respectively red, green, and blue. The value range is (0 ~ 1.

  6.1.4 3D object Rendering
Each group of 3D objects has two forms: wire and solid ). The mesh does not have a plane direction, while the solid body has the ability to perform light and shadow calculations. When there is light, the solid body model is used. The parameters of the following functions define the object size and can be changed.

Function Function
Draw ball Void auxwiresphere (gldouble radius)
Void auxsolidsphere (gldouble radius)
Draw cube Void auxwirecube (gldouble size)
Void auxsolidcube (gldouble size)
Draw a cube Void auxwirebox (gldouble width, gldouble height, gldouble depth)
Void auxsolidbox (gldouble width, gldouble height, gldouble depth)
Draw a circular area Void auxwiretorus (gldouble innerradius, gldouble outerradius)
Void auxsolidtorus (gldouble innerradius, gldouble outerradius)
Draw a cylindrical Void auxwirecylinder (gldouble radius, gldouble height)
Void auxsolidcylinder (gldouble radius, gldouble height)
Draw 20 Faces Void auxwireicosahedron (gldouble radius)
Void auxsolidicosahedron (gldouble radius)
Drawing an ry Void auxwireoctahedron (gldouble radius)
Void auxsolidoctahedron (gldouble radius)
Draw a triangle Void auxwiretetrahedron (gldouble radius)
Void auxsolidtetrahedron (gldouble radius)
Draw a 12 Void auxwiredodecahedron (gldouble radius)
Void auxsoliddodecahedron (gldouble radius)
Draw a cone Void auxwirecone (gldouble radius, gldouble height)
Void auxsolidcone (gldouble radius, gldouble height)
Drawing a teapot Void auxwireteapot (gldouble size)
Void aucsolidteapot (gldouble size)
Table 6-1


All the above objects are drawn at their respective centers. All coordinates are units and can be scaled.

  6.1.5 background process management

Void auxidlefunc (void * func)

Defines the idle state execution function. The func parameter is a pointer pointing to the function to be executed. When it is zero, func execution is invalid.

  6.1.6 program running

Void auxmainloop (void (* displayfunc) (void ))

Define the Cycle Function of the scenario. The displayfunc Pointer Points to the scenario rendering function. When the window needs to be updated or the scenario changes, the program calls the function it refers to and re-draws the scenario.

6.2 application examples of auxiliary Libraries
The following is an example of the application of the secondary database, testaux. C:

  Example 6-1 Application routine testaux. c

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

Void myinit (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

Void myinit (void)
{
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Glclear (gl_color_buffer_bit );
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
If (W <= H)
Glortho (-1.5, 1.5,-1.5 * (glfloat) h/(glfloat) W, 1.5 * (glfloat) h/(glfloat) W,-10.0, 10.0 );
Else
Glortho (-1.5 * (glfloat) h/(glfloat) W, 1.5 * (glfloat) h/(glfloat) W,-1.5, 1.5,-10.0, 10.0 );
Glmatrixmode (gl_modelview );
Glloadidentity ();
}

Void callback display (void)
{
Glcolor3f (1.0, 1.0, 0.0 );
Auxwiresphere (1.0 );
Glflush ();
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0, 500,500 );
Auxinitwindow ("aux_sample ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

Figure 6-1 mesh sphere

The above program running result is to draw a Yellow mesh sphere in the screen window, which fully reflects the basic application method of the auxiliary library.
First, use the Helper library function in the main function to define a window auxinitwindow (), and then initialize the color myinit (), which is described in Chapter 1. The following two very important functions are auxreshapefunc () and auxmainloop (). The parameters are both a function pointer pointing to the callback function (callback is used for callback function definition ).
The former is the window shape redefinition function, and the parameter Pointer Points to the function myreshape (). Its two parameters are the new width and height of the window. Then, use glviewport (0, 0, W, h) to redefine the projection matrix in the new video,

Glmatrixmode (gl_projection );
Glloadidentity ();
If (W <= H)
Glortho (-1.5, 1.5,-1.5 * (glfloat) h/(glfloat) W, 1.5 * (glfloat) h/(glfloat) W,-10.0, 10.0 );
Else
Glortho (-1.5 * (glfloat) h/(glfloat) W, 1.5 * (glfloat) h/(glfloat) W,-1.5, 1.5,-10.0, 10.0 );

First, use glmatrixmode () to describe the gl_projection relationship between the current matrix operation and projection. Then, use glloadidentity () to clear the matrix as a matrix to avoid interference from other matrix operations. Then call glortho () perform a forward projection on the object, and use the judgment statement to give two conditions, so that the proportion of the projected image matches the viewpoint to avoid the imbalance.
Next, call glmatrixmode () to change the matrix operation to gl_modelview, which is related to object observation. Similarly, use glloadidentity () to clear the matrix. The latter is the main function loop function. The parameter Pointer Points to the function display (), that is, to draw an object. When the window needs to be updated or the object changes, the program calls it to redraw. The above example is the most basic application of the auxiliary database. The complex application will be detailed in subsequent chapters.

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.