235
launcher/ui/dialogs/skins/draw/BoxGeometry.cpp
Normal file
235
launcher/ui/dialogs/skins/draw/BoxGeometry.cpp
Normal file
@@ -0,0 +1,235 @@
|
||||
|
||||
#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)
|
||||
};
|
||||
|
||||
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, indices.size(), GL_UNSIGNED_SHORT, nullptr);
|
||||
}
|
||||
|
||||
void BoxGeometry::initGeometry(float u, float v, float width, float height, float depth, float textureWidth, float textureHeight)
|
||||
{
|
||||
// auto positions = getVericles(m_size, m_position);
|
||||
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> vertices;
|
||||
vertices.reserve(positions.size()); // Reserve space for efficiency
|
||||
|
||||
for (int i = 0; i < positions.size(); ++i) {
|
||||
vertices.append(VertexData(positions[i], textureCord[i]));
|
||||
}
|
||||
|
||||
// Transfer vertex data to VBO 0
|
||||
m_vertexBuf.bind();
|
||||
m_vertexBuf.allocate(vertices.constData(), vertices.size() * sizeof(VertexData));
|
||||
|
||||
// Transfer index data to VBO 1
|
||||
m_indexBuf.bind();
|
||||
m_indexBuf.allocate(indices.constData(), indices.size() * sizeof(GLushort));
|
||||
}
|
||||
|
||||
void BoxGeometry::rotate(float angle, const QVector3D& vector)
|
||||
{
|
||||
m_matrix.rotate(angle, vector);
|
||||
}
|
||||
} // namespace opengl
|
||||
28
launcher/ui/dialogs/skins/draw/BoxGeometry.h
Normal file
28
launcher/ui/dialogs/skins/draw/BoxGeometry.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#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 });
|
||||
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;
|
||||
};
|
||||
} // namespace opengl
|
||||
115
launcher/ui/dialogs/skins/draw/Scene.cpp
Normal file
115
launcher/ui/dialogs/skins/draw/Scene.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
|
||||
#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(4, 12, 3), QVector3D(-6, -6, 0), QPoint(40, 16), QVector3D(3, 12, 4)),
|
||||
new opengl::BoxGeometry(QVector3D(4.5, 12.5, 3.5), QVector3D(-6, -6, 0), QPoint(40, 32), QVector3D(3, 12, 4)),
|
||||
// Left Arm
|
||||
new opengl::BoxGeometry(QVector3D(4, 12, 3), QVector3D(6, -6, 0), QPoint(32, 48), QVector3D(3, 12, 4)),
|
||||
new opengl::BoxGeometry(QVector3D(4.5, 12.5, 3.5), QVector3D(6, -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
|
||||
28
launcher/ui/dialogs/skins/draw/Scene.h
Normal file
28
launcher/ui/dialogs/skins/draw/Scene.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#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
|
||||
240
launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.cpp
Normal file
240
launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
#include "ui/dialogs/skins/draw/SkinOpenGLWidget.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
#include <QOpenGLBuffer>
|
||||
#include <QVector2D>
|
||||
#include <QVector3D>
|
||||
#include "minecraft/skins/SkinModel.h"
|
||||
#include "ui/dialogs/skins/SkinManageDialog.h"
|
||||
#include "ui/dialogs/skins/draw/Scene.h"
|
||||
|
||||
SkinOpenGLWidget::~SkinOpenGLWidget()
|
||||
{
|
||||
// Make sure the context is current when deleting the texture
|
||||
// and the buffers.
|
||||
makeCurrent();
|
||||
delete m_scene;
|
||||
glDeleteTextures(1, &m_chessboardTexture);
|
||||
doneCurrent();
|
||||
}
|
||||
|
||||
void SkinOpenGLWidget::mousePressEvent(QMouseEvent* e)
|
||||
{
|
||||
// Save mouse press position
|
||||
m_mousePosition = QVector2D(e->position());
|
||||
m_isMousePressed = true;
|
||||
}
|
||||
void SkinOpenGLWidget::mouseMoveEvent(QMouseEvent* event)
|
||||
{
|
||||
if (m_isMousePressed) {
|
||||
int dx = event->x() - m_mousePosition.x();
|
||||
int dy = event->y() - m_mousePosition.y();
|
||||
|
||||
// Update rotation angles based on mouse movement
|
||||
m_rotationX += dy;
|
||||
m_rotationY += dx;
|
||||
|
||||
m_mousePosition = QVector2D(event->position());
|
||||
update(); // Trigger a repaint
|
||||
}
|
||||
}
|
||||
void SkinOpenGLWidget::mouseReleaseEvent(QMouseEvent* e)
|
||||
{
|
||||
m_isMousePressed = false;
|
||||
}
|
||||
|
||||
void SkinOpenGLWidget::initializeGL()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
|
||||
glClearColor(0, 0, 1, 1);
|
||||
|
||||
initShaders();
|
||||
|
||||
m_chessboardTexture = generateChessboardTexture(512, 512, 16);
|
||||
|
||||
QImage skin, cape;
|
||||
bool slim = false;
|
||||
if (auto p = dynamic_cast<SkinManageDialog*>(parent()); p) {
|
||||
if (auto s = p->getSelectedSkin()) {
|
||||
skin = s->getTexture();
|
||||
slim = s->getModel() == SkinModel::SLIM;
|
||||
cape = p->capes().value(s->getCapeId(), {});
|
||||
}
|
||||
}
|
||||
|
||||
m_scene = new opengl::Scene(skin, slim, cape);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
void SkinOpenGLWidget::initShaders()
|
||||
{
|
||||
// Compile vertex shader
|
||||
if (!m_program.addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, R"(// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
#ifdef GL_ES
|
||||
// Set default precision to medium
|
||||
precision mediump int;
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform mat4 mvp_matrix;
|
||||
uniform mat4 model_matrix;
|
||||
|
||||
attribute vec4 a_position;
|
||||
attribute vec2 a_texcoord;
|
||||
|
||||
varying vec2 v_texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate vertex position in screen space
|
||||
gl_Position = mvp_matrix * model_matrix * a_position;
|
||||
|
||||
// Pass texture coordinate to fragment shader
|
||||
// Value will be automatically interpolated to fragments inside polygon faces
|
||||
v_texcoord = a_texcoord;
|
||||
}
|
||||
)"))
|
||||
close();
|
||||
|
||||
// Compile fragment shader
|
||||
if (!m_program.addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, R"(// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
#ifdef GL_ES
|
||||
// Set default precision to medium
|
||||
precision mediump int;
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform sampler2D texture;
|
||||
|
||||
varying vec2 v_texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Set fragment color from texture
|
||||
// gl_FragColor = texture2D(texture, v_texcoord);
|
||||
vec4 texColor = texture2D(texture, v_texcoord);
|
||||
if (texColor.a < 0.1) discard; // Optional: Discard fully transparent pixels
|
||||
gl_FragColor = texColor;
|
||||
}
|
||||
)"))
|
||||
close();
|
||||
|
||||
// Link shader pipeline
|
||||
if (!m_program.link())
|
||||
close();
|
||||
|
||||
// Bind shader pipeline for use
|
||||
if (!m_program.bind())
|
||||
close();
|
||||
}
|
||||
|
||||
void SkinOpenGLWidget::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 SkinOpenGLWidget::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);
|
||||
|
||||
renderBackground();
|
||||
|
||||
m_program.bind();
|
||||
|
||||
// Calculate model view transformation
|
||||
QMatrix4x4 matrix;
|
||||
matrix.translate(0.0, 6.0, -50.);
|
||||
matrix.rotate(m_rotationX, 1.0f, 0.0f, 0.0f);
|
||||
matrix.rotate(m_rotationY, 0.0f, 1.0f, 0.0f);
|
||||
|
||||
// Set modelview-projection matrix
|
||||
m_program.setUniformValue("mvp_matrix", m_projection * matrix);
|
||||
|
||||
m_scene->draw(&m_program);
|
||||
m_program.release();
|
||||
}
|
||||
|
||||
void SkinOpenGLWidget::updateScene(SkinModel* skin)
|
||||
{
|
||||
if (skin && m_scene) {
|
||||
m_scene->setMode(skin->getModel() == SkinModel::SLIM);
|
||||
m_scene->setSkin(skin->getTexture());
|
||||
update();
|
||||
}
|
||||
}
|
||||
void SkinOpenGLWidget::updateCape(const QImage& cape)
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->setCapeVisible(!cape.isNull());
|
||||
m_scene->setCape(cape);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
GLuint SkinOpenGLWidget::generateChessboardTexture(int width, int height, int tileSize)
|
||||
{
|
||||
std::vector<unsigned char> textureData(width * height * 3);
|
||||
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
bool isWhite = ((x / tileSize) % 2) == ((y / tileSize) % 2);
|
||||
unsigned char color = isWhite ? 100 : 50;
|
||||
|
||||
int index = (y * width + x) * 3;
|
||||
textureData[index] = color; // Red
|
||||
textureData[index + 1] = color; // Green
|
||||
textureData[index + 2] = color; // Blue
|
||||
}
|
||||
}
|
||||
|
||||
GLuint texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData.data());
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
void SkinOpenGLWidget::renderBackground()
|
||||
{
|
||||
glDepthMask(GL_FALSE); // Disable depth buffer writing
|
||||
glBindTexture(GL_TEXTURE_2D, m_chessboardTexture);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0.0f, 0.0f);
|
||||
glVertex3f(-1.0f, -1.0f, -0.5f); // Bottom-left
|
||||
glTexCoord2f(1.0f, 0.0f);
|
||||
glVertex3f(1.0f, -1.0f, -0.5f); // Bottom-right
|
||||
glTexCoord2f(1.0f, 1.0f);
|
||||
glVertex3f(1.0f, 1.0f, -0.5f); // Top-right
|
||||
glTexCoord2f(0.0f, 1.0f);
|
||||
glVertex3f(-1.0f, 1.0f, -0.5f); // Top-left
|
||||
glEnd();
|
||||
glDepthMask(GL_TRUE); // Re-enable depth buffer writing
|
||||
}
|
||||
49
launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.h
Normal file
49
launcher/ui/dialogs/skins/draw/SkinOpenGLWidget.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMatrix4x4>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QOpenGLTexture>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QVector2D>
|
||||
#include "minecraft/skins/SkinModel.h"
|
||||
#include "ui/dialogs/skins/draw/Scene.h"
|
||||
|
||||
class SkinOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
SkinOpenGLWidget(QWidget* parent = nullptr) : QOpenGLWidget(parent), QOpenGLFunctions() {}
|
||||
virtual ~SkinOpenGLWidget();
|
||||
|
||||
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 initializeGL() override;
|
||||
void resizeGL(int w, int h) override;
|
||||
void paintGL() override;
|
||||
|
||||
void initShaders();
|
||||
|
||||
GLuint generateChessboardTexture(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;
|
||||
int m_rotationX = 0, m_rotationY = 0;
|
||||
|
||||
// background
|
||||
GLuint m_chessboardTexture;
|
||||
};
|
||||
Reference in New Issue
Block a user