package electrosphere.renderer.model; import electrosphere.engine.Globals; import electrosphere.renderer.OpenGLState; import electrosphere.renderer.texture.Texture; import org.lwjgl.assimp.AIMaterial; import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; import static org.lwjgl.opengl.GL11.glBindTexture; import static org.lwjgl.opengl.GL13.GL_TEXTURE0; import static org.lwjgl.opengl.GL13.glActiveTexture; import static org.lwjgl.opengl.GL20.glGetUniformLocation; import static org.lwjgl.opengl.GL20.glUniform1i; /** * * @author amaterasu */ public class Material { String diffuse; String specular; boolean hasTransparency = false; //Sets whether this material should get its texture pointers from the assetManager by looking up diffuse and specular paths //or whether it should have a manually set texturePointer and not look up while binding boolean usesFetch = true; int texturePointer; public Material(){ } //basically useless because blender doesn't support exporting mats with fbx public static Material load_material_from_aimaterial(AIMaterial input){ Material rVal = new Material(); // if(input.mNumProperties() > 0){ // PointerBuffer property_buffer = input.mProperties(); // while(property_buffer.hasRemaining()){ // AIMaterialProperty new_property = AIMaterialProperty.create(property_buffer.get()); // // System.out.println("Property: " + new_property.mKey().dataString()); // } // } return rVal; } public String get_diffuse(){ return diffuse; } public String get_specular(){ return specular; } public void set_diffuse(String t){ diffuse = t; // if(t.isTransparent()){ // hasTransparency = true; // } } public void set_specular(String t){ specular = t; // if(t.isTransparent()){ // hasTransparency = true; // } } public void setTexturePointer(int pointer){ texturePointer = pointer; usesFetch = false; } /** * Applies the material */ public void apply_material(OpenGLState openGLState){ //Controls whether the texturePointer should be resolved by looking up the diffuse in asset manager or using the texture pointer already set in this material if(usesFetch){ Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse); if(diffuseTexture != null){ diffuseTexture.bind(openGLState,0); glUniform1i(glGetUniformLocation(Globals.renderingEngine.getOpenGLState().getActiveShader().getShaderId(), "material.diffuse"), 0); } Texture specularTexture = Globals.assetManager.fetchTexture(specular); if(specularTexture != null){ specularTexture.bind(openGLState,1); glUniform1i(glGetUniformLocation(Globals.renderingEngine.getOpenGLState().getActiveShader().getShaderId(), "material.specular"), 1); } } else { openGLState.glBindTextureUnit(GL_TEXTURE0, texturePointer, GL_TEXTURE_2D); } } public void apply_material(OpenGLState openGLState, int diffuse_channel, int specular_channel){ Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse); if(diffuseTexture != null){ diffuseTexture.bind(openGLState,diffuse_channel); } Texture specularTexture = Globals.assetManager.fetchTexture(specular); if(specularTexture != null){ specularTexture.bind(openGLState,specular_channel); } } public boolean isTransparent(){ boolean rVal = false; Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse); if(diffuseTexture != null && diffuseTexture.isTransparent()){ rVal = true; } Texture specularTexture = Globals.assetManager.fetchTexture(specular); if(specularTexture != null && specularTexture.isTransparent()){ rVal = true; } return rVal; } }