GIS 3D Scene Simulation Design (I) infrastructure

Source: Internet
Author: User

Return to the GIS 3D Scene Simulation Design tutorial summary page

  Introduction

GIS (Geographic Information System), GPS (global positioning system), RS (Satellite Remote Sensing Technology) three large space science technology, known as "3 S" technology, it is regarded as an important cutting-edge technology for the development of information science and technology in the 21st century. Particularly in the new century, GIS has become one of the pillar industries in China's information industry. In various terrain digital simulation application systems, the core application systems are both highly dependent on spatial data and GIS technology platforms. GIS is a widely used technical discipline. This series of articles will introduce it from the perspective of 3D geographic simulation, in this section, we will introduce the main techniques such as DEM Digital Elevation Model and OpenGL.

This type of GIS application software is usually built on the OpenGL platform, and the system is no exception. Before implementing other special functions, you must correctly configure and install the OpenGL environment before developing various practical functions. The following is a brief introduction to OpenGL and starts the construction of the OpenGL application framework.

  OpenGL Overview

OpenGL is a software interface to graphic hardware. Essentially, it is a fully portable and fast 3D graphics and modeling library. By using OpenGL, you can create exquisite and beautiful 3D images with visual quality close to the ray tracking program. However, it is executed several times faster than the ray tracing program. OpenGL uses optimization algorithms carefully developed by Silicon Graphcs (SGI), a recognized industry leader in computer graphics and animation. Developers can use over 150 graphical functions provided by OpenGL to easily create 3D models and perform 3D real-time interaction. These functions do not require developers to write data from 3D object models into a fixed data format.
Developers can not only directly use their own data, but also use data sources in other formats, which can greatly shorten the software development cycle.

OpenGL not only renders the entire 3D model and draws a realistic 3D scene, but also supports 3D Interaction and action simulation. Its basic functions include the following:

(1) model rendering. In OpenGL, complex 3D models can be constructed by drawing basic shapes such as points, lines, and polygon. OpenGL often uses the polygon of the model and its vertices to describe the 3D model.

(2) model observation. After creating a 3D model, you can observe the model through OpenGL description. This observation process is implemented through a series of coordinate transformations. This transformation allows the observer to obtain a 3D model scene that is appropriate to the viewpoint position. The type of projection transformation has a great impact on the observation of the model. The 3D model scenarios obtained under different projection transformations are also different. At the end of the model observation process, scenes must be cropped and scaled to determine the display of the entire 3D model scenario on the screen.

(3) Specify the color mode. In OpenGL, you can specify the color mode of the model (RGBA mode and color table mode ). In addition, you can select the model coloring method (plane coloring and smooth coloring) to color the entire 3D scene.

(4) illumination effect. To make the 3D model drawn by OpenGL more realistic, you must also increase the illumination effect. Currently, OpenGL only provides management methods for radiant light, ambient light, mirrored light, and diffuse reflection light. In addition, you can also specify the reflection characteristics of the model surface.

(5) enhanced image effects. OpenGL also provides a series of functions to enhance the effect of 3D scene images. These functions enhance the image effect through anti-image processing, mixing, and atomization. Here, the anti-image is used to improve the image's midline image's sawtooth to make it smoother; the mixture is used to process the translucent effect of the model; the atomization gradually fades the scene image from the viewpoint to the distance, bring it closer to the reality.

(6) bitmap and image processing. OpenGL provides functions for bitmap and image processing.

(7) Texture ing. Texture is common to the surface of a real object. If the details of a 3D model are missing, the texture ing function is provided to show 3D scenes more realistically. The texture ing function provided by OpenGL allows you to easily paste a texture image to a scene polygon.

(8) Dual cache technology. The dual cache technology provided by OpenGL is mainly used for real-time animation. To achieve smooth animation effect, you need to first generate the next frame of image in the memory, and then copy it from the memory to the screen.

(9) human-computer interaction. OpenGL provides a convenient 3D graphic human-machine interaction interface, through which you can choose to modify objects in a 3D landscape.

  Establishment of OpenGL Application Framework

First, create a single-document application and add the header files and imported files to the project so that compilation can be successful. In VC ++, the OpenGL header file is generally stored in the GL subdirectory of the system header file directory. Therefore, you must specify the relative path when specifying the inclusion:

# Include <gl \ gl. h> // header file of the OpenGL32 Library
# Include <gl \ glu. h> // header file of the GLu32 Library
# Include <gl \ glaux. h> // header file of the GLaux Library

Here gl. h is the basic header file. h is the application header file. Most applications need to contain both header files, glaux. h is a secondary header file that is only included when needed. Next, bring up the "Project Settings" dialog box and add the Standard import library of glu32.lib, glaux. lib, and OpenGL win32 to the Project on the "Link" option page.

Next, initialize OpenGL, which is also the most important part of this article. First, let's take a rough look at the basic steps: first obtain the device environment (DC) that needs to be drawn above, set the pixel format for the device environment, and then create an OpenGL device based on the device environment. Finally, initialize the OpenGL Rendering scenario and State settings. The implementation of the first three steps is implemented in the SetOpenGLInterface () function:

