Commit 45408bb6 by Kai Westerkamp

loading mesh

parent aaeaf832
......@@ -14,10 +14,14 @@ TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mainwidget.cpp
mainwidget.cpp \
mesh.cpp \
texture.cpp
HEADERS += mainwindow.h \
mainwidget.h
mainwidget.h \
mesh.h \
texture.h
FORMS +=
......
#version 330
layout(location = 0) out vec3 color;
in vec3 vPosition;
in vec3 vCamPosition;
in vec3 vCamNormal;
in vec2 vUV;
uniform sampler2D colorTexture;
uniform vec3 LightPos;
struct Material
{
vec3 Diffuse;
vec3 Specular;
float Shininess;
bool hasTexture;
};
uniform Material materialInfo;
vec3 phong(vec3 lightPos){
vec3 dcolor = materialInfo.Diffuse;
if(materialInfo.hasTexture){
dcolor = texture2D(colorTexture,vUV).xyz;
}
vec3 V = normalize(-vCamPosition);
vec3 N = normalize(vCamNormal);
vec3 L = normalize(lightPos-vCamPosition);
vec3 R = normalize(reflect(-L,N));
float diffuse = max(dot(L,N),0.0);
float specular = pow(max(dot(R,V),0.0),materialInfo.Shininess);
//return (color * specular);
//return (color * diffuse);
return (dcolor * diffuse+ materialInfo.Specular * specular);
}
void main(void)
{
color = vec3(1.0,0.0,0.0);
color = phong(LightPos);
//color = texture2D(colorTexture,vUV).xyz;
}
#version 330
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 UV;
layout(location = 2) in ivec4 BoneIDs;
layout(location = 2) in vec4 Weights;
out vec3 vPosition;
uniform mat4x4 VP;
out vec3 vCamPosition;
out vec3 vCamNormal;
out vec2 vUV;
uniform mat4x4 MV;
uniform mat4x4 MVP;
uniform mat3x3 N;
void main(void)
{
vPosition = Position;
gl_Position = VP * vec4(Position,1.0);
vCamPosition = vec4(MV*vec4(Position,1.0)).xyz;
vCamNormal = N*Normal;
vUV = UV;
gl_Position = MVP * vec4(Position,1.0);
}
......@@ -2,19 +2,7 @@
MainWidget::MainWidget()
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("",
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
if( !scene)
{
qDebug()<<importer.GetErrorString();
}else {
//DoTheSceneProcessing( scene);
}
}
QSize MainWidget::minimumSizeHint() const
......@@ -27,6 +15,19 @@ QSize MainWidget::sizeHint() const
return QSize(1280, 720);
}
void MainWidget::wheelEvent(QWheelEvent *event )
{
if(event->delta()<0)
camDistance -=10.0;
else
camDistance +=10.0;
m_view.setToIdentity();
m_view.lookAt(QVector3D(0.0,camDistance,camDistance),QVector3D(0.0,0.0,0.0),QVector3D(0.0,1.0,0.0));
}
void MainWidget::initializeGL(){
bool glFunctionsOK = initializeOpenGLFunctions();
......@@ -34,8 +35,6 @@ void MainWidget::initializeGL(){
if(!glFunctionsOK)
exit(4);
qDebug()<<"Version"<<this->format().version();
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
......@@ -43,54 +42,23 @@ void MainWidget::initializeGL(){
// Shader
animationShader = initShader(QLatin1String(":/animate.frag"),QLatin1String(":/animate.vert"));
camDistance = 30.0;
m_view = QMatrix4x4();
m_view.lookAt(QVector3D(2.0,2.0,4.0),QVector3D(0.0,0.0,0.0),QVector3D(0.0,1.0,0.0));
//temp
static const GLfloat g_vertex_buffer_data[] = {
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f
};
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
m_view.lookAt(QVector3D(0.0,camDistance,camDistance),QVector3D(0.0,0.0,0.0),QVector3D(0.0,1.0,0.0));
loadNewMesh();
}
void MainWidget::loadNewMesh(){
QString fn = QFileDialog::getOpenFileName(NULL, tr("Open Mesh..."),
QString("D:\\Projekte\\GraPa\\A5\\Models"),
tr("*.md5mesh *.3ds *.md2 *.obj" ));
if(fn.isEmpty())
return;
qDebug()<<"Opening File:"<<fn;
mesh = new Mesh(this,fn);
}
QOpenGLShaderProgram* MainWidget::initShader(QString fragSource, QString vertSource){
......@@ -115,40 +83,36 @@ QOpenGLShaderProgram* MainWidget::initShader(QString fragSource, QString vertSou
exit(5);
}
return shader;
}
void MainWidget::paintGL(){
glViewport(0,0,width(),height());
qDebug()<<"Format"<<this->format().version();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
animationShader->bind();
QMatrix4x4 VP = m_projection*m_view;
//QMatrix4x4 VP = m_view*m_projection;
QMatrix4x4 rot;
int time = QTime::currentTime().second()*1000+QTime::currentTime().msec();
rot.rotate(time/100.0*36/5,QVector3D(0,1,0));
animationShader->bind();
animationShader->setUniformValue("colorTexture",0);
animationShader->setUniformValue("LightPos",QVector3D(0,100,100));
animationShader->setUniformValue("VP",VP);
glEnableVertexAttribArray(positionIndex);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, 0,(void*)0 );
glDrawArrays(GL_TRIANGLES, 0, 12*3);
glDisableVertexAttribArray(positionIndex);
mesh->render(animationShader,m_view*rot, m_projection);
animationShader->release();
update();
}
void MainWidget::resizeGL(int width, int height){
qDebug()<<"Resize"<<width<<height;
m_projection = QMatrix4x4();
m_projection.perspective(45.0,1.0*width/height,0.01,100.0);
m_projection.perspective(45.0f,1.0*width/height,0.01f,1000.0f);
}
......
......@@ -4,14 +4,15 @@
#include <QObject>
#include <QtOpenGL>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLFunctions_4_3_Core>
#include <QOpenGLShaderProgram>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "mesh.h"
class MainWidget : public QOpenGLWidget, public QOpenGLFunctions_3_3_Core
class MainWidget : public QOpenGLWidget, public QOpenGLFunctions_4_3_Core
{
Q_OBJECT
public:
......@@ -20,23 +21,24 @@ public:
QSize minimumSizeHint() const;
QSize sizeHint() const;
public slots:
void loadNewMesh();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void wheelEvent(QWheelEvent *event ) ;
private:
static const int positionIndex = 0;
static const int normalIndex = 1;
static const int uvIndex = 2;
static const int boneIndex = 3;
static const int boneweightIndex = 4;
QOpenGLShaderProgram* animationShader;
QMatrix4x4 m_projection;
QMatrix4x4 m_view;
GLuint vertexbuffer;
float camDistance;
Mesh *mesh;
QOpenGLShaderProgram* initShader(QString fragSource, QString vertSource);
};
......
......@@ -3,8 +3,13 @@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
m_centralWidget = new MainWidget();
loadMesh = new QAction("Add Mesh",this);
loadMesh->setShortcut(QKeySequence("Ctrl+N"));
connect(loadMesh, SIGNAL(triggered()), m_centralWidget, SLOT(loadNewMesh()));
this->addAction(loadMesh);
m_centralWidget = new MainWidget();
this->setCentralWidget(m_centralWidget);
// showMaximized();
......
......@@ -3,6 +3,7 @@
#include <QMainWindow>
#include <mainwidget.h>
#include <mesh.h>
namespace Ui {
class MainWindow;
......@@ -18,7 +19,9 @@ public:
private:
MainWidget *m_centralWidget;
QMatrix4x4 m_projection;
QAction *loadMesh;
};
#endif // MAINWINDOW_H
#ifndef MESH_H
#define MESH_H
#include <QtOpenGL>
#include <QOpenGLFunctions_4_3_Core>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <postprocess.h>
#include "texture.h"
#define positionIndex 0
#define normalIndex 1
#define uvIndex 2
#define boneIndex 3
#define boneweightIndex 4
struct Vertex
{
QVector3D pos;
QVector3D normal;
QVector2D tex;
uint IDs[4];
float weights[4];
Vertex() {
for(int i = 0; i < 4; i++){
IDs[i] = 0;
weights[i] = 0;
}
}
Vertex(const QVector3D& pos, const QVector3D& normal, const QVector2D& tex)
{
this->pos = pos;
this->tex = tex;
this->normal = normal;
}
void AddBoneData(uint BoneID, float Weight);
};
class Mesh
{
public:
Mesh(QOpenGLFunctions_4_3_Core *f,QString filename);
~Mesh();
void render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P);
private:
struct MeshEntry{
MeshEntry();
~MeshEntry();
void init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Vertices,
QVector<unsigned int>& Indices);
QString name;
GLuint VB;
GLuint IB;
int numIndex;
int materialIndex;
QOpenGLFunctions_4_3_Core *f;
};
struct MaterialInfo
{
MaterialInfo();
QString Name;
QVector3D Diffuse;
QVector3D Specular;
bool hasTexture;
Texture texture;
float Shininess;
};
struct LightInfo
{
QVector4D Position;
QVector3D Intensity;
};
struct Node
{
Node();
QString name;
QMatrix4x4 transformation;
QVector<int> meshes;
QVector<Node> children;
};
const aiScene *scene;
QMatrix4x4 globalInverseTransform;
QVector<MeshEntry> entries;
QVector<MaterialInfo> materials;
Node rootNode;
QOpenGLFunctions_4_3_Core *f;
bool loaded;
void initMeshEntry(int i,aiMesh * entry);
void initMaterial(QString dir, unsigned int i, const aiMaterial* material);
void initNode(const aiScene *scene, aiNode *node, Node &newNode);
void renderNode(QOpenGLShaderProgram *shader, Node &node, QMatrix4x4 V,QMatrix4x4 P,QMatrix4x4 M);
void renderMesh(QOpenGLShaderProgram *shader, int index);
};
#endif // MESH_H
#include "texture.h"
Texture::Texture()
{
fileName= QString();
textureTarget = GL_TEXTURE_2D;
textureObj = 0xFFFFFFFF;
}
void Texture::Load(GLenum textureTarget, QString fileName)
{
this->textureTarget = textureTarget;
this->fileName = fileName;
//load
if(fileName.isEmpty()){
qDebug()<<"No Image to load";
return;
}
qDebug()<<"Loading Texture:"<<fileName;
QString suffix = QFileInfo(fileName).completeSuffix();
if(suffix.endsWith("pcx")||suffix.endsWith("tga")){
fileName.replace(QRegularExpression(suffix),QString("png"));
suffix = QString("png");
}
if(!QFile::exists(fileName))
{
qDebug()<<"File "<<fileName<<"don't exists";
return;
}
QImage *texture = new QImage(fileName, suffix.toStdString().c_str());
if (texture == NULL|| texture->isNull()){
qDebug()<<"Could not load Texture:"<<fileName<<"Suffix:"<< suffix;
qDebug()<<QImageReader::supportedImageFormats();
return;
}
QImage textureImg = QGLWidget::convertToGLFormat( *texture );
glGenTextures( 1, &textureObj );
glBindTexture( this->textureTarget, textureObj );
glTexImage2D(this->textureTarget, 0, GL_RGBA, textureImg.width(), textureImg.height(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, textureImg.bits());
// Filtering
glTexParameteri( this->textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( this->textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glBindTexture( this->textureTarget, 0 );
qDebug()<<"Loaded texture"+fileName;
}
Texture::~Texture(){
glDeleteTextures(1,&textureObj);
}
void Texture::bind(QOpenGLFunctions_4_3_Core *f, GLenum textureUnit){
f->glActiveTexture(textureUnit);
f->glBindTexture(textureTarget, textureObj);
}
#ifndef TEXTURE_H
#define TEXTURE_H
#include <QObject>
#include <QImage>
#include <QtOpenGL>
#include <QOpenGLFunctions_4_3_Core>
class Texture
{
public:
Texture();
~Texture();
void bind(QOpenGLFunctions_4_3_Core *f, GLenum textureUnit);
void Load(GLenum TextureTarget, const QString FileName);
private:
QString fileName;
GLenum textureTarget;
GLuint textureObj;
};
#endif // TEXTURE_H
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
# Blender v2.57 (sub 0) OBJ File: ''
# www.blender.org
mtllib box.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 2.094305 1.396205
vt 1.396205 2.094305
vt 0.698100 1.396205
vt 1.396205 0.698100
vt 5.000000 5.000000
vt 2.792410 5.000000
vt 2.792410 2.792410
vt 5.000000 2.792410
vt 2.792410 2.094305
vt 2.094305 2.792410
vt 0.698100 2.792410
vt 0.000000 2.094305
vt 0.000000 0.698100
vt 0.698100 0.000000
vt 2.792410 0.698100
vt 2.094305 0.000000
usemtl Material
s off
f 1/1 2/2 3/3
f 1/1 3/3 4/4
f 5/5 8/6 7/7
f 5/5 7/7 6/8
f 1/1 5/9 6/10
f 1/1 6/10 2/2
f 2/2 6/11 7/12
f 2/2 7/12 3/3
f 3/3 7/13 8/14
f 3/3 8/14 4/4
f 5/15 1/1 4/4
f 5/15 4/4 8/16
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
# Blender v2.57 (sub 0) OBJ File: ''
# www.blender.org
o Cube_Cube.001
v -1.000000 1.000000 0.000000
v 1.000000 1.000000 0.000000
v -1.000000 -1.000000 0.000000
v 1.000000 -1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
usemtl (null)
s off
f 4/4 3/3 1/2
f 2/1 4/4 1/2
# Blender v2.57 (sub 0) OBJ File: ''
# www.blender.org
o Cube_Cube.001
v -1.000000 1.000000 0.000000
v 1.000000 1.000000 0.000000
v -1.000000 -1.000000 0.000000
v 1.000000 -1.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
usemtl (null)
s off
f 4/4 3/3 1/2
f 2/1 4/4 1/2
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment