openblocks/editor/mainglwidget.cpp

145 lines
4.2 KiB
C++
Raw Normal View History

2024-12-01 12:08:45 +00:00
#include <GL/glew.h>
#include <chrono>
#include <QMouseEvent>
2025-02-01 20:15:53 +00:00
#include <glm/ext/matrix_projection.hpp>
#include <glm/ext/vector_float3.hpp>
#include <glm/geometric.hpp>
#include <glm/matrix.hpp>
#include <reactphysics3d/collision/RaycastInfo.h>
2025-02-01 21:55:07 +00:00
#include <vector>
2024-12-01 11:26:14 +00:00
2024-12-01 23:14:42 +00:00
#include "GLFW/glfw3.h"
2025-02-01 21:55:07 +00:00
#include "physics/util.h"
2024-12-01 23:14:42 +00:00
#include "qcursor.h"
2024-12-01 12:08:45 +00:00
#include "qevent.h"
#include "qnamespace.h"
2025-02-01 20:15:53 +00:00
#include "qwindowdefs.h"
2024-12-01 11:26:14 +00:00
#include "rendering/renderer.h"
#include "physics/simulation.h"
#include "camera.h"
#include "common.h"
#include "mainglwidget.h"
2024-12-02 10:01:51 +00:00
MainGLWidget::MainGLWidget(QWidget* parent): QOpenGLWidget(parent) {
setFocusPolicy(Qt::FocusPolicy::ClickFocus);
}
2024-12-01 11:26:14 +00:00
void MainGLWidget::initializeGL() {
glewInit();
renderInit(NULL, width(), height());
2024-12-01 11:26:14 +00:00
}
extern int vpx, vpy;
2024-12-01 11:26:14 +00:00
void MainGLWidget::resizeGL(int w, int h) {
// Update projection matrix and other size related settings:
// m_projection.setToIdentity();
// m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);
// ...
// glViewport(0, 0, w, h);
setViewport(w, h);
2024-12-01 11:26:14 +00:00
}
void MainGLWidget::paintGL() {
::render(NULL);
}
2024-12-01 12:08:45 +00:00
bool isMouseDragging = false;
QPoint lastMousePos;
void MainGLWidget::mouseMoveEvent(QMouseEvent* evt) {
// if (!(evt->buttons() & Qt::RightButton)) return;
if (!isMouseDragging) return;
camera.processRotation(evt->pos().x() - lastMousePos.x(), evt->pos().y() - lastMousePos.y());
lastMousePos = evt->pos();
2024-12-01 23:14:42 +00:00
// QCursor::setPos(lastMousePos);
2024-12-01 12:08:45 +00:00
}
2025-02-01 20:15:53 +00:00
class FirstRayHit : public rp::RaycastCallback {
rp::Body** target;
2024-12-01 12:08:45 +00:00
2025-02-01 20:15:53 +00:00
virtual rp::decimal notifyRaycastHit(const rp::RaycastInfo& raycastInfo) override {
if (reinterpret_cast<Part*>(raycastInfo.body->getUserData())->name == "Baseplate") return 1;
*target = raycastInfo.body;
return 0;
}
public:
FirstRayHit(rp::Body** target) : target(target) {}
};
void MainGLWidget::mousePressEvent(QMouseEvent* evt) {
switch(evt->button()) {
// Camera drag
case Qt::RightButton: {
lastMousePos = evt->pos();
isMouseDragging = true;
return;
// Clicking on objects
} case Qt::LeftButton: {
QPoint position = evt->pos();
rp::Body* rayHitTarget = NULL;
2025-02-01 21:55:07 +00:00
glm::vec3 pointDir = camera.getScreenDirection(glm::vec2(position.x(), position.y()), glm::vec2(width(), height()));
castRay(camera.cameraPos, pointDir, 50000, new FirstRayHit(&rayHitTarget));
2025-02-01 20:15:53 +00:00
if (!rayHitTarget) return;
2025-02-01 21:55:07 +00:00
std::shared_ptr<Part> part = partFromBody(rayHitTarget);
//part.selected = true;
setSelection(std::vector<InstanceRefWeak> { part });
2025-02-01 20:15:53 +00:00
return;
} default:
return;
}
2024-12-01 12:08:45 +00:00
}
void MainGLWidget::mouseReleaseEvent(QMouseEvent* evt) {
isMouseDragging = false;
2024-12-02 10:01:51 +00:00
}
static int moveZ = 0;
static int moveX = 0;
static std::chrono::time_point lastTime = std::chrono::steady_clock::now();
void MainGLWidget::updateCycle() {
float deltaTime = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - lastTime).count();
lastTime = std::chrono::steady_clock::now();
if (moveZ)
camera.processMovement(moveZ == 1 ? DIRECTION_FORWARD : DIRECTION_BACKWARDS, deltaTime);
if (moveX)
camera.processMovement(moveX == 1 ? DIRECTION_LEFT : DIRECTION_RIGHT, deltaTime);
}
void MainGLWidget::keyPressEvent(QKeyEvent* evt) {
if (evt->key() == Qt::Key_W) moveZ = 1;
else if (evt->key() == Qt::Key_S) moveZ = -1;
if (evt->key() == Qt::Key_A) moveX = 1;
else if (evt->key() == Qt::Key_D) moveX = -1;
if (evt->key() == Qt::Key_F) {
2025-01-17 08:24:18 +00:00
workspace->AddChild(lastPart = Part::New({
2024-12-02 10:01:51 +00:00
.position = camera.cameraPos + camera.cameraFront * glm::vec3(3),
.rotation = glm::vec3(0),
.scale = glm::vec3(1, 1, 1),
.material = Material {
.diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 32.0f,
}
2025-01-17 08:24:18 +00:00
}));
syncPartPhysics(lastPart);
2024-12-02 10:01:51 +00:00
}
}
void MainGLWidget::keyReleaseEvent(QKeyEvent* evt) {
if (evt->key() == Qt::Key_W || evt->key() == Qt::Key_S) moveZ = 0;
else if (evt->key() == Qt::Key_A || evt->key() == Qt::Key_D) moveX = 0;
2024-12-01 12:08:45 +00:00
}