qtk/src/qtkwidget.cpp

285 lines
8.1 KiB
C++
Raw Normal View History

2021-09-03 16:56:57 +00:00
/*##############################################################################
## Author: Shaun Reed ##
2022-03-06 16:54:05 +00:00
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Main window for Qt6 OpenGL widget application ##
2021-09-03 16:56:57 +00:00
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#include <QKeyEvent>
#include <input.h>
#include <mesh.h>
2022-11-24 22:26:53 +00:00
#include <qtkwidget.h>
2022-11-26 18:24:38 +00:00
#include <scene.h>
2021-09-03 16:56:57 +00:00
using namespace Qtk;
2021-09-03 16:56:57 +00:00
/*******************************************************************************
* Constructors, Destructors
******************************************************************************/
2022-11-24 22:26:53 +00:00
QtkWidget::QtkWidget() : mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR) {
initializeWidget();
}
2022-11-24 22:26:53 +00:00
QtkWidget::QtkWidget(QWidget * parent) :
QOpenGLWidget(parent), mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR) {
initializeWidget();
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
QtkWidget::QtkWidget(const QSurfaceFormat & format) :
mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR) {
2021-09-03 16:56:57 +00:00
setFormat(format);
setFocusPolicy(Qt::ClickFocus);
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
QtkWidget::~QtkWidget() {
2021-09-03 16:56:57 +00:00
makeCurrent();
teardownGL();
}
/*******************************************************************************
2022-11-26 18:24:38 +00:00
* Public Inherited Virtual Methods
2021-09-03 16:56:57 +00:00
******************************************************************************/
2022-11-24 22:26:53 +00:00
void QtkWidget::initializeGL() {
2021-09-03 16:56:57 +00:00
initializeOpenGLFunctions();
// Connect the frameSwapped signal to call the update() function
connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
// Initialize OpenGL debug context
#ifdef QTK_DEBUG
mDebugLogger = new QOpenGLDebugLogger(this);
2022-11-24 22:26:53 +00:00
if(mDebugLogger->initialize()) {
2021-09-03 16:56:57 +00:00
qDebug() << "GL_DEBUG Debug Logger" << mDebugLogger << "\n";
2022-11-24 22:26:53 +00:00
connect(
mDebugLogger, SIGNAL(messageLogged(QOpenGLDebugMessage)), this,
SLOT(messageLogged(QOpenGLDebugMessage)));
2021-09-03 16:56:57 +00:00
mDebugLogger->startLogging();
}
2022-11-24 22:26:53 +00:00
#endif // QTK_DEBUG
2021-09-03 16:56:57 +00:00
printContextInformation();
2022-11-24 22:26:53 +00:00
// Initialize opengl settings
2021-09-03 16:56:57 +00:00
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.1f, 1.0f);
glClearDepth(1.0f);
glClearColor(0.0f, 0.25f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
2022-11-24 22:26:53 +00:00
void QtkWidget::resizeGL(int width, int height) {
2022-11-26 18:24:38 +00:00
Scene::getProjectionMatrix().setToIdentity();
Scene::getProjectionMatrix().perspective(
2022-11-24 22:26:53 +00:00
45.0f, float(width) / float(height), 0.1f, 1000.0f);
2021-09-03 16:56:57 +00:00
}
2022-11-26 18:24:38 +00:00
void QtkWidget::paintGL() {
// Clear buffers and draw the scene if it is valid.
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
if(mScene != Q_NULLPTR) {
mScene->draw();
}
}
2021-09-03 16:56:57 +00:00
/*******************************************************************************
* Protected Slots
******************************************************************************/
2022-11-24 22:26:53 +00:00
void QtkWidget::update() {
2021-09-03 16:56:57 +00:00
updateCameraInput();
2022-11-24 22:26:53 +00:00
if(mScene != Q_NULLPTR) {
mScene->update();
}
2021-09-03 16:56:57 +00:00
QWidget::update();
}
2022-11-24 22:26:53 +00:00
void QtkWidget::messageLogged(const QOpenGLDebugMessage & msg) {
2021-09-03 16:56:57 +00:00
QString error;
// Format based on severity
2022-11-24 22:26:53 +00:00
switch(msg.severity()) {
2021-09-03 16:56:57 +00:00
case QOpenGLDebugMessage::NotificationSeverity:
error += "--";
break;
case QOpenGLDebugMessage::HighSeverity:
error += "!!";
break;
case QOpenGLDebugMessage::MediumSeverity:
error += "!~";
break;
case QOpenGLDebugMessage::LowSeverity:
error += "~~";
break;
}
error += " (";
// Format based on source
2022-11-24 22:26:53 +00:00
#define CASE(c) \
case QOpenGLDebugMessage::c: \
error += #c; \
break
switch(msg.source()) {
2021-09-03 16:56:57 +00:00
CASE(APISource);
CASE(WindowSystemSource);
CASE(ShaderCompilerSource);
CASE(ThirdPartySource);
CASE(ApplicationSource);
CASE(OtherSource);
CASE(InvalidSource);
}
#undef CASE
error += " : ";
// Format based on type
2022-11-24 22:26:53 +00:00
#define CASE(c) \
case QOpenGLDebugMessage::c: \
error += #c; \
break
switch(msg.type()) {
2021-09-03 16:56:57 +00:00
CASE(InvalidType);
CASE(ErrorType);
CASE(DeprecatedBehaviorType);
CASE(UndefinedBehaviorType);
CASE(PortabilityType);
CASE(PerformanceType);
CASE(OtherType);
CASE(MarkerType);
CASE(GroupPushType);
CASE(GroupPopType);
}
#undef CASE
error += ")";
qDebug() << qPrintable(error) << "\n" << qPrintable(msg.message()) << "\n";
}
/*******************************************************************************
2022-11-26 18:24:38 +00:00
* Protected Methods
2021-09-03 16:56:57 +00:00
******************************************************************************/
2022-11-24 22:26:53 +00:00
void QtkWidget::keyPressEvent(QKeyEvent * event) {
if(event->isAutoRepeat()) {
// Do not repeat input while a key is held down
2021-09-03 16:56:57 +00:00
event->ignore();
} else {
Input::registerKeyPress(event->key());
}
}
2022-11-24 22:26:53 +00:00
void QtkWidget::keyReleaseEvent(QKeyEvent * event) {
if(event->isAutoRepeat()) {
2021-09-03 16:56:57 +00:00
event->ignore();
} else {
Input::registerKeyRelease(event->key());
}
}
2022-11-24 22:26:53 +00:00
void QtkWidget::mousePressEvent(QMouseEvent * event) {
2021-09-03 16:56:57 +00:00
Input::registerMousePress(event->button());
}
2022-11-24 22:26:53 +00:00
void QtkWidget::mouseReleaseEvent(QMouseEvent * event) {
2021-09-03 16:56:57 +00:00
Input::registerMouseRelease(event->button());
}
/*******************************************************************************
2022-11-26 18:24:38 +00:00
* Private Methods
2021-09-03 16:56:57 +00:00
******************************************************************************/
2022-11-24 22:26:53 +00:00
void QtkWidget::initializeWidget() {
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4, 6);
// Set the number of samples used for glEnable(GL_MULTISAMPLING)
format.setSamples(4);
// Set the size of the depth bufer for glEnable(GL_DEPTH_TEST)
format.setDepthBufferSize(16);
// If QTK_DEBUG is set, enable debug context
#ifdef QTK_DEBUG
format.setOption(QSurfaceFormat::DebugContext);
#endif
setFormat(format);
setFocusPolicy(Qt::ClickFocus);
}
2022-11-24 22:26:53 +00:00
void QtkWidget::printContextInformation() {
2021-09-03 16:56:57 +00:00
QString glType;
QString glVersion;
QString glProfile;
QString glVendor;
QString glRenderer;
// Get Version Information
glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
glVersion = reinterpret_cast<const char *>(glGetString(GL_VERSION));
2022-11-24 22:26:53 +00:00
glVendor = reinterpret_cast<const char *>(glGetString(GL_VENDOR));
glRenderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
2021-09-03 16:56:57 +00:00
// Get Profile Information
2022-11-24 22:26:53 +00:00
#define CASE(c) \
case QSurfaceFormat::c: \
glProfile = #c; \
break
switch(format().profile()) {
2021-09-03 16:56:57 +00:00
CASE(NoProfile);
CASE(CoreProfile);
CASE(CompatibilityProfile);
}
#undef CASE
2022-11-24 22:26:53 +00:00
// qPrintable() will print our QString w/o quotes around it.
2021-09-03 16:56:57 +00:00
qDebug() << qPrintable(glType) << qPrintable(glVersion) << "("
<< qPrintable(glProfile) << ")"
<< "\nOpenGL Vendor: " << qPrintable(glVendor)
<< "\nRendering Device: " << qPrintable(glRenderer) << "\n";
}
2022-11-24 22:26:53 +00:00
void QtkWidget::updateCameraInput() {
2021-09-03 16:56:57 +00:00
Input::update();
// Camera Transformation
2022-11-24 22:26:53 +00:00
if(Input::buttonPressed(Qt::RightButton)) {
2021-09-03 16:56:57 +00:00
static const float transSpeed = 0.1f;
static const float rotSpeed = 0.5f;
// Handle rotations
2022-11-26 18:24:38 +00:00
Scene::getCamera().getTransform().rotate(
2022-11-24 22:26:53 +00:00
-rotSpeed * Input::mouseDelta().x(), Camera3D::LocalUp);
2022-11-26 18:24:38 +00:00
Scene::getCamera().getTransform().rotate(
-rotSpeed * Input::mouseDelta().y(), Scene::getCamera().right());
2021-09-03 16:56:57 +00:00
// Handle translations
QVector3D translation;
2022-11-24 22:26:53 +00:00
if(Input::keyPressed(Qt::Key_W)) {
2022-11-26 18:24:38 +00:00
translation += Scene::getCamera().forward();
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
if(Input::keyPressed(Qt::Key_S)) {
2022-11-26 18:24:38 +00:00
translation -= Scene::getCamera().forward();
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
if(Input::keyPressed(Qt::Key_A)) {
2022-11-26 18:24:38 +00:00
translation -= Scene::getCamera().right();
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
if(Input::keyPressed(Qt::Key_D)) {
2022-11-26 18:24:38 +00:00
translation += Scene::getCamera().right();
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
if(Input::keyPressed(Qt::Key_Q)) {
2022-11-26 18:24:38 +00:00
translation -= Scene::getCamera().up() / 2.0f;
2021-09-03 16:56:57 +00:00
}
2022-11-24 22:26:53 +00:00
if(Input::keyPressed(Qt::Key_E)) {
2022-11-26 18:24:38 +00:00
translation += Scene::getCamera().up() / 2.0f;
2021-09-03 16:56:57 +00:00
}
2022-11-26 18:24:38 +00:00
Scene::getCamera().getTransform().translate(transSpeed * translation);
2021-09-03 16:56:57 +00:00
}
}