Commit d63d5276 authored by Anakin's avatar Anakin

Merge branch 'master' of git.rwth-aachen.de:carstenf/OpenGL

parents 32b0a95a b6ef34f9
<?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>
#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; };
};
\ No newline at end of file
#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;
};
#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);
};
\ No newline at end of file
#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);
};
#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;
};
#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"
<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>
#version 330
// Input
in vec2 UV;
// Output
out vec4 color;
void main()
{
color = {255,0,0};//texture(textureSampler, UV).rgb;
}
\ No newline at end of file
#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;
}
\ No newline at end of file
#version 330
in vec4 vColor;
out vec4 fColor;
void main()
{
fColor = vColor;
}
\ No newline at end of file
#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);
}
\ No newline at end of file
#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);
}
#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