diff --git a/ummisco.gama.annotations/src/ummisco/gama/dev/utils/DEBUG.java b/ummisco.gama.annotations/src/ummisco/gama/dev/utils/DEBUG.java index 426442e1ed..e8350d8076 100644 --- a/ummisco.gama.annotations/src/ummisco/gama/dev/utils/DEBUG.java +++ b/ummisco.gama.annotations/src/ummisco/gama/dev/utils/DEBUG.java @@ -464,6 +464,7 @@ public static String CALLER() { * Stack. */ public static void STACK() { + if (!ENABLE_LOGGING || !IS_ON(findCallingClassName())) return; LOG(PAD("--- Stack trace ", 80, '-')); STACK_WALKER.walk(stream1 -> { stream1.skip(2).forEach(s -> LOG("> " + s)); diff --git a/ummisco.gama.opengl/src/ummisco/gama/opengl/files/GamaObjFile.java b/ummisco.gama.opengl/src/ummisco/gama/opengl/files/GamaObjFile.java index 9017e305b1..1ce67bc376 100644 --- a/ummisco.gama.opengl/src/ummisco/gama/opengl/files/GamaObjFile.java +++ b/ummisco.gama.opengl/src/ummisco/gama/opengl/files/GamaObjFile.java @@ -1,12 +1,12 @@ /******************************************************************************************************* * - * GamaObjFile.java, in ummisco.gama.opengl, is part of the source code of the - * GAMA modeling and simulation platform (v.1.8.2). + * GamaObjFile.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation platform + * (v.1.8.2). * * (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. - * + * ********************************************************************************************************/ package ummisco.gama.opengl.files; @@ -19,6 +19,7 @@ import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2; +import com.jogamp.opengl.GL2ES3; import com.jogamp.opengl.util.texture.Texture; import msi.gama.common.geometry.Envelope3D; @@ -58,50 +59,50 @@ public class GamaObjFile extends Gama3DGeometryFile { /** The set of vertex. */ public final ArrayList setOfVertex = new ArrayList<>(); - + /** The set of vertex normals. */ private final ArrayList setOfVertexNormals = new ArrayList<>(); - + /** The set of vertex textures. */ private final ArrayList setOfVertexTextures = new ArrayList<>(); - + /** The faces. */ private final ArrayList faces = new ArrayList<>(); - + /** The faces texs. */ private final ArrayList facesTexs = new ArrayList<>(); - + /** The faces norms. */ private final ArrayList facesNorms = new ArrayList<>(); - + /** The mat timings. */ private final ArrayList matTimings = new ArrayList<>(); - + /** The materials. */ private MtlLoader materials; // private int objectList; /** The toppoint. */ // private int numPolys = 0; public double toppoint = 0f; - + /** The bottompoint. */ public double bottompoint = 0f; - + /** The leftpoint. */ public double leftpoint = 0f; - + /** The rightpoint. */ public double rightpoint = 0f; - + /** The farpoint. */ public double farpoint = 0f; - + /** The nearpoint. */ public double nearpoint = 0f; - + /** The mtl path. */ private final String mtlPath; - + /** The loaded. */ boolean loaded = false; @@ -123,10 +124,14 @@ public GamaObjFile(final IScope scope, final String pathName) throws GamaRuntime /** * Instantiates a new gama obj file. * - * @param scope the scope - * @param pathName the path name - * @param initRotation the init rotation - * @throws GamaRuntimeException the gama runtime exception + * @param scope + * the scope + * @param pathName + * the path name + * @param initRotation + * the init rotation + * @throws GamaRuntimeException + * the gama runtime exception */ @doc ( value = "This file constructor allows to read an obj file and apply an init rotation to it. The rotation" @@ -143,9 +148,12 @@ public GamaObjFile(final IScope scope, final String pathName, final GamaPair rightpoint) { - rightpoint = coords[0]; - } - if (coords[0] < leftpoint) { - leftpoint = coords[0]; - } - if (coords[1] > toppoint) { - toppoint = coords[1]; - } - if (coords[1] < bottompoint) { - bottompoint = coords[1]; - } - if (coords[2] > nearpoint) { - nearpoint = coords[2]; - } - if (coords[2] < farpoint) { - farpoint = coords[2]; - } + if (coords[0] > rightpoint) { rightpoint = coords[0]; } + if (coords[0] < leftpoint) { leftpoint = coords[0]; } + if (coords[1] > toppoint) { toppoint = coords[1]; } + if (coords[1] < bottompoint) { bottompoint = coords[1]; } + if (coords[2] > nearpoint) { nearpoint = coords[2]; } + if (coords[2] < farpoint) { farpoint = coords[2]; } setOfVertex.add(coords); } else @@ -275,11 +277,9 @@ public void loadObject(final BufferedReader br) throws IOException { if (newline.startsWith("vt")) { final double coords[] = new double[4]; // final String coordstext[] = new String[4]; - newline = newline.substring(3, newline.length()); + newline = newline.substring(3); final StringTokenizer st = new StringTokenizer(newline, " "); - for (int i = 0; st.hasMoreTokens(); i++) { - coords[i] = Double.parseDouble(st.nextToken()); - } + for (int i = 0; st.hasMoreTokens(); i++) { coords[i] = Double.parseDouble(st.nextToken()); } setOfVertexTextures.add(coords); } else @@ -288,19 +288,15 @@ public void loadObject(final BufferedReader br) throws IOException { if (newline.startsWith("vn")) { final double coords[] = new double[4]; // final String coordstext[] = new String[4]; - newline = newline.substring(3, newline.length()); + newline = newline.substring(3); final StringTokenizer st = new StringTokenizer(newline, " "); - for (int i = 0; st.hasMoreTokens(); i++) { - coords[i] = Double.parseDouble(st.nextToken()); - } - + for (int i = 0; st.hasMoreTokens(); i++) { coords[i] = Double.parseDouble(st.nextToken()); } setOfVertexNormals.add(coords); } else - // LOADS FACES COORDINATES if (newline.startsWith("f ")) { facecounter++; - newline = newline.substring(2, newline.length()); + newline = newline.substring(2); final StringTokenizer st = new StringTokenizer(newline, " "); final int count = st.countTokens(); final int v[] = new int[count]; @@ -308,12 +304,10 @@ public void loadObject(final BufferedReader br) throws IOException { final int vn[] = new int[count]; for (int i = 0; i < count; i++) { final char chars[] = st.nextToken().toCharArray(); - final StringBuffer sb = new StringBuffer(); + final StringBuilder sb = new StringBuilder(); char lc = 'x'; for (final char c : chars) { - if (c == '/' && lc == '/') { - sb.append('0'); - } + if (c == '/' && lc == '/') { sb.append('0'); } lc = c; sb.append(lc); } @@ -343,9 +337,7 @@ public void loadObject(final BufferedReader br) throws IOException { && newline.charAt(3) == 'l' && newline.charAt(4) == 'i' && newline.charAt(5) == 'b') { // String[] coordstext = new String[3]; // coordstext = newline.split("\\s+"); - if (mtlPath != null) { - loadMaterials(); - } + if (mtlPath != null) { loadMaterials(); } } else // USES MATERIALS @@ -448,22 +440,20 @@ public void drawToOpenGL(final OpenGL gl) { final String mapd = materials.getMapd(nextmatname); if (mapKa != null || mapKd != null || mapd != null) { File f = new File(mtlPath); - String path = f.getAbsolutePath().replace(f.getName(), ""); + StringBuilder path = new StringBuilder().append(f.getAbsolutePath().replace(f.getName(), "")); if (mapd != null) { - path += mapd; + path.append(mapd); } else if (mapKa != null) { - path += mapKa; - } else if (mapKd != null) { - path += mapKd; - } - f = new File(path); + path.append(mapKa); + } else if (mapKd != null) { path.append(mapKd); } + f = new File(path.toString()); if (f.exists()) { // Solves Issue #1951. Asynchronous loading of textures // was not possible when displaying the file texture = gl.getTexture(f, false, true); gl.setCurrentTextures(texture.getTextureObject(), texture.getTextureObject()); - texture.setTexParameteri(gl.getGL(), GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT); - texture.setTexParameteri(gl.getGL(), GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT); + texture.setTexParameteri(gl.getGL(), GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT); + texture.setTexParameteri(gl.getGL(), GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT); } } @@ -481,7 +471,7 @@ public void drawToOpenGL(final OpenGL gl) { //// Quad Begin Header //// final int polytype = - tempfaces.length == 3 ? GL.GL_TRIANGLES : tempfaces.length == 4 ? GL2.GL_QUADS : GL2.GL_POLYGON; + tempfaces.length == 3 ? GL.GL_TRIANGLES : tempfaces.length == 4 ? GL2ES3.GL_QUADS : GL2.GL_POLYGON; gl.beginDrawing(polytype); //////////////////////////// @@ -496,21 +486,15 @@ public void drawToOpenGL(final OpenGL gl) { final double[] arrayOfVertices = new double[tempfaces.length * 3]; for (int w = 0; w < tempfaces.length; w++) { final double[] ordinates = setOfVertex.get(tempfaces[w] - 1); - for (int k = 0; k < 3; k++) { - arrayOfVertices[w * 3 + k] = ordinates[k]; - } + for (int k = 0; k < 3; k++) { arrayOfVertices[w * 3 + k] = ordinates[k]; } } final ICoordinates coords = ICoordinates.ofLength(tempfaces.length + 1); coords.setTo(arrayOfVertices); coords.completeRing(); - if (!hasNormals) { - gl.setNormal(coords, !coords.isClockwise()); - } + if (!hasNormals) { gl.setNormal(coords, !coords.isClockwise()); } for (int w = 0; w < tempfaces.length; w++) { - if (tempfaces[w] == 0) { - continue; - } + if (tempfaces[w] == 0) { continue; } final boolean hasNormal = norms[w] != 0; final boolean hasTex = texs[w] != 0; if (hasNormal) { @@ -537,6 +521,7 @@ public void drawToOpenGL(final OpenGL gl) { if (texture != null) { gl.disableTextures(); + texture.destroy(gl.getGL()); texture = null; } // gl.glEndList(); diff --git a/ummisco.gama.opengl/src/ummisco/gama/opengl/renderer/caches/TextureCache2.java b/ummisco.gama.opengl/src/ummisco/gama/opengl/renderer/caches/TextureCache2.java index 66a6bde1a3..7a44bf7408 100644 --- a/ummisco.gama.opengl/src/ummisco/gama/opengl/renderer/caches/TextureCache2.java +++ b/ummisco.gama.opengl/src/ummisco/gama/opengl/renderer/caches/TextureCache2.java @@ -1,27 +1,26 @@ /******************************************************************************************************* * - * TextureCache2.java, in ummisco.gama.opengl, is part of the source code of the - * GAMA modeling and simulation platform (v.1.8.2). + * TextureCache2.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and simulation platform + * (v.1.8.2). * * (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. - * + * ********************************************************************************************************/ package ummisco.gama.opengl.renderer.caches; import java.awt.image.BufferedImage; import java.io.File; -import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import com.jogamp.opengl.GL; import com.jogamp.opengl.GLException; import com.jogamp.opengl.util.texture.Texture; @@ -40,39 +39,34 @@ public class TextureCache2 implements ITextureCache { static { - DEBUG.OFF(); + DEBUG.ON(); } /** The volatile textures. */ - private final LoadingCache volatileTextures; - + private Map volatileTextures; + /** The static textures. */ private final Cache staticTextures = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.SECONDS).build(); - + /** The textures to process. */ final List texturesToProcess = new CopyOnWriteArrayList<>(); - + /** The gl. */ final OpenGL gl; - + /** The is non power of 2 textures available. */ Boolean isNonPowerOf2TexturesAvailable; /** * Instantiates a new texture cache 2. * - * @param gl the gl + * @param gl + * the gl */ public TextureCache2(final OpenGL gl) { this.gl = gl; - volatileTextures = CacheBuilder.newBuilder().build(new CacheLoader() { - - @Override - public Texture load(final BufferedImage key) throws Exception { - return buildTexture(gl.getGL(), key); - } - }); + volatileTextures = new HashMap<>(); } /* @@ -101,9 +95,8 @@ public void initialize() { */ @Override public void deleteVolatileTextures() { - final Collection textures = volatileTextures.asMap().values(); - for (final Texture t : textures) { t.destroy(gl.getGL()); } - volatileTextures.invalidateAll(); + volatileTextures.forEach((s, t) -> t.destroy(gl.getGL())); + volatileTextures = new HashMap<>(); } /* @@ -146,7 +139,13 @@ public void processUnloaded() { */ @Override public Texture getTexture(final BufferedImage img) { - return volatileTextures.getUnchecked(img); + String id = String.valueOf(img.hashCode()); + Texture texture = volatileTextures.get(id); + if (texture == null) { + texture = this.buildTexture(gl.getGL(), img); + volatileTextures.put(id, texture); + } + return texture; } @Override @@ -154,10 +153,14 @@ public Texture getTexture(final File file, final boolean isAnimated, final boole if (file == null) return null; Texture texture = null; if (isAnimated || !useCache) { - final BufferedImage image = ImageUtils.getInstance().getImageFromFile(file, useCache, true, null - /* new ProgressCounter(GAMA.getRuntimeScope(), file.getName()) */); - texture = volatileTextures.getUnchecked(image); - + String path = file.getAbsolutePath(); + texture = volatileTextures.get(path); + if (texture == null) { + final BufferedImage image = ImageUtils.getInstance().getImageFromFile(file, useCache, true, null); + // DEBUG.OUT("Building a new volatile texture... "); + texture = this.buildTexture(gl.getGL(), image); + volatileTextures.put(path, texture); + } } else { try { texture = staticTextures.get(file.getAbsolutePath(), () -> buildTexture(gl.getGL(), file)); @@ -171,38 +174,24 @@ public Texture getTexture(final File file, final boolean isAnimated, final boole /** * Builds the texture. * - * @param gl the gl - * @param file the file + * @param gl + * the gl + * @param file + * the file * @return the texture */ private Texture buildTexture(final GL gl, final File file) { - return buildTexture(gl, ImageUtils.getInstance().getImageFromFile(file, - GamaPreferences.Displays.OPENGL_USE_IMAGE_CACHE.getValue(), true, null - /* new ProgressCounter(GAMA.getRuntimeScope(), file.getName()) */)); - - // try { - // - // // final TextureData data = TextureIO.newTextureData(gl.getGLProfile(), file, true, null); - // // final Texture texture = new Texture(gl, data); - // // texture.setMustFlipVertically(false); - // // data.flush(); - // // return texture; - // Texture t = TextureIO.newTexture(file, true); - // t.setMustFlipVertically(true); - // - // return t; - // } catch (final GLException | IOException e) { - // e.printStackTrace(); - // return null; - // } + GamaPreferences.Displays.OPENGL_USE_IMAGE_CACHE.getValue(), true, null)); } /** * Builds the texture. * - * @param gl the gl - * @param im the im + * @param gl + * the gl + * @param im + * the im * @return the texture */ Texture buildTexture(final GL gl, final BufferedImage im) { diff --git a/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/displays/LayeredDisplayDecorator.java b/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/displays/LayeredDisplayDecorator.java index fb09e06e75..d871b2ab1a 100644 --- a/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/displays/LayeredDisplayDecorator.java +++ b/ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/displays/LayeredDisplayDecorator.java @@ -64,7 +64,7 @@ public class LayeredDisplayDecorator implements DisplayDataListener { static { - DEBUG.ON(); + DEBUG.OFF(); } /** The key and mouse listener. */ diff --git a/ummisco.gama.ui.shared/src/ummisco/gama/ui/utils/SwtGui.java b/ummisco.gama.ui.shared/src/ummisco/gama/ui/utils/SwtGui.java index 1ee94af168..a0398be56e 100644 --- a/ummisco.gama.ui.shared/src/ummisco/gama/ui/utils/SwtGui.java +++ b/ummisco.gama.ui.shared/src/ummisco/gama/ui/utils/SwtGui.java @@ -98,7 +98,7 @@ public class SwtGui implements IGui { static { - DEBUG.ON(); + DEBUG.OFF(); } /** The all tests running. */