O Introduction
Virtual reality refers to a virtual environment generated by computers, which allows people to enter a three-dimensional, multimedia virtual world, interact with virtual objects through some operating interfaces, and combine with various sensors and mechanical components to give people an immersive experience. With the development of computer hardware and software and the enhancement of graphics processing capabilities, the application of virtual reality technology in military, medical, education, industry and games has become more and more extensive.
Virtual 3D environment is an extremely important part of virtual reality technology. Although it can be created with ready-made 3D engines, most of them are expensive and mainly target the 3D game market. Therefore, if the problem to be solved is not particularly complex, it can be completed by programming yourself and using some tool software.
1 Development environment and software
1.1 Microsoft Visual C++ 6.0
Microsoft Visual C++6.0 (hereinafter referred to as vC++6.0) is one of the powerful visual development platforms produced by Microsoft. We can use it to complete various stages of application creation, coding, testing, and improvement on the Windows operating system.
I.2 OpenGL
OpenGL (Open Graphics Library) is a 3D API that is independent of the hardware system and operating system. It is a cross-platform "software interface for graphics hardware" and is highly portable. It includes a large number of drawing and display functions for quickly creating real-time 3D graphics. It is mainly used in the field of graphics and has become an industrial standard. The latest version is OpenGL2.0.
1.3 3dsmax 7
3ds max 7 is a software produced by Autcdesk for making 3D models and animations. It can be used to make various 3D models and animations of lifelike people, animals, plants, buildings, etc. We use 3ds max 7 to make some 3D models of animals, plants, and buildings, and export them as 3ds files, and then call them in a virtual three-dimensional environment.
1.4 Vue 5 Infinite
Vue5 Infinite (hereinafter referred to as vue5) is produced by e-on software company. It can also be used to make 3D models and animations. Its specialty is the simulation of 3D natural environment. In a virtual 3D environment, the realism of the sky is one of the important factors affecting the visual effect. We use Vue5's environmental simulation function to create pictures of the sky under various atmospheric conditions, and then use these pictures as the material map of the "sky box" in the virtual 3D environment to show a realistic atmospheric environment effect. In addition, we can also use Vue5's powerful terrain editing function to create some terrain models with different characteristics to enrich the virtual 3D scene.
2. Production of 3D Models in Virtual Scenes
2.1 Skybox Model
We use 3ds max 7 and other tool software to create 3D models and other elements in virtual 3D scenes. First, start with the sky. Open 3ds max 7, create a cube, and adjust its side length to a relatively large value (such as 2000 meters). We call this cube a sky box. Then make the material map of the sky box, which is the picture of the atmospheric environment. Open Vue5, select the "Manned Atmosphere" option, and then select an atmospheric environment you need from the pop-up dialog box (of course, you can also use its editing function to create the atmospheric effect you want), then click the "Export Sky" option under the file menu to export the atmospheric environment as a bitmap, and then use the image processing tool to decompose the entire bitmap into six bitmaps corresponding to the six faces of the cube; enter 3ds max 7 again, select the cube just now, use the material editor to assign the sky picture just exported to the cube, make some adjustments, and the sky box model is completed. The last step is to export the sky box model as a "3dS" type file so that it can be called in the program.
2.2 Models of various entities such as buildings
As needed, we should also add some 3D models such as buildings to the scene. Of course, these models are also made using 3ds max 7. The specific production methods are not repeated here. However, it should be noted that the 3D models should be drawn in real time in the program. Therefore, when making models, we should not blindly pursue precision, but should consciously control the number of polygons to increase the rendering speed when the program is running. Similarly, save the finished model as a "3ds" type file.
2.3 Trees and other plants
Generally, 2D pictures are used to display plants. In this way, even if there is a large forest in the scene, it will not affect the rendering speed. When making plant pictures, you should first choose a suitable plant picture (preferably a frontal photo) and do some simple processing on the picture to meet the following requirements: 1) The picture size is 2 to the power of N (32, 64...); 2) The background color of the picture and the places that do not need to be displayed are pure black. In order to process these places into a transparent state in the program. Finally, save the picture in "bmp" format, and the plants in the virtual 3D scene are ready.
2.4 Generating terrain data
The creation of terrain no longer adopts the method of pre-modeling, but adopts the method of real-time drawing in the program, which is convenient for detecting and calculating the ground height of the location when roaming in the scene. The specific steps are as follows: First, create a three-dimensional floating-point array to save the vertex coordinate values of the terrain, and the three components are the x, y, and z coordinates of the terrain vertices; then, we create two more arrays to save the vertex index value (integer) and the plane coordinates of the surface map (floating point); then, use a loop code to randomly generate the height value of the ground and assign values to the three arrays at the same time; finally, call the functions glEnableClientState() and glVertexPointev() in OpenGL to load the terrain vertex coordinate array, and use glEnableClientState() and glTexCoordPointer() functions to load the vertex index array. In this way, the initialization of the terrain is completed, and this process can be written as a function called lniin(), which is convenient for calling in the program.
3 Creating an OpenGL-based application framework
All drawing functions in OpenGL must be run in the OpenGL environment, which is called the OpenGL framework. The OpenGL framework must also run in the Windows application framework. In other words, you must first establish the Windows application framework to respond to and process various messages and commands, and then establish the OpenGI runtime environment framework and call various OpenOL drawing functions in it.
3.1 Creating a Windows Application Framework
First, create a Win32 application in VC+6.0; then, add four global variables htX2, hRC, hWnd and hlnstance, which are used to save the device description table, the shader description table, the window handle and the instance handle of the program respectively; then include the three header files g1.h, glu.h and glaux.h and the three static link library files opengl32.1ib, glu32.1ib and glaux.1ib in the application; finally, add the winMain() function and WndProc() function to the program (please refer to MSDN for specific methods). Among them, the WinMain() function is the entrance of the main program. We add some code to create the Windows window and use the PeekMessage() function in the WinMain() function to complete the message loop (that is, the main loop of the program, which is repeatedly executed by the program). It is used to monitor and pass information to the WndProc() function. WndProc() is a window callback function used to process various window messages and respond to external events. In this way, a basic Windows framework is built.
3.2 Creating the OpenGL runtime framework
The steps are as follows: 1) Initialize the OpenGL operating environment; 2) Call the functions in the OpenGL function library to complete the drawing and display work and refresh the screen. The code that completes these tasks can be written into a function named DrawScene() so that it can be called in the program; 3) Release various resources before the program exits and disconnect OpenGL from the Windows window 121.
3.3 Relationships between framework programs
The winMain() function is the entry point of the program. We use it to create a window and set its size and style. The WndProc() callback function will respond to the "Create Window" message and immediately call the SetPixelFomat() function in the OpenGL framework to detect the machine's support for OpenGL and install the OpenGL display interface. When the window is successfully created, the WndProc() function responds to the window size change message and calls gluPerspective() and other functions in the OpenGL framework to transform and adjust the OpenGL view. After the Windows window is generated, it enters the main loop of the program, in which the DrawScene() function is called to draw the scene, while listening to and assigning various messages. When the program exits, the wglDeleteContext() function in OpenGL is called to cut off the connection between OpenGL and the Windows window.
4 Display of virtual 3D scenes and human-computer interaction
After building the application framework, we can add the code to draw the scene. We need to draw the sky, ground, plants and buildings in the scene, gradually create a virtual three-dimensional environment, and then add the function of roaming in it to achieve simple interaction.
4.1 Drawing and displaying terrain
After the OpenOL framework is initialized, the InitTerrain() function needs to be called to generate the terrain data array and initialize the terrain. Write a function to draw the terrain, named DrawTerrain(), in which the glDrawgle. ments() function is called to draw the terrain by drawing triangle strips. If the terrain has a texture, the glBindTexture() function can be called to map the ground. Add DrawTerrain() to the DrawScene() function so that the terrain can be automatically redrawn every time the screen is refreshed.
4.2 Load and display 3ds format model files
Virtual objects such as the sky and buildings are created in a pre-modeled way, and we have saved them in a unified "3ds" format file. We also need to include a header file named "3ds.h" in the program, and its corresponding source file is called "3ds.cpp", both of which can be found on the Internet. For each 3ds file, you can first use the loading function in the 3ds.h file to call it into the program, assign it a number, and then call the display model function to display the corresponding 3D model in the OpenGL environment. The parameters of the display model function also define the position, direction and scale of the model display. The work of loading the model can be completed when the program is initialized, and the function of displaying the model should be added to the DrawScene() function.
4.3 Loading and displaying plants
According to the method mentioned above, displaying a plant in a virtual 3D scene is actually displaying a picture of the plant (not a 3D model). We first load the picture in the program, then draw a quadrilateral on the screen and display the picture of the plant as a texture. When roaming in a virtual 3D scene, we know the direction of our sight and can also calculate the coordinates of our position. Then, we can write a piece of code to make the quadrilateral with plant texture always perpendicular to our sight direction every time it is redrawn, thus forming a 3D model effect.
4.4 Add roaming function
After completing the creation and drawing of the virtual 3D scene, we need to add a roaming function to the program. With the roaming function, we can not only see the virtual 3D scene, but also participate in it in the first person, and experience this virtual world more "realistically".
In reality, the picture effect people see mainly depends on two aspects: the position of the observation point and the direction of the line of sight. In OpenGL, there is also something similar to our eyes. We can use the function gluLookAt() to change the position of the observation point and the direction of the line of sight in the OpenGL scene. We add this function to the DrawScene() function. During the program running, the observation point coordinates and the line of sight can be changed by using the keyboard and mouse. Each time the screen is refreshed, the parameter value in the function gluLookAt() is changed, and OpenGL will redraw the screen with a new perspective, thus creating the effect of roaming in the scene.
So far, a simple interactive virtual 3D environment has been built, which can serve as the basic module of any practical virtual 3D environment. On this basis, we can continue to enrich the scene content, add other functions, and gradually improve the program.
5. Program optimization and introduction of other functions
There are many methods worth learning from in the design of virtual 3D environments. For example, using terrain grayscale maps to generate height data, using regional block display technology to increase rendering speed, using M0D (hierarchical display) technology to speed up the display of textures and produce better results, using bitmap fonts, outline fonts, etc. to display Chinese characters, using display lists to display multiple repeated models to improve efficiency, using particle animation to simulate explosions, smoke and other scenes, adding collision detection functions to the program to comply with physical principles, and so on.
6 Summary
With the development of computer technology, the close connection between the virtual world and the real world can no longer be ignored. The virtual world is reproducing every plant and tree in the real world more and more realistically. As the main body of the virtual world, the virtual three-dimensional environment directly determines the realism of the virtual world. The application of OpenGL and other technologies has made three-dimensional graphics technology more and more widely used in virtual reality, three-dimensional simulation, simulation training, electronic entertainment and other fields. The simulation of some natural environments and working environments that are usually impossible or difficult to achieve has become commonplace in the virtual world. While giving the experiencer a more realistic experience, it also greatly reduces various costs and improves work efficiency, and has broad development space.
Previous article:Application of Virtual Reality Technology
Next article:Design of distance measurement system based on ultrasonic sensor
- Popular Resources
- Popular amplifiers
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Tips for getting STM32 code running time
- Release an operating system source code package that runs on NXP LPC1768.
- EEWORLD University Hall----Live Replay: SimpleLink? Latest Software and Hardware Solutions for Wireless Platform
- Thank you-Brothers of EE Water Army
- nF capacitor and 1M resistor in parallel between the metal case and GND
- Do you know about Allegro's latest solution for vehicle electrification and emission control 48V system?
- 【CH579M-R1】+ Bluetooth function key preliminary test
- Angle estimation problem of micro-core sliding film observer SMO
- BPM shortwave receiver module
- DAC8552 of MSP430F5529 microcontroller