The path to genius: real-time environment ing (Real-Time evironmnet mapping)

Source: Internet
Author: User

The path to genius: real-time environment ing (Real-Time evironmnet mapping)

<Made in coollen>

I carefully cut out the line wrapped in rubber and prayed that God would give it another chance. Each time you cut a knife, a sweat exists in the palm of your hand ...... When I cut 7 segments, I finally found the problem. It turned out that two of the four lines were broken ...... I inserted it into the USB port of the computer, holding the two broken wires in my hand to keep the circuit connected. After a long wait of three seconds, I was excited to see its name: PSX-BSB adaptor again in the list of game controllers. This was always with me since I was in my sophomore year. The game gamepad with me was revived. This proves the fact that I am a genius!

>>>>>> Fast Forward time: 32: 02: 27 >>>>>>

 

If you are a fan of racing games, you must have played need for speed: underground2, a racing game popular all over the world. In this game, players play an underground racing driver who makes a living by a racing car and fly in a city that is always night. This game uses real-time evironmnet mapping ), on the road that goes through the rain, we can see that the night scenes on both sides and the true reflection of the surrounding environment on the players' cars are the merits of the Environment ing technology. This cool and gorgeous special effect has now been applied to a large number of real-time virtual display games, and will become an essential element in the future.

The most typical application of Environment ing technology is the "streamer" of the car body, which is very common in reality. When you drive in a street full of neon lights, the surrounding lights will be projected on your body with a spot or band (if your car is very clean ). When the car is galloping on the street, it is like colorful spots flowing on your car body, which has a very dazzling effect. Even when you pass by someone else's car, other people's car will map to your car body.

To map environment maps, a set of environment information is required. Suppose there is a car you drive in the scenario (this example will be used all the time in this article). The environment information of the car is that after the car is removed, all the pictures you see in the car's position. Then you use a camera to take six photos in different directions, which generates a set of environment information. Each pixel in the car corresponds to a point in the environment information (that is, a ing ). At last, when rendering the car, mix the corresponding points on the car body and the effect of the car body in the environment.

To achieve environment map ing, perform the following steps:
A. Create an environment map
B. render the objects in the scene to the environment map.
C. When rendering the car body, the original texture and environment texture are processed and finally rendered to the screen.

Are you ready for your car? You must try to see how it feels to map the environment of your car. However, it is difficult to find the correct method without understanding the relevant knowledge.
 
What is environment texture?
As I mentioned above, in the analogy of taking a camera, the six pictures are similar to a set of Environment maps, and the six pictures represent the six faces of the cube. You can imagine that when your eyes are in this cube, you will see all the scenes around you, so it is called an environment map.

Generally, the Environment Maps we use are Environment Maps of cubes. These Environment Maps are usually called cube maps ). Each cube map contains six sides, which are represented by + X,-X, + Y,-y, + Z, and-Z in DirectX, each of its surfaces covers a 90-degree angle of view on both the horizontal and vertical planes.

How do we operate this set of cube maps?
In DirectX 9 we use six different identifiers to indicate which plane in the cube map. This group of identifiers is d3dcubemap_faces. The d3dcubemap_face_positive_x represents the plane pointed to by the positive X axis with the center point as the origin in the cube map, and so on. Each plane of the cube map is an lpdirect3dsurface9 object. When you need to perform some operations on a plane, you need to call the getcubemapsurface function to obtain the surface you specified, and then operate it like an ordinary surface. In this article, we will take the plane as the rendering target and draw all the scenes in the surrounding environment to this cube map. Because the cube map has a plane in each direction, rendering all the scenes in the scene is required each time one plane is drawn. The overhead of this process is very large. You only need to plot the scenario once. If an object in your scenario uses environment ing, it will increase rendering consumption by six times! If a game originally ran 70 frames, it immediately dropped to 10 frames when creating a special effect using environment ing! This is a terrible number! You may consider: Will this technology lose the value of use because of its high overhead? The answer is no. At present, human intelligence can well handle the efficiency of Environment ing, so don't worry, and we will explain these methods at the end of the article.

How to Use cube map?
When you render your Paster coordinates, the next step is how to use them. To draw a texture onto the screen, the coordinates correspond to the vertices of the model. We all know that for 2D textures, the u and v coordinates are used to represent the texture coordinates corresponding to the vertex. In cube map, only two vertices cannot represent a point in the cube. Therefore, the texture coordinates of cube map are represented by three vectors, you can simply understand the color of the texture coordinates as the pixel color value of the point where the box is located from the center of the box to the direction of the 3D vector and the intersection of the box. For example, you want to map a ball in the scenario to the surrounding environment, the simplest way is to pass the normal of each point of the ball as the texture coordinate to the graphics processing chip, so that the ball feels like a metal ball. However, in this case, the plot is somewhat different from the ing in the real world.

Now you can drive your car into your garage, stop and get off, and put the walls, ceilings, and floors of your garage in enlarged photos, just like doing environment ing ...... Wrong. The next step is to see how to proceed.

Microsoft DirectX 9 provides an id3dxrendertoenvmap interface for application development convenience. Through this help class interface, we can quickly complete environment Paster operations. Of course, as a common interface, it not only contains operations on cube map, but also supports other environment maps, such as sphere map. For more information, see the related documents of DirectX 9. The following code creates an environment texture:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

# Define cubemap_size 128

Idirect3dcubetexture9 * m_pcubemap;
Id3dxrendertoenvmap m_prendertoenvmap;

// Create the rendertoenvmap object. M_pd3ddevice and m_d3dsdbackbuffer are pointers to direct3d devices and cache pages respectively. The cubemap_size macro specifies the edge length of the cube map, which is related to the size of the environment map. The larger the value, the more space occupied, and the slower the painting.

