Skip to content

Commit

Permalink
Changes after 1st PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk Skowroński committed Jul 5, 2023
1 parent 7882ceb commit 52775f9
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 219 deletions.
4 changes: 0 additions & 4 deletions avogadro/rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ target_sources(Rendering PRIVATE
ambientocclusionspheregeometry.cpp
)

if(USE_3DCONNEXION AND (WIN32 OR APPLE))
target_sources(Rendering PRIVATE tdxextensions.cpp)
endif()

set(shader_files
"arrow_vs.glsl"
"cylinders_fs.glsl"
Expand Down
20 changes: 18 additions & 2 deletions avogadro/rendering/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Vector3f Camera::unProject(const Vector2f& point,
}

void Camera::calculatePerspective(float fieldOfView, float aspectRatio,
float zNear, float zFar)
float zNear, float zFar)
{
m_data->projection.setIdentity();
float f = 1.0f / std::tan(fieldOfView * float(M_PI) / 360.0f);
Expand All @@ -152,7 +152,7 @@ void Camera::calculatePerspective(float fieldOfView, float zNear, float zFar)
}

void Camera::calculateOrthographic(float left, float right, float bottom,
float top, float zNear, float zFar)
float top, float zNear, float zFar)
{
left *= m_orthographicScale;
right *= m_orthographicScale;
Expand Down Expand Up @@ -184,4 +184,20 @@ void Camera::setModelView(const Eigen::Affine3f& transform)
m_data->modelView = transform;
}

void Camera::calculatePerspective(float left, float right,
float bottom, float top,
float zNear, float zFar)
{
m_data->projection.setIdentity();

m_data->projection(0, 0) = (2.0f * zNear) / (right - left);
m_data->projection(1, 1) = (2.0f * zNear) / (top - bottom);
m_data->projection(0, 2) = (right + left) / (right - left);
m_data->projection(1, 2) = (top + bottom) / (top - bottom);
m_data->projection(2, 2) = -(zFar + zNear) / (zFar - zNear);
m_data->projection(3, 2) = -1.0f;
m_data->projection(2, 3) = -(2.0f * zFar * zNear) / (zFar - zNear);
m_data->projection(3, 3) = 0.0f;
}

} // namespace Avogadro
10 changes: 5 additions & 5 deletions avogadro/rendering/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AVOGADRORENDERING_EXPORT Camera
* reference point (defaults to the origin if nothing is supplied).
*/
Vector3f unProject(const Vector2f& point,
const Vector3f& reference = Vector3f::Zero()) const;
const Vector3f& reference = Vector3f::Zero()) const;

/**
* Calculate the perspective projection matrix.
Expand All @@ -110,7 +110,7 @@ class AVOGADRORENDERING_EXPORT Camera
* @param zFar is the distance from the viewer to the far clipping plane.
*/
void calculatePerspective(float fieldOfView, float aspectRatio, float zNear,
float zFar);
float zFar);

/**
* Calculate the perspective projection matrix. Computes the aspect ratio
Expand All @@ -120,7 +120,7 @@ class AVOGADRORENDERING_EXPORT Camera
* @param zFar is the distance from the viewer to the far clipping plane.
*/
void calculatePerspective(float fieldOfView, float zNear, float zFar);
#ifdef _3DCONNEXION

/**
* << API Extension for TDX >>
* Calculate the perspective projection matrix using frustum planes
Expand All @@ -134,7 +134,7 @@ class AVOGADRORENDERING_EXPORT Camera
*/
void calculatePerspective(float left, float right, float bottom, float top,
float zNear, float zFar);
#endif

/**
* Calculate the orthographic projection matrix.
* @param left left vertical clipping plane.
Expand All @@ -145,7 +145,7 @@ class AVOGADRORENDERING_EXPORT Camera
* @param zFar distance to the far clipping plane.
*/
void calculateOrthographic(float left, float right, float bottom, float top,
float zNear, float zFar);
float zNear, float zFar);

