Skip to content

Commit

Permalink
Add fluid simulation example
Browse files Browse the repository at this point in the history
  • Loading branch information
ttnghia committed Nov 2, 2019
1 parent 4a83ecb commit 57b3ed5
Show file tree
Hide file tree
Showing 21 changed files with 2,384 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -56,6 +56,7 @@ option(WITH_TRIANGLE_PLAIN_GLFW_EXAMPLE "Build Plain GLFW Triangle example" OFF)
option(WITH_TRIANGLE_SOKOL_EXAMPLE "Build sokol_gfx Triangle example" OFF)
option(WITH_TRIANGLE_VULKAN_EXAMPLE "Build Vulkan Triangle example" OFF)
option(WITH_VIEWER_EXAMPLE "Build Viewer example (requires the AnySceneImporter plugin)" OFF)
option(WITH_FLUIDSIMULATION_EXAMPLE "Build FluidSimulation example (requires the ImGui integration)" OFF)
if(CORRADE_TARGET_EMSCRIPTEN)
option(WITH_WEBVR_EXAMPLE "Build WebVR example" OFF)
endif()
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -125,3 +125,7 @@ endif()
if(WITH_WEBVR_EXAMPLE)
add_subdirectory(webvr)
endif()

if(WITH_FLUIDSIMULATION_EXAMPLE)
add_subdirectory(fluid-simulation)
endif()
76 changes: 76 additions & 0 deletions src/fluid-simulation/CMakeLists.txt
@@ -0,0 +1,76 @@
#
# This file is part of Magnum.
#
# Original authors — credit is appreciated but not required:
#
# 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
# Vladimír Vondruš <mosra@centrum.cz>
# 2019 — Nghia Truong <nghiatruong.vn@gmail.com>
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute
# this software, either in source code form or as a compiled binary, for any
# purpose, commercial or non-commercial, and by any means.
#
# In jurisdictions that recognize copyright laws, the author or authors of
# this software dedicate any and all copyright interest in the software to
# the public domain. We make this dedication for the benefit of the public
# at large and to the detriment of our heirs and successors. We intend this
# dedication to be an overt act of relinquishment in perpetuity of all
# present and future rights to this software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

cmake_minimum_required(VERSION 3.4)
project(magnum-fluidsimulation)

# Add module path in case this is project root
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../../modules/" ${CMAKE_MODULE_PATH})
endif()

############################################################
find_package(Corrade REQUIRED Main)
find_package(Magnum REQUIRED
GL
MeshTools
Primitives
SceneGraph
Shaders
Sdl2Application)
find_package(MagnumIntegration REQUIRED ImGui)

############################################################
set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON)
corrade_add_resource(FluidSimulation_RESOURCES Resources.conf)

############################################################
include_directories(.)
file(GLOB_RECURSE CPP_FILES *.cpp)

add_executable(${PROJECT_NAME} WIN32
${CPP_FILES}
${FluidSimulation_RESOURCES})

target_link_libraries(${PROJECT_NAME} PRIVATE
Corrade::Main
Magnum::Application
Magnum::GL
Magnum::Magnum
Magnum::MeshTools
Magnum::Primitives
Magnum::SceneGraph
Magnum::Shaders
MagnumIntegration::ImGui)

install(TARGETS magnum-fluidsimulation DESTINATION ${MAGNUM_BINARY_INSTALL_DIR})

# Make the executable a default target to build & run in Visual Studio
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT magnum-fluidsimulation)
66 changes: 66 additions & 0 deletions src/fluid-simulation/DrawableObjects/FlatShadeObject.h
@@ -0,0 +1,66 @@
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
Vladimír Vondruš <mosra@centrum.cz>
2019 — Nghia Truong <nghiatruong.vn@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

#include <Magnum/GL/Mesh.h>
#include <Magnum/Math/Color.h>
#include <Magnum/Shaders/Flat.h>
#include <Magnum/SceneGraph/Camera.h>
#include <Magnum/SceneGraph/Drawable.h>
#include <Magnum/SceneGraph/MatrixTransformation3D.h>

namespace Magnum { namespace Examples {
using Object3D = SceneGraph::Object<SceneGraph::MatrixTransformation3D>;

/****************************************************************************************************/
class FlatShadeObject : public SceneGraph::Drawable3D {
public:
explicit FlatShadeObject(Object3D& object, Shaders::Flat3D& shader,
const Color3& color, GL::Mesh& mesh,
SceneGraph::DrawableGroup3D* const drawables) :
SceneGraph::Drawable3D{object, drawables}, _shader(shader), _color(color), _mesh(mesh) {}

virtual void draw(const Matrix4& transformation, SceneGraph::Camera3D& camera) override {
_shader.setColor(_color)
.setTransformationProjectionMatrix(camera.projectionMatrix() * transformation);
_mesh.draw(_shader);
}

FlatShadeObject& setColor(const Color3& color) { _color = color; return *this; }

private:
Shaders::Flat3D& _shader;
Color3 _color;
GL::Mesh& _mesh;
};

/****************************************************************************************************/
} } /* namespace Magnum::Examples */
87 changes: 87 additions & 0 deletions src/fluid-simulation/DrawableObjects/ParticleGroup.cpp
@@ -0,0 +1,87 @@
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
Vladimír Vondruš <mosra@centrum.cz>
2019 — Nghia Truong <nghiatruong.vn@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "DrawableObjects/ParticleGroup.h"

