Просмотр исходного кода

* Blender loader now writes color as a normalized byte buffer, reduces mem usage by 4 times

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9015 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
Sha..rd 14 лет назад
Родитель
Сommit
3695f8f1fc

+ 23 - 0
engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java

@@ -38,6 +38,7 @@ import com.jme3.scene.plugins.blender.file.Pointer;
 import com.jme3.scene.plugins.blender.file.Structure;
 import com.jme3.scene.plugins.blender.objects.Properties;
 import com.jme3.util.BufferUtils;
+import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.util.List;
 
@@ -94,6 +95,28 @@ public abstract class AbstractBlenderHelper {
 		return true;
 	}
 
+        /**
+	 * Generate a new ByteBuffer using the given array of byte[4] objects. The ByteBuffer will be 4 * data.length
+	 * long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc.
+	 * @param data
+	 *        list of byte[4] objects to place into a new ByteBuffer
+	 */
+	protected ByteBuffer createByteBuffer(List<byte[]> data) {
+		if (data == null) {
+			return null;
+		}
+		ByteBuffer buff = BufferUtils.createByteBuffer(4 * data.size());
+		for (byte[] v : data) {
+			if (v != null) {
+				buff.put(v[0]).put(v[1]).put(v[2]).put(v[3]);
+			} else {
+				buff.put((byte)0).put((byte)0).put((byte)0).put((byte)0);
+			}
+		}
+		buff.flip();
+		return buff;
+	}
+        
 	/**
 	 * Generate a new FloatBuffer using the given array of float[4] objects. The FloatBuffer will be 4 * data.length
 	 * long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc.

+ 14 - 11
engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java

@@ -57,6 +57,7 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper;
 import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator;
 import com.jme3.texture.Texture;
 import com.jme3.util.BufferUtils;
+import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.util.*;
 import java.util.Map.Entry;
@@ -113,7 +114,7 @@ public class MeshHelper extends AbstractBlenderHelper {
         int verticesAmount = vertices.length;
 
         // vertices Colors
-        List<float[]> verticesColors = this.getVerticesColors(structure, blenderContext);
+        List<byte[]> verticesColors = this.getVerticesColors(structure, blenderContext);
 
         // reading faces
         // the following map sorts faces by material number (because in jme Mesh can have only one material)
@@ -297,7 +298,8 @@ public class MeshHelper extends AbstractBlenderHelper {
         Properties properties = this.loadProperties(structure, blenderContext);
 
         // generating meshes
-        FloatBuffer verticesColorsBuffer = this.createFloatBuffer(verticesColors);
+        //FloatBuffer verticesColorsBuffer = this.createFloatBuffer(verticesColors);
+        ByteBuffer verticesColorsBuffer = createByteBuffer(verticesColors);
         for (Entry<Integer, List<Integer>> meshEntry : meshesMap.entrySet()) {
             Mesh mesh = new Mesh();
 
@@ -323,6 +325,7 @@ public class MeshHelper extends AbstractBlenderHelper {
             // setting vertices colors
             if (verticesColorsBuffer != null) {
                 mesh.setBuffer(Type.Color, 4, verticesColorsBuffer);
+                mesh.getBuffer(Type.Color).setNormalized(true);
             }
 
             // setting faces' normals
@@ -462,7 +465,7 @@ public class MeshHelper extends AbstractBlenderHelper {
     }
 
     /**
-     * This method returns the vertices colors. Each vertex is stored in float[4] array.
+     * This method returns the vertices colors. Each vertex is stored in byte[4] array.
      * 
      * @param meshStructure
      *            the structure containing the mesh data
@@ -472,19 +475,19 @@ public class MeshHelper extends AbstractBlenderHelper {
      * @throws BlenderFileException
      *             this exception is thrown when the blend file structure is somehow invalid or corrupted
      */
-    public List<float[]> getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException {
+    public List<byte[]> getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException {
         Pointer pMCol = (Pointer) meshStructure.getFieldValue("mcol");
-        List<float[]> verticesColors = null;
+        List<byte[]> verticesColors = null;
         List<Structure> mCol = null;
         if (pMCol.isNotNull()) {
-            verticesColors = new LinkedList<float[]>();
+            verticesColors = new LinkedList<byte[]>();
             mCol = pMCol.fetchData(blenderContext.getInputStream());
             for (Structure color : mCol) {
-                float r = (((Number)color.getFieldValue("r")).byteValue() & 0xFF) / 256.0f;
-                float g = (((Number)color.getFieldValue("g")).byteValue() & 0xFF) / 256.0f;
-                float b = (((Number)color.getFieldValue("b")).byteValue() & 0xFF) / 256.0f;
-                float a = (((Number)color.getFieldValue("a")).byteValue() & 0xFF) / 256.0f;
-                verticesColors.add(new float[]{b, g, r, a});
+                byte r = ((Number)color.getFieldValue("r")).byteValue();
+                byte g = ((Number)color.getFieldValue("g")).byteValue();
+                byte b = ((Number)color.getFieldValue("b")).byteValue();
+                byte a = ((Number)color.getFieldValue("a")).byteValue();
+                verticesColors.add(new byte[]{b, g, r, a});
             }
         }
         return verticesColors;