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