#include <Corrade/Utility/Assert.h>
#include <Corrade/Containers/ArrayView.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/Math/Functions.h>
#include <Magnum/Shaders/Generic.h>
#include <Magnum/SceneGraph/Drawable.h>
#include <Magnum/Trade/MeshData3D.h>

namespace Magnum { namespace Examples {
/****************************************************************************************************/
ParticleGroup::ParticleGroup(const std::vector<Vector3>& points, float particleRadius) :
_points(points),
_particleRadius(particleRadius),
_meshParticles(GL::MeshPrimitive::Points) {
_meshParticles.addVertexBuffer(_bufferParticles, 0, Shaders::Generic3D::Position{});
_particleShader.reset(new ParticleSphereShader);
}

/****************************************************************************************************/
ParticleGroup& ParticleGroup::draw(Containers::Pointer<SceneGraph::Camera3D>& camera, const Vector2i& viewportSize) {
if(_points.empty()) { return *this; }

if(_bDirty) {
Containers::ArrayView<const float> data(reinterpret_cast<const float*>(&_points[0]), _points.size() * 3);
_bufferParticles.setData(data);
_meshParticles.setCount(static_cast<int>(_points.size()));
_bDirty = false;
}

using namespace Math::Literals;
(*_particleShader)
/* particle data */
.setNumParticles(static_cast<int>(_points.size()))
.setParticleRadius(_particleRadius)
/* sphere render data */
.setPointSizeScale(static_cast<float>(viewportSize.x()) /
Math::tan(22.5_degf)) /* tan(half field-of-view angle (45_deg)*/
.setColorMode(_colorMode)
.setAmbientColor(_ambientColor)
.setDiffuseColor(_diffuseColor)
.setSpecularColor(_specularColor)
.setShininess(_shininess)
/* view/prj matrices and light */
.setViewMatrix(camera->cameraMatrix())
.setProjectionMatrix(camera->projectionMatrix())
.setLightDirection(_lightDir);

glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
_meshParticles.draw(*_particleShader);

return *this;
}

/****************************************************************************************************/
} } /* namespace Magnum::Examples */
86 changes: 86 additions & 0 deletions src/fluid-simulation/DrawableObjects/ParticleGroup.h
@@ -0,0 +1,86 @@
/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
Vladimír Vondruš <mosra@centrum.cz>
2019 — Nghia Truong <nghiatruong.vn@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

#include "Shaders/ParticleSphereShader.h"

#include <Corrade/Containers/Pointer.h>
#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/Math/Color.h>
#include <Magnum/SceneGraph/Camera.h>

#include <vector>

namespace Magnum { namespace Examples {
/****************************************************************************************************/
class ParticleGroup {
public:
explicit ParticleGroup(const std::vector<Vector3>& points, float particleRadius);

ParticleGroup& draw(Containers::Pointer<SceneGraph::Camera3D>& camera, const Vector2i& viewportSize);

ParticleGroup& setDirty() { _bDirty = true; return *this; }
ParticleGroup& setParticleRadius(float radius) { _particleRadius = radius; return *this; }
ParticleGroup& setColorMode(int colorMode) { _colorMode = colorMode; return *this; }
ParticleGroup& setAmbientColor(const Color3& color) { _ambientColor = color; return *this; }
ParticleGroup& setDiffuseColor(const Color3& color) { _diffuseColor = color; return *this; }
ParticleGroup& setSpecularColor(const Color3& color) { _specularColor = color; return *this; }
ParticleGroup& setShininess(float shininess) { _shininess = shininess; return *this; }
ParticleGroup& setLightDirection(const Vector3& lightDir) { _lightDir = lightDir; return *this; }

int& colorMode() { return _colorMode; }
Color3& ambientColor() { return _ambientColor; }
Color3& diffuseColor() { return _diffuseColor; }
Color3& specularColor() { return _specularColor; }
float& shininess() { return _shininess; }
Vector3& lightDirection() { return _lightDir; }

private:
const std::vector<Vector3>& _points;
bool _bDirty { false };

float _particleRadius { 1.0f };
int _colorMode { static_cast<int>(ParticleSphereShader::ColorMode::RAMP_COLOR_BY_ID) };
Color3 _ambientColor { 0.1f };
Color3 _diffuseColor { 0.0f, 0.5f, 0.9f };
Color3 _specularColor { 1.0f };
float _shininess { 150.0f };
Vector3 _lightDir { 1.0f, 1.0f, 2.0f };

GL::Buffer _bufferParticles;
GL::Mesh _meshParticles;

Containers::Pointer<ParticleSphereShader> _particleShader;
};

/****************************************************************************************************/
} } /* namespace Magnum::Examples */

0 comments on commit 57b3ed5

Please sign in to comment.