From a97fa446b4ec6fa292196040b00f14b892543164 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Wed, 3 May 2023 13:19:03 +0500 Subject: [PATCH] Added mtl file loading --- .gitignore | 2 ++ 3dobj-renderer.pro | 2 +- obj.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ obj.h | 38 ++++++++++++++++++++++++++++++++++++ opglwidget.cpp | 4 ++++ opglwidget.h | 1 + 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 obj.cpp create mode 100644 obj.h diff --git a/.gitignore b/.gitignore index 716ab95..574c181 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.o Makefile ui_mainwindow.h +untitled.mtl +untitled.obj diff --git a/3dobj-renderer.pro b/3dobj-renderer.pro index 10ca2df..d86befc 100644 --- a/3dobj-renderer.pro +++ b/3dobj-renderer.pro @@ -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 diff --git a/obj.cpp b/obj.cpp new file mode 100644 index 0000000..14cd47e --- /dev/null +++ b/obj.cpp @@ -0,0 +1,48 @@ +#include "obj.h" +#include +#include +#include +#include + +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(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)) { + + } +} diff --git a/obj.h b/obj.h new file mode 100644 index 0000000..f2a3ac9 --- /dev/null +++ b/obj.h @@ -0,0 +1,38 @@ +#include +#include +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 vertices; + Normal normal; + Material mtl; +}; + +class ObjectLoader +{ +public: + void load_obj(string filename); + void load_mtl(string filename); + + vector faces; + +protected: + vector vertices; + vector normals; + vector mtls; +}; diff --git a/opglwidget.cpp b/opglwidget.cpp index 280a79a..9e137bd 100644 --- a/opglwidget.cpp +++ b/opglwidget.cpp @@ -2,6 +2,7 @@ #include #include #include +#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() { diff --git a/opglwidget.h b/opglwidget.h index 180f9d3..2c56299 100644 --- a/opglwidget.h +++ b/opglwidget.h @@ -10,5 +10,6 @@ protected: void resizeGL(int w, int h); void reorient(); + int mtl_idx = -1; float loc[3] = {-10, 0, -10}; };