Skip to content

Commit

Permalink
Scene graph implementation (#986)
Browse files Browse the repository at this point in the history
* Adding Scene Graph Node class

* Camera now extends Node and can be used inside a Scene Graph

* WireframeObjects now extends Node and can be used inside a Scene Graph

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* ClippingPlane now extends Node and can be used inside a Scene Graph

* Unused variable deleted.

* Add function to transform the analytical plane equation to vectorial form.

* Switch off GPU information prints.

* Add a function to transform the vector plane representation into an analytical form. Apply global transformation to analytical plan equation.

* add GetClipPlane() function

* Add test for GetClipPlane, and implicitly, for fromAnalyticalToVectorial and fromVectorialToAnalytical

* Switch off context change when DestroyVBO is called

* Fix Sonar warnings.

* Opengl Context switching off

* Fix plane, local space transformation.

* Refactoring TranslateClipPlane().

* Add debug loging for QOpenGLMouseTracker::clear().

* Fix inaccessible  name variable.

* Fix parameter missing.

* Add TODO: Implement use case note with transform in global coordinates.

* Fix typo

* Changing the inheritance order s.t. the OpenGL context is destroyed after the GPU memory is deallocated.

* switch assert condition

* Fix OpenGL context distribution order.

* Fix rotationMatrixToEulerAngles() euler angle computation.

* Delete debug log print.

* Change debug log print format.

* Updating node removing method.

* Add comment for incomplete feature.

* Switch from .get() to -> while accesing shared ptr objects.

* Discarded unwanted comment.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
nashmit and pre-commit-ci[bot] committed May 15, 2024
1 parent 8039f4d commit 59fcfea
Show file tree
Hide file tree
Showing 16 changed files with 749 additions and 157 deletions.
5 changes: 3 additions & 2 deletions gui/rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ target_sources(
ShaderProgram.cpp
Utils.cpp
WireframeObjects.cpp
ClippingPlane.cpp)
ClippingPlane.cpp
Node.cpp)

target_include_directories(gui PUBLIC .)

