Click here to Skip to main content
13,002,169 members (75,197 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends,

I am bit new to openGl And have a doubt which I am not able to clear out.

I am implementing an application with random colored circles popping up the random locations. The image of the application can be found

My requirement is that I should generate waves or ripples the location I click.

I had searched the same in codeproject and other sites but I am not getting the idea of implementing the same It will helpful if any one could tell me the basic logic or idea.

And another question when do we need to use texture in OpegGl?

Thanking you,
Posted 7-Mar-12 0:13am
Updated 7-Mar-12 0:15am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Answer 1:

Well, the way I'd go about it would be to create a flat grid. I'd make it fairly dense, so that the ripples would be nice and smooth. Perhaps I'd make the grid 128 x 128 vertices.

I would make sure that the verts all had texture co-ords assigned, such that the UVs ranged from 0,0(top-left) to 1,1(bot-right).

I would take a copy of whatever image is displayed in your window and make this the texture that I assigned to the grid.

At this point, you should have a flat (planar) 128 x 128 vertex grid, that had the client are of your screen-shot applied to it. At this stage, it would render no different on the screen than your screen-shot appears currently.

From there, I would create a couple of variables that would keep track of (a) the time of most recent-mouse-click and (b) the position of most recent mouse-click.

From there, it would be a simply matter of displacing the vertices of the grid in the Y plane (up-down), based on how long since the click was received and the position that it happened at. I'd most likely use a dampened version of the sine function to displace the verts. I'd dampen based on both distance from click position and time since it happened.

Whenever the user clicked the mouse, any previous values for these two variables would be re-assigned the current position and time.

Unfortunately, since we're only maintaining a single pair of variables for the last mouse-click, each time we clicked the mouse any/all current movement would be lost. This could easily be solved by maintaining a list of click-pos/click-time.

I strongly suggest that you take a look at the first tute on this page:[^]
It's a tute that deals with displacing such a texture-mapped grid with a simple sine wave. Just modify the displacement code to account for your time and position of last click.

Answer 2:
There's two times I can think of to rely on texture mapping in GL (or any other lib for that matter)
1) Because you can use an image that gives the same appearance with a small number of vertices that you may need to use a much larger number of vertices for without the map.
2) Because a texture map looks much better than a single solid/shaded colour.

Texture mapping when done by the gpu is very fast and (most often) serves as a good way to keep high image quality while maintaining a low vertex count.
abhishek.biradar 7-Mar-12 8:27am
Hi let me know if my guess is correct. Ok first I have to create a texture from the present window display(since I am not loading any image in the background) and then add the z axis displacement, redisplay the window with the texture created.

Please bare with me If am not making sense I m still a beginner with texture stuff!!
enhzflep 7-Mar-12 10:19am
Gday, Yes indeed - your guess is spot-on the money! It's been a while since I did anything with GL, though from memory it's the Y axis, not the Z (x=left/right, y=up/down, z=in/out of screen) - But that's a trivial matter. Unimportant for the discussion just now. Using textures is actually pretty easy - many people use TGAs since they're pretty easy to load. But all you really need is a byte(char) array of all of the uncompressed pixel data, along with the width & height of the image. You can use GetDibBits to get this data from a HBITMAP, you can also use GetObject on the HBITMAP of the HDC you're drawing to, in order to get the dimensions of the bitmap that will be taken from your client area. You could also maintain some vars that keep track of the size of the client portion of the window. It's a clearer implementation though if you query the size of the client area at the time you take the 'screen-shot', since you don't have to maintain variables that hold this for you. Variables which you then have to use in the screenShot function.
abhishek.biradar 8-Mar-12 0:37am
Thanks I didn't know the concept of converting the displayed buffer to texture and using it for generating waves. Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 8 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100