Skip to content

Commit

Permalink
adds sun calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
GeraldKimmersdorfer committed Nov 1, 2023
1 parent c2c3c35 commit 65cc4ff
Show file tree
Hide file tree
Showing 14 changed files with 1,851 additions and 52 deletions.
3 changes: 3 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ qt_add_qml_module(alpineapp
components/DrawerSeparator.qml
components/DrawerSpacer.qml
components/VectorEditor.qml
components/DatePicker.qml
components/DateMonthTablePicker.qml
components/SunAnglePicker.qml
RESOURCES
icons/camera_operation_centre.svg
icons/compass.svg
Expand Down
71 changes: 71 additions & 0 deletions app/GeneralSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,84 @@ SetPanel {
lod_slider.value = map.render_quality
fov_slider.value = map.field_of_view
cache_size_slider.value = map.tile_cache_size

map.frame_limit = Qt.binding(function() { return frame_rate_slider.value })
map.render_quality = Qt.binding(function() { return lod_slider.value })
map.field_of_view = Qt.binding(function() { return fov_slider.value })
map.tile_cache_size = Qt.binding(function() { return cache_size_slider.value })
datetimegroup.initializePropertys();
responsive_update()
}

SetGroup {
name: qsTr("Date and Time")
id: datetimegroup

property bool initialized: false;

function initializePropertys() {
var jdt = new Date(map.selected_datetime);
currentTime.value = jdt.getHours() + jdt.getMinutes() / 60;
currentDate.selectedDate = jdt;
initialized = true;
}

function updateMapDateTimeProperty() {
if (!initialized) return;
let jsDate = currentDate.selectedDate;
jsDate.setHours(currentTime.hours);
jsDate.setMinutes(currentTime.minutes);
map.selected_datetime = jsDate;
}

Label { text: qsTr("Date:") }
DatePicker {
id: currentDate;
onSelectedDateChanged: {
datetimegroup.updateMapDateTimeProperty();
}
}

Label { text: qsTr("Time:") }
ValSlider {
property int hours;
property int minutes;
id: currentTime;
from: 0.0; to: 24.0; stepSize: 1 / (60 / 15); // in 15 min steps
onMoved: {
//console.log("because of Time");
datetimegroup.updateMapDateTimeProperty();
}
formatCallback: function (value) {
let h = parseInt(value);
hours = h;
let m = parseInt((value - h) * 60);
minutes = m;
return String(h).padStart(2, '0') + ":" + String(m).padStart(2, '0');
}
}

CheckBox {
id: link_gl_settings;
text: "Link GL Sun Configuration"
Layout.fillWidth: true;
Layout.columnSpan: 2;
checked: map.link_gl_sundirection;
onCheckStateChanged: map.link_gl_sundirection = this.checked;
}

Label { text: qsTr("Sun Angles:"); visible: map.link_gl_sundirection; }
Label {
visible: map.link_gl_sundirection;
text: {
return "Az(" + map.sun_angles.x.toFixed(2) + "°) , Ze(" + map.sun_angles.y.toFixed(2) + "°)";
}
}



}

SetGroup {
name: qsTr("Camera")
Label { text: qsTr("Field of view:") }
Expand Down
53 changes: 4 additions & 49 deletions app/GlSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -185,55 +185,10 @@ SetPanel {
}

Label { text: "Light-Direction:" }
RowLayout {
function update_sun_position() {
let phi = sun_phi.value * Math.PI / 180.0;
let theta = sun_theta.value * Math.PI / 180.0;
let dir = Qt.vector3d(-Math.sin(theta) * Math.cos(phi), -Math.sin(theta) * Math.sin(phi), -Math.cos(theta));
dir = dir.normalized();
map.shared_config.sun_light_dir = Qt.vector4d(dir.x, dir.y, dir.z, 1.0);
}
Dial {
id: sun_phi
implicitHeight: 75
implicitWidth: 70
stepSize: 5
value: 135
wrap: true
onValueChanged: {
children[0].text = value + "°";
parent.update_sun_position();
}
from: 0
to: 360
snapMode: Dial.SnapOnRelease
Label {
x: parent.width / 2 - this.width / 2
y: parent.height / 2 - this.height / 2
text: parent.value + "°"
}
}
Slider {
id: sun_theta
from: -110
to: 110
implicitHeight: 75
orientation: Qt.Vertical
value: 45
onValueChanged: {
children[0].text = value + "°";
parent.update_sun_position();
}
snapMode: Slider.SnapOnRelease
stepSize: 5
Label {
x: 40
y: parent.height / 2 - this.height / 2
text: parent.value + "°"
}

}
Component.onCompleted: update_sun_position()
SunAnglePicker {
sun_angles: map.sun_angles
onSun_anglesChanged: map.sun_angles = sun_angles;
enabled: !map.link_gl_sundirection
}

Label { text: "Mat.-Color:" }
Expand Down
61 changes: 61 additions & 0 deletions app/TerrainRendererItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@
#include <QThread>
#include <QTimer>

#include <glm/glm.hpp>
#include <glm/gtx/string_cast.hpp>

#include "RenderThreadNotifier.h"
#include "TerrainRenderer.h"
#include "gl_engine/Window.h"
#include "nucleus/camera/Controller.h"
#include "nucleus/Controller.h"
#include <nucleus/tile_scheduler/Scheduler.h>

#include "nucleus/srs.h" // for calculation of lat long from cam pos
#include "nucleus/utils/sun_calculations.h"
#include "nucleus/camera/PositionStorage.h"
#include "nucleus/utils/UrlModifier.h"

Expand Down Expand Up @@ -76,6 +81,8 @@ TerrainRendererItem::TerrainRendererItem(QQuickItem* parent)
set_shared_config(tmp);
}

selected_datetime_changed(QDateTime());

connect(m_update_timer, &QTimer::timeout, this, [this]() {
emit update_camera_requested();
RenderThreadNotifier::instance()->notify();
Expand Down Expand Up @@ -109,6 +116,10 @@ QQuickFramebufferObject::Renderer* TerrainRendererItem::createRenderer() const
&TerrainRendererItem::rotation_north_requested,
r->controller()->camera_controller(),
&nucleus::camera::Controller::rotate_north);

// Connect definition change to aquire camera position for sun angle calculation
connect(r->controller()->camera_controller(), &nucleus::camera::Controller::definition_changed, this, &TerrainRendererItem::camera_definition_changed);

connect(this, &TerrainRendererItem::camera_definition_set_by_user, r->controller()->camera_controller(), &nucleus::camera::Controller::set_definition);
connect(r->controller()->camera_controller(), &nucleus::camera::Controller::global_cursor_position_changed, this, &TerrainRendererItem::read_global_position);
//connect(this, &TerrainRendererItem::ind)
Expand Down Expand Up @@ -198,6 +209,12 @@ void TerrainRendererItem::read_global_position(glm::dvec3 latlonalt) {
emit gui_update_global_cursor_pos(latlonalt.x, latlonalt.y, latlonalt.z);
}

void TerrainRendererItem::camera_definition_changed(const nucleus::camera::Definition& new_definition)
{
m_last_camera_latlonalt = nucleus::srs::world_to_lat_long_alt(new_definition.position());
recalculate_sun_angles();
}

void TerrainRendererItem::set_position(double latitude, double longitude)
{
emit position_set_by_user(latitude, longitude);
Expand Down Expand Up @@ -442,3 +459,47 @@ void TerrainRendererItem::set_tile_cache_size(unsigned int new_tile_cache_size)
m_tile_cache_size = new_tile_cache_size;
emit tile_cache_size_changed(m_tile_cache_size);
}

QDateTime TerrainRendererItem::selected_datetime() const
{
return m_selected_datetime;
}

void TerrainRendererItem::set_selected_datetime(QDateTime new_datetime)
{
if (m_selected_datetime != new_datetime) {
m_selected_datetime = new_datetime;
emit selected_datetime_changed(m_selected_datetime);
recalculate_sun_angles();
}
}

QVector2D TerrainRendererItem::sun_angles() const {
return m_sun_angles;
}

void TerrainRendererItem::set_sun_angles(QVector2D new_sunAngles) {
if (new_sunAngles == m_sun_angles) return;
m_sun_angles = new_sunAngles;
emit sun_angles_changed(m_sun_angles);

// Update the direction inside the gls shared_config
auto newDir = nucleus::utils::sun_calculations::sun_rays_direction_from_sun_angles(glm::vec2(m_sun_angles.x(), m_sun_angles.y()));
QVector4D newDirUboEntry(newDir.x, newDir.y, newDir.z, m_shared_config.m_sun_light_dir.w());
m_shared_config.m_sun_light_dir = newDirUboEntry;
emit shared_config_changed(m_shared_config);
}

void TerrainRendererItem::set_link_gl_sundirection(bool newValue) {
if (newValue == m_link_gl_sundirection) return;
m_link_gl_sundirection = newValue;
emit link_gl_sundirection_changed(m_link_gl_sundirection);
}

void TerrainRendererItem::recalculate_sun_angles() {
// Calculate sun angles
if (m_link_gl_sundirection) {
auto angles = nucleus::utils::sun_calculations::calculate_sun_angles(m_selected_datetime, m_last_camera_latlonalt);
set_sun_angles(QVector2D(angles.x, angles.y));
}
}
24 changes: 24 additions & 0 deletions app/TerrainRendererItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <QList>
#include <QString>
#include <QVector3D>
#include <QVector2D>
#include <QDateTime>
#include <map>

class TerrainRendererItem : public QQuickFramebufferObject {
Expand All @@ -54,6 +56,9 @@ class TerrainRendererItem : public QQuickFramebufferObject {
Q_PROPERTY(bool render_looped READ render_looped WRITE set_render_looped NOTIFY render_looped_changed)
Q_PROPERTY(unsigned int selected_camera_position_index MEMBER m_selected_camera_position_index WRITE set_selected_camera_position_index)
Q_PROPERTY(bool hud_visible READ hud_visible WRITE set_hud_visible NOTIFY hud_visible_changed)
Q_PROPERTY(QDateTime selected_datetime READ selected_datetime WRITE set_selected_datetime NOTIFY selected_datetime_changed)
Q_PROPERTY(QVector2D sun_angles READ sun_angles WRITE set_sun_angles NOTIFY sun_angles_changed)
Q_PROPERTY(bool link_gl_sundirection READ link_gl_sundirection WRITE set_link_gl_sundirection NOTIFY link_gl_sundirection_changed)

public:
explicit TerrainRendererItem(QQuickItem* parent = 0);
Expand Down Expand Up @@ -100,6 +105,9 @@ class TerrainRendererItem : public QQuickFramebufferObject {

void gui_update_global_cursor_pos(double latitude, double longitude, double altitude);

void selected_datetime_changed(QDateTime newDateTime);
void sun_angles_changed(QVector2D newSunAngles);
void link_gl_sundirection_changed(bool newValue);

protected:
void touchEvent(QTouchEvent*) override;
Expand All @@ -113,6 +121,7 @@ public slots:
void set_position(double latitude, double longitude);
void rotate_north();
void read_global_position(glm::dvec3 latlonalt);
void camera_definition_changed(const nucleus::camera::Definition& new_definition); // gets called whenever camera changes

private slots:
void schedule_update();
Expand Down Expand Up @@ -172,7 +181,18 @@ private slots:
[[nodiscard]] unsigned int tile_cache_size() const;
void set_tile_cache_size(unsigned int new_tile_cache_size);

QDateTime selected_datetime() const;
void set_selected_datetime(QDateTime new_datetime);

QVector2D sun_angles() const;
void set_sun_angles(QVector2D new_sunAngles);

bool link_gl_sundirection() const { return m_link_gl_sundirection; }
void set_link_gl_sundirection(bool newValue);

private:
void recalculate_sun_angles();

float m_camera_rotation_from_north = 0;
QPointF m_camera_operation_centre;
bool m_camera_operation_centre_visibility = false;
Expand All @@ -187,6 +207,8 @@ private slots:
unsigned int m_selected_camera_position_index = 0;
bool m_render_looped = false;
bool m_hud_visible = true;
bool m_link_gl_sundirection = true;
QDateTime m_selected_datetime = QDateTime::currentDateTime();

gl_engine::uboSharedConfig m_shared_config;

Expand All @@ -197,6 +219,8 @@ private slots:
int m_camera_height = 0;

TimerFrontendManager* m_timer_manager;
QVector2D m_sun_angles; // azimuth and zenith
glm::dvec3 m_last_camera_latlonalt;
};

#endif // TERRAINRENDERERITEM_H
Loading

0 comments on commit 65cc4ff

Please sign in to comment.