/**
* Set the dimensions of the viewport in pixels.
Expand Down
63 changes: 51 additions & 12 deletions avogadro/rendering/geometryvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include "curvegeometry.h"
#include "linestripgeometry.h"
#include "spheregeometry.h"
#ifdef _3DCONNEXION
#include "cylindergeometry.h"
#endif

namespace Avogadro::Rendering {

Expand All @@ -30,9 +27,6 @@ void GeometryVisitor::visit(Drawable&)

void GeometryVisitor::visit(SphereGeometry& geometry)
{
#ifdef _3DCONNEXION
m_sphereGeometries.push_back(geometry);
#endif
const Core::Array<SphereColor>& spheres = geometry.spheres();
if (!spheres.size())
return;
Expand All @@ -53,9 +47,7 @@ void GeometryVisitor::visit(SphereGeometry& geometry)
float distance = (it->center - tmpCenter).squaredNorm();
if (distance > tmpRadius)
tmpRadius = distance;
#ifdef _3DCONNEXION
m_spheres.push_back(*it);
#endif
}
}
tmpRadius = std::sqrt(tmpRadius);
Expand Down Expand Up @@ -154,11 +146,7 @@ void GeometryVisitor::clear()
m_dirty = false;
m_centers.clear();
m_radii.clear();
#ifdef _3DCONNEXION
m_spheres.clear();
m_sphereGeometries.clear();
m_cylinderGeometries.clear();
#endif
}

Vector3f GeometryVisitor::center()
Expand Down Expand Up @@ -201,4 +189,55 @@ void GeometryVisitor::average()
}
}

void GeometryVisitor::boundingBox(double& minX, double& minY, double& minZ,
double& maxX, double& maxY, double& maxZ,
const std::vector<bool>& flags) const
{
minX = std::numeric_limits<double>::max();
minY = minX;
minZ = minX;
maxX = -minX;
maxY = maxX;
maxZ = maxX;

bool noSelection = true;

for (uint32_t i = 0; i < flags.size(); i++) {
if (flags[i]) {
noSelection = false;
break;
}
}

for (uint32_t i = 0; i < m_spheres.size(); i++) {
if (flags.empty() || noSelection || flags[i]) {
float radius = m_spheres[i].radius + 0.5f;
double bufferMinX = m_spheres[i].center.x() - radius;
double bufferMinY = m_spheres[i].center.y() - radius;
double bufferMinZ = m_spheres[i].center.z() - radius;
double bufferMaxX = m_spheres[i].center.x() + radius;
double bufferMaxY = m_spheres[i].center.y() + radius;
double bufferMaxZ = m_spheres[i].center.z() + radius;

if (bufferMinX < minX)
minX = bufferMinX;

if (bufferMinY < minY)
minY = bufferMinY;

if (bufferMinZ < minZ)
minZ = bufferMinZ;

if (bufferMaxX > maxX)
maxX = bufferMaxX;

if (bufferMaxY > maxY)
maxY = bufferMaxY;

if (bufferMaxZ > maxZ)
maxZ = bufferMaxZ;
}
}
}

} // End namespace Avogadro
25 changes: 1 addition & 24 deletions avogadro/rendering/geometryvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ namespace Rendering {
* notably the center and radius of the bounding sphere.
*/

#ifdef _3DCONNEXION
struct SphereColor;
class SphereGeometry;
class CylinderGeometry;
#endif

