OpenGL basic Graphics Programming (VI)

Source: Internet
Author: User
Tags clear screen

VI. Basic use of OpenGL Auxiliary Group Library

   OpenGL is an open system that is independent of any windowing system or operating system. Although it contains many graphical functions, it does not have a window function, and there is no function to read events from the keyboard and mouse, so it is quite difficult for beginners to write a complete graphics program. In addition, OpenGL graphical functions provide only basic geometric prototypes: Point, Line, and polygon, so it is not easy to create basic three-dimensional geometry such as balls and cones. and OpenGL Auxiliary Library is designed to solve these basic problems, it provides some basic window management functions and three-dimensional graphics drawing functions, can help beginners into the OpenGL world as soon as possible, master the key three-dimensional graphics technology, experience the wonderful fun. However, for complex applications, these functions are far from sufficient and can only be used as a reference.

6.1. Auxiliary Library Function classification

This section can be consulted as a manual, and beginners need not delve into it. The Auxiliary library functions are broadly divided into six categories: window initialization and exit, window processing and event input, color table loading, three-dimensional object drawing, background process management, and program operation.
6.1.1 Window initialization and exit
there are three related functions, which are mentioned in chapter one and are described in detail here: 
void Auxinitwindow (Glbyte *titlestring)
opens a window specified by Auxinitdisplaymode () and Auxinitposition (). The function parameter is the window caption, the default color of the window background is the black or color table under RGBA (Color_index) under the color of the No. 0 palette. Press the Escape button to complete the closing of the window, the end of the program, all clear screen three functions.   
void Auxinitdisplaymode (Glbitfield mask)

Sets the window display mode. The basic mode has RGBA or color tables, single or dual caches, and other additional modes can be specified: depth, template, or cumulative cache (depth,stencil,and/or accumulation buffer). A parameter mask is a combination of a set of bit flags (take or), Aux_rgba or Aux_index, Aux_single, or aux_double, and other valid flags aux_depth, Aux_stencil, or aux_accum.

void Auxinitposition (Glint x,glint y,glsizei width,glsizei height)
sets the window position and size. The parameter (x, y) is the screen coordinate of the upper-left corner of the window, and the parameter (width, height) is the width and height of the window, in pixels, and the default value is (0, 0, 100, 100).

6.1.2 Window processing and event input
when the window is created, and before entering the main function loop, the following listed callback functions should be registered (callback function):
void Auxreshapefunc (void (*function) (Glsizei,glsizei))
defines the shape reset function when the window changes. The argument function is a function pointer with two parameters, a new width and a new height after the window has changed. Normally, the function is Glviewport (), showing the new size after the cut, redefining the projection matrix so that the scale of the projected image matches the viewpoint and avoids the imbalance. If you do not call Auxreshapefunc (), the function function of the default Reset object shape is to call a two-dimensional orthographic projection matrix. Using a secondary library, the window will automatically redraw after each event has changed.
void Auxkeyfunction (Glint key,void (*function) (void))
defines the keyboard response function. parameter functions are the function pointers that are called when the key key is pressed, and the secondary library 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 (arrow keys), Aux_escape, Aux_space, or Aux_return.
void Auxmousefunc (Glint button,glint mode,void (*function) (AUX_EVENTREC *))
defines the mouse response function. A parameter function is one that is called when the mouse acts on a button in mode mode. The parameter button has Aux_leftbutton, Aux_middlebutton, or Aux_rightbutton (the right hand is the standard). The parameter mode means that the mouse strikes the state, when the hit is Aux_mousedown, and the Aux_mouseup is released. The parameter function must take a parameter, which is a pointer to the structure AUX_EVENNTREC. When the function Auxmousefunc () is called, the corresponding memory is allocated for the structure. The usual 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 itself does not have a windowing system, the color map that relies on the window system cannot be loaded into a color lookup table. If you are using the color table pattern, use a single color index function that is provided by the secondary library with RGB values: 
void Auxsetonecolor (Glint index,glfloat red,glfloat green,glfloat Blue)
sets the index of the custom color. The parameter, index, is the red, green, and blue values, respectively, in the range (0~1).
   6.1.43-dimensional object rendering
each group of three-dimensional objects consists of two forms: a mesh body ( Wire) and solid body (Solid). The mesh has no plane normal direction, and the solid body has, can carry on the light and Shade computation, has the illumination to use the solid body model. The parameters of these functions are defined as the size of the object and can be changed.


All of the above objects are drawn at the origin of their respective centers, and all coordinates are flat and can be scaled.

    6.1.5 Background Process Management
void Auxidlefunc (void *func)
defines an idle state execution function. The parameter func is a pointer to the function function to be executed. When it is zero, func execution is invalid.

   6.1.6 program Run 
void Auxmainloop (void (*displayfunc) (void))
defines the scene draw loop function. The Displayfunc pointer points to the scene drawing function. When the window needs to be updated or the scene changes, the program invokes the function it refers to and redraws the scene.
6.2. Auxiliary Library application Example
Here is an example of an application for an auxiliary library, TESTAUX.C:
  Example 6-1 Auxiliary library application routines 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); }

The result of the program operation is to draw a screen window within a Yellow mesh sphere, this program fully embodies the basic application of the auxiliary library method.
First, define a window Auxinitwindow () with the Auxiliary library function in the main function, and then initialize the color Myinit (), as explained in the first chapter. Next is the two very important functions Auxreshapefunc () and Auxmainloop (), the parameters are a function pointer, which points to the callback function (the callback function is defined with the callback description).
The former is a window shape reset function, the parameter pointer points to the function Myreshape (), its two parameters is the window's new width and new height. Then use Glviewport (0, 0, W, h) to reposition the viewport and redefine the projection matrix within the new viewport,
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);
that is, using Glmatrixmode () to illustrate the current matrix operation and projection of the gl_projection, and then use Glloadidentity () to clear the matrix to the unit matrix, to avoid interference by other matrix operations; then call Glortho () The object is positively projected, and two cases are given by the judgment statement, so that the proportion of the projected image is matched with the viewpoint to avoid the imbalance.
again, call Glmatrixmode () to change the matrix operation to the Gl_modelview of the observed object, and also to clear the matrix with the Glloadidentity (). The latter is the main function loop function, the parameter pointer points to the function display (), that is, the object is drawn. 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 library,

OpenGL basic Graphics Programming (VI)

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.