Browse Source

Fix possible nullptr dereferencing.

Kim Kulling 7 years ago
parent
commit
6668eeb68e
2 changed files with 39 additions and 3 deletions
  1. 39 1
      code/D3MFExporter.cpp
  2. 0 2
      include/assimp/fast_atof.h

+ 39 - 1
code/D3MFExporter.cpp

@@ -49,8 +49,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <assimp/IOStream.hpp>
 #include <assimp/Exporter.hpp>
 #include <assimp/DefaultLogger.hpp>
-
+#include <assimp/StringUtils.h>
 #include <assimp/Exceptional.h>
+
 #include "3MFXmlTags.h"
 #include "D3MFOpcPackage.h"
 
@@ -204,8 +205,45 @@ void D3MFExporter::writeHeader() {
     mModelOutput << std::endl;
 }
 
+static std::string to_hex( int to_convert ) {
+    std::string result;
+    std::stringstream ss;
+    ss << std::hex << to_convert;
+    ss >> result;
+    return result;
+}
+
 void D3MFExporter::writeBaseMaterials() {
+    mModelOutput << "<basematerials id=\"1\">\n";
+    for ( size_t i = 0; i < mScene->mNumMaterials; ++i ) {
+        aiMaterial *mat = mScene->mMaterials[ i ];
+        std::string strName;
+        aiString name;
+        if ( mat->Get( AI_MATKEY_NAME, name ) != aiReturn_SUCCESS ) {
+            strName = "basemat_" + to_string( i );
+        } else {
+            strName = name.C_Str();
+        }
+        std::string hexDiffuseColor;
+        aiColor4D color;
+        if ( mat->Get( AI_MATKEY_COLOR_DIFFUSE, color ) == aiReturn_SUCCESS ) {
+            hexDiffuseColor = "#";
+            std::string tmp;
+            tmp = to_hex( color.r );
+            hexDiffuseColor += tmp;
+            tmp = to_hex( color.g );
+            hexDiffuseColor += tmp;
+            tmp = to_hex( color.b );
+            hexDiffuseColor += tmp;
+            tmp = to_hex( color.a );
+            hexDiffuseColor += tmp;
+        } else {
+            hexDiffuseColor = "#FFFFFFFF";
+        }
 
+        mModelOutput << "<base name=\""+strName+"\" "+" displaycolor=\""+hexDiffuseColor+"\">\n";
+    }
+    mModelOutput << "</basematerials>\n";
 }
 
 void D3MFExporter::writeObjects() {

+ 0 - 2
include/assimp/fast_atof.h

@@ -146,7 +146,6 @@ uint8_t HexOctetToDecimal(const char* in) {
     return ((uint8_t)HexDigitToDecimal(in[0])<<4)+(uint8_t)HexDigitToDecimal(in[1]);
 }
 
-
 // ------------------------------------------------------------------------------------
 // signed variant of strtoul10
 // ------------------------------------------------------------------------------------
@@ -353,7 +352,6 @@ ai_real fast_atof(const char* c) {
     return ret;
 }
 
-
 inline
 ai_real fast_atof( const char* c, const char** cout) {
     ai_real ret(0.0);