class GeometryVisitor : public Visitor
{
Expand All @@ -47,11 +43,7 @@ class GeometryVisitor : public Visitor
void visit(SphereGeometry&) override;
void visit(AmbientOcclusionSphereGeometry&) override;
void visit(CurveGeometry&) override;
#ifdef _3DCONNEXION
void visit(CylinderGeometry&) override;
#else
void visit(CylinderGeometry&) override { return; }
#endif
void visit(MeshGeometry&) override { return; }
void visit(TextLabel2D&) override { return; }
void visit(TextLabel3D&) override { return; }
Expand All @@ -71,7 +63,7 @@ class GeometryVisitor : public Visitor
* Get the radius of the scene.
*/
float radius();
#ifdef _3DCONNEXION

/**
* <<API Extension for TDX>>
* Calculates the bounding box of the molecule.
Expand All @@ -88,17 +80,6 @@ class GeometryVisitor : public Visitor
double& maxY, double& maxZ,
const std::vector<bool>& flags) const;

/**
* <<API Extension for TDX>>
* Hit-tests underlying geometry.
* @param rayOrigin Origin of the ray.
* @param rayDirection Normalized direction of the ray.
* @return Distance to the intersection point lying on the passed ray.
* If returned value is less than zero, then there is no intersection.
*/
float hit(const Vector3f& rayOrigin, const Vector3f& rayDirection,
const float rayLength);
#endif
private:
/**
* Get the average of the accumulated spherical centers and minimal radius.
Expand All @@ -111,11 +92,7 @@ class GeometryVisitor : public Visitor

std::vector<Vector3f> m_centers;
std::vector<float> m_radii;
#ifdef _3DCONNEXION
std::vector<SphereColor> m_spheres;
std::vector<SphereGeometry> m_sphereGeometries;
std::vector<CylinderGeometry> m_cylinderGeometries;
#endif
};

} // End namespace Rendering
Expand Down
30 changes: 13 additions & 17 deletions avogadro/rendering/glrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GLRenderer::GLRenderer()
#endif
{
m_overlayCamera.setIdentity();
#ifdef _3DCONNEXION

float aspectRatio = static_cast<float>(m_camera.width()) /
static_cast<float>(m_camera.height());
float distance = m_camera.distance(m_center);
Expand All @@ -45,7 +45,7 @@ GLRenderer::GLRenderer()
m_orthographicFrustum = {
-5.0f * aspectRatio, 5.0f * aspectRatio, -5.0f, 5.0f, -offset, offset
};
#endif

}

GLRenderer::~GLRenderer()
Expand Down Expand Up @@ -204,26 +204,18 @@ void GLRenderer::setTextRenderStrategy(TextRenderStrategy* tren)
void GLRenderer::applyProjection()
{
float distance = m_camera.distance(m_center);
#ifdef _3DCONNEXION
float aspectRatio = static_cast<float>(m_camera.width()) /
static_cast<float>(m_camera.height());
#endif
if (m_camera.projectionType() == Perspective) {
#ifdef _3DCONNEXION
m_perspectiveFrustum[0] = m_perspectiveFrustum[2] * aspectRatio;
m_perspectiveFrustum[1] = m_perspectiveFrustum[3] * aspectRatio;
m_perspectiveFrustum[5] = distance + m_radius;
m_camera.calculatePerspective(
m_perspectiveFrustum[0], m_perspectiveFrustum[1], m_perspectiveFrustum[2],
m_perspectiveFrustum[3], m_perspectiveFrustum[4],
m_perspectiveFrustum[5]);
#else
m_camera.calculatePerspective(40.0f, std::max(2.0f, distance - m_radius),
distance + m_radius);
#endif
} else {
// Renders the orthographic projection of the molecule
#ifdef _3DCONNEXION
m_orthographicFrustum[0] = m_orthographicFrustum[2] * aspectRatio;
m_orthographicFrustum[1] = m_orthographicFrustum[3] * aspectRatio;
m_orthographicFrustum[5] = distance + m_radius;
Expand All @@ -234,13 +226,6 @@ void GLRenderer::applyProjection()
m_orthographicFrustum[3], // T
m_orthographicFrustum[4], // N
m_orthographicFrustum[5]); // F
#else
const double halfHeight = m_radius;
const double halfWidth = halfHeight * m_camera.width() / m_camera.height();
m_camera.calculateOrthographic(
-halfWidth, halfWidth, -halfHeight, halfHeight,
std::max(2.0f, distance - m_radius), distance + m_radius);
#endif
}
m_overlayCamera.calculateOrthographic(
0.f, static_cast<float>(m_overlayCamera.width()), 0.f,
Expand Down Expand Up @@ -320,6 +305,17 @@ Array<Identifier> GLRenderer::hits(const GroupNode* group,
return result;
}

float GLRenderer::hit(const Vector3f& rayOrigin,
const Vector3f& rayEnd,
const Vector3f& rayDirection) const
{
std::multimap<float, Identifier> results =
hits(&m_scene.rootNode(), rayOrigin, rayEnd, rayDirection);
if (results.size())
return results.begin()->first;
return std::numeric_limits<float>::max();
}

Array<Identifier> GLRenderer::hits(int x1, int y1, int x2, int y2) const
{
// Figure out where the corners of our rectangle are.
Expand Down
8 changes: 7 additions & 1 deletion avogadro/rendering/glrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class AVOGADRORENDERING_EXPORT GLRenderer
*/
Identifier hit(int x, int y) const;

/** Return the depth of provided ray - geometry hit test.
*/
float hit(const Vector3f& rayOrigin,
const Vector3f& rayEnd,
const Vector3f& rayDirection) const;

/** Return the primitives in the rectangular area provided. */
Core::Array<Identifier> hits(int x1, int y1, int x2, int y2) const;

Expand Down Expand Up @@ -104,9 +110,9 @@ class AVOGADRORENDERING_EXPORT GLRenderer
void setTextRenderStrategy(TextRenderStrategy* tren);
/** @} */

#ifdef _3DCONNEXION
std::array<float, 6> m_perspectiveFrustum; // L, R, B, T, N, F (planes order)
std::array<float, 6> m_orthographicFrustum; // L, R, B, T, N, F (planes order)
#ifdef _3DCONNEXION
bool m_drawIcon;
void* m_iconData;
uint32_t m_iconWidth;
Expand Down
10 changes: 10 additions & 0 deletions avogadro/rendering/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ void Scene::clear()
m_dirty = true;
}

void Scene::getBoundingBox(double& minX, double& minY, double& minZ,
double& maxX, double& maxY, double& maxZ,
const std::vector<bool>& flags)
{
GeometryVisitor visitor;

m_rootNode.accept(visitor);
visitor.boundingBox(minX, minY, minZ, maxX, maxY, maxZ, flags);
}

} // End Avogadro namespace
14 changes: 1 addition & 13 deletions avogadro/rendering/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class AVOGADRORENDERING_EXPORT Scene

/** Clear the scene of all elements. */
void clear();
#ifdef _3DCONNEXION

/**
* <<API Extension for TDX>>
* Calculte and return bounding box of the scene objects.
Expand All @@ -147,18 +147,6 @@ class AVOGADRORENDERING_EXPORT Scene
double& maxY, double& maxZ,
const std::vector<bool>& flags);

/** <<API Extension for TDX>>
* Return distance from ray origin to the ray with scene content instersection
* point.
* @param rayOrigin Origin of the ray.
* @param rayEnd End point of the ray.
* @param rayDirection Normalized direction of the ray.
* @return Distance to the intersection point lying on the passed ray. If
* returned value is less than zero, then there is no intersection.
*/
float getHitDistance(const Vector3f& rayOrigin, const Vector3f& rayDirection,
const float rayLength);
#endif
private:
GroupNode m_rootNode;
Vector4ub m_backgroundColor;
Expand Down
Loading

0 comments on commit 52775f9

Please sign in to comment.