Compare commits
77 Commits
Version_0.
...
Version_1.
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e41b831047 | ||
![]() |
948578f506 | ||
![]() |
5c2f5503fc | ||
![]() |
c94a9c3462 | ||
![]() |
3be5793ffc | ||
![]() |
f5ee8a973d | ||
![]() |
8c2ca44f20 | ||
![]() |
e963b7538e | ||
![]() |
b58b7c47e5 | ||
![]() |
44e36b8b0d | ||
![]() |
1d5d20cfb8 | ||
![]() |
0bbc0da324 | ||
![]() |
552f86bf2d | ||
![]() |
a2f5324a3c | ||
![]() |
a221ed4957 | ||
![]() |
c28a12cd8f | ||
![]() |
0fefc6168b | ||
![]() |
ae84c55559 | ||
![]() |
191c0cbcea | ||
![]() |
fc7941a890 | ||
![]() |
24d26cfdfa | ||
![]() |
876c689a1d | ||
![]() |
fa8808fea8 | ||
![]() |
0735ef996d | ||
![]() |
dca6e61c4b | ||
![]() |
3758a2601c | ||
![]() |
9a5d09b80c | ||
![]() |
ee8705f9e4 | ||
![]() |
c96d05decb | ||
![]() |
d63d5276a1 | ||
![]() |
32b0a95afb | ||
![]() |
e863c68a77 | ||
![]() |
353d932147 | ||
![]() |
82ce8ad72a | ||
![]() |
0499982150 | ||
![]() |
faea3b0737 | ||
![]() |
a30f1b125c | ||
![]() |
b6ef34f988 | ||
![]() |
96379a9afb | ||
![]() |
481256e8ea | ||
![]() |
1cc4f1ca90 | ||
![]() |
53ac8c3e5b | ||
![]() |
5faf584d84 | ||
![]() |
8cf86a41eb | ||
![]() |
1c5a33cfa7 | ||
![]() |
4078347023 | ||
![]() |
46c4a166c4 | ||
![]() |
1918d5c844 | ||
![]() |
3118118953 | ||
![]() |
9b3d12dfeb | ||
![]() |
a47eefe92e | ||
![]() |
1c0ed61b34 | ||
![]() |
ef2c341a1a | ||
![]() |
5ab2f2eaf9 | ||
![]() |
5c5b9ac2f1 | ||
![]() |
e1e8e165fe | ||
![]() |
97a38d5260 | ||
![]() |
23ce58291e | ||
![]() |
232acedce7 | ||
![]() |
60cc7bb562 | ||
![]() |
9abd285239 | ||
![]() |
bcdc17c362 | ||
![]() |
b3a8b4bb81 | ||
![]() |
f81f03353d | ||
![]() |
9ac1a25954 | ||
![]() |
57df0a2e15 | ||
![]() |
927ce1cd0a | ||
![]() |
f4d8018f8f | ||
![]() |
806024f4f9 | ||
![]() |
0f379ba04a | ||
![]() |
8929717c9f | ||
![]() |
bbe657d030 | ||
![]() |
1ab385475b | ||
![]() |
9298d88260 | ||
![]() |
91b65bf6e3 | ||
![]() |
b4bd314450 | ||
![]() |
2524971d19 |
35
MeshViewerQt/Form Files/MainWindow.ui
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindowClass</class>
|
||||
<widget class="QMainWindow" name="MainWindowClass">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>600</width>
|
||||
<height>400</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget"/>
|
||||
<widget class="QToolBar" name="mainToolBar">
|
||||
<property name="movable">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
</attribute>
|
||||
<attribute name="toolBarBreak">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources>
|
||||
<include location="../Resources/MainWindow.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
72
MeshViewerQt/Header/FileInterface.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <QMatrix4x4>
|
||||
|
||||
//TODO: shouldn't be here
|
||||
enum ModelTyp {
|
||||
null,
|
||||
dynamicMesh,
|
||||
cloth,
|
||||
bone,
|
||||
staticMesh,
|
||||
shadowMesh = 6
|
||||
};
|
||||
|
||||
struct BoundingBox {
|
||||
float quaternion[4];
|
||||
float center[3];
|
||||
float extents[3];
|
||||
};
|
||||
|
||||
struct Segment {
|
||||
std::uint32_t textureIndex = 0;
|
||||
float* vertex = nullptr;
|
||||
float* uv = nullptr;
|
||||
std::vector<std::vector<std::uint32_t>> polyIndices; // indices into vertex array
|
||||
};
|
||||
|
||||
struct Model {
|
||||
std::string name = "";
|
||||
std::string parent = "";
|
||||
ModelTyp type = null; //TODO: should be removed
|
||||
std::int32_t renderFlags = -1; //TODO: should be removed
|
||||
QMatrix4x4 m4x4Translation;
|
||||
std::vector<Segment*> segmList;
|
||||
};
|
||||
|
||||
class FileInterface
|
||||
{
|
||||
public:
|
||||
FileInterface(const char* path)
|
||||
: m_vModels(new std::vector<Model*>)
|
||||
{
|
||||
//open file
|
||||
m_fsMesh.open(path, std::ios::in | std::ios::binary);
|
||||
|
||||
if (!m_fsMesh.is_open())
|
||||
throw std::invalid_argument(std::string("file not found: ") += path);
|
||||
};
|
||||
|
||||
virtual ~FileInterface()
|
||||
{
|
||||
// close file
|
||||
m_fsMesh.close();
|
||||
|
||||
//clean up
|
||||
m_vTextureNames.clear();
|
||||
};
|
||||
|
||||
protected:
|
||||
std::vector<Model*>* m_vModels;
|
||||
std::fstream m_fsMesh;
|
||||
std::vector<std::string> m_vTextureNames;
|
||||
BoundingBox m_sceneBbox;
|
||||
|
||||
virtual void import() = 0;
|
||||
|
||||
public:
|
||||
virtual std::vector<Model*>* getModels() const { return m_vModels; };
|
||||
virtual std::vector<std::string> getTextureNames() const { return m_vTextureNames; };
|
||||
virtual BoundingBox getBoundingBox() const { return m_sceneBbox; };
|
||||
};
|
32
MeshViewerQt/Header/MainWindow.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = Q_NULLPTR);
|
||||
~MainWindow();
|
||||
|
||||
// Variables
|
||||
private:
|
||||
Ui::MainWindowClass* ui;
|
||||
|
||||
// Functions
|
||||
private:
|
||||
void setupWindow();
|
||||
void import(const char* path);
|
||||
|
||||
// Slots
|
||||
private slots:
|
||||
void openFile();
|
||||
void aboutFile();
|
||||
|
||||
|
||||
// Override Functions
|
||||
protected:
|
||||
virtual void keyPressEvent(QKeyEvent * keyEvent) override final;
|
||||
};
|
34
MeshViewerQt/Header/MshFile.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "FileInterface.h"
|
||||
|
||||
|
||||
struct ChunkHeader {
|
||||
char name[5];
|
||||
std::uint32_t size;
|
||||
std::streampos position;
|
||||
};
|
||||
|
||||
|
||||
class MshFile : public FileInterface
|
||||
{
|
||||
public:
|
||||
MshFile(const char* path);
|
||||
~MshFile();
|
||||
|
||||
private:
|
||||
virtual void import() override final;
|
||||
|
||||
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t length);
|
||||
|
||||
void analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList);
|
||||
|
||||
void analyseMatdChunks(std::list<ChunkHeader*> &chunkList);
|
||||
|
||||
void analyseModlChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseGeomChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseSegmChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseClthChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
|
||||
void readVertex(Segment* dataDestination, std::streampos position);
|
||||
void readUV(Segment* dataDestination, std::streampos position);
|
||||
};
|
67
MeshViewerQt/Header/OpenGlViewer.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
#include <QOpenGLWidget>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLBuffer>
|
||||
#include <QOPenGLVertexArrayObject>
|
||||
#include <QOpenGlShaderProgram>
|
||||
#include "..\Header\Texture.h"
|
||||
#include "..\Header\FileInterface.h"
|
||||
|
||||
struct Vertex {
|
||||
GLfloat position[3];
|
||||
GLfloat uv[2];
|
||||
};
|
||||
|
||||
struct TextureData {
|
||||
bool alpha;
|
||||
std::uint32_t width;
|
||||
std::uint32_t height;
|
||||
std::vector<std::uint8_t>* data;
|
||||
};
|
||||
|
||||
class OpenGlViewer : public QOpenGLWidget, protected QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
OpenGlViewer(QWidget *parent);
|
||||
~OpenGlViewer();
|
||||
|
||||
private:
|
||||
// OpenGL ======================================
|
||||
int m_uniformMVP;
|
||||
GLuint m_oglTexture;
|
||||
GLuint m_vertexBuffer;
|
||||
QOpenGLVertexArrayObject m_vertexArray;
|
||||
QOpenGLShaderProgram* m_program = nullptr;
|
||||
|
||||
// Data ========================================
|
||||
std::vector<Model*>* m_vModels = nullptr;
|
||||
std::vector<TextureData*>* m_vTextures = nullptr;
|
||||
BoundingBox m_sceneBoundings;
|
||||
|
||||
// Transformation ==============================
|
||||
float m_fRotX = 0;
|
||||
float m_fRotY = 0;
|
||||
float m_fRotZ = 0;
|
||||
float m_fTranX = 0;
|
||||
float m_fTranY = 0;
|
||||
float m_fTranZ = 0;
|
||||
|
||||
// Camera ======================================
|
||||
float m_fFOV = 45.0f;
|
||||
float m_fMinView = 0.1f;
|
||||
float m_fMaxView = 100.0f;
|
||||
|
||||
private:
|
||||
virtual void initializeGL() override final;
|
||||
virtual void paintGL() override final;
|
||||
|
||||
void printContextInformation();
|
||||
QMatrix4x4 getModelMatrix(unsigned int index) const;
|
||||
QMatrix4x4 getMVPMatrix(unsigned int index) const;
|
||||
void deleteData();
|
||||
|
||||
public:
|
||||
void setData(std::vector<Model*>* models, std::vector<TextureData*>* textures, BoundingBox bbox);
|
||||
};
|
22
MeshViewerQt/Header/Texture.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
|
||||
class TextureTGA
|
||||
{
|
||||
public:
|
||||
TextureTGA(const char* filePath);
|
||||
~TextureTGA();
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>* vui8Pixels;
|
||||
std::uint32_t ui32BpP;
|
||||
std::uint32_t ui32Width;
|
||||
std::uint32_t ui32Height;
|
||||
|
||||
public:
|
||||
std::vector<std::uint8_t>* getData() const;
|
||||
bool hasAlpha() const;
|
||||
std::uint32_t getWidth() const;
|
||||
std::uint32_t getHeight() const;
|
||||
};
|
8
MeshViewerQt/Header/defines.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#define WINDOW_NAME "Mesh Viewer"
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
#define DEFAULT_STATUS_MESSAGE "Mesh Viewer pre alpha by Anakin"
|
||||
|
11
MeshViewerQt/Resources/MainWindow.qrc
Normal file
@@ -0,0 +1,11 @@
|
||||
<RCC>
|
||||
<qresource prefix="/MainWindow">
|
||||
<file>icon.ico</file>
|
||||
</qresource>
|
||||
<qresource prefix="/shaders">
|
||||
<file>simple.frag</file>
|
||||
<file>simple.vert</file>
|
||||
<file>TextureShader.frag</file>
|
||||
<file>TextureShader.vert</file>
|
||||
</qresource>
|
||||
</RCC>
|
12
MeshViewerQt/Resources/TextureShader.frag
Normal file
@@ -0,0 +1,12 @@
|
||||
#version 330
|
||||
|
||||
// Input
|
||||
in vec2 UV;
|
||||
|
||||
// Output
|
||||
out vec4 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = {255,0,0};//texture(textureSampler, UV).rgb;
|
||||
}
|
17
MeshViewerQt/Resources/TextureShader.vert
Normal file
@@ -0,0 +1,17 @@
|
||||
#version 330
|
||||
|
||||
// Input vertex data, different for all executions of this shader
|
||||
layout(location = 0) in vec3 vertexPosition;
|
||||
layout(location = 1) in vec3 vertexUV;
|
||||
|
||||
// Input that stay constant fpr the whole mesh
|
||||
uniform mat4 MVP;
|
||||
|
||||
// Output
|
||||
out vec2 UV;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = MVP * vec4(vertexPosition, 1);
|
||||
UV = vertexUV;
|
||||
}
|
BIN
MeshViewerQt/Resources/icon.ico
Normal file
After Width: | Height: | Size: 264 KiB |
8
MeshViewerQt/Resources/simple.frag
Normal file
@@ -0,0 +1,8 @@
|
||||
#version 330
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
10
MeshViewerQt/Resources/simple.vert
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in vec3 color;
|
||||
out vec4 vColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(position, 1.0);
|
||||
vColor = vec4(color, 1.0);
|
||||
}
|
151
MeshViewerQt/Source/MainWindow.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
#include "..\Header\MainWindow.h"
|
||||
#include "..\Header\OpenGlViewer.h"
|
||||
#include "..\Header\MshFile.h"
|
||||
#include "..\Header\defines.h"
|
||||
#include <QKeyEvent>
|
||||
#include <QMessageBox>
|
||||
#include <QFileDialog>
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// constructor/destructor
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui (new Ui::MainWindowClass)
|
||||
{
|
||||
setupWindow();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void MainWindow::setupWindow()
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
this->setWindowTitle(WINDOW_NAME);
|
||||
this->setWindowIcon(QIcon(":/MainWindow/icon.ico"));
|
||||
this->resize(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
|
||||
this->setCentralWidget(new OpenGlViewer(this));
|
||||
|
||||
ui->mainToolBar->addAction("Open File", this, &MainWindow::openFile);
|
||||
ui->mainToolBar->addAction("File Info", this, &MainWindow::aboutFile);
|
||||
|
||||
ui->statusBar->showMessage(DEFAULT_STATUS_MESSAGE);
|
||||
}
|
||||
|
||||
void MainWindow::import(const char * path)
|
||||
{
|
||||
// variables
|
||||
std::vector<Model*>* tmp_models = nullptr;
|
||||
std::vector<TextureData*>* tmp_textures = new std::vector<TextureData*>;
|
||||
std::vector<std::string> tmp_texNames;
|
||||
BoundingBox tmp_bbox;
|
||||
|
||||
// model file
|
||||
try
|
||||
{
|
||||
MshFile file(path);
|
||||
tmp_models = file.getModels();
|
||||
tmp_texNames = file.getTextureNames();
|
||||
tmp_bbox = file.getBoundingBox();
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
QMessageBox msg(this);
|
||||
msg.addButton(QMessageBox::Ok);
|
||||
msg.setText(QString::fromStdString(e.what()));
|
||||
msg.setIcon(QMessageBox::Critical);
|
||||
msg.setWindowTitle("Open File Error");
|
||||
|
||||
msg.exec();
|
||||
return;
|
||||
}
|
||||
|
||||
// parth to texture
|
||||
std::string tmp_path = path;
|
||||
while (tmp_path.back() != '/' && tmp_path.back() != '\\')
|
||||
tmp_path.pop_back();
|
||||
|
||||
// load all textures
|
||||
for (auto& texIt : tmp_texNames)
|
||||
{
|
||||
TextureData* new_data = new TextureData;
|
||||
try
|
||||
{
|
||||
TextureTGA tmp_texFile(std::string(tmp_path + texIt).c_str());
|
||||
|
||||
new_data->alpha = tmp_texFile.hasAlpha();
|
||||
new_data->width = tmp_texFile.getWidth();
|
||||
new_data->height = tmp_texFile.getHeight();
|
||||
new_data->data = tmp_texFile.getData();
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
new_data->alpha = true;
|
||||
new_data->width = 1;
|
||||
new_data->height = 1;
|
||||
new_data->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
|
||||
}
|
||||
|
||||
tmp_textures->push_back(new_data);
|
||||
}
|
||||
|
||||
// add a solid default color at the end (maybe there is an invalid index later)
|
||||
TextureData* new_data = new TextureData;
|
||||
new_data->alpha = true;
|
||||
new_data->width = 1;
|
||||
new_data->height = 1;
|
||||
new_data->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
|
||||
tmp_textures->push_back(new_data);
|
||||
|
||||
// clean up texture name list
|
||||
tmp_texNames.clear();
|
||||
|
||||
// give the data to the viewer
|
||||
OpenGlViewer* tmp_viewer = dynamic_cast<OpenGlViewer*>(centralWidget());
|
||||
tmp_viewer->setData(tmp_models, tmp_textures, tmp_bbox);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private slots
|
||||
|
||||
void MainWindow::openFile()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "../Release/Msh", "Mesh (*.msh)");
|
||||
|
||||
import(fileName.toStdString().c_str());
|
||||
}
|
||||
|
||||
void MainWindow::aboutFile()
|
||||
{
|
||||
//TODO: Open Window with file information
|
||||
QMessageBox* dialog = new QMessageBox(QMessageBox::Information, WINDOW_NAME, "File Info", QMessageBox::StandardButton::Close, this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
|
||||
dialog->setDetailedText("This is the cool detailed text\n");
|
||||
dialog->exec();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// events
|
||||
|
||||
void MainWindow::keyPressEvent(QKeyEvent * keyEvent)
|
||||
{
|
||||
switch (keyEvent->key())
|
||||
{
|
||||
case Qt::Key::Key_Escape:
|
||||
close();
|
||||
break;
|
||||
}
|
||||
|
||||
QMainWindow::keyPressEvent(keyEvent);
|
||||
}
|
569
MeshViewerQt/Source/MshFile.cpp
Normal file
@@ -0,0 +1,569 @@
|
||||
#include "..\Header\MshFile.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
// helper function to save data from file to any variable type
|
||||
#define F2V(variableName) reinterpret_cast<char*>(&variableName)
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public constructor/destructor
|
||||
|
||||
MshFile::MshFile(const char * path)
|
||||
: FileInterface(path)
|
||||
{
|
||||
import();
|
||||
}
|
||||
|
||||
MshFile::~MshFile()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void MshFile::import()
|
||||
{
|
||||
// go to file size information
|
||||
m_fsMesh.seekg(4);
|
||||
|
||||
std::uint32_t tmp_fileSize;
|
||||
std::list<ChunkHeader*> tmp_mainChunks;
|
||||
|
||||
// get all chunks under HEDR
|
||||
m_fsMesh.read(F2V(tmp_fileSize), sizeof(tmp_fileSize));
|
||||
loadChunks(tmp_mainChunks, m_fsMesh.tellg(), tmp_fileSize);
|
||||
|
||||
// evaulate HEDR subchunks (= find MSH2)
|
||||
for (ChunkHeader* it : tmp_mainChunks)
|
||||
{
|
||||
if (!strcmp("MSH2", it->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tmp_msh2Chunks;
|
||||
loadChunks(tmp_msh2Chunks, it->position, it->size);
|
||||
|
||||
// evaluate MSH2 subchunks
|
||||
analyseMsh2Chunks(tmp_msh2Chunks);
|
||||
|
||||
// clean up
|
||||
while (!tmp_msh2Chunks.empty())
|
||||
{
|
||||
ChunkHeader* curs = tmp_msh2Chunks.front();
|
||||
tmp_msh2Chunks.pop_front();
|
||||
delete curs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
while (!tmp_mainChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_mainChunks.front();
|
||||
tmp_mainChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t length)
|
||||
{
|
||||
// jump to first chunk
|
||||
m_fsMesh.seekg(start);
|
||||
|
||||
do
|
||||
{
|
||||
ChunkHeader* tmp_header = new ChunkHeader();
|
||||
|
||||
// get information
|
||||
m_fsMesh.read(F2V(tmp_header->name[0]), sizeof(tmp_header->name) - 1);
|
||||
m_fsMesh.read(F2V(tmp_header->size), sizeof(tmp_header->size));
|
||||
tmp_header->position = m_fsMesh.tellg();
|
||||
|
||||
// store information
|
||||
destination.push_back(tmp_header);
|
||||
|
||||
// jump to next header
|
||||
m_fsMesh.seekg(tmp_header->size, std::ios_base::cur);
|
||||
|
||||
// out of file. Maybe a size information is corrupted
|
||||
if (!m_fsMesh.good())
|
||||
{
|
||||
//TODO: different way for output
|
||||
std::cout << "WARNING: corrupted file. Trying to continue" << std::endl;
|
||||
m_fsMesh.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
} while (m_fsMesh.tellg() - start != length);
|
||||
}
|
||||
|
||||
void MshFile::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// scene information
|
||||
if (!strcmp("SINF", it->name))
|
||||
{
|
||||
// get SINF subchunks
|
||||
std::list<ChunkHeader*> tmp_sinfChunks;
|
||||
loadChunks(tmp_sinfChunks, it->position, it->size);
|
||||
|
||||
// evaluate SINF subchunks
|
||||
for (auto& it : tmp_sinfChunks)
|
||||
{
|
||||
if (!strcmp("BBOX", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
|
||||
// read in the quaternion
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_fsMesh.read(F2V(m_sceneBbox.quaternion[i]), sizeof(float));
|
||||
|
||||
//read in the center
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_fsMesh.read(F2V(m_sceneBbox.center[i]), sizeof(float));
|
||||
|
||||
//read in the extents
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_fsMesh.read(F2V(m_sceneBbox.extents[i]), sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
// clean up SINF subchunks
|
||||
for (ChunkHeader* it : tmp_sinfChunks)
|
||||
delete it;
|
||||
}
|
||||
|
||||
// material list
|
||||
else if (!strcmp("MATL", it->name))
|
||||
{
|
||||
// "useless" information how many MATD follow, jump over it
|
||||
m_fsMesh.seekg(it->position);
|
||||
m_fsMesh.seekg(sizeof(std::uint32_t), std::ios_base::cur);
|
||||
|
||||
// get all MATL subchunk
|
||||
std::list<ChunkHeader*> tmp_matlChunks;
|
||||
loadChunks(tmp_matlChunks, m_fsMesh.tellg(), it->size - 4);
|
||||
|
||||
// evaluate MATL subchunks
|
||||
for (auto& it : tmp_matlChunks)
|
||||
{
|
||||
// This shouldn't be anything else than MATD
|
||||
if (!strcmp("MATD", it->name))
|
||||
{
|
||||
// get all subchunks from MATD
|
||||
std::list<ChunkHeader*> tmp_matdChunks;
|
||||
loadChunks(tmp_matdChunks, it->position, it->size);
|
||||
|
||||
m_vTextureNames.push_back("");
|
||||
|
||||
// analyse MATD subchunks
|
||||
analyseMatdChunks(tmp_matdChunks);
|
||||
|
||||
// clean up MATD subchunks
|
||||
while (!tmp_matdChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_matdChunks.front();
|
||||
tmp_matdChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clean up MATL subchunks
|
||||
while (!tmp_matlChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_matlChunks.front();
|
||||
tmp_matlChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
|
||||
// model
|
||||
else if (!strcmp("MODL", it->name))
|
||||
{
|
||||
Model* new_model = new Model;
|
||||
|
||||
// get all MODL subchunks
|
||||
std::list<ChunkHeader*> tmp_chunks;
|
||||
loadChunks(tmp_chunks, it->position, it->size);
|
||||
|
||||
// evaluate MODL subchunks
|
||||
analyseModlChunks(new_model, tmp_chunks);
|
||||
|
||||
//clean up MODL subchunks
|
||||
while (!tmp_chunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_chunks.front();
|
||||
tmp_chunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
|
||||
// save Model data
|
||||
m_vModels->push_back(new_model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
if (!strcmp("TX0D", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_fsMesh.read(buffer, it->size);
|
||||
m_vTextureNames.back() = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseModlChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// model type
|
||||
if (!strcmp("MTYP", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
std::uint32_t tmp_type;
|
||||
m_fsMesh.read(F2V(tmp_type), sizeof(tmp_type));
|
||||
dataDestination->type = (ModelTyp)tmp_type;
|
||||
}
|
||||
|
||||
// parent name
|
||||
else if (!strcmp("PRNT", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_fsMesh.read(buffer, it->size);
|
||||
dataDestination->parent = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// model name
|
||||
else if (!strcmp("NAME", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_fsMesh.read(buffer, it->size);
|
||||
dataDestination->name = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// render flags
|
||||
else if (!strcmp("FLGS", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
m_fsMesh.read(F2V(dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
|
||||
}
|
||||
|
||||
// translation
|
||||
else if (!strcmp("TRAN", it->name))
|
||||
{
|
||||
float tmp_scale[3];
|
||||
float tmp_rotation[4];
|
||||
float tmp_trans[3];
|
||||
|
||||
m_fsMesh.seekg(it->position);
|
||||
|
||||
// read in the data
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_fsMesh.read(F2V(tmp_scale[i]), sizeof(float));
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_fsMesh.read(F2V(tmp_rotation[i]), sizeof(float));
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_fsMesh.read(F2V(tmp_trans[i]), sizeof(float));
|
||||
|
||||
// modify the matrix
|
||||
dataDestination->m4x4Translation.scale(tmp_scale[0], tmp_scale[1], tmp_scale[2]);
|
||||
dataDestination->m4x4Translation.rotate(QQuaternion(tmp_rotation[3], tmp_rotation[0], tmp_rotation[1], tmp_rotation[2]));
|
||||
dataDestination->m4x4Translation.translate(tmp_trans[0], tmp_trans[1], tmp_trans[2]);
|
||||
|
||||
}
|
||||
|
||||
// geometry data
|
||||
else if (!strcmp("GEOM", it->name))
|
||||
{
|
||||
// get all GEOM subchunks
|
||||
std::list<ChunkHeader*> tmp_geomChunks;
|
||||
loadChunks(tmp_geomChunks, it->position, it->size);
|
||||
|
||||
// evaluate GEOM subchunks
|
||||
analyseGeomChunks(dataDestination, tmp_geomChunks);
|
||||
|
||||
// clean up GEOM subchunks
|
||||
while (!tmp_geomChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_geomChunks.front();
|
||||
tmp_geomChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseGeomChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// segment
|
||||
if (!strcmp("SEGM", it->name))
|
||||
{
|
||||
// get all SEGM subchunks
|
||||
std::list<ChunkHeader*> tmp_segmChunks;
|
||||
loadChunks(tmp_segmChunks, it->position, it->size);
|
||||
|
||||
// evaluate SEGM subchunks
|
||||
analyseSegmChunks(dataDestination, tmp_segmChunks);
|
||||
|
||||
// clean up SEGM subchunk
|
||||
while (!tmp_segmChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_segmChunks.front();
|
||||
tmp_segmChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
|
||||
// cloth
|
||||
else if (!strcmp("CLTH", it->name))
|
||||
{
|
||||
// get all CLTH subchunks
|
||||
std::list<ChunkHeader*> tmp_clthChunks;
|
||||
loadChunks(tmp_clthChunks, it->position, it->size);
|
||||
|
||||
// evaluate CLTH subchunks
|
||||
analyseClthChunks(dataDestination, tmp_clthChunks);
|
||||
|
||||
// clean up CLTH subchunks
|
||||
while (!tmp_clthChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_clthChunks.front();
|
||||
tmp_clthChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseSegmChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
Segment* new_segment = new Segment;
|
||||
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// material index
|
||||
if (!strcmp("MATI", it->name))
|
||||
{
|
||||
m_fsMesh.seekg(it->position);
|
||||
m_fsMesh.read(F2V(new_segment->textureIndex), sizeof(new_segment->textureIndex));
|
||||
}
|
||||
|
||||
// position list (vertex)
|
||||
else if (!strcmp("POSL", it->name))
|
||||
{
|
||||
readVertex(new_segment, it->position);
|
||||
}
|
||||
|
||||
// normals
|
||||
/*else if (!strcmp("NRML", it->name))
|
||||
{
|
||||
fsMesh.seekg(it->position);
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
|
||||
// List of normals
|
||||
// long int - 4 - number of normal vectores stored in this list
|
||||
// float[3][] - 12 each - UVW vector for each vertex
|
||||
}*/
|
||||
|
||||
// uv
|
||||
else if (!strcmp("UV0L", it->name))
|
||||
{
|
||||
readUV(new_segment, it->position);
|
||||
}
|
||||
|
||||
// polygons (indices into vertex/uv list)
|
||||
else if (!strcmp("STRP", it->name))
|
||||
{
|
||||
// don't get null, bone, shadowMesh and hidden mesh indices
|
||||
if (dataDestination->type == null ||
|
||||
dataDestination->type == bone ||
|
||||
dataDestination->type == shadowMesh ||
|
||||
dataDestination->renderFlags == 1)
|
||||
continue;
|
||||
|
||||
// jump to the data section and read the size;
|
||||
std::uint32_t tmp_size;
|
||||
m_fsMesh.seekg(it->position);
|
||||
m_fsMesh.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
int highBitCount(0);
|
||||
std::vector<uint32_t> new_poly;
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size; i++)
|
||||
{
|
||||
// ReadData
|
||||
std::uint16_t tmp_value;
|
||||
m_fsMesh.read(F2V(tmp_value), sizeof(tmp_value));
|
||||
|
||||
// Check if highbit is set
|
||||
if (tmp_value >> 15)
|
||||
{
|
||||
highBitCount++;
|
||||
// remove the high bit, to get the actually value
|
||||
tmp_value = (std::uint16_t(tmp_value << 1) >> 1);
|
||||
}
|
||||
|
||||
// save data
|
||||
new_poly.push_back((std::uint32_t)tmp_value);
|
||||
|
||||
// if the last 2 highBits are set, it was a new poly
|
||||
if (highBitCount == 2)
|
||||
{
|
||||
// reset highBitCount
|
||||
highBitCount = 0;
|
||||
|
||||
// remove the last two values..
|
||||
std::uint32_t temp[2];
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
temp[i] = new_poly.back();
|
||||
new_poly.pop_back();
|
||||
}
|
||||
|
||||
// ..save the old polygon..
|
||||
new_segment->polyIndices.push_back(new_poly);
|
||||
|
||||
// ..and move the values to a new polygon
|
||||
new_poly.clear();
|
||||
for (int i = 1; i >= 0; i--)
|
||||
new_poly.push_back(temp[i]);
|
||||
|
||||
} // if high bit set
|
||||
|
||||
} // for all values
|
||||
|
||||
// save the last polygon (no 2 high bit followed)
|
||||
new_segment->polyIndices.push_back(new_poly);
|
||||
|
||||
// kick the first element, it's empty as a reason of the algo above;
|
||||
new_segment->polyIndices.erase(new_segment->polyIndices.begin());
|
||||
}
|
||||
}
|
||||
|
||||
dataDestination->segmList.push_back(new_segment);
|
||||
}
|
||||
|
||||
void MshFile::analyseClthChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
Segment* new_segment = new Segment;
|
||||
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// texture name
|
||||
if (!strcmp("CTEX", it->name))
|
||||
{
|
||||
// read the texture name
|
||||
m_fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_fsMesh.read(buffer, it->size);
|
||||
|
||||
// search if it is already known
|
||||
bool tmp_found(false);
|
||||
for (unsigned int i = 0; i < m_vTextureNames.size(); i++)
|
||||
{
|
||||
if (!strcmp(buffer, m_vTextureNames[i].c_str()))
|
||||
{
|
||||
// if found, save the index and stop searching
|
||||
new_segment->textureIndex = i;
|
||||
tmp_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if it was not found add the texturename to the list
|
||||
if (!tmp_found)
|
||||
{
|
||||
m_vTextureNames.push_back(std::string(buffer));
|
||||
new_segment->textureIndex = m_vTextureNames.size() - 1;
|
||||
}
|
||||
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// position list (vertex)
|
||||
else if (!strcmp("CPOS", it->name))
|
||||
{
|
||||
readVertex(new_segment, it->position);
|
||||
}
|
||||
|
||||
// uv
|
||||
else if (!strcmp("CUV0", it->name))
|
||||
{
|
||||
readUV(new_segment, it->position);
|
||||
}
|
||||
|
||||
// triangles (indices into vertex/uv list)
|
||||
else if (!strcmp("CMSH", it->name))
|
||||
{
|
||||
// jump to the data section and read the size;
|
||||
std::uint32_t tmp_size;
|
||||
m_fsMesh.seekg(it->position);
|
||||
m_fsMesh.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
std::vector<uint32_t> new_poly;
|
||||
|
||||
// for every triangle..
|
||||
for (unsigned int i = 0; i < tmp_size * 3; i += 3)
|
||||
{
|
||||
new_poly.clear();
|
||||
|
||||
// ..get the 3 indices and save them
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
std::uint32_t tmp_value;
|
||||
m_fsMesh.read(F2V(tmp_value), sizeof(std::uint32_t));
|
||||
new_poly.push_back(tmp_value);
|
||||
}
|
||||
|
||||
new_segment->polyIndices.push_back(new_poly);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dataDestination->segmList.push_back(new_segment);
|
||||
}
|
||||
|
||||
void MshFile::readVertex(Segment * dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tmp_size;
|
||||
m_fsMesh.seekg(position);
|
||||
m_fsMesh.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
dataDestination->vertex = new float[tmp_size * 3];
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size * 3; i++)
|
||||
m_fsMesh.read(F2V(dataDestination->vertex[i]), sizeof(float));
|
||||
}
|
||||
|
||||
void MshFile::readUV(Segment * dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tmp_size;
|
||||
m_fsMesh.seekg(position);
|
||||
m_fsMesh.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
dataDestination->uv = new float[tmp_size * 2];
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size * 2; i++)
|
||||
m_fsMesh.read(F2V(dataDestination->uv[i]), sizeof(float));
|
||||
}
|
372
MeshViewerQt/Source/OpenGlViewer.cpp
Normal file
@@ -0,0 +1,372 @@
|
||||
#include "..\Header\OpenGlViewer.h"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <QMessageBox>
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Defined values
|
||||
|
||||
//opengl
|
||||
#define DEFAULT_MAJOR_VERSION 4
|
||||
#define DEFAULT_MINOR_VERSION 5
|
||||
#define DEAFAULT_BACKGROUND 0.5000f, 0.8000f, 1.0000f, 0.0000f
|
||||
|
||||
//piplines
|
||||
#define VERTEX_INDEX_XYZ 0
|
||||
#define VERTEX_INDEX_UV 1
|
||||
|
||||
#define VERTEX_COMPONENTS_XYZ 3
|
||||
#define VERTEX_COMPONENTS_UV 2
|
||||
|
||||
#define VERTEX_SIZE_XYZ (sizeof(float) * VERTEX_COMPONENTS_XYZ)
|
||||
#define VERTEX_SIZE_UV (sizeof(float) * VERTEX_COMPONENTS_UV)
|
||||
|
||||
#define VERTEX_OFFSET_XYZ 0
|
||||
#define VERTEX_OFFSET_UV (VERTEX_SIZE_XYZ)
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public constructor/destructor
|
||||
|
||||
OpenGlViewer::OpenGlViewer(QWidget *parent)
|
||||
: QOpenGLWidget(parent)
|
||||
{
|
||||
QSurfaceFormat format;
|
||||
format.setRenderableType(QSurfaceFormat::OpenGL);
|
||||
format.setSamples(4);
|
||||
format.setProfile(QSurfaceFormat::CoreProfile);
|
||||
format.setVersion(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);
|
||||
setFormat(format);
|
||||
|
||||
//TODO: mouse, move, key, drag/drop, scroll, resize
|
||||
}
|
||||
|
||||
OpenGlViewer::~OpenGlViewer()
|
||||
{
|
||||
glDeleteTextures(1, &m_oglTexture);
|
||||
glDeleteBuffers(1, &m_vertexBuffer);
|
||||
m_vertexArray.destroy();
|
||||
delete m_program;
|
||||
|
||||
deleteData();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void OpenGlViewer::initializeGL()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
printContextInformation();
|
||||
|
||||
// set Background
|
||||
glClearColor(DEAFAULT_BACKGROUND);
|
||||
|
||||
//TODO: z-order?
|
||||
|
||||
// draw vertices only from one side
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
// Create texture
|
||||
glGenTextures(1, &m_oglTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, m_oglTexture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
|
||||
// Create Shader
|
||||
m_program = new QOpenGLShaderProgram();
|
||||
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/simple.vert");
|
||||
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/simple.frag");
|
||||
m_program->link();
|
||||
m_program->bind();
|
||||
|
||||
// get Uniform location
|
||||
//TODO: faster to give everything to shader and calculate there?
|
||||
m_uniformMVP = m_program->uniformLocation("MVP");
|
||||
|
||||
// Create Vertex Buffer
|
||||
glGenBuffers(1, &m_vertexBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
|
||||
|
||||
// Create Vertex Array Object
|
||||
m_vertexArray.create();
|
||||
m_vertexArray.bind();
|
||||
m_program->enableAttributeArray(0);
|
||||
m_program->enableAttributeArray(1);
|
||||
m_program->setAttributeBuffer(VERTEX_INDEX_XYZ, GL_FLOAT, VERTEX_OFFSET_XYZ, VERTEX_COMPONENTS_XYZ, sizeof(Vertex));
|
||||
m_program->setAttributeBuffer(VERTEX_INDEX_UV, GL_FLOAT, VERTEX_OFFSET_UV, VERTEX_COMPONENTS_UV, sizeof(Vertex));
|
||||
|
||||
// unbind everything
|
||||
m_vertexArray.release();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
m_program->release();
|
||||
|
||||
}
|
||||
|
||||
void OpenGlViewer::paintGL()
|
||||
{
|
||||
//TODO: paint here
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
m_program->bind();
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, m_oglTexture);
|
||||
m_program->setUniformValue("textureSampler", 0);
|
||||
|
||||
|
||||
if (m_vModels != nullptr)
|
||||
{
|
||||
int tmp_offset(0);
|
||||
|
||||
for (unsigned int modelIndex = 0; modelIndex < m_vModels->size(); modelIndex++)
|
||||
{
|
||||
// skip null, bones, shadowMesh, hidden things
|
||||
if (m_vModels->at(modelIndex)->type == null ||
|
||||
m_vModels->at(modelIndex)->type == bone ||
|
||||
m_vModels->at(modelIndex)->type == shadowMesh ||
|
||||
m_vModels->at(modelIndex)->renderFlags == 1)
|
||||
continue;
|
||||
|
||||
for (auto& segmentIterator : m_vModels->at(modelIndex)->segmList)
|
||||
{
|
||||
// set the texture
|
||||
std::uint32_t tmp_textureIndex = segmentIterator->textureIndex >= m_vTextures->size() ? m_vTextures->size() - 1 : segmentIterator->textureIndex;
|
||||
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
m_vTextures->at(tmp_textureIndex)->alpha ? GL_RGBA : GL_RGB,
|
||||
m_vTextures->at(tmp_textureIndex)->width,
|
||||
m_vTextures->at(tmp_textureIndex)->height,
|
||||
0,
|
||||
m_vTextures->at(tmp_textureIndex)->alpha ? GL_BGRA : GL_BGR,
|
||||
GL_UNSIGNED_BYTE,
|
||||
m_vTextures->at(tmp_textureIndex)->data->data()
|
||||
);
|
||||
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
// give the MVP to the shader
|
||||
m_program->setUniformValue(m_uniformMVP, getMVPMatrix(modelIndex));
|
||||
|
||||
// calculate the number of vertex
|
||||
unsigned int tmp_vertexCount(0);
|
||||
for (auto&it : segmentIterator->polyIndices)
|
||||
tmp_vertexCount += (it.size() - 2) * 3;
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, tmp_offset, tmp_vertexCount);
|
||||
|
||||
// increase the offset
|
||||
tmp_offset += tmp_vertexCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_program->release();
|
||||
|
||||
this->frameSwapped();
|
||||
}
|
||||
|
||||
void OpenGlViewer::printContextInformation()
|
||||
{
|
||||
QString glType;
|
||||
QString glVersion;
|
||||
QString glProfile;
|
||||
|
||||
glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
|
||||
glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
|
||||
|
||||
#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
|
||||
switch (format().profile())
|
||||
{
|
||||
CASE(NoProfile);
|
||||
CASE(CoreProfile);
|
||||
CASE(CompatibilityProfile);
|
||||
}
|
||||
#undef CASE
|
||||
|
||||
std::cout << glType.toStdString() << " - " << glVersion.toStdString() << " (" << glProfile.toStdString() << ")";
|
||||
}
|
||||
|
||||
QMatrix4x4 OpenGlViewer::getModelMatrix(unsigned int index) const
|
||||
{
|
||||
QMatrix4x4 tmp_parent;
|
||||
|
||||
for (unsigned int loop = 0; loop < m_vModels->size(); loop++)
|
||||
{
|
||||
if (!strcmp(m_vModels->at(index)->parent.c_str(), m_vModels->at(loop)->name.c_str()))
|
||||
{
|
||||
tmp_parent = getModelMatrix(loop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return tmp_parent * m_vModels->at(index)->m4x4Translation;
|
||||
}
|
||||
|
||||
QMatrix4x4 OpenGlViewer::getMVPMatrix(unsigned int index) const
|
||||
{
|
||||
QMatrix4x4 tmp_mvp;
|
||||
|
||||
// projection
|
||||
tmp_mvp.perspective(m_fFOV, float(QWidget::width()) / float(QWidget::height()), m_fMinView, m_fMaxView);
|
||||
|
||||
// view
|
||||
tmp_mvp.lookAt(QVector3D(m_fTranX, m_fTranY, m_fTranZ), QVector3D(m_fTranX, m_fTranY, m_fTranZ - 1), QVector3D(0, 0, 1));
|
||||
|
||||
// user controlled rotation
|
||||
tmp_mvp.rotate(m_fRotX, QVector3D(1, 0, 0));
|
||||
tmp_mvp.rotate(m_fRotY, QVector3D(0, 1, 0));
|
||||
tmp_mvp.rotate(m_fRotZ, QVector3D(0, 0, 1));
|
||||
|
||||
//scale to 1
|
||||
float maxExtent = std::max(std::max(m_sceneBoundings.extents[0], m_sceneBoundings.extents[1]), m_sceneBoundings.extents[2]);
|
||||
tmp_mvp.scale(1 / maxExtent);
|
||||
|
||||
// move to center
|
||||
tmp_mvp.translate(-m_sceneBoundings.center[0], -m_sceneBoundings.center[1], -m_sceneBoundings.center[2]);
|
||||
|
||||
return tmp_mvp * getModelMatrix(index);
|
||||
}
|
||||
|
||||
void OpenGlViewer::deleteData()
|
||||
{
|
||||
if (m_vModels != nullptr)
|
||||
{
|
||||
while (!m_vModels->empty())
|
||||
{
|
||||
// remove the last Model
|
||||
Model* modelVectorElement = m_vModels->back();
|
||||
m_vModels->pop_back();
|
||||
|
||||
while (!modelVectorElement->segmList.empty())
|
||||
{
|
||||
// remove the last Segment
|
||||
Segment* segmentVectorElement = modelVectorElement->segmList.back();
|
||||
modelVectorElement->segmList.pop_back();
|
||||
|
||||
// delete data from Segment
|
||||
delete[] segmentVectorElement->uv;
|
||||
delete[] segmentVectorElement->vertex;
|
||||
|
||||
while (!segmentVectorElement->polyIndices.empty())
|
||||
{
|
||||
// clear the poly vector and remove it from the list
|
||||
segmentVectorElement->polyIndices.back().clear();
|
||||
segmentVectorElement->polyIndices.pop_back();
|
||||
}
|
||||
|
||||
// delete the actual Segment
|
||||
delete segmentVectorElement;
|
||||
}
|
||||
|
||||
// delete the actual Model
|
||||
delete modelVectorElement;
|
||||
}
|
||||
|
||||
// delete the Model's Vector
|
||||
delete m_vModels;
|
||||
}
|
||||
|
||||
if (m_vTextures != nullptr)
|
||||
{
|
||||
while (!m_vTextures->empty())
|
||||
{
|
||||
// remove the last texture
|
||||
TextureData* cursor = m_vTextures->back();
|
||||
m_vTextures->pop_back();
|
||||
|
||||
cursor->data->clear();
|
||||
delete cursor->data;
|
||||
|
||||
//delete image
|
||||
delete cursor;
|
||||
}
|
||||
// delete the Textrue's Vector
|
||||
delete m_vTextures;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public functions
|
||||
|
||||
void OpenGlViewer::setData(std::vector<Model*>* models, std::vector<TextureData*>* textures, BoundingBox bbox)
|
||||
{
|
||||
// new Data, so clean up the old things
|
||||
deleteData();
|
||||
|
||||
// save the variables
|
||||
m_vModels = models;
|
||||
m_vTextures = textures;
|
||||
m_sceneBoundings = bbox;
|
||||
|
||||
// collect vertex data of all models
|
||||
std::vector<Vertex> tmp_bufferData;
|
||||
|
||||
for (auto& modIt : *m_vModels) // for every model chunk
|
||||
{
|
||||
for (auto& segIt : modIt->segmList) // for every cluster
|
||||
{
|
||||
for (auto& mshIt : segIt->polyIndices) // for every polygon
|
||||
{
|
||||
if (mshIt.size() >= 3) // multipoly
|
||||
{
|
||||
// for every triangle of the multi polygon
|
||||
for (unsigned int tri = 0; tri < mshIt.size() - 2; tri++)
|
||||
{
|
||||
// for every edge of the triangle
|
||||
for (int triEdge = 0; triEdge < 3; triEdge++)
|
||||
{
|
||||
Vertex tempVertex;
|
||||
// every edge has 3 coordinates
|
||||
for (int j = 0; j < 3; j++)
|
||||
tempVertex.position[j] = (GLfloat)segIt->vertex[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j];
|
||||
|
||||
// and 2 UV
|
||||
if (segIt->uv == NULL)
|
||||
{
|
||||
tempVertex.uv[0] = 1.0;
|
||||
tempVertex.uv[1] = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < 2; j++)
|
||||
tempVertex.uv[j] = (GLfloat)segIt->uv[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j];
|
||||
}
|
||||
tmp_bufferData.push_back(tempVertex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else // this shouldn't happen (polygon with less then 3 vertex)
|
||||
{
|
||||
deleteData();
|
||||
|
||||
QMessageBox msg(this);
|
||||
msg.addButton(QMessageBox::Ok);
|
||||
msg.setText("You have polygons with less then 3 vertices!");
|
||||
msg.setIcon(QMessageBox::Critical);
|
||||
msg.setWindowTitle("Open File Error");
|
||||
msg.exec();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
sizeof(Vertex) * tmp_bufferData.size(),
|
||||
tmp_bufferData.data(),
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
tmp_bufferData.clear();
|
||||
}
|
119
MeshViewerQt/Source/Texture.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "Texture.h"
|
||||
#include <fstream>
|
||||
|
||||
TextureTGA::TextureTGA(const char * filePath)
|
||||
{
|
||||
// open the file
|
||||
std::fstream fsPicture(filePath, std::ios::in | std::ios::binary);
|
||||
|
||||
if (!fsPicture.is_open())
|
||||
throw std::invalid_argument(std::string("file not found: ") += filePath);
|
||||
|
||||
// read in the header
|
||||
std::uint8_t ui8x18Header[19] = { 0 };
|
||||
fsPicture.read(reinterpret_cast<char*>(&ui8x18Header), sizeof(ui8x18Header)-1);
|
||||
|
||||
//get variables
|
||||
vui8Pixels = new std::vector<std::uint8_t>;
|
||||
bool bCompressed;
|
||||
std::uint32_t ui32IDLength;
|
||||
std::uint32_t ui32PicType;
|
||||
std::uint32_t ui32PaletteLength;
|
||||
std::uint32_t ui32Size;
|
||||
|
||||
// extract all information from header
|
||||
ui32IDLength = ui8x18Header[0];
|
||||
ui32PicType = ui8x18Header[2];
|
||||
ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5];
|
||||
ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12];
|
||||
ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14];
|
||||
ui32BpP = ui8x18Header[16];
|
||||
|
||||
// calculate some more information
|
||||
ui32Size = ui32Width * ui32Height * ui32BpP/8;
|
||||
bCompressed = ui32PicType == 9 || ui32PicType == 10;
|
||||
vui8Pixels->resize(ui32Size);
|
||||
|
||||
// jump to the data block
|
||||
fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur);
|
||||
|
||||
// If not compressed 24 or 32 bit
|
||||
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
|
||||
{
|
||||
fsPicture.read(reinterpret_cast<char*>(vui8Pixels->data()), ui32Size);
|
||||
}
|
||||
// else if compressed 24 or 32 bit
|
||||
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
|
||||
{
|
||||
std::uint8_t tempChunkHeader;
|
||||
std::uint8_t tempData[5];
|
||||
unsigned int tempByteIndex = 0;
|
||||
|
||||
do {
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempChunkHeader), sizeof(tempChunkHeader));
|
||||
|
||||
if (tempChunkHeader >> 7) // repeat count
|
||||
{
|
||||
// just use the first 7 bits
|
||||
tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1);
|
||||
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP/8);
|
||||
|
||||
for (int i = 0; i <= tempChunkHeader; i++)
|
||||
{
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[0];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[1];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[2];
|
||||
if(ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3];
|
||||
}
|
||||
}
|
||||
else // data count
|
||||
{
|
||||
// just use the first 7 bits
|
||||
tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1);
|
||||
|
||||
for (int i = 0; i <= tempChunkHeader; i++)
|
||||
{
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP/8);
|
||||
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[0];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[1];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[2];
|
||||
if (ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3];
|
||||
}
|
||||
}
|
||||
} while (tempByteIndex < ui32Size);
|
||||
}
|
||||
// not useable format
|
||||
else
|
||||
{
|
||||
fsPicture.close();
|
||||
throw std::invalid_argument("Invaild File Format! Required 24 or 31 Bit Image.");
|
||||
}
|
||||
|
||||
fsPicture.close();
|
||||
}
|
||||
|
||||
TextureTGA::~TextureTGA()
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t>* TextureTGA::getData() const
|
||||
{
|
||||
return vui8Pixels;
|
||||
}
|
||||
|
||||
bool TextureTGA::hasAlpha() const
|
||||
{
|
||||
return ui32BpP == 32;
|
||||
}
|
||||
|
||||
std::uint32_t TextureTGA::getWidth() const
|
||||
{
|
||||
return ui32Width;
|
||||
}
|
||||
|
||||
std::uint32_t TextureTGA::getHeight() const
|
||||
{
|
||||
return ui32Height;
|
||||
}
|
18
MeshViewerQt/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "Header\MainWindow.h"
|
||||
#include <QtWidgets/QApplication>
|
||||
|
||||
|
||||
int startGUI(int argc, char* argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exitstatus = startGUI(argc, argv);
|
||||
|
||||
return exitstatus;
|
||||
}
|
@@ -3,7 +3,6 @@
|
||||
#include <list>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <gl\glew.h>
|
||||
#include <glm\gtc\matrix_transform.hpp>
|
||||
|
||||
enum Mtyp {
|
||||
@@ -15,23 +14,32 @@ enum Mtyp {
|
||||
shadowMesh = 6
|
||||
};
|
||||
|
||||
struct Bbox {
|
||||
float quaternion[4];
|
||||
float center[3];
|
||||
float extents[3];
|
||||
};
|
||||
|
||||
struct ChunkHeader {
|
||||
char name[5];
|
||||
std::uint32_t size;
|
||||
std::streampos position;
|
||||
};
|
||||
|
||||
struct Segment {
|
||||
std::uint32_t textureIndex = 0;
|
||||
float* vertex = nullptr;
|
||||
float* uv = nullptr;
|
||||
std::vector<std::vector<std::uint32_t>> meshIndices; // indices into vertex array
|
||||
};
|
||||
|
||||
struct Modl {
|
||||
std::string name;
|
||||
std::string parent;
|
||||
Mtyp type;
|
||||
std::uint32_t renderFlags;
|
||||
glm::mat4 m4x4Translation;
|
||||
std::string texture;
|
||||
float* vertex;
|
||||
float* uv;
|
||||
std::uint32_t* mesh;
|
||||
std::uint32_t meshSize;
|
||||
std::string name = "";
|
||||
std::string parent = "";
|
||||
Mtyp type = null;
|
||||
std::int32_t renderFlags = -1;
|
||||
glm::mat4 m4x4Translation = glm::mat4(1.0f);
|
||||
std::vector<Segment*> segmLst;
|
||||
};
|
||||
|
||||
|
||||
@@ -43,13 +51,12 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
std::vector<Modl*> vModls;
|
||||
std::vector<Modl*>* vModls;
|
||||
std::fstream fsMesh;
|
||||
std::vector<std::string> vTextures;
|
||||
|
||||
Bbox boundingBox;
|
||||
|
||||
private:
|
||||
void setModlDefault(Modl* model);
|
||||
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end);
|
||||
void analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList);
|
||||
void analyseMatdChunks(std::list<ChunkHeader*> &chunkList);
|
||||
@@ -57,11 +64,13 @@ private:
|
||||
void analyseGeomChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseSegmChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseClthChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void readVertex(Modl* dataDestination, std::streampos position);
|
||||
void readUV(Modl* dataDestination, std::streampos position);
|
||||
void readVertex(Segment* dataDestination, std::streampos position);
|
||||
void readUV(Segment* dataDestination, std::streampos position);
|
||||
void quat2eul(float &quat0, float &quat1, float &quat2, float &quat3);
|
||||
|
||||
|
||||
public:
|
||||
std::vector<Modl*> getModels() const;
|
||||
|
||||
std::vector<Modl*>* getModels() const;
|
||||
std::vector<std::string> getTextureList() const;
|
||||
Bbox getBoundgBox() const;
|
||||
};
|
||||
|
@@ -1,11 +1,9 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <gl\glew.h>
|
||||
#include <gl\glfw3.h>
|
||||
#include <glm\glm.hpp>
|
||||
#include <vector>
|
||||
#include "Object.h"
|
||||
#include "Texture.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
#define VERTEX_INDEX_XYZ 0
|
||||
#define VERTEX_INDEX_UV 1
|
||||
@@ -72,8 +70,9 @@ private:
|
||||
// ========================================
|
||||
|
||||
// data
|
||||
std::vector<Modl*> vModels;
|
||||
std::vector<Modl*>* vModels = NULL;
|
||||
std::vector<textureData*> vTextures;
|
||||
Bbox sceneBoundingBox;
|
||||
|
||||
// transformation =========================
|
||||
//values
|
||||
@@ -82,7 +81,7 @@ private:
|
||||
float fRotationZ = 0;
|
||||
double dTranslationX = 0;
|
||||
double dTranslationY = 0;
|
||||
double dTranslationZ = 5;
|
||||
double dTranslationZ = 3;
|
||||
|
||||
// ========================================
|
||||
|
||||
@@ -96,12 +95,14 @@ private:
|
||||
// private functions
|
||||
private:
|
||||
void processInit();
|
||||
void deleteVectors();
|
||||
|
||||
void startGLFW();
|
||||
void createWindow();
|
||||
void startGLEW();
|
||||
void setCallbackFunctions();
|
||||
|
||||
glm::mat4 getModelMatrix(unsigned int index);
|
||||
glm::mat4 getMVPMatrix(unsigned int index);
|
||||
|
||||
|
||||
@@ -116,6 +117,7 @@ public:
|
||||
void addTransX(double value);
|
||||
void addTransY(double value);
|
||||
void addTransZ(double value);
|
||||
void resetView();
|
||||
|
||||
// main routine
|
||||
GLFWwindow* getWindow() const;
|
||||
|
@@ -9,22 +9,13 @@ public:
|
||||
~TextureTGA();
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t> vui8Pixels;
|
||||
bool bCompressed;
|
||||
std::uint32_t ui32IDLength;
|
||||
bool bColorTabel;
|
||||
std::uint32_t ui32PicType;
|
||||
std::uint32_t ui32PaletteBegin;
|
||||
std::uint32_t ui32PaletteLength;
|
||||
std::uint32_t ui32PaletteBpP;
|
||||
std::vector<std::uint8_t>* vui8Pixels;
|
||||
std::uint32_t ui32BpP;
|
||||
std::uint32_t ui32Width;
|
||||
std::uint32_t ui32Height;
|
||||
std::uint32_t ui32Size;
|
||||
std::uint32_t ui32BpP;
|
||||
std::uint32_t ui32Attribut;
|
||||
|
||||
public:
|
||||
std::vector<std::uint8_t> getData() const;
|
||||
std::vector<std::uint8_t>* getData() const;
|
||||
bool hasAlpha() const;
|
||||
std::uint32_t getWidth() const;
|
||||
std::uint32_t getHeight() const;
|
||||
|
@@ -9,3 +9,5 @@ extern void mouseMove(GLFWwindow *window, double xpos, double ypos);
|
||||
extern void mouseWheel(GLFWwindow *window, double xoffset, double yoffset);
|
||||
|
||||
extern void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods);
|
||||
|
||||
extern void dragNdrop(GLFWwindow* window, int count, const char** paths);
|
@@ -1,5 +1,7 @@
|
||||
#include "Object.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#define PI (4.0*atan(1.0))
|
||||
|
||||
|
||||
@@ -8,6 +10,8 @@
|
||||
|
||||
Object::Object(const char* path)
|
||||
{
|
||||
vModls = new std::vector<Modl*>;
|
||||
|
||||
// open file
|
||||
fsMesh.open(path, std::ios::in | std::ios::binary);
|
||||
|
||||
@@ -63,29 +67,12 @@ Object::~Object()
|
||||
{
|
||||
// clear texture list
|
||||
vTextures.clear();
|
||||
|
||||
// clear Model list (don't delete the elements)
|
||||
vModls.clear();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void Object::setModlDefault(Modl * model)
|
||||
{
|
||||
model->name = "";
|
||||
model->parent = "";
|
||||
model->type = null;
|
||||
model->renderFlags = -1;
|
||||
model->m4x4Translation = glm::mat4(1.0f);
|
||||
model->texture = "";
|
||||
model->vertex = NULL;
|
||||
model->uv = NULL;
|
||||
model->mesh = NULL;
|
||||
model->meshSize = 0;
|
||||
}
|
||||
|
||||
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
|
||||
{
|
||||
// jump to first chunk
|
||||
@@ -120,28 +107,59 @@ void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos sta
|
||||
|
||||
void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
if (!strcmp("MATL", (*it)->name))
|
||||
if (!strcmp("SINF", it->name))
|
||||
{
|
||||
std::list<ChunkHeader*> tempSinfChunks;
|
||||
loadChunks(tempSinfChunks, it->position, it->size);
|
||||
|
||||
// evaluate MATL subchunks
|
||||
for (auto& it : tempSinfChunks)
|
||||
{
|
||||
if (!strcmp("BBOX", it->name))
|
||||
{
|
||||
fsMesh.seekg(it->position);
|
||||
|
||||
// read in the quaternion
|
||||
for (int i = 0; i < 4; i++)
|
||||
fsMesh.read(reinterpret_cast<char*>(&boundingBox.quaternion[i]), sizeof(float));
|
||||
|
||||
//read in the center
|
||||
for (int i = 0; i < 3; i++)
|
||||
fsMesh.read(reinterpret_cast<char*>(&boundingBox.center[i]), sizeof(float));
|
||||
|
||||
//read in the extents
|
||||
for (int i = 0; i < 3; i++)
|
||||
fsMesh.read(reinterpret_cast<char*>(&boundingBox.extents[i]), sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
for (ChunkHeader* it : tempSinfChunks)
|
||||
delete it;
|
||||
}
|
||||
|
||||
else if (!strcmp("MATL", it->name))
|
||||
{
|
||||
// "useless" information how many MATD follow
|
||||
fsMesh.seekg((*it)->position);
|
||||
std::uint32_t tempMatdCount;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempMatdCount), sizeof(std::uint32_t));
|
||||
fsMesh.seekg(it->position);
|
||||
fsMesh.seekg(sizeof(std::uint32_t), std::ios_base::cur);
|
||||
|
||||
// get all MATD from MATL list
|
||||
std::list<ChunkHeader*> tempMatlChunks;
|
||||
loadChunks(tempMatlChunks, fsMesh.tellg(), (*it)->size - 4);
|
||||
loadChunks(tempMatlChunks, fsMesh.tellg(), it->size - 4);
|
||||
|
||||
// evaluate MATL subchunks
|
||||
for (std::list<ChunkHeader*>::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++)
|
||||
for (auto& it : tempMatlChunks)
|
||||
{
|
||||
// This shouldn't be anything else than MATD
|
||||
if (!strcmp("MATD", (*it)->name))
|
||||
if (!strcmp("MATD", it->name))
|
||||
{
|
||||
// get all subchunks from MATD
|
||||
std::list<ChunkHeader*> tempMatdChunks;
|
||||
loadChunks(tempMatdChunks, (*it)->position, (*it)->size);
|
||||
loadChunks(tempMatdChunks, it->position, it->size);
|
||||
|
||||
vTextures.push_back("");
|
||||
|
||||
// analyse MATD subchunks
|
||||
analyseMatdChunks(tempMatdChunks);
|
||||
@@ -163,18 +181,15 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
|
||||
tempMatlChunks.pop_front();
|
||||
delete tempCursor;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("MODL", (*it)->name))
|
||||
else if (!strcmp("MODL", it->name))
|
||||
{
|
||||
Modl* tempModl = new Modl;
|
||||
setModlDefault(tempModl);
|
||||
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tempChunks;
|
||||
loadChunks(tempChunks, (*it)->position, (*it)->size);
|
||||
loadChunks(tempChunks, it->position, it->size);
|
||||
|
||||
// evaluate MODL subchunks
|
||||
analyseModlChunks(tempModl, tempChunks);
|
||||
@@ -188,100 +203,84 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
|
||||
}
|
||||
|
||||
// save Model data
|
||||
vModls.push_back(tempModl);
|
||||
|
||||
continue;
|
||||
vModls->push_back(tempModl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
//TX1D, TX2D, TX3D
|
||||
if (!strcmp("TX0D", (*it)->name))
|
||||
if (!strcmp("TX0D", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
char* buffer = new char[(*it)->size + 1];
|
||||
fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
vTextures.push_back(buffer);
|
||||
delete buffer;
|
||||
continue;
|
||||
fsMesh.read(buffer, it->size);
|
||||
vTextures.back() = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
if (!strcmp("MTYP", (*it)->name))
|
||||
if (!strcmp("MTYP", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.seekg(it->position);
|
||||
std::uint32_t tempType;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType));
|
||||
dataDestination->type = (Mtyp)tempType;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("PRNT", (*it)->name))
|
||||
else if (!strcmp("PRNT", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size];
|
||||
*buffer = { 0 };
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
fsMesh.read(buffer, it->size);
|
||||
dataDestination->parent = buffer;
|
||||
delete buffer;
|
||||
continue;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
if (!strcmp("NAME", (*it)->name))
|
||||
else if (!strcmp("NAME", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size];
|
||||
*buffer = { 0 };
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
fsMesh.read(buffer, it->size);
|
||||
dataDestination->name = buffer;
|
||||
delete buffer;
|
||||
continue;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
if (!strcmp("FLGS", (*it)->name))
|
||||
else if (!strcmp("FLGS", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.seekg(it->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("TRAN", (*it)->name))
|
||||
else if (!strcmp("TRAN", it->name))
|
||||
{
|
||||
float tempScale[3];
|
||||
float tempRotation[4];
|
||||
float tempTrans[3];
|
||||
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.seekg(it->position);
|
||||
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempScale[2]), sizeof(float));
|
||||
for(int i = 0; i < 3; i++)
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempScale[i]), sizeof(float));
|
||||
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempRotation[0]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempRotation[1]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float));
|
||||
for (int i = 0; i < 4; i++)
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempRotation[i]), sizeof(float));
|
||||
|
||||
//calculate x,y,z rotation
|
||||
tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]),
|
||||
1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2)));
|
||||
tempRotation[1] = asin(2 * (tempRotation[0] * tempRotation[2] - tempRotation[3] * tempRotation[1]));
|
||||
tempRotation[2] = atan2(2 * (tempRotation[0] * tempRotation[3] + tempRotation[1] * tempRotation[2]),
|
||||
1 - 2 * (pow(tempRotation[2], 2) + pow(tempRotation[3], 2))) - PI;
|
||||
quat2eul(tempRotation[0], tempRotation[1], tempRotation[2], tempRotation[3]);
|
||||
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempTrans[0]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempTrans[1]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempTrans[2]), sizeof(float));
|
||||
for (int i = 0; i < 3; i++)
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempTrans[i]), sizeof(float));
|
||||
|
||||
dataDestination->m4x4Translation = glm::scale(
|
||||
dataDestination->m4x4Translation,
|
||||
@@ -311,14 +310,13 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
||||
glm::vec3(0, 0, 1)
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("GEOM", (*it)->name))
|
||||
else if (!strcmp("GEOM", it->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tempGeomChunks;
|
||||
loadChunks(tempGeomChunks, (*it)->position, (*it)->size);
|
||||
loadChunks(tempGeomChunks, it->position, it->size);
|
||||
|
||||
// evaluate GEOM subchunks
|
||||
analyseGeomChunks(dataDestination, tempGeomChunks);
|
||||
@@ -330,21 +328,19 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
||||
tempGeomChunks.pop_front();
|
||||
delete tempCursor;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
if (!strcmp("SEGM", (*it)->name))
|
||||
if (!strcmp("SEGM", it->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tempSegmChunks;
|
||||
loadChunks(tempSegmChunks, (*it)->position, (*it)->size);
|
||||
loadChunks(tempSegmChunks, it->position, it->size);
|
||||
|
||||
// evaluate SEGM subchunks
|
||||
analyseSegmChunks(dataDestination, tempSegmChunks);
|
||||
@@ -359,11 +355,11 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("CLTH", (*it)->name))
|
||||
if (!strcmp("CLTH", it->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tempClthChunks;
|
||||
loadChunks(tempClthChunks, (*it)->position, (*it)->size);
|
||||
loadChunks(tempClthChunks, it->position, it->size);
|
||||
|
||||
// evaluate CLTH subchunks
|
||||
analyseClthChunks(dataDestination, tempClthChunks);
|
||||
@@ -382,139 +378,177 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
||||
|
||||
void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
|
||||
{
|
||||
/*if (!strcmp("SHDW", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
/* shadow mesh geometry
|
||||
Segment* tempData = new Segment;
|
||||
|
||||
long int - 4 - number of vertex positions
|
||||
float[3][] - 12 each - vertex positions (XYZ)
|
||||
long int - 4 - number of edges
|
||||
short int[4][] - 8 each - edge the following 4 entries from one edge
|
||||
> short int - 2 - vertex index of this edge, referes to the vertex list
|
||||
> short int - 2 - Reference into an edge. Defines the target vertex (the local edge vertex of the referenced edge) to which the edge should be dran from the local vertex
|
||||
> short int - 2 - Second reference into an edge. In all example .msh files I've seen this always refers to the same vertex as the first edge reference
|
||||
> short int - 2 - MAX_VALUE of short integers (65535). Indicates the end of this edge
|
||||
* /
|
||||
continue;
|
||||
}*/
|
||||
|
||||
if (!strcmp("MATI", (*it)->name))
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
std::uint32_t tempIndex;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempIndex), sizeof(tempIndex));
|
||||
if (vTextures.size() <= tempIndex)
|
||||
if (!strcmp("MATI", it->name))
|
||||
{
|
||||
std::cout << "warning texture index <" << tempIndex << "> unknown" << std::endl;
|
||||
dataDestination->texture = "";
|
||||
continue;
|
||||
}
|
||||
dataDestination->texture = vTextures[tempIndex];
|
||||
continue;
|
||||
fsMesh.seekg(it->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempData->textureIndex), sizeof(tempData->textureIndex));
|
||||
}
|
||||
|
||||
if (!strcmp("POSL", (*it)->name))
|
||||
else if (!strcmp("POSL", it->name))
|
||||
{
|
||||
readVertex(dataDestination, (*it)->position);
|
||||
continue;
|
||||
readVertex(tempData, it->position);
|
||||
}
|
||||
|
||||
/*if (!strcmp("NRML", (*it)->name))
|
||||
/*else if (!strcmp("NRML", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.seekg(it->position);
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
|
||||
// List of normals
|
||||
// long int - 4 - number of normal vectores stored in this list
|
||||
// float[3][] - 12 each - UVW vector for each vertex
|
||||
continue;
|
||||
}*/
|
||||
|
||||
if (!strcmp("UV0L", (*it)->name))
|
||||
else if (!strcmp("UV0L", it->name))
|
||||
{
|
||||
readUV(dataDestination, (*it)->position);
|
||||
readUV(tempData, it->position);
|
||||
}
|
||||
|
||||
else if (!strcmp("STRP", it->name))
|
||||
{
|
||||
// don't get null, bone, shadowMesh and hidden mesh indices
|
||||
if (dataDestination->type == null ||
|
||||
dataDestination->type == bone ||
|
||||
dataDestination->type == shadowMesh ||
|
||||
dataDestination->renderFlags == 1)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("STRP", (*it)->name))
|
||||
// jump to the data section and read the size;
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.seekg(it->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
|
||||
|
||||
int highBitCount(0);
|
||||
std::vector<uint32_t> tempPoly; // = new std::vector<uint32_t>;
|
||||
|
||||
for (unsigned int i = 0; i < tempSize; i++)
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// ReadData
|
||||
std::uint16_t tempValue;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint16_t));
|
||||
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));
|
||||
|
||||
dataDestination->mesh = new std::uint32_t[dataDestination->meshSize];
|
||||
|
||||
for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
|
||||
// Check for highbit
|
||||
if (tempValue >> 15)
|
||||
{
|
||||
std::uint16_t tempValue[3];
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue[0]), sizeof(std::uint16_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue[1]), sizeof(std::uint16_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue[2]), sizeof(std::uint16_t));
|
||||
|
||||
if (!(tempValue[0] >> 15 && tempValue[1] >> 15 && !(tempValue[2] >> 15)))
|
||||
throw std::invalid_argument("invalid file. go and triangulate!");
|
||||
|
||||
tempValue[0] = (std::uint16_t(tempValue[0] << 1) >> 1);
|
||||
tempValue[1] = (std::uint16_t(tempValue[1] << 1) >> 1);
|
||||
|
||||
dataDestination->mesh[i] = (std::uint32_t)tempValue[0];
|
||||
dataDestination->mesh[i + 1] = (std::uint32_t)tempValue[1];
|
||||
dataDestination->mesh[i + 2] = (std::uint32_t)tempValue[2];
|
||||
highBitCount++;
|
||||
tempValue = (std::uint16_t(tempValue << 1) >> 1);
|
||||
}
|
||||
|
||||
continue;
|
||||
tempPoly.push_back((std::uint32_t)tempValue);
|
||||
|
||||
// new Polygon found
|
||||
if (highBitCount == 2)
|
||||
{
|
||||
// reset highBitCount
|
||||
highBitCount = 0;
|
||||
|
||||
// remove the last two values..
|
||||
std::uint32_t saveData[2];
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
saveData[i] = tempPoly.back();
|
||||
tempPoly.pop_back();
|
||||
}
|
||||
|
||||
// ..and save them in the new vector
|
||||
tempData->meshIndices.push_back(tempPoly);
|
||||
|
||||
tempPoly.clear();
|
||||
for (int i = 1; i >= 0; i--)
|
||||
tempPoly.push_back(saveData[i]);
|
||||
|
||||
} // if high bit set
|
||||
|
||||
} // for all values
|
||||
|
||||
// save the last values (where no 2 high bits follow);
|
||||
tempData->meshIndices.push_back(tempPoly);
|
||||
|
||||
// kick the first element, it's empty as a reason of the algo above;
|
||||
tempData->meshIndices.erase(tempData->meshIndices.begin());
|
||||
}
|
||||
}
|
||||
|
||||
dataDestination->segmLst.push_back(tempData);
|
||||
}
|
||||
|
||||
void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
|
||||
Segment* tempData = new Segment;
|
||||
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
if (!strcmp("CTEX", (*it)->name))
|
||||
if (!strcmp("CTEX", it->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.seekg(it->position);
|
||||
char* buffer = new char[it->size];
|
||||
*buffer = { 0 };
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
dataDestination->texture = buffer;
|
||||
delete buffer;
|
||||
continue;
|
||||
}
|
||||
fsMesh.read(buffer, it->size);
|
||||
|
||||
if (!strcmp("CPOS", (*it)->name))
|
||||
bool tempFound(false);
|
||||
|
||||
for (unsigned int index = 0; index < vTextures.size(); index++)
|
||||
{
|
||||
readVertex(dataDestination, (*it)->position);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("CUV0", (*it)->name))
|
||||
if (!strcmp(buffer, vTextures[index].c_str()))
|
||||
{
|
||||
readUV(dataDestination, (*it)->position);
|
||||
continue;
|
||||
tempData->textureIndex = index;
|
||||
tempFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp("CMSH", (*it)->name))
|
||||
if (!tempFound)
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));
|
||||
vTextures.push_back(std::string(buffer));
|
||||
tempData->textureIndex = vTextures.size() - 1;
|
||||
}
|
||||
|
||||
dataDestination->mesh = new std::uint32_t[dataDestination->meshSize * 3];
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
|
||||
else if (!strcmp("CPOS", it->name))
|
||||
{
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i]), sizeof(std::uint32_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i + 1]), sizeof(std::uint32_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i + 2]), sizeof(std::uint32_t));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
readVertex(tempData, it->position);
|
||||
}
|
||||
|
||||
void Object::readVertex(Modl* dataDestination, std::streampos position)
|
||||
else if (!strcmp("CUV0", it->name))
|
||||
{
|
||||
readUV(tempData, it->position);
|
||||
}
|
||||
|
||||
else if (!strcmp("CMSH", it->name))
|
||||
{
|
||||
// jump to the data section and read the size;
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.seekg(it->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
|
||||
|
||||
std::vector<uint32_t> tempPoly;
|
||||
|
||||
// for every triangle..
|
||||
for (unsigned int i = 0; i < tempSize * 3; i += 3)
|
||||
{
|
||||
tempPoly.clear();
|
||||
|
||||
// ..get the 3 indices and save them
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
std::uint32_t tempValue;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint32_t));
|
||||
tempPoly.push_back(tempValue);
|
||||
}
|
||||
tempData->meshIndices.push_back(tempPoly);
|
||||
}
|
||||
}
|
||||
}
|
||||
dataDestination->segmLst.push_back(tempData);
|
||||
}
|
||||
|
||||
void Object::readVertex(Segment* dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.seekg(position);
|
||||
@@ -526,7 +560,7 @@ void Object::readVertex(Modl* dataDestination, std::streampos position)
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i]), sizeof(float));
|
||||
}
|
||||
|
||||
void Object::readUV(Modl* dataDestination, std::streampos position)
|
||||
void Object::readUV(Segment* dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.seekg(position);
|
||||
@@ -538,15 +572,43 @@ void Object::readUV(Modl* dataDestination, std::streampos position)
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->uv[i]), sizeof(float));
|
||||
}
|
||||
|
||||
void Object::quat2eul(float &quat0, float &quat1, float & quat2, float &quat3)
|
||||
{
|
||||
float ysqr = quat1 * quat1;
|
||||
float t0 = -2.0f * (ysqr + quat2 * quat2) + 1.0f;
|
||||
float t1 = +2.0f * (quat0 * quat1 - quat3 * quat2);
|
||||
float t2 = -2.0f * (quat0 * quat2 + quat3 * quat1);
|
||||
float t3 = +2.0f * (quat1 * quat2 - quat3 * quat0);
|
||||
float t4 = -2.0f * (quat0 * quat0 + ysqr) + 1.0f;
|
||||
|
||||
t2 = t2 > 1.0f ? 1.0f : t2;
|
||||
t2 = t2 < -1.0f ? -1.0f : t2;
|
||||
|
||||
quat1 = std::asin(t2);
|
||||
quat0 = std::atan2(t3, t4);
|
||||
quat2 = std::atan2(t1, t0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public getter
|
||||
|
||||
std::vector<Modl*> Object::getModels() const
|
||||
std::vector<Modl*>* Object::getModels() const
|
||||
{
|
||||
return vModls;
|
||||
}
|
||||
|
||||
std::vector<std::string> Object::getTextureList() const
|
||||
{
|
||||
return vTextures;
|
||||
}
|
||||
|
||||
Bbox Object::getBoundgBox() const
|
||||
{
|
||||
return boundingBox;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public functions
|
||||
|
@@ -1,17 +1,14 @@
|
||||
#include <gl\glew.h>
|
||||
#include <gl\glfw3.h>
|
||||
#include <Windows.h>
|
||||
#include "OpenGLController.h"
|
||||
#include "callback.h"
|
||||
|
||||
#include <glm\gtc\matrix_transform.hpp>
|
||||
#include "shader.hpp"
|
||||
#include "Texture.h"
|
||||
#include <iostream>
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
#include <glm\gtc\matrix_transform.hpp>
|
||||
|
||||
|
||||
#define VERTEX_SHADER "Shader/TextureShader.vert"
|
||||
#define FRAGMENT_SHADER "Shader/TextureShader.frag"
|
||||
#define MAX_MODEL_COUNT 1000
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public constructor/destructor
|
||||
@@ -31,24 +28,7 @@ OpenGLController::~OpenGLController()
|
||||
glDeleteTextures(1, &gluiSamplerID);
|
||||
glfwTerminate();
|
||||
|
||||
while (!vModels.empty())
|
||||
{
|
||||
Modl* cursor = vModels.back();
|
||||
vModels.pop_back();
|
||||
|
||||
delete cursor->uv;
|
||||
delete cursor->mesh;
|
||||
delete cursor->vertex;
|
||||
delete cursor;
|
||||
}
|
||||
|
||||
while (!vTextures.empty())
|
||||
{
|
||||
textureData* cursor = vTextures.back();
|
||||
vTextures.pop_back();
|
||||
delete cursor->data;
|
||||
delete cursor;
|
||||
}
|
||||
deleteVectors();
|
||||
}
|
||||
|
||||
|
||||
@@ -127,6 +107,47 @@ void OpenGLController::processInit()
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
}
|
||||
|
||||
void OpenGLController::deleteVectors()
|
||||
{
|
||||
if (vModels != NULL)
|
||||
{
|
||||
while (!vModels->empty())
|
||||
{
|
||||
Modl* modelVectorElement = vModels->back();
|
||||
vModels->pop_back();
|
||||
|
||||
while (!modelVectorElement->segmLst.empty())
|
||||
{
|
||||
Segment* segmentVectorElement = modelVectorElement->segmLst.back();
|
||||
modelVectorElement->segmLst.pop_back();
|
||||
|
||||
delete[] segmentVectorElement->uv;
|
||||
delete[] segmentVectorElement->vertex;
|
||||
|
||||
while (!segmentVectorElement->meshIndices.empty())
|
||||
{
|
||||
segmentVectorElement->meshIndices.back().clear();
|
||||
segmentVectorElement->meshIndices.pop_back();
|
||||
}
|
||||
|
||||
delete segmentVectorElement;
|
||||
}
|
||||
|
||||
delete modelVectorElement;
|
||||
}
|
||||
delete vModels;
|
||||
}
|
||||
|
||||
while (!vTextures.empty())
|
||||
{
|
||||
textureData* cursor = vTextures.back();
|
||||
vTextures.pop_back();
|
||||
cursor->data->clear();
|
||||
delete cursor->data;
|
||||
delete cursor;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLController::startGLFW()
|
||||
{
|
||||
if (!glfwInit())
|
||||
@@ -182,6 +203,23 @@ void OpenGLController::setCallbackFunctions()
|
||||
glfwSetWindowSizeCallback(pWindow, windowResize);
|
||||
glfwSetScrollCallback(pWindow, mouseWheel);
|
||||
glfwSetKeyCallback(pWindow, keyPress);
|
||||
glfwSetDropCallback(pWindow, dragNdrop);
|
||||
}
|
||||
|
||||
glm::mat4 OpenGLController::getModelMatrix(unsigned int index)
|
||||
{
|
||||
glm::mat4 tempParentMatrix = glm::mat4(1.0f);
|
||||
|
||||
for (unsigned int loop = 0; loop < vModels->size(); loop++)
|
||||
{
|
||||
if (!strcmp(vModels->at(index)->parent.c_str(), vModels->at(loop)->name.c_str()))
|
||||
{
|
||||
tempParentMatrix = getModelMatrix(loop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return tempParentMatrix * vModels->at(index)->m4x4Translation;
|
||||
}
|
||||
|
||||
glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
|
||||
@@ -196,18 +234,25 @@ glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
|
||||
glm::vec3(0, 1, 0)
|
||||
);
|
||||
|
||||
// Model
|
||||
//TODO for all
|
||||
glm::mat4 m4x4Model = vModels[index]->m4x4Translation;
|
||||
|
||||
// User controlled rotation
|
||||
glm::mat4 m4x4ModelRot = glm::mat4(1.0f);
|
||||
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationX, glm::vec3(1, 0, 0));
|
||||
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationY, glm::vec3(0, 1, 0));
|
||||
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationZ, glm::vec3(0, 0, 1));
|
||||
|
||||
// move to center
|
||||
glm::mat4 m4x4ModelCenter = glm::translate(
|
||||
glm::mat4(1.0f),
|
||||
glm::vec3(-sceneBoundingBox.center[0], -sceneBoundingBox.center[1], -sceneBoundingBox.center[2])
|
||||
);
|
||||
|
||||
//scale to 1
|
||||
float maxExtent = max(max(sceneBoundingBox.extents[0], sceneBoundingBox.extents[1]), sceneBoundingBox.extents[2]);
|
||||
glm::mat4 m4x4Normalize = glm::mat4(1.0f);
|
||||
m4x4Normalize = glm::scale(m4x4Normalize, glm::vec3(1/maxExtent, 1 / maxExtent, 1 / maxExtent));
|
||||
|
||||
// Return MVP
|
||||
return m4x4Projection * m4x4View * m4x4ModelRot * m4x4Model;
|
||||
return m4x4Projection * m4x4View * m4x4ModelRot * m4x4Normalize * m4x4ModelCenter * getModelMatrix(index);
|
||||
}
|
||||
|
||||
|
||||
@@ -254,6 +299,16 @@ void OpenGLController::addTransZ(double value)
|
||||
dTranslationZ += value;
|
||||
}
|
||||
|
||||
void OpenGLController::resetView()
|
||||
{
|
||||
dTranslationX = 0;
|
||||
dTranslationY = 0;
|
||||
dTranslationZ = 5;
|
||||
fRotationX = 0;
|
||||
fRotationY = 0;
|
||||
fRotationZ = 0;
|
||||
}
|
||||
|
||||
void OpenGLController::updateScene()
|
||||
{
|
||||
// get new matrices
|
||||
@@ -268,20 +323,34 @@ void OpenGLController::updateScene()
|
||||
// tell sampler to use texture unit 0
|
||||
glUniform1i(gluiSamplerID, 0);
|
||||
|
||||
if (vModels != NULL)
|
||||
{
|
||||
int instanceOffset(0);
|
||||
|
||||
for (int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
|
||||
for (unsigned int modelIndex = 0; modelIndex < vModels->size(); modelIndex++)
|
||||
{
|
||||
// skip null, bones, shadowMesh, hidden things (don't increase textrue index!!)
|
||||
if (vModels->at(modelIndex)->type == null ||
|
||||
vModels->at(modelIndex)->type == bone ||
|
||||
vModels->at(modelIndex)->type == shadowMesh ||
|
||||
vModels->at(modelIndex)->renderFlags == 1)
|
||||
continue;
|
||||
|
||||
for (auto& segIt : vModels->at(modelIndex)->segmLst)
|
||||
{
|
||||
// give texture to the shader
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
std::uint32_t tempTexIndex = segIt->textureIndex >= vTextures.size() ? vTextures.size() - 1 : segIt->textureIndex;
|
||||
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
vTextures[modelIndex]->alpha ? GL_RGBA : GL_RGB,
|
||||
vTextures[modelIndex]->width,
|
||||
vTextures[modelIndex]->height,
|
||||
vTextures[tempTexIndex]->alpha ? GL_RGBA : GL_RGB,
|
||||
vTextures[tempTexIndex]->width,
|
||||
vTextures[tempTexIndex]->height,
|
||||
0,
|
||||
vTextures[modelIndex]->alpha ? GL_BGRA : GL_BGR,
|
||||
vTextures[tempTexIndex]->alpha ? GL_BGRA : GL_BGR,
|
||||
GL_UNSIGNED_BYTE,
|
||||
vTextures[modelIndex]->data->data()
|
||||
vTextures[tempTexIndex]->data->data()
|
||||
);
|
||||
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
@@ -289,55 +358,90 @@ void OpenGLController::updateScene()
|
||||
// give the MVP to the shader
|
||||
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize);
|
||||
// calculate the number of vertex
|
||||
unsigned int vertexCount(0);
|
||||
for (auto& it : segIt->meshIndices)
|
||||
vertexCount += (it.size() - 2) * 3;
|
||||
|
||||
instanceOffset += vModels[modelIndex]->meshSize;
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount);
|
||||
|
||||
// increase the offset
|
||||
instanceOffset += vertexCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glfwSwapBuffers(pWindow);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void OpenGLController::loadMsh(const char * path)
|
||||
{
|
||||
// clean up old stuff first
|
||||
deleteVectors();
|
||||
|
||||
std::vector<std::string> tempTexList;
|
||||
|
||||
// get all models
|
||||
try
|
||||
{
|
||||
Object obj(path);
|
||||
vModels = obj.getModels();
|
||||
tempTexList = obj.getTextureList();
|
||||
sceneBoundingBox = obj.getBoundgBox();
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
|
||||
exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
// collect vertex data of all models
|
||||
std::vector<Vertex> tempBufferData;
|
||||
|
||||
for (auto& it : vModels)
|
||||
for (auto& modIt : *vModels) // for every model chunk
|
||||
{
|
||||
for (unsigned int i = 0; i < it->meshSize; i++)
|
||||
for (auto& segIt : modIt->segmLst) // for every cluster
|
||||
{
|
||||
for (auto& mshIt : segIt->meshIndices) // for every polygon
|
||||
{
|
||||
if (mshIt.size() >= 3) // multipoly
|
||||
{
|
||||
// for every triangle of the multi polygon
|
||||
for (unsigned int tri = 0; tri < mshIt.size() - 2; tri++)
|
||||
{
|
||||
// for every edge of the triangle
|
||||
for (int triEdge = 0; triEdge < 3; triEdge++)
|
||||
{
|
||||
Vertex tempVertex;
|
||||
tempVertex.position[0] = (GLfloat)it->vertex[it->mesh[i] * 3];
|
||||
tempVertex.position[1] = (GLfloat)it->vertex[it->mesh[i] * 3 + 1];
|
||||
tempVertex.position[2] = (GLfloat)it->vertex[it->mesh[i] * 3 + 2];
|
||||
// every edge has 3 coordinates
|
||||
for (int j = 0; j < 3; j++)
|
||||
tempVertex.position[j] = (GLfloat)segIt->vertex[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j];
|
||||
|
||||
if (it->uv == NULL)
|
||||
// and 2 UV
|
||||
if (segIt->uv == NULL)
|
||||
{
|
||||
tempVertex.uv[0] = 1.0;
|
||||
tempVertex.uv[1] = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tempVertex.uv[0] = (GLfloat)it->uv[it->mesh[i] * 2];
|
||||
tempVertex.uv[1] = (GLfloat)it->uv[it->mesh[i] * 2 + 1];
|
||||
for (int j = 0; j < 2; j++)
|
||||
tempVertex.uv[j] = (GLfloat)segIt->uv[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j];
|
||||
}
|
||||
|
||||
tempBufferData.push_back(tempVertex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else // this shouldn't happen (polygon with less then 3 vertex)
|
||||
{
|
||||
deleteVectors();
|
||||
MessageBox(NULL, "You have polygons with less then 3 vertices!", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fill the buffer
|
||||
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
|
||||
@@ -349,31 +453,31 @@ void OpenGLController::loadMsh(const char * path)
|
||||
);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
tempBufferData.clear();
|
||||
|
||||
// get textures
|
||||
for (auto& it : vModels)
|
||||
{
|
||||
textureData* tempData = new textureData;
|
||||
try
|
||||
{
|
||||
if (it->texture == "")
|
||||
throw std::invalid_argument("no texture name");
|
||||
|
||||
// get textures path
|
||||
std::string tempPath = path;
|
||||
|
||||
while (tempPath.back() != '/' && tempPath.back() != '\\')
|
||||
tempPath.pop_back();
|
||||
|
||||
TextureTGA tempTex(std::string(tempPath + it->texture).c_str());
|
||||
// load all textures;
|
||||
for (auto& texIt : tempTexList)
|
||||
{
|
||||
textureData* tempData = new textureData;
|
||||
|
||||
try
|
||||
{
|
||||
TextureTGA tempTex(std::string(tempPath + texIt).c_str());
|
||||
|
||||
tempData->alpha = tempTex.hasAlpha();
|
||||
tempData->width = tempTex.getWidth();
|
||||
tempData->height = tempTex.getHeight();
|
||||
tempData->data = new std::vector<std::uint8_t>(tempTex.getData());
|
||||
tempData->data = tempTex.getData();
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
GLubyte solidColor[4] = { 0, 0, 255, 255 };
|
||||
tempData->alpha = true;
|
||||
tempData->width = 1;
|
||||
tempData->height = 1;
|
||||
@@ -382,4 +486,14 @@ void OpenGLController::loadMsh(const char * path)
|
||||
|
||||
vTextures.push_back(tempData);
|
||||
}
|
||||
|
||||
// add a solid default color at the end (maybe there is an invalid index later)
|
||||
textureData* tempData = new textureData;
|
||||
tempData->alpha = true;
|
||||
tempData->width = 1;
|
||||
tempData->height = 1;
|
||||
tempData->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
|
||||
vTextures.push_back(tempData);
|
||||
|
||||
tempTexList.clear();
|
||||
}
|
||||
|
@@ -1,7 +1,5 @@
|
||||
#include "Texture.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <Windows.h>
|
||||
|
||||
TextureTGA::TextureTGA(const char * filePath)
|
||||
{
|
||||
@@ -15,35 +13,26 @@ TextureTGA::TextureTGA(const char * filePath)
|
||||
std::uint8_t ui8x18Header[19] = { 0 };
|
||||
fsPicture.read(reinterpret_cast<char*>(&ui8x18Header), sizeof(ui8x18Header)-1);
|
||||
|
||||
//get variables
|
||||
vui8Pixels = new std::vector<std::uint8_t>;
|
||||
bool bCompressed;
|
||||
std::uint32_t ui32IDLength;
|
||||
std::uint32_t ui32PicType;
|
||||
std::uint32_t ui32PaletteLength;
|
||||
std::uint32_t ui32Size;
|
||||
|
||||
// extract all information from header
|
||||
ui32IDLength = ui8x18Header[0];
|
||||
bColorTabel = ui8x18Header[1] == 1;
|
||||
ui32PicType = ui8x18Header[2];
|
||||
ui32PaletteBegin = ui8x18Header[4] * 0x100 + ui8x18Header[3];
|
||||
ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5];
|
||||
ui32PaletteBpP = ui8x18Header[7];
|
||||
ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12];
|
||||
ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14];
|
||||
ui32BpP = ui8x18Header[16];
|
||||
ui32Attribut = ui8x18Header[17];
|
||||
|
||||
// calculate some more information
|
||||
ui32Size = ui32Width * ui32Height * ui32BpP/8;
|
||||
bCompressed = ui32PicType == 9 || ui32PicType == 10;
|
||||
vui8Pixels.resize(ui32Size);
|
||||
|
||||
/* consol output of the header
|
||||
std::cout << "Header\n"
|
||||
<< "ID l<>nge: " << ui32IDLength << std::endl
|
||||
<< "Farbtabelle: " << (int)bColorTabel << std::endl
|
||||
<< "Bildtype: " << ui32PicType << std::endl
|
||||
<< "Palletenbegin: " << ui32PaletteBegin << std::endl
|
||||
<< "Palletenl<6E>ngen: " << ui32PaletteLength << std::endl
|
||||
<< "Bits pro Palleteneintrag: " << ui32PaletteBpP << std::endl
|
||||
<< "Breite: " << ui32Width << std::endl
|
||||
<< "H<>he: " << ui32Height << std::endl
|
||||
<< "Bit pro Pixel: " << ui32BpP << std::endl
|
||||
<< "Bild Attribute: " << ui32Attribut << std::endl;*/
|
||||
vui8Pixels->resize(ui32Size);
|
||||
|
||||
// jump to the data block
|
||||
fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur);
|
||||
@@ -51,15 +40,14 @@ TextureTGA::TextureTGA(const char * filePath)
|
||||
// If not compressed 24 or 32 bit
|
||||
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
|
||||
{
|
||||
fsPicture.read(reinterpret_cast<char*>(vui8Pixels.data()), ui32Size);
|
||||
fsPicture.read(reinterpret_cast<char*>(vui8Pixels->data()), ui32Size);
|
||||
}
|
||||
// else if compressed 24 or 32 bit
|
||||
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
|
||||
{
|
||||
std::uint8_t tempChunkHeader;
|
||||
std::uint8_t tempData[5];
|
||||
int tempByteIndex = 0;
|
||||
std::size_t tempPixelIndex = 0;
|
||||
unsigned int tempByteIndex = 0;
|
||||
|
||||
do {
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempChunkHeader), sizeof(tempChunkHeader));
|
||||
@@ -73,10 +61,10 @@ TextureTGA::TextureTGA(const char * filePath)
|
||||
|
||||
for (int i = 0; i <= tempChunkHeader; i++)
|
||||
{
|
||||
vui8Pixels[tempByteIndex++] = tempData[0];
|
||||
vui8Pixels[tempByteIndex++] = tempData[1];
|
||||
vui8Pixels[tempByteIndex++] = tempData[2];
|
||||
if(ui32BpP == 32) vui8Pixels[tempByteIndex++] = tempData[3];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[0];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[1];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[2];
|
||||
if(ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3];
|
||||
}
|
||||
}
|
||||
else // data count
|
||||
@@ -88,10 +76,10 @@ TextureTGA::TextureTGA(const char * filePath)
|
||||
{
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP/8);
|
||||
|
||||
vui8Pixels[tempByteIndex++] = tempData[0];
|
||||
vui8Pixels[tempByteIndex++] = tempData[1];
|
||||
vui8Pixels[tempByteIndex++] = tempData[2];
|
||||
if (ui32BpP == 32) vui8Pixels[tempByteIndex++] = tempData[3];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[0];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[1];
|
||||
vui8Pixels->at(tempByteIndex++) = tempData[2];
|
||||
if (ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3];
|
||||
}
|
||||
}
|
||||
} while (tempByteIndex < ui32Size);
|
||||
@@ -108,10 +96,9 @@ TextureTGA::TextureTGA(const char * filePath)
|
||||
|
||||
TextureTGA::~TextureTGA()
|
||||
{
|
||||
vui8Pixels.clear();
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> TextureTGA::getData() const
|
||||
std::vector<std::uint8_t>* TextureTGA::getData() const
|
||||
{
|
||||
return vui8Pixels;
|
||||
}
|
||||
|
@@ -1,6 +1,3 @@
|
||||
//#include "callback.h"
|
||||
#include <gl\glew.h>
|
||||
#include <gl\glfw3.h>
|
||||
#include "OpenGLController.h"
|
||||
|
||||
|
||||
@@ -81,6 +78,8 @@ void mouseWheel(GLFWwindow *window, double xoffset, double yoffset)
|
||||
|
||||
void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
OpenGLController* controller = reinterpret_cast<OpenGLController*>(glfwGetWindowUserPointer(window));
|
||||
|
||||
if (action == GLFW_PRESS || action == GLFW_REPEAT)
|
||||
{
|
||||
switch (key)
|
||||
@@ -96,8 +95,22 @@ void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
case GLFW_KEY_PLUS_GER: case GLFW_KEY_KP_ADD:
|
||||
mouse.speed += 0.1;
|
||||
break;
|
||||
case GLFW_KEY_SPACE:
|
||||
controller->resetView();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dragNdrop(GLFWwindow* window, int count, const char** paths)
|
||||
{
|
||||
OpenGLController* controller = reinterpret_cast<OpenGLController*>(glfwGetWindowUserPointer(window));
|
||||
|
||||
if (count < 1)
|
||||
return;
|
||||
|
||||
controller->resetView();
|
||||
controller->loadMsh(paths[0]);
|
||||
}
|
||||
|
@@ -3,9 +3,6 @@
|
||||
#endif // DEBUG
|
||||
|
||||
#include "OpenGLController.h"
|
||||
#include "Object.h"
|
||||
#include <iostream>
|
||||
#include <Windows.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
@@ -20,8 +17,9 @@ int main(int argc, char** argv)
|
||||
else
|
||||
scene = OpenGLController::getInstance();
|
||||
|
||||
scene->loadMsh("..\\Release\\Msh\\multiModTex.msh");
|
||||
//scene->loadMsh("..\\Release\\Msh\\cubeTex.msh");
|
||||
#ifdef _DEBUG
|
||||
scene->loadMsh("..\\Release\\Msh\\triClothMan.msh");
|
||||
#endif // DEBUG
|
||||
|
||||
do {
|
||||
scene->updateScene();
|
||||
|
32
QtMeshViewer/Form Files/MainWindow.ui
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindowClass</class>
|
||||
<widget class="QMainWindow" name="MainWindowClass">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>600</width>
|
||||
<height>400</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget"/>
|
||||
<widget class="QToolBar" name="mainToolBar">
|
||||
<attribute name="toolBarArea">
|
||||
<enum>TopToolBarArea</enum>
|
||||
</attribute>
|
||||
<attribute name="toolBarBreak">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources>
|
||||
<include location="../Resources/Resources.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
104
QtMeshViewer/Header/FileInterface.h
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <QVector>
|
||||
#include <QVector2D>
|
||||
#include <QVector3D>
|
||||
#include <QStringList>
|
||||
#include <QMatrix4x4>
|
||||
#include <QQuaternion>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QObject>
|
||||
#include <..\Header\MainWindow.h>
|
||||
|
||||
|
||||
struct BoundingBox {
|
||||
QQuaternion rotation;
|
||||
QVector3D center;
|
||||
QVector3D extents;
|
||||
};
|
||||
|
||||
struct VertexData
|
||||
{
|
||||
QVector3D position;
|
||||
QVector2D texCoord;
|
||||
QVector3D normal;
|
||||
};
|
||||
|
||||
struct Segment {
|
||||
std::uint32_t textureIndex = 0;
|
||||
QVector<VertexData> vertices;
|
||||
QVector<GLuint> indices;
|
||||
};
|
||||
|
||||
struct Model {
|
||||
std::string name = "";
|
||||
std::string parent = "";
|
||||
QMatrix4x4 m4x4Translation;
|
||||
QQuaternion quadRotation;
|
||||
std::vector<Segment*> segmList;
|
||||
};
|
||||
|
||||
class FileInterface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FileInterface(QString path, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_models(new QVector<Model*>)
|
||||
, m_textureNames(new QStringList)
|
||||
{
|
||||
//open file
|
||||
m_file.open(path.toStdString().c_str(), std::ios::in | std::ios::binary);
|
||||
|
||||
if (!m_file.is_open())
|
||||
throw std::invalid_argument(std::string("ERROR: file not found: ") += path.toStdString());
|
||||
|
||||
MainWindow* tmp = dynamic_cast<MainWindow*>(parent->parent()->parent());
|
||||
if(tmp != NULL)
|
||||
connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(printMessage(QString, int)));
|
||||
|
||||
|
||||
};
|
||||
|
||||
virtual ~FileInterface()
|
||||
{
|
||||
// close file
|
||||
m_file.close();
|
||||
|
||||
//clean up
|
||||
m_textureNames->clear();
|
||||
delete m_textureNames;
|
||||
|
||||
for (Model* modelIt : *m_models)
|
||||
{
|
||||
for (Segment* segIt : modelIt->segmList)
|
||||
{
|
||||
segIt->indices.clear();
|
||||
segIt->vertices.clear();
|
||||
delete segIt;
|
||||
}
|
||||
modelIt->segmList.clear();
|
||||
|
||||
delete modelIt;
|
||||
}
|
||||
m_models->clear();
|
||||
delete m_models;
|
||||
};
|
||||
|
||||
protected:
|
||||
QVector<Model*>* m_models;
|
||||
std::fstream m_file;
|
||||
QStringList* m_textureNames;
|
||||
BoundingBox m_sceneBbox;
|
||||
|
||||
virtual void import() = 0;
|
||||
|
||||
public:
|
||||
virtual QVector<Model*>* getModels() const { return m_models; };
|
||||
virtual QStringList* getTextureNames() const { return m_textureNames; };
|
||||
virtual BoundingBox getBoundingBox() const { return m_sceneBbox; };
|
||||
|
||||
signals:
|
||||
void sendMessage(QString msg, int severity);
|
||||
};
|
45
QtMeshViewer/Header/GeometryEngine.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include "..\Header\FileInterface.h"
|
||||
#include <QObject>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QOpenGLBuffer>
|
||||
#include <QOpenGLTexture>
|
||||
#include <QVector>
|
||||
|
||||
struct DrawInformation {
|
||||
unsigned int offset;
|
||||
unsigned int size;
|
||||
unsigned int textureIndex;
|
||||
QMatrix4x4 modelMatrix;
|
||||
};
|
||||
|
||||
class GeometryEngine : public QObject, protected QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GeometryEngine(QObject *parent = Q_NULLPTR);
|
||||
virtual ~GeometryEngine();
|
||||
|
||||
private:
|
||||
QOpenGLBuffer m_arrayBuf;
|
||||
QOpenGLBuffer m_indexBuf;
|
||||
QVector<QOpenGLTexture*> m_textures;
|
||||
QVector<DrawInformation> m_drawList;
|
||||
BoundingBox m_boundings;
|
||||
|
||||
void loadTexture(QString filePath, QString fileName);
|
||||
void clearData();
|
||||
|
||||
public slots:
|
||||
void loadFile(QString filePath);
|
||||
void drawGeometry(QOpenGLShaderProgram *program);
|
||||
|
||||
signals:
|
||||
void requestResetView();
|
||||
void sendMessage(QString message, int severity);
|
||||
void requestUpdate();
|
||||
void sendFileInfo(QString name, QStringList textures, int vertices, int triangle);
|
||||
};
|
||||
|
34
QtMeshViewer/Header/MainWindow.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include <QByteArray>
|
||||
#include <QStringList>
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = Q_NULLPTR);
|
||||
~MainWindow();
|
||||
|
||||
private:
|
||||
Ui::MainWindowClass* ui;
|
||||
int m_curSeverity;
|
||||
void setupWidgets();
|
||||
QByteArray m_fileInfo;
|
||||
|
||||
|
||||
private slots:
|
||||
void openFile();
|
||||
void aboutFile();
|
||||
void aboutTool();
|
||||
|
||||
public slots:
|
||||
void printMessage(QString message, int severity);
|
||||
void setFileInfo(QString name, QStringList textures, int vertices, int triangle);
|
||||
|
||||
signals:
|
||||
void loadFile(QString);
|
||||
};
|
48
QtMeshViewer/Header/MshFile.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include "..\Header\FileInterface.h"
|
||||
|
||||
|
||||
struct ChunkHeader {
|
||||
char name[5];
|
||||
std::uint32_t size;
|
||||
std::streampos position;
|
||||
};
|
||||
|
||||
enum ModelTyp {
|
||||
null,
|
||||
dynamicMesh,
|
||||
cloth,
|
||||
bone,
|
||||
staticMesh,
|
||||
shadowMesh = 6
|
||||
};
|
||||
|
||||
class MshFile : public FileInterface
|
||||
{
|
||||
public:
|
||||
explicit MshFile(QString path, QObject *parent = Q_NULLPTR);
|
||||
virtual ~MshFile();
|
||||
|
||||
private:
|
||||
ModelTyp m_currentType = ModelTyp::null;
|
||||
std::int32_t m_currentRenderFlag = -1;
|
||||
|
||||
virtual void import() Q_DECL_OVERRIDE Q_DECL_FINAL;
|
||||
|
||||
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t length);
|
||||
|
||||
void analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList);
|
||||
|
||||
void analyseMatdChunks(std::list<ChunkHeader*> &chunkList);
|
||||
|
||||
void analyseModlChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseGeomChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseSegmChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseClthChunks(Model* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
|
||||
void readVertex(Segment* dataDestination, std::streampos position);
|
||||
void readUV(Segment* dataDestination, std::streampos position);
|
||||
|
||||
QMatrix4x4 getParentMatrix(std::string parent) const;
|
||||
QQuaternion getParentRotation(std::string parent) const;
|
||||
};
|
70
QtMeshViewer/Header/OglViewerWidget.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "geometryengine.h"
|
||||
|
||||
#include <QOpenGLWidget>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QMatrix4x4>
|
||||
#include <QQuaternion>
|
||||
#include <QVector2D>
|
||||
#include <QBasicTimer>
|
||||
#include <QOpenGLShaderProgram>
|
||||
|
||||
|
||||
class GeometryEngine;
|
||||
|
||||
class OglViewerWidget : public QOpenGLWidget, protected QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OglViewerWidget(QWidget *parent = 0);
|
||||
~OglViewerWidget();
|
||||
|
||||
signals:
|
||||
void loadFile(QString);
|
||||
|
||||
private:
|
||||
struct {
|
||||
bool left = false;
|
||||
bool right = false;
|
||||
QVector2D position;
|
||||
} m_mouse;
|
||||
|
||||
struct {
|
||||
bool x = true;
|
||||
bool y = true;
|
||||
bool z = true;
|
||||
} m_rotDirections;
|
||||
|
||||
QOpenGLShaderProgram m_program;
|
||||
GeometryEngine *m_dataEngine;
|
||||
|
||||
QMatrix4x4 m_projection;
|
||||
QVector3D m_translation;
|
||||
QQuaternion m_rotation;
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
||||
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
||||
void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
||||
void wheelEvent(QWheelEvent *e) Q_DECL_OVERRIDE;
|
||||
void dragEnterEvent(QDragEnterEvent *e) Q_DECL_OVERRIDE;
|
||||
void dropEvent(QDropEvent * event) Q_DECL_OVERRIDE;
|
||||
void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
|
||||
|
||||
void initializeGL() Q_DECL_OVERRIDE;
|
||||
void resizeGL(int w, int h) Q_DECL_OVERRIDE;
|
||||
void paintGL() Q_DECL_OVERRIDE;
|
||||
|
||||
private:
|
||||
void initShaders();
|
||||
void setConnections();
|
||||
|
||||
private slots:
|
||||
void resetView();
|
||||
|
||||
public slots:
|
||||
void changeDirection(int direction);
|
||||
};
|
||||
|
140
QtMeshViewer/Header/tga.h
Normal file
@@ -0,0 +1,140 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <QImage>
|
||||
#include <QColor>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
QImage loadTga(QString filePath, bool &success)
|
||||
{
|
||||
QImage img;
|
||||
if (!img.load(filePath))
|
||||
{
|
||||
|
||||
// open the file
|
||||
std::fstream fsPicture(filePath.toStdString().c_str(), std::ios::in | std::ios::binary);
|
||||
|
||||
if (!fsPicture.is_open())
|
||||
{
|
||||
img = QImage(1, 1, QImage::Format_RGB32);
|
||||
img.fill(Qt::red);
|
||||
success = false;
|
||||
return img;
|
||||
}
|
||||
|
||||
// read in the header
|
||||
std::uint8_t ui8x18Header[19] = { 0 };
|
||||
fsPicture.read(reinterpret_cast<char*>(&ui8x18Header), sizeof(ui8x18Header) - 1);
|
||||
|
||||
//get variables
|
||||
std::uint32_t ui32BpP;
|
||||
std::uint32_t ui32Width;
|
||||
std::uint32_t ui32Height;
|
||||
std::uint32_t ui32IDLength;
|
||||
std::uint32_t ui32PicType;
|
||||
std::uint32_t ui32PaletteLength;
|
||||
std::uint32_t ui32Size;
|
||||
|
||||
// extract all information from header
|
||||
ui32IDLength = ui8x18Header[0];
|
||||
ui32PicType = ui8x18Header[2];
|
||||
ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5];
|
||||
ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12];
|
||||
ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14];
|
||||
ui32BpP = ui8x18Header[16];
|
||||
|
||||
// calculate some more information
|
||||
ui32Size = ui32Width * ui32Height * ui32BpP / 8;
|
||||
|
||||
// jump to the data block
|
||||
fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur);
|
||||
|
||||
img = QImage(ui32Width, ui32Height, ui32BpP == 32? QImage::Format_RGBA8888 : QImage::Format_RGB888);
|
||||
|
||||
// uncompressed
|
||||
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
|
||||
{
|
||||
std::vector<std::uint8_t> vui8Pixels;
|
||||
vui8Pixels.resize(ui32Size);
|
||||
fsPicture.read(reinterpret_cast<char*>(vui8Pixels.data()), ui32Size);
|
||||
|
||||
for (unsigned int y = 0; y < ui32Height; y++)
|
||||
{
|
||||
for (unsigned int x = 0; x < ui32Width; x++)
|
||||
{
|
||||
int valr = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 2);
|
||||
int valg = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 1);
|
||||
int valb = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8);
|
||||
|
||||
QColor value(valr, valg, valb);
|
||||
img.setPixelColor(x, ui32Width - 1 - y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
// else if compressed 24 or 32 bit
|
||||
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
|
||||
{
|
||||
std::uint8_t tempChunkHeader;
|
||||
std::uint8_t tempData[5];
|
||||
unsigned int tmp_pixelIndex = 0;
|
||||
|
||||
do {
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempChunkHeader), sizeof(tempChunkHeader));
|
||||
|
||||
if (tempChunkHeader >> 7) // repeat count
|
||||
{
|
||||
// just use the first 7 bits
|
||||
tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1);
|
||||
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP / 8);
|
||||
|
||||
for (int i = 0; i <= tempChunkHeader; i++)
|
||||
{
|
||||
QColor color;
|
||||
|
||||
if (ui32BpP == 32)
|
||||
color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], tempData[3]));
|
||||
else
|
||||
color.setRgb(qRgb(tempData[2], tempData[1], tempData[0]));
|
||||
|
||||
img.setPixelColor(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color);
|
||||
tmp_pixelIndex++;
|
||||
}
|
||||
}
|
||||
else // data count
|
||||
{
|
||||
// just use the first 7 bits
|
||||
tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1);
|
||||
|
||||
for (int i = 0; i <= tempChunkHeader; i++)
|
||||
{
|
||||
fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP / 8);
|
||||
|
||||
QColor color;
|
||||
|
||||
if (ui32BpP == 32)
|
||||
color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], tempData[3]));
|
||||
else
|
||||
color.setRgb(qRgb(tempData[2], tempData[1], tempData[0]));
|
||||
|
||||
img.setPixelColor(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color);
|
||||
tmp_pixelIndex++;
|
||||
}
|
||||
}
|
||||
} while (tmp_pixelIndex < (ui32Width * ui32Height));
|
||||
}
|
||||
// not useable format
|
||||
else
|
||||
{
|
||||
fsPicture.close();
|
||||
img = QImage(1, 1, QImage::Format_RGB32);
|
||||
img.fill(Qt::red);
|
||||
success = false;
|
||||
return img;
|
||||
}
|
||||
|
||||
fsPicture.close();
|
||||
}
|
||||
success = true;
|
||||
return img;
|
||||
}
|
12
QtMeshViewer/Resources/Resources.qrc
Normal file
@@ -0,0 +1,12 @@
|
||||
<RCC>
|
||||
<qresource prefix="/shaders">
|
||||
<file>fshader.glsl</file>
|
||||
<file>vshader.glsl</file>
|
||||
</qresource>
|
||||
<qresource prefix="/images">
|
||||
<file>icon.ico</file>
|
||||
</qresource>
|
||||
<qresource prefix="/files">
|
||||
<file>about.txt</file>
|
||||
</qresource>
|
||||
</RCC>
|
21
QtMeshViewer/Resources/about.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
This is a viewer for .msh files made by Anakin.
|
||||
questions, bug reports, requests: http://www.gametoast.com/viewtopic.php?f=29&t=32624
|
||||
source code: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/QtMeshViewer
|
||||
|
||||
===============================================================
|
||||
Controll:
|
||||
left mouse - rotate
|
||||
right mouse - move
|
||||
scroll - zoom
|
||||
space - reset view
|
||||
esc - close
|
||||
|
||||
using the X, Y, Z Button you can activate/deactivate the rotating directions
|
||||
|
||||
tipp: do round movement to rotate the object in z-direction, when all directions are acitvated
|
||||
|
||||
===============================================================
|
||||
Credits:
|
||||
ANDEWEGET
|
||||
opengl.org
|
||||
forum.qt.io
|
15
QtMeshViewer/Resources/fshader.glsl
Normal file
@@ -0,0 +1,15 @@
|
||||
#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);
|
||||
}
|
BIN
QtMeshViewer/Resources/icon.ico
Normal file
After Width: | Height: | Size: 264 KiB |
1
QtMeshViewer/Resources/prgmExeIcon.rc
Normal file
@@ -0,0 +1 @@
|
||||
IDI_ICON1 ICON DISCARDABLE "icon.ico"
|
25
QtMeshViewer/Resources/vshader.glsl
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifdef GL_ES
|
||||
// Set default precision to medium
|
||||
precision mediump int;
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform mat4 vp_matrix;
|
||||
uniform mat4 norm_matrix;
|
||||
uniform mat4 m_matrix;
|
||||
|
||||
attribute vec4 a_position;
|
||||
attribute vec2 a_texcoord;
|
||||
attribute vec3 a_normal;
|
||||
|
||||
varying vec2 v_texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate vertex position in screen space
|
||||
gl_Position = vp_matrix * norm_matrix * m_matrix * a_position;
|
||||
|
||||
// Pass texture coordinate to fragment shader
|
||||
// Value will be automatically interpolated to fragments inside polygon faces
|
||||
v_texcoord = a_texcoord;
|
||||
}
|
224
QtMeshViewer/Source/GeometryEngine.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
#include "..\Header\GeometryEngine.h"
|
||||
#include "..\Header\MshFile.h"
|
||||
#include "..\Header\OglViewerWidget.h"
|
||||
#include "..\Header\MainWindow.h"
|
||||
#include "..\Header\tga.h"
|
||||
#include <cmath>
|
||||
#include <QRegExp>
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public constructor/destructor
|
||||
|
||||
GeometryEngine::GeometryEngine(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_indexBuf(QOpenGLBuffer::IndexBuffer)
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
}
|
||||
|
||||
GeometryEngine::~GeometryEngine()
|
||||
{
|
||||
clearData();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void GeometryEngine::loadTexture(QString filePath, QString fileName)
|
||||
{
|
||||
bool loadSuccess(false);
|
||||
QImage img;
|
||||
|
||||
if (fileName.isEmpty())
|
||||
{
|
||||
loadSuccess = true;
|
||||
img = QImage(1, 1, QImage::Format_RGB32);
|
||||
img.fill(Qt::red);
|
||||
}
|
||||
else
|
||||
img = loadTga(filePath + "/" + fileName, loadSuccess);
|
||||
|
||||
if (!loadSuccess)
|
||||
emit sendMessage("WARNING: texture not found or corrupted: " + QString(fileName), 1);
|
||||
|
||||
// Load image to OglTexture
|
||||
QOpenGLTexture* new_texture = new QOpenGLTexture(img.mirrored());
|
||||
|
||||
// Set nearest filtering mode for texture minification
|
||||
new_texture->setMinificationFilter(QOpenGLTexture::Nearest);
|
||||
|
||||
// Set bilinear filtering mode for texture magnification
|
||||
new_texture->setMagnificationFilter(QOpenGLTexture::Linear);
|
||||
|
||||
// Wrap texture coordinates by repeating
|
||||
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
|
||||
new_texture->setWrapMode(QOpenGLTexture::Repeat);
|
||||
|
||||
m_textures.push_back(new_texture);
|
||||
}
|
||||
|
||||
void GeometryEngine::clearData()
|
||||
{
|
||||
if (m_arrayBuf.isCreated())
|
||||
m_arrayBuf.destroy();
|
||||
if (m_indexBuf.isCreated())
|
||||
m_indexBuf.destroy();
|
||||
|
||||
for (auto it : m_textures)
|
||||
delete it;
|
||||
m_textures.clear();
|
||||
m_drawList.clear();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public slots
|
||||
|
||||
void GeometryEngine::loadFile(QString filePath)
|
||||
{
|
||||
// cleanup old stuff and recreate buffers
|
||||
clearData();
|
||||
m_arrayBuf.create();
|
||||
m_indexBuf.create();
|
||||
|
||||
//reset view
|
||||
emit requestResetView();
|
||||
emit sendMessage("loading file..", 0);
|
||||
|
||||
try
|
||||
{
|
||||
QVector<Model*>* models;
|
||||
QStringList* textureNames;
|
||||
QVector<VertexData> vertexData;
|
||||
QVector<GLuint> indexData;
|
||||
|
||||
// open file and get the information
|
||||
MshFile file(filePath, this);
|
||||
|
||||
models = file.getModels();
|
||||
textureNames = file.getTextureNames();
|
||||
m_boundings = file.getBoundingBox();
|
||||
|
||||
// collect data
|
||||
unsigned int indexOffset(0);
|
||||
unsigned int vertexOffset(0);
|
||||
for (auto& modelIterator : *models)
|
||||
{
|
||||
for (auto& segmentIterator : modelIterator->segmList)
|
||||
{
|
||||
// get draw information
|
||||
DrawInformation new_info;
|
||||
new_info.offset = indexOffset;
|
||||
new_info.size = segmentIterator->indices.size();
|
||||
new_info.textureIndex = segmentIterator->textureIndex;
|
||||
new_info.modelMatrix = modelIterator->m4x4Translation;
|
||||
new_info.modelMatrix.rotate(modelIterator->quadRotation);
|
||||
|
||||
// add offset to indices, no need to do it for the first one (maybe it's very big)
|
||||
if (vertexOffset != 0)
|
||||
for (auto& it : segmentIterator->indices)
|
||||
it += vertexOffset;
|
||||
|
||||
// save data
|
||||
vertexData += segmentIterator->vertices;
|
||||
indexData += segmentIterator->indices;
|
||||
m_drawList.push_back(new_info);
|
||||
|
||||
// update offset
|
||||
indexOffset += new_info.size;
|
||||
vertexOffset += segmentIterator->vertices.size();
|
||||
}
|
||||
}
|
||||
|
||||
// Transfer vertex data to VBO 0
|
||||
m_arrayBuf.bind();
|
||||
m_arrayBuf.allocate(vertexData.data(), vertexData.size() * sizeof(VertexData));
|
||||
|
||||
// Transfer index data to VBO 1
|
||||
m_indexBuf.bind();
|
||||
m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint));
|
||||
|
||||
emit sendMessage("loading textures..", 0);
|
||||
|
||||
// load the textures
|
||||
int split = filePath.lastIndexOf(QRegExp("/|\\\\"));
|
||||
for (auto& it : *textureNames)
|
||||
loadTexture(filePath.left(split), it);
|
||||
|
||||
loadTexture("", "");
|
||||
|
||||
emit requestUpdate();
|
||||
emit sendMessage("done..", 0);
|
||||
emit sendFileInfo(filePath.right(filePath.size() - split - 1), QStringList(*textureNames), vertexData.size(), indexData.size() / 3);
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
clearData();
|
||||
emit sendMessage(QString(e.what()), 2);
|
||||
}
|
||||
}
|
||||
|
||||
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
||||
{
|
||||
if (!m_arrayBuf.isCreated() || !m_indexBuf.isCreated())
|
||||
return;
|
||||
|
||||
// Setup
|
||||
// Tell OpenGL which VBOs to use
|
||||
m_arrayBuf.bind();
|
||||
m_indexBuf.bind();
|
||||
|
||||
// Allways normalize by this
|
||||
QMatrix4x4 normMatrix;
|
||||
float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]);
|
||||
normMatrix.scale(1 / maxExtent);
|
||||
normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]);
|
||||
program->setUniformValue("norm_matrix", normMatrix);
|
||||
|
||||
// Allways use texture unit 0
|
||||
program->setUniformValue("texture", 0);
|
||||
|
||||
// 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, 3, sizeof(VertexData));
|
||||
|
||||
// Offset for texture coordinate
|
||||
offset += sizeof(QVector3D);
|
||||
|
||||
// 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));
|
||||
|
||||
//Offset for normal
|
||||
offset += sizeof(QVector2D);
|
||||
|
||||
// Tell OpenGL programmable pipeline how to locate vertex normal data
|
||||
int normLocation = program->attributeLocation("a_normal");
|
||||
program->enableAttributeArray(normLocation);
|
||||
program->setAttributeBuffer(normLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
||||
|
||||
// Paint
|
||||
|
||||
for (auto& it : m_drawList)
|
||||
{
|
||||
// bind the correct texture
|
||||
if (it.textureIndex < m_textures.size())
|
||||
m_textures.at(it.textureIndex)->bind();
|
||||
else
|
||||
m_textures.last()->bind();
|
||||
|
||||
// Set model matrix
|
||||
program->setUniformValue("m_matrix", it.modelMatrix);
|
||||
|
||||
// Draw cube geometry using indices from VBO 1
|
||||
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
|
||||
}
|
||||
}
|
||||
|
181
QtMeshViewer/Source/MainWindow.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
#include "..\Header\MainWindow.h"
|
||||
#include "..\Header\OglViewerWidget.h"
|
||||
#include <QSurfaceFormat>
|
||||
#include <QMessageBox>
|
||||
#include <QFileDialog>
|
||||
#include <QPalette>
|
||||
#include <QAction>
|
||||
#include <QSignalMapper>
|
||||
#include <QFile>
|
||||
#include <QSizePolicy>
|
||||
#include "..\Header\FileInterface.h"
|
||||
|
||||
#define WINDOW_NAME "Mesh Viewer"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindowClass)
|
||||
, m_curSeverity(0)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
setWindowTitle(WINDOW_NAME);
|
||||
setWindowIcon(QIcon(":/images/icon.ico"));
|
||||
|
||||
QSurfaceFormat format;
|
||||
format.setDepthBufferSize(24);
|
||||
QSurfaceFormat::setDefaultFormat(format);
|
||||
|
||||
setupWidgets();
|
||||
|
||||
ui->statusBar->showMessage("MeshViewer by Anakin", 0);
|
||||
|
||||
m_fileInfo += "Filename: -\nMaterials: -\nVertices: -\nTriangle: -\n<detail>No file is open";
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::openFile()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Mesh (*.msh)");
|
||||
if(!fileName.isEmpty())
|
||||
emit loadFile(fileName);
|
||||
}
|
||||
|
||||
void MainWindow::setupWidgets()
|
||||
{
|
||||
OglViewerWidget* viewer = new OglViewerWidget(this);
|
||||
setCentralWidget(viewer);
|
||||
|
||||
QAction *openFile = new QAction("Open file", this);
|
||||
connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
|
||||
ui->mainToolBar->addAction(openFile);
|
||||
|
||||
QSignalMapper* signalMapper = new QSignalMapper(this);
|
||||
|
||||
QAction *x = new QAction("X", this);
|
||||
x->setCheckable(true);
|
||||
x->setChecked(true);
|
||||
ui->mainToolBar->addAction(x);
|
||||
|
||||
QAction *y = new QAction("Y", this);
|
||||
y->setCheckable(true);
|
||||
y->setChecked(true);
|
||||
ui->mainToolBar->addAction(y);
|
||||
|
||||
QAction *z = new QAction("Z", this);
|
||||
z->setCheckable(true);
|
||||
z->setChecked(true);
|
||||
ui->mainToolBar->addAction(z);
|
||||
|
||||
connect(x, SIGNAL(triggered()), signalMapper, SLOT(map()));
|
||||
connect(y, SIGNAL(triggered()), signalMapper, SLOT(map()));
|
||||
connect(z, SIGNAL(triggered()), signalMapper, SLOT(map()));
|
||||
|
||||
signalMapper->setMapping(x, 1);
|
||||
signalMapper->setMapping(y, 2);
|
||||
signalMapper->setMapping(z, 3);
|
||||
|
||||
connect(signalMapper, SIGNAL(mapped(int)), viewer, SLOT(changeDirection(int)));
|
||||
|
||||
|
||||
QAction *fileInfo = new QAction("File info", this);
|
||||
connect(fileInfo, &QAction::triggered, this, &MainWindow::aboutFile);
|
||||
ui->mainToolBar->addAction(fileInfo);
|
||||
|
||||
QAction *help = new QAction("Help", this);
|
||||
connect(help, &QAction::triggered, this, &MainWindow::aboutTool);
|
||||
ui->mainToolBar->addAction(help);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void MainWindow::aboutFile()
|
||||
{
|
||||
QMessageBox* dialog = new QMessageBox(QMessageBox::NoIcon,
|
||||
WINDOW_NAME,
|
||||
QString(m_fileInfo.left(m_fileInfo.indexOf("<detail>"))),
|
||||
QMessageBox::StandardButton::Close,
|
||||
this,
|
||||
Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
|
||||
|
||||
dialog->setStyleSheet("QLabel{min-width: 200px;}");
|
||||
dialog->setDetailedText(QString(m_fileInfo.right(m_fileInfo.size() - m_fileInfo.indexOf("<detail>") - 8)));
|
||||
dialog->exec();
|
||||
delete dialog;
|
||||
}
|
||||
|
||||
void MainWindow::aboutTool()
|
||||
{
|
||||
QFile file(":/files/about.txt");
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QMessageBox* dialog = new QMessageBox(
|
||||
QMessageBox::Question,
|
||||
WINDOW_NAME,
|
||||
QString(file.readAll()),
|
||||
QMessageBox::StandardButton::Close,
|
||||
this,
|
||||
Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
|
||||
|
||||
file.close();
|
||||
|
||||
dialog->exec();
|
||||
|
||||
delete dialog;
|
||||
}
|
||||
|
||||
void MainWindow::setFileInfo(QString name, QStringList textures, int vertices, int triangle)
|
||||
{
|
||||
m_fileInfo = QByteArray("Filename: ");
|
||||
m_fileInfo += name;
|
||||
m_fileInfo += "\nMaterials: ";
|
||||
m_fileInfo += QByteArray::number(textures.size());
|
||||
m_fileInfo += "\nVertices: ";
|
||||
m_fileInfo += QByteArray::number(vertices);
|
||||
m_fileInfo += "\nTriangle: ";
|
||||
m_fileInfo += QByteArray::number(triangle);
|
||||
m_fileInfo += "<detail>";
|
||||
|
||||
int count(0);
|
||||
for (auto& it : textures)
|
||||
{
|
||||
m_fileInfo += "Material ";
|
||||
m_fileInfo += QByteArray::number(count++);
|
||||
m_fileInfo += " - ";
|
||||
m_fileInfo += it;
|
||||
m_fileInfo += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::printMessage(QString message, int severity)
|
||||
{
|
||||
if (!ui->statusBar->currentMessage().isEmpty() && severity < m_curSeverity)
|
||||
return;
|
||||
|
||||
m_curSeverity = severity;
|
||||
int time(0);
|
||||
QPalette palette;
|
||||
|
||||
switch (severity)
|
||||
{
|
||||
case 1:
|
||||
time = 3000;
|
||||
palette.setColor(QPalette::WindowText, Qt::darkYellow);
|
||||
break;
|
||||
case 2:
|
||||
time = 3000;
|
||||
palette.setColor(QPalette::WindowText, Qt::red);
|
||||
break;
|
||||
case 0:
|
||||
default:
|
||||
time = 2000;
|
||||
palette.setColor(QPalette::WindowText, Qt::black);
|
||||
break;
|
||||
}
|
||||
|
||||
ui->statusBar->setPalette(palette);
|
||||
ui->statusBar->showMessage(message, time);
|
||||
}
|
637
QtMeshViewer/Source/MshFile.cpp
Normal file
@@ -0,0 +1,637 @@
|
||||
#include "..\Header\MshFile.h"
|
||||
|
||||
|
||||
// helper function to save data from file to any variable type
|
||||
#define F2V(variableName) reinterpret_cast<char*>(&variableName)
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public constructor/destructor
|
||||
|
||||
MshFile::MshFile(QString path, QObject * parent)
|
||||
: FileInterface(path, parent)
|
||||
{
|
||||
import();
|
||||
}
|
||||
|
||||
MshFile::~MshFile()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void MshFile::import()
|
||||
{
|
||||
// go to file size information
|
||||
m_file.seekg(4);
|
||||
|
||||
std::uint32_t tmp_fileSize;
|
||||
std::list<ChunkHeader*> tmp_mainChunks;
|
||||
|
||||
// get all chunks under HEDR
|
||||
m_file.read(F2V(tmp_fileSize), sizeof(tmp_fileSize));
|
||||
loadChunks(tmp_mainChunks, m_file.tellg(), tmp_fileSize);
|
||||
|
||||
// evaulate HEDR subchunks (= find MSH2)
|
||||
for (ChunkHeader* it : tmp_mainChunks)
|
||||
{
|
||||
if (!strcmp("MSH2", it->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tmp_msh2Chunks;
|
||||
loadChunks(tmp_msh2Chunks, it->position, it->size);
|
||||
|
||||
// evaluate MSH2 subchunks
|
||||
analyseMsh2Chunks(tmp_msh2Chunks);
|
||||
|
||||
// clean up
|
||||
while (!tmp_msh2Chunks.empty())
|
||||
{
|
||||
ChunkHeader* curs = tmp_msh2Chunks.front();
|
||||
tmp_msh2Chunks.pop_front();
|
||||
delete curs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
while (!tmp_mainChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_mainChunks.front();
|
||||
tmp_mainChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t length)
|
||||
{
|
||||
// jump to first chunk
|
||||
m_file.seekg(start);
|
||||
|
||||
do
|
||||
{
|
||||
ChunkHeader* tmp_header = new ChunkHeader();
|
||||
|
||||
// get information
|
||||
m_file.read(F2V(tmp_header->name[0]), sizeof(tmp_header->name) - 1);
|
||||
m_file.read(F2V(tmp_header->size), sizeof(tmp_header->size));
|
||||
tmp_header->position = m_file.tellg();
|
||||
|
||||
// store information
|
||||
destination.push_back(tmp_header);
|
||||
|
||||
// jump to next header
|
||||
m_file.seekg(tmp_header->size, std::ios_base::cur);
|
||||
|
||||
// out of file. Maybe a size information is corrupted
|
||||
if (!m_file.good())
|
||||
{
|
||||
emit sendMessage("WARNING: corrupted file. Trying to continue..", 1);
|
||||
m_file.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
} while (m_file.tellg() - start != length);
|
||||
}
|
||||
|
||||
void MshFile::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// scene information
|
||||
if (!strcmp("SINF", it->name))
|
||||
{
|
||||
// get SINF subchunks
|
||||
std::list<ChunkHeader*> tmp_sinfChunks;
|
||||
loadChunks(tmp_sinfChunks, it->position, it->size);
|
||||
|
||||
// evaluate SINF subchunks
|
||||
for (auto& it : tmp_sinfChunks)
|
||||
{
|
||||
if (!strcmp("BBOX", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
|
||||
// read in the quaternion
|
||||
float tmp_quat[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_file.read(F2V(tmp_quat[i]), sizeof(float));
|
||||
|
||||
m_sceneBbox.rotation.setX(tmp_quat[0]);
|
||||
m_sceneBbox.rotation.setY(tmp_quat[1]);
|
||||
m_sceneBbox.rotation.setZ(tmp_quat[2]);
|
||||
m_sceneBbox.rotation.setScalar(tmp_quat[3]);
|
||||
|
||||
//read in the center
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_file.read(F2V(m_sceneBbox.center[i]), sizeof(float));
|
||||
|
||||
//read in the extents
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_file.read(F2V(m_sceneBbox.extents[i]), sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
// clean up SINF subchunks
|
||||
for (ChunkHeader* it : tmp_sinfChunks)
|
||||
delete it;
|
||||
}
|
||||
|
||||
// material list
|
||||
else if (!strcmp("MATL", it->name))
|
||||
{
|
||||
// "useless" information how many MATD follow, jump over it
|
||||
m_file.seekg(it->position);
|
||||
m_file.seekg(sizeof(std::uint32_t), std::ios_base::cur);
|
||||
|
||||
// get all MATL subchunk
|
||||
std::list<ChunkHeader*> tmp_matlChunks;
|
||||
loadChunks(tmp_matlChunks, m_file.tellg(), it->size - 4);
|
||||
|
||||
// evaluate MATL subchunks
|
||||
for (auto& it : tmp_matlChunks)
|
||||
{
|
||||
// This shouldn't be anything else than MATD
|
||||
if (!strcmp("MATD", it->name))
|
||||
{
|
||||
// get all subchunks from MATD
|
||||
std::list<ChunkHeader*> tmp_matdChunks;
|
||||
loadChunks(tmp_matdChunks, it->position, it->size);
|
||||
|
||||
m_textureNames->push_back("");
|
||||
|
||||
// analyse MATD subchunks
|
||||
analyseMatdChunks(tmp_matdChunks);
|
||||
|
||||
// clean up MATD subchunks
|
||||
while (!tmp_matdChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_matdChunks.front();
|
||||
tmp_matdChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clean up MATL subchunks
|
||||
while (!tmp_matlChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_matlChunks.front();
|
||||
tmp_matlChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
|
||||
// model
|
||||
else if (!strcmp("MODL", it->name))
|
||||
{
|
||||
Model* new_model = new Model;
|
||||
m_currentType = ModelTyp::null;
|
||||
m_currentRenderFlag = -1;
|
||||
|
||||
// get all MODL subchunks
|
||||
std::list<ChunkHeader*> tmp_chunks;
|
||||
loadChunks(tmp_chunks, it->position, it->size);
|
||||
|
||||
// evaluate MODL subchunks
|
||||
analyseModlChunks(new_model, tmp_chunks);
|
||||
|
||||
//clean up MODL subchunks
|
||||
while (!tmp_chunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_chunks.front();
|
||||
tmp_chunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
|
||||
// save Model data
|
||||
m_models->push_back(new_model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
if (!strcmp("TX0D", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_file.read(buffer, it->size);
|
||||
m_textureNames->back() = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseModlChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// model type
|
||||
if (!strcmp("MTYP", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
std::uint32_t tmp_type;
|
||||
m_file.read(F2V(tmp_type), sizeof(tmp_type));
|
||||
m_currentType = (ModelTyp)tmp_type;
|
||||
}
|
||||
|
||||
// parent name
|
||||
else if (!strcmp("PRNT", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_file.read(buffer, it->size);
|
||||
dataDestination->parent = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// model name
|
||||
else if (!strcmp("NAME", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_file.read(buffer, it->size);
|
||||
dataDestination->name = buffer;
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// render flags
|
||||
else if (!strcmp("FLGS", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
m_file.read(F2V(m_currentRenderFlag), sizeof(m_currentRenderFlag));
|
||||
}
|
||||
|
||||
// translation
|
||||
else if (!strcmp("TRAN", it->name))
|
||||
{
|
||||
float tmp_scale[3];
|
||||
float tmp_rotation[4];
|
||||
float tmp_trans[3];
|
||||
|
||||
m_file.seekg(it->position);
|
||||
|
||||
// read in the data
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_file.read(F2V(tmp_scale[i]), sizeof(float));
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_file.read(F2V(tmp_rotation[i]), sizeof(float));
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
m_file.read(F2V(tmp_trans[i]), sizeof(float));
|
||||
|
||||
// modify the matrix and quaternion
|
||||
dataDestination->m4x4Translation.scale(tmp_scale[0], tmp_scale[1], tmp_scale[2]);
|
||||
dataDestination->m4x4Translation.translate(tmp_trans[0], tmp_trans[1], tmp_trans[2]);
|
||||
dataDestination->quadRotation.setVector(QVector3D(tmp_rotation[0], tmp_rotation[1], tmp_rotation[2]));
|
||||
dataDestination->quadRotation.setScalar(tmp_rotation[3]);
|
||||
|
||||
dataDestination->m4x4Translation = getParentMatrix(dataDestination->parent) * dataDestination->m4x4Translation;
|
||||
dataDestination->quadRotation = getParentRotation(dataDestination->parent) * dataDestination->quadRotation;
|
||||
}
|
||||
|
||||
// geometry data
|
||||
else if (!strcmp("GEOM", it->name))
|
||||
{
|
||||
// get all GEOM subchunks
|
||||
std::list<ChunkHeader*> tmp_geomChunks;
|
||||
loadChunks(tmp_geomChunks, it->position, it->size);
|
||||
|
||||
// evaluate GEOM subchunks
|
||||
analyseGeomChunks(dataDestination, tmp_geomChunks);
|
||||
|
||||
// clean up GEOM subchunks
|
||||
while (!tmp_geomChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_geomChunks.front();
|
||||
tmp_geomChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseGeomChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// segment
|
||||
if (!strcmp("SEGM", it->name))
|
||||
{
|
||||
// get all SEGM subchunks
|
||||
std::list<ChunkHeader*> tmp_segmChunks;
|
||||
loadChunks(tmp_segmChunks, it->position, it->size);
|
||||
|
||||
// evaluate SEGM subchunks
|
||||
analyseSegmChunks(dataDestination, tmp_segmChunks);
|
||||
|
||||
// clean up SEGM subchunk
|
||||
while (!tmp_segmChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_segmChunks.front();
|
||||
tmp_segmChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
|
||||
// cloth
|
||||
else if (!strcmp("CLTH", it->name))
|
||||
{
|
||||
// get all CLTH subchunks
|
||||
std::list<ChunkHeader*> tmp_clthChunks;
|
||||
loadChunks(tmp_clthChunks, it->position, it->size);
|
||||
|
||||
// evaluate CLTH subchunks
|
||||
analyseClthChunks(dataDestination, tmp_clthChunks);
|
||||
|
||||
// clean up CLTH subchunks
|
||||
while (!tmp_clthChunks.empty())
|
||||
{
|
||||
ChunkHeader* cur = tmp_clthChunks.front();
|
||||
tmp_clthChunks.pop_front();
|
||||
delete cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::analyseSegmChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
Segment* new_segment = new Segment;
|
||||
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// material index
|
||||
if (!strcmp("MATI", it->name))
|
||||
{
|
||||
m_file.seekg(it->position);
|
||||
m_file.read(F2V(new_segment->textureIndex), sizeof(new_segment->textureIndex));
|
||||
}
|
||||
|
||||
// position list (vertex)
|
||||
else if (!strcmp("POSL", it->name))
|
||||
{
|
||||
readVertex(new_segment, it->position);
|
||||
}
|
||||
|
||||
// normals
|
||||
else if (!strcmp("NRML", it->name))
|
||||
{
|
||||
std::uint32_t tmp_size;
|
||||
m_file.seekg(it->position);
|
||||
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
if (tmp_size < new_segment->vertices.size())
|
||||
{
|
||||
emit sendMessage("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1);
|
||||
|
||||
for (unsigned int i = new_segment->vertices.size(); i != tmp_size; i--)
|
||||
for (unsigned int j = 0; j < 3; j++)
|
||||
new_segment->vertices[i - 1].normal[j] = 0;
|
||||
}
|
||||
else if (tmp_size > new_segment->vertices.size())
|
||||
{
|
||||
emit sendMessage("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1);
|
||||
tmp_size = new_segment->vertices.size();
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size; i++)
|
||||
for (unsigned int j = 0; j < 3; j++)
|
||||
m_file.read(F2V(new_segment->vertices[i].normal[j]), sizeof(float));
|
||||
|
||||
}
|
||||
|
||||
// uv
|
||||
else if (!strcmp("UV0L", it->name))
|
||||
{
|
||||
readUV(new_segment, it->position);
|
||||
}
|
||||
|
||||
// polygons (indices into vertex/uv list)
|
||||
else if (!strcmp("STRP", it->name))
|
||||
{
|
||||
// don't get null, bone, shadowMesh and hidden mesh indices
|
||||
if (m_currentType == null || m_currentType == bone || m_currentType == shadowMesh || m_currentRenderFlag == 1)
|
||||
continue;
|
||||
|
||||
// jump to the data section and read the size;
|
||||
std::uint32_t tmp_size;
|
||||
m_file.seekg(it->position);
|
||||
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
int highBitCount(0);
|
||||
QVector<GLuint> tmp_buffer;
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size; i++)
|
||||
{
|
||||
// ReadData
|
||||
std::uint16_t tmp_value;
|
||||
m_file.read(F2V(tmp_value), sizeof(tmp_value));
|
||||
|
||||
// Check if highbit is set
|
||||
if (tmp_value >> 15)
|
||||
{
|
||||
highBitCount++;
|
||||
// remove the high bit, to get the actually value
|
||||
tmp_value = (std::uint16_t(tmp_value << 1) >> 1);
|
||||
}
|
||||
|
||||
// save data
|
||||
tmp_buffer.push_back((GLuint)tmp_value);
|
||||
|
||||
// if the last 2 highBits are set, it was a new poly
|
||||
if (highBitCount == 2)
|
||||
{
|
||||
// reset highBitCount
|
||||
highBitCount = 0;
|
||||
|
||||
if (tmp_buffer.size() == 5)
|
||||
{
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
new_segment->indices.push_back(tmp_buffer.takeFirst());
|
||||
}
|
||||
else if (tmp_buffer.size() > 5)
|
||||
{
|
||||
unsigned int tmp_multiPolySize = tmp_buffer.size() - 2;
|
||||
// for every triangle of the multi polygon..
|
||||
for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
|
||||
// ..calculate the edge indices
|
||||
for (int triEdge = 0; triEdge < 3; triEdge++)
|
||||
new_segment->indices.push_back(tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))]);
|
||||
|
||||
tmp_buffer.remove(0, tmp_multiPolySize);
|
||||
}
|
||||
|
||||
} // if 2 high bits are set
|
||||
|
||||
} // for all values
|
||||
|
||||
// save the last polygon (no 2 high bit followed)
|
||||
if (tmp_buffer.size() == 3)
|
||||
{
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
new_segment->indices.push_back(tmp_buffer.takeFirst());
|
||||
}
|
||||
else if (tmp_buffer.size() > 3)
|
||||
{
|
||||
unsigned int tmp_multiPolySize = tmp_buffer.size();
|
||||
// for every triangle of the multi polygon..
|
||||
for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
|
||||
// ..calculate the edge indices
|
||||
for (int triEdge = 0; triEdge < 3; triEdge++)
|
||||
new_segment->indices.push_back(tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dataDestination->segmList.push_back(new_segment);
|
||||
}
|
||||
|
||||
void MshFile::analyseClthChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
|
||||
{
|
||||
Segment* new_segment = new Segment;
|
||||
|
||||
for (auto& it : chunkList)
|
||||
{
|
||||
// texture name
|
||||
if (!strcmp("CTEX", it->name))
|
||||
{
|
||||
// read the texture name
|
||||
m_file.seekg(it->position);
|
||||
char* buffer = new char[it->size + 1];
|
||||
*buffer = { 0 };
|
||||
m_file.read(buffer, it->size);
|
||||
|
||||
// search if it is already known
|
||||
bool tmp_found(false);
|
||||
|
||||
int index = m_textureNames->indexOf(QString::fromStdString(buffer));
|
||||
if (index != -1)
|
||||
new_segment->textureIndex = index;
|
||||
else
|
||||
{
|
||||
m_textureNames->push_back(QString::fromStdString(buffer));
|
||||
new_segment->textureIndex = m_textureNames->size() - 1;
|
||||
}
|
||||
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
// position list (vertex)
|
||||
else if (!strcmp("CPOS", it->name))
|
||||
{
|
||||
readVertex(new_segment, it->position);
|
||||
}
|
||||
|
||||
// uv
|
||||
else if (!strcmp("CUV0", it->name))
|
||||
{
|
||||
readUV(new_segment, it->position);
|
||||
}
|
||||
|
||||
// triangles (indices into vertex/uv list)
|
||||
else if (!strcmp("CMSH", it->name))
|
||||
{
|
||||
// jump to the data section and read the size;
|
||||
std::uint32_t tmp_size;
|
||||
m_file.seekg(it->position);
|
||||
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
// for every triangle..
|
||||
for (unsigned int i = 0; i < tmp_size * 3; i++)
|
||||
{
|
||||
std::uint32_t tmp_value;
|
||||
m_file.read(F2V(tmp_value), sizeof(std::uint32_t));
|
||||
|
||||
new_segment->indices.push_back((GLuint)tmp_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dataDestination->segmList.push_back(new_segment);
|
||||
}
|
||||
|
||||
void MshFile::readVertex(Segment * dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tmp_size;
|
||||
m_file.seekg(position);
|
||||
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size; i++)
|
||||
{
|
||||
float tmp[3];
|
||||
for (unsigned int j = 0; j < 3; j++)
|
||||
m_file.read(F2V(tmp[j]), sizeof(float));
|
||||
|
||||
VertexData new_data;
|
||||
new_data.position = QVector3D(tmp[0], tmp[1], tmp[2]);
|
||||
|
||||
dataDestination->vertices.push_back(new_data);
|
||||
}
|
||||
}
|
||||
|
||||
void MshFile::readUV(Segment * dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tmp_size;
|
||||
m_file.seekg(position);
|
||||
m_file.read(F2V(tmp_size), sizeof(tmp_size));
|
||||
|
||||
if (tmp_size < dataDestination->vertices.size())
|
||||
{
|
||||
emit sendMessage("WARNING: too less UVs " + QString::number(tmp_size) + " < " + QString::number(dataDestination->vertices.size()),1);
|
||||
|
||||
for (unsigned int i = dataDestination->vertices.size(); i != tmp_size; i--)
|
||||
for (unsigned int j = 0; j < 2; j++)
|
||||
dataDestination->vertices[i - 1].texCoord[j] = 0;
|
||||
}
|
||||
else if (tmp_size > dataDestination->vertices.size())
|
||||
{
|
||||
emit sendMessage("WARNING: too many UVs " + QString::number(tmp_size) + " > " + QString::number(dataDestination->vertices.size()), 1);
|
||||
tmp_size = dataDestination->vertices.size();
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < tmp_size; i++)
|
||||
for (unsigned int j = 0; j < 2; j++)
|
||||
m_file.read(F2V(dataDestination->vertices[i].texCoord[j]), sizeof(float));
|
||||
}
|
||||
|
||||
QMatrix4x4 MshFile::getParentMatrix(std::string parent) const
|
||||
{
|
||||
QMatrix4x4 matrix;
|
||||
|
||||
for (auto& it : *m_models)
|
||||
{
|
||||
if (!strcmp(parent.c_str(), it->name.c_str()))
|
||||
{
|
||||
matrix = getParentMatrix(it->parent) * it->m4x4Translation;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
QQuaternion MshFile::getParentRotation(std::string parent) const
|
||||
{
|
||||
QQuaternion rotation;
|
||||
|
||||
for (auto& it : *m_models)
|
||||
{
|
||||
if (!strcmp(parent.c_str(), it->name.c_str()))
|
||||
{
|
||||
rotation = getParentRotation(it->parent) * it->quadRotation;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rotation;
|
||||
}
|
291
QtMeshViewer/Source/OglViewerWidget.cpp
Normal file
@@ -0,0 +1,291 @@
|
||||
#include "..\Header\OglViewerWidget.h"
|
||||
#include "..\Header\MainWindow.h"
|
||||
#include <QMouseEvent>
|
||||
#include <QDropEvent>
|
||||
#include <QMimeData>
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
|
||||
#define DEFAULT_Z_DISTANCE -4.0
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public constructor/destructor
|
||||
|
||||
OglViewerWidget::OglViewerWidget(QWidget *parent) :
|
||||
QOpenGLWidget(parent),
|
||||
m_dataEngine(0)
|
||||
{
|
||||
setFocus();
|
||||
m_translation.setZ(DEFAULT_Z_DISTANCE);
|
||||
setAcceptDrops(true);
|
||||
|
||||
}
|
||||
|
||||
OglViewerWidget::~OglViewerWidget()
|
||||
{
|
||||
// Make sure the context is current when deleting the texture
|
||||
// and the buffers.
|
||||
makeCurrent();
|
||||
delete m_dataEngine;
|
||||
doneCurrent();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// protected functions
|
||||
|
||||
void OglViewerWidget::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
// Save mouse press position
|
||||
m_mouse.position = QVector2D(e->localPos());
|
||||
|
||||
// Which button has been pressed?
|
||||
if (e->button() == Qt::LeftButton)
|
||||
m_mouse.left = true;
|
||||
else if (e->button() == Qt::RightButton)
|
||||
m_mouse.right = true;
|
||||
}
|
||||
|
||||
void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e)
|
||||
{
|
||||
if (e->button() == Qt::LeftButton)
|
||||
m_mouse.left = false;
|
||||
else if (e->button() == Qt::RightButton)
|
||||
m_mouse.right = false;
|
||||
}
|
||||
|
||||
void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
|
||||
{
|
||||
if (m_mouse.left)
|
||||
{
|
||||
// get the difference between last press and now
|
||||
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
|
||||
|
||||
// update the new position
|
||||
m_mouse.position = QVector2D(e->localPos());
|
||||
|
||||
// calculate the rotation axis and rotate
|
||||
if (m_rotDirections.x && m_rotDirections.y && m_rotDirections.z)
|
||||
{
|
||||
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
|
||||
}
|
||||
else if (m_rotDirections.x && m_rotDirections.y && !m_rotDirections.z)
|
||||
{
|
||||
|
||||
float pitch, yaw, roll;
|
||||
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
|
||||
|
||||
pitch += diff.y() * 0.5;
|
||||
yaw += diff.x() * 0.5;
|
||||
|
||||
if (pitch > 89)
|
||||
pitch = 89;
|
||||
else if (pitch < -89)
|
||||
pitch = -89;
|
||||
|
||||
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
|
||||
|
||||
}
|
||||
else if (m_rotDirections.x && !m_rotDirections.y && !m_rotDirections.z)
|
||||
{
|
||||
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(0.0, 1.0, 0.0).normalized(), diff.x() * 0.5) * m_rotation;
|
||||
}
|
||||
else if (!m_rotDirections.x && m_rotDirections.y && !m_rotDirections.z)
|
||||
{
|
||||
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0).normalized(), diff.y() * 0.5) * m_rotation;
|
||||
}
|
||||
else if (!m_rotDirections.x && !m_rotDirections.y && m_rotDirections.z)
|
||||
{
|
||||
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(0.0, 0.0, 1.0).normalized(), diff.x() * 0.5) * m_rotation;
|
||||
}
|
||||
else if (m_rotDirections.x && !m_rotDirections.y && m_rotDirections.z)
|
||||
{
|
||||
float pitch, yaw, roll;
|
||||
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
|
||||
roll -= diff.y() * 0.5;
|
||||
yaw += diff.x() * 0.5;
|
||||
|
||||
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
|
||||
}
|
||||
else if (!m_rotDirections.x && m_rotDirections.y && m_rotDirections.z)
|
||||
{
|
||||
float pitch, yaw, roll;
|
||||
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
|
||||
pitch += diff.y() * 0.5;
|
||||
roll += diff.x() * 0.5;
|
||||
|
||||
if (pitch > 89)
|
||||
pitch = 89;
|
||||
else if (pitch < -89)
|
||||
pitch = -89;
|
||||
|
||||
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
|
||||
}
|
||||
|
||||
|
||||
// request an update
|
||||
update();
|
||||
}
|
||||
else if (m_mouse.right)
|
||||
{
|
||||
// get the difference between last press and now
|
||||
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
|
||||
|
||||
// update the new position
|
||||
m_mouse.position = QVector2D(e->localPos());
|
||||
|
||||
// calculate the translation
|
||||
m_translation += {(float)(diff.x() * 0.01), (float)(diff.y() * -0.01), 0.0};
|
||||
|
||||
// request an update
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void OglViewerWidget::wheelEvent(QWheelEvent *e)
|
||||
{
|
||||
m_translation += {0.0, 0.0, (float)e->angleDelta().y() / 240};
|
||||
update();
|
||||
}
|
||||
|
||||
void OglViewerWidget::dragEnterEvent(QDragEnterEvent *e)
|
||||
{
|
||||
if (e->mimeData()->hasUrls())
|
||||
if(e->mimeData()->urls().size() == 1)
|
||||
if(e->mimeData()->urls().first().toLocalFile().endsWith(".msh"))
|
||||
e->acceptProposedAction();
|
||||
|
||||
}
|
||||
|
||||
void OglViewerWidget::dropEvent(QDropEvent * e)
|
||||
{
|
||||
emit loadFile(e->mimeData()->urls().first().toLocalFile());
|
||||
}
|
||||
|
||||
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
|
||||
{
|
||||
if (e->key() == Qt::Key_Space)
|
||||
{
|
||||
resetView();
|
||||
}
|
||||
else if (e->key() == Qt::Key_Escape)
|
||||
{
|
||||
parentWidget()->close();
|
||||
}
|
||||
}
|
||||
|
||||
void OglViewerWidget::initializeGL()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
|
||||
glClearColor(0.5000f, 0.8000f, 1.0000f, 0.0000f);
|
||||
|
||||
initShaders();
|
||||
|
||||
// Enable depth buffer
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// Enable back face culling
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
m_dataEngine = new GeometryEngine(this);
|
||||
setConnections();
|
||||
}
|
||||
|
||||
void OglViewerWidget::resizeGL(int w, int h)
|
||||
{
|
||||
// Calculate aspect ratio
|
||||
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
||||
|
||||
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
|
||||
const qreal zNear = 0.1, zFar = 100.0, fov = 45.0;
|
||||
|
||||
// Reset projection
|
||||
m_projection.setToIdentity();
|
||||
|
||||
// Set perspective projection
|
||||
m_projection.perspective(fov, aspect, zNear, zFar);
|
||||
}
|
||||
|
||||
void OglViewerWidget::paintGL()
|
||||
{
|
||||
// Clear color and depth buffer
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Calculate model view transformation
|
||||
QMatrix4x4 view;
|
||||
view.translate(m_translation);
|
||||
view.rotate(m_rotation);
|
||||
|
||||
// Set view-projection matrix
|
||||
m_program.setUniformValue("vp_matrix", m_projection * view);
|
||||
|
||||
// Draw cube geometry
|
||||
m_dataEngine->drawGeometry(&m_program);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void OglViewerWidget::initShaders()
|
||||
{
|
||||
// Compile vertex shader
|
||||
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
|
||||
close();
|
||||
|
||||
// Compile fragment shader
|
||||
if (!m_program.addShaderFromSourceFile(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 OglViewerWidget::setConnections()
|
||||
{
|
||||
connect(m_dataEngine, &GeometryEngine::requestResetView, this, &OglViewerWidget::resetView);
|
||||
connect(parentWidget(), SIGNAL(loadFile(QString)), m_dataEngine, SLOT(loadFile(QString)));
|
||||
connect(this, SIGNAL(loadFile(QString)), m_dataEngine, SLOT(loadFile(QString)));
|
||||
connect(m_dataEngine, SIGNAL(sendMessage(QString, int)), parentWidget(), SLOT(printMessage(QString, int)));
|
||||
connect(m_dataEngine, SIGNAL(requestUpdate()), this, SLOT(update()));
|
||||
connect(m_dataEngine, SIGNAL(sendFileInfo(QString, QStringList, int, int)), parentWidget(), SLOT(setFileInfo(QString, QStringList, int, int)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// private slots
|
||||
|
||||
void OglViewerWidget::resetView()
|
||||
{
|
||||
m_rotation = QQuaternion();
|
||||
m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// public slots
|
||||
|
||||
void OglViewerWidget::changeDirection(int direction)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case 1:
|
||||
m_rotDirections.x = !m_rotDirections.x;
|
||||
break;
|
||||
case 2:
|
||||
m_rotDirections.y = !m_rotDirections.y;
|
||||
break;
|
||||
case 3:
|
||||
m_rotDirections.z = !m_rotDirections.z;
|
||||
break;
|
||||
}
|
||||
}
|
13
QtMeshViewer/main.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "Header\MainWindow.h"
|
||||
#include <QtWidgets/QApplication>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
MainWindow w;
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
11
README.md
@@ -13,18 +13,11 @@ Feel free to use my code the way you like. But remember i used some public libra
|
||||
licence, too.
|
||||
|
||||
### To Do
|
||||
- crashing sometimes - no idea why,
|
||||
- bones are not triangulated,
|
||||
- nulls are not triangulated,
|
||||
- crash when loading trooper,
|
||||
- display cloth testing
|
||||
- optional display bones, shadow, collision,...
|
||||
- integrate into a software:
|
||||
-> gui open file ( + drag and drop),
|
||||
-> list all msh under a directory,
|
||||
-> display shadows,
|
||||
-> display colisions,
|
||||
-> lights,
|
||||
-> handle render flags,
|
||||
- draw more then one model,
|
||||
- use different textures for one or multiple models,
|
||||
- take a look at destruction
|
||||
|
||||
|
BIN
Release/Msh/1.tga
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
Release/Msh/2.tga
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
Release/Msh/3.tga
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
Release/Msh/4-Poly.msh
Normal file
BIN
Release/Msh/4.tga
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
Release/Msh/5-Poly.msh
Normal file
BIN
Release/Msh/5.tga
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
Release/Msh/6-poly.msh
Normal file
BIN
Release/Msh/6.tga
Normal file
After Width: | Height: | Size: 48 KiB |