• Stars
    star
    149
  • Rank 247,959 (Top 5 %)
  • Language
    C
  • License
    ISC License
  • Created about 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Water-Simulation with real time specular reflection on the waters surface. The reflection is implemented in GLSL and runs on the GPU and in screen space. The water itself is implemented using a pressure based approach for the surface calculation.

Water-Simulation with Realtime Reflections/Refractions from the Surface

This project consists of several smaller projects joined together. Everything is done completely from scratch. No external libraries or dependencies (except basic OpenGL) are needed to run this simulation.

The final result looks like that:

Water-Simulation

Water Simulation

The water simulation itself is a pressure based height field.

The water's surface is divided into a small grid of points. Each height represents the pressure at that point. For each time step the pressure is divided and continued on to the next water point. The calculation itself is similar to image processing calculations, where a kernel over each pixel calculates the color at that pixel.

A height field water simulation looks very real for smaller waves and distorted water surfaces. But because of using a height-field (real time efficiency and simplicity to implement) there is no possibility of calculating single drops or any kind of detachment of waterbodies from the surface. Also no overlapping waves or such things are possible.

Example with random displacement added to the water's surface, creating very small waves (distortion):

Water-Distortion

Example with some small spreading waves:

Spreading waves

Example with waves bouncing back from the shore:

Bouncing waves

Reflections / Refractions (Screenspace)

Both the reflection and refraction of the water's surface is calculated in screen space with OpenGL fragment shaders.

The main benefit of using screenspace reflections/refractions is, that it takes a constant time to calculate and doesn't depend on the complexity of the scene. Another benefit is, that both the reflection and refraction can be calculated using the exact same calculations.

  • For each frame, two textures (color-texture and depth-texture) are created from the point of view and provided to the OpenGL fragment shader.

  • For each pixel, the position of each reflection/refraction-point is provided in world-coordinates to the fragment shader.

  • A step-size is determined (could also be constant).

  • For each step, the reflection-vector is scaled with the determinded step-size and transformed into screenspace. We now have two vectors. One is the reflection-vector in world coordinates and the same one transformed into screen space.

  • Because we look at the same scene which is saved into the texture, we can now sample the depth-texture and get the depth of the scene at the point of the scaled reflection-vector.

  • We continue the scaling of the reflection-vector until the depth of the sampled depth-texture is smaller than the z-coordinate (depth) of the reflection-vector in world coordinates. In this case we know, that we hit something which should be displayed as the reflection for the original fragment.

  • We can then take the transformed reflection-vector and sample the color texture. The result should be the color of the reflection. We take that color and are finished.

Positive features: Works in real time looks good most of the time.

Negative features: Actually takes longer to calculate, if there are no reflections, is not always correct, not defined for situations where the reflection goes outside the screenspace and can only reflect objects which are on the screen! (Also there are incorrect reflections if there is an object between the camera and the water's surface.)

Water refraction:

Refraction

Reflection and refraction with no waves at all:

No Wave-Reflection

Refraction and Reflection of smaller waves from above:

Small waves

Terrain Modelling

The terrain is modelled using a 2D-Perlin-Noise-function (implemented from scratch!). It overlays different frequences of sinus and cosinus waves. The perlin-noise height field is then textured based on the current height (Overlapping of different textures). The mountain like hill is added by adding a gauss function to the perlin-noise output. The same goes for the valley where the water is.

Install && Run

I only tested and ran this simulation on a debian-based Linux OS (Ubuntu, Mint, ...). It should run on other machines as well but is not tested.

Requirements

The following system-attributes are required for running this simulation:

  • A graphics card supporting OpenGL version 3.3 (For the shaders).

  • Unix-Libraries: xorg-dev, freeglut3-dev and mesa-common-dev

Running

Compiling and running is pretty straight forward.

  • make

  • ./water

While the simulation runs, you can move around (always looking to the center!) with your mouse (left-klick and move).

Initializing a random movement (choppy water) is done with 'r'.

Creating one/several waves can be done by pressing 'n'.

More Repositories

1

Delaunay_Triangulation

My own implementation of a Delaunay triangulation and Voronoi partition in Python applied to images.
Python
255
star
2

Partikel_accelleration_on_GPU

Particle accelleration with OpenGL 4.3, using the compute shader to calculate particle movement on graphics hardware.
C
253
star
3

skiplist

A Go library for an efficient implementation of a skip list: https://godoc.org/github.com/MauriceGit/skiplist
Go
252
star
4

compiler

Compiler for a small language into x86-64 Assembly
Go
243
star
5

Simple_GLSL_Shader_Example

A very simple example of how shaders in OpenGL can be used, to color Objects or map a texture on some triangles.
C
239
star
6

Voronoi_Image_Manipulation

A system independent tool for interactive image manipulation with Voronoi and Delaunay data structures.
Go
227
star
7

XBox_Controller_Linux_Interface

An interface that interacts with an XBox One controller via the usb stream. With simple methods for object or camera control (i.e. for OpenGL contexts).
C
132
star
8

Cloth_Simulation

Cloth-Visualization via particle-simulation.
C
83
star
9

Advanced_Algorithms

Quick implementations of some advanced algorithms for searching, sorting and trees
Python
77
star
10

Screen_Space_Ambient_Occlusion

Giving a scene a depth-dependent shading, completely irregarding the scene complexity, in screen-space.
C
47
star
11

Quaternion_Library

A small library that capsulates most commonly used operations on Quaternions. Also a small sample implementation, that rotates an object around an axis, using Quaternions.
C
40
star
12

Energy-Dome_Terrain

A visually appealing terrain visualization from real-world data with some extras, such as an animated energy dome, LOD tessellation and multisampling
Go
29
star
13

advsearch

A Go library for advanced searching in sorted data structures
Go
22
star
14

Repeat_History

A small but useful command for a linux shell. It makes the bash history more easily accessible than cmd+r.
Shell
19
star
15

Marching_Cubes_on_GPU

A marching cube algorithm, that is executed in parallel on the GPU, using compute shaders. This will later enable a highly parallel creation of advanced landscape/terrain structures in potentially real-time (next project).
Go
9
star
16

tree23

An implementation of a balanced 2,3-tree that allows accessing next/previous elements in O(1) at all times.
Go
8
star
17

Vector_Library

Small vector library in C, containing all basic vector operations.
C
4
star
18

2D_Bin_Packing

several parallel solutions for a 2D bin packing problem for a programming contest.
Python
2
star
19

Variance_Shadow_Maps

Small example of a project, that uses variance shadow maps with hardware Multisampling.
Go
2
star
20

Voronoi_DivideAndConquer

A Go implementation of a voronoi tessellation with the divide and conquer algorithm. It is still work in progress and not yet working properly!
Go
2
star
21

mtVector

A short vector library with all necessary operations for a Delaunay triangulation, including optimized matrix multiplication
Go
1
star
22

game

JavaScript
1
star
23

Agar_Clone_Joystick_Client

A C++ client for our programming challenge (2016) supporting an X-BOX-Controller.
C++
1
star
24

FastDelaunayImages

Go
1
star
25

AdventOfCode

My solutions for the advent of code puzzles (AOC)
Python
1
star
26

Artificial_Neural_Network_Character_Classification

Implementation of an artificial neural network with backpropagation for classification of characters.
Python
1
star
27

Bachelor_Thesis_Arimaa

Implementation of the practical part of my bachelor thesis for the FH-Wedel. The thesis has been awarded with the "Wedeler Hochschulpreis" for innovation. The code implements an artificial intelligence which plays the game of Arimaa.
Java
1
star