载入中...
搜索中...
未找到
VolumeFluidShadowCaster.cpp
浏览该文件的文档.
2
3#include "graphics/Graphics.h"
4#include "graphics/Mesh.h"
6
7#include <exception>
8#include <glm/gtc/quaternion.hpp>
9
10namespace eve::fluids {
11namespace {
12Status failure(const char* message) {
13 return Status::failure(Diagnostic::error(DiagnosticCode::Failed, message, "fluids.volume.shadowCaster"));
14}
15} // namespace
16
18
21 if (graphics_ && mesh_ && graphics_->releaseMesh(mesh_)) delete mesh_;
22}
23
24Result<void> VolumeFluidShadowCaster::update(const VolumeFluid& fluid, unsigned actorGroup, float radiusScale,
25 float alpha, unsigned maxParticles) {
26 std::vector<VolumeFluidParticleInstance> candidate;
27 candidate.swap(instances_);
28 auto copied = fluid.copyParticleImpostors(actorGroup, radiusScale, glm::vec4(1.f), alpha, maxParticles, candidate);
29 if (!copied) {
30 candidate.swap(instances_);
31 return Result<void>::failure(copied.status());
32 }
33 const size_t count = candidate.size();
34 positions_.resize(count * 18u);
35 normals_.resize(count * 18u);
36 indices_.resize(count * 24u);
37 static constexpr glm::vec3 directions[6] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
38 static constexpr uint32_t faces[24] = {0, 2, 4, 2, 1, 4, 1, 3, 4, 3, 0, 4, 2, 0, 5, 1, 2, 5, 3, 1, 5, 0, 3, 5};
39 for (size_t i = 0; i < count; ++i) {
40 const auto q = glm::quat(candidate[i].orientation.w, candidate[i].orientation.x, candidate[i].orientation.y,
41 candidate[i].orientation.z);
42 for (size_t v = 0; v < 6; ++v) {
43 const glm::vec3 local = directions[v] * candidate[i].scale;
44 const glm::vec3 normal = q * directions[v];
45 const glm::vec3 point = candidate[i].position + q * local;
46 for (size_t c = 0; c < 3; ++c) {
47 const auto component = static_cast<glm::vec3::length_type>(c);
48 positions_[(i * 6 + v) * 3 + c] = point[component];
49 normals_[(i * 6 + v) * 3 + c] = normal[component];
50 }
51 }
52 for (size_t k = 0; k < 24; ++k) indices_[i * 24 + k] = uint32_t(i * 6 + faces[k]);
53 }
54 try {
55 if (count != 0 && !mesh_)
56 mesh_ = graphics_->newMeshFromArrays(positions_.data(), normals_.data(), nullptr, int(count * 6),
57 indices_.data(), int(count * 24));
58 else if (count != 0 && !graphics_->updateMeshVertices(mesh_, positions_.data(), normals_.data(), nullptr,
59 int(count * 6), indices_.data(), int(count * 24)))
60 return Result<void>::failure(failure("Particle shadow mesh update failed"));
61 } catch (const std::exception&) {
62 return Result<void>::failure(failure("Particle shadow mesh creation failed"));
63 }
64 if (mesh_ && drawerToken_ == 0)
66 [this](graphics::Graphics& gfx, const glm::mat4& lightVP, const auto&) {
67 if (particleCount_ != 0) gfx.drawMeshShadow(mesh_, lightVP);
68 });
69 instances_.swap(candidate);
70 particleCount_ = unsigned(count);
71 return Result<void>::success();
72}
73
74} // namespace eve::fluids
std::string message
std::array< double, 10 > q
float v
std::int32_t c
std::string local
Texture * normal
size_t directions
Definition OnnxLstm.cpp:29
glm::mat4 lightVP
std::uint32_t count
glm::vec3 point
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status failure(StatusCode code, Diagnostic diagnostic)
Construct a failed status with one diagnostic.
Definition Status.h:84
Result< void > update(const VolumeFluid &fluid, unsigned actorGroup, float radiusScale, float alpha=1.f, unsigned maxParticles=4096)
Rebuild one actor's bounded octahedral shadow batch.
VolumeFluidShadowCaster(graphics::Graphics &graphics)
Constructs a VolumeFluidShadowCaster.
~VolumeFluidShadowCaster()
Releases VolumeFluidShadowCaster resources.
CPU position-based free-volume fluid with a bounded spatial grid. @ownership Owns all particle state;...
Result< void > copyParticleImpostors(unsigned actorGroup, float radiusScale, glm::vec4 tint, float alpha, unsigned maxInstances, std::vector< VolumeFluidParticleInstance > &instances) const
Writes Fluid3DParticleRenderer-style instances into caller-owned reusable storage.
virtual bool releaseMesh(Mesh *mesh)
Eagerly releases a mesh created by this Graphics.
Definition Graphics.h:927
virtual void drawMeshShadow(Mesh *mesh, const glm::mat4 &lightMVP, bool doubleSided=true)=0
Queue an opaque shadow caster.
virtual bool updateMeshVertices(Mesh *mesh, const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
In-place update of a mesh's vertex/index data (CPU -> host-visible VBO). Mirrors bakeMeshMorph: the u...
virtual Mesh * newMeshFromArrays(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
Upload a triangle mesh from packed CPU arrays. Owned by Graphics. posXYZ required (vertexCount*3)....
static void removeShadowExtraDrawer(uint64_t token)
Remove one shadow callback before destroying its captured state.
static uint64_t addShadowExtraDrawer(ShadowExtraDrawer drawer)
Adds shadow extra drawer.
eve::StatusCode Status
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12