Added mtl file loading
This commit is contained in:
parent
d3adb5c325
commit
a97fa446b4
6 changed files with 94 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,3 +2,5 @@
|
|||
*.o
|
||||
Makefile
|
||||
ui_mainwindow.h
|
||||
untitled.mtl
|
||||
untitled.obj
|
||||
|
|
|
@ -17,6 +17,6 @@ DEPENDPATH += . \
|
|||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
# Input
|
||||
SOURCES += main.cpp opglwidget.cpp
|
||||
SOURCES += main.cpp opglwidget.cpp obj.cpp
|
||||
FORMS += mainwindow.ui
|
||||
QT += core gui widgets
|
||||
|
|
48
obj.cpp
Normal file
48
obj.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "obj.h"
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
void ObjectLoader::load_mtl(string filename) {
|
||||
string tmp;
|
||||
ifstream f(filename);
|
||||
int mtl_idx = -1;
|
||||
while (getline(f, tmp)) {
|
||||
if (tmp.rfind("newmtl", 0) == 0) {
|
||||
mtl_idx += 1;
|
||||
mtls.push_back(*(new Material));
|
||||
mtls.back().name = tmp.substr(tmp.rfind(' ') + 1);
|
||||
} else if (tmp[0] == 'K') {
|
||||
float* target_list;
|
||||
switch (tmp[1]) {
|
||||
case 'a':
|
||||
target_list = mtls.back().ambient;
|
||||
break;
|
||||
case 'd':
|
||||
target_list = mtls.back().diffuse;
|
||||
break;
|
||||
case 's':
|
||||
target_list = mtls.back().specular;
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
};
|
||||
char delim = ' ';
|
||||
char *token = strtok(const_cast<char*>(tmp.c_str()), &delim);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
token = strtok(NULL, &delim);
|
||||
target_list[i] = atof(token);
|
||||
};
|
||||
target_list[3] = 1.0f;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
void ObjectLoader::load_obj(string filename) {
|
||||
string tmp;
|
||||
ifstream f(filename);
|
||||
while (getline(f, tmp)) {
|
||||
|
||||
}
|
||||
}
|
38
obj.h
Normal file
38
obj.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
struct Vertex {
|
||||
float pos[3];
|
||||
};
|
||||
|
||||
struct Normal {
|
||||
float pos[3];
|
||||
};
|
||||
|
||||
struct Material {
|
||||
int idx;
|
||||
string name;
|
||||
float ambient[4];
|
||||
float specular[4];
|
||||
float diffuse[4];
|
||||
};
|
||||
|
||||
struct Face {
|
||||
vector<Vertex> vertices;
|
||||
Normal normal;
|
||||
Material mtl;
|
||||
};
|
||||
|
||||
class ObjectLoader
|
||||
{
|
||||
public:
|
||||
void load_obj(string filename);
|
||||
void load_mtl(string filename);
|
||||
|
||||
vector<Face> faces;
|
||||
|
||||
protected:
|
||||
vector<Vertex> vertices;
|
||||
vector<Normal> normals;
|
||||
vector<Material> mtls;
|
||||
};
|
|
@ -2,6 +2,7 @@
|
|||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <math.h>
|
||||
#include "obj.h"
|
||||
|
||||
void OPGLWidget::initializeGL() {
|
||||
glClearDepth(1.0f);
|
||||
|
@ -20,6 +21,9 @@ void OPGLWidget::initializeGL() {
|
|||
glLightfv(GL_LIGHT0, GL_SPECULAR, light_array);
|
||||
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
ObjectLoader obj;
|
||||
obj.load_mtl("untitled.mtl");
|
||||
}
|
||||
|
||||
void OPGLWidget::paintGL() {
|
||||
|
|
|
@ -10,5 +10,6 @@ protected:
|
|||
void resizeGL(int w, int h);
|
||||
void reorient();
|
||||
|
||||
int mtl_idx = -1;
|
||||
float loc[3] = {-10, 0, -10};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue