Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"debug"
],
"displayName": "Ninja Debug Config",
"description": "Configure with vcpkg toolchain"
"description": "Configure with ninja generator"
},
{
"name": "ninja_release",
Expand All @@ -76,7 +76,7 @@
"release"
],
"displayName": "Ninja Release Config",
"description": "Configure with vcpkg toolchain"
"description": "Configure with ninja generator"
},
{
"hidden": true,
Expand Down
1 change: 1 addition & 0 deletions include/GLWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ private slots:

HDRToneMapMode _toneMappingMode;

bool _openGLInitialized = false;
float _anisotropicFilteringLevel = 16.0f;

Cone* _axisCone;
Expand Down
2 changes: 2 additions & 0 deletions include/ModelViewerApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ModelViewerApplication : public QApplication {
public:
ModelViewerApplication(int& argc, char** argv);

static void configureOpenGLFormat();

// Static utility method for supported extensions
static QStringList supportedImportExtensions();

Expand Down
28 changes: 14 additions & 14 deletions src/AssImpMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void AssImpMesh::render()

// Set render state efficiently
setRenderStateOptimized();
// Transparent draws must NOT write depth, but should still depth-test.

// Transparent draws must NOT write depth, but should still depth-test.
GLboolean prevDepthMask = GL_TRUE;
glGetBooleanv(GL_DEPTH_WRITEMASK, &prevDepthMask);
if (isTransparent() && needsDepthMaskOff()) glDepthMask(GL_FALSE);
Expand All @@ -112,7 +112,7 @@ void AssImpMesh::render()

// Draw the mesh
glDrawElements(_primitiveMode, drawCount, GL_UNSIGNED_INT, nullptr);

// Reset point size
if (_primitiveMode == GL_POINTS)
{
Expand All @@ -124,7 +124,7 @@ void AssImpMesh::render()
if (isTransparent()) glDepthMask(prevDepthMask); // restore immediately

_prog->release();

}

void AssImpMesh::optimizeMesh()
Expand Down Expand Up @@ -206,13 +206,13 @@ void AssImpMesh::setupMesh()
normals.push_back(v.Normal.x);
normals.push_back(v.Normal.y);
normals.push_back(v.Normal.z);

colors.reserve(_vertices.size() * 4);
colors.push_back(v.Color.r);
colors.push_back(v.Color.g);
colors.push_back(v.Color.b);
colors.push_back(v.Color.a);

// Extract all 4 texCoord sets
for (int i = 0; i < 4; i++)
{
Expand Down Expand Up @@ -534,7 +534,7 @@ void AssImpMesh::cacheTextureBindings()
{
addBinding("specularColorMap" /*+ std::to_string(specularColorNr)*/, GL_TEXTURE26);
specularColorNr++;
}
}
else if (texture.type == "diffuseMap") // === KHR_materials_pbrSpecularGlossiness ===
{
addBinding("diffuseMap" /*+ std::to_string(diffuseSpecGlossNr)*/, GL_TEXTURE10);
Expand Down Expand Up @@ -864,7 +864,7 @@ void AssImpMesh::serialize(QDataStream& out) const
QString qtype = QString::fromUtf8(t.type.c_str());
QString qpath = QString::fromUtf8(t.path);
out << qtype;
out << qpath;
out << qpath;
}


Expand Down Expand Up @@ -925,13 +925,13 @@ void AssImpMesh::deserialize(QDataStream& in)
t.type = typeQ.toStdString();
t.path = pathQ.toStdString();
t.id = 0; // must be reloaded via TextureManager later
_textures.push_back(t);
_textures.push_back(t);
}

// Read material
_material.deserialize(in);

// --- Ensure deserialized texture paths get actual GL texture IDs and keep material in sync ---
// --- Ensure deserialized texture paths get actual GL texture IDs and keep material in sync ---
// Helper lambda: convert our Texture.type -> update GLMaterial
auto updateMaterialFromTexture = [this](const GLMaterial::Texture& tex) {
QString qpath = QString::fromUtf8(tex.path).trimmed();
Expand Down Expand Up @@ -1031,7 +1031,7 @@ void AssImpMesh::deserialize(QDataStream& in)
{
t.id = static_cast<unsigned int>(newId);
t.hasAlpha = hasAlpha;

// register/replace in the mesh's internal bindings list
replaceOrAppendTexture(t.type, newId, hasAlpha);

Expand Down Expand Up @@ -1360,7 +1360,7 @@ void AssImpMesh::setTextureMaps(const GLMaterial& material)
if (material.hasOpacityMap())
{
_hasOpacityPBRMap = true;
setOpacityPBRMap(material.opacityTextureId());
setOpacityPBRMap(material.opacityTextureId());
}
if (material.hasTransmissionMap())
{
Expand Down Expand Up @@ -1434,7 +1434,7 @@ void AssImpMesh::releaseCurrentShader()
QOpenGLContext* ctx = QOpenGLContext::currentContext();
if (ctx && ctx->isValid() && ctx->thread() == QThread::currentThread())
{
_currentBoundShader->release();
_currentBoundShader->release();
}
}
_currentBoundShader = nullptr;
Expand Down
23 changes: 22 additions & 1 deletion src/GLWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#include "Utils.h"
#include <algorithm>
#include <iostream>
#include <QDebug>
#include <QMessageBox>
#include <QOpenGLContext>
#include <QStyleFactory>


Expand Down Expand Up @@ -656,7 +658,19 @@ QUuid GLWidget::getUuidByIndex(int index) const

void GLWidget::initializeGL()
{
initializeOpenGLFunctions();
_openGLInitialized = false;

if (!QOpenGLContext::currentContext())
{
qCritical() << "GLWidget::initializeGL: no current OpenGL context";
return;
}

if (!initializeOpenGLFunctions())
{
qCritical() << "GLWidget::initializeGL: failed to initialize OpenGL 4.5 Core functions";
return;
}

int maxSamples = 0;
glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
Expand Down Expand Up @@ -768,10 +782,14 @@ void GLWidget::initializeGL()
glEnable(GL_DEPTH_TEST);

glClearColor(0.0f, 0.0f, 0.0f, 1.f);
_openGLInitialized = true;
}

void GLWidget::resizeGL(int width, int height)
{
if (!_openGLInitialized)
return;

float w = (float)width;
float h = (float)height;

Expand Down Expand Up @@ -811,6 +829,9 @@ void GLWidget::resizeGL(int width, int height)

void GLWidget::paintGL()
{
if (!_openGLInitialized)
return;

QColor topColor = !_visibleSwapped ? _bgTopColor : QColor::fromRgbF(1.0f - _bgTopColor.redF(),
1.0f - _bgTopColor.greenF(), 1.0f - _bgTopColor.blueF(),
_bgTopColor.alphaF());
Expand Down
14 changes: 1 addition & 13 deletions src/ModelViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,8 @@ ModelViewer::ModelViewer(QWidget* parent) : QWidget(parent)

setAttribute(Qt::WA_DeleteOnClose);

int values[] = { 0, 2, 4, 8, 16, 32 };
int samples = values[settings.value("msaaComboBox", 4).toInt()];

QSurfaceFormat format;
format.setVersion(4, 5); // OpenGL version 4.5
format.setProfile(QSurfaceFormat::CoreProfile);
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setSwapInterval(0);
format.setStereo(true);
format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setSamples(samples); // Set MSAA samples
_glWidget = new GLWidget(this, "glwidget");
_glWidget->setAttribute(Qt::WA_DeleteOnClose);
_glWidget->setFormat(format);
Expand Down Expand Up @@ -2772,4 +2761,3 @@ void ModelViewer::setVisibilityWithoutUndo(const QSet<QUuid>& visibleUuids)
// This will update _displayedObjectsIds and _hiddenObjectsIds
on_listWidgetModel_itemChanged(nullptr);
}

25 changes: 16 additions & 9 deletions src/ModelViewerApplication.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ModelViewerApplication.h"
#include <QOpenGLFunctions>
#include <QSettings>
#include <QSurfaceFormat>
#include <QTranslator>
#include <QLocale>
#include <assimp/Importer.hpp>
Expand All @@ -10,16 +11,12 @@ QStringList ModelViewerApplication::_supportedExtensions;
int ModelViewerApplication::_supportedMSAASamples = 4; // Default MSAA samples
int ModelViewerApplication::_supportedAnisotropicFilteringLevel = 16; // Default anisotropic filtering level

ModelViewerApplication::ModelViewerApplication(int& argc, char** argv)
: QApplication(argc, argv)
void ModelViewerApplication::configureOpenGLFormat()
{
setDesktopSettingsAware(true);
setApplicationName("ModelViewer");
setOrganizationName("Sharjith N");

QString version = QString(APP_VERSION_STRING);
setApplicationVersion(version);

QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QCoreApplication::setApplicationName("ModelViewer");
QCoreApplication::setOrganizationName("Sharjith N");

QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
int values[] = {0, 2, 4, 8, 16, 32};
Expand All @@ -36,7 +33,17 @@ ModelViewerApplication::ModelViewerApplication(int& argc, char** argv)
format.setSamples(samples); // Set MSAA samples

QSurfaceFormat::setDefaultFormat(format);
}

ModelViewerApplication::ModelViewerApplication(int& argc, char** argv)
: QApplication(argc, argv)
{
setDesktopSettingsAware(true);
setApplicationName("ModelViewer");
setOrganizationName("Sharjith N");

QString version = QString(APP_VERSION_STRING);
setApplicationVersion(version);
}

QStringList ModelViewerApplication::supportedImportExtensions()
Expand Down
53 changes: 32 additions & 21 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QApplication>
#include <QDebug>
#include <QFileInfo>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QStyleFactory>
#include <sstream>
Expand All @@ -17,6 +18,7 @@ int main(int argc, char** argv)
{
Q_INIT_RESOURCE(ModelViewer);

ModelViewerApplication::configureOpenGLFormat();
ModelViewerApplication app(argc, argv);

#if QT_VERSION_MAJOR == 6
Expand Down Expand Up @@ -76,28 +78,37 @@ int main(int argc, char** argv)
}
}

QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
auto logOpenGLInfo = [&glFuncs](auto& outputStream) {
std::map<std::string, GLenum> infoMap = {
{"Renderer", GL_RENDERER},
{"Vendor", GL_VENDOR},
{"OpenGL Version", GL_VERSION},
{"Shader Version", GL_SHADING_LANGUAGE_VERSION}
};

for (const auto& [label, value] : infoMap) {
const char* info = reinterpret_cast<const char*>(glFuncs.glGetString(value));
outputStream << label << ": " << info << '\n';
}
};

// Log information to std::cout
logOpenGLInfo(std::cout);
if (QOpenGLContext::currentContext())
{
QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
auto logOpenGLInfo = [&glFuncs](auto& outputStream) {
std::map<std::string, GLenum> infoMap = {
{"Renderer", GL_RENDERER},
{"Vendor", GL_VENDOR},
{"OpenGL Version", GL_VERSION},
{"Shader Version", GL_SHADING_LANGUAGE_VERSION}
};

for (const auto& [label, value] : infoMap) {
const char* info = reinterpret_cast<const char*>(glFuncs.glGetString(value));
outputStream << label << ": " << info << '\n';
}
};

// Log information to std::cout
logOpenGLInfo(std::cout);

// Collect information into std::stringstream and set it to mw
std::stringstream ss;
logOpenGLInfo(ss);
mw->setGraphicsInfo(ss.str().c_str());
}
else
{
qWarning() << "OpenGL context is not current during startup graphics-info collection";
mw->setGraphicsInfo("OpenGL context information is unavailable until the viewer is initialized.");
}

// Collect information into std::stringstream and set it to mw
std::stringstream ss;
logOpenGLInfo(ss);
mw->setGraphicsInfo(ss.str().c_str());

/*
#ifdef QT_DEBUG
Expand Down
Loading