PIXELFORMATDESCRIPTOR pfd = {
// Initialize the pixel Storage Format
Sizeof (PIXELFORMATDESCRIPTOR), // pfd size
1, // version number
PFD_DRAW_TO_WINDOW | // supported window
PFD_SUPPORT_OPENGL | // supports OpenGL
PFD_DOUBLEBUFFER, // supports dual-Cache
PFD_TYPE_RGBA, // RGBA type
24, // 24-digit color depth
0, 0, 0, 0, 0, 0, // color bits (ignored)
0, // No alpha Cache
0, // ignore the conversion bit
0, // No accumulated bit
0, 0, 0, 0,
32, // 32-bit deep Cache
0, // no template Cache
0, // no secondary Cache
PFD_MAIN_PLANE, // master Drawing Layer
0, // Reserved
0, 0, 0 // layer mask ignored
};
M_pDC = GetDC (); // get the device environment handle
Int iFormat = ChoosePixelFormat (m_pDC-> m_hDC, & pfd); // you can specify the pixel format.
SetPixelFormat (m_pDC-> m_hDC, iFormat, & pfd );
M_hGlrc = wglCreateContext (m_pDC-> m_hDC); // create a rendering context
WglMakeCurrent (m_pDC-> m_hDC, m_hGlrc); // you can specify the description of the current drawing of a thread.

Here, we first fill in the PIXELFORMATDESCRIPTOR structure variable that describes the pixel storage format. After obtaining the device environment handle, we call the ChoosePixelFormat () and SetPixelFormat () functions to return and set the best matching pixel format. Finally, call wglCreateContext () to create a rendering context RC and use it as a parameter to create a current Drawing description table through wglMakeCurrent, release the WM_DESTORY message after it is drawn:

ReleaseDC (m_pDC); // release the DC
If (m_hGlrc! = NULL) // release RC
WglDeleteContext (m_hGlrc );

After processing OpenGL, the initialization is complete. However, to achieve a realistic visual effect, you need to further set the scenario, which is completed in the InitOpenGL () function. Specific work includes various definitions of light sources:

GLfloat light_position [] = {0.0, 0.0, 1.0, 0.0}; // defines the coordinates of the position of the light source.
GlLightfv (GL_LIGHT0, GL_POSITION, light_position );
GLfloat light_ambient [] = {0.0, 0.0, 0.0, 1.0}; // define ambient reflected light
GlLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient );
GLfloat light_diffuse [] = {1.0, 1.0, 1.0, 1.0}; // defines the diffuse light.
GlLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse );
GLfloat light_specular [] = {1.0, 1.0, 1.0, 1.0}; // defines the reflected light.
GlLightfv (GL_LIGHT0, GL_SPECULAR, light_specular );
GLfloat light_model_ambient [] = {0.4f, 0.4f, 0.4f, 1.0f}; // defines the optical model parameters.
GlLightModelfv (GL_LIGHT_MODEL_AMBIENT, light_model_ambient );
GLfloat local_view [] ={ 0.0 };
GlLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view );

And enable various functions:

Glable (GL_LIGHTING); // valid GL_LIGHTING
Glable (GL_LIGHT0); // valid for GL_LIGHT0
Glable (GL_DEPTH_TEST); // allows deep comparison.
GlDepthFunc (GL_LESS); // compare the activation depth
GlClearColor (0.1f, 0.1f, 0.5f, 0.0f); // you can specify a blue background.
GlHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE); // weigh image quality and rendering speed

Generally, SetOpenGLInterface () and InitOpenGL () are executed after the WM_CREATE message is sent to ensure that the OpenGL environment is set at the beginning of the program startup. After the view initialization and update are completed, the final processing will be performed to define the view. The following InitViewPort () function implementation code will complete this function:

CRect rect; // obtain the size of the drawing customer area
GetClientRect (rect );
GlMatrixMode (GL_PROJECTION); // sets the projection mode.
GlLoadIdentity (); // load unit matrix
If (m_nViewMode = 0) // create a pivot Projection Matrix
GluPerspective (90.0, rect. Width ()/rect. Height (), 1.0, 10000.0 );
If (m_nViewMode = 1) // create a normal Projection Matrix
GlOrtho (-0.5*10000.0, 0.5*10000.0,-0.5*10000.0, 0.5*10000.0, 1.0, 10000.0); glViewport (0, 0, rect. width (), rect. height (); // refresh the view
GlMatrixMode (GL_MODELVIEW); // you can specify the current matrix mode.
GlLoadIdentity (); // load unit matrix

The main tasks completed here are the setting of the projection mode, the establishment of the projection matrix, and the re-setting of the visual interfaces. The value of the control variable m_nViewMode determines the projection mode (perspective projection or normal projection), and calls the gluPerspective () or glOrtho () function based on different projection modes () create a projection matrix. The gluPerspective () function is used to create a symmetric perspective object. The first parameter defines the angle of the field of view on the X-Z plane, with a value range of [0.0, 180.0]; the second parameter is the ratio of the width and height of the projection plane. The last two parameters are the distance from the distance cropping surface to the viewpoint along the Z negative axis, which is always positive. GlOrtho () is used to create a parallel Visual Object (actually, to create a projection matrix and multiply it by the current matrix ). The near-crop and far-crop planes are rectangles. The three-dimensional coordinates of the bottom-left and upper-right vertices of the near-crop rectangles are (left, bottom,-near) and (right, top, -near); the space coordinates of the remote cropping plane are (left, bottom,-far) and (right, top,-far ). Here, all near and far values are both positive and negative. If no other transformation is performed, the direction of the projection is parallel to the Z axis, and the viewpoint is oriented to the Z negative axis.

You can start to draw the scene after the view is determined. This is mainly completed in ReDraw () and called in OnSize (), OnDraw (), and other places that need to be re-painted. This part is not part of the OpenGL framework, so the implementation of this function will be described in detail in subsequent articles.

  Summary

This article mainly introduces the general construction method of the OpenGL program framework, and provides a basic OpenGL environment for further development of the 3D Scene Simulation processing system. Readers need to pay attention to the following knowledge points: initialization of OpenGL, light source settings, and Visual View settings. The program described in this article is compiled by Microsoft Visual C ++ 2000 under Windows 6.0 Professional + SP4.

Author: Qingdao Lang Rui Source: Tianji development responsibility Editor: Fangzhou

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.