Skip to content

Commit

Permalink
easier acces to mesh parts
Browse files Browse the repository at this point in the history
  • Loading branch information
underdoeg committed Jun 29, 2014
1 parent 8d1dc1f commit ec7a9b8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 29 deletions.
8 changes: 6 additions & 2 deletions src/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ Material::Material() {
isTwoSided = false;
}

Material::Material(string imagePath):Material() {
textures.push_back(new Texture(imagePath));
}

Material::~Material() {
}

void Material::begin() {

ofSetColor(255);
if(textures.size()>0) {
if(textures[0]->img.isAllocated()) {
Expand Down Expand Up @@ -45,7 +49,7 @@ void Material::end() {
if(!useShader) {
material.end();
}

}

}
Expand Down
2 changes: 2 additions & 0 deletions src/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace blender {
class Texture {
public:
Texture():isEnabled(true) {}
Texture(string imgPath):isEnabled(true) {img.loadImage(imgPath);}
string name;
ofImage img;
string uvLayerName;
Expand All @@ -20,6 +21,7 @@ class Scene;
class Material {
public:
Material();
Material(string imagePath);
~Material();

void begin();
Expand Down
52 changes: 49 additions & 3 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void Mesh::customDraw() {
glCullFace(GL_BACK);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
}


for(Part& part: parts) {
if(part.hasTriangles)
part.draw();
Expand Down Expand Up @@ -101,6 +101,48 @@ void Mesh::addVertex(ofVec3f pos, ofVec3f norm) {
normals.push_back(norm);
}

void Mesh::addMesh(ofMesh& mesh) {
std::vector<ofVec3f>& verts = mesh.getVertices();
std::vector<ofVec3f>& normals = mesh.getNormals();
//std::vector<ofVec3f>& colors = mesh.getColors();
std::vector<ofVec2f>& uvs = mesh.getTexCoords();
std::vector<ofIndexType>& indices = mesh.getIndices();

for(unsigned int i=0; i<verts.size(); i++) {
if(normals.size() > i)
addVertex(verts[i], normals[i]);
else
addVertex(verts[i]);
}

ofPrimitiveMode mode = mesh.getMode();
switch(mode) {
case OF_PRIMITIVE_TRIANGLES:
for(unsigned int i=2; i<indices.size(); i+=3) {
Triangle tri(indices[i-2], indices[i-1], indices[i]);
if(uvs.size() > tri.c) {
TriangleUVs triUv(uvs[tri.a], uvs[tri.b], uvs[tri.c]);
tri.uvs.push_back(triUv);
}
addTriangle(tri);
}
break;
case OF_PRIMITIVE_TRIANGLE_STRIP:
for(unsigned int i=2; i<indices.size(); i++) {
Triangle tri(indices[i-2], indices[i-1], indices[i]);
if(uvs.size() > tri.c) {
TriangleUVs triUv(uvs[tri.a], uvs[tri.b], uvs[tri.c]);
tri.uvs.push_back(triUv);
}
addTriangle(tri);
}
break;
default:
ofLogWarning(OFX_BLENDER) << "Mesh::addMesh - mesh mode not supported";
}
}


ofVec3f& Mesh::getVertex(unsigned int pos) {
return vertices[pos];
}
Expand Down Expand Up @@ -129,6 +171,10 @@ Mesh::Part& Mesh::getPart(Material* mat, Shading shading, bool hasUvs) {
return parts.back();
}

std::vector<Mesh::Part>& Mesh::getParts() {
return parts;
}

void Mesh::pushMaterial(Material* material) {
if(material == curMaterial)
return;
Expand Down Expand Up @@ -182,7 +228,7 @@ void Mesh::build() {
mesh.addTexCoord(tri.uvs[0].c);
}
mesh.addTriangle(curIndex, curIndex+1, curIndex+2);

//add the material to the used materials
if(part.material && std::find(materials.begin(), materials.end(), part.material)==materials.end())
materials.push_back(part.material);
Expand Down
53 changes: 29 additions & 24 deletions src/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class Scene;

class Mesh: public ofx::blender::Object {
public:
Mesh();
~Mesh();

class TriangleUVs {
public:
TriangleUVs(ofVec2f a_, ofVec2f b_, ofVec2f c_) {
Expand All @@ -39,7 +36,7 @@ class Mesh: public ofx::blender::Object {
b = b_;
c = c_;
}

Triangle(unsigned int a_, unsigned int b_, unsigned int c_, ofVec2f uv1, ofVec2f uv2, ofVec2f uv3) {
Triangle(a_, b_, c_);
addUVs(uv1, uv2, uv3);
Expand All @@ -56,19 +53,46 @@ class Mesh: public ofx::blender::Object {
Shading shading;
std::vector<TriangleUVs> uvs;
};


class Part {
public:
Part(Material* mat, Shading shade, bool hasUvs_) {
material = mat;
shading = shade;
hasTriangles = false;
hasUvs = hasUvs_;
primitive.setUseVbo(true);
}

void draw();

of3dPrimitive primitive;
Material* material;
Shading shading;
std::vector<Triangle> polys;
bool hasTriangles;
bool hasUvs;
};

///////////////////////////////////////////////////////////////
Mesh();
~Mesh();

void pushMaterial(Material* material);
void pushShading(Shading shading);

void addVertex(ofVec3f pos, ofVec3f norm=ofVec3f());
void addTriangle(Triangle triangle);
void addMesh(ofMesh& mesh);

ofVec3f& getVertex(unsigned int index);
ofVec3f& getNormal(unsigned int index);
Triangle& getTriangle(unsigned int index);

void exportUVs(int w=1024, int h=1024, unsigned int layer=0, string path="");

std::vector<Part>& getParts();

void clear();

void build();
Expand All @@ -85,25 +109,6 @@ class Mesh: public ofx::blender::Object {

private:
friend class Scene;
class Part {
public:
Part(Material* mat, Shading shade, bool hasUvs_) {
material = mat;
shading = shade;
hasTriangles = false;
hasUvs = hasUvs_;
primitive.setUseVbo(true);
}

void draw();

of3dPrimitive primitive;
Material* material;
Shading shading;
std::vector<Triangle> polys;
bool hasTriangles;
bool hasUvs;
};

Part& getPart(Material* mat, Shading shading, bool hasUvs);

Expand Down

0 comments on commit ec7a9b8

Please # to comment.