| 811 | } |
| 812 | |
| 813 | void PbrtExporter::WriteMesh(aiMesh* mesh) { |
| 814 | mOutput << "# - Mesh: "; |
| 815 | const char* mName; |
| 816 | if (mesh->mName == aiString("")) |
| 817 | mName = "<No Name>"; |
| 818 | else |
| 819 | mName = mesh->mName.C_Str(); |
| 820 | mOutput << mName << "\n"; |
| 821 | |
| 822 | // Check if any types other than tri |
| 823 | if ( (mesh->mPrimitiveTypes & aiPrimitiveType_POINT) |
| 824 | || (mesh->mPrimitiveTypes & aiPrimitiveType_LINE) |
| 825 | || (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON)) { |
| 826 | std::cerr << "Error: ignoring point / line / polygon mesh " << mName << ".\n"; |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | mOutput << "AttributeBegin\n"; |
| 831 | aiMaterial* material = mScene->mMaterials[mesh->mMaterialIndex]; |
| 832 | mOutput << " NamedMaterial \"" << material->GetName().C_Str() << "\"\n"; |
| 833 | |
| 834 | // Handle area lights |
| 835 | aiColor3D emission; |
| 836 | if (material->Get(AI_MATKEY_COLOR_EMISSIVE, emission) == AI_SUCCESS && |
| 837 | (emission.r > 0 || emission.g > 0 || emission.b > 0)) |
| 838 | mOutput << " AreaLightSource \"diffuse\" \"rgb L\" [ " << emission.r << |
| 839 | " " << emission.g << " " << emission.b << " ]\n"; |
| 840 | |
| 841 | // Alpha mask |
| 842 | std::string alpha; |
| 843 | aiString opacityTexture; |
| 844 | if (material->Get(AI_MATKEY_TEXTURE_OPACITY(0), opacityTexture) == AI_SUCCESS || |
| 845 | material->Get(AI_MATKEY_TEXTURE_DIFFUSE(0), opacityTexture) == AI_SUCCESS) { |
| 846 | // material->Get(AI_MATKEY_TEXTURE_BASE_COLOR(0), opacityTexture) == AI_SUCCESS) |
| 847 | std::string texName = std::string("alpha:") + CleanTextureFilename(opacityTexture); |
| 848 | if (mTextureSet.find(texName) != mTextureSet.end()) |
| 849 | alpha = std::string(" \"texture alpha\" \"") + texName + "\"\n"; |
| 850 | } else { |
| 851 | float opacity = 1; |
| 852 | if (material->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS && opacity < 1) |
| 853 | alpha = std::string(" \"float alpha\" [ ") + std::to_string(opacity) + " ]\n"; |
| 854 | } |
| 855 | |
| 856 | // Output the shape specification |
| 857 | mOutput << "Shape \"trianglemesh\"\n" << |
| 858 | alpha << |
| 859 | " \"integer indices\" ["; |
| 860 | |
| 861 | // Start with faces (which hold indices) |
| 862 | for (unsigned int i = 0; i < mesh->mNumFaces; i++) { |
| 863 | auto face = mesh->mFaces[i]; |
| 864 | if (face.mNumIndices != 3) throw DeadlyExportError("oh no not a tri!"); |
| 865 | |
| 866 | for (unsigned int j = 0; j < face.mNumIndices; j++) { |
| 867 | mOutput << face.mIndices[j] << " "; |
| 868 | } |
| 869 | if ((i % 7) == 6) mOutput << "\n "; |
| 870 | } |