forked from openframeworks/openFrameworks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofxAssimpSrcMesh.cpp
More file actions
173 lines (146 loc) · 5.08 KB
/
ofxAssimpSrcMesh.cpp
File metadata and controls
173 lines (146 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "ofxAssimpSrcMesh.h"
#include "ofxAssimpUtils.h"
#include "of3dGraphics.h"
#include "ofGraphics.h"
using std::make_shared;
using std::shared_ptr;
using namespace ofxAssimp;
static unsigned int sUniqueMeshCounter = 0;
ofTexture SrcMesh::sDummyTex;
//-------------------------------------------
void SrcMesh::addTexture( std::shared_ptr<ofxAssimp::Texture> aAssimpTex){
if(!material) {
material = std::make_shared<ofMaterial>();
}
auto tAiType = aAssimpTex->getAiTextureType();
auto tofType = ofxAssimp::Texture::ofTextureTypeForAiType(tAiType);
if(tofType == OF_MATERIAL_TEXTURE_NONE ) {
ofLogError("ofxAssimp::Mesh::addTexture") << aAssimpTex->getAiTextureTypeAsString();
return;
}
bool bGoAheadAndAdd = false;
if( tofType == OF_MATERIAL_TEXTURE_DIFFUSE) {
if(!hasTexture(OF_MATERIAL_TEXTURE_DIFFUSE)) {
bGoAheadAndAdd = true;
}
} else {
if( tofType != OF_MATERIAL_TEXTURE_NONE ) {
bGoAheadAndAdd = true;
}
}
if(bGoAheadAndAdd) {
// auto nAssimpTex = std::make_shared<ofxAssimpTexture>();
// (*nAssimpTex.get()) = aAssimpTex;
meshTextures.push_back(aAssimpTex);
material->setTexture(tofType, aAssimpTex->getTextureRef());
}
}
//-------------------------------------------
bool SrcMesh::hasTexture() {
return meshTextures.size() > 0;
}
//-------------------------------------------
bool SrcMesh::hasTexture(aiTextureType aTexType){
for( auto& tex : meshTextures ){
if( tex && tex->getAiTextureType() == aTexType){
return tex->hasTexture();
}
}
return false;
}
//-------------------------------------------
bool SrcMesh::hasTexture(ofMaterialTextureType aType){
return hasTexture( ofxAssimp::Texture::aiTextureTypeForOfType(aType));
}
//-------------------------------------------
std::size_t SrcMesh::getNumTextures() {
return meshTextures.size();
}
//-------------------------------------------
ofTexture& SrcMesh::getTexture() {
for( auto iter = ofxAssimp::Texture::sAiTexTypeToOfTexTypeMap.begin(); iter != ofxAssimp::Texture::sAiTexTypeToOfTexTypeMap.end(); iter++ ) {
if( hasTexture((aiTextureType)iter->first) ) {
return getTexture((aiTextureType)iter->first);
}
}
ofLogWarning("ofxAssimp::Mesh::getTexture") << " unable to find any allocated texture";
return sDummyTex;
}
//-------------------------------------------
ofTexture& SrcMesh::getTexture(aiTextureType aTexType){
for( auto & tex : meshTextures ){
if( tex && tex->getAiTextureType() == aTexType){
return tex->getTextureRef();
}
}
ofLogWarning("ofxAssimp::SrcMesh::getTexture : unable to find texture ref for ") << aTexType;
return sDummyTex;
}
//-------------------------------------------
ofTexture& SrcMesh::getTexture(ofMaterialTextureType aType){
return getTexture( ofxAssimp::Texture::aiTextureTypeForOfType(aType) );
}
//-------------------------------------------
void SrcMesh::setAiMesh( aiMesh* amesh, aiNode* aAiNode ) {
SrcNode::setAiNode(aAiNode);
mAiMesh = amesh;
if( mAiMesh != NULL ) {
vbo = std::make_shared<ofVbo>();
mName = mAiMesh->mName.data;
getName(); // forces setting of the name
// if(!material) {
// material = std::make_shared<ofMaterial>();
// }
mName = mAiMesh->mName.data;
if(mName.empty() ) {
// the mesh is not named or we were unable to detect one
mName = "Default Mesh "+ofToString(sUniqueMeshCounter,0);
if( sUniqueMeshCounter > std::numeric_limits<unsigned int>().max()-3) {
sUniqueMeshCounter = 0;
}
}
}
}
//-------------------------------------------
void SrcMesh::setupVbo( std::shared_ptr<ofVbo> avbo ) {
ofMesh tempMesh;
if( hasTexture() ) {
ofxAssimp::Utils::aiMeshToOfMesh(mAiMesh, tempMesh, !bConvertedToLeftHand, &getTexture() );
} else {
ofxAssimp::Utils::aiMeshToOfMesh(mAiMesh, tempMesh, !bConvertedToLeftHand, nullptr);
}
avbo->setVertexData(&mAiMesh->mVertices[0].x,3,mAiMesh->mNumVertices,usage,sizeof(aiVector3D));
if(mAiMesh->HasVertexColors(0)){
avbo->setColorData(&mAiMesh->mColors[0][0].r,mAiMesh->mNumVertices,GL_STATIC_DRAW,sizeof(aiColor4D));
}
if(mAiMesh->HasNormals()){
avbo->setNormalData(&mAiMesh->mNormals[0].x,mAiMesh->mNumVertices,usage,sizeof(aiVector3D));
}
if (tempMesh.hasTexCoords()){
avbo->setTexCoordData(&tempMesh.getTexCoords()[0].x, mAiMesh->mNumVertices,GL_STATIC_DRAW,sizeof(glm::vec2));
}
std::vector<ofIndexType> tempIndices;
tempIndices.resize(mAiMesh->mNumFaces * 3);
int j=0;
for (unsigned int x = 0; x < mAiMesh->mNumFaces; ++x){
for (unsigned int a = 0; a < mAiMesh->mFaces[x].mNumIndices; ++a){
tempIndices[j++] = mAiMesh->mFaces[x].mIndices[a];
}
}
avbo->setIndexData(&tempIndices[0],tempIndices.size(),GL_STATIC_DRAW);
}
//-------------------------------------------
void SrcMesh::setMeshFromAiMesh( ofMesh& amesh ) {
if( mAiMesh != NULL && amesh.getNumVertices() < 1 ) {
if( hasTexture() ) {
ofxAssimp::Utils::aiMeshToOfMesh(mAiMesh, amesh, !bConvertedToLeftHand, &getTexture());
} else {
ofxAssimp::Utils::aiMeshToOfMesh(mAiMesh, amesh, !bConvertedToLeftHand, nullptr);
}
}
}
//-------------------------------------------
void SrcMesh::calculateLocalBounds(ofMesh& amesh) {
mLocalBounds.clear();
mLocalBounds.include(amesh.getVertices());
}