Add skin preview (#3283)

This commit is contained in:
Alexandru Ionut Tripon
2025-03-02 08:18:51 +02:00
committed by GitHub
20 changed files with 1151 additions and 137 deletions
+75 -62
View File
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
* Copyright (c) 2023-2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,6 +17,7 @@
*/
#include "SkinManageDialog.h"
#include "ui/dialogs/skins/draw/SkinOpenGLWindow.h"
#include "ui_SkinManageDialog.h"
#include <FileSystem.h>
@@ -52,13 +53,15 @@
#include "ui/instanceview/InstanceDelegate.h"
SkinManageDialog::SkinManageDialog(QWidget* parent, MinecraftAccountPtr acct)
: QDialog(parent), m_acct(acct), ui(new Ui::SkinManageDialog), m_list(this, APPLICATION->settings()->get("SkinsDir").toString(), acct)
: QDialog(parent), m_acct(acct), m_ui(new Ui::SkinManageDialog), m_list(this, APPLICATION->settings()->get("SkinsDir").toString(), acct)
{
ui->setupUi(this);
m_ui->setupUi(this);
m_skinPreview = new SkinOpenGLWindow(this, palette().color(QPalette::Normal, QPalette::Base));
setWindowModality(Qt::WindowModal);
auto contentsWidget = ui->listView;
auto contentsWidget = m_ui->listView;
contentsWidget->setViewMode(QListView::IconMode);
contentsWidget->setFlow(QListView::LeftToRight);
contentsWidget->setIconSize(QSize(48, 48));
@@ -88,28 +91,31 @@ SkinManageDialog::SkinManageDialog(QWidget* parent, MinecraftAccountPtr acct)
connect(contentsWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
SLOT(selectionChanged(QItemSelection, QItemSelection)));
connect(ui->listView, &QListView::customContextMenuRequested, this, &SkinManageDialog::show_context_menu);
connect(m_ui->listView, &QListView::customContextMenuRequested, this, &SkinManageDialog::show_context_menu);
setupCapes();
ui->listView->setCurrentIndex(m_list.index(m_list.getSelectedAccountSkin()));
m_ui->listView->setCurrentIndex(m_list.index(m_list.getSelectedAccountSkin()));
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("OK"));
m_ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("OK"));
m_ui->skinLayout->insertWidget(0, QWidget::createWindowContainer(m_skinPreview, this));
}
SkinManageDialog::~SkinManageDialog()
{
delete ui;
delete m_ui;
delete m_skinPreview;
}
void SkinManageDialog::activated(QModelIndex index)
{
m_selected_skin = index.data(Qt::UserRole).toString();
m_selectedSkinKey = index.data(Qt::UserRole).toString();
accept();
}
void SkinManageDialog::selectionChanged(QItemSelection selected, QItemSelection deselected)
void SkinManageDialog::selectionChanged(QItemSelection selected, [[maybe_unused]] QItemSelection deselected)
{
if (selected.empty())
return;
@@ -117,19 +123,20 @@ void SkinManageDialog::selectionChanged(QItemSelection selected, QItemSelection
QString key = selected.first().indexes().first().data(Qt::UserRole).toString();
if (key.isEmpty())
return;
m_selected_skin = key;
auto skin = m_list.skin(key);
if (!skin || !skin->isValid())
m_selectedSkinKey = key;
auto skin = getSelectedSkin();
if (!skin)
return;
ui->selectedModel->setPixmap(skin->getTexture().scaled(size() * (1. / 3), Qt::KeepAspectRatio, Qt::FastTransformation));
ui->capeCombo->setCurrentIndex(m_capes_idx.value(skin->getCapeId()));
ui->steveBtn->setChecked(skin->getModel() == SkinModel::CLASSIC);
ui->alexBtn->setChecked(skin->getModel() == SkinModel::SLIM);
m_skinPreview->updateScene(skin);
m_ui->capeCombo->setCurrentIndex(m_capesIdx.value(skin->getCapeId()));
m_ui->steveBtn->setChecked(skin->getModel() == SkinModel::CLASSIC);
m_ui->alexBtn->setChecked(skin->getModel() == SkinModel::SLIM);
}
void SkinManageDialog::delayed_scroll(QModelIndex model_index)
{
auto contentsWidget = ui->listView;
auto contentsWidget = m_ui->listView;
contentsWidget->scrollTo(model_index);
}
@@ -152,23 +159,19 @@ void SkinManageDialog::on_fileBtn_clicked()
}
}
QPixmap previewCape(QPixmap capeImage)
QPixmap previewCape(QImage capeImage)
{
QPixmap preview = QPixmap(10, 16);
QPainter painter(&preview);
painter.drawPixmap(0, 0, capeImage.copy(1, 1, 10, 16));
return preview.scaled(80, 128, Qt::IgnoreAspectRatio, Qt::FastTransformation);
return QPixmap::fromImage(capeImage.copy(1, 1, 10, 16).scaled(80, 128, Qt::IgnoreAspectRatio, Qt::FastTransformation));
}
void SkinManageDialog::setupCapes()
{
// FIXME: add a model for this, download/refresh the capes on demand
auto& accountData = *m_acct->accountData();
int index = 0;
ui->capeCombo->addItem(tr("No Cape"), QVariant());
m_ui->capeCombo->addItem(tr("No Cape"), QVariant());
auto currentCape = accountData.minecraftProfile.currentCape;
if (currentCape.isEmpty()) {
ui->capeCombo->setCurrentIndex(index);
m_ui->capeCombo->setCurrentIndex(index);
}
auto capesDir = FS::PathCombine(m_list.getDir(), "capes");
@@ -177,9 +180,9 @@ void SkinManageDialog::setupCapes()
for (auto& cape : accountData.minecraftProfile.capes) {
auto path = FS::PathCombine(capesDir, cape.id + ".png");
if (cape.data.size()) {
QPixmap capeImage;
QImage capeImage;
if (capeImage.loadFromData(cape.data, "PNG") && capeImage.save(path)) {
m_capes[cape.id] = previewCape(capeImage);
m_capes[cape.id] = capeImage;
continue;
}
}
@@ -197,46 +200,48 @@ void SkinManageDialog::setupCapes()
}
for (auto& cape : accountData.minecraftProfile.capes) {
index++;
QPixmap capeImage;
QImage capeImage;
if (!m_capes.contains(cape.id)) {
auto path = FS::PathCombine(capesDir, cape.id + ".png");
if (QFileInfo(path).exists() && capeImage.load(path)) {
capeImage = previewCape(capeImage);
m_capes[cape.id] = capeImage;
}
}
if (!capeImage.isNull()) {
ui->capeCombo->addItem(capeImage, cape.alias, cape.id);
m_ui->capeCombo->addItem(previewCape(capeImage), cape.alias, cape.id);
} else {
ui->capeCombo->addItem(cape.alias, cape.id);
m_ui->capeCombo->addItem(cape.alias, cape.id);
}
m_capes_idx[cape.id] = index;
m_capesIdx[cape.id] = index;
}
}
void SkinManageDialog::on_capeCombo_currentIndexChanged(int index)
{
auto id = ui->capeCombo->currentData();
auto id = m_ui->capeCombo->currentData();
auto cape = m_capes.value(id.toString(), {});
if (!cape.isNull()) {
ui->capeImage->setPixmap(cape.scaled(size() * (1. / 3), Qt::KeepAspectRatio, Qt::FastTransformation));
m_ui->capeImage->setPixmap(previewCape(cape).scaled(size() * (1. / 3), Qt::KeepAspectRatio, Qt::FastTransformation));
}
if (auto skin = m_list.skin(m_selected_skin); skin) {
m_skinPreview->updateCape(cape);
if (auto skin = getSelectedSkin(); skin) {
skin->setCapeId(id.toString());
m_skinPreview->updateScene(skin);
}
}
void SkinManageDialog::on_steveBtn_toggled(bool checked)
{
if (auto skin = m_list.skin(m_selected_skin); skin) {
if (auto skin = getSelectedSkin(); skin) {
skin->setModel(checked ? SkinModel::CLASSIC : SkinModel::SLIM);
m_skinPreview->updateScene(skin);
}
}
void SkinManageDialog::accept()
{
auto skin = m_list.skin(m_selected_skin);
auto skin = m_list.skin(m_selectedSkinKey);
if (!skin) {
reject();
return;
@@ -286,15 +291,15 @@ void SkinManageDialog::on_resetBtn_clicked()
void SkinManageDialog::show_context_menu(const QPoint& pos)
{
QMenu myMenu(tr("Context menu"), this);
myMenu.addAction(ui->action_Rename_Skin);
myMenu.addAction(ui->action_Delete_Skin);
myMenu.addAction(m_ui->action_Rename_Skin);
myMenu.addAction(m_ui->action_Delete_Skin);
myMenu.exec(ui->listView->mapToGlobal(pos));
myMenu.exec(m_ui->listView->mapToGlobal(pos));
}
bool SkinManageDialog::eventFilter(QObject* obj, QEvent* ev)
{
if (obj == ui->listView) {
if (obj == m_ui->listView) {
if (ev->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(ev);
switch (keyEvent->key()) {
@@ -314,22 +319,22 @@ bool SkinManageDialog::eventFilter(QObject* obj, QEvent* ev)
void SkinManageDialog::on_action_Rename_Skin_triggered(bool checked)
{
if (!m_selected_skin.isEmpty()) {
ui->listView->edit(ui->listView->currentIndex());
if (!m_selectedSkinKey.isEmpty()) {
m_ui->listView->edit(m_ui->listView->currentIndex());
}
}
void SkinManageDialog::on_action_Delete_Skin_triggered(bool checked)
{
if (m_selected_skin.isEmpty())
if (m_selectedSkinKey.isEmpty())
return;
if (m_list.getSkinIndex(m_selected_skin) == m_list.getSelectedAccountSkin()) {
if (m_list.getSkinIndex(m_selectedSkinKey) == m_list.getSelectedAccountSkin()) {
CustomMessageBox::selectable(this, tr("Delete error"), tr("Can not delete skin that is in use."), QMessageBox::Warning)->exec();
return;
}
auto skin = m_list.skin(m_selected_skin);
auto skin = m_list.skin(m_selectedSkinKey);
if (!skin)
return;
@@ -341,15 +346,15 @@ void SkinManageDialog::on_action_Delete_Skin_triggered(bool checked)
->exec();
if (response == QMessageBox::Yes) {
if (!m_list.deleteSkin(m_selected_skin, true)) {
m_list.deleteSkin(m_selected_skin, false);
if (!m_list.deleteSkin(m_selectedSkinKey, true)) {
m_list.deleteSkin(m_selectedSkinKey, false);
}
}
}
void SkinManageDialog::on_urlBtn_clicked()
{
auto url = QUrl(ui->urlLine->text());
auto url = QUrl(m_ui->urlLine->text());
if (!url.isValid()) {
CustomMessageBox::selectable(this, tr("Invalid url"), tr("Invalid url"), QMessageBox::Critical)->show();
return;
@@ -366,13 +371,13 @@ void SkinManageDialog::on_urlBtn_clicked()
if (!s.isValid()) {
CustomMessageBox::selectable(this, tr("URL is not a valid skin"),
QFileInfo::exists(path) ? tr("Skin images must be 64x64 or 64x32 pixel PNG files.")
: tr("Unable to download the skin: '%1'.").arg(ui->urlLine->text()),
: tr("Unable to download the skin: '%1'.").arg(m_ui->urlLine->text()),
QMessageBox::Critical)
->show();
QFile::remove(path);
return;
}
ui->urlLine->setText("");
m_ui->urlLine->setText("");
if (QFileInfo(path).suffix().isEmpty()) {
QFile::rename(path, path + ".png");
}
@@ -405,7 +410,7 @@ class WaitTask : public Task {
void SkinManageDialog::on_userBtn_clicked()
{
auto user = ui->urlLine->text();
auto user = m_ui->urlLine->text();
if (user.isEmpty()) {
return;
}
@@ -499,7 +504,7 @@ void SkinManageDialog::on_userBtn_clicked()
QFile::remove(path);
return;
}
ui->urlLine->setText("");
m_ui->urlLine->setText("");
s.setModel(mcProfile.skin.variant.toUpper() == "SLIM" ? SkinModel::SLIM : SkinModel::CLASSIC);
s.setURL(mcProfile.skin.url);
if (m_capes.contains(mcProfile.currentCape)) {
@@ -513,14 +518,22 @@ void SkinManageDialog::resizeEvent(QResizeEvent* event)
QWidget::resizeEvent(event);
QSize s = size() * (1. / 3);
if (auto skin = m_list.skin(m_selected_skin); skin) {
if (skin->isValid()) {
ui->selectedModel->setPixmap(skin->getTexture().scaled(s, Qt::KeepAspectRatio, Qt::FastTransformation));
}
}
auto id = ui->capeCombo->currentData();
auto id = m_ui->capeCombo->currentData();
auto cape = m_capes.value(id.toString(), {});
if (!cape.isNull()) {
ui->capeImage->setPixmap(cape.scaled(s, Qt::KeepAspectRatio, Qt::FastTransformation));
m_ui->capeImage->setPixmap(previewCape(cape).scaled(s, Qt::KeepAspectRatio, Qt::FastTransformation));
}
}
SkinModel* SkinManageDialog::getSelectedSkin()
{
if (auto skin = m_list.skin(m_selectedSkinKey); skin && skin->isValid()) {
return skin;
}
return nullptr;
}
QHash<QString, QImage> SkinManageDialog::capes()
{
return m_capes;
}
+13 -7
View File
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
* Copyright (c) 2023-2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -24,18 +24,22 @@
#include "minecraft/auth/MinecraftAccount.h"
#include "minecraft/skins/SkinList.h"
#include "minecraft/skins/SkinModel.h"
#include "ui/dialogs/skins/draw/SkinOpenGLWindow.h"
namespace Ui {
class SkinManageDialog;
}
class SkinManageDialog : public QDialog {
class SkinManageDialog : public QDialog, public SkinProvider {
Q_OBJECT
public:
explicit SkinManageDialog(QWidget* parent, MinecraftAccountPtr acct);
virtual ~SkinManageDialog();
void resizeEvent(QResizeEvent* event) override;
virtual SkinModel* getSelectedSkin() override;
virtual QHash<QString, QImage> capes() override;
public slots:
void selectionChanged(QItemSelection, QItemSelection);
void activated(QModelIndex);
@@ -56,10 +60,12 @@ class SkinManageDialog : public QDialog {
private:
void setupCapes();
private:
MinecraftAccountPtr m_acct;
Ui::SkinManageDialog* ui;
Ui::SkinManageDialog* m_ui;
SkinList m_list;
QString m_selected_skin;
QHash<QString, QPixmap> m_capes;
QHash<QString, int> m_capes_idx;
QString m_selectedSkinKey;
QHash<QString, QImage> m_capes;
QHash<QString, int> m_capesIdx;
SkinOpenGLWindow* m_skinPreview = nullptr;
};
+1 -11
View File
@@ -19,17 +19,7 @@
<item>
<layout class="QVBoxLayout" name="selectedVLayout" stretch="2,1,3">
<item>
<widget class="QLabel" name="selectedModel">
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<layout class="QVBoxLayout" name="skinLayout"/>
</item>
<item>
<widget class="QGroupBox" name="modelBox">
@@ -0,0 +1,277 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "BoxGeometry.h"
#include <QMatrix4x4>
#include <QVector2D>
#include <QVector3D>
#include <QVector>
struct VertexData {
QVector4D position;
QVector2D texCoord;
VertexData(const QVector4D& pos, const QVector2D& tex) : position(pos), texCoord(tex) {}
};
// For cube we would need only 8 vertices but we have to
// duplicate vertex for each face because texture coordinate
// is different.
static const QVector<QVector4D> vertices = {
// Vertex data for face 0
QVector4D(-0.5f, -0.5f, 0.5f, 1.0f), // v0
QVector4D(0.5f, -0.5f, 0.5f, 1.0f), // v1
QVector4D(-0.5f, 0.5f, 0.5f, 1.0f), // v2
QVector4D(0.5f, 0.5f, 0.5f, 1.0f), // v3
// Vertex data for face 1
QVector4D(0.5f, -0.5f, 0.5f, 1.0f), // v4
QVector4D(0.5f, -0.5f, -0.5f, 1.0f), // v5
QVector4D(0.5f, 0.5f, 0.5f, 1.0f), // v6
QVector4D(0.5f, 0.5f, -0.5f, 1.0f), // v7
// Vertex data for face 2
QVector4D(0.5f, -0.5f, -0.5f, 1.0f), // v8
QVector4D(-0.5f, -0.5f, -0.5f, 1.0f), // v9
QVector4D(0.5f, 0.5f, -0.5f, 1.0f), // v10
QVector4D(-0.5f, 0.5f, -0.5f, 1.0f), // v11
// Vertex data for face 3
QVector4D(-0.5f, -0.5f, -0.5f, 1.0f), // v12
QVector4D(-0.5f, -0.5f, 0.5f, 1.0f), // v13
QVector4D(-0.5f, 0.5f, -0.5f, 1.0f), // v14
QVector4D(-0.5f, 0.5f, 0.5f, 1.0f), // v15
// Vertex data for face 4
QVector4D(-0.5f, -0.5f, -0.5f, 1.0f), // v16
QVector4D(0.5f, -0.5f, -0.5f, 1.0f), // v17
QVector4D(-0.5f, -0.5f, 0.5f, 1.0f), // v18
QVector4D(0.5f, -0.5f, 0.5f, 1.0f), // v19
// Vertex data for face 5
QVector4D(-0.5f, 0.5f, 0.5f, 1.0f), // v20
QVector4D(0.5f, 0.5f, 0.5f, 1.0f), // v21
QVector4D(-0.5f, 0.5f, -0.5f, 1.0f), // v22
QVector4D(0.5f, 0.5f, -0.5f, 1.0f), // v23
};
// Indices for drawing cube faces using triangle strips.
// Triangle strips can be connected by duplicating indices
// between the strips. If connecting strips have opposite
// vertex order then last index of the first strip and first
// index of the second strip needs to be duplicated. If
// connecting strips have same vertex order then only last
// index of the first strip needs to be duplicated.
static const QVector<GLushort> indices = {
0, 1, 2, 3, 3, // Face 0 - triangle strip ( v0, v1, v2, v3)
4, 4, 5, 6, 7, 7, // Face 1 - triangle strip ( v4, v5, v6, v7)
8, 8, 9, 10, 11, 11, // Face 2 - triangle strip ( v8, v9, v10, v11)
12, 12, 13, 14, 15, 15, // Face 3 - triangle strip (v12, v13, v14, v15)
16, 16, 17, 18, 19, 19, // Face 4 - triangle strip (v16, v17, v18, v19)
20, 20, 21, 22, 23 // Face 5 - triangle strip (v20, v21, v22, v23)
};
static const QVector<VertexData> planeVertices = {
{ QVector4D(-1.0f, -1.0f, -0.5f, 1.0f), QVector2D(0.0f, 0.0f) }, // Bottom-left
{ QVector4D(1.0f, -1.0f, -0.5f, 1.0f), QVector2D(1.0f, 0.0f) }, // Bottom-right
{ QVector4D(-1.0f, 1.0f, -0.5f, 1.0f), QVector2D(0.0f, 1.0f) }, // Top-left
{ QVector4D(1.0f, 1.0f, -0.5f, 1.0f), QVector2D(1.0f, 1.0f) }, // Top-right
};
static const QVector<GLushort> planeIndices = {
0, 1, 2, 3, 3 // Face 0 - triangle strip ( v0, v1, v2, v3)
};
QVector<QVector4D> transformVectors(const QMatrix4x4& matrix, const QVector<QVector4D>& vectors)
{
QVector<QVector4D> transformedVectors;
transformedVectors.reserve(vectors.size());
for (const QVector4D& vec : vectors) {
if (!matrix.isIdentity()) {
transformedVectors.append(matrix * vec);
} else {
transformedVectors.append(vec);
}
}
return transformedVectors;
}
// Function to calculate UV coordinates
// this is pure magic (if something is wrong with textures this is at fault)
QVector<QVector2D> getCubeUVs(float u, float v, float width, float height, float depth, float textureWidth, float textureHeight)
{
auto toFaceVertices = [textureHeight, textureWidth](float x1, float y1, float x2, float y2) -> QVector<QVector2D> {
return {
QVector2D(x1 / textureWidth, 1.0 - y2 / textureHeight),
QVector2D(x2 / textureWidth, 1.0 - y2 / textureHeight),
QVector2D(x2 / textureWidth, 1.0 - y1 / textureHeight),
QVector2D(x1 / textureWidth, 1.0 - y1 / textureHeight),
};
};
auto top = toFaceVertices(u + depth, v, u + width + depth, v + depth);
auto bottom = toFaceVertices(u + width + depth, v, u + width * 2 + depth, v + depth);
auto left = toFaceVertices(u, v + depth, u + depth, v + depth + height);
auto front = toFaceVertices(u + depth, v + depth, u + width + depth, v + depth + height);
auto right = toFaceVertices(u + width + depth, v + depth, u + width + depth * 2, v + height + depth);
auto back = toFaceVertices(u + width + depth * 2, v + depth, u + width * 2 + depth * 2, v + height + depth);
auto uvRight = {
right[0],
right[1],
right[3],
right[2],
};
auto uvLeft = {
left[0],
left[1],
left[3],
left[2],
};
auto uvTop = {
top[0],
top[1],
top[3],
top[2],
};
auto uvBottom = {
bottom[3],
bottom[2],
bottom[0],
bottom[1],
};
auto uvFront = {
front[0],
front[1],
front[3],
front[2],
};
auto uvBack = {
back[0],
back[1],
back[3],
back[2],
};
// Create a new array to hold the modified UV data
QVector<QVector2D> uvData;
uvData.reserve(24);
// Iterate over the arrays and copy the data to newUVData
for (const auto& uvArray : { uvFront, uvRight, uvBack, uvLeft, uvBottom, uvTop }) {
uvData.append(uvArray);
}
return uvData;
}
namespace opengl {
BoxGeometry::BoxGeometry(QVector3D size, QVector3D position) : m_indexBuf(QOpenGLBuffer::IndexBuffer), m_size(size), m_position(position)
{
initializeOpenGLFunctions();
// Generate 2 VBOs
m_vertexBuf.create();
m_indexBuf.create();
}
BoxGeometry::BoxGeometry(QVector3D size, QVector3D position, QPoint uv, QVector3D textureDim, QSize textureSize)
: BoxGeometry(size, position)
{
initGeometry(uv.x(), uv.y(), textureDim.x(), textureDim.y(), textureDim.z(), textureSize.width(), textureSize.height());
}
BoxGeometry::~BoxGeometry()
{
m_vertexBuf.destroy();
m_indexBuf.destroy();
}
void BoxGeometry::draw(QOpenGLShaderProgram* program)
{
// Tell OpenGL which VBOs to use
program->setUniformValue("model_matrix", m_matrix);
m_vertexBuf.bind();
m_indexBuf.bind();
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 4, sizeof(VertexData));
// Offset for texture coordinate
offset += sizeof(QVector4D);
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program->attributeLocation("a_texcoord");
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, m_indecesCount, GL_UNSIGNED_SHORT, nullptr);
}
void BoxGeometry::initGeometry(float u, float v, float width, float height, float depth, float textureWidth, float textureHeight)
{
auto textureCord = getCubeUVs(u, v, width, height, depth, textureWidth, textureHeight);
// this should not be needed to be done on each render for most of the objects
QMatrix4x4 transformation;
transformation.translate(m_position);
transformation.scale(m_size);
auto positions = transformVectors(transformation, vertices);
QVector<VertexData> verticesData;
verticesData.reserve(positions.size()); // Reserve space for efficiency
for (int i = 0; i < positions.size(); ++i) {
verticesData.append(VertexData(positions[i], textureCord[i]));
}
// Transfer vertex data to VBO 0
m_vertexBuf.bind();
m_vertexBuf.allocate(verticesData.constData(), verticesData.size() * sizeof(VertexData));
// Transfer index data to VBO 1
m_indexBuf.bind();
m_indexBuf.allocate(indices.constData(), indices.size() * sizeof(GLushort));
m_indecesCount = indices.size();
}
void BoxGeometry::rotate(float angle, const QVector3D& vector)
{
m_matrix.rotate(angle, vector);
}
BoxGeometry* BoxGeometry::Plane()
{
auto b = new BoxGeometry(QVector3D(), QVector3D());
// Transfer vertex data to VBO 0
b->m_vertexBuf.bind();
b->m_vertexBuf.allocate(planeVertices.constData(), planeVertices.size() * sizeof(VertexData));
// Transfer index data to VBO 1
b->m_indexBuf.bind();
b->m_indexBuf.allocate(planeIndices.constData(), planeIndices.size() * sizeof(GLushort));
b->m_indecesCount = planeIndices.size();
return b;
}
} // namespace opengl
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QMatrix4x4>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QVector3D>
namespace opengl {
class BoxGeometry : protected QOpenGLFunctions {
public:
BoxGeometry(QVector3D size, QVector3D position);
BoxGeometry(QVector3D size, QVector3D position, QPoint uv, QVector3D textureDim, QSize textureSize = { 64, 64 });
static BoxGeometry* Plane();
virtual ~BoxGeometry();
void draw(QOpenGLShaderProgram* program);
void initGeometry(float u, float v, float width, float height, float depth, float textureWidth = 64, float textureHeight = 64);
void rotate(float angle, const QVector3D& vector);
private:
QOpenGLBuffer m_vertexBuf;
QOpenGLBuffer m_indexBuf;
QVector3D m_size;
QVector3D m_position;
QMatrix4x4 m_matrix;
GLsizei m_indecesCount;
};
} // namespace opengl
+134
View File
@@ -0,0 +1,134 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ui/dialogs/skins/draw/Scene.h"
namespace opengl {
Scene::Scene(const QImage& skin, bool slim, const QImage& cape) : m_slim(slim), m_capeVisible(!cape.isNull())
{
m_staticComponents = {
// head
new opengl::BoxGeometry(QVector3D(8, 8, 8), QVector3D(0, 4, 0), QPoint(0, 0), QVector3D(8, 8, 8)),
new opengl::BoxGeometry(QVector3D(9, 9, 9), QVector3D(0, 4, 0), QPoint(32, 0), QVector3D(8, 8, 8)),
// body
new opengl::BoxGeometry(QVector3D(8, 12, 4), QVector3D(0, -6, 0), QPoint(16, 16), QVector3D(8, 12, 4)),
new opengl::BoxGeometry(QVector3D(8.5, 12.5, 4.5), QVector3D(0, -6, 0), QPoint(16, 32), QVector3D(8, 12, 4)),
// right leg
new opengl::BoxGeometry(QVector3D(4, 12, 4), QVector3D(-1.9, -18, -0.1), QPoint(0, 16), QVector3D(4, 12, 4)),
new opengl::BoxGeometry(QVector3D(4.5, 12.5, 4.5), QVector3D(-1.9, -18, -0.1), QPoint(0, 32), QVector3D(4, 12, 4)),
// left leg
new opengl::BoxGeometry(QVector3D(4, 12, 4), QVector3D(1.9, -18, -0.1), QPoint(16, 48), QVector3D(4, 12, 4)),
new opengl::BoxGeometry(QVector3D(4.5, 12.5, 4.5), QVector3D(1.9, -18, -0.1), QPoint(0, 48), QVector3D(4, 12, 4)),
};
m_normalArms = {
// Right Arm
new opengl::BoxGeometry(QVector3D(4, 12, 4), QVector3D(-6, -6, 0), QPoint(40, 16), QVector3D(4, 12, 4)),
new opengl::BoxGeometry(QVector3D(4.5, 12.5, 4.5), QVector3D(-6, -6, 0), QPoint(40, 32), QVector3D(4, 12, 4)),
// Left Arm
new opengl::BoxGeometry(QVector3D(4, 12, 4), QVector3D(6, -6, 0), QPoint(32, 48), QVector3D(4, 12, 4)),
new opengl::BoxGeometry(QVector3D(4.5, 12.5, 4.5), QVector3D(6, -6, 0), QPoint(48, 48), QVector3D(4, 12, 4)),
};
m_slimArms = {
// Right Arm
new opengl::BoxGeometry(QVector3D(3, 12, 4), QVector3D(-5.5, -6, 0), QPoint(40, 16), QVector3D(3, 12, 4)),
new opengl::BoxGeometry(QVector3D(3.5, 12.5, 4.5), QVector3D(-5.5, -6, 0), QPoint(40, 32), QVector3D(3, 12, 4)),
// Left Arm
new opengl::BoxGeometry(QVector3D(3, 12, 4), QVector3D(5.5, -6, 0), QPoint(32, 48), QVector3D(3, 12, 4)),
new opengl::BoxGeometry(QVector3D(3.5, 12.5, 4.5), QVector3D(5.5, -6, 0), QPoint(48, 48), QVector3D(3, 12, 4)),
};
m_cape = new opengl::BoxGeometry(QVector3D(10, 16, 1), QVector3D(0, -8, 2.5), QPoint(0, 0), QVector3D(10, 16, 1), QSize(64, 32));
m_cape->rotate(10.8, QVector3D(1, 0, 0));
m_cape->rotate(180, QVector3D(0, 1, 0));
// texture init
m_skinTexture = new QOpenGLTexture(skin.mirrored());
m_skinTexture->setMinificationFilter(QOpenGLTexture::Nearest);
m_skinTexture->setMagnificationFilter(QOpenGLTexture::Nearest);
m_capeTexture = new QOpenGLTexture(cape.mirrored());
m_capeTexture->setMinificationFilter(QOpenGLTexture::Nearest);
m_capeTexture->setMagnificationFilter(QOpenGLTexture::Nearest);
}
Scene::~Scene()
{
for (auto array : { m_staticComponents, m_normalArms, m_slimArms }) {
for (auto g : array) {
delete g;
}
}
delete m_cape;
m_skinTexture->destroy();
delete m_skinTexture;
m_capeTexture->destroy();
delete m_capeTexture;
}
void Scene::draw(QOpenGLShaderProgram* program)
{
m_skinTexture->bind();
program->setUniformValue("texture", 0);
for (auto toDraw : { m_staticComponents, m_slim ? m_slimArms : m_normalArms }) {
for (auto g : toDraw) {
g->draw(program);
}
}
m_skinTexture->release();
if (m_capeVisible) {
m_capeTexture->bind();
program->setUniformValue("texture", 0);
m_cape->draw(program);
m_capeTexture->release();
}
}
void updateTexture(QOpenGLTexture* texture, const QImage& img)
{
if (texture) {
if (texture->isBound())
texture->release();
texture->destroy();
texture->create();
texture->setSize(img.width(), img.height());
texture->setData(img);
texture->setMinificationFilter(QOpenGLTexture::Nearest);
texture->setMagnificationFilter(QOpenGLTexture::Nearest);
}
}
void Scene::setSkin(const QImage& skin)
{
updateTexture(m_skinTexture, skin.mirrored());
}
void Scene::setMode(bool slim)
{
m_slim = slim;
}
void Scene::setCape(const QImage& cape)
{
updateTexture(m_capeTexture, cape.mirrored());
}
void Scene::setCapeVisible(bool visible)
{
m_capeVisible = visible;
}
} // namespace opengl
+46
View File
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "ui/dialogs/skins/draw/BoxGeometry.h"
#include <QOpenGLTexture>
namespace opengl {
class Scene {
public:
Scene(const QImage& skin, bool slim, const QImage& cape);
virtual ~Scene();
void draw(QOpenGLShaderProgram* program);
void setSkin(const QImage& skin);
void setCape(const QImage& cape);
void setMode(bool slim);
void setCapeVisible(bool visible);
private:
QVector<BoxGeometry*> m_staticComponents;
QVector<BoxGeometry*> m_normalArms;
QVector<BoxGeometry*> m_slimArms;
BoxGeometry* m_cape = nullptr;
QOpenGLTexture* m_skinTexture = nullptr;
QOpenGLTexture* m_capeTexture = nullptr;
bool m_slim = false;
bool m_capeVisible = false;
};
} // namespace opengl
@@ -0,0 +1,265 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ui/dialogs/skins/draw/SkinOpenGLWindow.h"
#include <QMouseEvent>
#include <QOpenGLBuffer>
#include <QVector2D>
#include <QVector3D>
#include <QtMath>
#include "minecraft/skins/SkinModel.h"
#include "rainbow.h"
#include "ui/dialogs/skins/draw/BoxGeometry.h"
#include "ui/dialogs/skins/draw/Scene.h"
SkinOpenGLWindow::SkinOpenGLWindow(SkinProvider* parent, QColor color)
: QOpenGLWindow(), QOpenGLFunctions(), m_baseColor(color), m_parent(parent)
{
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setDepthBufferSize(24);
setFormat(format);
}
SkinOpenGLWindow::~SkinOpenGLWindow()
{
// Make sure the context is current when deleting the texture
// and the buffers.
makeCurrent();
// double check if resources were initialized because they are not
// initialized together with the object
if (m_scene) {
delete m_scene;
}
if (m_background) {
delete m_background;
}
if (m_backgroundTexture) {
if (m_backgroundTexture->isCreated()) {
m_backgroundTexture->destroy();
}
delete m_backgroundTexture;
}
if (m_program) {
if (m_program->isLinked()) {
m_program->release();
}
m_program->removeAllShaders();
delete m_program;
}
doneCurrent();
}
void SkinOpenGLWindow::mousePressEvent(QMouseEvent* e)
{
// Save mouse press position
m_mousePosition = QVector2D(e->pos());
m_isMousePressed = true;
}
void SkinOpenGLWindow::mouseMoveEvent(QMouseEvent* event)
{
if (m_isMousePressed) {
int dx = event->x() - m_mousePosition.x();
int dy = event->y() - m_mousePosition.y();
m_yaw += dx * 0.5f;
m_pitch += dy * 0.5f;
// Normalize yaw to keep it manageable
if (m_yaw > 360.0f)
m_yaw -= 360.0f;
else if (m_yaw < 0.0f)
m_yaw += 360.0f;
m_mousePosition = QVector2D(event->pos());
update(); // Trigger a repaint
}
}
void SkinOpenGLWindow::mouseReleaseEvent([[maybe_unused]] QMouseEvent* e)
{
m_isMousePressed = false;
}
void SkinOpenGLWindow::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(0, 0, 1, 1);
initShaders();
generateBackgroundTexture(32, 32, 1);
QImage skin, cape;
bool slim = false;
if (m_parent) {
if (auto s = m_parent->getSelectedSkin()) {
skin = s->getTexture();
slim = s->getModel() == SkinModel::SLIM;
cape = m_parent->capes().value(s->getCapeId(), {});
}
}
m_scene = new opengl::Scene(skin, slim, cape);
m_background = opengl::BoxGeometry::Plane();
glEnable(GL_TEXTURE_2D);
}
void SkinOpenGLWindow::initShaders()
{
m_program = new QOpenGLShaderProgram(this);
// Compile vertex shader
if (!m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
close();
// Compile fragment shader
if (!m_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
close();
// Link shader pipeline
if (!m_program->link())
close();
// Bind shader pipeline for use
if (!m_program->bind())
close();
}
void SkinOpenGLWindow::resizeGL(int w, int h)
{
// Calculate aspect ratio
qreal aspect = qreal(w) / qreal(h ? h : 1);
const qreal zNear = .1, zFar = 1000., fov = 45;
// Reset projection
m_projection.setToIdentity();
// Set perspective projection
m_projection.perspective(fov, aspect, zNear, zFar);
}
void SkinOpenGLWindow::paintGL()
{
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable depth buffer
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Enable back face culling
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_program->bind();
renderBackground();
// Calculate model view transformation
QMatrix4x4 matrix;
float yawRad = qDegreesToRadians(m_yaw);
float pitchRad = qDegreesToRadians(m_pitch);
matrix.lookAt(QVector3D( //
m_distance * qCos(pitchRad) * qCos(yawRad), //
m_distance * qSin(pitchRad) - 8, //
m_distance * qCos(pitchRad) * qSin(yawRad)),
QVector3D(0, -8, 0), QVector3D(0, 1, 0));
// Set modelview-projection matrix
m_program->setUniformValue("mvp_matrix", m_projection * matrix);
m_scene->draw(m_program);
m_program->release();
}
void SkinOpenGLWindow::updateScene(SkinModel* skin)
{
if (skin && m_scene) {
m_scene->setMode(skin->getModel() == SkinModel::SLIM);
m_scene->setSkin(skin->getTexture());
update();
}
}
void SkinOpenGLWindow::updateCape(const QImage& cape)
{
if (m_scene) {
m_scene->setCapeVisible(!cape.isNull());
m_scene->setCape(cape);
update();
}
}
QColor calculateContrastingColor(const QColor& color)
{
constexpr float contrast = 0.2;
auto luma = Rainbow::luma(color);
if (luma < 0.5) {
return Rainbow::lighten(color, contrast);
} else {
return Rainbow::darken(color, contrast);
}
}
QImage generateChessboardImage(int width, int height, int tileSize, QColor baseColor)
{
QImage image(width, height, QImage::Format_RGB888);
auto white = baseColor;
auto black = calculateContrastingColor(baseColor);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
bool isWhite = ((x / tileSize) % 2) == ((y / tileSize) % 2);
image.setPixelColor(x, y, isWhite ? white : black);
}
}
return image;
}
void SkinOpenGLWindow::generateBackgroundTexture(int width, int height, int tileSize)
{
m_backgroundTexture = new QOpenGLTexture(generateChessboardImage(width, height, tileSize, m_baseColor));
m_backgroundTexture->setMinificationFilter(QOpenGLTexture::Nearest);
m_backgroundTexture->setMagnificationFilter(QOpenGLTexture::Nearest);
}
void SkinOpenGLWindow::renderBackground()
{
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE); // Disable depth buffer writing
m_backgroundTexture->bind();
QMatrix4x4 matrix;
m_program->setUniformValue("mvp_matrix", matrix);
m_program->setUniformValue("texture", 0);
m_background->draw(m_program);
m_backgroundTexture->release();
glDepthMask(GL_TRUE); // Re-enable depth buffer writing
glEnable(GL_DEPTH_TEST);
}
void SkinOpenGLWindow::wheelEvent(QWheelEvent* event)
{
// Adjust distance based on scroll
int delta = event->angleDelta().y(); // Positive for scroll up, negative for scroll down
m_distance -= delta * 0.01f; // Adjust sensitivity factor
m_distance = qMax(16.f, m_distance); // Clamp distance
update(); // Trigger a repaint
}
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QMatrix4x4>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QOpenGLWindow>
#include <QVector2D>
#include "minecraft/skins/SkinModel.h"
#include "ui/dialogs/skins/draw/BoxGeometry.h"
#include "ui/dialogs/skins/draw/Scene.h"
class SkinProvider {
public:
virtual ~SkinProvider() = default;
virtual SkinModel* getSelectedSkin() = 0;
virtual QHash<QString, QImage> capes() = 0;
};
class SkinOpenGLWindow : public QOpenGLWindow, protected QOpenGLFunctions {
Q_OBJECT
public:
SkinOpenGLWindow(SkinProvider* parent, QColor color);
virtual ~SkinOpenGLWindow();
void updateScene(SkinModel* skin);
void updateCape(const QImage& cape);
protected:
void mousePressEvent(QMouseEvent* e) override;
void mouseReleaseEvent(QMouseEvent* e) override;
void mouseMoveEvent(QMouseEvent* event) override;
void wheelEvent(QWheelEvent* event) override;
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;
void initShaders();
void generateBackgroundTexture(int width, int height, int tileSize);
void renderBackground();
private:
QOpenGLShaderProgram* m_program;
opengl::Scene* m_scene = nullptr;
QMatrix4x4 m_projection;
QVector2D m_mousePosition;
bool m_isMousePressed = false;
float m_distance = 48;
float m_yaw = 90; // Horizontal rotation angle
float m_pitch = 0; // Vertical rotation angle
opengl::BoxGeometry* m_background = nullptr;
QOpenGLTexture* m_backgroundTexture = nullptr;
QColor m_baseColor;
SkinProvider* m_parent = nullptr;
};