Added Format loader into Engine

This commit is contained in:
Speiger 2021-10-01 11:53:15 +02:00
parent d7164fb152
commit 94f5c178d2
3 changed files with 195 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package speiger.src.coreengine.rendering.models; package speiger.src.coreengine.rendering.models;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map; import java.util.Map;
import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap; import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap;
@ -38,6 +40,36 @@ public enum DataType
return this == FLOAT || this == DOUBLE; return this == FLOAT || this == DOUBLE;
} }
public void putIntoBuffer(ByteBuffer buffer, List<Number> data, int offset, int size)
{
for(int i = 0;i<size;putIntoBuffer(buffer, data.get(offset+i)),i++);
}
public void putIntoBuffer(ByteBuffer buffer, Number data)
{
switch(this)
{
case BYTE:
buffer.put(data.byteValue());
break;
case SHORT:
buffer.putShort(data.shortValue());
break;
case INT:
buffer.putInt(data.intValue());
break;
case LONG:
buffer.putLong(data.longValue());
break;
case FLOAT:
buffer.putFloat(data.floatValue());
break;
case DOUBLE:
buffer.putDouble(data.doubleValue());
break;
}
}
public static DataType byID(String id) public static DataType byID(String id)
{ {
return BY_ID.get(id); return BY_ID.get(id);

View File

@ -0,0 +1,78 @@
package speiger.src.coreengine.rendering.models.loader;
import java.util.List;
import com.google.gson.JsonObject;
import speiger.src.coreengine.rendering.models.GLDataType;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class VertexEntry
{
String name;
int size;
GLDataType glType;
boolean optional;
public VertexEntry(JsonObject obj)
{
this(obj.get("type").getAsString(), obj.get("length").getAsInt(), GLDataType.byName(obj.get("glType").getAsString()), JsonUtil.getOrDefault(obj, "optional", false));
}
public VertexEntry(String name, int size, GLDataType glType)
{
this(name, size, glType, false);
}
public VertexEntry(String name, int size, GLDataType glType, boolean optional)
{
this.name = name;
this.size = size;
this.glType = glType;
this.optional = optional;
}
public String getName()
{
return name;
}
public int getSize()
{
return size;
}
public GLDataType getType()
{
return glType;
}
public boolean isOptional()
{
return optional;
}
public static int calculateStride(List<VertexEntry> array, boolean excludeOptional)
{
return excludeOptional ? calculateNonOptionalStride(array) : caclulateStride(array);
}
public static int calculateNonOptionalStride(List<VertexEntry> array)
{
int size = 0;
for(int i = 0,m=array.size();i<m;i++)
{
VertexEntry entry = array.get(i);
if(entry.isOptional()) continue;
size += entry.getSize();
}
return size;
}
public static int caclulateStride(List<VertexEntry> array)
{
int size = 0;
for(int i = 0,m=array.size();i<m;size+=array.get(i++).getSize());
return size;
}
}

View File

@ -0,0 +1,85 @@
package speiger.src.coreengine.rendering.models.loader;
import java.nio.ByteBuffer;
import java.util.List;
import com.google.gson.JsonObject;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class VertexLoader
{
public static List<VertexEntry> loadVertexFormat(JsonObject obj)
{
List<VertexEntry> list = new ObjectArrayList<>();
JsonUtil.iterate(obj.get("dataOrder"), T -> list.add(new VertexEntry(T)));
return list;
}
public static ByteBuffer parseVertexData(JsonObject obj, int vertexCount, List<VertexEntry> entries, boolean excludeOptional)
{
List<Number> numbers = parseVertexData(obj, entries, excludeOptional);
ByteBuffer buffer = ByteBuffer.allocate(vertexCount * VertexEntry.calculateStride(entries, excludeOptional));
for(int i = 0,offset=0,m=vertexCount*entries.size();i<m;i++)
{
VertexEntry entry = entries.get(i % entries.size());
if(entry.isOptional() && excludeOptional) continue;
entry.getType().getDataType().putIntoBuffer(buffer, numbers, offset, entry.getSize());
offset+=entry.getSize();
}
return buffer;
}
public static List<Number> parseVertexData(JsonObject obj, List<VertexEntry> entries, boolean excludeOptional)
{
int stride = VertexEntry.caclulateStride(entries);
int outStride = excludeOptional ? VertexEntry.calculateNonOptionalStride(entries) : stride;
int[] vertexes = JsonUtil.parseIntArray(obj.getAsJsonArray("vertexes"));
Number[] data = new Number[excludeOptional ? (vertexes.length / stride) * outStride : vertexes.length];
for(int i = 0,inOff=0,outOff=0,m=entries.size();i<m;i++)
{
VertexEntry entry = entries.get(i);
if(!entry.isOptional() || !excludeOptional)
{
deserializeIndecies(vertexes, JsonUtil.parseArray(obj.getAsJsonArray(entry.getName())), entry.getSize(), stride, inOff, data, outStride, outOff);
outOff += entry.getSize();
}
inOff += entry.getSize();
}
return ObjectArrayList.wrap(data);
}
public static Object2ObjectMap<String, List<Number>> parseMappedVertexData(JsonObject obj, List<VertexEntry> entries, boolean excludeOptional)
{
Object2ObjectMap<String, List<Number>> mappedData = Object2ObjectMap.createLinkedMap();
int stride = VertexEntry.caclulateStride(entries);
int[] vertexes = JsonUtil.parseIntArray(obj.getAsJsonArray("vertexes"));
for(int i = 0,offset=0,m=entries.size();i<m;i++)
{
VertexEntry entry = entries.get(i);
if(!entry.isOptional() || !excludeOptional) mappedData.put(entry.getName(), deserializeIndecies(vertexes, JsonUtil.parseArray(obj.getAsJsonArray(entry.getName())), entry.getSize(), stride, offset));
offset += entry.getSize();
}
return mappedData;
}
public static <T> void deserializeIndecies(int[] indecies, T[] in, int size, int stride, int offset, T[] out, int outStride, int outOffset)
{
for(int i = 0,m=(indecies.length/stride)*size;i<m;i++)
{
out[outOffset + ((i/size) * outStride) + (i % size)] = in[indecies[offset + ((i/size) * stride) + (i % size)]];
}
}
public static <T> List<T> deserializeIndecies(int[] indecies, T[] in, int size, int stride, int offset)
{
List<T> out = new ObjectArrayList<>();
for(int i = 0,m=(indecies.length/stride)*size;i<m;i++)
{
out.add(in[indecies[offset + ((i/size) * stride) + (i % size)]]);
}
return out;
}
}