From bb1ff2e9e5a0c9cbdb7e0a8950e36c9396b13365 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Sun, 15 Jan 2012 00:17:03 +0000 Subject: [PATCH] - improve atlas test, display quad with atlas texture - clamp texture coordinates to 0-1 for atlas textures to better see actual texture mapping git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9035 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../test/jme3test/tools/TestTextureAtlas.java | 21 +++++++----- .../optimize/GeometryBatchFactory.java | 4 +-- .../jme3tools/optimize/TextureAtlas.java | 32 ++++++++++++++----- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/engine/src/test/jme3test/tools/TestTextureAtlas.java b/engine/src/test/jme3test/tools/TestTextureAtlas.java index 40bb20495..77a95a214 100644 --- a/engine/src/test/jme3test/tools/TestTextureAtlas.java +++ b/engine/src/test/jme3test/tools/TestTextureAtlas.java @@ -29,10 +29,8 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package jme3test.tools; -import jme3test.model.shape.*; import com.jme3.app.SimpleApplication; import com.jme3.asset.plugins.ZipLocator; import com.jme3.light.AmbientLight; @@ -41,22 +39,24 @@ import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.Spatial; +import com.jme3.scene.shape.Quad; import jme3tools.optimize.GeometryBatchFactory; public class TestTextureAtlas extends SimpleApplication { - public static void main(String[] args){ + public static void main(String[] args) { TestTextureAtlas app = new TestTextureAtlas(); app.start(); } @Override public void simpleInitApp() { - assetManager.registerLocator("wildhouse.zip", ZipLocator.class.getName()); + flyCam.setMoveSpeed(50); + assetManager.registerLocator("town.zip", ZipLocator.class.getName()); Spatial scene = assetManager.loadModel("main.scene"); - - Geometry geom = GeometryBatchFactory.makeAtlasBatch(scene, assetManager, 4096); - + + Geometry geom = GeometryBatchFactory.makeAtlasBatch(scene, assetManager, 2048); + AmbientLight al = new AmbientLight(); rootNode.addLight(al); @@ -66,6 +66,11 @@ public class TestTextureAtlas extends SimpleApplication { rootNode.addLight(sun); rootNode.attachChild(geom); - } + //quad to display material + Geometry box = new Geometry("displayquad", new Quad(4, 4)); + box.setMaterial(geom.getMaterial()); + box.setLocalTranslation(0, 1, 0); + rootNode.attachChild(box); + } } diff --git a/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java b/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java index d8c395aa4..96a07bd55 100644 --- a/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java +++ b/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java @@ -352,9 +352,6 @@ public class GeometryBatchFactory { Geometry geom = new Geometry(); Mesh mesh = new Mesh(); mergeGeometries(geometries, mesh, atlas); - TangentBinormalGenerator.generate(mesh); - mesh.updateCounts(); - mesh.updateBound(); geom.setMesh(mesh); Material mat = new Material(mgr, "Common/MatDefs/Light/Lighting.j3md"); @@ -365,6 +362,7 @@ public class GeometryBatchFactory { mat.setTexture("DiffuseMap", diffuseMap); } if (normalMap != null) { + TangentBinormalGenerator.generate(mesh); mat.setTexture("NormalMap", normalMap); } if (specularMap != null) { diff --git a/engine/src/tools/jme3tools/optimize/TextureAtlas.java b/engine/src/tools/jme3tools/optimize/TextureAtlas.java index 7fd37b846..8251aac15 100644 --- a/engine/src/tools/jme3tools/optimize/TextureAtlas.java +++ b/engine/src/tools/jme3tools/optimize/TextureAtlas.java @@ -151,7 +151,6 @@ public class TextureAtlas { } private void drawImage(Image source, int x, int y, String mapName) { - //TODO: all buffers? if (images == null) { images = new HashMap(); } @@ -160,6 +159,7 @@ public class TextureAtlas { image = new byte[atlasWidth * atlasHeight * 4]; images.put(mapName, image); } + //TODO: all buffers? ByteBuffer sourceData = source.getData(0); int height = source.getHeight(); int width = source.getWidth(); @@ -174,13 +174,13 @@ public class TextureAtlas { image[i + 3] = sourceData.get(j + 3); //r } else if (source.getFormat() == Format.BGR8) { int j = (xPos + yPos * width) * 3; - image[i] = 0; //b + image[i] = 0; //a image[i + 1] = sourceData.get(j); //b image[i + 2] = sourceData.get(j + 1); //g image[i + 3] = sourceData.get(j + 2); //r } else if (source.getFormat() == Format.RGB8) { int j = (xPos + yPos * width) * 3; - image[i] = 0; //b + image[i] = 0; //a image[i + 1] = sourceData.get(j + 2); //b image[i + 2] = sourceData.get(j + 1); //g image[i + 3] = sourceData.get(j); //r @@ -322,8 +322,23 @@ public class TextureAtlas { float w = (float) getWidth() / (float) atlasWidth; float h = (float) getHeight() / (float) atlasHeight; Vector2f location = new Vector2f(x, y); - Vector2f scale = new Vector2f(w, h); - return location.addLocal(previousLocation.multLocal(scale)); + float prevX = previousLocation.x; + float prevY = previousLocation.y; + //TODO: remove workaround for texture wrap + if (prevX > 1) { + prevX = 1; + } + if (prevY > 1) { + prevY = 1; + } + if (prevX < 0) { + prevX = 0; + } + if (prevY < 0) { + prevY = 0; + } + location.addLocal(prevX * w, prevY * h); + return location; } /** @@ -342,9 +357,10 @@ public class TextureAtlas { for (int i = 0; i < inBuf.capacity() / 2; i++) { tex.x = inBuf.get(i * 2 + 0); tex.y = inBuf.get(i * 2 + 1); - Vector2f outLoc = getLocation(tex); - outBuf.put(offset + i * 2 + 0, outLoc.x); - outBuf.put(offset + i * 2 + 1, outLoc.y); + Vector2f location = getLocation(tex); + //TODO: replace with proper texture wrapping for atlases.. + outBuf.put(offset + i * 2 + 0, location.x); + outBuf.put(offset + i * 2 + 1, location.y); } }