Advertisement
Guest User

Untitled

a guest
Jan 1st, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 51.38 KB | None | 0 0
  1. import java.nio.ByteBuffer;
  2. import java.util.ArrayList;
  3. import java.util.BitSet;
  4. import java.util.EnumSet;
  5.  
  6. import org.lwjgl.PointerBuffer;
  7.  
  8. import static org.lwjgl.system.MemoryUtil.memUTF8;
  9. import static org.lwjgl.vulkan.VK10.VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
  10. import static org.lwjgl.vulkan.VK10.VK_DESCRIPTOR_TYPE_SAMPLER;
  11. import static org.lwjgl.vulkan.VK10.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
  12. import static org.lwjgl.vulkan.VK10.VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
  13. import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_FRAGMENT_BIT;
  14. import static org.lwjgl.system.MemoryUtil.memAllocPointer;
  15. import static org.lwjgl.system.MemoryUtil.memFree;
  16. import static org.lwjgl.system.MemoryUtil.memAddress;
  17. import static org.lwjgl.system.MemoryUtil.memAlloc;
  18.  
  19. import org.lwjgl.assimp.AIColor4D;
  20. import org.lwjgl.assimp.AIMaterial;
  21. import org.lwjgl.assimp.AIMaterialProperty;
  22. import org.lwjgl.assimp.AIString;
  23. import org.lwjgl.assimp.AIUVTransform;
  24. import org.lwjgl.assimp.AIVector3D;
  25. import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding;
  26.  
  27. import static org.lwjgl.assimp.Assimp.AI_MATKEY_NAME;
  28. import static org.lwjgl.assimp.Assimp.AI_MATKEY_COLOR_DIFFUSE;
  29. import static org.lwjgl.assimp.Assimp.AI_MATKEY_COLOR_SPECULAR;
  30. import static org.lwjgl.assimp.Assimp.AI_MATKEY_COLOR_AMBIENT;
  31. import static org.lwjgl.assimp.Assimp.AI_MATKEY_COLOR_EMISSIVE;
  32. import static org.lwjgl.assimp.Assimp.AI_MATKEY_COLOR_TRANSPARENT;
  33. import static org.lwjgl.assimp.Assimp.AI_MATKEY_COLOR_REFLECTIVE;
  34. import static org.lwjgl.assimp.Assimp.AI_MATKEY_ENABLE_WIREFRAME;
  35. import static org.lwjgl.assimp.Assimp.AI_MATKEY_SHADING_MODEL;
  36. import static org.lwjgl.assimp.Assimp.AI_MATKEY_BLEND_FUNC;
  37. import static org.lwjgl.assimp.Assimp.AI_MATKEY_TWOSIDED;
  38. import static org.lwjgl.assimp.Assimp.AI_MATKEY_OPACITY;
  39. import static org.lwjgl.assimp.Assimp.AI_MATKEY_REFLECTIVITY;
  40. import static org.lwjgl.assimp.Assimp.AI_MATKEY_SHININESS;
  41. import static org.lwjgl.assimp.Assimp.AI_MATKEY_SHININESS_STRENGTH;
  42. import static org.lwjgl.assimp.Assimp.AI_MATKEY_REFRACTI;
  43. import static org.lwjgl.assimp.Assimp.AI_MATKEY_BUMPSCALING;
  44. import static org.lwjgl.assimp.Assimp.AI_MATKEY_GLOBAL_BACKGROUND_IMAGE;
  45. import static org.lwjgl.assimp.Assimp._AI_MATKEY_TEXTURE_BASE;
  46. import static org.lwjgl.assimp.Assimp._AI_MATKEY_MAPPING_BASE;
  47. import static org.lwjgl.assimp.Assimp._AI_MATKEY_MAPPINGMODE_U_BASE;
  48. import static org.lwjgl.assimp.Assimp._AI_MATKEY_MAPPINGMODE_V_BASE;
  49. import static org.lwjgl.assimp.Assimp._AI_MATKEY_TEXBLEND_BASE;
  50. import static org.lwjgl.assimp.Assimp._AI_MATKEY_TEXOP_BASE;
  51. import static org.lwjgl.assimp.Assimp._AI_MATKEY_TEXFLAGS_BASE;
  52. import static org.lwjgl.assimp.Assimp._AI_MATKEY_TEXMAP_AXIS_BASE;
  53. import static org.lwjgl.assimp.Assimp._AI_MATKEY_UVTRANSFORM_BASE;
  54. import static org.lwjgl.assimp.Assimp._AI_MATKEY_UVWSRC_BASE;
  55. import static org.lwjgl.assimp.Assimp.aiTextureType_NONE;
  56. import static org.lwjgl.assimp.Assimp.aiTextureType_DIFFUSE;
  57. import static org.lwjgl.assimp.Assimp.aiTextureType_SPECULAR;
  58. import static org.lwjgl.assimp.Assimp.aiTextureType_AMBIENT;
  59. import static org.lwjgl.assimp.Assimp.aiTextureType_EMISSIVE;
  60. import static org.lwjgl.assimp.Assimp.aiTextureType_HEIGHT;
  61. import static org.lwjgl.assimp.Assimp.aiTextureType_NORMALS;
  62. import static org.lwjgl.assimp.Assimp.aiTextureType_OPACITY;
  63. import static org.lwjgl.assimp.Assimp.aiTextureType_SHININESS;
  64. import static org.lwjgl.assimp.Assimp.aiTextureType_DISPLACEMENT;
  65. import static org.lwjgl.assimp.Assimp.aiTextureType_LIGHTMAP;
  66. import static org.lwjgl.assimp.Assimp.aiTextureType_REFLECTION;
  67. import static org.lwjgl.assimp.Assimp.aiShadingMode_Flat;
  68. import static org.lwjgl.assimp.Assimp.aiShadingMode_Gouraud;
  69. import static org.lwjgl.assimp.Assimp.aiShadingMode_Phong;
  70. import static org.lwjgl.assimp.Assimp.aiShadingMode_Blinn;
  71. import static org.lwjgl.assimp.Assimp.aiShadingMode_Toon;
  72. import static org.lwjgl.assimp.Assimp.aiShadingMode_OrenNayar;
  73. import static org.lwjgl.assimp.Assimp.aiShadingMode_Minnaert;
  74. import static org.lwjgl.assimp.Assimp.aiShadingMode_CookTorrance;
  75. import static org.lwjgl.assimp.Assimp.aiShadingMode_NoShading;
  76. import static org.lwjgl.assimp.Assimp.aiShadingMode_Fresnel;
  77. import static org.lwjgl.assimp.Assimp.aiBlendMode_Default;
  78. import static org.lwjgl.assimp.Assimp.aiBlendMode_Additive;
  79. import static org.lwjgl.assimp.Assimp.aiTextureOp_Multiply;
  80. import static org.lwjgl.assimp.Assimp.aiTextureOp_Add;
  81. import static org.lwjgl.assimp.Assimp.aiTextureOp_Subtract;
  82. import static org.lwjgl.assimp.Assimp.aiTextureOp_Divide;
  83. import static org.lwjgl.assimp.Assimp.aiTextureOp_SmoothAdd;
  84. import static org.lwjgl.assimp.Assimp.aiTextureOp_SignedAdd;
  85. import static org.lwjgl.assimp.Assimp.aiTextureMapping_UV;
  86. import static org.lwjgl.assimp.Assimp.aiTextureMapping_SPHERE;
  87. import static org.lwjgl.assimp.Assimp.aiTextureMapping_CYLINDER;
  88. import static org.lwjgl.assimp.Assimp.aiTextureMapping_BOX;
  89. import static org.lwjgl.assimp.Assimp.aiTextureMapping_PLANE;
  90. import static org.lwjgl.assimp.Assimp.aiTextureMapping_OTHER;
  91. import static org.lwjgl.assimp.Assimp.aiTextureMapMode_Wrap;
  92. import static org.lwjgl.assimp.Assimp.aiTextureMapMode_Clamp;
  93. import static org.lwjgl.assimp.Assimp.aiTextureMapMode_Decal;
  94. import static org.lwjgl.assimp.Assimp.aiTextureMapMode_Mirror;
  95. import static org.lwjgl.assimp.Assimp.aiTextureFlags_Invert;
  96. import static org.lwjgl.assimp.Assimp.aiTextureFlags_UseAlpha;
  97. import static org.lwjgl.assimp.Assimp.aiTextureFlags_IgnoreAlpha;
  98. import static org.lwjgl.assimp.Assimp.aiGetMaterialString;
  99. import static org.lwjgl.assimp.Assimp.aiGetMaterialColor;
  100. import static org.lwjgl.assimp.Assimp.aiGetMaterialIntegerArray;
  101. import static org.lwjgl.assimp.Assimp.aiGetMaterialFloatArray;
  102. import static org.lwjgl.assimp.Assimp.aiGetMaterialUVTransform;
  103.  
  104. public class Material {
  105.    
  106.     public static enum TextureType {NONE, DIFFUSE, SPECULAR, AMBIENT, EMISSIVE, HEIGHT, NORMALS, SHININESS, OPACITY, DISPLACEMENT, LIGHTMAP, REFLECTION, UNKNOWN};
  107.     public static enum ShadingMode {FLAT, GOURAUD, PHONG, BLINN, TOON, OREN_NAYAR, MINNAERT, COOK_TORRANCE, NO_SHADING, FRESNEL};
  108.     public static enum BlendFunc {DEFAULT, ADDITIVE};
  109.     public static enum TextureOp {MULTIPLY, ADD, SUBTRACT, DIVIDE, SMOOTH_ADD, SIGNED_ADD};
  110.     public static enum TextureMapping {UV, SPHERE, CYLINDER, BOX, PLANE, OTHER};
  111.     public static enum TextureMappingMode {WRAP, CLAMP, DECAL, MIRROR};
  112.     public static enum TextureFlag {INVERT, USE_ALPHA, IGNORE_ALPHA};
  113.  
  114.     public final MaterialProperty[] materialProperties;
  115.    
  116.     public final String name;
  117.  
  118.     public final double[] diffuseColor;//default is black; Is allready multiplied with export diffuse factor
  119.     public final double[] specularColor;//default is black
  120.     public final double[] ambientColor;//default is black
  121.     public final double[] emissiveColor;//default is black
  122.     public final double[] transparentColor;//default is black
  123.     public final double[] reflectiveColor;//default is black
  124.    
  125.     public final boolean wireframe;//default is false
  126.     public final boolean twosided;//default is false
  127.    
  128.     public final ShadingMode shadingModel;//Default is Phong
  129.     public final BlendFunc blendFunc;//Default is Default
  130.    
  131.     public final float opacity;//When not 1 => not cull faces; default is 1.0
  132.     public float shininess;//0 means shadingMode Gouraud; Default is 0.0
  133.     public final float shininessStrength;//Default is 1.0;scales specular color
  134.    
  135.     public final float refractionIndex;//Interesting for Raytracing; default is 1.0
  136.    
  137.     //TODO:Reflectivity, bumpscaling, etc.
  138.     public final float bumpScaling;
  139.     public final float reflectivity;//Needed in shader. than higher, than more incoming light is reflected.
  140.     public final boolean globalBackgroundImage;//Not yet used
  141.    
  142.     //TODO: Normalmap, heightmap, shininessMap, dosplacemep, lightmap, reflectionmap
  143.     public final TextureStack diffuseTextureStack;
  144.     public final TextureStack specularTextureStack;
  145.     public final TextureStack ambientTextureStack;
  146.     public final TextureStack emissiveTextureStack;
  147.     public final TextureStack heightTextureStack;
  148.     public final TextureStack normalsTextureStack;
  149.     public final TextureStack shininessTextureStack;
  150.     public final TextureStack opacityTextureStack;
  151.     public final TextureStack displacementTextureStack;
  152.     public final TextureStack lightmapTextureStack;
  153.     public final TextureStack reflectionTextureStack;
  154.    
  155.     public final BitSet activeTextures;
  156.     public final BitSet activeColors;
  157.    
  158.     public static final VkDescriptorSetLayoutBinding.Buffer advancedLayoutBindings = VkDescriptorSetLayoutBinding.calloc(2);
  159.     static {
  160.                 //Image binding
  161.                 advancedLayoutBindings.get(0).binding(0);
  162.                 advancedLayoutBindings.get(0).descriptorType(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
  163.                 advancedLayoutBindings.get(0).descriptorCount(1);
  164.                 advancedLayoutBindings.get(0).pImmutableSamplers(null);
  165.                 advancedLayoutBindings.get(0).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  166.                
  167.                 //Sampler binding
  168.                 advancedLayoutBindings.get(1).binding(1);
  169.                 advancedLayoutBindings.get(1).descriptorType(VK_DESCRIPTOR_TYPE_SAMPLER);
  170.                 advancedLayoutBindings.get(1).descriptorCount(1);
  171.                 advancedLayoutBindings.get(1).pImmutableSamplers(null);
  172.                 advancedLayoutBindings.get(1).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  173.     }
  174.    
  175.     public static final VkDescriptorSetLayoutBinding.Buffer layoutBindings = VkDescriptorSetLayoutBinding.calloc(3);
  176.     static {
  177.                 //Image binding
  178.                 layoutBindings.get(0).binding(0);
  179.                 layoutBindings.get(0).descriptorType(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
  180.                 layoutBindings.get(0).descriptorCount(1);
  181.                 layoutBindings.get(0).pImmutableSamplers(null);
  182.                 layoutBindings.get(0).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  183.                
  184.                 //Sampler binding
  185.                 layoutBindings.get(1).binding(1);
  186.                 layoutBindings.get(1).descriptorType(VK_DESCRIPTOR_TYPE_SAMPLER);
  187.                 layoutBindings.get(1).descriptorCount(1);
  188.                 layoutBindings.get(1).pImmutableSamplers(null);
  189.                 layoutBindings.get(1).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  190.                
  191.                 //Dst-Image binding
  192.                 layoutBindings.get(2).binding(2);
  193.                 layoutBindings.get(2).descriptorType(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
  194.                 layoutBindings.get(2).descriptorCount(1);
  195.                 layoutBindings.get(2).pImmutableSamplers(null);
  196.                 layoutBindings.get(2).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  197.                
  198.     }
  199.    
  200.     //TODO: gltf
  201.    
  202.     public Material(AIMaterial material) {
  203.         this.materialProperties = new MaterialProperty[material.mNumProperties()];
  204.         PointerBuffer aiMaterialProperties = material.mProperties();
  205.         for(int i=0;i<material.mNumProperties();++i) {
  206.             this.materialProperties[i] = new MaterialProperty(AIMaterialProperty.create(aiMaterialProperties.get(i)));
  207.         }
  208.        
  209.         //Get Data
  210.         AIString aiName = AIString.calloc();
  211.         int found = aiGetMaterialString(material, AI_MATKEY_NAME, aiTextureType_NONE, 0,  aiName);
  212.         if(found == 0) {
  213.             this.name = memUTF8(aiName.data());
  214.         }else {
  215.             this.name = null;
  216.         }
  217.         aiName.free();
  218.        
  219.         this.activeColors = new BitSet(6);
  220.        
  221.         AIColor4D aiColor = AIColor4D.calloc();
  222.         found = aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, aiTextureType_NONE, 0, aiColor);
  223.         if(found == 0) {
  224.             this.diffuseColor = new double[] {aiColor.r(), aiColor.g(), aiColor.b(), aiColor.a()};
  225.             this.activeColors.flip(0);
  226.         }else {
  227.             this.diffuseColor = null;
  228.         }
  229.         found = aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, aiTextureType_NONE, 0, aiColor);
  230.         if(found == 0) {
  231.             this.specularColor = new double[] {aiColor.r(), aiColor.g(), aiColor.b(), aiColor.a()};
  232.             this.activeColors.flip(1);
  233.         }else {
  234.             this.specularColor = null;
  235.         }
  236.         found = aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, aiTextureType_NONE, 0, aiColor);
  237.         if(found == 0) {
  238.             this.ambientColor = new double[] {aiColor.r(), aiColor.g(), aiColor.b(), aiColor.a()};
  239.             this.activeColors.flip(2);
  240.         }else {
  241.             this.ambientColor = null;
  242.         }
  243.         found = aiGetMaterialColor(material, AI_MATKEY_COLOR_EMISSIVE, aiTextureType_NONE, 0, aiColor);
  244.         if(found == 0) {
  245.             this.emissiveColor = new double[] {aiColor.r(), aiColor.g(), aiColor.b(), aiColor.a()};
  246.             this.activeColors.flip(3);
  247.         }else {
  248.             this.emissiveColor = null;
  249.         }
  250.         found = aiGetMaterialColor(material, AI_MATKEY_COLOR_TRANSPARENT, aiTextureType_NONE, 0, aiColor);
  251.         if(found == 0) {
  252.             this.transparentColor = new double[] {aiColor.r(), aiColor.g(), aiColor.b(), aiColor.a()};
  253.             this.activeColors.flip(4);
  254.         }else {
  255.             this.transparentColor = null;
  256.         }
  257.         found = aiGetMaterialColor(material, AI_MATKEY_COLOR_REFLECTIVE, aiTextureType_NONE, 0, aiColor);
  258.         if(found == 0) {
  259.             this.reflectiveColor = new double[] {aiColor.r(), aiColor.g(), aiColor.b(), aiColor.a()};
  260.             this.activeColors.flip(5);
  261.         }else {
  262.             this.reflectiveColor = null;
  263.         }
  264.         aiColor.free();
  265.        
  266.         int[] aiInteger = new int[1];
  267.         found = aiGetMaterialIntegerArray(material, AI_MATKEY_ENABLE_WIREFRAME, aiTextureType_NONE, 0, aiInteger, new int[] {1});
  268.         if(found == 0) {
  269.             this.wireframe = (aiInteger[0] != 0);
  270.         }else {
  271.             this.wireframe = false;
  272.         }
  273.         found = aiGetMaterialIntegerArray(material, AI_MATKEY_SHADING_MODEL, aiTextureType_NONE, 0, aiInteger, new int[] {1});
  274.         this.shadingModel = ShadingMode.PHONG;
  275.         /*
  276.         if(found == 0) {
  277.             switch(aiInteger[0]) {
  278.                 case aiShadingMode_Flat:
  279.                     this.shadingModel = ShadingMode.FLAT;
  280.                     break;
  281.                 case aiShadingMode_Gouraud:
  282.                     this.shadingModel = ShadingMode.GOURAUD;
  283.                     break;
  284.                 case aiShadingMode_Phong:
  285.                     this.shadingModel = ShadingMode.PHONG;
  286.                     break;
  287.                 case aiShadingMode_Blinn:
  288.                     this.shadingModel = ShadingMode.BLINN;
  289.                     break;
  290.                 case aiShadingMode_Toon:
  291.                     this.shadingModel = ShadingMode.TOON;
  292.                     break;
  293.                 case aiShadingMode_OrenNayar:
  294.                     this.shadingModel = ShadingMode.OREN_NAYAR;
  295.                     break;
  296.                 case aiShadingMode_Minnaert:
  297.                     this.shadingModel = ShadingMode.MINNAERT;
  298.                     break;
  299.                 case aiShadingMode_CookTorrance:
  300.                     this.shadingModel = ShadingMode.COOK_TORRANCE;
  301.                     break;
  302.                 case aiShadingMode_NoShading:
  303.                     this.shadingModel = ShadingMode.NO_SHADING;
  304.                     break;
  305.                 case aiShadingMode_Fresnel:
  306.                     this.shadingModel = ShadingMode.FRESNEL;
  307.                     break;
  308.                 default:
  309.                     throw new AssertionError("Unspecified type!");
  310.             }
  311.         }else {
  312.             this.shadingModel = ShadingMode.PHONG;
  313.         }
  314.         */
  315.         found = aiGetMaterialIntegerArray(material, AI_MATKEY_BLEND_FUNC, aiTextureType_NONE, 0, aiInteger, new int[] {1});
  316.         if(found == 0) {
  317.             switch(aiInteger[0]) {
  318.                 case aiBlendMode_Default:
  319.                     this.blendFunc = BlendFunc.DEFAULT;
  320.                     break;
  321.                 case aiBlendMode_Additive:
  322.                     this.blendFunc = BlendFunc.ADDITIVE;
  323.                     break;
  324.                 default:
  325.                     throw new AssertionError("Unspecified type!");
  326.             }
  327.         }else {
  328.             this.blendFunc = BlendFunc.DEFAULT;
  329.         }
  330.         found = aiGetMaterialIntegerArray(material, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE, aiTextureType_NONE, 0, aiInteger, new int[] {1});
  331.         if(found == 0) {
  332.             this.globalBackgroundImage = (aiInteger[0] != 0);//TODO: What is backgroundimage; Probably no index but boolean?
  333.         }else {
  334.             this.globalBackgroundImage = false;
  335.         }
  336.        
  337.         float[] aiFloat = new float[1];
  338.         found = aiGetMaterialFloatArray(material, AI_MATKEY_OPACITY, aiTextureType_NONE, 0, aiFloat, new int[] {1});
  339.         if(found == 0) {
  340.             this.opacity = aiFloat[0];
  341.         }else {
  342.             this.opacity = 1.0f;
  343.         }
  344.         found = aiGetMaterialIntegerArray(material, AI_MATKEY_TWOSIDED, aiTextureType_NONE, 0, aiInteger, new int[] {1});
  345.         if(found == 0) {
  346.             //this.twosided = (aiInteger[0] != 0);
  347.             //FIXME:
  348.             this.twosided = true;//Set for Debug reason
  349.         }else {
  350.             this.twosided = true;//Should depend on global all opacity values.
  351.         }
  352.         found = aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS, aiTextureType_NONE, 0, aiFloat, new int[] {1});
  353.         if(found == 0) {
  354.             this.shininess = aiFloat[0];
  355.         }else {
  356.             this.shininess = 0.0f;
  357.         }
  358.         found = aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS_STRENGTH, aiTextureType_NONE, 0, aiFloat, new int[] {1});
  359.         if(found == 0) {
  360.             this.shininessStrength = aiFloat[0];
  361.         }else {
  362.             this.shininessStrength = 1.0f;
  363.         }
  364.         found = aiGetMaterialFloatArray(material, AI_MATKEY_REFRACTI, aiTextureType_NONE, 0, aiFloat, new int[] {1});
  365.         if(found == 0) {
  366.             this.refractionIndex = aiFloat[0];
  367.         }else {
  368.             this.refractionIndex = 1.0f;
  369.         }
  370.         found = aiGetMaterialFloatArray(material, AI_MATKEY_BUMPSCALING, aiTextureType_NONE, 0, aiFloat, new int[] {1});
  371.         if(found == 0) {
  372.             this.bumpScaling = aiFloat[0];
  373.         }else {
  374.             this.bumpScaling = 1.0f;
  375.         }
  376.         found = aiGetMaterialFloatArray(material, AI_MATKEY_REFLECTIVITY, aiTextureType_NONE, 0, aiFloat, new int[] {1});
  377.         if(found == 0) {
  378.             this.reflectivity = aiFloat[0];
  379.         }else {
  380.             this.reflectivity = 0.0f;
  381.         }
  382.  
  383.         ArrayList<TextureStackItem> diffuseTextureStackItems = new ArrayList<TextureStackItem>();
  384.         ArrayList<TextureStackItem> specularTextureStackItems = new ArrayList<TextureStackItem>();
  385.         ArrayList<TextureStackItem> ambientTextureStackItems = new ArrayList<TextureStackItem>();
  386.         ArrayList<TextureStackItem> emissiveTextureStackItems = new ArrayList<TextureStackItem>();
  387.         ArrayList<TextureStackItem> heightTextureStackItems = new ArrayList<TextureStackItem>();
  388.         ArrayList<TextureStackItem> normalsTextureStackItems = new ArrayList<TextureStackItem>();
  389.         ArrayList<TextureStackItem> shininessTextureStackItems = new ArrayList<TextureStackItem>();
  390.         ArrayList<TextureStackItem> opacityTextureStackItems = new ArrayList<TextureStackItem>();
  391.         ArrayList<TextureStackItem> displacementTextureStackItems = new ArrayList<TextureStackItem>();
  392.         ArrayList<TextureStackItem> lightmapTextureStackItems = new ArrayList<TextureStackItem>();
  393.         ArrayList<TextureStackItem> reflectionTextureStackItems = new ArrayList<TextureStackItem>();
  394.         for(int i=0;i<materialProperties.length;++i) {
  395.             switch(materialProperties[i].semantic) {
  396.                 case NONE:
  397.                     break;
  398.                 case DIFFUSE:
  399.                     handleProperty(diffuseTextureStackItems, aiTextureType_DIFFUSE, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  400.                     break;
  401.                 case SPECULAR:
  402.                     handleProperty(specularTextureStackItems, aiTextureType_SPECULAR, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  403.                     break;
  404.                 case AMBIENT:
  405.                     handleProperty(ambientTextureStackItems, aiTextureType_AMBIENT, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  406.                     break;
  407.                 case EMISSIVE:
  408.                     handleProperty(emissiveTextureStackItems, aiTextureType_EMISSIVE, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  409.                     break;
  410.                 case HEIGHT:
  411.                     handleProperty(heightTextureStackItems, aiTextureType_HEIGHT, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  412.                     break;
  413.                 case NORMALS:
  414.                     handleProperty(normalsTextureStackItems, aiTextureType_NORMALS, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  415.                     break;
  416.                 case SHININESS:
  417.                     handleProperty(shininessTextureStackItems, aiTextureType_SHININESS, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  418.                     break;
  419.                 case OPACITY:
  420.                     handleProperty(opacityTextureStackItems, aiTextureType_OPACITY, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  421.                     break;
  422.                 case DISPLACEMENT:
  423.                     handleProperty(displacementTextureStackItems, aiTextureType_DISPLACEMENT, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  424.                     break;
  425.                 case LIGHTMAP:
  426.                     handleProperty(lightmapTextureStackItems, aiTextureType_LIGHTMAP, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  427.                     break;
  428.                 case REFLECTION:
  429.                     handleProperty(reflectionTextureStackItems, aiTextureType_REFLECTION, materialProperties[i].key, materialProperties[i].index, material, materialProperties[i].data);
  430.                     break;
  431.                 case UNKNOWN:
  432.                     break;
  433.                 default:
  434.                     throw new AssertionError("Unspecified type!");
  435.             }
  436.         }
  437.         this.diffuseTextureStack = new TextureStack(diffuseTextureStackItems.toArray(new TextureStackItem[0]), TextureType.DIFFUSE, this);
  438.         this.specularTextureStack = new TextureStack(specularTextureStackItems.toArray(new TextureStackItem[0]), TextureType.SPECULAR, this);
  439.         this.ambientTextureStack = new TextureStack(ambientTextureStackItems.toArray(new TextureStackItem[0]), TextureType.AMBIENT, this);
  440.         this.emissiveTextureStack = new TextureStack(emissiveTextureStackItems.toArray(new TextureStackItem[0]), TextureType.EMISSIVE, this);
  441.         this.heightTextureStack = new TextureStack(heightTextureStackItems.toArray(new TextureStackItem[0]), TextureType.HEIGHT, this);
  442.         this.normalsTextureStack = new TextureStack(normalsTextureStackItems.toArray(new TextureStackItem[0]), TextureType.NORMALS, this);
  443.         this.shininessTextureStack = new TextureStack(shininessTextureStackItems.toArray(new TextureStackItem[0]), TextureType.SHININESS, this);
  444.         this.opacityTextureStack = new TextureStack(opacityTextureStackItems.toArray(new TextureStackItem[0]), TextureType.OPACITY, this);
  445.         this.displacementTextureStack = new TextureStack(displacementTextureStackItems.toArray(new TextureStackItem[0]), TextureType.DISPLACEMENT, this);
  446.         this.lightmapTextureStack = new TextureStack(lightmapTextureStackItems.toArray(new TextureStackItem[0]), TextureType.LIGHTMAP, this);
  447.         this.reflectionTextureStack = new TextureStack(reflectionTextureStackItems.toArray(new TextureStackItem[0]), TextureType.REFLECTION, this);
  448.        
  449.         this.activeTextures = new BitSet(11);
  450.         if(diffuseTextureStackItems.size() > 0) {
  451.             this.activeTextures.flip(0);
  452.         }
  453.         if(specularTextureStackItems.size() > 0) {
  454.             this.activeTextures.flip(1);
  455.         }
  456.         if(ambientTextureStackItems.size() > 0) {
  457.             this.activeTextures.flip(2);
  458.         }
  459.         if(emissiveTextureStackItems.size() > 0) {
  460.             this.activeTextures.flip(3);
  461.         }
  462.         if(heightTextureStackItems.size() > 0) {
  463.             this.activeTextures.flip(4);
  464.         }
  465.         if(normalsTextureStackItems.size() > 0) {
  466.             this.activeTextures.flip(5);
  467.         }
  468.         if(shininessTextureStackItems.size() > 0) {
  469.             this.activeTextures.flip(6);
  470.         }
  471.         if(opacityTextureStackItems.size() > 0) {
  472.             this.activeTextures.flip(7);
  473.         }
  474.         if(displacementTextureStackItems.size() > 0) {
  475.             this.activeTextures.flip(8);
  476.         }
  477.         if(lightmapTextureStackItems.size() > 0) {
  478.             this.activeTextures.flip(9);
  479.         }
  480.         if(reflectionTextureStackItems.size() > 0) {
  481.             this.activeTextures.flip(10);
  482.         }
  483.        
  484.         if(!activeTextures.get(1) && specularColor != null) {//TODO:Premultiply specularColor with it's strength, when no texture is applied. Otherwise blend texture
  485.             shininess *= shininessStrength;
  486.         }
  487.        
  488.         this.printOut();
  489.     }
  490.    
  491.     public static void handleProperty(ArrayList<TextureStackItem> currentList, int textureType, String materialKey, int textureIndex, AIMaterial material, ByteBuffer data) {
  492.         if(textureIndex > currentList.size()-1) {
  493.             for(int j=currentList.size();j<textureIndex;++j) {
  494.                 currentList.add(null);
  495.             }
  496.             currentList.add(new TextureStackItem());
  497.         }else if(currentList.get(textureIndex) == null) {
  498.             currentList.set(textureIndex, new TextureStackItem());
  499.         }
  500.         switch(materialKey) {
  501.             case _AI_MATKEY_TEXTURE_BASE:
  502.                 data.getInt();
  503.                 String path = memUTF8(data);
  504.                 path = path.substring(0, path.length()-1);
  505.                 //System.out.println(path);
  506.                 data.rewind();
  507.                
  508.                 currentList.get(textureIndex).texturePath = path;//TODO: When embeeded special path. Preload all Textures in Texelbuffers.
  509.                 break;
  510.             case _AI_MATKEY_TEXBLEND_BASE:
  511.                 float[] aiFloat = new float[1];
  512.                 int found = aiGetMaterialFloatArray(material, _AI_MATKEY_TEXBLEND_BASE, textureType, textureIndex, aiFloat, new int[] {1});
  513.                 if(found == 0) {
  514.                     currentList.get(textureIndex).textureStrength = aiFloat[0];
  515.                 }else {
  516.                     currentList.get(textureIndex).textureStrength = 1.0f;
  517.                 }
  518.                 break;
  519.             case _AI_MATKEY_TEXOP_BASE:
  520.                 int[] aiInteger = new int[1];
  521.                 found = aiGetMaterialIntegerArray(material, _AI_MATKEY_TEXOP_BASE, textureType, textureIndex, aiInteger, new int[] {1});
  522.                 if(found == 0) {
  523.                     switch(aiInteger[0]) {
  524.                         case aiTextureOp_Multiply:
  525.                             currentList.get(textureIndex).textureOp = TextureOp.MULTIPLY;
  526.                             break;
  527.                         case aiTextureOp_Add:
  528.                             currentList.get(textureIndex).textureOp = TextureOp.ADD;
  529.                             break;
  530.                         case aiTextureOp_Subtract:
  531.                             currentList.get(textureIndex).textureOp = TextureOp.SUBTRACT;
  532.                             break;
  533.                         case aiTextureOp_Divide:
  534.                             currentList.get(textureIndex).textureOp = TextureOp.DIVIDE;
  535.                             break;
  536.                         case aiTextureOp_SmoothAdd:
  537.                             currentList.get(textureIndex).textureOp = TextureOp.SMOOTH_ADD;
  538.                             break;
  539.                         case aiTextureOp_SignedAdd:
  540.                             currentList.get(textureIndex).textureOp = TextureOp.SIGNED_ADD;
  541.                             break;
  542.                         default:
  543.                             throw new AssertionError("Unknown operation!");
  544.                     }
  545.                 }else {
  546.                     currentList.get(textureIndex).textureOp = TextureOp.MULTIPLY;
  547.                 }
  548.                 break;
  549.             case _AI_MATKEY_MAPPING_BASE:
  550.                 aiInteger = new int[1];
  551.                 found = aiGetMaterialIntegerArray(material, _AI_MATKEY_MAPPING_BASE, textureType, textureIndex, aiInteger, new int[] {1});
  552.                 if(found == 0) {
  553.                     switch(aiInteger[0]) {
  554.                         case aiTextureMapping_UV:
  555.                             currentList.get(textureIndex).textureMapping = TextureMapping.UV;
  556.                             break;
  557.                         case aiTextureMapping_SPHERE:
  558.                             currentList.get(textureIndex).textureMapping = TextureMapping.SPHERE;
  559.                             break;
  560.                         case aiTextureMapping_CYLINDER:
  561.                             currentList.get(textureIndex).textureMapping = TextureMapping.CYLINDER;
  562.                             break;
  563.                         case aiTextureMapping_BOX:
  564.                             currentList.get(textureIndex).textureMapping = TextureMapping.BOX;
  565.                             break;
  566.                         case aiTextureMapping_PLANE:
  567.                             currentList.get(textureIndex).textureMapping = TextureMapping.PLANE;
  568.                             break;
  569.                         case aiTextureMapping_OTHER:
  570.                             currentList.get(textureIndex).textureMapping = TextureMapping.OTHER;
  571.                             break;
  572.                         default:
  573.                             throw new AssertionError("Unknown mapping!");
  574.                     }
  575.                 }else {
  576.                     currentList.get(textureIndex).textureMapping = TextureMapping.UV;
  577.                 }
  578.                 break;
  579.             case _AI_MATKEY_UVWSRC_BASE:
  580.                 aiInteger = new int[1];
  581.                 found = aiGetMaterialIntegerArray(material, _AI_MATKEY_UVWSRC_BASE, textureType, textureIndex, aiInteger, new int[] {1});
  582.                 if(found == 0) {
  583.                     currentList.get(textureIndex).textureUVChannel = aiInteger[0];
  584.                 }else {
  585.                     currentList.get(textureIndex).textureUVChannel = 0;
  586.                 }
  587.                 break;
  588.             case _AI_MATKEY_MAPPINGMODE_U_BASE:
  589.                 aiInteger = new int[1];
  590.                 found = aiGetMaterialIntegerArray(material, _AI_MATKEY_MAPPINGMODE_U_BASE, textureType, textureIndex, aiInteger, new int[] {1});
  591.                 if(found == 0) {
  592.                     switch(aiInteger[0]) {
  593.                         case aiTextureMapMode_Wrap:
  594.                             currentList.get(textureIndex).textureMappingModeU = TextureMappingMode.WRAP;
  595.                             break;
  596.                         case aiTextureMapMode_Clamp:
  597.                             currentList.get(textureIndex).textureMappingModeU = TextureMappingMode.CLAMP;
  598.                             break;
  599.                         case aiTextureMapMode_Decal:
  600.                             currentList.get(textureIndex).textureMappingModeU = TextureMappingMode.DECAL;
  601.                             break;
  602.                         case aiTextureMapMode_Mirror:
  603.                             currentList.get(textureIndex).textureMappingModeU = TextureMappingMode.MIRROR;
  604.                             break;
  605.                         default:
  606.                             throw new AssertionError("Unknown mappingmode!");
  607.                     }
  608.                 }else {
  609.                     currentList.get(textureIndex).textureMappingModeU = TextureMappingMode.WRAP;
  610.                 }
  611.                 break;
  612.             case _AI_MATKEY_MAPPINGMODE_V_BASE:
  613.                 aiInteger = new int[1];
  614.                 found = aiGetMaterialIntegerArray(material, _AI_MATKEY_MAPPINGMODE_V_BASE, textureType, textureIndex, aiInteger, new int[] {1});
  615.                 if(found == 0) {
  616.                     switch(aiInteger[0]) {
  617.                         case aiTextureMapMode_Wrap:
  618.                             currentList.get(textureIndex).textureMappingModeV = TextureMappingMode.WRAP;
  619.                             break;
  620.                         case aiTextureMapMode_Clamp:
  621.                             currentList.get(textureIndex).textureMappingModeV = TextureMappingMode.CLAMP;
  622.                             break;
  623.                         case aiTextureMapMode_Decal:
  624.                             currentList.get(textureIndex).textureMappingModeV = TextureMappingMode.DECAL;
  625.                             break;
  626.                         case aiTextureMapMode_Mirror:
  627.                             currentList.get(textureIndex).textureMappingModeV = TextureMappingMode.MIRROR;
  628.                             break;
  629.                         default:
  630.                             throw new AssertionError("Unknown mappingmode!");
  631.                     }
  632.                 }else {
  633.                     currentList.get(textureIndex).textureMappingModeV = TextureMappingMode.WRAP;
  634.                 }
  635.                 break;
  636.             case _AI_MATKEY_TEXMAP_AXIS_BASE:
  637.                 currentList.get(textureIndex).textureMappingBaseAxis = new double[] {data.getFloat(), data.getFloat(), data.getFloat()};//TODO: test and probably add align at the front
  638.                 data.rewind();
  639.                 break;
  640.             case _AI_MATKEY_TEXFLAGS_BASE:
  641.                 aiInteger = new int[1];
  642.                 found = aiGetMaterialIntegerArray(material, _AI_MATKEY_TEXFLAGS_BASE, textureType, textureIndex, aiInteger, new int[] {1});
  643.                 if(found == 0) {
  644.                     currentList.get(textureIndex).textureFlags = EnumSet.noneOf(TextureFlag.class);
  645.                     if((aiInteger[0] & aiTextureFlags_Invert) == aiTextureFlags_Invert){
  646.                         currentList.get(textureIndex).textureFlags.add(TextureFlag.INVERT);
  647.                     }
  648.                     if((aiInteger[0] & aiTextureFlags_UseAlpha) == aiTextureFlags_UseAlpha){
  649.                         currentList.get(textureIndex).textureFlags.add(TextureFlag.USE_ALPHA);
  650.                     }
  651.                     if((aiInteger[0] & aiTextureFlags_IgnoreAlpha) == aiTextureFlags_IgnoreAlpha){
  652.                         currentList.get(textureIndex).textureFlags.add(TextureFlag.IGNORE_ALPHA);
  653.                     }
  654.                 }else {
  655.                     currentList.get(textureIndex).textureFlags = EnumSet.noneOf(TextureFlag.class);
  656.                 }
  657.                 break;
  658.             case _AI_MATKEY_UVTRANSFORM_BASE:
  659.                 AIUVTransform aiUVTransform = AIUVTransform.calloc();
  660.                 found = aiGetMaterialUVTransform(material, _AI_MATKEY_UVTRANSFORM_BASE, textureType, textureIndex, aiUVTransform);
  661.                 if(found == 0) {
  662.                     currentList.get(textureIndex).textureUVTranslation = new double[] {aiUVTransform.mTranslation().x(), aiUVTransform.mTranslation().y()};
  663.                     currentList.get(textureIndex).textureUVScaling = new double[] {aiUVTransform.mScaling().x(), aiUVTransform.mScaling().y()};
  664.                     currentList.get(textureIndex).textureUVRotation = aiUVTransform.mRotation();
  665.                 }else {
  666.                     currentList.get(textureIndex).textureUVTranslation = new double[2];
  667.                     currentList.get(textureIndex).textureUVScaling = new double[] {1.0, 1.0};
  668.                     currentList.get(textureIndex).textureUVRotation = 0.0f;
  669.                 }
  670.                 aiUVTransform.free();
  671.                 break;
  672.             default:
  673.                 throw new AssertionError("Unknown key!");
  674.         }
  675.     }
  676.    
  677.     public static VkDescriptorSetLayoutBinding.Buffer createLayoutBindings(int descriptorCount) {
  678.        
  679.         VkDescriptorSetLayoutBinding.Buffer layoutBindings = VkDescriptorSetLayoutBinding.calloc(1+2*7);
  680.         layoutBindings.get(0).binding(0);
  681.         layoutBindings.get(0).descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
  682.         layoutBindings.get(0).descriptorCount(descriptorCount);
  683.         layoutBindings.get(0).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  684.         layoutBindings.get(0).pImmutableSamplers(null);
  685.        
  686.         for(int i=0;i<7;++i) {
  687.             layoutBindings.get(2*i+1).binding(2*i+1);
  688.             layoutBindings.get(2*i+1).descriptorType(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
  689.             layoutBindings.get(2*i+1).descriptorCount(descriptorCount);
  690.             layoutBindings.get(2*i+1).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  691.             layoutBindings.get(2*i+1).pImmutableSamplers(null);
  692.            
  693.             layoutBindings.get(2*i+2).binding(2*i+2);
  694.             layoutBindings.get(2*i+2).descriptorType(VK_DESCRIPTOR_TYPE_SAMPLER);
  695.             layoutBindings.get(2*i+2).descriptorCount(descriptorCount);
  696.             layoutBindings.get(2*i+2).stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
  697.             layoutBindings.get(2*i+2).pImmutableSamplers(null);
  698.         }
  699.        
  700.         return layoutBindings;
  701.     }
  702.    
  703.     public ByteBuffer getPerMaterialUniformData(long minUniformBufferOffsetAlignment) {
  704.         ArrayList<double[]> colors = new ArrayList<double[]>();
  705.         double[] zeroVal = new double[] {0.0, 0.0, 0.0, 0.0};
  706.         if(diffuseColor!=null) {
  707.             colors.add(diffuseColor);
  708.         }else {
  709.             colors.add(zeroVal);
  710.         }
  711.         if(specularColor!=null) {
  712.             colors.add(specularColor);
  713.         }else {
  714.             colors.add(zeroVal);
  715.         }
  716.         if(ambientColor!=null) {
  717.             colors.add(ambientColor);
  718.         }else {
  719.             colors.add(zeroVal);
  720.         }
  721.         if(emissiveColor!=null) {
  722.             colors.add(emissiveColor);
  723.         }else {
  724.             colors.add(zeroVal);
  725.         }
  726.         if(transparentColor!=null) {
  727.             colors.add(transparentColor);
  728.         }else {
  729.             colors.add(zeroVal);
  730.         }
  731.         int shadingType;
  732.         switch(shadingModel) {
  733.             case FLAT:
  734.                 shadingType = 0;
  735.                 break;
  736.             case GOURAUD:
  737.                 shadingType = 1;
  738.                 break;
  739.             case PHONG:
  740.                 shadingType = 2;
  741.                 break;
  742.             case BLINN:
  743.                 shadingType = 3;
  744.                 break;
  745.             case TOON:
  746.                 shadingType = 4;
  747.                 break;
  748.             case OREN_NAYAR:
  749.                 shadingType = 5;
  750.                 break;
  751.             case MINNAERT:
  752.                 shadingType = 6;
  753.                 break;
  754.             case COOK_TORRANCE:
  755.                 shadingType = 7;
  756.                 break;
  757.             case NO_SHADING:
  758.                 shadingType = 9;
  759.                 break;
  760.             case FRESNEL:
  761.                 shadingType = 8;
  762.                 break;
  763.             default:
  764.                 shadingType = -1;
  765.                 throw new AssertionError("Unreachable state!");
  766.         }
  767.        
  768.         ByteBuffer out = memAlloc((colors.size()*4 + 4)*(Float.SIZE/Byte.SIZE));
  769.        
  770.         for(int i=0;i<colors.size();++i) {
  771.             for(int j=0;j<4;++j) {
  772.                 out.putFloat((float)colors.get(i)[j]);
  773.             }
  774.         }
  775.    
  776.         out.putFloat(reflectivity);
  777.         out.putFloat(opacity);
  778.         out.putFloat(shininess);
  779.         out.putFloat(Float.intBitsToFloat(shadingType));
  780.         while(!(out.position()%16==0)){
  781.             out.put((byte)0);
  782.         }
  783.        
  784.         out.flip();
  785.                
  786.         return out;
  787.     }
  788.    
  789.     public TextureStack[] getTextureStacks() {
  790.         TextureStack[] out = new TextureStack[11];
  791.         out[0] = diffuseTextureStack;
  792.         out[1] = specularTextureStack;
  793.         out[2] = ambientTextureStack;
  794.         out[3] = emissiveTextureStack;
  795.         out[4] = heightTextureStack;
  796.         out[5] = normalsTextureStack;
  797.         out[6] = shininessTextureStack;
  798.         out[7] = opacityTextureStack;
  799.         out[8] = displacementTextureStack;
  800.         out[9] = lightmapTextureStack;
  801.         out[10] = reflectionTextureStack;
  802.         return out;
  803.     }
  804.    
  805.     public void printOut() {
  806.         System.out.println("Name: "+name);
  807.         System.out.println("diffuseColor: "+(diffuseColor==null?"null":diffuseColor[0]+" "+diffuseColor[1]+" "+diffuseColor[2]));
  808.         System.out.println("specularColor: "+(specularColor==null?"null":specularColor[0]+" "+specularColor[1]+" "+specularColor[2]));
  809.         System.out.println("ambientColor: "+(ambientColor==null?"null":ambientColor[0]+" "+ambientColor[1]+" "+ambientColor[2]));
  810.         System.out.println("emissiveColor: "+(emissiveColor==null?"null":emissiveColor[0]+" "+emissiveColor[1]+" "+emissiveColor[2]));
  811.         System.out.println("transparentColor: "+(transparentColor==null?"null":transparentColor[0]+" "+transparentColor[1]+" "+transparentColor[2]));
  812.         System.out.println("reflectiveColor: "+(reflectiveColor==null?"null":reflectiveColor[0]+" "+reflectiveColor[1]+" "+reflectiveColor[2]));
  813.         System.out.println("Wireframe: "+wireframe);
  814.         System.out.println("Twosided: "+twosided);
  815.         System.out.println("GlobalBackgroundImage: "+globalBackgroundImage);
  816.         System.out.println("Shading Model: "+shadingModel);
  817.         System.out.println("Blend Function: "+blendFunc);
  818.         System.out.println("Opacity: "+opacity);
  819.         System.out.println("Shininess: "+shininess);
  820.         System.out.println("Shininess Strength: "+shininessStrength);
  821.         System.out.println("Refraction Index: "+refractionIndex);
  822.         System.out.println("Bump Scaling: "+bumpScaling);
  823.         System.out.println("Reflectivity: "+reflectivity);
  824.         printTextureStacks();
  825.     }
  826.    
  827.     public void printTextureStacks() {
  828.         if(diffuseTextureStack.texturePaths.length != 0) {
  829.             System.out.println("Diffuse TextureStack:");
  830.             diffuseTextureStack.printOut();
  831.         }
  832.         if(specularTextureStack.texturePaths.length != 0) {
  833.             System.out.println("Specular TextureStack:");
  834.             specularTextureStack.printOut();
  835.         }
  836.         if(ambientTextureStack.texturePaths.length != 0) {
  837.             System.out.println("Ambient TextureStack:");
  838.             ambientTextureStack.printOut();
  839.         }
  840.         if(emissiveTextureStack.texturePaths.length != 0) {
  841.             System.out.println("Emissive TextureStack:");
  842.             emissiveTextureStack.printOut();
  843.         }
  844.         if(heightTextureStack.texturePaths.length != 0) {
  845.             System.out.println("Height TextureStack:");
  846.             heightTextureStack.printOut();
  847.         }
  848.         if(normalsTextureStack.texturePaths.length != 0) {
  849.             System.out.println("Normals TextureStack:");
  850.             normalsTextureStack.printOut();
  851.         }
  852.         if(shininessTextureStack.texturePaths.length != 0) {
  853.             System.out.println("Shininess TextureStack:");
  854.             shininessTextureStack.printOut();
  855.         }
  856.         if(opacityTextureStack.texturePaths.length != 0) {
  857.             System.out.println("Opacity TextureStack:");
  858.             opacityTextureStack.printOut();
  859.         }
  860.         if(displacementTextureStack.texturePaths.length != 0) {
  861.             System.out.println("Displacement TextureStack:");
  862.             displacementTextureStack.printOut();
  863.         }
  864.         if(lightmapTextureStack.texturePaths.length != 0) {
  865.             System.out.println("Lightmap TextureStack:");
  866.             lightmapTextureStack.printOut();
  867.         }
  868.         if(reflectionTextureStack.texturePaths.length != 0) {
  869.             System.out.println("Reflection TextureStack:");
  870.             reflectionTextureStack.printOut();
  871.         }
  872.     }
  873.    
  874.     public static class TextureStack{
  875.         public final String[] texturePaths;
  876.         public final float[] textureStrengths;
  877.         public final TextureOp[] textureOps;
  878.         public final int[] textureUVChannels;
  879.         public final TextureMapping[] textureMappings;
  880.         public final TextureMappingMode[] textureMappingModesU;
  881.         public final TextureMappingMode[] textureMappingModesV;
  882.         public final double[][] textureMappingBaseAxis;
  883.         public final EnumSet<TextureFlag>[] textureFlags;
  884.         public final double[][] textureUVTranslations;
  885.         public final double[][] textureUVScalings;
  886.         public final float[] textureUVRotations;
  887.        
  888.         public int[] textureIndices;
  889.        
  890.         public final TextureType textureType;
  891.         public final boolean mustBeBlended;
  892.         public final boolean colorIsDefault;
  893.         public final Material material;
  894.        
  895.         public final double[] baseColor;
  896.        
  897.         public TextureStack(String[] texturePaths, float[] textureStrengths, TextureOp[] textureOps, int[] textureUVChannels, TextureMapping[] textureMappings,
  898.                 TextureMappingMode[] textureMappingModesU, TextureMappingMode[] textureMappingModesV, double[][] textureMappingBaseAxis,
  899.                 EnumSet<TextureFlag>[] textureFlags, double[][] textureUVTranslations, double[][] textureUVScalings, float[] textureUVRotations, TextureType textureType, Material material) {
  900.             this.texturePaths = texturePaths;
  901.             this.textureStrengths = textureStrengths;
  902.             this.textureOps = textureOps;
  903.             this.textureUVChannels = textureUVChannels;
  904.             this.textureMappings = textureMappings;
  905.             this.textureMappingModesU = textureMappingModesU;
  906.             this.textureMappingModesV = textureMappingModesV;
  907.             this.textureMappingBaseAxis = textureMappingBaseAxis;
  908.             this.textureFlags = textureFlags;
  909.             this.textureUVTranslations = textureUVTranslations;
  910.             this.textureUVScalings = textureUVScalings;
  911.             this.textureUVRotations = textureUVRotations;
  912.             this.textureIndices = new int[texturePaths.length];
  913.             this.textureType = textureType;
  914.             this.material = material;
  915.             this.colorIsDefault = calculateColorIsDefault();
  916.            
  917.             if(!colorIsDefault) {
  918.                 baseColor = fetchBaseColor();
  919.             }else {
  920.                 baseColor = null;
  921.             }
  922.            
  923.             this.mustBeBlended = calculateMustBeBlended();
  924.         }
  925.        
  926.         public TextureStack(TextureStackItem[] stackItems, TextureType textureType, Material material) {
  927.             this.textureType = textureType;
  928.            
  929.             this.texturePaths = new String[stackItems.length];
  930.             this.textureStrengths = new float[stackItems.length];
  931.             this.textureOps = new TextureOp[stackItems.length];
  932.             this.textureUVChannels = new int[stackItems.length];
  933.             this.textureMappings = new TextureMapping[stackItems.length];
  934.             this.textureMappingModesU = new TextureMappingMode[stackItems.length];
  935.             this.textureMappingModesV = new TextureMappingMode[stackItems.length];
  936.             this.textureMappingBaseAxis = new double[stackItems.length][];
  937.            
  938.             @SuppressWarnings("unchecked")
  939.             EnumSet<TextureFlag>[] tmp = (EnumSet<TextureFlag>[]) new EnumSet<?>[stackItems.length];
  940.             this.textureFlags = tmp;
  941.            
  942.             this.textureUVTranslations = new double[stackItems.length][];
  943.             this.textureUVScalings = new double[stackItems.length][];
  944.             this.textureUVRotations = new float[stackItems.length];
  945.            
  946.             this.textureIndices = new int[texturePaths.length];
  947.            
  948.             for(int i=0;i<stackItems.length;++i) {
  949.                 this.texturePaths[i] = stackItems[i].texturePath;
  950.                 this.textureStrengths[i] = stackItems[i].textureStrength;
  951.                 this.textureOps[i] = (stackItems[i].textureOp==null?TextureOp.MULTIPLY:stackItems[i].textureOp);
  952.                 this.textureUVChannels[i] = (stackItems[i].textureUVChannel == -1?i:stackItems[i].textureUVChannel);
  953.                 this.textureMappings[i] = (stackItems[i].textureMapping==null?stackItems[i].textureMapping.UV:stackItems[i].textureMapping);//All converted to UV currently...
  954.                 this.textureMappingModesU[i] = (stackItems[i].textureMappingModeU==null?TextureMappingMode.WRAP:stackItems[i].textureMappingModeU);
  955.                 this.textureMappingModesV[i] = (stackItems[i].textureMappingModeV==null?TextureMappingMode.WRAP:stackItems[i].textureMappingModeV);
  956.                 this.textureMappingBaseAxis[i] = stackItems[i].textureMappingBaseAxis;
  957.                 this.textureFlags[i] = (stackItems[i].textureFlags==null?EnumSet.noneOf(TextureFlag.class):stackItems[i].textureFlags);
  958.                 this.textureUVTranslations[i] = stackItems[i].textureUVTranslation;
  959.                 this.textureUVScalings[i] = stackItems[i].textureUVScaling;
  960.                 this.textureUVRotations[i] = stackItems[i].textureUVRotation;
  961.             }
  962.             this.material = material;
  963.             this.colorIsDefault = calculateColorIsDefault();
  964.            
  965.             if(!colorIsDefault) {
  966.                 baseColor = fetchBaseColor();
  967.             }else {
  968.                 baseColor = null;
  969.             }
  970.            
  971.             this.mustBeBlended = calculateMustBeBlended();
  972.         }
  973.        
  974.         private boolean calculateColorIsDefault() {
  975.             if(this.textureType != TextureType.DIFFUSE && this.textureType != TextureType.SPECULAR && this.textureType != TextureType.AMBIENT && this.textureType != TextureType.EMISSIVE &&
  976.                     this.textureType != TextureType.REFLECTION && this.textureType != TextureType.SHININESS) {
  977.                 return true;
  978.             }else {
  979.                 boolean colorIsDefault;
  980.                 switch(this.textureType) {
  981.                     case DIFFUSE:
  982.                         colorIsDefault = (this.material.diffuseColor == null || (this.material.diffuseColor[0] == 0.0 && this.material.diffuseColor[1] == 0.0 && this.material.diffuseColor[2] == 0.0));
  983.                         break;
  984.                     case SPECULAR:
  985.                         colorIsDefault = (this.material.specularColor == null || (this.material.specularColor[0] == 0.0 && this.material.specularColor[1] == 0.0 && this.material.specularColor[2] == 0.0));
  986.                         break;
  987.                     case AMBIENT:
  988.                         colorIsDefault = (this.material.ambientColor == null || (this.material.ambientColor[0] == 0.0 && this.material.ambientColor[1] == 0.0 && this.material.ambientColor[2] == 0.0));
  989.                         break;
  990.                     case EMISSIVE:
  991.                         colorIsDefault = (this.material.emissiveColor == null || (this.material.emissiveColor[0] == 0.0 && this.material.emissiveColor[1] == 0.0 && this.material.emissiveColor[2] == 0.0));
  992.                         break;
  993.                     case REFLECTION:
  994.                         colorIsDefault = (this.material.reflectiveColor == null || (this.material.reflectiveColor[0] == 0.0 && this.material.reflectiveColor[1] == 0.0 && this.material.reflectiveColor[2] == 0.0));
  995.                         break;
  996.                     case SHININESS:
  997.                         colorIsDefault = (this.material.shininessStrength != 0.0);
  998.                         break;
  999.                     default:
  1000.                         throw new AssertionError("Unreachable!");
  1001.                 }
  1002.                
  1003.                 return colorIsDefault;
  1004.             }
  1005.         }
  1006.        
  1007.         private double[] fetchBaseColor() {
  1008.             switch(this.textureType) {
  1009.                 case DIFFUSE:
  1010.                     return this.material.diffuseColor;
  1011.                 case SPECULAR:
  1012.                     return this.material.specularColor;
  1013.                 case AMBIENT:
  1014.                     return this.material.ambientColor;
  1015.                 case EMISSIVE:
  1016.                     return this.material.emissiveColor;
  1017.                 case REFLECTION:
  1018.                     return this.material.reflectiveColor;
  1019.                 case SHININESS:
  1020.                     return new double[] {this.material.shininessStrength, this.material.shininessStrength, this.material.shininessStrength, this.material.shininessStrength};
  1021.                 default:
  1022.                     throw new AssertionError("Unreachable!");
  1023.             }
  1024.         }
  1025.        
  1026.         private boolean calculateMustBeBlended() {
  1027.             if(colorIsDefault && this.textureIndices.length<2) {
  1028.                 return false;
  1029.             }
  1030.             if(this.textureIndices.length<1) {
  1031.                 return false;
  1032.             }else {
  1033.                 return true;
  1034.             }
  1035.         }
  1036.        
  1037.         public boolean equalsBlendKind(TextureStack t) {
  1038.             if(this.mustBeBlended == false && t.mustBeBlended == false) {
  1039.                 return true;
  1040.             }else if(this.textureIndices.length == t.textureIndices.length) {
  1041.                 return true;
  1042.             }else {
  1043.                 return false;
  1044.             }
  1045.         }
  1046.        
  1047.         public void printOut() {
  1048.             int pathWidth = 0, strengthWidth = 0, channelWidth = 0, axisWidth = 0, flagsWidth = 0, translationWidth = 0, scalingWidth = 0, rotationWidth = 0, indexWidth = 0;
  1049.             final int opWidth = 10;
  1050.             final int mappingWidth = 8;
  1051.             final int mappingModeWidth = 12;
  1052.             for(int i=0;i<texturePaths.length;++i) {
  1053.                 if(texturePaths[i].length() > pathWidth) {
  1054.                     pathWidth = texturePaths[i].length();
  1055.                 }
  1056.                 if((textureStrengths[i]+"").length() > strengthWidth) {
  1057.                     strengthWidth = (textureStrengths[i]+"").length();
  1058.                 }
  1059.                 if((textureUVChannels[i]+"").length() > channelWidth) {
  1060.                     channelWidth = (textureUVChannels[i]+"").length();
  1061.                 }
  1062.                 if(textureMappingBaseAxis[i] != null) {
  1063.                     if((textureMappingBaseAxis[i][0]+" "+textureMappingBaseAxis[i][1]+" "+textureMappingBaseAxis[i][2]).length() > axisWidth) {
  1064.                         axisWidth = (textureMappingBaseAxis[i][0]+" "+textureMappingBaseAxis[i][1]+" "+textureMappingBaseAxis[i][2]).length();
  1065.                     }
  1066.                 }else {
  1067.                     if(4 > axisWidth) {
  1068.                         axisWidth = 4;
  1069.                     }
  1070.                 }
  1071.                 if(this.textureFlags[i] != null) {
  1072.                     String tmp = "";
  1073.                     TextureFlag[] dum = this.textureFlags[i].toArray(new TextureFlag[0]);
  1074.                     for(int j=0;j<dum.length;++j) {
  1075.                         tmp += dum[j]+"";
  1076.                         if(j!=dum.length-1) {
  1077.                             tmp += "|";
  1078.                         }
  1079.                     }
  1080.                     if(tmp.length() > flagsWidth) {
  1081.                         flagsWidth = tmp.length();
  1082.                     }
  1083.                 }else {
  1084.                     if(4 > flagsWidth) {
  1085.                         flagsWidth = 4;
  1086.                     }
  1087.                 }
  1088.                 if(textureUVTranslations[i] != null) {
  1089.                     if((textureUVTranslations[i][0]+" "+textureUVTranslations[i][1]).length() > translationWidth) {
  1090.                         translationWidth = (textureUVTranslations[i][0]+" "+textureUVTranslations[i][1]).length();
  1091.                     }
  1092.                     if((textureUVScalings[i][0]+" "+textureUVScalings[i][1]).length() > scalingWidth) {
  1093.                         scalingWidth = (textureUVScalings[i][0]+" "+textureUVScalings[i][1]).length();
  1094.                     }
  1095.                 }else {
  1096.                     if(4 > translationWidth) {
  1097.                         translationWidth = 4;
  1098.                     }
  1099.                     if(4 > scalingWidth) {
  1100.                         scalingWidth = 4;
  1101.                     }
  1102.                 }
  1103.                 if((textureUVRotations[i]+"").length() > rotationWidth) {
  1104.                     rotationWidth = (textureUVRotations[i]+"").length();
  1105.                 }
  1106.                 indexWidth = (i+"").length();
  1107.             }
  1108.             String out = "Index";
  1109.             if(indexWidth < 5) {
  1110.                 indexWidth = 5;
  1111.             }
  1112.             for(int i=0;i<indexWidth-5;++i) {
  1113.                 out +=" ";
  1114.             }
  1115.             out +=" | ";
  1116.             out += "Path";
  1117.             if(pathWidth < 4) {
  1118.                 pathWidth = 4;
  1119.             }
  1120.             for(int i=0;i<pathWidth-4;++i) {
  1121.                 out +=" ";
  1122.             }
  1123.             out +=" | ";
  1124.             out += "Strength";
  1125.             if(strengthWidth < 8) {
  1126.                 strengthWidth = 8;
  1127.             }
  1128.             for(int i=0;i<strengthWidth-8;++i) {
  1129.                 out +=" ";
  1130.             }
  1131.             out +=" | ";
  1132.             out += "Operation";
  1133.             for(int i=0;i<opWidth-9;++i) {
  1134.                 out +=" ";
  1135.             }
  1136.             out +=" | ";
  1137.             out += "UVChannel";
  1138.             if(channelWidth < 9) {
  1139.                 channelWidth = 9;
  1140.             }
  1141.             for(int i=0;i<channelWidth-9;++i) {
  1142.                 out +=" ";
  1143.             }
  1144.             out +=" | ";
  1145.             out += "Mapping";
  1146.             for(int i=0;i<mappingWidth-7;++i) {
  1147.                 out +=" ";
  1148.             }
  1149.             out +=" | ";
  1150.             out += "MappingModeU";
  1151.             for(int i=0;i<mappingModeWidth-12;++i) {
  1152.                 out +=" ";
  1153.             }
  1154.             out +=" | ";
  1155.             out += "MappingModeV";
  1156.             for(int i=0;i<mappingModeWidth-12;++i) {
  1157.                 out +=" ";
  1158.             }
  1159.             out +=" | ";
  1160.             out += "MappingBaseAxis";
  1161.             if(axisWidth < 15) {
  1162.                 axisWidth = 15;
  1163.             }
  1164.             for(int i=0;i<axisWidth-15;++i) {
  1165.                 out +=" ";
  1166.             }
  1167.             out +=" | ";
  1168.             out += "TextureFlags";
  1169.             if(flagsWidth < 12) {
  1170.                 flagsWidth = 12;
  1171.             }
  1172.             for(int i=0;i<flagsWidth-12;++i) {
  1173.                 out +=" ";
  1174.             }
  1175.             out +=" | ";
  1176.             out += "UVTranslation";
  1177.             if(translationWidth < 13) {
  1178.                 translationWidth = 13;
  1179.             }
  1180.             for(int i=0;i<translationWidth-13;++i) {
  1181.                 out +=" ";
  1182.             }
  1183.             out +=" | ";
  1184.             out += "UVScaling";
  1185.             if(scalingWidth < 9) {
  1186.                 scalingWidth = 9;
  1187.             }
  1188.             for(int i=0;i<scalingWidth-9;++i) {
  1189.                 out +=" ";
  1190.             }
  1191.             out +=" | ";
  1192.             out += "UVRotation";
  1193.             if(rotationWidth < 10) {
  1194.                 rotationWidth = 10;
  1195.             }
  1196.             for(int i=0;i<rotationWidth-10;++i) {
  1197.                 out +=" ";
  1198.             }
  1199.             System.out.println(out);
  1200.             for(int j=0;j<texturePaths.length;++j) {
  1201.                 String tmp = j+"";
  1202.                 out = tmp;
  1203.                 for(int i=0;i<indexWidth-tmp.length();++i) {
  1204.                     out +=" ";
  1205.                 }
  1206.                 out +=" | ";
  1207.                 tmp = texturePaths[j];
  1208.                 out += tmp;
  1209.                 for(int i=0;i<pathWidth-tmp.length();++i) {
  1210.                     out +=" ";
  1211.                 }
  1212.                 out +=" | ";
  1213.                 tmp = textureStrengths[j]+"";
  1214.                 out += tmp;
  1215.                 for(int i=0;i<strengthWidth-tmp.length();++i) {
  1216.                     out +=" ";
  1217.                 }
  1218.                 out +=" | ";
  1219.                 tmp = textureOps[j]+"";
  1220.                 out += tmp;
  1221.                 for(int i=0;i<opWidth-tmp.length();++i) {
  1222.                     out +=" ";
  1223.                 }
  1224.                 out +=" | ";
  1225.                 tmp = textureUVChannels[j]+"";
  1226.                 out += tmp;
  1227.                 for(int i=0;i<channelWidth-tmp.length();++i) {
  1228.                     out +=" ";
  1229.                 }
  1230.                 out +=" | ";
  1231.                 tmp = textureMappings[j]+"";
  1232.                 out += tmp;
  1233.                 for(int i=0;i<mappingWidth-tmp.length();++i) {
  1234.                     out +=" ";
  1235.                 }
  1236.                 out +=" | ";
  1237.                 tmp = textureMappingModesU[j]+"";
  1238.                 out += tmp;
  1239.                 for(int i=0;i<mappingModeWidth-tmp.length();++i) {
  1240.                     out +=" ";
  1241.                 }
  1242.                 out +=" | ";
  1243.                 tmp = textureMappingModesV[j]+"";
  1244.                 out += tmp;
  1245.                 for(int i=0;i<mappingModeWidth-tmp.length();++i) {
  1246.                     out +=" ";
  1247.                 }
  1248.                 out +=" | ";
  1249.                 if(textureMappingBaseAxis[j] != null) {
  1250.                     tmp = textureMappingBaseAxis[j][0]+" "+textureMappingBaseAxis[j][1]+" "+textureMappingBaseAxis[j][2];
  1251.                 }else {
  1252.                     tmp = "null";
  1253.                 }
  1254.                 out += tmp;
  1255.                 for(int i=0;i<axisWidth-tmp.length();++i) {
  1256.                     out +=" ";
  1257.                 }
  1258.                 out +=" | ";
  1259.                 tmp = textureFlags[j]+"";
  1260.                 out += tmp;
  1261.                 for(int i=0;i<flagsWidth-tmp.length();++i) {
  1262.                     out +=" ";
  1263.                 }
  1264.                 out +=" | ";
  1265.                 if(textureUVTranslations[j] != null) {
  1266.                     tmp = textureUVTranslations[j][0]+" "+textureUVTranslations[j][1];
  1267.                 }else {
  1268.                     tmp = "null";
  1269.                 }
  1270.                 out += tmp;
  1271.                 for(int i=0;i<translationWidth-tmp.length();++i) {
  1272.                     out +=" ";
  1273.                 }
  1274.                 out +=" | ";
  1275.                 if(textureUVScalings[j] != null) {
  1276.                     tmp = textureUVScalings[j][0]+" "+textureUVScalings[j][1];
  1277.                 }else {
  1278.                     tmp = "null";
  1279.                 }
  1280.                 out += tmp;
  1281.                 for(int i=0;i<scalingWidth-tmp.length();++i) {
  1282.                     out +=" ";
  1283.                 }
  1284.                 out +=" | ";
  1285.                 tmp = textureUVRotations[j]+"";
  1286.                 out += tmp;
  1287.                 for(int i=0;i<rotationWidth-tmp.length();++i) {
  1288.                     out +=" ";
  1289.                 }
  1290.                 System.out.println(out);
  1291.             }
  1292.         }
  1293.     }
  1294.    
  1295.     private static class TextureStackItem{
  1296.         public String texturePath = "";
  1297.         public float textureStrength = 1.0f;
  1298.         public TextureOp textureOp = TextureOp.MULTIPLY;
  1299.         public int textureUVChannel;
  1300.         public TextureMapping textureMapping = TextureMapping.UV;
  1301.         public TextureMappingMode textureMappingModeU = TextureMappingMode.WRAP;
  1302.         public TextureMappingMode textureMappingModeV = TextureMappingMode.WRAP;
  1303.         public double[] textureMappingBaseAxis;
  1304.         public EnumSet<TextureFlag> textureFlags;
  1305.         public double[] textureUVTranslation;
  1306.         public double[] textureUVScaling;
  1307.         public float textureUVRotation;
  1308.        
  1309.         public TextureStackItem() {
  1310.             textureUVChannel = -1;
  1311.         }
  1312.     }
  1313. }
  1314.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement