Managing textures in asset manager
This commit is contained in:
		
							parent
							
								
									f66dab4f5f
								
							
						
					
					
						commit
						3d0acb530e
					
				| @ -138,11 +138,11 @@ public class Globals { | |||||||
|     // |     // | ||||||
|     //OpenGL - Abstracted engine objects |     //OpenGL - Abstracted engine objects | ||||||
|     // |     // | ||||||
|     public static Texture textureDiffuseDefault; |     public static String textureDiffuseDefault; | ||||||
|     public static Texture textureSpecularDefault; |     public static String textureSpecularDefault; | ||||||
|     public static Material materialDefault; |     public static Material materialDefault; | ||||||
|      |      | ||||||
|     public static Texture blackTexture; |     public static String blackTexture; | ||||||
|      |      | ||||||
|     public static ArrayList<PointLight> lightPointListDefault; |     public static ArrayList<PointLight> lightPointListDefault; | ||||||
|     public static SpotLight lightSpotDefault; |     public static SpotLight lightSpotDefault; | ||||||
| @ -254,18 +254,19 @@ public class Globals { | |||||||
|      |      | ||||||
|     public static void initDefaultGraphicalResources(){ |     public static void initDefaultGraphicalResources(){ | ||||||
|         //create default textures |         //create default textures | ||||||
|         textureDiffuseDefault = new Texture("Textures/default_diffuse.png"); |         Globals.assetManager.addTexturePathtoQueue("Textures/default_diffuse.png"); | ||||||
|         textureSpecularDefault = new Texture("Textures/default_specular.png"); |         Globals.assetManager.addTexturePathtoQueue("Textures/default_specular.png"); | ||||||
|         //create default material |         //create default material | ||||||
|         materialDefault = new Material(); |         materialDefault = new Material(); | ||||||
|         materialDefault.set_diffuse(textureDiffuseDefault); |         materialDefault.set_diffuse("Textures/default_diffuse.png"); | ||||||
|         materialDefault.set_specular(textureSpecularDefault); |         materialDefault.set_specular("Textures/default_specular.png"); | ||||||
|         //create default lights |         //create default lights | ||||||
|         assetManager.registerModelToSpecificString(ModelUtils.createBitmapDisplay(), AssetDataStrings.ASSET_STRING_BITMAP_FONT); |         assetManager.registerModelToSpecificString(ModelUtils.createBitmapDisplay(), AssetDataStrings.ASSET_STRING_BITMAP_FONT); | ||||||
|         RawFontMap fontMap = FileLoadingUtils.loadObjectFromAssetPath("Textures/Fonts/myFontMap.json", RawFontMap.class); |         RawFontMap fontMap = FileLoadingUtils.loadObjectFromAssetPath("Textures/Fonts/myFontMap.json", RawFontMap.class); | ||||||
|         FontUtils.setFontDataMap(fontMap); |         FontUtils.setFontDataMap(fontMap); | ||||||
|         //black texture for backgrouns |         //black texture for backgrouns | ||||||
|         blackTexture = new Texture("Textures/b1.png"); |         blackTexture = "Textures/b1.png"; | ||||||
|  |         Globals.assetManager.addTexturePathtoQueue("Textures/b1.png"); | ||||||
|         //loading box |         //loading box | ||||||
|         loadingBox = WidgetUtils.createVerticallyAlignedTextBox(520, 100, 50, 7, 1, "LOADING", true); |         loadingBox = WidgetUtils.createVerticallyAlignedTextBox(520, 100, 50, 7, 1, "LOADING", true); | ||||||
|         //init default shaderProgram |         //init default shaderProgram | ||||||
| @ -278,6 +279,10 @@ public class Globals { | |||||||
|         assetManager.addModelPathToQueue("Models/unitsphere_1.fbx"); |         assetManager.addModelPathToQueue("Models/unitsphere_1.fbx"); | ||||||
|         //init smallcube |         //init smallcube | ||||||
|         assetManager.addModelPathToQueue("Models/SmallCube.fbx"); |         assetManager.addModelPathToQueue("Models/SmallCube.fbx"); | ||||||
|  |          | ||||||
|  |         //as these assets are required for the renderer to work, we go ahead and | ||||||
|  |         //load them into memory now. The loading time penalty is worth it I think. | ||||||
|  |         Globals.assetManager.loadAssetsInQueue(); | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     static void initEntityTypeMap(){ |     static void initEntityTypeMap(){ | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ | |||||||
|  */ |  */ | ||||||
| package electrosphere.renderer; | package electrosphere.renderer; | ||||||
| 
 | 
 | ||||||
|  | import electrosphere.main.Globals; | ||||||
| import electrosphere.renderer.texture.Texture; | import electrosphere.renderer.texture.Texture; | ||||||
| import org.lwjgl.PointerBuffer; | import org.lwjgl.PointerBuffer; | ||||||
| import org.lwjgl.assimp.AIMaterial; | import org.lwjgl.assimp.AIMaterial; | ||||||
| @ -15,8 +16,8 @@ import org.lwjgl.assimp.AIMaterialProperty; | |||||||
|  * @author amaterasu |  * @author amaterasu | ||||||
|  */ |  */ | ||||||
| public class Material { | public class Material { | ||||||
|     Texture diffuse; |     String diffuse; | ||||||
|     Texture specular; |     String specular; | ||||||
|     boolean hasTransparency = false; |     boolean hasTransparency = false; | ||||||
|     public Material(){ |     public Material(){ | ||||||
|          |          | ||||||
| @ -33,35 +34,56 @@ public class Material { | |||||||
|         } |         } | ||||||
|         return rVal; |         return rVal; | ||||||
|     } |     } | ||||||
|     public Texture get_diffuse(){ |     public String get_diffuse(){ | ||||||
|         return diffuse; |         return diffuse; | ||||||
|     } |     } | ||||||
|     public Texture get_specular(){ |     public String get_specular(){ | ||||||
|         return specular; |         return specular; | ||||||
|     } |     } | ||||||
|     public void set_diffuse(Texture t){ |     public void set_diffuse(String t){ | ||||||
|         diffuse = t; |         diffuse = t; | ||||||
|         if(t.isTransparent()){ | //        if(t.isTransparent()){ | ||||||
|             hasTransparency = true; | //            hasTransparency = true; | ||||||
|  | //        } | ||||||
|     } |     } | ||||||
|     } |     public void set_specular(String t){ | ||||||
|     public void set_specular(Texture t){ |  | ||||||
|         specular = t; |         specular = t; | ||||||
|         if(t.isTransparent()){ | //        if(t.isTransparent()){ | ||||||
|             hasTransparency = true; | //            hasTransparency = true; | ||||||
|         } | //        } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public void apply_material(){ |     public void apply_material(){ | ||||||
|         diffuse.bind(0); |         Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse); | ||||||
|         specular.bind(1); |         if(diffuseTexture != null){ | ||||||
|  |             diffuseTexture.bind(0); | ||||||
|  |         } | ||||||
|  |         Texture specularTexture = Globals.assetManager.fetchTexture(specular); | ||||||
|  |         if(specularTexture != null){ | ||||||
|  |             specularTexture.bind(1); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|     public void apply_material(int diffuse_channel, int specular_channel){ |     public void apply_material(int diffuse_channel, int specular_channel){ | ||||||
|         diffuse.bind(diffuse_channel); |         Texture diffuseTexture = Globals.assetManager.fetchTexture(diffuse); | ||||||
|         specular.bind(specular_channel); |         if(diffuseTexture != null){ | ||||||
|  |             diffuseTexture.bind(diffuse_channel); | ||||||
|  |         } | ||||||
|  |         Texture specularTexture = Globals.assetManager.fetchTexture(specular); | ||||||
|  |         if(specularTexture != null){ | ||||||
|  |             specularTexture.bind(specular_channel); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public boolean isTransparent(){ |     public boolean isTransparent(){ | ||||||
|         return hasTransparency; |         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; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,6 @@ | |||||||
| package electrosphere.renderer; | package electrosphere.renderer; | ||||||
| 
 | 
 | ||||||
|  | import electrosphere.main.Globals; | ||||||
| import electrosphere.renderer.assetmanager.AssetDataStrings; | import electrosphere.renderer.assetmanager.AssetDataStrings; | ||||||
| import electrosphere.renderer.texture.Texture; | import electrosphere.renderer.texture.Texture; | ||||||
| import java.nio.FloatBuffer; | import java.nio.FloatBuffer; | ||||||
| @ -154,9 +155,9 @@ public class ModelUtils { | |||||||
|         m.parent = rVal; |         m.parent = rVal; | ||||||
|          |          | ||||||
|         Material groundMat = new Material(); |         Material groundMat = new Material(); | ||||||
|         Texture groundTex = new Texture("/Textures/Ground/Dirt1.png"); |         Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); | ||||||
|         groundMat.set_diffuse(groundTex); |         groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); | ||||||
|         groundMat.set_specular(groundTex); |         groundMat.set_specular("/Textures/Ground/Dirt1.png"); | ||||||
|         m.set_material(groundMat); |         m.set_material(groundMat); | ||||||
|          |          | ||||||
|         rVal.meshes.add(m); |         rVal.meshes.add(m); | ||||||
| @ -318,9 +319,9 @@ public class ModelUtils { | |||||||
|         m.parent = rVal; |         m.parent = rVal; | ||||||
|          |          | ||||||
|         Material groundMat = new Material(); |         Material groundMat = new Material(); | ||||||
|         Texture groundTex = new Texture("/Textures/Ground/Dirt1.png"); |         Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); | ||||||
|         groundMat.set_diffuse(groundTex); |         groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); | ||||||
|         groundMat.set_specular(groundTex); |         groundMat.set_specular("/Textures/Ground/Dirt1.png"); | ||||||
|         m.set_material(groundMat); |         m.set_material(groundMat); | ||||||
|          |          | ||||||
|         rVal.meshes.add(m); |         rVal.meshes.add(m); | ||||||
| @ -486,9 +487,9 @@ public class ModelUtils { | |||||||
|         m.parent = rVal; |         m.parent = rVal; | ||||||
|          |          | ||||||
|         Material groundMat = new Material(); |         Material groundMat = new Material(); | ||||||
|         Texture groundTex = new Texture("/Textures/Ground/Dirt1.png"); |         Globals.assetManager.addTexturePathtoQueue("/Textures/Ground/Dirt1.png"); | ||||||
|         groundMat.set_diffuse(groundTex); |         groundMat.set_diffuse("/Textures/Ground/Dirt1.png"); | ||||||
|         groundMat.set_specular(groundTex); |         groundMat.set_specular("/Textures/Ground/Dirt1.png"); | ||||||
|         m.set_material(groundMat); |         m.set_material(groundMat); | ||||||
|          |          | ||||||
|         rVal.meshes.add(m); |         rVal.meshes.add(m); | ||||||
| @ -606,9 +607,9 @@ public class ModelUtils { | |||||||
|         m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME; |         m.nodeID = AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME; | ||||||
|          |          | ||||||
|         Material uiMat = new Material(); |         Material uiMat = new Material(); | ||||||
|         Texture uiTex = new Texture("/Textures/Fonts/myfont1-harsher.png"); |         Globals.assetManager.addTexturePathtoQueue("/Textures/Fonts/myfont1-harsher.png"); | ||||||
|         uiMat.set_diffuse(uiTex); |         uiMat.set_diffuse("/Textures/Fonts/myfont1-harsher.png"); | ||||||
|         uiMat.set_specular(uiTex); |         uiMat.set_specular("/Textures/Fonts/myfont1-harsher.png"); | ||||||
|         m.set_material(uiMat); |         m.set_material(uiMat); | ||||||
|          |          | ||||||
|         rVal.meshes.add(m); |         rVal.meshes.add(m); | ||||||
|  | |||||||
| @ -12,6 +12,7 @@ import static electrosphere.renderer.RenderUtils.createScreenTextureVAO; | |||||||
| import electrosphere.renderer.framebuffer.Framebuffer; | import electrosphere.renderer.framebuffer.Framebuffer; | ||||||
| import electrosphere.renderer.framebuffer.FramebufferUtils; | import electrosphere.renderer.framebuffer.FramebufferUtils; | ||||||
| import electrosphere.renderer.framebuffer.Renderbuffer; | import electrosphere.renderer.framebuffer.Renderbuffer; | ||||||
|  | import electrosphere.renderer.texture.Texture; | ||||||
| import electrosphere.renderer.ui.Widget; | import electrosphere.renderer.ui.Widget; | ||||||
| import org.joml.Matrix4f; | import org.joml.Matrix4f; | ||||||
| import org.joml.Vector3f; | import org.joml.Vector3f; | ||||||
| @ -403,7 +404,10 @@ public class RenderingEngine { | |||||||
|         //render full screen quad |         //render full screen quad | ||||||
|         glUseProgram(screenTextureShaders.shaderProgram); |         glUseProgram(screenTextureShaders.shaderProgram); | ||||||
|         glBindVertexArray(screenTextureVAO); |         glBindVertexArray(screenTextureVAO); | ||||||
|         Globals.blackTexture.bind(); |         Texture blackTexture = Globals.assetManager.fetchTexture(Globals.blackTexture); | ||||||
|  |         if(blackTexture != null){ | ||||||
|  |             blackTexture.bind(); | ||||||
|  |         } | ||||||
| //        glBindTexture(GL_TEXTURE_2D, screenFramebuffer.getTexture()); | //        glBindTexture(GL_TEXTURE_2D, screenFramebuffer.getTexture()); | ||||||
|         glDrawArrays(GL_TRIANGLES, 0, 6); |         glDrawArrays(GL_TRIANGLES, 0, 6); | ||||||
|         glBindVertexArray(0); |         glBindVertexArray(0); | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| package electrosphere.renderer.assetmanager; | package electrosphere.renderer.assetmanager; | ||||||
| 
 | 
 | ||||||
| import electrosphere.renderer.Model; | import electrosphere.renderer.Model; | ||||||
|  | import electrosphere.renderer.texture.Texture; | ||||||
| import electrosphere.util.ModelLoader; | import electrosphere.util.ModelLoader; | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.LinkedList; | import java.util.LinkedList; | ||||||
| @ -16,14 +17,20 @@ import java.util.concurrent.CopyOnWriteArrayList; | |||||||
| public class AssetManager { | public class AssetManager { | ||||||
|      |      | ||||||
|     ConcurrentHashMap<String,Model> modelsLoadedIntoMemory = new ConcurrentHashMap(); |     ConcurrentHashMap<String,Model> modelsLoadedIntoMemory = new ConcurrentHashMap(); | ||||||
|      |  | ||||||
|     CopyOnWriteArrayList<String> modelsInQueue = new CopyOnWriteArrayList(); |     CopyOnWriteArrayList<String> modelsInQueue = new CopyOnWriteArrayList(); | ||||||
|      |      | ||||||
|  |     ConcurrentHashMap<String,Texture> texturesLoadedIntoMemory = new ConcurrentHashMap(); | ||||||
|  |     CopyOnWriteArrayList<String> texturesInQueue = new CopyOnWriteArrayList(); | ||||||
|  |      | ||||||
|     public void loadAssetsInQueue(){ |     public void loadAssetsInQueue(){ | ||||||
|         for(String currentPath : modelsInQueue){ |         for(String currentPath : modelsInQueue){ | ||||||
|             modelsInQueue.remove(currentPath); |             modelsInQueue.remove(currentPath); | ||||||
|             modelsLoadedIntoMemory.put(currentPath, ModelLoader.load_Model_From_File(currentPath)); |             modelsLoadedIntoMemory.put(currentPath, ModelLoader.load_Model_From_File(currentPath)); | ||||||
|         } |         } | ||||||
|  |         for(String currentPath : texturesInQueue){ | ||||||
|  |             texturesInQueue.remove(currentPath); | ||||||
|  |             texturesLoadedIntoMemory.put(currentPath, new Texture(currentPath)); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public void addModelPathToQueue(String path){ |     public void addModelPathToQueue(String path){ | ||||||
| @ -55,4 +62,26 @@ public class AssetManager { | |||||||
|     public void registerModelToSpecificString(Model m, String s){ |     public void registerModelToSpecificString(Model m, String s){ | ||||||
|         modelsLoadedIntoMemory.put(s,m); |         modelsLoadedIntoMemory.put(s,m); | ||||||
|     } |     } | ||||||
|  |      | ||||||
|  |     public void addTexturePathtoQueue(String path){ | ||||||
|  |         if(!texturesInQueue.contains(path) && !texturesLoadedIntoMemory.containsKey(path)){ | ||||||
|  |             texturesInQueue.add(path); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public Texture fetchTexture(String path){ | ||||||
|  |         Texture rVal = null; | ||||||
|  |         if(texturesLoadedIntoMemory.containsKey(path)){ | ||||||
|  |             rVal = texturesLoadedIntoMemory.get(path); | ||||||
|  |         } | ||||||
|  |         return rVal; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public String registerTexture(Texture t){ | ||||||
|  |         String rVal; | ||||||
|  |         UUID newUUID = UUID.randomUUID(); | ||||||
|  |         rVal = newUUID.toString(); | ||||||
|  |         texturesLoadedIntoMemory.put(rVal,t); | ||||||
|  |         return rVal; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -67,17 +67,19 @@ public class ModelLoader { | |||||||
|                     String diffuse_path = TextureMap.get_diffuse_path(texture_path_list); |                     String diffuse_path = TextureMap.get_diffuse_path(texture_path_list); | ||||||
|                     if(diffuse_path != null){ |                     if(diffuse_path != null){ | ||||||
|                         LoggerInterface.loggerRenderer.DEBUG(diffuse_path); |                         LoggerInterface.loggerRenderer.DEBUG(diffuse_path); | ||||||
|                         Texture diffuse = new Texture(diffuse_path); | //                        Texture diffuse = new Texture(diffuse_path); | ||||||
|                         final_material.set_diffuse(diffuse); |                         Globals.assetManager.addTexturePathtoQueue(diffuse_path); | ||||||
|                         LoggerInterface.loggerRenderer.DEBUG(diffuse.toString()); |                         final_material.set_diffuse(diffuse_path); | ||||||
|  |                         LoggerInterface.loggerRenderer.DEBUG(diffuse_path); | ||||||
|                     } else { |                     } else { | ||||||
|                         final_material.set_diffuse(Globals.textureDiffuseDefault); |                         final_material.set_diffuse(Globals.textureDiffuseDefault); | ||||||
|                     } |                     } | ||||||
|                     String specular_path = TextureMap.get_specular_path(texture_path_list); |                     String specular_path = TextureMap.get_specular_path(texture_path_list); | ||||||
|                     if(specular_path != null){ |                     if(specular_path != null){ | ||||||
|                         Texture specular = new Texture(specular_path); | //                        Texture specular = new Texture(specular_path); | ||||||
|                         final_material.set_specular(specular); |                         Globals.assetManager.addTexturePathtoQueue(specular_path); | ||||||
|                         LoggerInterface.loggerRenderer.DEBUG(specular.toString()); |                         final_material.set_specular(specular_path); | ||||||
|  |                         LoggerInterface.loggerRenderer.DEBUG(specular_path); | ||||||
|                     } else { |                     } else { | ||||||
|                         final_material.set_specular(Globals.textureSpecularDefault); |                         final_material.set_specular(Globals.textureSpecularDefault); | ||||||
|                     } |                     } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user