-OpenGL-based 3D sky Simulation

Source: Internet
Author: User

In 3D Scene Rendering, the sky is an essential factor. 3D sky simulation is widely used in visual simulation systems, computer games, and 3D animations. However, there are still many deficiencies in the current simulation of the sky. Some simulation methods have problems such as complicated implementation, computing time consumption, low image resolution, less realistic sense, and only static images can be generated. In this paper, we study the simulated skyAlgorithmBased on the existing 3D graphics industry's popular OpenGL technology and the fractal theory known as "The ry of Nature", a more simple and practical method of 3D sky simulation is proposed, it can simulate a 3D sky with dynamic effects and different weather conditions. The rendering speed is fast and realistic.

L construct a sky model

1.1 OpenGL technology [1.2]

As a set of graphics standards, OpenGL is composed of hundreds of functional functions and provides all the features for accessing graphics hardware. It is designed based on the principles of computer graphics and conforms to the principles of Optics and vision, it is ideal for visual simulation systems. Its advantage is: firstly, in OpenGL, visual objects are allowed to be expressed in graphics; secondly, OpenGL can express the three-dimensional characteristics of objects through illumination, and its illumination model is the overall illumination model. The color of the visual simulation image reflects the spatial location relationship between the object and the viewpoint and the light source, and has a strong three-dimensional effect. In addition, OpenGL provides a method to use image data, that is, to directly read, write, and copy image data, or define image data as a combination of texture and graphic methods to generate visual images to enhance the effect. In this paper, 3D sky Simulation Based on OpenGL technology is studied, which makes the algorithm easy to implement and has good visualization effect.

1.2 traditional model

In traditional 3D games,ProgramPersonnel often adopt a very simplified sky model from the perspective of real-time simplicity. The following three methods are generally used: Clear the background with a light blue close to the sky; use the skybox method: first draw a polygon and thenCubeBody texture map; octal cone sky model: defines a roof, draws a circle of triangles centered on this vertex, and then maps the texture.

These three methods have the advantages of real-time and simple method, but there are also obvious deficiencies. The first method draws the sky effect too flat, simple and rough, low approximation. The second method requires a very high texture. If the texture is not used well, the texture on a large polygon is prone to side effects of tensile deformation. In addition, the atomization settings may also cause problems. If the fog settings are close to the observation point, the color of the sky will fade or even disappear. The third method has a significant roof.

Obviously, in today's era with increasingly high requirements for 3D scenario simulation, these methods are no longer suitable for the times and need to seek a fast rendering speed and a strong sense of realism, 3D sky models with certain interaction capabilities.

1.3 dome sky model

The earth is spherical, so the sky is like a half ball on the earth. When using a computer to simulate a 3D sky, the semi-sphere sky model is clearly in line with the real-world sky model. The following model can be created by using the spherical equation in mathematics:

P is a point on the sphere, and r is the radius of the ball. In the spherical coordinate system, the equation is changed:

For a hemisphere where. Phi is the latitude, its variation range is O ≤ Phi ≤ 90 °; θ is the longitude, and its variation range is O ≤ θ ≤ 360 °. Compared with the box method, the dome method has many advantages. First, because the sky drawn by the spherical method has more vertices, the atomization effect can be drawn more evenly [5]. At the same time, you can change the color of a single vertex in real time to achieve some very good effects, such as the sunlight at different times of the day.

There are also deficiencies in the semi-sphere sky model:

(1) The rendering speed is affected when the number of vertices is too large.

N is the total number of vertices to be drawn. △phi and △θ are the longitude and latitude intervals of vertices on the top of the spherical roof. The smaller the value, the closer the grid is, the finer the painting. Because of the use of the openg Technology in the Triangle Belt rendering (GL-TRIANGLE strip), each draw 4 vertices, each vertex is drawn 4 times, it needs to multiply by 4.

(2) There are too many trigonometric operations.

Rendering speed is not affected in a single scenario, but in a complex 3D scenario, the disadvantage of rendering speed is shown. In the computer, the calculation of trigonometric functions is much slower than that of addition, subtraction, multiplication, division, and so on.

1.4 curved sky model

In order to maintain the advantages of the global sky model and make up for the shortcomings of this algorithm, this paper studies a curved sky model with faster rendering speed, as shown in Model 1.

This model is similar to a four-point drop-down of a subdivided plane. It looks like an open parachute. It covers the scene and can simulate a 3D sky. The surface length is s:

The formula for calculating vertex coordinates (x, y, z) is:

 

△S is the subdivision interval of the surface, and J and I are the cyclic parameters.

The curved sky model can still be drawn using the triangle band (gltriangle_strip) in OpenGL technology, but it avoids the operation of trigonometric functions, and the number of vertices is less than that of the spherical sky model with the same precision, therefore, the rendering speed is increased.