if(BUILD_TESTING)
target_sources(gui_tests PUBLIC)
target_sources(gui_tests PUBLIC Node_t.cpp)
endif()
37 changes: 10 additions & 27 deletions gui/rendering/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,16 @@ void rendering::Camera::SetFrustum(GLfloat FOV, GLfloat width, GLfloat height,
rendering::Camera::Camera(GLfloat FOV, GLfloat width, GLfloat height,
GLfloat nearZ, GLfloat farZ, GLfloat posX,
GLfloat posY, GLfloat posZ, GLfloat rotX,
GLfloat rotY, GLfloat rotZ) {
SetFrustum(FOV, width, height, nearZ, farZ);

SetPosition(posX, posY, posZ);
SetRotation(rotX, rotY, rotZ);
}
GLfloat rotY, GLfloat rotZ)
: Node("Camera", QVector3D(posX, posY, posZ), QVector3D(rotX, rotY, rotZ),
QVector3D(1, 1, 1)) {

void rendering::Camera::SetPosition(GLfloat posX, GLfloat posY, GLfloat posZ) {
m_viewPosition.setX(posX);
m_viewPosition.setY(posY);
m_viewPosition.setZ(posZ);
}

void rendering::Camera::SetPosition(QVector3D position) {
SetPosition(position.x(), position.y(), position.z());
SetFrustum(FOV, width, height, nearZ, farZ);
}

QVector3D rendering::Camera::GetPosition() const { return m_viewPosition; }
void rendering::Camera::setRot(GLfloat rotX, GLfloat rotY, GLfloat rotZ) {

void rendering::Camera::SetRotation(GLfloat rotX, GLfloat rotY, GLfloat rotZ) {
m_viewRotation.setX(rotX);
m_viewRotation.setY(rotY);
m_viewRotation.setZ(rotZ);
Node::setRot(rotX, rotY, rotZ);

GLfloat _forwardX, _forwardY, _forwardZ;
GLfloat forwardX, forwardY, forwardZ;
Expand Down Expand Up @@ -112,12 +99,10 @@ void rendering::Camera::SetRotation(GLfloat rotX, GLfloat rotY, GLfloat rotZ) {
m_viewUp.setZ(upZ);
}

void rendering::Camera::SetRotation(QVector3D rotation) {
SetRotation(rotation.x(), rotation.y(), rotation.z());
void rendering::Camera::setRot(QVector3D rotation) {
setRot(rotation.x(), rotation.y(), rotation.z());
}

QVector3D rendering::Camera::GetRotation() const { return m_viewRotation; }

QVector3D rendering::Camera::GetForwardVector() const { return m_viewForward; }

QVector3D rendering::Camera::GetUpVector() const { return m_viewUp; }
Expand All @@ -129,10 +114,8 @@ void rendering::Camera::UpdateProjection(

void rendering::Camera::UpdateView(
std::unique_ptr<rendering::ShaderProgram> &program) const {
program->SetViewPosition(m_viewPosition.x(), m_viewPosition.y(),
m_viewPosition.z());
program->SetViewRotation(m_viewRotation.x(), m_viewRotation.y(),
m_viewRotation.z());
program->SetViewPosition(m_position.x(), m_position.y(), m_position.z());
program->SetViewRotation(m_rotation.x(), m_rotation.y(), m_rotation.z());
}

GLfloat rendering::Camera::getNear() const { return m_near; }
Expand Down
15 changes: 4 additions & 11 deletions gui/rendering/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

#include <math.h>

#include "Node.hpp"
#include "ShaderProgram.hpp"

namespace rendering {

constexpr float PI = 3.1415926535f;

class Camera {
class Camera : public Node {
public:
Camera(GLfloat FOV, GLfloat width, GLfloat height, GLfloat nearZ,
GLfloat farZ, GLfloat posX = 0.0f, GLfloat posY = 0.0f,
Expand All @@ -22,13 +23,8 @@ class Camera {
void SetFrustum(GLfloat FOV, GLfloat width, GLfloat height, GLfloat nearZ,
GLfloat farZ);

void SetPosition(GLfloat posX, GLfloat posY, GLfloat posZ);
void SetPosition(QVector3D position);
QVector3D GetPosition() const;

void SetRotation(GLfloat rotX, GLfloat rotY, GLfloat rotZ);
void SetRotation(QVector3D rotation);
QVector3D GetRotation() const;
void setRot(GLfloat rotX, GLfloat rotY, GLfloat rotZ) override;
void setRot(QVector3D rotation) override;

QVector3D GetForwardVector() const;
QVector3D GetUpVector() const;
Expand All @@ -50,9 +46,6 @@ class Camera {

GLfloat m_projectionMatrix[4][4];

QVector3D m_viewPosition;
QVector3D m_viewRotation;

QVector3D m_viewForward;
QVector3D m_viewUp;
};
Expand Down
55 changes: 48 additions & 7 deletions gui/rendering/ClippingPlane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include <QVector3D>

rendering::ClippingPlane::ClippingPlane(uint32_t planeIndex, bool active)
: m_planeIndex(planeIndex), m_active(active) {}
: Node("ClippingPlane " + std::to_string(planeIndex),
QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0f, 0.0f, 0.0f),
QVector3D(1.0f, 1.0f, 1.0f)),
m_planeIndex(planeIndex), m_active(active) {}

std::set<std::shared_ptr<rendering::ClippingPlane>>
rendering::ClippingPlane::BuildClippingPlanes() {
Expand All @@ -23,6 +26,29 @@ rendering::ClippingPlane::BuildClippingPlanes() {
return planes;
}

std::tuple<QVector3D, QVector3D>
rendering::ClippingPlane::fromAnalyticalToVectorial(float a, float b, float c,
float d) {

GLfloat length = QVector3D(a, b, c).length();
QVector3D normal(a / length, b / length, c / length);

GLfloat p = d / length;
QVector3D point(normal * p);

return {point, normal};
}

std::tuple<float, float, float, float>
rendering::ClippingPlane::fromVectorialToAnalytical(QVector3D position,
QVector3D direction) {

direction.normalize();

return {direction.x(), direction.y(), direction.z(),
-QVector3D::dotProduct(direction, position)};
}

void rendering::ClippingPlane::SetClipPlane(GLfloat a, GLfloat b, GLfloat c,
GLfloat d) {

Expand All @@ -41,13 +67,15 @@ void rendering::ClippingPlane::SetClipPlane(QVector3D normal,
-QVector3D::dotProduct(normal, point));
}

void rendering::ClippingPlane::TranslateClipPlane(GLfloat value) {
std::tuple<QVector3D, QVector3D>
rendering::ClippingPlane::GetClipPlane() const {

GLfloat length = QVector3D(m_a, m_b, m_c).length();
QVector3D normal(m_a / length, m_b / length, m_c / length);
return fromAnalyticalToVectorial(m_a, m_b, m_c, m_d);
}

GLfloat p = m_d / length;
QVector3D point(normal * p);
void rendering::ClippingPlane::TranslateAlongsideNormal(GLfloat value) {

auto [point, normal] = fromAnalyticalToVectorial(m_a, m_b, m_c, m_d);

point += normal * value;
SetClipPlane(normal, point);
Expand All @@ -59,9 +87,22 @@ bool rendering::ClippingPlane::getStatus() const { return m_active; }

void rendering::ClippingPlane::UpdateClipPlane(
std::unique_ptr<rendering::ShaderProgram> &program) const {

if (m_active) {

// assert(m_dirty == false);

auto [position, normal] = fromAnalyticalToVectorial(m_a, m_b, m_c, m_d);
DecomposedTransform globalTransform = getGlobalTransform();
QVector4D normalRotated =
globalTransform.rotation * QVector4D(normal, 1.0f);
QVector3D positionTranslated = globalTransform.position + position;

auto [a, b, c, d] = fromVectorialToAnalytical(positionTranslated,
normalRotated.toVector3D());

program->EnableClippingPlane(m_planeIndex);
program->SetClippingPlane(m_a, m_b, m_c, m_d, m_planeIndex);
program->SetClippingPlane(a, b, c, d, m_planeIndex);
} else {
program->DisableClippingPlane(m_planeIndex);
}
Expand Down
37 changes: 35 additions & 2 deletions gui/rendering/ClippingPlane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
#ifndef SPATIALMODELEDITOR_CLIPPLANE_H
#define SPATIALMODELEDITOR_CLIPPLANE_H

#include "Node.hpp"
#include "ShaderProgram.hpp"

#include <set>
#include <tuple>
#include <vector>

class QOpenGLMouseTracker;

namespace rendering {

class ClippingPlane {
class ClippingPlane : public Node {

public:
void
Expand All @@ -26,15 +29,45 @@ class ClippingPlane {
static std::set<std::shared_ptr<rendering::ClippingPlane>>
BuildClippingPlanes();

/**
* @brief It computes the position and the normal vector for a plane starting
* from the analytical equation.
*
* @param a
* @param b
* @param c
* @param d
* @return [Position, Normal]
*/
static std::tuple<QVector3D, QVector3D>
fromAnalyticalToVectorial(float a, float b, float c, float d);

/**
* @brief It computes the analytical equation, starting from position and
* direction.
* @param position
* @param direction ( Is is not necessary an unity vector )
* @return [a, b, c, d] -> plane parameters
*/
static std::tuple<float, float, float, float>
fromVectorialToAnalytical(QVector3D position, QVector3D direction);

void SetClipPlane(GLfloat a, GLfloat b, GLfloat c, GLfloat d);
void SetClipPlane(QVector3D normal, const QVector3D &point);

/**
* Return the vectorial form of the plane.
* @return [ Position, Normal ]
*/

std::tuple<QVector3D, QVector3D> GetClipPlane() const;

/**
* @brief: Translate the plane alongside the normal
*
* @param value how much it gets in translation
*/
void TranslateClipPlane(GLfloat value);
void TranslateAlongsideNormal(GLfloat value);

/**
* @brief: toggle between using and not the plane
Expand Down

0 comments on commit 59fcfea

Please sign in to comment.