added first version of obj loader
This commit is contained in:
parent
66c35fd0f9
commit
99bc0326c3
1281
data/sphere.obj
Normal file
1281
data/sphere.obj
Normal file
File diff suppressed because it is too large
Load Diff
@ -48,19 +48,20 @@ Scene* MyGLWidget::buildScene()
|
||||
SkyBox* skybox = new SkyBox(filenames);
|
||||
scene->addEntity(skybox);
|
||||
|
||||
Mesh* myGrid = new GridMesh(10, 10, true); //new Sphere(2);
|
||||
//Mesh* myGrid = new GridMesh(10, 10, true); //new Sphere(2);
|
||||
Mesh* myGrid = Utils::loadOBJ("../data/sphere.obj");
|
||||
myGrid->initGL();
|
||||
|
||||
std::string vertSource = Utils::fileToString("../phong.vert");
|
||||
std::string fragSource = Utils::fileToString("../phong.frag");
|
||||
Shader* shader = new Shader(vertSource, fragSource);
|
||||
PhongMaterial* mat = new PhongMaterial(shader, glm::vec3(1), glm::vec3(1), 20.0f);
|
||||
Texture* tex = new Texture(); // new Texture("../data/noise.png");
|
||||
Texture* tex = new Texture("../data/noise.png");
|
||||
mat->setTexture(tex);
|
||||
|
||||
Entity* myEntity = new Entity(NULL, myGrid, mat);
|
||||
glm::mat4* transform = myEntity->getTransform();
|
||||
*transform = glm::translate(glm::rotate(glm::scale(*transform, glm::vec3(4)), 3.1416f/2, glm::vec3(-1, 0, 0)), glm::vec3(-0.5f, -0.5f, 0));
|
||||
*transform = glm::translate(glm::rotate(glm::scale(*transform, glm::vec3(0.1f)), 3.1416f/2, glm::vec3(-1, 0, 0)), glm::vec3(-0.5f, -0.5f, 0));
|
||||
scene->addEntity(myEntity);
|
||||
|
||||
scene->addDirectionnalLight(glm::vec3(6, 4, -4), glm::vec3(0.7f, 0.6f, 0.4f)); // sun
|
||||
|
73
utils.cpp
73
utils.cpp
@ -1,7 +1,9 @@
|
||||
#include "utils.h"
|
||||
#include "mesh.h"
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QImage>
|
||||
#include <vector>
|
||||
|
||||
std::string Utils::fileToString(const std::string &filename)
|
||||
{
|
||||
@ -12,6 +14,77 @@ std::string Utils::fileToString(const std::string &filename)
|
||||
return in.readAll().toStdString();
|
||||
}
|
||||
|
||||
Mesh* Utils::loadOBJ(const std::string &filename)
|
||||
{
|
||||
Mesh* mesh = new Mesh();
|
||||
std::vector<glm::vec3> pos;
|
||||
std::vector<glm::vec3> norm;
|
||||
std::vector<glm::vec2> tex;
|
||||
QFile f(QString(filename.c_str()));
|
||||
if(!f.open(QFile::ReadOnly | QFile::Text))
|
||||
return NULL;
|
||||
QTextStream in(&f);
|
||||
QString line;
|
||||
QStringList list;
|
||||
QStringList faceList;
|
||||
Mesh::Vertex v;
|
||||
int nb_vertices = 0;
|
||||
line = in.readLine();
|
||||
while(!line.isNull())
|
||||
{
|
||||
if(line.isEmpty())
|
||||
{
|
||||
line = in.readLine();
|
||||
continue;
|
||||
}
|
||||
switch(line.at(0).toLatin1())
|
||||
{
|
||||
case 'v':
|
||||
//vertex attribute
|
||||
list = line.split(QChar(' '));
|
||||
switch(line.at(1).toLatin1())
|
||||
{
|
||||
case ' ':
|
||||
// vertex position
|
||||
pos.push_back(glm::vec3(list[1].toFloat(), list[2].toFloat(), list[3].toFloat()));
|
||||
break;
|
||||
case 't':
|
||||
// texCoord
|
||||
tex.push_back(glm::vec2(list[1].toFloat(), list[2].toFloat()));
|
||||
break;
|
||||
case 'n':
|
||||
// normal
|
||||
norm.push_back(glm::vec3(list[1].toFloat(), list[2].toFloat(), list[3].toFloat()));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
// face
|
||||
list = line.split(QChar(' '));
|
||||
mesh->addFace(nb_vertices, nb_vertices+1, nb_vertices+2);
|
||||
for(int i=0; i<3; ++i)
|
||||
{
|
||||
faceList = list[i+1].split(QChar('/'));
|
||||
v.position = pos[faceList[0].toInt()];
|
||||
v.texCoord= tex[faceList[1].toInt()];
|
||||
v.normal = norm[faceList[2].toInt()];
|
||||
mesh->addVertex(v);
|
||||
}
|
||||
nb_vertices += 3;
|
||||
break;
|
||||
case 'u':
|
||||
// usemtl
|
||||
break;
|
||||
default:
|
||||
case '#':
|
||||
// comment
|
||||
break;
|
||||
}
|
||||
line = in.readLine();
|
||||
}
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Utils::Image::Image(std::string filename)
|
||||
{
|
||||
img = new QImage(QString(filename.c_str()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user