From 5db14a389062b1e58193a036974d1f433dcbc683 Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Fri, 10 Mar 2017 00:56:37 -0600 Subject: [PATCH] added materials to the stim::obj object --- stim/visualization/obj.h | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ stim/visualization/obj/obj_material.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 stim/visualization/obj/obj_material.h diff --git a/stim/visualization/obj.h b/stim/visualization/obj.h index 3e68623..32087de 100644 --- a/stim/visualization/obj.h +++ b/stim/visualization/obj.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include @@ -93,13 +95,13 @@ protected: }; //end vertex //triplet used to specify geometric vertices consisting of a position vertex, texture vertex, and normal - struct triplet : public std::vector{ + struct triplet : public std::vector{ //default constructor, empty triplet triplet(){} //create a triplet given a parameter list (OBJ indices start at 1, so 0 can be used to indicate no value) - triplet(unsigned int v, unsigned int vt = 0, unsigned int vn = 0){ + triplet(size_t v, size_t vt = 0, size_t vn = 0){ push_back(v); if(vn != 0){ push_back(vt); @@ -140,12 +142,12 @@ protected: if(size() == 3){ if(at(1) == 0) - ss<<"\\\\"< P; //list of points structures std::vector F; //list of faces + //material lists + std::vector< obj_material > M; //list of material descriptors + std::vector Mf; //face index where each material begins + //information for the current geometric object geometry current_geo; vertex current_vt; vertex current_vn; + obj_material current_material; //stores the current material + bool new_material; //flags if a material property has been changed since the last material was pushed //flags for the current geometric object obj_type current_type; @@ -258,9 +266,9 @@ protected: //create a triple and add it to the current geometry void update_v(vertex vv){ - unsigned int v; - unsigned int vt = 0; - unsigned int vn = 0; + size_t v; + size_t vt = 0; + size_t vn = 0; //if the current geometry is using a texture coordinate, add the current texture coordinate to the geometry if(geo_flag_vt){ @@ -303,6 +311,8 @@ protected: geo_flag_vn = false; vert_flag_vt = false; vert_flag_vn = false; + + new_material = false; //initialize a new material to false (start with no material) } //gets the type of token representing the entry in the OBJ file @@ -346,10 +356,80 @@ public: void Vertex(T x, T y, T z){ update_v(vertex(x, y, z));} void Vertex(T x, T y, T z, T w){ update_v(vertex(x, y, z, w));} + ///Material functions + void matKa(T r, T g, T b) { + new_material = true; + current_material.ka[0] = r; + current_material.ka[1] = g; + current_material.ka[2] = b; + } + void matKa(std::string tex = std::string()) { + new_material = true; + current_material.tex_ka = tex; + } + void matKd(T r, T g, T b) { + new_material = true; + current_material.kd[0] = r; + current_material.kd[1] = g; + current_material.kd[2] = b; + } + void matKd(std::string tex = std::string()) { + new_material = true; + current_material.tex_kd = tex; + } + void matKs(T r, T g, T b) { + new_material = true; + current_material.ks[0] = r; + current_material.ks[1] = g; + current_material.ks[2] = b; + } + void matKs(std::string tex = std::string()) { + new_material = true; + current_material.tex_ks = tex; + } + void matNs(T n) { + new_material = true; + current_material.ns = n; + } + void matNs(std::string tex = std::string()) { + new_material = true; + current_material.tex_ns = tex; + } + void matIllum(int i) { + new_material = true; + current_material.illum = i; + } + void matD(std::string tex = std::string()) { + new_material = true; + current_material.tex_alpha = tex; + } + void matBump(std::string tex = std::string()) { + new_material = true; + current_material.tex_bump = tex; + } + void matDisp(std::string tex = std::string()) { + new_material = true; + current_material.tex_disp = tex; + } + void matDecal(std::string tex = std::string()) { + new_material = true; + current_material.tex_decal = tex; + } + ///This function starts drawing of a primitive object, such as a line, face, or point set /// @param t is the type of object to be drawn: OBJ_POINTS, OBJ_LINE, OBJ_FACE void Begin(obj_type t){ + if (new_material) { //if a new material has been specified + if (current_material.name == "") { //if a name wasn't given, create a new one + std::stringstream ss; //create a name for it + ss << "material" << M.size(); //base it on the material number + current_material.name = ss.str(); + } + Mf.push_back(F.size()); //start the material at the current face index + M.push_back(current_material); //push the current material + current_material.name = ""; //reset the name of the current material + } current_type = t; } @@ -468,10 +548,15 @@ public: } } - //output all of the lines + //output all of the faces if(F.size()){ ss< + +namespace stim { + +template +struct obj_material { + std::string name; //material name + T ka[3]; //ambient color + T kd[3]; //diffuse color + T ks[3]; //specular color + T ns; //specular exponent + + int illum; //illumination model + + std::string tex_ka; //ambient texture + std::string tex_kd; //diffuse texture + std::string tex_ks; //specular texture + std::string tex_ns; //texture map for the specular exponent + std::string tex_alpha; //texture map for the alpha component + std::string tex_bump; //bump map + std::string tex_disp; //displacement map + std::string tex_decal; //stencil decal + + obj_material() { //constructor + memset(ka, 0, sizeof(T) * 3); + memset(kd, 0, sizeof(T) * 3); + memset(ks, 0, sizeof(T) * 3); + ns = 10; + illum = 2; + + } + std::string str() { + std::stringstream ss; + ss << "newmtl " << name << std::endl; + ss << "Ka " << ka[0] << " " << ka[1] << " " << ka[2] << std::endl; + ss << "Kd " << kd[0] << " " << kd[1] << " " << kd[2] << std::endl; + ss << "Ks " << ks[0] << " " << ks[1] << " " << ks[2] << std::endl; + ss << "Ns " << ns << std::endl; + ss << "illum " << illum << std::endl; + if (tex_ka != "") ss << "map_Ka " << tex_ka << std::endl; + if (tex_kd != "") ss << "map_Kd " << tex_kd << std::endl; + if (tex_ks != "") ss << "map_Ks " << tex_ks << std::endl; + if (tex_ns != "") ss << "map_Ns " << tex_ns << std::endl; + if (tex_alpha != "") ss << "map_d " << tex_alpha << std::endl; + if (tex_bump != "") ss << "bump " << tex_bump << std::endl; + if (tex_disp != "") ss << "disp " << tex_disp << std::endl; + if (tex_decal != "") ss << "decal " << tex_decal << std::endl; + return ss.str(); + } +}; + +} //end namespace stim + +#endif \ No newline at end of file -- libgit2 0.21.4