Added mtl file loading

This commit is contained in:
Muaz Ahmad 2023-05-03 13:19:03 +05:00
parent d3adb5c325
commit a97fa446b4
6 changed files with 94 additions and 1 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@
*.o *.o
Makefile Makefile
ui_mainwindow.h ui_mainwindow.h
untitled.mtl
untitled.obj

View file

@ -17,6 +17,6 @@ DEPENDPATH += . \
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input # Input
SOURCES += main.cpp opglwidget.cpp SOURCES += main.cpp opglwidget.cpp obj.cpp
FORMS += mainwindow.ui FORMS += mainwindow.ui
QT += core gui widgets QT += core gui widgets

48
obj.cpp Normal file
View 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
View 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;
};

View file

@ -2,6 +2,7 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <math.h> #include <math.h>
#include "obj.h"
void OPGLWidget::initializeGL() { void OPGLWidget::initializeGL() {
glClearDepth(1.0f); glClearDepth(1.0f);
@ -20,6 +21,9 @@ void OPGLWidget::initializeGL() {
glLightfv(GL_LIGHT0, GL_SPECULAR, light_array); glLightfv(GL_LIGHT0, GL_SPECULAR, light_array);
glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
ObjectLoader obj;
obj.load_mtl("untitled.mtl");
} }
void OPGLWidget::paintGL() { void OPGLWidget::paintGL() {

View file

@ -10,5 +10,6 @@ protected:
void resizeGL(int w, int h); void resizeGL(int w, int h);
void reorient(); void reorient();
int mtl_idx = -1;
float loc[3] = {-10, 0, -10}; float loc[3] = {-10, 0, -10};
}; };