Water ripple (water ripple)

Source: Internet
Author: User

Demo download

Water Ripple, the mouse click after the water will be scattered, creating a feeling of ripples, very real.

Implementation principle:


diffusion: When you cast a stone into the water, you will see a circle of water waves formed by the point of the stone into the center, where you may bemisleading to think that every point on a water wave is spread out at the center of a stone into the point of the world, which is wrong. In fact, any point on the wavewhen all the time is the center of their own to spread around, the reason is to form a ring of water, because the inner waves of the interior because of the symmetry of diffusion and mutualoffset it.
Attenuation: Because the water has damping, otherwise, when you put the stone in the pool, the waves will never stop the concussion.
refraction of water:because the angle of inclination of different locations on the waves is different, so because of the refraction of water, the bottom we see vertically from the point of view is notThe point is directly below, and there is a certain offset. This is why we can feel the shape of the waves without considering the reflection of the light at the top of the water.
Reflection:Water waves are reflected when they encounter obstacles.
diffraction: If you can put a rock in the center of the pool, or a gap in the middle of the slit, then you can see the diffraction phenomenon of water waves


More useless, look at


This looks like a great one.

I'm going to define two wave buffers first.

int *M_PWAVEBUF1; //energy Wave buffer 1
int *m_pwavebuf2; //energy Wave buffer 2


In the initialization function, An array of the same size as the wave buffer pool image

Gets the bitmap width, height, and the number of bytes in a row m_ibmpwidth = Stbitmap.bmwidth;m_ibmpheight = stbitmap.bmheight;//allocating the energy buffer m_pwavebuf1 = new Int[m_ IBMPWIDTH*M_IBMPHEIGHT];M_PWAVEBUF2 = new Int[m_ibmpwidth*m_ibmpheight];



Wave diffusion function

void Cripple::wavespread () {int *lpwave1 = M_pwavebuf1;int *lpwave2 = m_pwavebuf2;for (int i = m_ibmpwidth; I < (m_iBmpHe ight-1) *m_ibmpwidth; i++) {//Wave wave diffusion lpwave2[i] = ((Lpwave1[i-1] + lpwave1[i-m_ibmpwidth] +   Lpwave1[i + 1] + lpwave1[i + m_ibmpwidth]) >& Gt 1)-lpwave2[i];//wave attenuation lpwave2[i]-= (Lpwave2[i] >> 5);} Swap buffer m_pwavebuf1 = LPWAVE2;M_PWAVEBUF2 = lpWave1;}


At a certain moment, the amplitude of the X0 point is affected by the amplitude of the X0 point itself, at the same time by the front, rear, left and right four points (X1, X2, X3, X4) from around it.

the impact. The influence of these four points on the A0 point can be said to be equal opportunity. Then we can assume that this one-time formula is:

X0 ' =a (x1+x2+x3+x4) +bx0 (equation 1)

Finally we get this function

X0 ' = (x1+x2+x3+x4)/2-x0

Water in practice is the existence of damping, otherwise, with the above formula, once you add a waves in the water, the water will never stop the shock. So

also need attenuation of the amplitude data, so that each point after a calculation, the amplitude is lower than the ideal value at a certain proportion. This attenuation rate passes through

test, with 1/32 more suitable, namely 1/2^5. Can be obtained quickly through the shift operation.


Render function:

In the program, the original image is loaded with one page, and another page is used to render it. Gets a pointer to the page memory area, and then uses the offset

The amount of shift copies each pixel on the original image onto the rendered page. For page rendering.


void Cripple::waverender () {int iptrsource = 0;int Iptrrender = 0;int Lineindex = m_ibmpwidth;int Iposx = 0;int IPosY = 0; Scans the bitmap for (int y = 1; y < m_ibmpheight-1; y++) {for (int x = 0; x < m_ibmpwidth, x + +) {//) to calculate the bitmap data offset value based on the amplitude, the render point (x, y) pair Should the original picture (Iposx,iposy)//Iposx = x + (M_pwavebuf1[lineindex-1]-m_pwavebuf1[lineindex + 1]);//Iposy = y + (M_pwavebuf1[lin Eindex-m_ibmpwidth]-m_pwavebuf1[lineindex + m_ibmpwidth]);//Another way to calculate offsets int wavedata = (1024-m_pwavebuf1[lineindex]) ; iposx = (X-M_IBMPWIDTH/2) *wavedata/1024 + M_ibmpwidth/2;iposy = (Y-M_IBMPHEIGHT/2) *wavedata/1024 + m_iBmpHeight/2;if ( 0 <= iposx && iposx < m_ibmpwidth && 0 <= iposy && iposy < M_ibmpheight) {//Compute the original bitmap separately ( Iposx,iposy) and the rendered bitmap (x, y) corresponding to the starting bitmap data Iptrsource = iposy*m_ibytesperwidth + Iposx*3;iptrrender = y*m_ibytesperwidth + x*3;//                        Render the bitmap, re-dot the data for (int c = 0; c < 3; C + +) {M_pbmprender[iptrrender + c] = M_pbmpsource[iptrsource + c];}}        lineindex++;}} SetBitmap SetDIBits (M_HRENDERDC, m_hrenderbmp, 0, M_ibmpheight, M_pbmprender, &m_stbitmapinfo, DIB_RGB_COLORS) after rendering;} 

Add waves: We have to add the waves to the pool, and you can imagine that putting stones into the water, the size and energy of the waves are related to the radius of the stone and the strength of the stone you throw. Knowing this, well, we just need to modify the Wave wave data buffer m_pwavebuf1, let it come to a negative "sharp pulse" at the point where the stones enter the water.
void Cripple::D ropstone (int x, int y, int stonesize, int stoneweight) {int PosX = 0;int PosY = 0;for (int i =-stonesize; I < stonesize; i++) {for (int j =-stonesize, J < Stonesize; J + +) {PosX = x + i;posy = y + j;//control range, cannot exceed picture if (PosX < 0 | | PosX >= M_ Ibmpwidth | |   PosY < 0 | | PosY >= m_ibmpheight) {continue;} In a circular region, initialize the energy buffer 1if (i*i + j*j <= stonesize*stonesize) {m_pwavebuf1[posy*m_ibmpwidth + PosX] = Stoneweight;}}}

This example is written by others, let's release this link, so that more people to learn to understand
Demo download

This article refer to http://blog.csdn.net/ghj1976/article/details/3071

Water ripple (water ripple)

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.