Skip to content

Commit

Permalink
BRAYNS-629 Refactor engine. (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien4193 committed Apr 26, 2024
1 parent 7eccac5 commit 95fe932
Show file tree
Hide file tree
Showing 40 changed files with 2,541 additions and 115 deletions.
4 changes: 2 additions & 2 deletions apps/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ int main(int argc, const char **argv)
}
catch (const std::exception &e)
{
brayns::Log::critical("Fatal error: '{}'.", e.what());
brayns::Log::fatal("Fatal error: '{}'.", e.what());
return 1;
}
catch (...)
{
brayns::Log::critical("Unknown fatal error.");
brayns::Log::fatal("Unknown fatal error.");
return 1;
}

Expand Down
14 changes: 0 additions & 14 deletions src/brayns/core/engine/components/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,4 @@

namespace brayns
{
TransformMatrix Transform::toMatrix() const noexcept
{
return TransformMatrix(translation, rotation, scale);
}

bool Transform::operator==(const Transform &other) const noexcept
{
return translation == other.translation && rotation == other.rotation && scale == other.scale;
}

bool Transform::operator!=(const Transform &other) const noexcept
{
return !(*this == other);
}
} // namespace brayns
14 changes: 0 additions & 14 deletions src/brayns/core/engine/components/Transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,4 @@

namespace brayns
{
/**
* @brief Defines the translation, rotation and scale parameters to be applied
* to a scene asset.
*/
struct Transform
{
Vector3f translation = Vector3f(0.f, 0.f, 0.f);
Quaternion rotation = Quaternion(1, 0, 0, 0);
Vector3f scale = Vector3f(1.f, 1.f, 1.f);

TransformMatrix toMatrix() const noexcept;
bool operator==(const Transform &other) const noexcept;
bool operator!=(const Transform &other) const noexcept;
};
} // namespace brayns
3 changes: 1 addition & 2 deletions src/brayns/core/engine/core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OsprayLogLevel
{
case brayns::LogLevel::Off:
return OSPLogLevel::OSP_LOG_NONE;
case brayns::LogLevel::Critical:
case brayns::LogLevel::Fatal:
case brayns::LogLevel::Error:
return OSPLogLevel::OSP_LOG_ERROR;
case brayns::LogLevel::Warn:
Expand All @@ -49,7 +49,6 @@ class OsprayLogLevel
return OSPLogLevel::OSP_LOG_INFO;
case brayns::LogLevel::Debug:
case brayns::LogLevel::Trace:
case brayns::LogLevel::Count:
return OSPLogLevel::OSP_LOG_DEBUG;
}

Expand Down
6 changes: 3 additions & 3 deletions src/brayns/core/engine/model/ModelInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ bool ModelInstance::commit()

TransformMatrix ModelInstance::_getFullTransform() const noexcept
{
auto matrix = _transform.toMatrix();
auto matrix = toAffine(_transform);

auto &components = _model->getComponents();
auto baseTransform = components.find<Transform>();
if (baseTransform)
{
matrix = matrix * baseTransform->toMatrix();
matrix = matrix * toAffine(*baseTransform);
}

return matrix;
return TransformMatrix(matrix);
}

void ModelInstance::_updateTransform()
Expand Down
55 changes: 55 additions & 0 deletions src/brayns/core/rendering/Camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "Camera.h"

namespace brayns::experimental
{
void Camera::setTransform(const Affine3 &transform)
{
setParam("transform", transform);
}

void Camera::setNearClip(float distance)
{
setParam("nearClip", distance);
}

void PerspectiveCamera::setFovy(float degrees)
{
setParam("fovy", degrees);
}

void PerspectiveCamera::setAspectRatio(float aspect)
{
setParam("aspect", aspect);
}

void OrthographicCamera::setHeight(float height)
{
setParam("height", height);
}

void OrthographicCamera::setAspectRatio(float aspect)
{
setParam("aspect", aspect);
}
}
65 changes: 65 additions & 0 deletions src/brayns/core/rendering/Camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include "Managed.h"

namespace brayns::experimental
{
class Camera : public Managed<OSPCamera>
{
public:
using Managed::Managed;

void setTransform(const Affine3 &transform);
void setNearClip(float distance);
};

class PerspectiveCamera : public Camera
{
public:
static inline const std::string name = "perspective";

using Camera::Camera;

void setFovy(float degrees);
void setAspectRatio(float aspect);
};

class OrthographicCamera : public Camera
{
public:
static inline const std::string name = "orthographic";

using Camera::Camera;

void setHeight(float height);
void setAspectRatio(float aspect);
};
}

namespace ospray
{
OSPTYPEFOR_SPECIALIZATION(brayns::experimental::Camera, OSP_CAMERA)
OSPTYPEFOR_SPECIALIZATION(brayns::experimental::PerspectiveCamera, OSP_CAMERA)
OSPTYPEFOR_SPECIALIZATION(brayns::experimental::OrthographicCamera, OSP_CAMERA)
}
38 changes: 38 additions & 0 deletions src/brayns/core/rendering/Data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <span>

#include <ospray/ospray_cpp.h>

namespace brayns::experimental
{
template<typename T>
using SharedArray = std::span<T>;

template<typename T>
ospray::cpp::SharedData toSharedData(SharedArray<T> data)
{
return ospray::cpp::SharedData(data.data(), data.size());
}
}
89 changes: 89 additions & 0 deletions src/brayns/core/rendering/Device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "Device.h"

namespace brayns::experimental
{
Device::Device(OSPDevice handle):
_handle(handle)
{
}

GeometricModel Device::createGeometricModel()
{
auto handle = ospNewGeometricModel();
return GeometricModel(handle);
}

VolumetricModel Device::createVolumetricModel()
{
auto handle = ospNewVolumetricModel();
return VolumetricModel(handle);
}

Group Device::createGroup()
{
auto handle = ospNewGroup();
return Group(handle);
}

Instance Device::createInstance()
{
auto handle = ospNewInstance();
return Instance(handle);
}

World Device::createWorld()
{
auto handle = ospNewWorld();
return World(handle);
}

Framebuffer Device::createFramebuffer(const FramebufferSettings &settings)
{
auto width = static_cast<int>(settings.width);
auto height = static_cast<int>(settings.height);
auto format = static_cast<OSPFrameBufferFormat>(settings.format);
auto channels = static_cast<std::uint32_t>(OSP_FB_NONE);
for (auto channel : settings.channels)
{
channels |= static_cast<OSPFrameBufferChannel>(channel);
}
auto handle = ospNewFrameBuffer(width, height, format, channels);
return Framebuffer(handle);
}

RenderTask Device::render(const RenderSettings &settings)
{
auto framebuffer = settings.framebuffer.getHandle();
auto renderer = settings.renderer.getHandle();
auto camera = settings.camera.getHandle();
auto world = settings.world.getHandle();
auto future = ospRenderFrame(framebuffer, renderer, camera, world);
return RenderTask(future);
}

void Device::Deleter::operator()(OSPDevice device) const
{
ospDeviceRelease(device);
}
}
Loading

0 comments on commit 95fe932

Please sign in to comment.