framework2
88 строк · 2.7 Кб
1//
2// ofxAssimpMeshHelper.cpp
3// Created by Lukasz Karluk on 4/12/12.
4//
5
6#include "ofxAssimpMeshHelper.h"
7#include "ofxAssimpUtils.h"
8
9using std::make_shared;
10using std::shared_ptr;
11
12void ofxAssimpMeshHelper::addTexture(ofxAssimpTexture & aAssimpTex){
13
14if( aAssimpTex.getTextureType() == aiTextureType_DIFFUSE ){
15bool bNeedsDiffuseAdd = false;
16if( !hasTexture(aiTextureType_DIFFUSE) ){
17bNeedsDiffuseAdd = true;
18}
19
20//we do this so we can have the diffuse texture in the meshTextures vector but also accessible via the old approach .assimpTexture;
21//wouldn't need to do this if we had protected vars :P
22assimpTexture = aAssimpTex;
23if( bNeedsDiffuseAdd ){
24meshTextures.push_back(shared_ptr<ofxAssimpTexture>(&assimpTexture,[](ofxAssimpTexture*){}));
25}
26}else{
27auto otherTex = make_shared<ofxAssimpTexture>();
28(*otherTex.get()) = aAssimpTex;
29
30meshTextures.push_back(otherTex);
31}
32
33auto ttype = meshTextures.back()->getTextureType();
34
35bool bAmbientOcclusion = false;
36bAmbientOcclusion = ttype == 17; //17 = aiTextureType_AMBIENT_OCCLUSION; //use this when we want to support newer assimp only
37
38if( ttype == aiTextureType_EMISSIVE ){
39material.setEmissiveTexture(meshTextures.back()->getTextureRef());
40}
41else if( ttype == aiTextureType_NORMALS ){
42material.setNormalTexture(meshTextures.back()->getTextureRef());
43}
44else if( ttype == aiTextureType_LIGHTMAP || bAmbientOcclusion ){
45material.setOcclusionTexture(meshTextures.back()->getTextureRef());
46}
47else if( ttype == aiTextureType_AMBIENT ){
48material.setAmbientTexture(meshTextures.back()->getTextureRef());
49}
50else if( ttype == aiTextureType_SPECULAR ){
51material.setSpecularTexture(meshTextures.back()->getTextureRef());
52} else if( ttype == 15 ) { //aiTextureType_METALNESS
53material.setMetallicTexture(meshTextures.back()->getTextureRef());
54} else if(ttype == 16 ) { //aiTextureType_DIFFUSE_ROUGHNESS
55material.setRoughnessTexture(meshTextures.back()->getTextureRef());
56} else if( ttype == aiTextureType_DISPLACEMENT ) {
57material.setDisplacementTexture(meshTextures.back()->getTextureRef());
58}
59}
60
61bool ofxAssimpMeshHelper::hasTexture(aiTextureType aTexType){
62if( aTexType == aiTextureType_DIFFUSE ){
63return assimpTexture.hasTexture();
64}else{
65for( auto & tex : meshTextures ){
66if( tex && tex->getTextureType() == aTexType){
67return tex->hasTexture();
68}
69}
70}
71return false;
72}
73
74
75ofTexture & ofxAssimpMeshHelper::getTextureRef(aiTextureType aTexType) {
76
77if( aTexType == aiTextureType_DIFFUSE ){
78return assimpTexture.getTextureRef();
79}else{
80for( auto & tex : meshTextures ){
81if( tex && tex->getTextureType() == aTexType){
82return tex->getTextureRef();
83}
84}
85}
86
87return assimpTexture.getTextureRef();
88}
89