If (failed (d3dxcreaterendertoenvmap (m_pd3ddevice, cubemap_size, 1,
M_d3dsdbackbuffer.format, true, d3dfmt_d16, & m_prendertoenvmap )))
{
Return e_fail;
}

// Create an empty cube map. d3dusage_rendertarget indicates that the created cube map is a rendering target.

If (m_d3dcaps.texturecaps & d3dptexturecaps_cubemap)
{
If (failed (d3dxcreatecubetexture (m_pd3ddevice, cubemap_size, 1,
D3dusage_rendertarget, m_d3dsdbackbuffer.format, d3dpool_default, & m_pcubemap )))
{
Return e_fail;
}
}
Else
{
Return e_fail;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

A new projection matrix needs to be obtained in the rendering process. This matrix sets the camera's angle of view and scene depth range. In addition, a new view matrix should be calculated so that the cube map will change with the angle of view on the "car.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

Hresult rendersceneintoenvmap ()
{
Hresult hr;

// Sets the projection matrix.
D3dxmatrixa16 matproj;
D3dxmatrixperspectivefovlh (& matproj, d3dx_pi * 0.5f, 1.0f, 0.5f, 1000.0f );

// Obtain the current observation matrix
D3dxmatrixa16 matviewdir (m_matview );
Matviewdir. _ 41 = 0.0f; matviewdir. _ 42 = 0.0f; matviewdir. _ 43 = 0.0f;

// Draw the scenario to the Cube Map
If (m_pcubemap)
HR = m_prendertoenvmap-> begincube (m_pcubemap );

If (failed (HR ))
Return hr;

For (uint I = 0; I <6; I ++)
{
// Set a plane in the cube map as the current rendering target.
M_prendertoenvmap-> face (d3dcubemap_faces) I, 0 );

// Calculate the new observation matrix
D3dxmatrixa16 matview;
Matview = d3dutil_getcubemapviewmatrix (d3dcubemap_faces) I );
D3dxmatrixmultiply (& matview, & matviewdir, & matview );

// Set the projection and observation matrix and render the scene (omitted ). Note: objects in the Rendering scenario do not include objects that use environment maps.
......
}

M_prendertoenvmap-> end (0 );
Return s_ OK;
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

After rendering the environment map, you need to use this environment map on the target object, that is, the car that has always been used as an example. We use a special effect (effect) in the Rendering scenario (not the Rendering scenario mentioned in the previous step, but when it is actually drawn to the screen. Special effects can be created from a string in the memory or from a special effect file (suffixed with FX, generated by effect edit of directx9 SDK. You can view the relevant documentation and examples in the SDK for how to use the special effects. Because there are a lot of things involved, this is not the case here. The code for the special effect file is as follows. Here we provide the implementation code based on the Fixed rendering pipeline (fixed function pipeline). In addition, you can also use the programmable pipeline.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

Texturecube texcubemap;

Matrix matworld;
Matrix matview;
Matrix matproject;

Technique cube
{
Pass P0
{
// Vertex state
Vertexshader = NULL;
Worldtransform [0] = <matworld>;
Viewtransform = <matview>;
Projectiontransform = <matproject>;

// Pixel state
Texture [0] = <texcubemap>;

Minfilter [0] = Linear;
Magfilter [0] = Linear;

Addressu [0] = clamp;
Addressv [0] = clamp;
Addressw [0] = clamp;

Colodrop [0] = selectarg1;
Colorarg1 [0] = texture;

Texcoordindex [0] = cameraspacereflectionvector;
Texturetransformflags [0] = count3;
}
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>

Note:
1. Use static environment textures instead of real-time rendering
In addition to real-time rendering environment maps, another common practice is to read a pre-generated environment map from a file. Static pasters are easier to achieve than real-time rendering. They do not need to plot the scenario to the Cube map, but only need to load the scenario from the hard disk file during creation. Static pasters can greatly reduce rendering overhead. They are suitable for low-end environments. However, using static textures will greatly reduce the game's authenticity. For example, textures on the car body will not change with the environment.

2. Reduce the rendering overhead of real-time rendering
In addition to using static environment textures, you can also use other methods to reduce system overhead. For example, "underground mania 2" uses a method that limits the refreshing rate of Environment textures to achieve real-time rendering. That is to say, the Environment textures are only rendered for a certain number of times per second, rather than rendering each frame. In addition, this frequency can be adjusted by players. Another method is to limit the number of rendered objects in the scenario. For example, if a car is driven to road section A, only the buildings on both sides of road section A are drawn. Alternatively, only a part of the objects that can be clearly mapped can be drawn. In addition, you can reduce the size of the Environment Paster, which will affect the rendering effect and blur the environment ing effect on the car body.

3. Use a programmable rendering pipeline
The use of programmable rendering pipelines greatly improves the flexibility of graphics card programming. As the graphics card performance increases day by day, you can draw more realistic effects from the graphics card. For environment textures, the above Code uses vertices to calculate the texture coordinates. If pixel-by-pixel (Per-pixel) calculations are used, the effect will be more realistic, of course, the overhead of this operation is too large. Currently, it is limited to top-level graphics cards.

4. Blog
Visit the blog of coollen for more information. (Code: Life http://blog.coollen.com)

 

>>>>>> Fast Forward time: 342: 34: 47 >>>>>>

The flame pattern is SLG, the handle is repaired, and it is nice to play.
The music is piano, and the speaker is 25. It sounds unpleasant.
The typhoon is in Public Beta, the bug is to be changed, and there is no money to work overtime.
It's terrible to write a program. There's no time, and my girlfriend ran away.
The score is countdown, the technology is cainiao, and there is no future.

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.