Commit 53ac8c3e authored by Anakin's avatar Anakin

copied from the old Ppoject:

shaders,
object class
Modified to use better names and use Qt things
parent 5faf584d
#pragma once
#include <fstream>
#include <vector>
#include <QMatrix4x4>
enum ModelTyp {
null,
dynamicMesh,
cloth,
bone,
staticMesh,
shadowMesh = 6
};
struct BoundingBox {
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>> polyIndices; // indices into vertex array
};
struct Model {
std::string name = "";
std::string parent = "";
ModelTyp type = null;
std::int32_t renderFlags = -1;
QMatrix4x4 m4x4Translation;
std::vector<Segment*> segmList;
};
class MshFile
{
public:
MshFile(const char* path);
~MshFile();
private:
std::vector<Model*>* m_vModels;
std::fstream m_fsMesh;
std::vector<std::string> m_vTextureNames;
BoundingBox m_sceneBbox;
private:
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);
public:
std::vector<Model*>* getModels() const;
std::vector<std::string> getTextureNames() const;
BoundingBox getBoundingBox() const;
};
\ No newline at end of file
#pragma once #pragma once
#include <QOpenGLWidget> #include <QOpenGLWidget>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
#include <QOpenGLBuffer> #include <QOpenGLBuffer>
#include <QOPenGLVertexArrayObject> #include <QOPenGLVertexArrayObject>
#include <QOpenGlShaderProgram> #include <QOpenGlShaderProgram>
#include "..\Header\MshFile.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 class OpenGlViewer : public QOpenGLWidget, protected QOpenGLFunctions
{ {
...@@ -15,17 +27,37 @@ public: ...@@ -15,17 +27,37 @@ public:
~OpenGlViewer(); ~OpenGlViewer();
private: private:
QOpenGLBuffer mVertexBuffer; // OpenGL ======================================
QOpenGLVertexArrayObject mVertexArray; QOpenGLBuffer m_vertexBuffer;
QOpenGLShaderProgram* mProgram = nullptr; QOpenGLVertexArrayObject m_vertexArray;
QOpenGLShaderProgram* m_program = nullptr;
private: // Data ========================================
void printContextInformation(); std::vector<Model*>* m_vModels = nullptr;
std::vector<textureData*> m_vTextures;
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;
protected: private:
virtual void initializeGL() override final; virtual void initializeGL() override final;
virtual void resizeGL(int w, int h) override final; virtual void resizeGL(int w, int h) override final;
virtual void paintGL() override final; virtual void paintGL() override final;
void printContextInformation();
void deleteData();
public: public:
void setData(std::vector<Model*>* models, std::vector<textureData*> textures);
}; };
#ifndef VERTEX_H #pragma once
#define VERTEX_H
#include <QVector3D> #include <QVector3D>
...@@ -51,5 +50,3 @@ void inline Vertex::setColor(const QVector3D& color) { m_color = color; } ...@@ -51,5 +50,3 @@ void inline Vertex::setColor(const QVector3D& color) { m_color = color; }
Q_DECL_CONSTEXPR inline int Vertex::positionOffset() { return offsetof(Vertex, m_position); } Q_DECL_CONSTEXPR inline int Vertex::positionOffset() { return offsetof(Vertex, m_position); }
Q_DECL_CONSTEXPR inline int Vertex::colorOffset() { return offsetof(Vertex, m_color); } Q_DECL_CONSTEXPR inline int Vertex::colorOffset() { return offsetof(Vertex, m_color); }
Q_DECL_CONSTEXPR inline int Vertex::stride() { return sizeof(Vertex); } Q_DECL_CONSTEXPR inline int Vertex::stride() { return sizeof(Vertex); }
#endif // VERTEX_H
\ No newline at end of file
...@@ -5,6 +5,4 @@ ...@@ -5,6 +5,4 @@
#define WINDOW_HEIGHT 480 #define WINDOW_HEIGHT 480
#define DEFAULT_STATUS_MESSAGE "Mesh Viewer pre alpha by Anakin" #define DEFAULT_STATUS_MESSAGE "Mesh Viewer pre alpha by Anakin"
#define DEFAULT_MAJOR_VERSION 4
#define DEFAULT_MINOR_VERSION 5
#define DEAFAULT_BACKGROUND 0.5000f, 0.8000f, 1.0000f, 0.0000f
...@@ -5,5 +5,7 @@ ...@@ -5,5 +5,7 @@
<qresource prefix="/shaders"> <qresource prefix="/shaders">
<file>simple.frag</file> <file>simple.frag</file>
<file>simple.vert</file> <file>simple.vert</file>
<file>TextureShader.frag</file>
<file>TextureShader.vert</file>
</qresource> </qresource>
</RCC> </RCC>
#version 450
// Input
in vec2 UV;
// Output
out vec4 color;
void main()
{
color = texture(textureSampler, UV).rgb;
}
\ No newline at end of file
#version 450
// 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
#include "MainWindow.h" #include "..\Header\MainWindow.h"
#include "OpenGlViewer.h" #include "..\Header\OpenGlViewer.h"
#include "defines.h" #include "..\Header\defines.h"
#include <QKeyEvent> #include <QKeyEvent>
#include <QMessageBox> #include <QMessageBox>
......
#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)
: 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);
// 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;
}
// close file
m_fsMesh.close();
}
MshFile::~MshFile()
{
m_vTextureNames.clear();
}
/////////////////////////////////////////////////////////////////////////
// private functions
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);