2. Sky Cloud Maps

2.1 fragment Cloud Maps

The object model built by the fractal [4] method has a fine structure and relatively simple algorithm control. It is suitable for describing objects with fine structures such as clouds and trees. This paper uses the interpolation algorithm in the fragment theory to generate the texture of the static sky cloud map. The algorithm is as follows:

(1) divide the square mesh n × N on the plane and randomly give the colors of the four corners A1, A2, A3, and A4. 2.

(2) Calculate the midpoint, b1 = (A1 + A2 + A3 + A4) × 0. 25 + ε x scale, where ε is a random volume; Scale A (1/2) H (scale) H that is, the urst index, which is also a measure of the fractal dimension D: d A 2 h, 3 shows.

 

(3) Repeat section (2) based on A1, A4, B1, and a virtual point outside the grid to obtain the color value of C1 at the Edge Point. Similarly, the color values of the midpoint of the C2, C3, and C4 edges can be calculated.

(4) recursive steps (2) and (3) keep the square mesh refined until the expected recursive depth is reached.

In the preceding steps, the color of the initial square vertex is randomly assigned, but all vertices in the square mesh are not randomly distributed. By using the fragtal interpolation algorithm, the adjacent points in the grid are connected to each other, making the internal transition of the generative body more natural. In this way, a two-dimensional array is obtained through the fractal interpolation algorithm, and each element value in the array is treated as cloud opacity data. The smallest array value represents the blue, the least cloud in the sky, the largest value represents the most white, the most cloud in the sky. Analyze the array and generate the blue sky and white clouds texture figure 4.

2.2 texture ing

Texture ing technology [1.2] is also called the texture map technology (texture map-ping). Compared with the method for building models, texture ing is a more effective method. The simulation of the surface texture details of a scene plays an important role in the realistic graphics synthesis technology. This simulation of the surface texture details of a scene is called the texture ing technology. Texture ing has become one of the most common technologies in the 3D graphics world. Its mathematical process is very complex, but OpenGL has processed it, as a result, programmers can use several functions to complete texture textures.

The steps for executing a texture map can be summarized as: defining a texture map, controlling a texture, describing the texture method, activating texture ing, and defining texture coordinates. Texture ing on a sphere is complex. Different ing methods may cause different problems. If plane ing is used, texture stretching will occur continuously. If spherical ing is used, the biggest problem is that texture clustering occurs at the spherical pole. For good textures, this phenomenon can be minimized. Based on the spherical geometric knowledge, we can obtain the formula for calculating texture coordinates:

The reason for adding 1/2 is that the V coordinate value of U is in the range of (0.0, 1.0.

3. Dynamic Simulation

This article uses the Timer technology and the method of changing the coordinate of the texture matrix to achieve the flow effect of the cloud. First, define a 100 ms timer and change the texture coordinates every Ms. Second, we use multiple textures to add a separate cloud texture and transform the texture matrix coordinates to realize flow. The method to transform texture coordinates is to first set the current matrix type to a texture matrix (GL-TEXTURE), then call gltranslate (), glrotate (), and glscale () the function implements coordinate translation, rotation, and scaling. In this way, the cloud can move slowly in the sky. In addition, by adding a key response function, you can use the () pengl model transformation function in the function body, such as glrotate (), to rotate the 3D sky from any angle, this allows users to interact with 3D scenes.

4. Simulation results

In this simulation experiment, the CPU is P4, the clock speed is 1.7 GHz, the memory is 256 m, and the video card is integrated. Simulation result 5 ~ Figure 8. Clouds in the sky can move slowly along the spherical sky, and customers can rotate the 3D sky through the upper and lower keys. This model can be used in real-time 3D scenario simulation, and has a good 3D effect and a strong sense of reality. The frame rate of the semi-spherical sky model is 250 ~ 285 F/s; the frame rate of a curved sky model is 324 ~ 340 F/s, while ensuring authenticity and interactivity, the curved sky model is superior to the semi-spherical sky model in terms of rendering speed.

 

 

 

5 knots

The software platform of this simulation system is VC ++ 6.o, and visual simulation is achieved through OpenGL graphics library functions. Various 3D sky models are built based on geometric principles such as spherical equations, plane equations, and trigonometric functions. The sky textures are written using interpolation algorithms in the fractal theory, and the sky frames are drawn using the OpenGL functions, texture ing Technology Maps 3D skies with real effects. From the simulation results, it is highly realistic and real-time. In addition, through the key response function, it can be rotated from any angle, and the 3D sky can be upgraded or downgraded at any height, which is highly interactive, it is ideal for outdoor simulation of 3D games.

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.